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.
- package/dist/api.d.ts +17 -19
- package/dist/browser.d.ts +4 -3
- package/dist/browser.js +4337 -2958
- package/dist/browser_external_directory.d.ts +13 -0
- package/dist/browser_worker.js +294 -112
- package/dist/client.d.ts +8 -6
- package/dist/hyperdb-wasm.cjs +1 -1
- package/dist/hyperdb-wasm.js +1 -1
- package/dist/hyperdb-wasm.wasm.br +0 -0
- package/dist/index.d.ts +10 -9
- package/dist/index.node.d.ts +5 -4
- package/dist/node.d.ts +3 -2
- package/dist/node.js +4371 -3007
- package/dist/node_external_directory.d.ts +11 -0
- package/dist/node_worker.js +329 -126
- package/dist/protocol.d.ts +71 -19
- package/dist/raw.browser.js +242 -81
- package/dist/raw.d.ts +2 -1
- package/dist/raw.node.js +237 -77
- package/dist/wasm_bindings.d.ts +21 -1
- package/dist/worker_setup.d.ts +8 -2
- package/package.json +1 -1
package/dist/node_worker.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/node_worker.ts
|
|
2
|
-
import { closeSync, createReadStream, fstatSync, openSync, read } from "node:fs";
|
|
2
|
+
import { closeSync, createReadStream, fstatSync, openSync, read, statSync } from "node:fs";
|
|
3
3
|
import { opendir as opendir2, stat } from "node:fs/promises";
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
5
|
import { basename as basename2, dirname, isAbsolute as isAbsolute2, join as join2, posix, resolve as resolve2 } from "node:path";
|
|
@@ -21,9 +21,18 @@ function safeInteger(value, name) {
|
|
|
21
21
|
if (typeof number !== "number" || !Number.isSafeInteger(number) || number < 0) throw new Error(`${name} must be a nonnegative safe integer`);
|
|
22
22
|
return number;
|
|
23
23
|
}
|
|
24
|
+
function validateCStringContents(value, name) {
|
|
25
|
+
if (value.includes("\0")) throw new Error(`${name} must not contain NUL`);
|
|
26
|
+
}
|
|
24
27
|
function validateCString(value, name) {
|
|
25
28
|
if (!value) throw new Error(`${name} must not be empty`);
|
|
26
|
-
|
|
29
|
+
validateCStringContents(value, name);
|
|
30
|
+
}
|
|
31
|
+
function resultError(result) {
|
|
32
|
+
if (result.state === "error" || result.state === "busy") {
|
|
33
|
+
return typeof result.error === "string" ? result.error : result.error.message;
|
|
34
|
+
}
|
|
35
|
+
return `unexpected Wasm result state: ${result.state}`;
|
|
27
36
|
}
|
|
28
37
|
function decodeStructuredError(payload) {
|
|
29
38
|
if (!structuredErrorMagic.every((byte, index) => payload[index] === byte)) throw new Error("structured error has invalid magic");
|
|
@@ -61,6 +70,8 @@ var HyperDBBindings = class {
|
|
|
61
70
|
registerExternalFile;
|
|
62
71
|
registerExternalDirectory;
|
|
63
72
|
removeExternalRegistration;
|
|
73
|
+
projectExternalFile;
|
|
74
|
+
projectExternalDirectory;
|
|
64
75
|
startQuery;
|
|
65
76
|
insertArrowIPCFromPath;
|
|
66
77
|
pollQuery;
|
|
@@ -70,6 +81,7 @@ var HyperDBBindings = class {
|
|
|
70
81
|
resultPayloadLength;
|
|
71
82
|
free;
|
|
72
83
|
nextTemporaryFile = 1;
|
|
84
|
+
mountedPaths = /* @__PURE__ */ new Set();
|
|
73
85
|
constructor(module2) {
|
|
74
86
|
this.module = module2;
|
|
75
87
|
this.initialize = module2.cwrap("hyperdb_wasm_initialize", "pointer", ["string", "string"]);
|
|
@@ -79,6 +91,8 @@ var HyperDBBindings = class {
|
|
|
79
91
|
this.registerExternalFile = module2.cwrap("hyperdb_wasm_register_external_file", "pointer", ["string", "string", "number"]);
|
|
80
92
|
this.registerExternalDirectory = module2.cwrap("hyperdb_wasm_register_external_directory", "pointer", ["string", "string", "number"]);
|
|
81
93
|
this.removeExternalRegistration = module2.cwrap("hyperdb_wasm_remove_external_registration", "pointer", ["string"]);
|
|
94
|
+
this.projectExternalFile = module2.cwrap("hyperdb_wasm_project_external_file", "pointer", ["string", "string", "number"]);
|
|
95
|
+
this.projectExternalDirectory = module2.cwrap("hyperdb_wasm_project_external_directory", "pointer", ["string", "string"]);
|
|
82
96
|
this.startQuery = module2.cwrap("hyperdb_wasm_query_start", "pointer", ["number", "string", "number"]);
|
|
83
97
|
this.insertArrowIPCFromPath = module2.cwrap("hyperdb_wasm_arrow_insert_from_path", "pointer", ["number", "string", "string", "string", "number"]);
|
|
84
98
|
this.pollQuery = module2.cwrap("hyperdb_wasm_query_poll", "pointer", ["number"]);
|
|
@@ -99,77 +113,40 @@ var HyperDBBindings = class {
|
|
|
99
113
|
this.module.FS.unlink(request.path);
|
|
100
114
|
return { response: { id: request.id, type: "response", result: { state: "ok", payload: new Uint8Array() } }, transfer: [] };
|
|
101
115
|
}
|
|
102
|
-
if (request.type === "
|
|
116
|
+
if (request.type === "mount" && request.source.kind === "node-path") {
|
|
103
117
|
if (!this.module.externalFileBackend) {
|
|
104
118
|
return { response: { id: request.id, type: "response", result: { state: "error", error: "Host external files are supported only by the Node client" } }, transfer: [] };
|
|
105
119
|
}
|
|
106
|
-
const registration = this.module.externalFileBackend.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const result2 = this.decodeResult(pointer2);
|
|
110
|
-
if (result2.state === "ok") {
|
|
111
|
-
const path = new TextDecoder("utf-8", { fatal: true }).decode(result2.payload);
|
|
112
|
-
try {
|
|
113
|
-
this.module.externalFileBackend.registered(path, registration.locator);
|
|
114
|
-
} catch (error) {
|
|
115
|
-
const cleanupPointer = safeInteger(this.removeExternalRegistration(path), "result pointer");
|
|
116
|
-
this.decodeResult(cleanupPointer);
|
|
117
|
-
throw error;
|
|
118
|
-
}
|
|
119
|
-
} else {
|
|
120
|
-
this.module.externalFileBackend.removed(registration.locator);
|
|
121
|
-
}
|
|
122
|
-
return { response: { id: request.id, type: "response", result: result2 }, transfer: [] };
|
|
123
|
-
} catch (error) {
|
|
124
|
-
this.module.externalFileBackend.removed(registration.locator);
|
|
125
|
-
throw error;
|
|
126
|
-
}
|
|
120
|
+
const registration = this.module.externalFileBackend.mount(request.source.path, request.mode);
|
|
121
|
+
if (registration.kind === "directory") return this.mountDirectory(request, request.source.path);
|
|
122
|
+
return this.mountFile(request, registration.locator, registration.size, this.module.externalFileBackend);
|
|
127
123
|
}
|
|
128
|
-
if (request.type === "
|
|
124
|
+
if (request.type === "mount" && request.source.kind === "browser-file" && !this.module.externalFileBackendAvailable) {
|
|
129
125
|
return { response: { id: request.id, type: "response", result: { state: "error", error: "Blob external files are supported only by the browser client" } }, transfer: [] };
|
|
130
126
|
}
|
|
131
|
-
if (request.type === "
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const registration = backend.register("handle" in request ? request.handle : request.path, request.mode);
|
|
137
|
-
try {
|
|
138
|
-
const pointer2 = safeInteger(this.registerExternalDirectory(registration.name, registration.locator, Number(request.mode === "readwrite")), "result pointer");
|
|
139
|
-
const result2 = this.decodeResult(pointer2);
|
|
140
|
-
if (result2.state === "ok") {
|
|
141
|
-
const path = new TextDecoder("utf-8", { fatal: true }).decode(result2.payload);
|
|
142
|
-
try {
|
|
143
|
-
backend.registered(path, registration.locator);
|
|
144
|
-
} catch (error) {
|
|
145
|
-
const cleanupPointer = safeInteger(this.removeExternalRegistration(path), "result pointer");
|
|
146
|
-
this.decodeResult(cleanupPointer);
|
|
147
|
-
throw error;
|
|
148
|
-
}
|
|
149
|
-
} else {
|
|
150
|
-
const completion = completionFor([backend.removed(registration.locator)]);
|
|
151
|
-
return { response: { id: request.id, type: "response", result: result2 }, transfer: [], ...completion && { completion } };
|
|
152
|
-
}
|
|
153
|
-
return { response: { id: request.id, type: "response", result: result2 }, transfer: [] };
|
|
154
|
-
} catch (error) {
|
|
155
|
-
void Promise.resolve(backend.removed(registration.locator)).catch(() => void 0);
|
|
156
|
-
throw error;
|
|
157
|
-
}
|
|
127
|
+
if (request.type === "mount" && request.source.kind === "browser-directory") {
|
|
128
|
+
return this.mountDirectory(request, request.source.handle);
|
|
129
|
+
}
|
|
130
|
+
if (request.type === "mount" && request.source.kind === "browser-file") {
|
|
131
|
+
return this.mountFile(request, request.source.url, safeInteger(request.source.size, "external file size"));
|
|
158
132
|
}
|
|
159
|
-
if (request.type === "
|
|
133
|
+
if (request.type === "unmount" && (this.module.externalFileBackend || this.module.externalDirectoryBackend)) {
|
|
160
134
|
validateCString(request.path, "external registration path");
|
|
161
135
|
const pointer2 = safeInteger(this.removeExternalRegistration(request.path), "result pointer");
|
|
162
136
|
const result2 = this.decodeResult(pointer2);
|
|
163
|
-
if (result2.state === "ok") {
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
137
|
+
if (result2.state === "ok" || result2.state === "error" && result2.error === "External registration was not found" && this.mountedPaths.has(request.path)) {
|
|
138
|
+
const cleanup = async () => {
|
|
139
|
+
await Promise.all([
|
|
140
|
+
this.module.externalFileBackend?.removed(request.path),
|
|
141
|
+
this.module.externalDirectoryBackend?.removed(request.path)
|
|
142
|
+
]);
|
|
143
|
+
this.mountedPaths.delete(request.path);
|
|
144
|
+
};
|
|
145
|
+
return { response: { id: request.id, type: "response", result: { state: "ok", payload: new Uint8Array() } }, transfer: [], completion: cleanup() };
|
|
169
146
|
}
|
|
170
147
|
return { response: { id: request.id, type: "response", result: result2 }, transfer: [] };
|
|
171
148
|
}
|
|
172
|
-
if (request.type === "
|
|
149
|
+
if (request.type === "unmount" && !this.module.externalFileBackendAvailable) {
|
|
173
150
|
return { response: { id: request.id, type: "response", result: { state: "error", error: "External files are not supported by this client" } }, transfer: [] };
|
|
174
151
|
}
|
|
175
152
|
const pointer = safeInteger(this.invoke(request), "result pointer");
|
|
@@ -192,6 +169,7 @@ var HyperDBBindings = class {
|
|
|
192
169
|
switch (request.type) {
|
|
193
170
|
case "initialize":
|
|
194
171
|
validateCString(request.settings, "settings");
|
|
172
|
+
validateCStringContents(this.module.hyperdbWasmStoragePath ?? "", "storage path");
|
|
195
173
|
return this.initialize(request.settings, this.module.hyperdbWasmStoragePath ?? "");
|
|
196
174
|
case "connect":
|
|
197
175
|
return this.connect();
|
|
@@ -199,20 +177,19 @@ var HyperDBBindings = class {
|
|
|
199
177
|
return this.disconnect(request.connection);
|
|
200
178
|
case "shutdown":
|
|
201
179
|
return this.shutdown();
|
|
202
|
-
case "
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
validateCString(request.url, "external file URL");
|
|
206
|
-
return this.registerExternalFile(request.name, request.url, safeInteger(request.size, "external file size"));
|
|
207
|
-
case "registerExternalDirectory":
|
|
208
|
-
throw new Error("external directory request has no backend");
|
|
209
|
-
case "removeExternalRegistration":
|
|
180
|
+
case "mount":
|
|
181
|
+
throw new Error("mount request has no backend");
|
|
182
|
+
case "unmount":
|
|
210
183
|
validateCString(request.path, "external registration path");
|
|
211
184
|
return this.removeExternalRegistration(request.path);
|
|
212
185
|
case "startQuery":
|
|
186
|
+
validateCStringContents(request.sql, "SQL");
|
|
213
187
|
return this.startQuery(request.connection, request.sql, Number(request.sqlTestMode ?? false));
|
|
214
188
|
case "insertArrowIPCFromPath": {
|
|
215
|
-
const path = request.internal ? request.path : this.module.resolveHyperDBPath?.(request.path) ?? request.path;
|
|
189
|
+
const path = request.internal || request.path.startsWith("/mnt/external/") ? request.path : this.module.resolveHyperDBPath?.(request.path) ?? request.path;
|
|
190
|
+
validateCString(path, "Arrow IPC input path");
|
|
191
|
+
validateCString(request.name, "Arrow insert table name");
|
|
192
|
+
validateCStringContents(request.schema ?? "", "Arrow insert schema name");
|
|
216
193
|
return this.insertArrowIPCFromPath(request.connection, path, request.name, request.schema ?? "", Number(request.create));
|
|
217
194
|
}
|
|
218
195
|
case "pollQuery":
|
|
@@ -223,6 +200,90 @@ var HyperDBBindings = class {
|
|
|
223
200
|
return this.finishQuery(request.query);
|
|
224
201
|
}
|
|
225
202
|
}
|
|
203
|
+
mountFile(request, locator, size, backend) {
|
|
204
|
+
validateCString(request.path, "external registration path");
|
|
205
|
+
validateCString(locator, "external file locator");
|
|
206
|
+
let path;
|
|
207
|
+
let registered = false;
|
|
208
|
+
let rollbackError;
|
|
209
|
+
try {
|
|
210
|
+
const result = this.decodeResult(safeInteger(this.registerExternalFile(request.path, locator, size), "result pointer"));
|
|
211
|
+
if (result.state !== "ok") {
|
|
212
|
+
backend?.removed(locator);
|
|
213
|
+
return { response: { id: request.id, type: "response", result }, transfer: [] };
|
|
214
|
+
}
|
|
215
|
+
path = new TextDecoder("utf-8", { fatal: true }).decode(result.payload);
|
|
216
|
+
const projection = this.decodeResult(safeInteger(this.projectExternalFile(path, locator, size), "result pointer"));
|
|
217
|
+
if (projection.state !== "ok") throw new Error(resultError(projection));
|
|
218
|
+
backend?.registered(path, locator);
|
|
219
|
+
registered = true;
|
|
220
|
+
this.mountedPaths.add(path);
|
|
221
|
+
return { response: { id: request.id, type: "response", result }, transfer: [] };
|
|
222
|
+
} catch (error) {
|
|
223
|
+
if (path !== void 0) {
|
|
224
|
+
try {
|
|
225
|
+
const rollback = this.decodeResult(safeInteger(this.removeExternalRegistration(path), "result pointer"));
|
|
226
|
+
if (rollback.state !== "ok") rollbackError = new Error(resultError(rollback));
|
|
227
|
+
} catch (failure) {
|
|
228
|
+
rollbackError = failure;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (rollbackError !== void 0) {
|
|
232
|
+
if (path !== void 0) {
|
|
233
|
+
backend?.registered(path, locator);
|
|
234
|
+
this.mountedPaths.add(path);
|
|
235
|
+
}
|
|
236
|
+
throw new Error(`mount failed and rollback also failed: ${rollbackError instanceof Error ? rollbackError.message : String(rollbackError)}`, { cause: error });
|
|
237
|
+
}
|
|
238
|
+
backend?.removed(registered && path !== void 0 ? path : locator);
|
|
239
|
+
throw error;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
mountDirectory(request, source) {
|
|
243
|
+
const backend = this.module.externalDirectoryBackend;
|
|
244
|
+
if (!backend) {
|
|
245
|
+
return { response: { id: request.id, type: "response", result: { state: "error", error: "External directories are not supported by this client" } }, transfer: [] };
|
|
246
|
+
}
|
|
247
|
+
validateCString(request.path, "external registration path");
|
|
248
|
+
const registration = backend.register(source, request.mode);
|
|
249
|
+
let preserveBackend = false;
|
|
250
|
+
try {
|
|
251
|
+
validateCString(registration.locator, "external directory locator");
|
|
252
|
+
const pointer = safeInteger(this.registerExternalDirectory(request.path, registration.locator, Number(request.mode === "readwrite")), "result pointer");
|
|
253
|
+
const result = this.decodeResult(pointer);
|
|
254
|
+
if (result.state === "ok") {
|
|
255
|
+
const path = new TextDecoder("utf-8", { fatal: true }).decode(result.payload);
|
|
256
|
+
try {
|
|
257
|
+
const projectionPointer = this.projectExternalDirectory(path, registration.locator);
|
|
258
|
+
const projection = this.decodeResult(safeInteger(projectionPointer, "result pointer"));
|
|
259
|
+
if (projection.state !== "ok") throw new Error(resultError(projection));
|
|
260
|
+
backend.registered(path, registration.locator);
|
|
261
|
+
this.mountedPaths.add(path);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
const cleanupPointer = safeInteger(this.removeExternalRegistration(path), "result pointer");
|
|
264
|
+
const cleanup = this.decodeResult(cleanupPointer);
|
|
265
|
+
if (cleanup.state !== "ok") {
|
|
266
|
+
backend.registered(path, registration.locator);
|
|
267
|
+
this.mountedPaths.add(path);
|
|
268
|
+
preserveBackend = true;
|
|
269
|
+
throw new Error(`mount failed and rollback also failed: ${resultError(cleanup)}`, { cause: error });
|
|
270
|
+
}
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
const completion = completionFor([backend.removed(registration.locator)]);
|
|
275
|
+
return { response: { id: request.id, type: "response", result }, transfer: [], ...completion && { completion } };
|
|
276
|
+
}
|
|
277
|
+
return { response: { id: request.id, type: "response", result }, transfer: [] };
|
|
278
|
+
} catch (error) {
|
|
279
|
+
if (preserveBackend) throw error;
|
|
280
|
+
return {
|
|
281
|
+
response: { id: request.id, type: "response", result: { state: "error", error: error instanceof Error ? error.message : String(error) } },
|
|
282
|
+
transfer: [],
|
|
283
|
+
completion: Promise.resolve(backend.removed(registration.locator))
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
226
287
|
decodeResult(pointer) {
|
|
227
288
|
if (pointer === 0) return { state: "error", error: "out of memory" };
|
|
228
289
|
try {
|
|
@@ -260,6 +321,11 @@ var HyperDBBindings = class {
|
|
|
260
321
|
};
|
|
261
322
|
|
|
262
323
|
// src/protocol.ts
|
|
324
|
+
function isUnaryRpcResult(value) {
|
|
325
|
+
if (!isRecord(value) || !Number.isSafeInteger(value.status) || value.status < 0 || value.status > 16) return false;
|
|
326
|
+
if (value.status === 0) return hasKeys(value, ["status", "response"]) && value.response instanceof Uint8Array;
|
|
327
|
+
return (hasKeys(value, ["status", "message"]) || hasKeys(value, ["status", "message", "details"])) && typeof value.message === "string" && (value.details === void 0 || value.details instanceof Uint8Array);
|
|
328
|
+
}
|
|
263
329
|
function isRecord(value) {
|
|
264
330
|
return typeof value === "object" && value !== null;
|
|
265
331
|
}
|
|
@@ -287,12 +353,21 @@ function isEngineRequest(value) {
|
|
|
287
353
|
return hasKeys(value, ["id", "type", "bytes"]) && value.bytes instanceof Uint8Array;
|
|
288
354
|
case "removeFile":
|
|
289
355
|
return hasKeys(value, ["id", "type", "path"]) && typeof value.path === "string";
|
|
290
|
-
case "
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
356
|
+
case "mount": {
|
|
357
|
+
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;
|
|
358
|
+
switch (value.source.kind) {
|
|
359
|
+
case "browser-file":
|
|
360
|
+
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;
|
|
361
|
+
case "browser-directory":
|
|
362
|
+
return hasKeys(value.source, ["kind", "handle"]) && isRecord(value.source.handle) && value.source.handle.kind === "directory";
|
|
363
|
+
case "node-path":
|
|
364
|
+
return hasKeys(value.source, ["kind", "path"]) && typeof value.source.path === "string" && !!value.source.path && !value.source.path.includes("\0");
|
|
365
|
+
default:
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
case "unmount":
|
|
370
|
+
return hasKeys(value, ["id", "type", "path"]) && typeof value.path === "string" && !!value.path && !value.path.includes("\0");
|
|
296
371
|
case "pollQuery":
|
|
297
372
|
case "cancelQuery":
|
|
298
373
|
case "finishQuery":
|
|
@@ -301,8 +376,48 @@ function isEngineRequest(value) {
|
|
|
301
376
|
return false;
|
|
302
377
|
}
|
|
303
378
|
}
|
|
379
|
+
function isWorkerRequest(value) {
|
|
380
|
+
if (isEngineRequest(value)) return true;
|
|
381
|
+
return isRecord(value) && value.type === "unaryRpcComplete" && hasKeys(value, ["type", "callId", "result"]) && Number.isSafeInteger(value.callId) && value.callId > 0 && isUnaryRpcResult(value.result);
|
|
382
|
+
}
|
|
304
383
|
|
|
305
384
|
// src/worker_setup.ts
|
|
385
|
+
function parseMetadata(lines) {
|
|
386
|
+
const result = /* @__PURE__ */ Object.create(null);
|
|
387
|
+
for (const line of lines.split("\n")) {
|
|
388
|
+
if (!line) continue;
|
|
389
|
+
const separator = line.indexOf(":");
|
|
390
|
+
if (separator <= 0) throw new Error("Unary RPC metadata is malformed");
|
|
391
|
+
result[line.slice(0, separator)] = line.slice(separator + 1);
|
|
392
|
+
}
|
|
393
|
+
return result;
|
|
394
|
+
}
|
|
395
|
+
function completeUnaryRpc(module2, completion) {
|
|
396
|
+
try {
|
|
397
|
+
if (completion.result.status === 0) {
|
|
398
|
+
const response = completion.result.response;
|
|
399
|
+
module2._hyperdb_wasm_unary_rpc_complete(completion.callId, 0, 0, 0, 0, response.byteLength ? allocate(module2, response) : 0, response.byteLength);
|
|
400
|
+
} else {
|
|
401
|
+
const messagePointer = safePointer(module2.stringToNewUTF8(completion.result.message), "unary RPC message pointer");
|
|
402
|
+
const details = completion.result.details;
|
|
403
|
+
module2._hyperdb_wasm_unary_rpc_complete(completion.callId, completion.result.status, messagePointer, details?.byteLength ? allocate(module2, details) : 0, details?.byteLength ?? 0, 0, 0);
|
|
404
|
+
}
|
|
405
|
+
} catch (error) {
|
|
406
|
+
const messagePointer = safePointer(module2.stringToNewUTF8(errorText(error)), "unary RPC bridge error pointer");
|
|
407
|
+
module2._hyperdb_wasm_unary_rpc_complete(completion.callId, 2, messagePointer, 0, 0, 0, 0);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
function installUnaryRpcRuntime(module2, transport2) {
|
|
411
|
+
module2.hyperdbWasmUnaryRpcStart = (callId, host, port, method, metadataLines, requestPointer, requestLength, timeoutMs) => {
|
|
412
|
+
const pointer = safePointer(requestPointer, "unary RPC request pointer");
|
|
413
|
+
const end = safePointer(pointer + requestLength, "unary RPC request end");
|
|
414
|
+
module2.getValue(pointer, "i8");
|
|
415
|
+
if (end > module2.HEAPU8.byteLength) throw new Error("Unary RPC request exceeds the wasm heap");
|
|
416
|
+
const request = module2.HEAPU8.slice(pointer, end);
|
|
417
|
+
transport2.postMessage({ type: "unaryRpc", callId, endpoint: { host, port }, method, request, metadata: parseMetadata(metadataLines), timeoutMs }, [request.buffer]);
|
|
418
|
+
};
|
|
419
|
+
module2.hyperdbWasmUnaryRpcCancel = (callId) => transport2.postMessage({ type: "unaryRpcCancel", callId });
|
|
420
|
+
}
|
|
306
421
|
function errorText(error) {
|
|
307
422
|
return error instanceof Error ? error.message : String(error);
|
|
308
423
|
}
|
|
@@ -334,6 +449,11 @@ function completeExternalDirectoryOperation(module2, id, complete, operation) {
|
|
|
334
449
|
);
|
|
335
450
|
}
|
|
336
451
|
function installHttpRuntime(module2, transport2) {
|
|
452
|
+
const fetches = /* @__PURE__ */ new Map();
|
|
453
|
+
const completeFetch = (id, pointer, length) => {
|
|
454
|
+
if (!fetches.delete(id)) return;
|
|
455
|
+
module2._hyperdb_wasm_fetch_complete(id, pointer, length);
|
|
456
|
+
};
|
|
337
457
|
module2.hyperdbWasmFetchStart = (id, url, offset, length, expectedSize) => {
|
|
338
458
|
if (url.startsWith("hyperdb-node-file:") || url.startsWith("hyperdb-node-host-file:") || url.startsWith("hyperdb-node-directory-file:") || url.startsWith("hyperdb-browser-file:")) {
|
|
339
459
|
const read2 = module2.externalFileBackend?.read(url, offset, length, expectedSize) ?? module2.externalDirectoryBackend?.read(url, offset, length, expectedSize);
|
|
@@ -350,63 +470,39 @@ function installHttpRuntime(module2, transport2) {
|
|
|
350
470
|
}
|
|
351
471
|
const headers = new Headers();
|
|
352
472
|
if (length > 0) headers.set("Range", `bytes=${offset}-${offset + length - 1}`);
|
|
353
|
-
|
|
473
|
+
const controller = new AbortController();
|
|
474
|
+
fetches.set(id, controller);
|
|
475
|
+
void fetch(url, { headers, redirect: "manual", signal: controller.signal }).then(async (response) => {
|
|
354
476
|
if (!response.ok || response.type === "opaqueredirect" || (length === 0 ? response.status !== 200 : response.status !== 206)) {
|
|
355
477
|
void response.body?.cancel().catch(() => void 0);
|
|
356
|
-
|
|
478
|
+
completeFetch(id, 0, -1);
|
|
357
479
|
return;
|
|
358
480
|
}
|
|
359
481
|
if (length > 0) {
|
|
360
482
|
const contentRange = /^bytes (\d+)-(\d+)\/(\d+)$/.exec(response.headers.get("content-range") ?? "");
|
|
361
483
|
if (!contentRange || contentRange[1] !== String(offset) || contentRange[2] !== String(offset + length - 1) || expectedSize >= 0 && contentRange[3] !== String(expectedSize)) {
|
|
362
484
|
void response.body?.cancel().catch(() => void 0);
|
|
363
|
-
|
|
485
|
+
completeFetch(id, 0, -1);
|
|
364
486
|
return;
|
|
365
487
|
}
|
|
366
488
|
}
|
|
367
489
|
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
368
490
|
if (length > 0 && bytes.byteLength !== length) {
|
|
369
|
-
|
|
491
|
+
completeFetch(id, 0, -1);
|
|
370
492
|
return;
|
|
371
493
|
}
|
|
372
494
|
try {
|
|
373
|
-
|
|
495
|
+
completeFetch(id, allocate(module2, bytes), bytes.byteLength);
|
|
374
496
|
} catch (error) {
|
|
375
497
|
transport2.postMessage({ type: "log", text: `fetch failed: ${errorText(error)}` });
|
|
376
|
-
|
|
498
|
+
completeFetch(id, 0, -1);
|
|
377
499
|
}
|
|
378
500
|
}).catch(() => {
|
|
379
501
|
transport2.postMessage({ type: "log", text: "fetch failed" });
|
|
380
|
-
|
|
381
|
-
});
|
|
382
|
-
};
|
|
383
|
-
module2.hyperdbWasmHttpStart = (id, method, url, headerLines, bodyPointer, bodyLength) => {
|
|
384
|
-
const headers = new Headers();
|
|
385
|
-
for (const line of headerLines.split("\n")) {
|
|
386
|
-
const separator = line.indexOf(":");
|
|
387
|
-
if (separator > 0) headers.set(line.slice(0, separator), line.slice(separator + 1));
|
|
388
|
-
}
|
|
389
|
-
const pointer = safePointer(bodyPointer, "HTTP body pointer");
|
|
390
|
-
const bodyEnd = safePointer(pointer + bodyLength, "HTTP body end");
|
|
391
|
-
if (bodyEnd > module2.HEAPU8.byteLength) throw new Error("HTTP body exceeds the wasm heap");
|
|
392
|
-
const body = bodyLength > 0 ? module2.HEAPU8.slice(pointer, bodyEnd) : void 0;
|
|
393
|
-
const init = { method, headers };
|
|
394
|
-
if (body) init.body = body;
|
|
395
|
-
void fetch(url, init).then(async (response) => {
|
|
396
|
-
const responseBody = new Uint8Array(await response.arrayBuffer());
|
|
397
|
-
let responseHeaders = "";
|
|
398
|
-
response.headers.forEach((value, name) => {
|
|
399
|
-
responseHeaders += `${name.toLowerCase()}:${value}
|
|
400
|
-
`;
|
|
401
|
-
});
|
|
402
|
-
const headersPointer = responseHeaders ? safePointer(module2.stringToNewUTF8(responseHeaders), "headers pointer") : 0;
|
|
403
|
-
const responseBodyPointer = responseBody.byteLength ? allocate(module2, responseBody) : 0;
|
|
404
|
-
module2._hyperdb_wasm_http_complete(id, response.status, headersPointer, responseBodyPointer, responseBody.byteLength);
|
|
405
|
-
}).catch((error) => {
|
|
406
|
-
const errorBytes = new TextEncoder().encode(errorText(error));
|
|
407
|
-
module2._hyperdb_wasm_http_complete(id, -1, 0, allocate(module2, errorBytes), errorBytes.byteLength);
|
|
502
|
+
completeFetch(id, 0, -1);
|
|
408
503
|
});
|
|
409
504
|
};
|
|
505
|
+
module2.hyperdbWasmFetchCancel = (id) => fetches.get(id)?.abort();
|
|
410
506
|
}
|
|
411
507
|
function installExternalDirectoryRuntime(module2) {
|
|
412
508
|
const backend = module2.externalDirectoryBackend;
|
|
@@ -499,6 +595,54 @@ function installHostFileRuntime(module2) {
|
|
|
499
595
|
};
|
|
500
596
|
}
|
|
501
597
|
}
|
|
598
|
+
function installFilesystemProjectionRuntime(module2) {
|
|
599
|
+
const complete = module2._hyperdb_wasm_filesystem_operation_complete;
|
|
600
|
+
if (!complete) return;
|
|
601
|
+
module2.hyperdbWasmFilesystemListStart = (id, locator, path) => {
|
|
602
|
+
const listing = module2.externalDirectoryBackend?.listEntries?.(locator, path);
|
|
603
|
+
if (!listing) {
|
|
604
|
+
complete(id, 0, -1);
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
void listing.then((entries) => {
|
|
608
|
+
const encoded = entries.map((entry) => `${entry.kind === "file" ? "f" : "d"}:${entry.name}`);
|
|
609
|
+
complete(id, safePointer(module2.stringToNewUTF8(JSON.stringify(encoded)), "filesystem listing pointer"), 0);
|
|
610
|
+
}).catch(() => complete(id, 0, -1));
|
|
611
|
+
};
|
|
612
|
+
module2.hyperdbWasmFilesystemLookupStart = (id, locator, path) => {
|
|
613
|
+
const lookup = module2.externalDirectoryBackend?.lookup?.(locator, path);
|
|
614
|
+
if (!lookup) {
|
|
615
|
+
complete(id, 0, -1);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
void lookup.then((entry) => {
|
|
619
|
+
if (entry === void 0) complete(id, 0, -2);
|
|
620
|
+
else if (entry.kind === "directory") complete(id, 0, -21);
|
|
621
|
+
else complete(id, safePointer(module2.stringToNewUTF8(entry.locator), "filesystem file locator pointer"), entry.size);
|
|
622
|
+
}).catch(() => complete(id, 0, -1));
|
|
623
|
+
};
|
|
624
|
+
module2.hyperdbWasmFilesystemReadStart = (id, locator, offset, length, expectedSize, pointerValue) => {
|
|
625
|
+
const read2 = module2.externalFileBackend?.read(locator, offset, length, expectedSize) ?? module2.externalDirectoryBackend?.read(locator, offset, length, expectedSize) ?? (locator.startsWith("blob:") ? fetch(locator, { headers: { Range: `bytes=${offset}-${offset + length - 1}` } }).then(async (response) => {
|
|
626
|
+
const contentRange = /^bytes (\d+)-(\d+)\/(\d+)$/.exec(response.headers.get("content-range") ?? "");
|
|
627
|
+
if (response.status !== 206 || !contentRange || contentRange[1] !== String(offset) || contentRange[2] !== String(offset + length - 1) || contentRange[3] !== String(expectedSize)) {
|
|
628
|
+
throw new Error("Blob URL returned an invalid byte range");
|
|
629
|
+
}
|
|
630
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
631
|
+
}) : void 0);
|
|
632
|
+
if (!read2) {
|
|
633
|
+
complete(id, 0, -1);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
void read2.then((bytes) => {
|
|
637
|
+
const pointer = safePointer(pointerValue, "filesystem read pointer");
|
|
638
|
+
const end = safePointer(pointer + bytes.byteLength, "filesystem read end");
|
|
639
|
+
module2.getValue(pointer, "i8");
|
|
640
|
+
if (bytes.byteLength !== length || end > module2.HEAPU8.byteLength) throw new Error("filesystem read returned an invalid payload");
|
|
641
|
+
module2.HEAPU8.set(bytes, pointer);
|
|
642
|
+
complete(id, 0, bytes.byteLength);
|
|
643
|
+
}).catch(() => complete(id, 0, -1));
|
|
644
|
+
};
|
|
645
|
+
}
|
|
502
646
|
function configureWorker(module2, transport2, configureFilesystem = () => void 0) {
|
|
503
647
|
const queuedRequests = [];
|
|
504
648
|
let bindings;
|
|
@@ -522,7 +666,9 @@ function configureWorker(module2, transport2, configureFilesystem = () => void 0
|
|
|
522
666
|
}
|
|
523
667
|
};
|
|
524
668
|
transport2.onMessage((message) => {
|
|
525
|
-
if (
|
|
669
|
+
if (!isWorkerRequest(message)) return;
|
|
670
|
+
if (message.type === "unaryRpcComplete") completeUnaryRpc(module2, message);
|
|
671
|
+
else handle(message);
|
|
526
672
|
});
|
|
527
673
|
module2.print = (text) => transport2.postMessage({ type: "log", text });
|
|
528
674
|
module2.printErr = (text) => transport2.postMessage({ type: "log", text });
|
|
@@ -530,8 +676,10 @@ function configureWorker(module2, transport2, configureFilesystem = () => void 0
|
|
|
530
676
|
try {
|
|
531
677
|
configureFilesystem(module2);
|
|
532
678
|
installHttpRuntime(module2, transport2);
|
|
679
|
+
installUnaryRpcRuntime(module2, transport2);
|
|
533
680
|
installHostFileRuntime(module2);
|
|
534
681
|
installExternalDirectoryRuntime(module2);
|
|
682
|
+
installFilesystemProjectionRuntime(module2);
|
|
535
683
|
bindings = new HyperDBBindings(module2);
|
|
536
684
|
transport2.postMessage({ type: "ready" });
|
|
537
685
|
for (const request of queuedRequests.splice(0)) handle(request);
|
|
@@ -630,14 +778,22 @@ var NodeExternalDirectoryBackend = class {
|
|
|
630
778
|
for (const [writer, state] of this.writers) {
|
|
631
779
|
if (state.locator !== locator) continue;
|
|
632
780
|
if (!state.terminal) writerCleanup.push(this.abort(writer));
|
|
633
|
-
else writerCleanup.push(state.chain.then(() => this.cleanupWriter(state)).
|
|
781
|
+
else writerCleanup.push(state.chain.then(() => this.cleanupWriter(state)).then(() => {
|
|
782
|
+
this.writers.delete(writer);
|
|
783
|
+
}));
|
|
634
784
|
}
|
|
635
785
|
await Promise.all(writerCleanup);
|
|
636
786
|
const fileCleanup = [];
|
|
637
|
-
for (const fileLocator of directory.locators) {
|
|
787
|
+
for (const fileLocator of [...directory.locators]) {
|
|
638
788
|
const file = this.files.get(fileLocator);
|
|
639
|
-
if (file)
|
|
640
|
-
|
|
789
|
+
if (!file) {
|
|
790
|
+
directory.locators.delete(fileLocator);
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
fileCleanup.push(file.descriptor.close().then(() => {
|
|
794
|
+
this.files.delete(fileLocator);
|
|
795
|
+
directory.locators.delete(fileLocator);
|
|
796
|
+
}));
|
|
641
797
|
}
|
|
642
798
|
await Promise.all(fileCleanup);
|
|
643
799
|
this.directories.delete(locator);
|
|
@@ -666,6 +822,40 @@ var NodeExternalDirectoryBackend = class {
|
|
|
666
822
|
files.sort();
|
|
667
823
|
return files;
|
|
668
824
|
}
|
|
825
|
+
async listEntries(locator, path) {
|
|
826
|
+
const directory = this.getDirectory(locator);
|
|
827
|
+
const parent = await ensureDirectory(directory.root, directorySegments(path), false);
|
|
828
|
+
const entries = [];
|
|
829
|
+
const opened = await opendir(parent);
|
|
830
|
+
for await (const entry of opened) {
|
|
831
|
+
validateSegment(entry.name);
|
|
832
|
+
const child = confinedPath(directory.root, [...directorySegments(path), entry.name]);
|
|
833
|
+
if (directory.temporaryPaths.has(child)) continue;
|
|
834
|
+
const info = await lstat(child);
|
|
835
|
+
if (info.isSymbolicLink()) throw new Error("External directory must not contain symbolic links");
|
|
836
|
+
if (info.isDirectory()) entries.push({ name: entry.name, kind: "directory" });
|
|
837
|
+
else if (info.isFile()) entries.push({ name: entry.name, kind: "file" });
|
|
838
|
+
else throw new Error("External directory contains an unsupported entry");
|
|
839
|
+
}
|
|
840
|
+
entries.sort((left, right) => left.name.localeCompare(right.name));
|
|
841
|
+
return entries;
|
|
842
|
+
}
|
|
843
|
+
async lookup(locator, path) {
|
|
844
|
+
const directory = this.getDirectory(locator);
|
|
845
|
+
const segments = relativeSegments(path);
|
|
846
|
+
await ensureDirectory(directory.root, segments.slice(0, -1), false);
|
|
847
|
+
const target = confinedPath(directory.root, segments);
|
|
848
|
+
try {
|
|
849
|
+
const info = await lstat(target);
|
|
850
|
+
if (info.isSymbolicLink()) throw new Error("External directory must not contain symbolic links");
|
|
851
|
+
if (info.isDirectory()) return { kind: "directory" };
|
|
852
|
+
if (!info.isFile()) throw new Error("External directory contains an unsupported entry");
|
|
853
|
+
return { kind: "file", ...await this.open(locator, path) };
|
|
854
|
+
} catch (error) {
|
|
855
|
+
if (isNotFound(error)) return void 0;
|
|
856
|
+
throw error;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
669
859
|
async open(locator, path) {
|
|
670
860
|
const directory = this.getDirectory(locator);
|
|
671
861
|
const cached = directory.files.get(path);
|
|
@@ -788,18 +978,28 @@ var NodeExternalDirectoryBackend = class {
|
|
|
788
978
|
}
|
|
789
979
|
}
|
|
790
980
|
if (failure !== void 0) {
|
|
791
|
-
|
|
981
|
+
try {
|
|
982
|
+
await this.cleanupWriter(state);
|
|
983
|
+
} catch (cleanupError) {
|
|
984
|
+
throw new AggregateError([failure, cleanupError], "External directory writer operation and cleanup failed");
|
|
985
|
+
}
|
|
792
986
|
throw failure;
|
|
793
987
|
}
|
|
794
988
|
this.getDirectory(state.locator).files.delete(state.path);
|
|
795
989
|
}, true);
|
|
796
|
-
return operation.
|
|
990
|
+
return operation.then(() => {
|
|
991
|
+
this.writers.delete(writer);
|
|
992
|
+
});
|
|
797
993
|
}
|
|
798
994
|
abort(writer) {
|
|
799
|
-
|
|
995
|
+
nonnegativeSafeInteger(writer, "External directory writer");
|
|
996
|
+
const state = this.writers.get(writer);
|
|
997
|
+
if (!state) throw new Error("External directory writer is not open");
|
|
800
998
|
state.terminal = true;
|
|
801
999
|
const operation = this.enqueue(state, () => this.cleanupWriter(state), true);
|
|
802
|
-
return operation.
|
|
1000
|
+
return operation.then(() => {
|
|
1001
|
+
this.writers.delete(writer);
|
|
1002
|
+
});
|
|
803
1003
|
}
|
|
804
1004
|
async shutdown() {
|
|
805
1005
|
await Promise.all([...this.directories.keys()].map((locator) => this.removed(locator)));
|
|
@@ -826,13 +1026,12 @@ var NodeExternalDirectoryBackend = class {
|
|
|
826
1026
|
}
|
|
827
1027
|
async cleanupWriter(state) {
|
|
828
1028
|
this.directories.get(state.locator)?.files.delete(state.path);
|
|
829
|
-
|
|
830
|
-
await state.descriptor
|
|
831
|
-
} finally {
|
|
1029
|
+
if (state.descriptor) {
|
|
1030
|
+
await state.descriptor.close();
|
|
832
1031
|
state.descriptor = void 0;
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
1032
|
+
}
|
|
1033
|
+
if (state.temp) {
|
|
1034
|
+
await unlink(state.temp).catch(() => void 0);
|
|
836
1035
|
state.temp = void 0;
|
|
837
1036
|
}
|
|
838
1037
|
}
|
|
@@ -903,11 +1102,15 @@ configureWorker(module, transport, (runtime) => {
|
|
|
903
1102
|
}
|
|
904
1103
|
};
|
|
905
1104
|
runtime.externalFileBackend = {
|
|
906
|
-
|
|
1105
|
+
mount: (path, mode) => {
|
|
907
1106
|
const absolute = resolve2(path);
|
|
1107
|
+
const info = statSync(absolute);
|
|
1108
|
+
if (info.isDirectory()) return { kind: "directory" };
|
|
1109
|
+
if (!info.isFile()) throw new Error(`Mount path is not a regular file or directory: ${path}`);
|
|
1110
|
+
if (mode === "readwrite") throw new Error("Regular files cannot be mounted readwrite");
|
|
908
1111
|
const locator = `hyperdb-node-file:${randomUUID2()}`;
|
|
909
1112
|
const { size } = openExternalFile(absolute, locator);
|
|
910
|
-
return { name: basename2(absolute), locator, size };
|
|
1113
|
+
return { kind: "file", name: basename2(absolute), locator, size };
|
|
911
1114
|
},
|
|
912
1115
|
registered: (path, locator) => {
|
|
913
1116
|
if (!externalFiles.has(locator)) throw new Error("External file descriptor is unavailable");
|