hyperdb-wasm 0.0.26862-dev.wasm.g89537caa → 0.0.26978-dev.wasm.g29b852db

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.
@@ -1,4 +1,8 @@
1
1
  import type { ExternalDirectoryBackend } from './wasm_bindings.js';
2
+ export type BrowserDirectoryEntry = {
3
+ readonly name: string;
4
+ readonly kind: 'file' | 'directory';
5
+ };
2
6
  export declare class BrowserExternalDirectoryBackend implements ExternalDirectoryBackend {
3
7
  private readonly directories;
4
8
  private readonly paths;
@@ -11,6 +15,14 @@ export declare class BrowserExternalDirectoryBackend implements ExternalDirector
11
15
  registered(path: string, locator: string): void;
12
16
  removed(pathOrLocator: string): void;
13
17
  list(locator: string): Promise<readonly string[]>;
18
+ listEntries(locator: string, path: string): Promise<readonly BrowserDirectoryEntry[]>;
19
+ lookup(locator: string, path: string): Promise<{
20
+ readonly kind: 'directory';
21
+ } | {
22
+ readonly kind: 'file';
23
+ readonly locator: string;
24
+ readonly size: number;
25
+ } | undefined>;
14
26
  open(locator: string, path: string): Promise<{
15
27
  readonly locator: string;
16
28
  readonly size: number;
@@ -24,6 +36,7 @@ export declare class BrowserExternalDirectoryBackend implements ExternalDirector
24
36
  shutdown(): void;
25
37
  private getDirectory;
26
38
  private getWritableDirectory;
39
+ private openDirectory;
27
40
  private getOpenWriter;
28
41
  private cleanupWriter;
29
42
  private enqueue;
@@ -12,9 +12,18 @@
12
12
  if (typeof number !== "number" || !Number.isSafeInteger(number) || number < 0) throw new Error(`${name} must be a nonnegative safe integer`);
13
13
  return number;
14
14
  }
15
+ function validateCStringContents(value, name) {
16
+ if (value.includes("\0")) throw new Error(`${name} must not contain NUL`);
17
+ }
15
18
  function validateCString(value, name) {
16
19
  if (!value) throw new Error(`${name} must not be empty`);
17
- if (value.includes("\0")) throw new Error(`${name} must not contain NUL`);
20
+ validateCStringContents(value, name);
21
+ }
22
+ function resultError(result) {
23
+ if (result.state === "error" || result.state === "busy") {
24
+ return typeof result.error === "string" ? result.error : result.error.message;
25
+ }
26
+ return `unexpected Wasm result state: ${result.state}`;
18
27
  }
19
28
  function decodeStructuredError(payload) {
20
29
  if (!structuredErrorMagic.every((byte, index) => payload[index] === byte)) throw new Error("structured error has invalid magic");
@@ -52,6 +61,8 @@
52
61
  registerExternalFile;
53
62
  registerExternalDirectory;
54
63
  removeExternalRegistration;
64
+ projectExternalFile;
65
+ projectExternalDirectory;
55
66
  startQuery;
56
67
  insertArrowIPCFromPath;
57
68
  pollQuery;
@@ -61,6 +72,7 @@
61
72
  resultPayloadLength;
62
73
  free;
63
74
  nextTemporaryFile = 1;
75
+ mountedPaths = /* @__PURE__ */ new Set();
64
76
  constructor(module) {
65
77
  this.module = module;
66
78
  this.initialize = module.cwrap("hyperdb_wasm_initialize", "pointer", ["string", "string"]);
@@ -70,6 +82,8 @@
70
82
  this.registerExternalFile = module.cwrap("hyperdb_wasm_register_external_file", "pointer", ["string", "string", "number"]);
71
83
  this.registerExternalDirectory = module.cwrap("hyperdb_wasm_register_external_directory", "pointer", ["string", "string", "number"]);
72
84
  this.removeExternalRegistration = module.cwrap("hyperdb_wasm_remove_external_registration", "pointer", ["string"]);
85
+ this.projectExternalFile = module.cwrap("hyperdb_wasm_project_external_file", "pointer", ["string", "string", "number"]);
86
+ this.projectExternalDirectory = module.cwrap("hyperdb_wasm_project_external_directory", "pointer", ["string", "string"]);
73
87
  this.startQuery = module.cwrap("hyperdb_wasm_query_start", "pointer", ["number", "string", "number"]);
74
88
  this.insertArrowIPCFromPath = module.cwrap("hyperdb_wasm_arrow_insert_from_path", "pointer", ["number", "string", "string", "string", "number"]);
75
89
  this.pollQuery = module.cwrap("hyperdb_wasm_query_poll", "pointer", ["number"]);
@@ -90,77 +104,40 @@
90
104
  this.module.FS.unlink(request.path);
91
105
  return { response: { id: request.id, type: "response", result: { state: "ok", payload: new Uint8Array() } }, transfer: [] };
92
106
  }
93
- if (request.type === "registerExternalFile" && "path" in request) {
107
+ if (request.type === "mount" && request.source.kind === "node-path") {
94
108
  if (!this.module.externalFileBackend) {
95
109
  return { response: { id: request.id, type: "response", result: { state: "error", error: "Host external files are supported only by the Node client" } }, transfer: [] };
96
110
  }
97
- const registration = this.module.externalFileBackend.register(request.path);
98
- try {
99
- const pointer2 = safeInteger(this.registerExternalFile(registration.name, registration.locator, registration.size), "result pointer");
100
- const result2 = this.decodeResult(pointer2);
101
- if (result2.state === "ok") {
102
- const path = new TextDecoder("utf-8", { fatal: true }).decode(result2.payload);
103
- try {
104
- this.module.externalFileBackend.registered(path, registration.locator);
105
- } catch (error) {
106
- const cleanupPointer = safeInteger(this.removeExternalRegistration(path), "result pointer");
107
- this.decodeResult(cleanupPointer);
108
- throw error;
109
- }
110
- } else {
111
- this.module.externalFileBackend.removed(registration.locator);
112
- }
113
- return { response: { id: request.id, type: "response", result: result2 }, transfer: [] };
114
- } catch (error) {
115
- this.module.externalFileBackend.removed(registration.locator);
116
- throw error;
117
- }
111
+ const registration = this.module.externalFileBackend.mount(request.source.path, request.mode);
112
+ if (registration.kind === "directory") return this.mountDirectory(request, request.source.path);
113
+ return this.mountFile(request, registration.locator, registration.size, this.module.externalFileBackend);
118
114
  }
119
- if (request.type === "registerExternalFile" && !this.module.externalFileBackendAvailable) {
115
+ if (request.type === "mount" && request.source.kind === "browser-file" && !this.module.externalFileBackendAvailable) {
120
116
  return { response: { id: request.id, type: "response", result: { state: "error", error: "Blob external files are supported only by the browser client" } }, transfer: [] };
121
117
  }
122
- if (request.type === "registerExternalDirectory") {
123
- const backend = this.module.externalDirectoryBackend;
124
- if (!backend) {
125
- return { response: { id: request.id, type: "response", result: { state: "error", error: "External directories are not supported by this client" } }, transfer: [] };
126
- }
127
- const registration = backend.register("handle" in request ? request.handle : request.path, request.mode);
128
- try {
129
- const pointer2 = safeInteger(this.registerExternalDirectory(registration.name, registration.locator, Number(request.mode === "readwrite")), "result pointer");
130
- const result2 = this.decodeResult(pointer2);
131
- if (result2.state === "ok") {
132
- const path = new TextDecoder("utf-8", { fatal: true }).decode(result2.payload);
133
- try {
134
- backend.registered(path, registration.locator);
135
- } catch (error) {
136
- const cleanupPointer = safeInteger(this.removeExternalRegistration(path), "result pointer");
137
- this.decodeResult(cleanupPointer);
138
- throw error;
139
- }
140
- } else {
141
- const completion = completionFor([backend.removed(registration.locator)]);
142
- return { response: { id: request.id, type: "response", result: result2 }, transfer: [], ...completion && { completion } };
143
- }
144
- return { response: { id: request.id, type: "response", result: result2 }, transfer: [] };
145
- } catch (error) {
146
- void Promise.resolve(backend.removed(registration.locator)).catch(() => void 0);
147
- throw error;
148
- }
118
+ if (request.type === "mount" && request.source.kind === "browser-directory") {
119
+ return this.mountDirectory(request, request.source.handle);
149
120
  }
150
- if (request.type === "removeExternalRegistration" && (this.module.externalFileBackend || this.module.externalDirectoryBackend)) {
121
+ if (request.type === "mount" && request.source.kind === "browser-file") {
122
+ return this.mountFile(request, request.source.url, safeInteger(request.source.size, "external file size"));
123
+ }
124
+ if (request.type === "unmount" && (this.module.externalFileBackend || this.module.externalDirectoryBackend)) {
151
125
  validateCString(request.path, "external registration path");
152
126
  const pointer2 = safeInteger(this.removeExternalRegistration(request.path), "result pointer");
153
127
  const result2 = this.decodeResult(pointer2);
154
- if (result2.state === "ok") {
155
- const completion = completionFor([
156
- this.module.externalFileBackend?.removed(request.path),
157
- this.module.externalDirectoryBackend?.removed(request.path)
158
- ]);
159
- return { response: { id: request.id, type: "response", result: result2 }, transfer: [], ...completion && { completion } };
128
+ if (result2.state === "ok" || result2.state === "error" && result2.error === "External registration was not found" && this.mountedPaths.has(request.path)) {
129
+ const cleanup = async () => {
130
+ await Promise.all([
131
+ this.module.externalFileBackend?.removed(request.path),
132
+ this.module.externalDirectoryBackend?.removed(request.path)
133
+ ]);
134
+ this.mountedPaths.delete(request.path);
135
+ };
136
+ return { response: { id: request.id, type: "response", result: { state: "ok", payload: new Uint8Array() } }, transfer: [], completion: cleanup() };
160
137
  }
161
138
  return { response: { id: request.id, type: "response", result: result2 }, transfer: [] };
162
139
  }
163
- if (request.type === "removeExternalRegistration" && !this.module.externalFileBackendAvailable) {
140
+ if (request.type === "unmount" && !this.module.externalFileBackendAvailable) {
164
141
  return { response: { id: request.id, type: "response", result: { state: "error", error: "External files are not supported by this client" } }, transfer: [] };
165
142
  }
166
143
  const pointer = safeInteger(this.invoke(request), "result pointer");
@@ -183,6 +160,7 @@
183
160
  switch (request.type) {
184
161
  case "initialize":
185
162
  validateCString(request.settings, "settings");
163
+ validateCStringContents(this.module.hyperdbWasmStoragePath ?? "", "storage path");
186
164
  return this.initialize(request.settings, this.module.hyperdbWasmStoragePath ?? "");
187
165
  case "connect":
188
166
  return this.connect();
@@ -190,20 +168,19 @@
190
168
  return this.disconnect(request.connection);
191
169
  case "shutdown":
192
170
  return this.shutdown();
193
- case "registerExternalFile":
194
- if ("path" in request) throw new Error("host external file request has no backend");
195
- validateCString(request.name, "external file name");
196
- validateCString(request.url, "external file URL");
197
- return this.registerExternalFile(request.name, request.url, safeInteger(request.size, "external file size"));
198
- case "registerExternalDirectory":
199
- throw new Error("external directory request has no backend");
200
- case "removeExternalRegistration":
171
+ case "mount":
172
+ throw new Error("mount request has no backend");
173
+ case "unmount":
201
174
  validateCString(request.path, "external registration path");
202
175
  return this.removeExternalRegistration(request.path);
203
176
  case "startQuery":
177
+ validateCStringContents(request.sql, "SQL");
204
178
  return this.startQuery(request.connection, request.sql, Number(request.sqlTestMode ?? false));
205
179
  case "insertArrowIPCFromPath": {
206
- const path = request.internal ? request.path : this.module.resolveHyperDBPath?.(request.path) ?? request.path;
180
+ const path = request.internal || request.path.startsWith("/mnt/external/") ? request.path : this.module.resolveHyperDBPath?.(request.path) ?? request.path;
181
+ validateCString(path, "Arrow IPC input path");
182
+ validateCString(request.name, "Arrow insert table name");
183
+ validateCStringContents(request.schema ?? "", "Arrow insert schema name");
207
184
  return this.insertArrowIPCFromPath(request.connection, path, request.name, request.schema ?? "", Number(request.create));
208
185
  }
209
186
  case "pollQuery":
@@ -214,6 +191,90 @@
214
191
  return this.finishQuery(request.query);
215
192
  }
216
193
  }
194
+ mountFile(request, locator, size, backend) {
195
+ validateCString(request.path, "external registration path");
196
+ validateCString(locator, "external file locator");
197
+ let path;
198
+ let registered = false;
199
+ let rollbackError;
200
+ try {
201
+ const result = this.decodeResult(safeInteger(this.registerExternalFile(request.path, locator, size), "result pointer"));
202
+ if (result.state !== "ok") {
203
+ backend?.removed(locator);
204
+ return { response: { id: request.id, type: "response", result }, transfer: [] };
205
+ }
206
+ path = new TextDecoder("utf-8", { fatal: true }).decode(result.payload);
207
+ const projection = this.decodeResult(safeInteger(this.projectExternalFile(path, locator, size), "result pointer"));
208
+ if (projection.state !== "ok") throw new Error(resultError(projection));
209
+ backend?.registered(path, locator);
210
+ registered = true;
211
+ this.mountedPaths.add(path);
212
+ return { response: { id: request.id, type: "response", result }, transfer: [] };
213
+ } catch (error) {
214
+ if (path !== void 0) {
215
+ try {
216
+ const rollback = this.decodeResult(safeInteger(this.removeExternalRegistration(path), "result pointer"));
217
+ if (rollback.state !== "ok") rollbackError = new Error(resultError(rollback));
218
+ } catch (failure) {
219
+ rollbackError = failure;
220
+ }
221
+ }
222
+ if (rollbackError !== void 0) {
223
+ if (path !== void 0) {
224
+ backend?.registered(path, locator);
225
+ this.mountedPaths.add(path);
226
+ }
227
+ throw new Error(`mount failed and rollback also failed: ${rollbackError instanceof Error ? rollbackError.message : String(rollbackError)}`, { cause: error });
228
+ }
229
+ backend?.removed(registered && path !== void 0 ? path : locator);
230
+ throw error;
231
+ }
232
+ }
233
+ mountDirectory(request, source) {
234
+ const backend = this.module.externalDirectoryBackend;
235
+ if (!backend) {
236
+ return { response: { id: request.id, type: "response", result: { state: "error", error: "External directories are not supported by this client" } }, transfer: [] };
237
+ }
238
+ validateCString(request.path, "external registration path");
239
+ const registration = backend.register(source, request.mode);
240
+ let preserveBackend = false;
241
+ try {
242
+ validateCString(registration.locator, "external directory locator");
243
+ const pointer = safeInteger(this.registerExternalDirectory(request.path, registration.locator, Number(request.mode === "readwrite")), "result pointer");
244
+ const result = this.decodeResult(pointer);
245
+ if (result.state === "ok") {
246
+ const path = new TextDecoder("utf-8", { fatal: true }).decode(result.payload);
247
+ try {
248
+ const projectionPointer = this.projectExternalDirectory(path, registration.locator);
249
+ const projection = this.decodeResult(safeInteger(projectionPointer, "result pointer"));
250
+ if (projection.state !== "ok") throw new Error(resultError(projection));
251
+ backend.registered(path, registration.locator);
252
+ this.mountedPaths.add(path);
253
+ } catch (error) {
254
+ const cleanupPointer = safeInteger(this.removeExternalRegistration(path), "result pointer");
255
+ const cleanup = this.decodeResult(cleanupPointer);
256
+ if (cleanup.state !== "ok") {
257
+ backend.registered(path, registration.locator);
258
+ this.mountedPaths.add(path);
259
+ preserveBackend = true;
260
+ throw new Error(`mount failed and rollback also failed: ${resultError(cleanup)}`, { cause: error });
261
+ }
262
+ throw error;
263
+ }
264
+ } else {
265
+ const completion = completionFor([backend.removed(registration.locator)]);
266
+ return { response: { id: request.id, type: "response", result }, transfer: [], ...completion && { completion } };
267
+ }
268
+ return { response: { id: request.id, type: "response", result }, transfer: [] };
269
+ } catch (error) {
270
+ if (preserveBackend) throw error;
271
+ return {
272
+ response: { id: request.id, type: "response", result: { state: "error", error: error instanceof Error ? error.message : String(error) } },
273
+ transfer: [],
274
+ completion: Promise.resolve(backend.removed(registration.locator))
275
+ };
276
+ }
277
+ }
217
278
  decodeResult(pointer) {
218
279
  if (pointer === 0) return { state: "error", error: "out of memory" };
219
280
  try {
@@ -251,6 +312,11 @@
251
312
  };
252
313
 
253
314
  // src/protocol.ts
315
+ function isUnaryRpcResult(value) {
316
+ if (!isRecord(value) || !Number.isSafeInteger(value.status) || value.status < 0 || value.status > 16) return false;
317
+ if (value.status === 0) return hasKeys(value, ["status", "response"]) && value.response instanceof Uint8Array;
318
+ return (hasKeys(value, ["status", "message"]) || hasKeys(value, ["status", "message", "details"])) && typeof value.message === "string" && (value.details === void 0 || value.details instanceof Uint8Array);
319
+ }
254
320
  function isRecord(value) {
255
321
  return typeof value === "object" && value !== null;
256
322
  }
@@ -278,12 +344,21 @@
278
344
  return hasKeys(value, ["id", "type", "bytes"]) && value.bytes instanceof Uint8Array;
279
345
  case "removeFile":
280
346
  return hasKeys(value, ["id", "type", "path"]) && typeof value.path === "string";
281
- case "registerExternalFile":
282
- return hasKeys(value, ["id", "type", "name", "url", "size"]) && typeof value.name === "string" && typeof value.url === "string" && typeof value.size === "number" && Number.isSafeInteger(value.size) && value.size >= 0 || hasKeys(value, ["id", "type", "path"]) && typeof value.path === "string";
283
- case "registerExternalDirectory":
284
- return (value.mode === "read" || value.mode === "readwrite") && (hasKeys(value, ["id", "type", "handle", "mode"]) && isRecord(value.handle) && value.handle.kind === "directory" || hasKeys(value, ["id", "type", "path", "mode"]) && typeof value.path === "string" && !!value.path && !value.path.includes("\0"));
285
- case "removeExternalRegistration":
286
- return hasKeys(value, ["id", "type", "path"]) && typeof value.path === "string";
347
+ case "mount": {
348
+ if (!hasKeys(value, ["id", "type", "source", "path", "mode"]) || typeof value.path !== "string" || !value.path.startsWith("/mnt/external/") || new TextEncoder().encode(value.path.slice("/mnt/external/".length)).byteLength === 0 || new TextEncoder().encode(value.path.slice("/mnt/external/".length)).byteLength > 200 || value.path.slice("/mnt/external/".length).split("/").some((component) => !component || component === "." || component === ".." || !/^[A-Za-z0-9._-]+$/.test(component)) || value.mode !== "read" && value.mode !== "readwrite" || !isRecord(value.source)) return false;
349
+ switch (value.source.kind) {
350
+ case "browser-file":
351
+ return value.mode === "read" && hasKeys(value.source, ["kind", "name", "url", "size"]) && typeof value.source.name === "string" && typeof value.source.url === "string" && typeof value.source.size === "number" && Number.isSafeInteger(value.source.size) && value.source.size >= 0;
352
+ case "browser-directory":
353
+ return hasKeys(value.source, ["kind", "handle"]) && isRecord(value.source.handle) && value.source.handle.kind === "directory";
354
+ case "node-path":
355
+ return hasKeys(value.source, ["kind", "path"]) && typeof value.source.path === "string" && !!value.source.path && !value.source.path.includes("\0");
356
+ default:
357
+ return false;
358
+ }
359
+ }
360
+ case "unmount":
361
+ return hasKeys(value, ["id", "type", "path"]) && typeof value.path === "string" && !!value.path && !value.path.includes("\0");
287
362
  case "pollQuery":
288
363
  case "cancelQuery":
289
364
  case "finishQuery":
@@ -292,8 +367,48 @@
292
367
  return false;
293
368
  }
294
369
  }
370
+ function isWorkerRequest(value) {
371
+ if (isEngineRequest(value)) return true;
372
+ return isRecord(value) && value.type === "unaryRpcComplete" && hasKeys(value, ["type", "callId", "result"]) && Number.isSafeInteger(value.callId) && value.callId > 0 && isUnaryRpcResult(value.result);
373
+ }
295
374
 
296
375
  // src/worker_setup.ts
376
+ function parseMetadata(lines) {
377
+ const result = /* @__PURE__ */ Object.create(null);
378
+ for (const line of lines.split("\n")) {
379
+ if (!line) continue;
380
+ const separator = line.indexOf(":");
381
+ if (separator <= 0) throw new Error("Unary RPC metadata is malformed");
382
+ result[line.slice(0, separator)] = line.slice(separator + 1);
383
+ }
384
+ return result;
385
+ }
386
+ function completeUnaryRpc(module, completion) {
387
+ try {
388
+ if (completion.result.status === 0) {
389
+ const response = completion.result.response;
390
+ module._hyperdb_wasm_unary_rpc_complete(completion.callId, 0, 0, 0, 0, response.byteLength ? allocate(module, response) : 0, response.byteLength);
391
+ } else {
392
+ const messagePointer = safePointer(module.stringToNewUTF8(completion.result.message), "unary RPC message pointer");
393
+ const details = completion.result.details;
394
+ module._hyperdb_wasm_unary_rpc_complete(completion.callId, completion.result.status, messagePointer, details?.byteLength ? allocate(module, details) : 0, details?.byteLength ?? 0, 0, 0);
395
+ }
396
+ } catch (error) {
397
+ const messagePointer = safePointer(module.stringToNewUTF8(errorText(error)), "unary RPC bridge error pointer");
398
+ module._hyperdb_wasm_unary_rpc_complete(completion.callId, 2, messagePointer, 0, 0, 0, 0);
399
+ }
400
+ }
401
+ function installUnaryRpcRuntime(module, transport) {
402
+ module.hyperdbWasmUnaryRpcStart = (callId, host, port, method, metadataLines, requestPointer, requestLength, timeoutMs) => {
403
+ const pointer = safePointer(requestPointer, "unary RPC request pointer");
404
+ const end = safePointer(pointer + requestLength, "unary RPC request end");
405
+ module.getValue(pointer, "i8");
406
+ if (end > module.HEAPU8.byteLength) throw new Error("Unary RPC request exceeds the wasm heap");
407
+ const request = module.HEAPU8.slice(pointer, end);
408
+ transport.postMessage({ type: "unaryRpc", callId, endpoint: { host, port }, method, request, metadata: parseMetadata(metadataLines), timeoutMs }, [request.buffer]);
409
+ };
410
+ module.hyperdbWasmUnaryRpcCancel = (callId) => transport.postMessage({ type: "unaryRpcCancel", callId });
411
+ }
297
412
  function errorText(error) {
298
413
  return error instanceof Error ? error.message : String(error);
299
414
  }
@@ -325,6 +440,11 @@
325
440
  );
326
441
  }
327
442
  function installHttpRuntime(module, transport) {
443
+ const fetches = /* @__PURE__ */ new Map();
444
+ const completeFetch = (id, pointer, length) => {
445
+ if (!fetches.delete(id)) return;
446
+ module._hyperdb_wasm_fetch_complete(id, pointer, length);
447
+ };
328
448
  module.hyperdbWasmFetchStart = (id, url, offset, length, expectedSize) => {
329
449
  if (url.startsWith("hyperdb-node-file:") || url.startsWith("hyperdb-node-host-file:") || url.startsWith("hyperdb-node-directory-file:") || url.startsWith("hyperdb-browser-file:")) {
330
450
  const read = module.externalFileBackend?.read(url, offset, length, expectedSize) ?? module.externalDirectoryBackend?.read(url, offset, length, expectedSize);
@@ -341,63 +461,39 @@
341
461
  }
342
462
  const headers = new Headers();
343
463
  if (length > 0) headers.set("Range", `bytes=${offset}-${offset + length - 1}`);
344
- void fetch(url, { headers, redirect: "manual" }).then(async (response) => {
464
+ const controller = new AbortController();
465
+ fetches.set(id, controller);
466
+ void fetch(url, { headers, redirect: "manual", signal: controller.signal }).then(async (response) => {
345
467
  if (!response.ok || response.type === "opaqueredirect" || (length === 0 ? response.status !== 200 : response.status !== 206)) {
346
468
  void response.body?.cancel().catch(() => void 0);
347
- module._hyperdb_wasm_fetch_complete(id, 0, -1);
469
+ completeFetch(id, 0, -1);
348
470
  return;
349
471
  }
350
472
  if (length > 0) {
351
473
  const contentRange = /^bytes (\d+)-(\d+)\/(\d+)$/.exec(response.headers.get("content-range") ?? "");
352
474
  if (!contentRange || contentRange[1] !== String(offset) || contentRange[2] !== String(offset + length - 1) || expectedSize >= 0 && contentRange[3] !== String(expectedSize)) {
353
475
  void response.body?.cancel().catch(() => void 0);
354
- module._hyperdb_wasm_fetch_complete(id, 0, -1);
476
+ completeFetch(id, 0, -1);
355
477
  return;
356
478
  }
357
479
  }
358
480
  const bytes = new Uint8Array(await response.arrayBuffer());
359
481
  if (length > 0 && bytes.byteLength !== length) {
360
- module._hyperdb_wasm_fetch_complete(id, 0, -1);
482
+ completeFetch(id, 0, -1);
361
483
  return;
362
484
  }
363
485
  try {
364
- module._hyperdb_wasm_fetch_complete(id, allocate(module, bytes), bytes.byteLength);
486
+ completeFetch(id, allocate(module, bytes), bytes.byteLength);
365
487
  } catch (error) {
366
488
  transport.postMessage({ type: "log", text: `fetch failed: ${errorText(error)}` });
367
- module._hyperdb_wasm_fetch_complete(id, 0, -1);
489
+ completeFetch(id, 0, -1);
368
490
  }
369
491
  }).catch(() => {
370
492
  transport.postMessage({ type: "log", text: "fetch failed" });
371
- module._hyperdb_wasm_fetch_complete(id, 0, -1);
372
- });
373
- };
374
- module.hyperdbWasmHttpStart = (id, method, url, headerLines, bodyPointer, bodyLength) => {
375
- const headers = new Headers();
376
- for (const line of headerLines.split("\n")) {
377
- const separator = line.indexOf(":");
378
- if (separator > 0) headers.set(line.slice(0, separator), line.slice(separator + 1));
379
- }
380
- const pointer = safePointer(bodyPointer, "HTTP body pointer");
381
- const bodyEnd = safePointer(pointer + bodyLength, "HTTP body end");
382
- if (bodyEnd > module.HEAPU8.byteLength) throw new Error("HTTP body exceeds the wasm heap");
383
- const body = bodyLength > 0 ? module.HEAPU8.slice(pointer, bodyEnd) : void 0;
384
- const init = { method, headers };
385
- if (body) init.body = body;
386
- void fetch(url, init).then(async (response) => {
387
- const responseBody = new Uint8Array(await response.arrayBuffer());
388
- let responseHeaders = "";
389
- response.headers.forEach((value, name) => {
390
- responseHeaders += `${name.toLowerCase()}:${value}
391
- `;
392
- });
393
- const headersPointer = responseHeaders ? safePointer(module.stringToNewUTF8(responseHeaders), "headers pointer") : 0;
394
- const responseBodyPointer = responseBody.byteLength ? allocate(module, responseBody) : 0;
395
- module._hyperdb_wasm_http_complete(id, response.status, headersPointer, responseBodyPointer, responseBody.byteLength);
396
- }).catch((error) => {
397
- const errorBytes = new TextEncoder().encode(errorText(error));
398
- module._hyperdb_wasm_http_complete(id, -1, 0, allocate(module, errorBytes), errorBytes.byteLength);
493
+ completeFetch(id, 0, -1);
399
494
  });
400
495
  };
496
+ module.hyperdbWasmFetchCancel = (id) => fetches.get(id)?.abort();
401
497
  }
402
498
  function installExternalDirectoryRuntime(module) {
403
499
  const backend = module.externalDirectoryBackend;
@@ -490,6 +586,54 @@
490
586
  };
491
587
  }
492
588
  }
589
+ function installFilesystemProjectionRuntime(module) {
590
+ const complete = module._hyperdb_wasm_filesystem_operation_complete;
591
+ if (!complete) return;
592
+ module.hyperdbWasmFilesystemListStart = (id, locator, path) => {
593
+ const listing = module.externalDirectoryBackend?.listEntries?.(locator, path);
594
+ if (!listing) {
595
+ complete(id, 0, -1);
596
+ return;
597
+ }
598
+ void listing.then((entries) => {
599
+ const encoded = entries.map((entry) => `${entry.kind === "file" ? "f" : "d"}:${entry.name}`);
600
+ complete(id, safePointer(module.stringToNewUTF8(JSON.stringify(encoded)), "filesystem listing pointer"), 0);
601
+ }).catch(() => complete(id, 0, -1));
602
+ };
603
+ module.hyperdbWasmFilesystemLookupStart = (id, locator, path) => {
604
+ const lookup = module.externalDirectoryBackend?.lookup?.(locator, path);
605
+ if (!lookup) {
606
+ complete(id, 0, -1);
607
+ return;
608
+ }
609
+ void lookup.then((entry) => {
610
+ if (entry === void 0) complete(id, 0, -2);
611
+ else if (entry.kind === "directory") complete(id, 0, -21);
612
+ else complete(id, safePointer(module.stringToNewUTF8(entry.locator), "filesystem file locator pointer"), entry.size);
613
+ }).catch(() => complete(id, 0, -1));
614
+ };
615
+ module.hyperdbWasmFilesystemReadStart = (id, locator, offset, length, expectedSize, pointerValue) => {
616
+ const read = module.externalFileBackend?.read(locator, offset, length, expectedSize) ?? module.externalDirectoryBackend?.read(locator, offset, length, expectedSize) ?? (locator.startsWith("blob:") ? fetch(locator, { headers: { Range: `bytes=${offset}-${offset + length - 1}` } }).then(async (response) => {
617
+ const contentRange = /^bytes (\d+)-(\d+)\/(\d+)$/.exec(response.headers.get("content-range") ?? "");
618
+ if (response.status !== 206 || !contentRange || contentRange[1] !== String(offset) || contentRange[2] !== String(offset + length - 1) || contentRange[3] !== String(expectedSize)) {
619
+ throw new Error("Blob URL returned an invalid byte range");
620
+ }
621
+ return new Uint8Array(await response.arrayBuffer());
622
+ }) : void 0);
623
+ if (!read) {
624
+ complete(id, 0, -1);
625
+ return;
626
+ }
627
+ void read.then((bytes) => {
628
+ const pointer = safePointer(pointerValue, "filesystem read pointer");
629
+ const end = safePointer(pointer + bytes.byteLength, "filesystem read end");
630
+ module.getValue(pointer, "i8");
631
+ if (bytes.byteLength !== length || end > module.HEAPU8.byteLength) throw new Error("filesystem read returned an invalid payload");
632
+ module.HEAPU8.set(bytes, pointer);
633
+ complete(id, 0, bytes.byteLength);
634
+ }).catch(() => complete(id, 0, -1));
635
+ };
636
+ }
493
637
  function configureWorker(module, transport, configureFilesystem = () => void 0) {
494
638
  const queuedRequests = [];
495
639
  let bindings;
@@ -513,7 +657,9 @@
513
657
  }
514
658
  };
515
659
  transport.onMessage((message) => {
516
- if (isEngineRequest(message)) handle(message);
660
+ if (!isWorkerRequest(message)) return;
661
+ if (message.type === "unaryRpcComplete") completeUnaryRpc(module, message);
662
+ else handle(message);
517
663
  });
518
664
  module.print = (text) => transport.postMessage({ type: "log", text });
519
665
  module.printErr = (text) => transport.postMessage({ type: "log", text });
@@ -521,8 +667,10 @@
521
667
  try {
522
668
  configureFilesystem(module);
523
669
  installHttpRuntime(module, transport);
670
+ installUnaryRpcRuntime(module, transport);
524
671
  installHostFileRuntime(module);
525
672
  installExternalDirectoryRuntime(module);
673
+ installFilesystemProjectionRuntime(module);
526
674
  bindings = new HyperDBBindings(module);
527
675
  transport.postMessage({ type: "ready" });
528
676
  for (const request of queuedRequests.splice(0)) handle(request);
@@ -608,6 +756,32 @@
608
756
  files.sort();
609
757
  return files;
610
758
  }
759
+ async listEntries(locator, path) {
760
+ const handle = await this.openDirectory(this.getDirectory(locator), directorySegments(path));
761
+ const entries = [];
762
+ for await (const child of handle.values()) {
763
+ validateSegment(child.name);
764
+ if (child.kind !== "file" && child.kind !== "directory") throw new Error("External directory contains an unsupported entry");
765
+ entries.push({ name: child.name, kind: child.kind });
766
+ }
767
+ entries.sort((left, right) => left.name.localeCompare(right.name));
768
+ return entries;
769
+ }
770
+ async lookup(locator, path) {
771
+ const directory = this.getDirectory(locator);
772
+ const segments = relativeSegments(path);
773
+ const parent = await this.openDirectory(directory, segments.slice(0, -1));
774
+ const name = segments.at(-1);
775
+ if (!name) return { kind: "directory" };
776
+ for await (const child of parent.values()) {
777
+ validateSegment(child.name);
778
+ if (child.name !== name) continue;
779
+ if (child.kind === "directory") return { kind: "directory" };
780
+ if (child.kind === "file") return { kind: "file", ...await this.open(locator, path) };
781
+ throw new Error("External directory contains an unsupported entry");
782
+ }
783
+ return void 0;
784
+ }
611
785
  async open(locator, path) {
612
786
  const directory = this.getDirectory(locator);
613
787
  const cached = directory.files.get(path);
@@ -757,6 +931,15 @@
757
931
  if (directory.mode !== "readwrite") throw new Error("External directory registration is not writable");
758
932
  return directory;
759
933
  }
934
+ async openDirectory(directory, segments) {
935
+ let handle = directory.handle;
936
+ for (const segment of segments) {
937
+ const child = await handle.getDirectoryHandle(segment);
938
+ if (child.kind !== "directory" || child.name !== segment) throw new Error("External directory returned an unexpected child handle");
939
+ handle = child;
940
+ }
941
+ return handle;
942
+ }
760
943
  getOpenWriter(writer) {
761
944
  nonnegativeSafeInteger(writer, "External directory writer");
762
945
  const state = this.writers.get(writer);
@@ -805,8 +988,7 @@
805
988
  }
806
989
  var storagePath = normalizeStoragePath(configuredStoragePath);
807
990
  function isPthreadWorker() {
808
- const location = self.location;
809
- return self.name === "em-pthread" || location.search?.includes("pthread") === true;
991
+ return self.name === "em-pthread";
810
992
  }
811
993
  if (isPthreadWorker()) {
812
994
  self.importScripts(resolvedEngineUrl.href);
@@ -854,7 +1036,7 @@
854
1036
  runtime.hyperdbWasmStoragePath = storagePath;
855
1037
  runtime.resolveHyperDBPath = (path) => {
856
1038
  const relativePath = normalizeStoragePath(path);
857
- return `${storagePath ? `/mnt/fs/${storagePath}` : "/mnt/fs"}/${relativePath}`;
1039
+ return `/mnt/fs/${relativePath}`;
858
1040
  };
859
1041
  });
860
1042
  self.importScripts(resolvedEngineUrl.href);