@waffle-os/runtime 0.0.153 → 0.0.155

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.
Files changed (61) hide show
  1. package/browser/net/helper.ts +114 -19
  2. package/browser/net/ring.ts +18 -6
  3. package/browser/net/runtime.ts +2 -2
  4. package/browser/notification-relay.ts +105 -18
  5. package/browser/notification-wait.ts +45 -4
  6. package/browser/notification-worker.ts +169 -76
  7. package/browser/preview.ts +15 -2
  8. package/core/filesystem.ts +18 -0
  9. package/core/wasi.ts +75 -4
  10. package/dist/browser/net/helper.js +83 -18
  11. package/dist/browser/net/ring.js +6 -5
  12. package/dist/browser/net/runtime.js +2 -2
  13. package/dist/browser/notification-relay.js +78 -15
  14. package/dist/browser/notification-wait.js +37 -5
  15. package/dist/browser/notification-worker.js +146 -67
  16. package/dist/core/wasi.js +62 -4
  17. package/dist/fs/backend.js +7 -0
  18. package/dist/fs/client.js +75 -17
  19. package/dist/fs/dirty-admission.js +15 -8
  20. package/dist/fs/guest-watch.js +267 -94
  21. package/dist/fs/indexeddb-chunks.js +88 -11
  22. package/dist/fs/indexeddb.js +366 -110
  23. package/dist/fs/memory.js +106 -20
  24. package/dist/fs/multitab-backend.js +291 -74
  25. package/dist/fs/multitab-protocol.js +1 -0
  26. package/dist/fs/persistent.js +70 -16
  27. package/dist/fs/protocol.js +15 -7
  28. package/dist/fs/publication-attachment.js +9 -7
  29. package/dist/fs/service.js +62 -9
  30. package/dist/fs/shared.js +100 -6
  31. package/dist/fs/storage.js +107 -8
  32. package/dist/fs/watch-service.js +97 -2
  33. package/dist/fs/watch-subscriptions.js +121 -8
  34. package/dist/proc/command-stdio.js +1 -1
  35. package/dist/proc/intrinsics/core.js +5 -0
  36. package/dist/proc/intrinsics.js +1 -0
  37. package/dist/tooling/program/program-transformer.json +1 -1
  38. package/fs/backend.ts +17 -0
  39. package/fs/client.ts +99 -16
  40. package/fs/dirty-admission.ts +22 -7
  41. package/fs/guest-watch.ts +299 -103
  42. package/fs/indexeddb-chunks.ts +103 -9
  43. package/fs/indexeddb.ts +477 -121
  44. package/fs/memory.ts +120 -19
  45. package/fs/multitab-backend.ts +440 -95
  46. package/fs/multitab-protocol.ts +1 -0
  47. package/fs/persistent.ts +88 -16
  48. package/fs/protocol.ts +26 -7
  49. package/fs/publication-attachment.ts +9 -7
  50. package/fs/service.ts +62 -7
  51. package/fs/shared.ts +110 -8
  52. package/fs/storage.ts +128 -10
  53. package/fs/watch-service.ts +110 -3
  54. package/fs/watch-subscriptions.ts +115 -7
  55. package/package.json +1 -1
  56. package/proc/command-stdio.ts +1 -1
  57. package/proc/intrinsics/core.ts +7 -0
  58. package/proc/intrinsics.ts +1 -0
  59. package/testing/idle-workspace-cancellation-browser.ts +22 -13
  60. package/testing/indexeddb-storage-browser.ts +564 -1
  61. package/tooling/program/program-transformer.json +1 -1
@@ -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
- return Array.from(digest, (byte) => byte.toString(16).padStart(2, "0")).join("");
30
+ let hex = "";
31
+ for (let i = 0;i < digest.length; i++)
32
+ hex += HEX[digest[i]];
33
+ return hex;
29
34
  }
30
- export async function preparePayload(bytes, signal) {
31
- const chunks = [];
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
- const part = bytes.slice(offset, offset + CHUNK_SIZE);
35
- chunks.push({ bytes: part, checksum: await chunkChecksum(part) });
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: chunks.length },
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
- for (const [key, bytes] of data)
102
- prepared.set(key, await preparePayload(bytes));
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) {