@peerbit/any-store-opfs 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/opfs/opfs.worker.min.js +166 -13
- package/dist/assets/opfs/opfs.worker.min.js.map +3 -3
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/opfs-store.d.ts +7 -3
- package/dist/src/opfs-store.d.ts.map +1 -1
- package/dist/src/opfs-store.js +104 -21
- package/dist/src/opfs-store.js.map +1 -1
- package/dist/src/protocol.d.ts +62 -0
- package/dist/src/protocol.d.ts.map +1 -0
- package/dist/src/protocol.js +45 -0
- package/dist/src/protocol.js.map +1 -0
- package/dist/src/worker.d.ts +3 -1
- package/dist/src/worker.d.ts.map +1 -1
- package/dist/src/worker.js +144 -8
- package/dist/src/worker.js.map +1 -1
- package/package.json +4 -4
- package/src/opfs-store.ts +147 -47
- package/src/protocol.ts +128 -0
- package/src/worker.ts +151 -15
package/dist/src/opfs-store.d.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { type AnyStore, type MaybePromise } from "@peerbit/any-store-interface";
|
|
2
2
|
import * as memory from "@peerbit/any-store-interface/messages";
|
|
3
|
+
import { type OPFSRequest, type OPFSResponse, type OPFSStoreProtocol } from "./protocol.js";
|
|
3
4
|
export declare class OPFSStore implements AnyStore {
|
|
4
5
|
readonly directory?: string;
|
|
5
6
|
worker: Worker;
|
|
6
7
|
levelMap: Map<string, AnyStore>;
|
|
7
8
|
root: AnyStore;
|
|
9
|
+
private readonly protocol;
|
|
8
10
|
private _responseCallbacks;
|
|
9
11
|
private _createStorage;
|
|
10
|
-
constructor(directory?: string
|
|
11
|
-
|
|
12
|
+
constructor(directory?: string, options?: {
|
|
13
|
+
protocol?: OPFSStoreProtocol;
|
|
14
|
+
});
|
|
15
|
+
status(): MaybePromise<"open" | "opening" | "closing" | "closed">;
|
|
12
16
|
close(): Promise<void>;
|
|
13
17
|
open(): Promise<void>;
|
|
14
18
|
get(key: string): MaybePromise<Uint8Array | undefined>;
|
|
@@ -23,7 +27,7 @@ export declare class OPFSStore implements AnyStore {
|
|
|
23
27
|
};
|
|
24
28
|
clear(): MaybePromise<void>;
|
|
25
29
|
size(): MaybePromise<number>;
|
|
26
|
-
request
|
|
30
|
+
request(request: OPFSRequest | memory.MemoryRequest): Promise<OPFSResponse | memory.MemoryRequest>;
|
|
27
31
|
persisted(): MaybePromise<boolean>;
|
|
28
32
|
}
|
|
29
33
|
//# sourceMappingURL=opfs-store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"opfs-store.d.ts","sourceRoot":"","sources":["../../src/opfs-store.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,KAAK,MAAM,MAAM,uCAAuC,CAAC;
|
|
1
|
+
{"version":3,"file":"opfs-store.d.ts","sourceRoot":"","sources":["../../src/opfs-store.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAChF,OAAO,KAAK,MAAM,MAAM,uCAAuC,CAAC;AAGhE,OAAO,EAEN,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EAEtB,MAAM,eAAe,CAAC;AA6EvB,qBAAa,SAAU,YAAW,QAAQ;IAgBxC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM;IAf5B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAChC,IAAI,EAAE,QAAQ,CAAC;IACf,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoB;IAE7C,OAAO,CAAC,kBAAkB,CAMZ;IAEd,OAAO,CAAC,cAAc,CAAgC;gBAE5C,SAAS,CAAC,EAAE,MAAM,EAC3B,OAAO,GAAE;QAAE,QAAQ,CAAC,EAAE,iBAAiB,CAAA;KAAO;IA4G/C,MAAM;IAGA,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAMtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAwB3B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAC,UAAU,GAAG,SAAS,CAAC;IAGtD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU;IAGlC,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,YAAY,CAAC,IAAI,CAAC;IAGjC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,GAAG,YAAY,CAAC,QAAQ,CAAC;IAGzD,QAAQ,IAAI;QACX,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,MAAM,aAAa,CAC1C;YAAC,MAAM;YAAE,UAAU;SAAC,EACpB,IAAI,EACJ,IAAI,CACJ,CAAC;KACF;IAGD,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC;IAI3B,IAAI,IAAI,YAAY,CAAC,MAAM,CAAC;IAItB,OAAO,CACZ,OAAO,EAAE,WAAW,GAAG,MAAM,CAAC,aAAa,GACzC,OAAO,CAAC,YAAY,GAAG,MAAM,CAAC,aAAa,CAAC;IAyB/C,SAAS;CAGT"}
|
package/dist/src/opfs-store.js
CHANGED
|
@@ -3,28 +3,52 @@ import {} from "@peerbit/any-store-interface";
|
|
|
3
3
|
import * as memory from "@peerbit/any-store-interface/messages";
|
|
4
4
|
import { v4 as uuid } from "uuid";
|
|
5
5
|
import { createWorker } from "./create.js";
|
|
6
|
-
|
|
6
|
+
import { getTransferables, isOPFSResponse, } from "./protocol.js";
|
|
7
|
+
function storeIterator(client, protocol, level) {
|
|
7
8
|
return {
|
|
8
9
|
[Symbol.asyncIterator]() {
|
|
9
10
|
const iteratorId = uuid();
|
|
10
11
|
return {
|
|
11
12
|
next: async () => {
|
|
12
|
-
const
|
|
13
|
-
|
|
13
|
+
const response = await client.request(protocol === "legacy"
|
|
14
|
+
? new memory.REQ_Iterator_Next({ id: iteratorId, level })
|
|
15
|
+
: {
|
|
16
|
+
type: "iterator-next",
|
|
17
|
+
id: iteratorId,
|
|
18
|
+
level,
|
|
19
|
+
messageId: uuid(),
|
|
20
|
+
});
|
|
21
|
+
const cloneResponse = isOPFSResponse(response) && response.type === "iterator-next"
|
|
22
|
+
? response
|
|
23
|
+
: undefined;
|
|
24
|
+
const keys = response instanceof memory.RESP_Iterator_Next
|
|
25
|
+
? response.keys
|
|
26
|
+
: cloneResponse?.keys ?? [];
|
|
27
|
+
const values = response instanceof memory.RESP_Iterator_Next
|
|
28
|
+
? response.values
|
|
29
|
+
: cloneResponse?.values ?? [];
|
|
30
|
+
if (keys.length > 1) {
|
|
14
31
|
throw new Error("Unsupported iteration response");
|
|
15
32
|
}
|
|
16
33
|
// Will only have 0 or 1 element for now
|
|
17
34
|
// eslint-disable-next-line no-unreachable-loop
|
|
18
|
-
for (let i = 0; i <
|
|
35
|
+
for (let i = 0; i < keys.length; i++) {
|
|
19
36
|
return {
|
|
20
37
|
done: false,
|
|
21
|
-
value: [
|
|
38
|
+
value: [keys[i], values[i]],
|
|
22
39
|
};
|
|
23
40
|
}
|
|
24
41
|
return { done: true, value: undefined };
|
|
25
42
|
},
|
|
26
43
|
async return() {
|
|
27
|
-
await client.request(
|
|
44
|
+
await client.request(protocol === "legacy"
|
|
45
|
+
? new memory.REQ_Iterator_Stop({ id: iteratorId, level })
|
|
46
|
+
: {
|
|
47
|
+
type: "iterator-stop",
|
|
48
|
+
id: iteratorId,
|
|
49
|
+
level,
|
|
50
|
+
messageId: uuid(),
|
|
51
|
+
});
|
|
28
52
|
return { done: true, value: undefined };
|
|
29
53
|
},
|
|
30
54
|
};
|
|
@@ -36,44 +60,92 @@ export class OPFSStore {
|
|
|
36
60
|
worker;
|
|
37
61
|
levelMap;
|
|
38
62
|
root;
|
|
63
|
+
protocol;
|
|
39
64
|
_responseCallbacks = new Map();
|
|
40
65
|
_createStorage;
|
|
41
|
-
constructor(directory) {
|
|
66
|
+
constructor(directory, options = {}) {
|
|
42
67
|
this.directory = directory;
|
|
68
|
+
this.protocol = options.protocol ?? "clone";
|
|
43
69
|
this.levelMap = new Map();
|
|
44
70
|
this._createStorage = (level = []) => {
|
|
45
71
|
return {
|
|
46
72
|
clear: async () => {
|
|
47
|
-
await this.request(
|
|
73
|
+
await this.request(this.protocol === "legacy"
|
|
74
|
+
? new memory.REQ_Clear({ level })
|
|
75
|
+
: { type: "clear", level, messageId: uuid() });
|
|
48
76
|
},
|
|
49
77
|
del: async (key) => {
|
|
50
|
-
await this.request(
|
|
78
|
+
await this.request(this.protocol === "legacy"
|
|
79
|
+
? new memory.REQ_Del({ level, key })
|
|
80
|
+
: { type: "del", level, key, messageId: uuid() });
|
|
51
81
|
},
|
|
52
82
|
get: async (key) => {
|
|
53
|
-
|
|
83
|
+
const response = await this.request(this.protocol === "legacy"
|
|
84
|
+
? new memory.REQ_Get({ level, key })
|
|
85
|
+
: { type: "get", level, key, messageId: uuid() });
|
|
86
|
+
if (response instanceof memory.RESP_Get) {
|
|
87
|
+
return response.bytes;
|
|
88
|
+
}
|
|
89
|
+
if (isOPFSResponse(response) && response.type === "get") {
|
|
90
|
+
return response.bytes;
|
|
91
|
+
}
|
|
92
|
+
return undefined;
|
|
54
93
|
},
|
|
55
94
|
put: async (key, value) => {
|
|
56
|
-
await this.request(
|
|
95
|
+
await this.request(this.protocol === "legacy"
|
|
96
|
+
? new memory.REQ_Put({ level, key, bytes: value })
|
|
97
|
+
: {
|
|
98
|
+
type: "put",
|
|
99
|
+
level,
|
|
100
|
+
key,
|
|
101
|
+
bytes: value,
|
|
102
|
+
messageId: uuid(),
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
status: async () => {
|
|
106
|
+
const response = await this.request(this.protocol === "legacy"
|
|
107
|
+
? new memory.REQ_Status({ level })
|
|
108
|
+
: { type: "status", level, messageId: uuid() });
|
|
109
|
+
if (response instanceof memory.RESP_Status) {
|
|
110
|
+
return response.status;
|
|
111
|
+
}
|
|
112
|
+
if (!isOPFSResponse(response) || response.type !== "status") {
|
|
113
|
+
throw new Error("Unexpected OPFS status response");
|
|
114
|
+
}
|
|
115
|
+
return response.status;
|
|
57
116
|
},
|
|
58
|
-
status: async () => (await this.request(new memory.REQ_Status({ level }))).status,
|
|
59
117
|
sublevel: async (name) => {
|
|
60
|
-
await this.request(
|
|
118
|
+
await this.request(this.protocol === "legacy"
|
|
119
|
+
? new memory.REQ_Sublevel({ level, name })
|
|
120
|
+
: { type: "sublevel", level, name, messageId: uuid() });
|
|
61
121
|
const newLevels = [...level, name];
|
|
62
122
|
const sublevel = this._createStorage(newLevels);
|
|
63
123
|
this.levelMap.set(memory.levelKey(newLevels), sublevel);
|
|
64
124
|
return sublevel;
|
|
65
125
|
},
|
|
66
|
-
iterator: () =>
|
|
126
|
+
iterator: () => storeIterator(this, this.protocol, level),
|
|
67
127
|
close: async () => {
|
|
68
|
-
await this.request(
|
|
128
|
+
await this.request(this.protocol === "legacy"
|
|
129
|
+
? new memory.REQ_Close({ level })
|
|
130
|
+
: { type: "close", level, messageId: uuid() });
|
|
69
131
|
/* this.levelMap.delete(memory.levelKey(level)); */
|
|
70
132
|
},
|
|
71
133
|
open: async () => {
|
|
72
|
-
await this.request(
|
|
134
|
+
await this.request(this.protocol === "legacy"
|
|
135
|
+
? new memory.REQ_Open({ level })
|
|
136
|
+
: { type: "open", level, messageId: uuid() });
|
|
73
137
|
},
|
|
74
138
|
size: async () => {
|
|
75
|
-
const
|
|
76
|
-
|
|
139
|
+
const response = await this.request(this.protocol === "legacy"
|
|
140
|
+
? new memory.REQ_Size({ level })
|
|
141
|
+
: { type: "size", level, messageId: uuid() });
|
|
142
|
+
if (response instanceof memory.RESP_Size) {
|
|
143
|
+
return response.size;
|
|
144
|
+
}
|
|
145
|
+
if (!isOPFSResponse(response) || response.type !== "size") {
|
|
146
|
+
throw new Error("Unexpected OPFS size response");
|
|
147
|
+
}
|
|
148
|
+
return response.size;
|
|
77
149
|
},
|
|
78
150
|
persisted: directory != null ? () => true : () => false,
|
|
79
151
|
};
|
|
@@ -99,7 +171,11 @@ export class OPFSStore {
|
|
|
99
171
|
this.worker = createWorker(this.directory);
|
|
100
172
|
this.root = this._createStorage([]);
|
|
101
173
|
this.worker.addEventListener("message", async (ev) => {
|
|
102
|
-
const message =
|
|
174
|
+
const message = isOPFSResponse(ev.data)
|
|
175
|
+
? ev.data
|
|
176
|
+
: deserialize(ev.data instanceof Uint8Array
|
|
177
|
+
? ev.data
|
|
178
|
+
: new Uint8Array(ev.data), memory.MemoryMessage);
|
|
103
179
|
this._responseCallbacks.get(message.messageId).fn(message);
|
|
104
180
|
});
|
|
105
181
|
await this.root.open();
|
|
@@ -133,6 +209,9 @@ export class OPFSStore {
|
|
|
133
209
|
if (message instanceof memory.RESP_Error) {
|
|
134
210
|
reject(new Error(message.error));
|
|
135
211
|
}
|
|
212
|
+
else if (isOPFSResponse(message) && message.type === "error") {
|
|
213
|
+
reject(new Error(message.error));
|
|
214
|
+
}
|
|
136
215
|
else {
|
|
137
216
|
resolve(message);
|
|
138
217
|
}
|
|
@@ -141,8 +220,12 @@ export class OPFSStore {
|
|
|
141
220
|
fn: onResponse,
|
|
142
221
|
once: true,
|
|
143
222
|
});
|
|
144
|
-
|
|
145
|
-
|
|
223
|
+
if (request instanceof memory.MemoryRequest) {
|
|
224
|
+
const bytes = serialize(request);
|
|
225
|
+
this.worker.postMessage(bytes, [bytes.buffer]);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
this.worker.postMessage(request, getTransferables(request));
|
|
146
229
|
});
|
|
147
230
|
}
|
|
148
231
|
persisted() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"opfs-store.js","sourceRoot":"","sources":["../../src/opfs-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,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I,EAAE,IAAI;aACV,CAAC,CAAC;YACH,IAAI,OAAO,YAAY,MAAM,CAAC,aAAa,EAAE,CAAC;gBAC7C,MAAM,KAAK,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;gBACjC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;gBAC/C,OAAO;YACR,CAAC;YACD,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;QAC7D,CAAC,CAAC,CAAC;IACJ,CAAC;IAED,SAAS;QACR,OAAO,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;IAC9B,CAAC;CACD"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export type OPFSStoreProtocol = "clone" | "legacy";
|
|
2
|
+
export type OPFSRequest = {
|
|
3
|
+
type: "open" | "close" | "clear" | "size" | "status";
|
|
4
|
+
messageId: string;
|
|
5
|
+
level: string[];
|
|
6
|
+
} | {
|
|
7
|
+
type: "get" | "del";
|
|
8
|
+
messageId: string;
|
|
9
|
+
level: string[];
|
|
10
|
+
key: string;
|
|
11
|
+
} | {
|
|
12
|
+
type: "put";
|
|
13
|
+
messageId: string;
|
|
14
|
+
level: string[];
|
|
15
|
+
key: string;
|
|
16
|
+
bytes: Uint8Array;
|
|
17
|
+
} | {
|
|
18
|
+
type: "sublevel";
|
|
19
|
+
messageId: string;
|
|
20
|
+
level: string[];
|
|
21
|
+
name: string;
|
|
22
|
+
} | {
|
|
23
|
+
type: "iterator-next" | "iterator-stop";
|
|
24
|
+
messageId: string;
|
|
25
|
+
level: string[];
|
|
26
|
+
id: string;
|
|
27
|
+
};
|
|
28
|
+
export type OPFSResponse = {
|
|
29
|
+
type: "ack";
|
|
30
|
+
messageId: string;
|
|
31
|
+
level: string[];
|
|
32
|
+
} | {
|
|
33
|
+
type: "status";
|
|
34
|
+
messageId: string;
|
|
35
|
+
level: string[];
|
|
36
|
+
status: "opening" | "open" | "closing" | "closed";
|
|
37
|
+
} | {
|
|
38
|
+
type: "get";
|
|
39
|
+
messageId: string;
|
|
40
|
+
level: string[];
|
|
41
|
+
bytes?: Uint8Array;
|
|
42
|
+
} | {
|
|
43
|
+
type: "size";
|
|
44
|
+
messageId: string;
|
|
45
|
+
level: string[];
|
|
46
|
+
size: number;
|
|
47
|
+
} | {
|
|
48
|
+
type: "iterator-next";
|
|
49
|
+
messageId: string;
|
|
50
|
+
level: string[];
|
|
51
|
+
keys: string[];
|
|
52
|
+
values: Uint8Array[];
|
|
53
|
+
} | {
|
|
54
|
+
type: "error";
|
|
55
|
+
messageId: string;
|
|
56
|
+
level: string[];
|
|
57
|
+
error: string;
|
|
58
|
+
};
|
|
59
|
+
export declare const isOPFSRequest: (value: unknown) => value is OPFSRequest;
|
|
60
|
+
export declare const isOPFSResponse: (value: unknown) => value is OPFSResponse;
|
|
61
|
+
export declare const getTransferables: (message: OPFSRequest | OPFSResponse) => Transferable[];
|
|
62
|
+
//# sourceMappingURL=protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/protocol.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEnD,MAAM,MAAM,WAAW,GACpB;IACA,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,QAAQ,CAAC;IACrD,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,EAAE,CAAC;CACf,GACD;IACA,IAAI,EAAE,KAAK,GAAG,KAAK,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;CACX,GACD;IACA,IAAI,EAAE,KAAK,CAAC;IACZ,SAAS,EAAE,MAAM,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E,MAAM,EAAE,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACb,CAAC;AAyBL,eAAO,MAAM,aAAa,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,WAQvD,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,YAQxD,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAC5B,SAAS,WAAW,GAAG,YAAY,KACjC,YAAY,EAUd,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const REQUEST_TYPES = new Set([
|
|
2
|
+
"open",
|
|
3
|
+
"close",
|
|
4
|
+
"clear",
|
|
5
|
+
"size",
|
|
6
|
+
"status",
|
|
7
|
+
"get",
|
|
8
|
+
"del",
|
|
9
|
+
"put",
|
|
10
|
+
"sublevel",
|
|
11
|
+
"iterator-next",
|
|
12
|
+
"iterator-stop",
|
|
13
|
+
]);
|
|
14
|
+
const RESPONSE_TYPES = new Set([
|
|
15
|
+
"ack",
|
|
16
|
+
"status",
|
|
17
|
+
"get",
|
|
18
|
+
"size",
|
|
19
|
+
"iterator-next",
|
|
20
|
+
"error",
|
|
21
|
+
]);
|
|
22
|
+
export const isOPFSRequest = (value) => {
|
|
23
|
+
return (typeof value === "object" &&
|
|
24
|
+
value !== null &&
|
|
25
|
+
typeof value.messageId === "string" &&
|
|
26
|
+
Array.isArray(value.level) &&
|
|
27
|
+
REQUEST_TYPES.has(value.type));
|
|
28
|
+
};
|
|
29
|
+
export const isOPFSResponse = (value) => {
|
|
30
|
+
return (typeof value === "object" &&
|
|
31
|
+
value !== null &&
|
|
32
|
+
typeof value.messageId === "string" &&
|
|
33
|
+
Array.isArray(value.level) &&
|
|
34
|
+
RESPONSE_TYPES.has(value.type));
|
|
35
|
+
};
|
|
36
|
+
export const getTransferables = (message) => {
|
|
37
|
+
if (message.type === "get" && "bytes" in message && message.bytes) {
|
|
38
|
+
return [message.bytes.buffer];
|
|
39
|
+
}
|
|
40
|
+
if (message.type === "iterator-next" && "values" in message) {
|
|
41
|
+
return message.values.map((value) => value.buffer);
|
|
42
|
+
}
|
|
43
|
+
return [];
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../src/protocol.ts"],"names":[],"mappings":"AAwEA,MAAM,aAAa,GAAG,IAAI,GAAG,CAAsB;IAClD,MAAM;IACN,OAAO;IACP,OAAO;IACP,MAAM;IACN,QAAQ;IACR,KAAK;IACL,KAAK;IACL,KAAK;IACL,UAAU;IACV,eAAe;IACf,eAAe;CACf,CAAC,CAAC;AAEH,MAAM,cAAc,GAAG,IAAI,GAAG,CAAuB;IACpD,KAAK;IACL,QAAQ;IACR,KAAK;IACL,MAAM;IACN,eAAe;IACf,OAAO;CACP,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAc,EAAwB,EAAE;IACrE,OAAO,CACN,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAQ,KAAqB,CAAC,SAAS,KAAK,QAAQ;QACpD,KAAK,CAAC,OAAO,CAAE,KAAqB,CAAC,KAAK,CAAC;QAC3C,aAAa,CAAC,GAAG,CAAE,KAAqB,CAAC,IAAI,CAAC,CAC9C,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAc,EAAyB,EAAE;IACvE,OAAO,CACN,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAQ,KAAsB,CAAC,SAAS,KAAK,QAAQ;QACrD,KAAK,CAAC,OAAO,CAAE,KAAsB,CAAC,KAAK,CAAC;QAC5C,cAAc,CAAC,GAAG,CAAE,KAAsB,CAAC,IAAI,CAAC,CAChD,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC/B,OAAmC,EAClB,EAAE;IACnB,IAAI,OAAO,CAAC,IAAI,KAAK,KAAK,IAAI,OAAO,IAAI,OAAO,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;QACnE,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,MAAqB,CAAC,CAAC;IAC9C,CAAC;IACD,IAAI,OAAO,CAAC,IAAI,KAAK,eAAe,IAAI,QAAQ,IAAI,OAAO,EAAE,CAAC;QAC7D,OAAO,OAAO,CAAC,MAAM,CAAC,GAAG,CACxB,CAAC,KAAiB,EAAE,EAAE,CAAC,KAAK,CAAC,MAAqB,CAClD,CAAC;IACH,CAAC;IACD,OAAO,EAAE,CAAC;AACX,CAAC,CAAC"}
|
package/dist/src/worker.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import * as memory from "@peerbit/any-store-interface/messages";
|
|
2
|
+
import { type OPFSRequest, type OPFSResponse } from "./protocol.js";
|
|
2
3
|
export declare class OPFSStoreWorker {
|
|
3
4
|
private _levels;
|
|
4
5
|
private _rootStore;
|
|
5
6
|
private _memoryIterator;
|
|
6
7
|
constructor();
|
|
7
|
-
|
|
8
|
+
private parseMessage;
|
|
9
|
+
respond(request: OPFSRequest | memory.MemoryRequest, response: OPFSResponse | memory.MemoryRequest, postMessageFn?: typeof postMessage): Promise<void>;
|
|
8
10
|
}
|
|
9
11
|
//# sourceMappingURL=worker.d.ts.map
|
package/dist/src/worker.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/worker.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,MAAM,MAAM,uCAAuC,CAAC;
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/worker.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,MAAM,MAAM,uCAAuC,CAAC;AAGhE,OAAO,EAEN,KAAK,WAAW,EAChB,KAAK,YAAY,EAEjB,MAAM,eAAe,CAAC;AA4CvB,qBAAa,eAAe;IAC3B,OAAO,CAAC,OAAO,CAAwB;IACvC,OAAO,CAAC,UAAU,CAAW;IAE7B,OAAO,CAAC,eAAe,CAGrB;;IA6YF,OAAO,CAAC,YAAY;IASd,OAAO,CACZ,OAAO,EAAE,WAAW,GAAG,MAAM,CAAC,aAAa,EAC3C,QAAQ,EAAE,YAAY,GAAG,MAAM,CAAC,aAAa,EAC7C,aAAa,qBAAc;CAa5B"}
|
package/dist/src/worker.js
CHANGED
|
@@ -4,6 +4,7 @@ import {} from "@peerbit/any-store-interface";
|
|
|
4
4
|
import * as memory from "@peerbit/any-store-interface/messages";
|
|
5
5
|
import { fromBase64URL, ready, toBase64URL } from "@peerbit/crypto";
|
|
6
6
|
import { waitForResolved } from "@peerbit/time";
|
|
7
|
+
import { getTransferables, isOPFSRequest, } from "./protocol.js";
|
|
7
8
|
const directory = location.hash.split("#")?.[1];
|
|
8
9
|
const encodeName = (name) => {
|
|
9
10
|
// since "/" and perhaps other characters might not be allowed we do encode
|
|
@@ -137,7 +138,8 @@ export class OPFSStoreWorker {
|
|
|
137
138
|
create: true,
|
|
138
139
|
});
|
|
139
140
|
const writeFileHandle = await createWriteHandle(fileHandle);
|
|
140
|
-
writeFileHandle.write(value);
|
|
141
|
+
writeFileHandle.write(value, { at: 0 });
|
|
142
|
+
writeFileHandle.truncate(value.byteLength);
|
|
141
143
|
writeFileHandle.flush();
|
|
142
144
|
writeFileHandle.close();
|
|
143
145
|
const prevSize = sizeMap.get(encodedKey);
|
|
@@ -183,7 +185,7 @@ export class OPFSStoreWorker {
|
|
|
183
185
|
};
|
|
184
186
|
this._rootStore = createMemory();
|
|
185
187
|
self.addEventListener("message", async (ev) => {
|
|
186
|
-
const message =
|
|
188
|
+
const message = this.parseMessage(ev.data);
|
|
187
189
|
try {
|
|
188
190
|
if (message instanceof memory.MemoryMessage) {
|
|
189
191
|
if (message instanceof memory.REQ_Open) {
|
|
@@ -261,19 +263,153 @@ export class OPFSStoreWorker {
|
|
|
261
263
|
await this.respond(message, new memory.RESP_Sublevel({ level: message.level }), postMessageFn);
|
|
262
264
|
}
|
|
263
265
|
}
|
|
266
|
+
if (isOPFSRequest(message)) {
|
|
267
|
+
if (message.type === "open") {
|
|
268
|
+
if ((await this._rootStore.status()) === "closed") {
|
|
269
|
+
await this._rootStore.open();
|
|
270
|
+
}
|
|
271
|
+
await this.respond(message, {
|
|
272
|
+
type: "ack",
|
|
273
|
+
level: message.level,
|
|
274
|
+
messageId: message.messageId,
|
|
275
|
+
});
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const m = message.level.length === 0
|
|
279
|
+
? this._rootStore
|
|
280
|
+
: this._levels.get(memory.levelKey(message.level));
|
|
281
|
+
if (!m) {
|
|
282
|
+
throw new Error("Recieved memory message for an undefined level");
|
|
283
|
+
}
|
|
284
|
+
if (message.type === "clear") {
|
|
285
|
+
await m.clear();
|
|
286
|
+
await this.respond(message, {
|
|
287
|
+
type: "ack",
|
|
288
|
+
level: message.level,
|
|
289
|
+
messageId: message.messageId,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
else if (message.type === "close") {
|
|
293
|
+
await m.close();
|
|
294
|
+
await this.respond(message, {
|
|
295
|
+
type: "ack",
|
|
296
|
+
level: message.level,
|
|
297
|
+
messageId: message.messageId,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
else if (message.type === "del") {
|
|
301
|
+
await m.del(message.key);
|
|
302
|
+
await this.respond(message, {
|
|
303
|
+
type: "ack",
|
|
304
|
+
level: message.level,
|
|
305
|
+
messageId: message.messageId,
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
else if (message.type === "iterator-next") {
|
|
309
|
+
let iterator = this._memoryIterator.get(message.id);
|
|
310
|
+
if (!iterator) {
|
|
311
|
+
iterator = m.iterator()[Symbol.asyncIterator]();
|
|
312
|
+
this._memoryIterator.set(message.id, iterator);
|
|
313
|
+
}
|
|
314
|
+
const next = await iterator.next();
|
|
315
|
+
const nextValue = next.done ? undefined : next.value;
|
|
316
|
+
const response = {
|
|
317
|
+
type: "iterator-next",
|
|
318
|
+
keys: nextValue ? [nextValue[0]] : [],
|
|
319
|
+
values: nextValue ? [nextValue[1]] : [],
|
|
320
|
+
level: message.level,
|
|
321
|
+
messageId: message.messageId,
|
|
322
|
+
};
|
|
323
|
+
await this.respond(message, response);
|
|
324
|
+
if (next.done) {
|
|
325
|
+
this._memoryIterator.delete(message.id);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
else if (message.type === "iterator-stop") {
|
|
329
|
+
this._memoryIterator.delete(message.id);
|
|
330
|
+
await this.respond(message, {
|
|
331
|
+
type: "ack",
|
|
332
|
+
level: message.level,
|
|
333
|
+
messageId: message.messageId,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
else if (message.type === "get") {
|
|
337
|
+
await this.respond(message, {
|
|
338
|
+
type: "get",
|
|
339
|
+
bytes: await m.get(message.key),
|
|
340
|
+
level: message.level,
|
|
341
|
+
messageId: message.messageId,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
else if (message.type === "put") {
|
|
345
|
+
await m.put(message.key, message.bytes);
|
|
346
|
+
await this.respond(message, {
|
|
347
|
+
type: "ack",
|
|
348
|
+
level: message.level,
|
|
349
|
+
messageId: message.messageId,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
else if (message.type === "size") {
|
|
353
|
+
await this.respond(message, {
|
|
354
|
+
type: "size",
|
|
355
|
+
size: await m.size(),
|
|
356
|
+
level: message.level,
|
|
357
|
+
messageId: message.messageId,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
else if (message.type === "status") {
|
|
361
|
+
await this.respond(message, {
|
|
362
|
+
type: "status",
|
|
363
|
+
status: await m.status(),
|
|
364
|
+
level: message.level,
|
|
365
|
+
messageId: message.messageId,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
else if (message.type === "sublevel") {
|
|
369
|
+
await m.sublevel(message.name);
|
|
370
|
+
await this.respond(message, {
|
|
371
|
+
type: "ack",
|
|
372
|
+
level: message.level,
|
|
373
|
+
messageId: message.messageId,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
264
377
|
}
|
|
265
378
|
catch (error) {
|
|
266
|
-
|
|
379
|
+
if (message instanceof memory.MemoryRequest) {
|
|
380
|
+
await this.respond(message, new memory.RESP_Error({
|
|
381
|
+
error: error?.["message"] || error?.constructor.name,
|
|
382
|
+
level: message["level"] || [],
|
|
383
|
+
}), postMessageFn);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
await this.respond(message, {
|
|
387
|
+
type: "error",
|
|
267
388
|
error: error?.["message"] || error?.constructor.name,
|
|
268
|
-
level: message
|
|
269
|
-
|
|
389
|
+
level: message?.level || [],
|
|
390
|
+
messageId: message?.messageId || "",
|
|
391
|
+
});
|
|
270
392
|
}
|
|
271
393
|
});
|
|
272
394
|
}
|
|
395
|
+
parseMessage(data) {
|
|
396
|
+
if (isOPFSRequest(data)) {
|
|
397
|
+
return data;
|
|
398
|
+
}
|
|
399
|
+
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
400
|
+
return deserialize(bytes, memory.MemoryRequest);
|
|
401
|
+
}
|
|
273
402
|
async respond(request, response, postMessageFn = postMessage) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
403
|
+
if (request instanceof memory.MemoryRequest) {
|
|
404
|
+
const legacyResponse = response;
|
|
405
|
+
legacyResponse.messageId = request.messageId;
|
|
406
|
+
const bytes = serialize(legacyResponse);
|
|
407
|
+
postMessageFn(bytes, { transfer: [bytes.buffer] });
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const cloneResponse = response;
|
|
411
|
+
cloneResponse.messageId = request.messageId;
|
|
412
|
+
postMessageFn(cloneResponse, { transfer: getTransferables(cloneResponse) });
|
|
277
413
|
}
|
|
278
414
|
}
|
|
279
415
|
new OPFSStoreWorker();
|