@waffle-os/runtime 0.0.154 → 0.0.156
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/core/filesystem.ts +22 -0
- package/core/wasi.ts +78 -5
- package/dist/core/filesystem.js +1 -0
- package/dist/core/wasi.js +71 -6
- package/dist/fs/backend.js +22 -1
- package/dist/fs/client.js +11 -3
- package/dist/fs/cold-read-cache.js +23 -0
- package/dist/fs/guest-watch.js +267 -94
- package/dist/fs/indexeddb-chunks.js +88 -11
- package/dist/fs/indexeddb.js +78 -14
- package/dist/fs/memory.js +17 -1
- package/dist/fs/multitab-backend.js +198 -64
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +19 -1
- package/dist/fs/service.js +30 -1
- package/dist/fs/shared.js +4 -0
- package/dist/fs/storage.js +107 -8
- package/dist/fs/watch-service.js +97 -2
- package/dist/fs/watch-subscriptions.js +121 -8
- package/dist/proc/intrinsics/core.js +5 -0
- package/dist/proc/intrinsics.js +1 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +27 -1
- package/fs/client.ts +11 -0
- package/fs/cold-read-cache.ts +38 -0
- package/fs/guest-watch.ts +298 -102
- package/fs/indexeddb-chunks.ts +103 -9
- package/fs/indexeddb.ts +98 -11
- package/fs/memory.ts +18 -0
- package/fs/multitab-backend.ts +273 -72
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +38 -1
- package/fs/service.ts +28 -0
- package/fs/shared.ts +4 -0
- package/fs/storage.ts +128 -10
- package/fs/watch-service.ts +110 -3
- package/fs/watch-subscriptions.ts +115 -7
- package/package.json +1 -1
- package/proc/intrinsics/core.ts +7 -0
- package/proc/intrinsics.ts +1 -0
- package/testing/indexeddb-storage-browser.ts +129 -0
- package/tooling/program/program-transformer.json +1 -1
package/dist/fs/guest-watch.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
FilesystemError
|
|
3
|
+
} from "../core/filesystem.js";
|
|
2
4
|
import {
|
|
3
5
|
arrayAppend,
|
|
4
6
|
atomicsLoad,
|
|
5
7
|
MapConstructor,
|
|
6
8
|
mapDelete,
|
|
9
|
+
mapFirst,
|
|
7
10
|
mapForEach,
|
|
8
11
|
mapGet,
|
|
9
12
|
mapSet,
|
|
@@ -12,6 +15,7 @@ import {
|
|
|
12
15
|
const EAGAIN = 6;
|
|
13
16
|
const EBADF = 8;
|
|
14
17
|
const ECANCELED = 11;
|
|
18
|
+
const ENOTSUP = 58;
|
|
15
19
|
const CHANGE = 1;
|
|
16
20
|
const RENAME = 2;
|
|
17
21
|
const OVERFLOW = 3;
|
|
@@ -22,147 +26,316 @@ const END = { kind: "records", records: [], length: 0 };
|
|
|
22
26
|
function failure(errno) {
|
|
23
27
|
return { kind: "error", errno };
|
|
24
28
|
}
|
|
29
|
+
function watchError(id, errno) {
|
|
30
|
+
return { kind: "watch_error", id, errno };
|
|
31
|
+
}
|
|
32
|
+
function nextOf(id, result) {
|
|
33
|
+
return result.kind === "error" ? watchError(id, result.errno) : { ...result, id };
|
|
34
|
+
}
|
|
35
|
+
function rotate(map, watch) {
|
|
36
|
+
mapDelete(map, watch.id);
|
|
37
|
+
mapSet(map, watch.id, watch);
|
|
38
|
+
}
|
|
25
39
|
|
|
26
40
|
export class GuestWatchTable {
|
|
27
|
-
fs;
|
|
28
|
-
watches
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
#fs;
|
|
42
|
+
#watches;
|
|
43
|
+
#byService;
|
|
44
|
+
#buffered;
|
|
45
|
+
#errored;
|
|
46
|
+
#ended;
|
|
47
|
+
#nextId = 1;
|
|
48
|
+
#revocationNotified = false;
|
|
49
|
+
#activeCount = 0;
|
|
50
|
+
#observedGen = 0;
|
|
51
|
+
#observedCount = 0;
|
|
52
|
+
#tableObserved;
|
|
53
|
+
#drainMore = false;
|
|
54
|
+
#noDrain = false;
|
|
55
|
+
#flip = false;
|
|
31
56
|
constructor(fs) {
|
|
32
|
-
this
|
|
57
|
+
this.#fs = fs;
|
|
58
|
+
this.drop();
|
|
33
59
|
}
|
|
34
60
|
get word() {
|
|
35
|
-
return this
|
|
61
|
+
return this.#fs.cells;
|
|
36
62
|
}
|
|
37
63
|
get active() {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
64
|
+
return this.#activeCount > 0;
|
|
65
|
+
}
|
|
66
|
+
#useDrain() {
|
|
67
|
+
if (typeof this.#fs.drain !== "function" || this.#noDrain)
|
|
68
|
+
return false;
|
|
69
|
+
if (this.#fs.canDrain === false) {
|
|
70
|
+
this.#tableObserved = undefined;
|
|
71
|
+
this.#drainMore = false;
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
#allObserved(generation) {
|
|
77
|
+
return this.#observedCount === this.#activeCount && this.#observedGen === generation || this.#useDrain() && !this.#drainMore && this.#tableObserved === generation;
|
|
44
78
|
}
|
|
45
79
|
pending() {
|
|
46
|
-
if (this
|
|
47
|
-
return !!this
|
|
48
|
-
return this
|
|
80
|
+
if (this.#fs.revoked)
|
|
81
|
+
return !!this.#fs.remote && !this.#revocationNotified && this.#activeCount > 0;
|
|
82
|
+
return this.#activeCount > 0 && (mapSize(this.#buffered) > 0 || mapSize(this.#errored) > 0 || !this.#allObserved(atomicsLoad(this.#fs.cells, 3)));
|
|
49
83
|
}
|
|
50
84
|
ready() {
|
|
51
|
-
if (!mapSize(this
|
|
85
|
+
if (!mapSize(this.#watches))
|
|
52
86
|
return false;
|
|
53
|
-
return this
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (watch.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
87
|
+
return this.#fs.revoked || mapSize(this.#buffered) > 0 || mapSize(this.#errored) > 0 || mapSize(this.#ended) > 0 || this.#activeCount > 0 && !this.#allObserved(atomicsLoad(this.#fs.cells, 3));
|
|
88
|
+
}
|
|
89
|
+
#setObserved(watch, generation) {
|
|
90
|
+
if (watch.observed === generation)
|
|
91
|
+
return;
|
|
92
|
+
if (!watch.ended) {
|
|
93
|
+
if (watch.observed === this.#observedGen)
|
|
94
|
+
this.#observedCount--;
|
|
95
|
+
if (generation !== this.#observedGen) {
|
|
96
|
+
this.#observedGen = generation;
|
|
97
|
+
this.#observedCount = 1;
|
|
98
|
+
} else
|
|
99
|
+
this.#observedCount++;
|
|
100
|
+
}
|
|
101
|
+
watch.observed = generation;
|
|
102
|
+
}
|
|
103
|
+
#markEnded(watch) {
|
|
104
|
+
if (watch.ended)
|
|
105
|
+
return;
|
|
106
|
+
watch.ended = true;
|
|
107
|
+
this.#activeCount--;
|
|
108
|
+
if (watch.observed === this.#observedGen)
|
|
109
|
+
this.#observedCount--;
|
|
110
|
+
watch.errno = 0;
|
|
111
|
+
mapDelete(this.#buffered, watch.id);
|
|
112
|
+
mapDelete(this.#errored, watch.id);
|
|
113
|
+
mapSet(this.#ended, watch.id, watch);
|
|
63
114
|
}
|
|
64
115
|
open(handle, recursive) {
|
|
65
|
-
if (this
|
|
116
|
+
if (this.#nextId > LAST_ID)
|
|
66
117
|
throw new FilesystemError(EAGAIN, "watch ids exhausted");
|
|
67
|
-
const observed = atomicsLoad(this
|
|
68
|
-
const service = this
|
|
69
|
-
const id = this
|
|
70
|
-
|
|
118
|
+
const observed = atomicsLoad(this.#fs.cells, 3);
|
|
119
|
+
const service = this.#fs.open(handle, recursive);
|
|
120
|
+
const id = this.#nextId++;
|
|
121
|
+
const watch = { id, service, observed: NaN, queue: [], head: 0, ended: false, errno: 0 };
|
|
122
|
+
mapSet(this.#watches, id, watch);
|
|
123
|
+
mapSet(this.#byService, service, watch);
|
|
124
|
+
this.#activeCount++;
|
|
125
|
+
this.#setObserved(watch, observed);
|
|
71
126
|
return id;
|
|
72
127
|
}
|
|
73
128
|
read(id, capacity) {
|
|
74
|
-
const watch = mapGet(this
|
|
129
|
+
const watch = mapGet(this.#watches, id);
|
|
75
130
|
if (!watch)
|
|
76
131
|
return failure(EBADF);
|
|
77
132
|
if (watch.ended)
|
|
78
133
|
return END;
|
|
79
|
-
if (watch.head
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
134
|
+
if (watch.head < watch.queue.length)
|
|
135
|
+
return this.#takeFrom(watch, capacity);
|
|
136
|
+
const force = watch.errno !== 0;
|
|
137
|
+
if (force) {
|
|
138
|
+
watch.errno = 0;
|
|
139
|
+
mapDelete(this.#errored, id);
|
|
140
|
+
}
|
|
141
|
+
return this.#refillSingle(watch, force) ?? this.#takeFrom(watch, capacity);
|
|
142
|
+
}
|
|
143
|
+
readNext(capacity) {
|
|
144
|
+
if (!mapSize(this.#watches))
|
|
145
|
+
return failure(EAGAIN);
|
|
146
|
+
if (this.#fs.revoked) {
|
|
147
|
+
if (this.#fs.remote)
|
|
148
|
+
this.#revocationNotified = true;
|
|
149
|
+
const first = mapFirst(this.#watches);
|
|
150
|
+
rotate(this.#watches, first);
|
|
151
|
+
return watchError(first.id, ECANCELED);
|
|
152
|
+
}
|
|
153
|
+
if (this.#useDrain() && (mapSize(this.#buffered) || mapSize(this.#errored) || !this.#drainService()))
|
|
154
|
+
return this.#takeNextReady(capacity);
|
|
155
|
+
return this.#readNextFallback(capacity);
|
|
156
|
+
}
|
|
157
|
+
#takeNextReady(capacity) {
|
|
158
|
+
const buffered = mapFirst(this.#buffered);
|
|
159
|
+
if (buffered) {
|
|
160
|
+
const read = this.#takeFrom(buffered, capacity);
|
|
161
|
+
if (buffered.head < buffered.queue.length)
|
|
162
|
+
rotate(this.#buffered, buffered);
|
|
163
|
+
return nextOf(buffered.id, read);
|
|
83
164
|
}
|
|
84
|
-
|
|
165
|
+
const ended = mapFirst(this.#ended);
|
|
166
|
+
const errored = mapFirst(this.#errored);
|
|
167
|
+
const pick = this.#flip ? ended ?? errored : errored ?? ended;
|
|
168
|
+
if (!pick)
|
|
169
|
+
return failure(EAGAIN);
|
|
170
|
+
this.#flip = !pick.ended;
|
|
171
|
+
if (pick.ended) {
|
|
172
|
+
rotate(this.#ended, pick);
|
|
173
|
+
return nextOf(pick.id, END);
|
|
174
|
+
}
|
|
175
|
+
const errno = pick.errno;
|
|
176
|
+
pick.errno = 0;
|
|
177
|
+
if (pick.observed === this.#observedGen)
|
|
178
|
+
this.#observedCount--;
|
|
179
|
+
pick.observed = NaN;
|
|
180
|
+
this.#drainMore = true;
|
|
181
|
+
mapDelete(this.#errored, pick.id);
|
|
182
|
+
return watchError(pick.id, errno);
|
|
183
|
+
}
|
|
184
|
+
#readNextFallback(capacity) {
|
|
185
|
+
let result;
|
|
186
|
+
let reported;
|
|
187
|
+
mapForEach(this.#watches, (watch) => {
|
|
188
|
+
if (result)
|
|
189
|
+
return;
|
|
190
|
+
try {
|
|
191
|
+
const read = this.read(watch.id, capacity);
|
|
192
|
+
if (read.kind !== "error" || read.errno !== EAGAIN) {
|
|
193
|
+
result = nextOf(watch.id, read);
|
|
194
|
+
reported = watch;
|
|
195
|
+
}
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if (!(error instanceof FilesystemError))
|
|
198
|
+
throw error;
|
|
199
|
+
result = watchError(watch.id, error.errno);
|
|
200
|
+
reported = watch;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
if (reported)
|
|
204
|
+
rotate(this.#watches, reported);
|
|
205
|
+
return result ?? failure(EAGAIN);
|
|
85
206
|
}
|
|
86
207
|
close(id) {
|
|
87
|
-
const watch = mapGet(this
|
|
208
|
+
const watch = mapGet(this.#watches, id);
|
|
88
209
|
if (!watch)
|
|
89
210
|
return EBADF;
|
|
90
|
-
if (!this
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
211
|
+
if (!this.#fs.revoked) {
|
|
212
|
+
try {
|
|
213
|
+
this.#fs.close(watch.service);
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (!(error instanceof FilesystemError) || error.errno !== ECANCELED)
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (!watch.ended) {
|
|
220
|
+
this.#activeCount--;
|
|
221
|
+
if (watch.observed === this.#observedGen)
|
|
222
|
+
this.#observedCount--;
|
|
101
223
|
}
|
|
224
|
+
mapDelete(this.#buffered, id);
|
|
225
|
+
mapDelete(this.#errored, id);
|
|
226
|
+
mapDelete(this.#ended, id);
|
|
227
|
+
mapDelete(this.#watches, id);
|
|
228
|
+
mapDelete(this.#byService, watch.service);
|
|
229
|
+
if (!mapSize(this.#watches)) {
|
|
230
|
+
this.#tableObserved = undefined;
|
|
231
|
+
this.#drainMore = false;
|
|
232
|
+
}
|
|
233
|
+
return 0;
|
|
102
234
|
}
|
|
103
235
|
drop() {
|
|
104
|
-
this
|
|
236
|
+
this.#watches = new MapConstructor;
|
|
237
|
+
this.#byService = new MapConstructor;
|
|
238
|
+
this.#buffered = new MapConstructor;
|
|
239
|
+
this.#errored = new MapConstructor;
|
|
240
|
+
this.#ended = new MapConstructor;
|
|
241
|
+
this.#activeCount = this.#observedCount = 0;
|
|
242
|
+
this.#tableObserved = undefined;
|
|
243
|
+
this.#drainMore = false;
|
|
105
244
|
}
|
|
106
|
-
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return failure(ECANCELED);
|
|
245
|
+
#applyBatch(watch, batch, generation) {
|
|
246
|
+
if (batch.kind === "closed") {
|
|
247
|
+
this.#markEnded(watch);
|
|
248
|
+
return true;
|
|
111
249
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
250
|
+
this.#setObserved(watch, generation);
|
|
251
|
+
watch.queue = encodeBatch(batch);
|
|
252
|
+
watch.head = 0;
|
|
253
|
+
if (watch.queue.length)
|
|
254
|
+
mapSet(this.#buffered, watch.id, watch);
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
#drainService() {
|
|
258
|
+
const generation = atomicsLoad(this.#fs.cells, 3);
|
|
259
|
+
if (!this.#drainMore && this.#allObserved(generation))
|
|
260
|
+
return false;
|
|
261
|
+
const previous = this.#tableObserved;
|
|
262
|
+
this.#tableObserved = generation;
|
|
263
|
+
let drained;
|
|
118
264
|
try {
|
|
119
|
-
|
|
265
|
+
drained = this.#fs.drain();
|
|
120
266
|
} catch (error) {
|
|
121
|
-
|
|
267
|
+
if (error instanceof FilesystemError && error.errno === ENOTSUP) {
|
|
268
|
+
this.#tableObserved = undefined;
|
|
269
|
+
this.#drainMore = false;
|
|
270
|
+
if (this.#fs.canDrain === undefined)
|
|
271
|
+
this.#noDrain = true;
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
this.#tableObserved = previous;
|
|
122
275
|
throw error;
|
|
123
276
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
277
|
+
this.#drainMore = drained.more;
|
|
278
|
+
for (let index = 0;index < drained.entries.length; index++) {
|
|
279
|
+
const entry = drained.entries[index];
|
|
280
|
+
const watch = mapGet(this.#byService, entry.id);
|
|
281
|
+
if (!watch || watch.ended)
|
|
282
|
+
continue;
|
|
283
|
+
if ("errno" in entry) {
|
|
284
|
+
watch.errno = entry.errno;
|
|
285
|
+
mapSet(this.#errored, watch.id, watch);
|
|
286
|
+
} else {
|
|
287
|
+
this.#applyBatch(watch, entry.batch, generation);
|
|
288
|
+
}
|
|
127
289
|
}
|
|
128
|
-
|
|
129
|
-
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
#refillSingle(watch, force) {
|
|
293
|
+
if (this.#fs.revoked) {
|
|
294
|
+
if (this.#fs.remote)
|
|
295
|
+
this.#revocationNotified = true;
|
|
296
|
+
return failure(ECANCELED);
|
|
297
|
+
}
|
|
298
|
+
const generation = atomicsLoad(this.#fs.cells, 3);
|
|
299
|
+
if (!force && !this.#fs.remote && (generation === watch.observed || this.#useDrain() && !this.#drainMore && generation === this.#tableObserved))
|
|
300
|
+
return failure(EAGAIN);
|
|
301
|
+
if (this.#applyBatch(watch, this.#fs.read(watch.service), generation))
|
|
302
|
+
return END;
|
|
130
303
|
if (!watch.queue.length)
|
|
131
304
|
return failure(EAGAIN);
|
|
132
305
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
306
|
+
#takeFrom(watch, capacity) {
|
|
307
|
+
const records = [];
|
|
308
|
+
let length = 0;
|
|
309
|
+
while (watch.head < watch.queue.length) {
|
|
310
|
+
const record = watch.queue[watch.head];
|
|
311
|
+
if (length + record.length > capacity)
|
|
312
|
+
break;
|
|
313
|
+
arrayAppend(records, record);
|
|
314
|
+
length += record.length;
|
|
315
|
+
watch.head++;
|
|
316
|
+
if (record[0] === OVERFLOW)
|
|
317
|
+
this.#markEnded(watch);
|
|
318
|
+
}
|
|
319
|
+
if (!length)
|
|
320
|
+
return { kind: "short", needed: watch.queue[watch.head].length };
|
|
321
|
+
if (watch.head === watch.queue.length) {
|
|
322
|
+
watch.queue = [];
|
|
323
|
+
watch.head = 0;
|
|
324
|
+
mapDelete(this.#buffered, watch.id);
|
|
325
|
+
}
|
|
326
|
+
return { kind: "records", records, length };
|
|
152
327
|
}
|
|
153
|
-
return { kind: "records", records, length };
|
|
154
328
|
}
|
|
155
329
|
function encodeBatch(batch) {
|
|
156
330
|
if (batch.kind === "overflow")
|
|
157
331
|
return [encodeRecord(OVERFLOW, null)];
|
|
158
332
|
const records = [];
|
|
159
|
-
for (let index = 0;index < batch.events.length; index++)
|
|
160
|
-
|
|
333
|
+
for (let index = 0;index < batch.events.length; index++) {
|
|
334
|
+
const event = batch.events[index];
|
|
335
|
+
arrayAppend(records, encodeRecord(event.type === "change" ? CHANGE : RENAME, event.name));
|
|
336
|
+
}
|
|
161
337
|
return records;
|
|
162
338
|
}
|
|
163
|
-
function encodeEvent(event) {
|
|
164
|
-
return encodeRecord(event.type === "change" ? CHANGE : RENAME, event.name);
|
|
165
|
-
}
|
|
166
339
|
function encodeRecord(type, name) {
|
|
167
340
|
const bytes = name === null ? undefined : encoder.encode(name);
|
|
168
341
|
const size = bytes ? bytes.length : 0;
|
|
@@ -23,22 +23,45 @@ export function parseChunk(value, size, key) {
|
|
|
23
23
|
export function chunkLength(manifest, index) {
|
|
24
24
|
return Math.min(CHUNK_SIZE, manifest.size - index * CHUNK_SIZE);
|
|
25
25
|
}
|
|
26
|
+
const HEX = Array.from({ length: 256 }, (_, byte) => byte.toString(16).padStart(2, "0"));
|
|
27
|
+
const PREPARE_CONCURRENCY = 32;
|
|
26
28
|
export async function chunkChecksum(bytes) {
|
|
27
29
|
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
|
28
|
-
|
|
30
|
+
let hex = "";
|
|
31
|
+
for (let i = 0;i < digest.length; i++)
|
|
32
|
+
hex += HEX[digest[i]];
|
|
33
|
+
return hex;
|
|
29
34
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
for (let offset = 0;offset < bytes.length; offset += CHUNK_SIZE) {
|
|
35
|
+
async function prepareOwnedPayload(bytes, signal) {
|
|
36
|
+
if (!bytes.length) {
|
|
33
37
|
signal?.throwIfAborted();
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
return {
|
|
39
|
+
manifest: { version: 2, size: 0, chunkSize: CHUNK_SIZE, count: 0 },
|
|
40
|
+
chunks: []
|
|
41
|
+
};
|
|
36
42
|
}
|
|
43
|
+
const count = Math.ceil(bytes.length / CHUNK_SIZE);
|
|
44
|
+
const chunks = Array.from({ length: count });
|
|
45
|
+
let next = 0;
|
|
46
|
+
const workers = Math.min(PREPARE_CONCURRENCY, count);
|
|
47
|
+
await Promise.all(Array.from({ length: workers }, async () => {
|
|
48
|
+
while (next < count) {
|
|
49
|
+
signal?.throwIfAborted();
|
|
50
|
+
const index = next++;
|
|
51
|
+
const offset = index * CHUNK_SIZE;
|
|
52
|
+
const part = bytes.slice(offset, offset + CHUNK_SIZE);
|
|
53
|
+
chunks[index] = { bytes: part, checksum: await chunkChecksum(part) };
|
|
54
|
+
}
|
|
55
|
+
}));
|
|
56
|
+
signal?.throwIfAborted();
|
|
37
57
|
return {
|
|
38
|
-
manifest: { version: 2, size: bytes.length, chunkSize: CHUNK_SIZE, count
|
|
58
|
+
manifest: { version: 2, size: bytes.length, chunkSize: CHUNK_SIZE, count },
|
|
39
59
|
chunks
|
|
40
60
|
};
|
|
41
61
|
}
|
|
62
|
+
export function preparePayload(bytes, signal) {
|
|
63
|
+
return prepareOwnedPayload(bytes, signal);
|
|
64
|
+
}
|
|
42
65
|
export function validateRange(range) {
|
|
43
66
|
if (!validInteger(range.offset) || !validInteger(range.length) || !Number.isSafeInteger(range.offset + range.length))
|
|
44
67
|
throw new RangeError("Invalid filesystem byte range");
|
|
@@ -48,7 +71,7 @@ export function deletePayload(store, key) {
|
|
|
48
71
|
store.delete(key);
|
|
49
72
|
store.delete(IDBKeyRange.bound([key, 0], [key, Number.MAX_SAFE_INTEGER]));
|
|
50
73
|
}
|
|
51
|
-
function putPayload(store, key, payload) {
|
|
74
|
+
export function putPayload(store, key, payload) {
|
|
52
75
|
for (const [index, chunk] of payload.chunks.entries())
|
|
53
76
|
store.put(chunk, [key, index]);
|
|
54
77
|
store.put(payload.manifest, key);
|
|
@@ -96,10 +119,64 @@ function compareChunk(value, chunk, key, finish, abort) {
|
|
|
96
119
|
abort(error);
|
|
97
120
|
}
|
|
98
121
|
}
|
|
99
|
-
export async function preparePayloads(data) {
|
|
122
|
+
export async function preparePayloads(data, owned = false, checkAbort) {
|
|
100
123
|
const prepared = new Map;
|
|
101
|
-
|
|
102
|
-
|
|
124
|
+
checkAbort?.();
|
|
125
|
+
if (!data.size)
|
|
126
|
+
return prepared;
|
|
127
|
+
const tasks = [];
|
|
128
|
+
for (const [key, bytes] of data) {
|
|
129
|
+
if (!bytes.length) {
|
|
130
|
+
prepared.set(key, {
|
|
131
|
+
manifest: { version: 2, size: 0, chunkSize: CHUNK_SIZE, count: 0 },
|
|
132
|
+
chunks: []
|
|
133
|
+
});
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const count = Math.ceil(bytes.length / CHUNK_SIZE);
|
|
137
|
+
const chunks = Array.from({ length: count });
|
|
138
|
+
prepared.set(key, {
|
|
139
|
+
manifest: { version: 2, size: bytes.length, chunkSize: CHUNK_SIZE, count },
|
|
140
|
+
chunks
|
|
141
|
+
});
|
|
142
|
+
const direct = owned && count === 1 && bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength && bytes.buffer instanceof ArrayBuffer;
|
|
143
|
+
for (let index = 0;index < count; index++)
|
|
144
|
+
tasks.push({ chunks, index, bytes, offset: index * CHUNK_SIZE, direct });
|
|
145
|
+
}
|
|
146
|
+
if (!tasks.length)
|
|
147
|
+
return prepared;
|
|
148
|
+
let next = 0;
|
|
149
|
+
let failed = false;
|
|
150
|
+
let failure;
|
|
151
|
+
const fail = (error) => {
|
|
152
|
+
if (!failed) {
|
|
153
|
+
failed = true;
|
|
154
|
+
failure = error;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const workers = Math.min(PREPARE_CONCURRENCY, tasks.length);
|
|
158
|
+
await Promise.allSettled(Array.from({ length: workers }, async () => {
|
|
159
|
+
while (!failed && next < tasks.length) {
|
|
160
|
+
try {
|
|
161
|
+
checkAbort?.();
|
|
162
|
+
} catch (error) {
|
|
163
|
+
fail(error);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const task = tasks[next++];
|
|
167
|
+
const part = task.direct ? task.bytes : task.bytes.slice(task.offset, task.offset + CHUNK_SIZE);
|
|
168
|
+
try {
|
|
169
|
+
const checksum = await chunkChecksum(part);
|
|
170
|
+
task.chunks[task.index] = { bytes: part, checksum };
|
|
171
|
+
} catch (error) {
|
|
172
|
+
fail(error);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}));
|
|
177
|
+
if (failed)
|
|
178
|
+
throw failure;
|
|
179
|
+
checkAbort?.();
|
|
103
180
|
return prepared;
|
|
104
181
|
}
|
|
105
182
|
export function copyLegacyChunks(data, key, prepared) {
|