[feat] node hosting support

This commit is contained in:
kwaroran
2023-05-27 23:15:23 +09:00
parent 4af6655471
commit 205cc2744f
61 changed files with 812 additions and 87 deletions

View File

@@ -0,0 +1,73 @@
export class NodeStorage{
async setItem(key:string, value:Uint8Array) {
const da = await fetch('/api/write', {
method: "POST",
body: JSON.stringify({
content: Buffer.from(value).toString('base64')
}),
headers: {
'content-type': 'application/json',
'file-path': Buffer.from(key, 'utf-8').toString('hex')
}
})
if(da.status < 200 || da.status >= 300){
throw "setItem Error"
}
const data = await da.json()
if(data.error){
throw data.error
}
}
async getItem(key:string):Promise<Buffer> {
const da = await fetch('/api/read', {
method: "GET",
headers: {
'file-path': Buffer.from(key, 'utf-8').toString('hex')
}
})
const data = await da.json()
if(da.status < 200 || da.status >= 300){
throw "getItem Error"
}
if(data.error){
throw data.error
}
if(data.content === null){
return null
}
return Buffer.from(data.content, 'base64')
}
async keys():Promise<string[]>{
const da = await fetch('/api/list', {
method: "GET",
})
const data = await da.json()
if(da.status < 200 || da.status >= 300){
throw "listItem Error"
}
if(data.error){
throw data.error
}
return data.content
}
async removeItem(key:string){
const da = await fetch('/api/list', {
method: "GET",
headers: {
'file-path': Buffer.from(key, 'utf-8').toString('hex')
}
})
if(da.status < 200 || da.status >= 300){
throw "removeItem Error"
}
const data = await da.json()
if(data.error){
throw data.error
}
}
listItem = this.keys
}