hyperdb-wasm 0.0.26862-dev.wasm.g8c0876f6 → 0.0.26979-dev.wasm.g78ca8a1b
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 -40
- package/dist/browser.d.ts +6 -3
- package/dist/browser.js +4340 -3080
- package/dist/browser_external_directory.d.ts +13 -0
- package/dist/browser_worker.js +317 -176
- package/dist/client.d.ts +8 -13
- 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 +4 -2
- package/dist/node.js +4373 -3128
- package/dist/node_external_directory.d.ts +11 -0
- package/dist/node_worker.js +335 -179
- package/dist/protocol.d.ts +71 -50
- package/dist/raw.browser.js +246 -140
- package/dist/raw.d.ts +2 -1
- package/dist/raw.node.js +239 -135
- package/dist/wasm_bindings.d.ts +22 -8
- package/dist/worker_setup.d.ts +8 -2
- package/examples/node-attached-databases.mjs +10 -9
- package/package.json +1 -1
package/dist/browser_worker.js
CHANGED
|
@@ -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
|
-
|
|
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");
|
|
@@ -48,17 +57,12 @@
|
|
|
48
57
|
initialize;
|
|
49
58
|
connect;
|
|
50
59
|
disconnect;
|
|
51
|
-
createDatabase;
|
|
52
|
-
openDatabase;
|
|
53
|
-
listDatabases;
|
|
54
|
-
checkpointDatabase;
|
|
55
|
-
dropDatabase;
|
|
56
60
|
shutdown;
|
|
57
61
|
registerExternalFile;
|
|
58
62
|
registerExternalDirectory;
|
|
59
63
|
removeExternalRegistration;
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
projectExternalFile;
|
|
65
|
+
projectExternalDirectory;
|
|
62
66
|
startQuery;
|
|
63
67
|
insertArrowIPCFromPath;
|
|
64
68
|
pollQuery;
|
|
@@ -68,22 +72,18 @@
|
|
|
68
72
|
resultPayloadLength;
|
|
69
73
|
free;
|
|
70
74
|
nextTemporaryFile = 1;
|
|
75
|
+
mountedPaths = /* @__PURE__ */ new Set();
|
|
71
76
|
constructor(module) {
|
|
72
77
|
this.module = module;
|
|
73
|
-
this.initialize = module.cwrap("hyperdb_wasm_initialize", "pointer", ["string"]);
|
|
78
|
+
this.initialize = module.cwrap("hyperdb_wasm_initialize", "pointer", ["string", "string"]);
|
|
74
79
|
this.connect = module.cwrap("hyperdb_wasm_connect", "pointer", []);
|
|
75
80
|
this.disconnect = module.cwrap("hyperdb_wasm_disconnect", "pointer", ["number"]);
|
|
76
|
-
this.createDatabase = module.cwrap("hyperdb_wasm_create_database", "pointer", ["string", "number"]);
|
|
77
|
-
this.openDatabase = module.cwrap("hyperdb_wasm_open_database", "pointer", ["string"]);
|
|
78
|
-
this.listDatabases = module.cwrap("hyperdb_wasm_list_databases", "pointer", []);
|
|
79
|
-
this.checkpointDatabase = module.cwrap("hyperdb_wasm_checkpoint_database", "pointer", ["string"]);
|
|
80
|
-
this.dropDatabase = module.cwrap("hyperdb_wasm_drop_database", "pointer", ["string"]);
|
|
81
81
|
this.shutdown = module.cwrap("hyperdb_wasm_shutdown", "pointer", []);
|
|
82
82
|
this.registerExternalFile = module.cwrap("hyperdb_wasm_register_external_file", "pointer", ["string", "string", "number"]);
|
|
83
83
|
this.registerExternalDirectory = module.cwrap("hyperdb_wasm_register_external_directory", "pointer", ["string", "string", "number"]);
|
|
84
84
|
this.removeExternalRegistration = module.cwrap("hyperdb_wasm_remove_external_registration", "pointer", ["string"]);
|
|
85
|
-
this.
|
|
86
|
-
this.
|
|
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"]);
|
|
87
87
|
this.startQuery = module.cwrap("hyperdb_wasm_query_start", "pointer", ["number", "string", "number"]);
|
|
88
88
|
this.insertArrowIPCFromPath = module.cwrap("hyperdb_wasm_arrow_insert_from_path", "pointer", ["number", "string", "string", "string", "number"]);
|
|
89
89
|
this.pollQuery = module.cwrap("hyperdb_wasm_query_poll", "pointer", ["number"]);
|
|
@@ -104,77 +104,40 @@
|
|
|
104
104
|
this.module.FS.unlink(request.path);
|
|
105
105
|
return { response: { id: request.id, type: "response", result: { state: "ok", payload: new Uint8Array() } }, transfer: [] };
|
|
106
106
|
}
|
|
107
|
-
if (request.type === "
|
|
107
|
+
if (request.type === "mount" && request.source.kind === "node-path") {
|
|
108
108
|
if (!this.module.externalFileBackend) {
|
|
109
109
|
return { response: { id: request.id, type: "response", result: { state: "error", error: "Host external files are supported only by the Node client" } }, transfer: [] };
|
|
110
110
|
}
|
|
111
|
-
const registration = this.module.externalFileBackend.
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const result2 = this.decodeResult(pointer2);
|
|
115
|
-
if (result2.state === "ok") {
|
|
116
|
-
const path = new TextDecoder("utf-8", { fatal: true }).decode(result2.payload);
|
|
117
|
-
try {
|
|
118
|
-
this.module.externalFileBackend.registered(path, registration.locator);
|
|
119
|
-
} catch (error) {
|
|
120
|
-
const cleanupPointer = safeInteger(this.removeExternalRegistration(path), "result pointer");
|
|
121
|
-
this.decodeResult(cleanupPointer);
|
|
122
|
-
throw error;
|
|
123
|
-
}
|
|
124
|
-
} else {
|
|
125
|
-
this.module.externalFileBackend.removed(registration.locator);
|
|
126
|
-
}
|
|
127
|
-
return { response: { id: request.id, type: "response", result: result2 }, transfer: [] };
|
|
128
|
-
} catch (error) {
|
|
129
|
-
this.module.externalFileBackend.removed(registration.locator);
|
|
130
|
-
throw error;
|
|
131
|
-
}
|
|
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);
|
|
132
114
|
}
|
|
133
|
-
if (request.type === "
|
|
115
|
+
if (request.type === "mount" && request.source.kind === "browser-file" && !this.module.externalFileBackendAvailable) {
|
|
134
116
|
return { response: { id: request.id, type: "response", result: { state: "error", error: "Blob external files are supported only by the browser client" } }, transfer: [] };
|
|
135
117
|
}
|
|
136
|
-
if (request.type === "
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const registration = backend.register("handle" in request ? request.handle : request.path, request.mode);
|
|
142
|
-
try {
|
|
143
|
-
const pointer2 = safeInteger(this.registerExternalDirectory(registration.name, registration.locator, Number(request.mode === "readwrite")), "result pointer");
|
|
144
|
-
const result2 = this.decodeResult(pointer2);
|
|
145
|
-
if (result2.state === "ok") {
|
|
146
|
-
const path = new TextDecoder("utf-8", { fatal: true }).decode(result2.payload);
|
|
147
|
-
try {
|
|
148
|
-
backend.registered(path, registration.locator);
|
|
149
|
-
} catch (error) {
|
|
150
|
-
const cleanupPointer = safeInteger(this.removeExternalRegistration(path), "result pointer");
|
|
151
|
-
this.decodeResult(cleanupPointer);
|
|
152
|
-
throw error;
|
|
153
|
-
}
|
|
154
|
-
} else {
|
|
155
|
-
const completion = completionFor([backend.removed(registration.locator)]);
|
|
156
|
-
return { response: { id: request.id, type: "response", result: result2 }, transfer: [], ...completion && { completion } };
|
|
157
|
-
}
|
|
158
|
-
return { response: { id: request.id, type: "response", result: result2 }, transfer: [] };
|
|
159
|
-
} catch (error) {
|
|
160
|
-
void Promise.resolve(backend.removed(registration.locator)).catch(() => void 0);
|
|
161
|
-
throw error;
|
|
162
|
-
}
|
|
118
|
+
if (request.type === "mount" && request.source.kind === "browser-directory") {
|
|
119
|
+
return this.mountDirectory(request, request.source.handle);
|
|
120
|
+
}
|
|
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"));
|
|
163
123
|
}
|
|
164
|
-
if (request.type === "
|
|
124
|
+
if (request.type === "unmount" && (this.module.externalFileBackend || this.module.externalDirectoryBackend)) {
|
|
165
125
|
validateCString(request.path, "external registration path");
|
|
166
126
|
const pointer2 = safeInteger(this.removeExternalRegistration(request.path), "result pointer");
|
|
167
127
|
const result2 = this.decodeResult(pointer2);
|
|
168
|
-
if (result2.state === "ok") {
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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() };
|
|
174
137
|
}
|
|
175
138
|
return { response: { id: request.id, type: "response", result: result2 }, transfer: [] };
|
|
176
139
|
}
|
|
177
|
-
if (request.type === "
|
|
140
|
+
if (request.type === "unmount" && !this.module.externalFileBackendAvailable) {
|
|
178
141
|
return { response: { id: request.id, type: "response", result: { state: "error", error: "External files are not supported by this client" } }, transfer: [] };
|
|
179
142
|
}
|
|
180
143
|
const pointer = safeInteger(this.invoke(request), "result pointer");
|
|
@@ -197,48 +160,27 @@
|
|
|
197
160
|
switch (request.type) {
|
|
198
161
|
case "initialize":
|
|
199
162
|
validateCString(request.settings, "settings");
|
|
200
|
-
|
|
163
|
+
validateCStringContents(this.module.hyperdbWasmStoragePath ?? "", "storage path");
|
|
164
|
+
return this.initialize(request.settings, this.module.hyperdbWasmStoragePath ?? "");
|
|
201
165
|
case "connect":
|
|
202
166
|
return this.connect();
|
|
203
167
|
case "disconnect":
|
|
204
168
|
return this.disconnect(request.connection);
|
|
205
|
-
case "createDatabase":
|
|
206
|
-
validateCString(request.databaseName, "database name");
|
|
207
|
-
return this.createDatabase(request.databaseName, Number(request.persistent));
|
|
208
|
-
case "openDatabase":
|
|
209
|
-
validateCString(request.databaseName, "database name");
|
|
210
|
-
return this.openDatabase(request.databaseName);
|
|
211
|
-
case "listDatabases":
|
|
212
|
-
return this.listDatabases();
|
|
213
|
-
case "checkpointDatabase":
|
|
214
|
-
validateCString(request.databaseName, "database name");
|
|
215
|
-
return this.checkpointDatabase(request.databaseName);
|
|
216
|
-
case "dropDatabase":
|
|
217
|
-
validateCString(request.databaseName, "database name");
|
|
218
|
-
return this.dropDatabase(request.databaseName);
|
|
219
|
-
case "attachDatabase":
|
|
220
|
-
validateCString(request.databaseName, "database name");
|
|
221
|
-
validateCString(request.alias, "database alias");
|
|
222
|
-
return this.attachDatabase(request.connection, request.databaseName, request.alias);
|
|
223
|
-
case "detachDatabase":
|
|
224
|
-
validateCString(request.alias, "database alias");
|
|
225
|
-
return this.detachDatabase(request.connection, request.alias);
|
|
226
169
|
case "shutdown":
|
|
227
170
|
return this.shutdown();
|
|
228
|
-
case "
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
validateCString(request.url, "external file URL");
|
|
232
|
-
return this.registerExternalFile(request.name, request.url, safeInteger(request.size, "external file size"));
|
|
233
|
-
case "registerExternalDirectory":
|
|
234
|
-
throw new Error("external directory request has no backend");
|
|
235
|
-
case "removeExternalRegistration":
|
|
171
|
+
case "mount":
|
|
172
|
+
throw new Error("mount request has no backend");
|
|
173
|
+
case "unmount":
|
|
236
174
|
validateCString(request.path, "external registration path");
|
|
237
175
|
return this.removeExternalRegistration(request.path);
|
|
238
176
|
case "startQuery":
|
|
177
|
+
validateCStringContents(request.sql, "SQL");
|
|
239
178
|
return this.startQuery(request.connection, request.sql, Number(request.sqlTestMode ?? false));
|
|
240
179
|
case "insertArrowIPCFromPath": {
|
|
241
|
-
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");
|
|
242
184
|
return this.insertArrowIPCFromPath(request.connection, path, request.name, request.schema ?? "", Number(request.create));
|
|
243
185
|
}
|
|
244
186
|
case "pollQuery":
|
|
@@ -249,6 +191,90 @@
|
|
|
249
191
|
return this.finishQuery(request.query);
|
|
250
192
|
}
|
|
251
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
|
+
}
|
|
252
278
|
decodeResult(pointer) {
|
|
253
279
|
if (pointer === 0) return { state: "error", error: "out of memory" };
|
|
254
280
|
try {
|
|
@@ -286,6 +312,11 @@
|
|
|
286
312
|
};
|
|
287
313
|
|
|
288
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
|
+
}
|
|
289
320
|
function isRecord(value) {
|
|
290
321
|
return typeof value === "object" && value !== null;
|
|
291
322
|
}
|
|
@@ -300,22 +331,11 @@
|
|
|
300
331
|
case "initialize":
|
|
301
332
|
return hasKeys(value, ["id", "type", "settings"]) && typeof value.settings === "string";
|
|
302
333
|
case "connect":
|
|
303
|
-
case "listDatabases":
|
|
304
334
|
return hasKeys(value, ["id", "type"]);
|
|
305
335
|
case "disconnect":
|
|
306
336
|
return hasKeys(value, ["id", "type", "connection"]) && Number.isSafeInteger(value.connection);
|
|
307
|
-
case "createDatabase":
|
|
308
|
-
return hasKeys(value, ["id", "type", "databaseName", "persistent"]) && typeof value.databaseName === "string" && typeof value.persistent === "boolean";
|
|
309
|
-
case "openDatabase":
|
|
310
|
-
case "checkpointDatabase":
|
|
311
|
-
case "dropDatabase":
|
|
312
|
-
return hasKeys(value, ["id", "type", "databaseName"]) && typeof value.databaseName === "string";
|
|
313
337
|
case "shutdown":
|
|
314
338
|
return hasKeys(value, ["id", "type"]);
|
|
315
|
-
case "attachDatabase":
|
|
316
|
-
return hasKeys(value, ["id", "type", "connection", "databaseName", "alias"]) && Number.isSafeInteger(value.connection) && typeof value.databaseName === "string" && typeof value.alias === "string";
|
|
317
|
-
case "detachDatabase":
|
|
318
|
-
return hasKeys(value, ["id", "type", "connection", "alias"]) && Number.isSafeInteger(value.connection) && typeof value.alias === "string";
|
|
319
339
|
case "startQuery":
|
|
320
340
|
return (hasKeys(value, ["id", "type", "connection", "sql"]) || hasKeys(value, ["id", "type", "connection", "sql", "sqlTestMode"])) && Number.isSafeInteger(value.connection) && typeof value.sql === "string" && (value.sqlTestMode === void 0 || typeof value.sqlTestMode === "boolean");
|
|
321
341
|
case "insertArrowIPCFromPath":
|
|
@@ -324,12 +344,21 @@
|
|
|
324
344
|
return hasKeys(value, ["id", "type", "bytes"]) && value.bytes instanceof Uint8Array;
|
|
325
345
|
case "removeFile":
|
|
326
346
|
return hasKeys(value, ["id", "type", "path"]) && typeof value.path === "string";
|
|
327
|
-
case "
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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");
|
|
333
362
|
case "pollQuery":
|
|
334
363
|
case "cancelQuery":
|
|
335
364
|
case "finishQuery":
|
|
@@ -338,8 +367,48 @@
|
|
|
338
367
|
return false;
|
|
339
368
|
}
|
|
340
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
|
+
}
|
|
341
374
|
|
|
342
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
|
+
}
|
|
343
412
|
function errorText(error) {
|
|
344
413
|
return error instanceof Error ? error.message : String(error);
|
|
345
414
|
}
|
|
@@ -371,6 +440,11 @@
|
|
|
371
440
|
);
|
|
372
441
|
}
|
|
373
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
|
+
};
|
|
374
448
|
module.hyperdbWasmFetchStart = (id, url, offset, length, expectedSize) => {
|
|
375
449
|
if (url.startsWith("hyperdb-node-file:") || url.startsWith("hyperdb-node-host-file:") || url.startsWith("hyperdb-node-directory-file:") || url.startsWith("hyperdb-browser-file:")) {
|
|
376
450
|
const read = module.externalFileBackend?.read(url, offset, length, expectedSize) ?? module.externalDirectoryBackend?.read(url, offset, length, expectedSize);
|
|
@@ -387,63 +461,39 @@
|
|
|
387
461
|
}
|
|
388
462
|
const headers = new Headers();
|
|
389
463
|
if (length > 0) headers.set("Range", `bytes=${offset}-${offset + length - 1}`);
|
|
390
|
-
|
|
464
|
+
const controller = new AbortController();
|
|
465
|
+
fetches.set(id, controller);
|
|
466
|
+
void fetch(url, { headers, redirect: "manual", signal: controller.signal }).then(async (response) => {
|
|
391
467
|
if (!response.ok || response.type === "opaqueredirect" || (length === 0 ? response.status !== 200 : response.status !== 206)) {
|
|
392
468
|
void response.body?.cancel().catch(() => void 0);
|
|
393
|
-
|
|
469
|
+
completeFetch(id, 0, -1);
|
|
394
470
|
return;
|
|
395
471
|
}
|
|
396
472
|
if (length > 0) {
|
|
397
473
|
const contentRange = /^bytes (\d+)-(\d+)\/(\d+)$/.exec(response.headers.get("content-range") ?? "");
|
|
398
474
|
if (!contentRange || contentRange[1] !== String(offset) || contentRange[2] !== String(offset + length - 1) || expectedSize >= 0 && contentRange[3] !== String(expectedSize)) {
|
|
399
475
|
void response.body?.cancel().catch(() => void 0);
|
|
400
|
-
|
|
476
|
+
completeFetch(id, 0, -1);
|
|
401
477
|
return;
|
|
402
478
|
}
|
|
403
479
|
}
|
|
404
480
|
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
405
481
|
if (length > 0 && bytes.byteLength !== length) {
|
|
406
|
-
|
|
482
|
+
completeFetch(id, 0, -1);
|
|
407
483
|
return;
|
|
408
484
|
}
|
|
409
485
|
try {
|
|
410
|
-
|
|
486
|
+
completeFetch(id, allocate(module, bytes), bytes.byteLength);
|
|
411
487
|
} catch (error) {
|
|
412
488
|
transport.postMessage({ type: "log", text: `fetch failed: ${errorText(error)}` });
|
|
413
|
-
|
|
489
|
+
completeFetch(id, 0, -1);
|
|
414
490
|
}
|
|
415
491
|
}).catch(() => {
|
|
416
492
|
transport.postMessage({ type: "log", text: "fetch failed" });
|
|
417
|
-
|
|
418
|
-
});
|
|
419
|
-
};
|
|
420
|
-
module.hyperdbWasmHttpStart = (id, method, url, headerLines, bodyPointer, bodyLength) => {
|
|
421
|
-
const headers = new Headers();
|
|
422
|
-
for (const line of headerLines.split("\n")) {
|
|
423
|
-
const separator = line.indexOf(":");
|
|
424
|
-
if (separator > 0) headers.set(line.slice(0, separator), line.slice(separator + 1));
|
|
425
|
-
}
|
|
426
|
-
const pointer = safePointer(bodyPointer, "HTTP body pointer");
|
|
427
|
-
const bodyEnd = safePointer(pointer + bodyLength, "HTTP body end");
|
|
428
|
-
if (bodyEnd > module.HEAPU8.byteLength) throw new Error("HTTP body exceeds the wasm heap");
|
|
429
|
-
const body = bodyLength > 0 ? module.HEAPU8.slice(pointer, bodyEnd) : void 0;
|
|
430
|
-
const init = { method, headers };
|
|
431
|
-
if (body) init.body = body;
|
|
432
|
-
void fetch(url, init).then(async (response) => {
|
|
433
|
-
const responseBody = new Uint8Array(await response.arrayBuffer());
|
|
434
|
-
let responseHeaders = "";
|
|
435
|
-
response.headers.forEach((value, name) => {
|
|
436
|
-
responseHeaders += `${name.toLowerCase()}:${value}
|
|
437
|
-
`;
|
|
438
|
-
});
|
|
439
|
-
const headersPointer = responseHeaders ? safePointer(module.stringToNewUTF8(responseHeaders), "headers pointer") : 0;
|
|
440
|
-
const responseBodyPointer = responseBody.byteLength ? allocate(module, responseBody) : 0;
|
|
441
|
-
module._hyperdb_wasm_http_complete(id, response.status, headersPointer, responseBodyPointer, responseBody.byteLength);
|
|
442
|
-
}).catch((error) => {
|
|
443
|
-
const errorBytes = new TextEncoder().encode(errorText(error));
|
|
444
|
-
module._hyperdb_wasm_http_complete(id, -1, 0, allocate(module, errorBytes), errorBytes.byteLength);
|
|
493
|
+
completeFetch(id, 0, -1);
|
|
445
494
|
});
|
|
446
495
|
};
|
|
496
|
+
module.hyperdbWasmFetchCancel = (id) => fetches.get(id)?.abort();
|
|
447
497
|
}
|
|
448
498
|
function installExternalDirectoryRuntime(module) {
|
|
449
499
|
const backend = module.externalDirectoryBackend;
|
|
@@ -536,6 +586,54 @@
|
|
|
536
586
|
};
|
|
537
587
|
}
|
|
538
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
|
+
}
|
|
539
637
|
function configureWorker(module, transport, configureFilesystem = () => void 0) {
|
|
540
638
|
const queuedRequests = [];
|
|
541
639
|
let bindings;
|
|
@@ -559,7 +657,9 @@
|
|
|
559
657
|
}
|
|
560
658
|
};
|
|
561
659
|
transport.onMessage((message) => {
|
|
562
|
-
if (
|
|
660
|
+
if (!isWorkerRequest(message)) return;
|
|
661
|
+
if (message.type === "unaryRpcComplete") completeUnaryRpc(module, message);
|
|
662
|
+
else handle(message);
|
|
563
663
|
});
|
|
564
664
|
module.print = (text) => transport.postMessage({ type: "log", text });
|
|
565
665
|
module.printErr = (text) => transport.postMessage({ type: "log", text });
|
|
@@ -567,8 +667,10 @@
|
|
|
567
667
|
try {
|
|
568
668
|
configureFilesystem(module);
|
|
569
669
|
installHttpRuntime(module, transport);
|
|
670
|
+
installUnaryRpcRuntime(module, transport);
|
|
570
671
|
installHostFileRuntime(module);
|
|
571
672
|
installExternalDirectoryRuntime(module);
|
|
673
|
+
installFilesystemProjectionRuntime(module);
|
|
572
674
|
bindings = new HyperDBBindings(module);
|
|
573
675
|
transport.postMessage({ type: "ready" });
|
|
574
676
|
for (const request of queuedRequests.splice(0)) handle(request);
|
|
@@ -654,6 +756,32 @@
|
|
|
654
756
|
files.sort();
|
|
655
757
|
return files;
|
|
656
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
|
+
}
|
|
657
785
|
async open(locator, path) {
|
|
658
786
|
const directory = this.getDirectory(locator);
|
|
659
787
|
const cached = directory.files.get(path);
|
|
@@ -803,6 +931,15 @@
|
|
|
803
931
|
if (directory.mode !== "readwrite") throw new Error("External directory registration is not writable");
|
|
804
932
|
return directory;
|
|
805
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
|
+
}
|
|
806
943
|
getOpenWriter(writer) {
|
|
807
944
|
nonnegativeSafeInteger(writer, "External directory writer");
|
|
808
945
|
const state = this.writers.get(writer);
|
|
@@ -835,9 +972,23 @@
|
|
|
835
972
|
var resolvedEngineUrl = new URL(engineUrl, self.location.href);
|
|
836
973
|
var configuredWasmUrl = new URL(self.location.href).searchParams.get("wasmUrl");
|
|
837
974
|
var resolvedWasmUrl = configuredWasmUrl == null ? new URL("hyperdb-wasm.wasm.br", resolvedEngineUrl) : new URL(configuredWasmUrl);
|
|
975
|
+
var configuredStoragePath = new URL(self.location.href).searchParams.get("storagePath") ?? "";
|
|
976
|
+
function normalizeStoragePath(path) {
|
|
977
|
+
const parts = [];
|
|
978
|
+
for (const part of path.replace(/^\/+/, "").split("/")) {
|
|
979
|
+
if (!part || part === ".") continue;
|
|
980
|
+
if (part === "..") {
|
|
981
|
+
if (parts.length === 0) throw new Error(`Storage path is outside the OPFS root: ${path}`);
|
|
982
|
+
parts.pop();
|
|
983
|
+
} else {
|
|
984
|
+
parts.push(part);
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
return parts.join("/");
|
|
988
|
+
}
|
|
989
|
+
var storagePath = normalizeStoragePath(configuredStoragePath);
|
|
838
990
|
function isPthreadWorker() {
|
|
839
|
-
|
|
840
|
-
return self.name === "em-pthread" || location.search?.includes("pthread") === true;
|
|
991
|
+
return self.name === "em-pthread";
|
|
841
992
|
}
|
|
842
993
|
if (isPthreadWorker()) {
|
|
843
994
|
self.importScripts(resolvedEngineUrl.href);
|
|
@@ -878,24 +1029,14 @@
|
|
|
878
1029
|
runtime.externalFileBackendAvailable = true;
|
|
879
1030
|
runtime.externalDirectoryBackend = new BrowserExternalDirectoryBackend();
|
|
880
1031
|
if (!runtime.OPFS) throw new Error("OPFS backend is unavailable");
|
|
881
|
-
runtime.FS.mkdirTree("/
|
|
882
|
-
runtime.FS.mount(runtime.OPFS, {}, "/
|
|
883
|
-
runtime.FS.mkdirTree(
|
|
884
|
-
runtime.FS.
|
|
885
|
-
runtime.
|
|
886
|
-
runtime.FS.mkdirTree("/hyperdb-persistent/logs");
|
|
1032
|
+
runtime.FS.mkdirTree("/mnt/fs");
|
|
1033
|
+
runtime.FS.mount(runtime.OPFS, {}, "/mnt/fs");
|
|
1034
|
+
if (storagePath) runtime.FS.mkdirTree(`/mnt/fs/${storagePath}`);
|
|
1035
|
+
runtime.FS.mkdirTree(`${storagePath ? `/mnt/fs/${storagePath}` : "/mnt/fs"}/logs`);
|
|
1036
|
+
runtime.hyperdbWasmStoragePath = storagePath;
|
|
887
1037
|
runtime.resolveHyperDBPath = (path) => {
|
|
888
|
-
const
|
|
889
|
-
|
|
890
|
-
if (!part || part === ".") continue;
|
|
891
|
-
if (part === "..") {
|
|
892
|
-
if (parts.length === 0) throw new Error(`Arrow IPC path is outside the OPFS root: ${path}`);
|
|
893
|
-
parts.pop();
|
|
894
|
-
} else {
|
|
895
|
-
parts.push(part);
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
return `/opfs/${parts.join("/")}`;
|
|
1038
|
+
const relativePath = normalizeStoragePath(path);
|
|
1039
|
+
return `/mnt/fs/${relativePath}`;
|
|
899
1040
|
};
|
|
900
1041
|
});
|
|
901
1042
|
self.importScripts(resolvedEngineUrl.href);
|