hyperdb-wasm 0.0.26845-dev.wasm.g5b622080 → 0.0.26862-dev.wasm.g89537caa

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/browser.js CHANGED
@@ -32,12 +32,16 @@ function isWasmResult(value) {
32
32
  case "done":
33
33
  return hasKeys(value, ["state"]);
34
34
  case "error":
35
+ return hasKeys(value, ["state", "error"]) && (typeof value.error === "string" || isHyperDBErrorData(value.error));
35
36
  case "busy":
36
37
  return hasKeys(value, ["state", "error"]) && typeof value.error === "string";
37
38
  default:
38
39
  return false;
39
40
  }
40
41
  }
42
+ function isHyperDBErrorData(value) {
43
+ return isRecord(value) && hasKeys(value, ["sqlState", "errorSource", "message", "detail", "hint"]) && typeof value.sqlState === "string" && /^[0-9A-Z]{5}$/.test(value.sqlState) && (value.errorSource === "User" || value.errorSource === "System" || value.errorSource === "OverloadRejection") && typeof value.message === "string" && typeof value.detail === "string" && typeof value.hint === "string";
44
+ }
41
45
  function isWorkerEvent(message) {
42
46
  if (!isRecord(message) || typeof message.type !== "string") return false;
43
47
  switch (message.type) {
@@ -102,32 +106,11 @@ var init_client = __esm({
102
106
  disconnect(connection) {
103
107
  return this.request({ id: this.nextId++, type: "disconnect", connection });
104
108
  }
105
- createDatabase(databaseName, persistent) {
106
- return this.request({ id: this.nextId++, type: "createDatabase", databaseName, persistent });
107
- }
108
- openDatabase(databaseName) {
109
- return this.request({ id: this.nextId++, type: "openDatabase", databaseName });
110
- }
111
- listDatabases() {
112
- return this.request({ id: this.nextId++, type: "listDatabases" });
113
- }
114
- checkpointDatabase(databaseName) {
115
- return this.request({ id: this.nextId++, type: "checkpointDatabase", databaseName });
116
- }
117
- dropDatabase(databaseName) {
118
- return this.request({ id: this.nextId++, type: "dropDatabase", databaseName });
119
- }
120
- attachDatabase(connection, databaseName, alias) {
121
- return this.request({ id: this.nextId++, type: "attachDatabase", connection, databaseName, alias });
122
- }
123
- detachDatabase(connection, alias) {
124
- return this.request({ id: this.nextId++, type: "detachDatabase", connection, alias });
125
- }
126
109
  shutdown() {
127
110
  return this.request({ id: this.nextId++, type: "shutdown" });
128
111
  }
129
- startQuery(connection, sql) {
130
- return this.request({ id: this.nextId++, type: "startQuery", connection, sql });
112
+ startQuery(connection, sql, sqlTestMode = false) {
113
+ return this.request({ id: this.nextId++, type: "startQuery", connection, sql, ...sqlTestMode && { sqlTestMode } });
131
114
  }
132
115
  insertArrowIPCFromPath(connection, path, name, schema, create, internal = false) {
133
116
  return this.request({ id: this.nextId++, type: "insertArrowIPCFromPath", connection, path, name, schema, create, internal });
@@ -139,11 +122,14 @@ var init_client = __esm({
139
122
  removeFile(path) {
140
123
  return this.request({ id: this.nextId++, type: "removeFile", path });
141
124
  }
142
- registerExternalFile(name, url, size) {
143
- return this.request({ id: this.nextId++, type: "registerExternalFile", name, url, size });
125
+ registerExternalFile(pathOrName, url, size) {
126
+ return url === void 0 || size === void 0 ? this.request({ id: this.nextId++, type: "registerExternalFile", path: pathOrName }) : this.request({ id: this.nextId++, type: "registerExternalFile", name: pathOrName, url, size });
127
+ }
128
+ registerExternalDirectory(source, mode) {
129
+ return typeof source === "string" ? this.request({ id: this.nextId++, type: "registerExternalDirectory", path: source, mode }) : this.request({ id: this.nextId++, type: "registerExternalDirectory", handle: source, mode });
144
130
  }
145
- removeExternalFile(path) {
146
- return this.request({ id: this.nextId++, type: "removeExternalFile", path });
131
+ removeExternalRegistration(path) {
132
+ return this.request({ id: this.nextId++, type: "removeExternalRegistration", path });
147
133
  }
148
134
  pollQuery(query) {
149
135
  return this.request({ id: this.nextId++, type: "pollQuery", query });
@@ -151,8 +137,8 @@ var init_client = __esm({
151
137
  cancelQuery(query) {
152
138
  return this.request({ id: this.nextId++, type: "cancelQuery", query });
153
139
  }
154
- releaseQuery(query) {
155
- return this.request({ id: this.nextId++, type: "releaseQuery", query });
140
+ finishQuery(query) {
141
+ return this.request({ id: this.nextId++, type: "finishQuery", query });
156
142
  }
157
143
  async terminate() {
158
144
  await this.transport.terminate();
@@ -232,6 +218,9 @@ function assertBrowserSupport(environment = globalThis) {
232
218
  if (!environment.WebAssembly.validate(exceptionsProbe)) {
233
219
  throw new Error("HyperDB cannot initialize because this browser does not support WebAssembly exception handling");
234
220
  }
221
+ if (!environment.WebAssembly.validate(tailCallsProbe)) {
222
+ throw new Error("HyperDB cannot initialize because this browser does not support WebAssembly tail calls");
223
+ }
235
224
  if (typeof environment.Worker !== "function") {
236
225
  throw new Error("HyperDB cannot initialize because this browser does not support Web Workers");
237
226
  }
@@ -242,10 +231,11 @@ function assertBrowserSupport(environment = globalThis) {
242
231
  throw new Error("HyperDB cannot initialize because this browser does not support the origin-private file system");
243
232
  }
244
233
  }
245
- function resolveBrowserWorkerUrl(workerUrl, engineUrl, wasmUrl) {
234
+ function resolveBrowserWorkerUrl(workerUrl, engineUrl, wasmUrl, storagePath) {
246
235
  const resolved = new URL(workerUrl);
247
236
  resolved.searchParams.set("engineUrl", new URL(engineUrl).href);
248
237
  if (wasmUrl != null) resolved.searchParams.set("wasmUrl", new URL(wasmUrl).href);
238
+ if (storagePath != null) resolved.searchParams.set("storagePath", storagePath);
249
239
  return resolved;
250
240
  }
251
241
  function createBrowserClient(options = {}) {
@@ -253,11 +243,11 @@ function createBrowserClient(options = {}) {
253
243
  const workerUrl = options.workerUrl ?? new URL("./browser_worker.js", import.meta.url);
254
244
  const engineUrl = options.engineUrl ?? new URL("./hyperdb-wasm.js", import.meta.url);
255
245
  return new HyperDBEngineClient(
256
- new BrowserWorker(new Worker(resolveBrowserWorkerUrl(workerUrl, engineUrl, options.wasmUrl), { type: "classic" })),
246
+ new BrowserWorker(new Worker(resolveBrowserWorkerUrl(workerUrl, engineUrl, options.wasmUrl, options.storagePath), { type: "classic" })),
257
247
  options.onSetupProgress
258
248
  );
259
249
  }
260
- var memory64Probe, threadsProbe, exceptionsProbe, BrowserWorker;
250
+ var memory64Probe, threadsProbe, exceptionsProbe, tailCallsProbe, BrowserWorker;
261
251
  var init_browser = __esm({
262
252
  "src/browser.ts"() {
263
253
  "use strict";
@@ -265,6 +255,7 @@ var init_browser = __esm({
265
255
  memory64Probe = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 5, 3, 1, 4, 1]);
266
256
  threadsProbe = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 5, 4, 1, 3, 1, 1]);
267
257
  exceptionsProbe = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 13, 3, 1, 0, 0]);
258
+ tailCallsProbe = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 3, 2, 0, 0, 10, 9, 2, 4, 0, 18, 1, 11, 2, 0, 11]);
268
259
  BrowserWorker = class {
269
260
  constructor(worker) {
270
261
  this.worker = worker;
@@ -12135,31 +12126,75 @@ RecordBatchFileWriter["throughDOM"] = recordBatchWriterThroughDOMStream;
12135
12126
  RecordBatchStreamWriter["throughDOM"] = recordBatchWriterThroughDOMStream;
12136
12127
 
12137
12128
  // src/api.ts
12138
- var maxPersistentDatabaseNameLength = 200;
12129
+ var HyperDBError = class extends Error {
12130
+ sqlState;
12131
+ errorSource;
12132
+ detail;
12133
+ hint;
12134
+ constructor(message, sqlState, errorSource, detail = "", hint = "") {
12135
+ super(message);
12136
+ this.name = "HyperDBError";
12137
+ this.sqlState = sqlState;
12138
+ this.errorSource = errorSource;
12139
+ this.detail = detail;
12140
+ this.hint = hint;
12141
+ }
12142
+ };
12143
+ function deferred() {
12144
+ let resolve;
12145
+ let reject;
12146
+ const promise = new Promise((resolvePromise, rejectPromise) => {
12147
+ resolve = resolvePromise;
12148
+ reject = rejectPromise;
12149
+ });
12150
+ void promise.catch(() => void 0);
12151
+ return { promise, resolve, reject };
12152
+ }
12153
+ function queryResult(result, info) {
12154
+ return { result, info };
12155
+ }
12156
+ function isRecord2(value) {
12157
+ return typeof value === "object" && value !== null && !Array.isArray(value);
12158
+ }
12159
+ function decodeQueryInfo(payload) {
12160
+ const value = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(payload));
12161
+ if (!isRecord2(value) || value.version !== 1) throw new Error("query information has an unsupported version");
12162
+ if (value.status !== "succeeded" && value.status !== "failed" && value.status !== "canceled") throw new Error("query information has an invalid status");
12163
+ if (typeof value.started !== "boolean" || typeof value.producesRows !== "boolean" || typeof value.cancellationRequestedByUser !== "boolean") {
12164
+ throw new Error("query information has invalid flags");
12165
+ }
12166
+ if (value.statementKind !== null && typeof value.statementKind !== "string") throw new Error("query information has an invalid statement kind");
12167
+ if (value.rowCount !== null && (typeof value.rowCount !== "string" || !/^\d+$/.test(value.rowCount))) throw new Error("query information has an invalid row count");
12168
+ if (value.columnCount !== null && (!Number.isSafeInteger(value.columnCount) || value.columnCount < 0)) throw new Error("query information has an invalid column count");
12169
+ if (value.queryPlan !== null && !isRecord2(value.queryPlan)) throw new Error("query information has an invalid query plan");
12170
+ if (!Array.isArray(value.dependencies) || value.dependencies.some((dependency) => typeof dependency !== "string")) throw new Error("query information has invalid dependencies");
12171
+ if (!isRecord2(value.timings)) throw new Error("query information has invalid timings");
12172
+ const timingNames = ["elapsedSeconds", "parsingSeconds", "compilationSeconds", "executionSeconds", "commitSeconds", "timeToScheduleSeconds", "timeToFirstResultSeconds", "cancellationSeconds"];
12173
+ for (const name of timingNames) {
12174
+ const timing = value.timings[name];
12175
+ if (timing !== null && (typeof timing !== "number" || !Number.isFinite(timing) || timing < 0)) throw new Error(`query information has an invalid ${name}`);
12176
+ }
12177
+ if (value.preExecutionMetrics !== null && !isRecord2(value.preExecutionMetrics)) throw new Error("query information has invalid pre-execution metrics");
12178
+ if (value.executionMetrics !== null && !isRecord2(value.executionMetrics)) throw new Error("query information has invalid execution metrics");
12179
+ return {
12180
+ status: value.status,
12181
+ started: value.started,
12182
+ statementKind: value.statementKind,
12183
+ producesRows: value.producesRows,
12184
+ rowCount: value.rowCount === null ? null : BigInt(value.rowCount),
12185
+ columnCount: value.columnCount,
12186
+ cancellationRequestedByUser: value.cancellationRequestedByUser,
12187
+ queryPlan: value.queryPlan,
12188
+ dependencies: value.dependencies,
12189
+ timings: value.timings,
12190
+ preExecutionMetrics: value.preExecutionMetrics,
12191
+ executionMetrics: value.executionMetrics
12192
+ };
12193
+ }
12139
12194
  function normalizeArrowInsertOptions(options) {
12140
12195
  if (!options.name) throw new Error("Arrow insert table name must not be empty");
12141
12196
  return { name: options.name, schema: options.schema ?? null, create: options.create ?? true };
12142
12197
  }
12143
- function validateDatabaseName(databaseName) {
12144
- if (!databaseName) throw new Error("Database name must not be empty");
12145
- if (databaseName.includes("\0")) throw new Error("Database name must not contain NUL");
12146
- }
12147
- function validatePersistentDatabaseName(databaseName) {
12148
- validateDatabaseName(databaseName);
12149
- if (databaseName.length > maxPersistentDatabaseNameLength) {
12150
- throw new Error(`Persistent database name must be at most ${maxPersistentDatabaseNameLength} characters`);
12151
- }
12152
- if (!/^[a-z0-9][a-z0-9_-]*$/.test(databaseName)) {
12153
- throw new Error("Persistent database name must start with a lowercase ASCII letter or digit and contain only lowercase ASCII letters, digits, hyphens, and underscores");
12154
- }
12155
- if (/^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])$/.test(databaseName)) {
12156
- throw new Error("Persistent database name is reserved by Windows");
12157
- }
12158
- }
12159
- function validateDatabaseAlias(alias) {
12160
- if (!alias) throw new Error("Database alias must not be empty");
12161
- if (alias.includes("\0")) throw new Error("Database alias must not contain NUL");
12162
- }
12163
12198
  function materializeDictionaryColumns(table) {
12164
12199
  const dictionaryColumns = table.schema.fields.map((field, index) => DataType.isDictionary(field.type) ? index : -1).filter((index) => index !== -1);
12165
12200
  if (dictionaryColumns.length === 0) return table;
@@ -12184,7 +12219,11 @@ function readHandleFromResult(result, operation) {
12184
12219
  return new DataView(result.payload.buffer, result.payload.byteOffset, result.payload.byteLength).getUint32(0, true);
12185
12220
  }
12186
12221
  function readErrorFromResult(result, operation) {
12187
- if (result.state === "error" || result.state === "busy") return new Error(result.error);
12222
+ if (result.state === "error") {
12223
+ if (typeof result.error === "string") return new Error(result.error);
12224
+ return new HyperDBError(result.error.message, result.error.sqlState, result.error.errorSource, result.error.detail, result.error.hint);
12225
+ }
12226
+ if (result.state === "busy") return new Error(result.error);
12188
12227
  return new Error(`${operation} returned unexpected state ${result.state}`);
12189
12228
  }
12190
12229
  function expectOk(result, operation) {
@@ -12196,34 +12235,6 @@ async function initializeHyperDB(client, settings) {
12196
12235
  if (serialized === void 0) throw new Error("HyperDB settings must be JSON-serializable");
12197
12236
  expectOk(await client.initialize(serialized), "initialize HyperDB settings");
12198
12237
  }
12199
- function readDatabasesFromResult(result) {
12200
- expectOk(result, "list databases");
12201
- const invalidResult = () => new Error("list databases returned invalid metadata");
12202
- if (result.payload.byteLength < 4) throw invalidResult();
12203
- const view = new DataView(result.payload.buffer, result.payload.byteOffset, result.payload.byteLength);
12204
- const count = view.getUint32(0, true);
12205
- let offset = 4;
12206
- const databases = [];
12207
- const decoder2 = new TextDecoder("utf-8", { fatal: true });
12208
- for (let index = 0; index < count; ++index) {
12209
- if (offset + 8 > result.payload.byteLength) throw invalidResult();
12210
- const rawStorage = view.getUint32(offset, true);
12211
- const nameLength = view.getUint32(offset + 4, true);
12212
- offset += 8;
12213
- if (nameLength > result.payload.byteLength - offset) throw invalidResult();
12214
- let name;
12215
- try {
12216
- name = decoder2.decode(result.payload.subarray(offset, offset + nameLength));
12217
- } catch {
12218
- throw invalidResult();
12219
- }
12220
- if (!name || name.includes("\0") || rawStorage !== 0 && rawStorage !== 1) throw invalidResult();
12221
- databases.push({ name, storage: rawStorage === 0 ? "memory" : "persistent" });
12222
- offset += nameLength;
12223
- }
12224
- if (offset !== result.payload.byteLength) throw invalidResult();
12225
- return databases;
12226
- }
12227
12238
  function yieldToEventLoop() {
12228
12239
  return new Promise((resolve) => setTimeout(resolve, 0));
12229
12240
  }
@@ -12263,26 +12274,20 @@ var HyperDB = class _HyperDB {
12263
12274
  this.connections.add(result);
12264
12275
  return result;
12265
12276
  }
12266
- async createDatabase(databaseName, options = {}) {
12267
- validateDatabaseName(databaseName);
12268
- if (options.storage !== void 0 && options.storage !== "memory" && options.storage !== "persistent") {
12269
- throw new Error(`Unsupported database storage: ${String(options.storage)}`);
12277
+ async registerExternalFile(...args) {
12278
+ if (args.length === 1) {
12279
+ const [sourcePath] = args;
12280
+ if (!sourcePath) throw new Error("External file path must not be empty");
12281
+ if (sourcePath.includes("\0")) throw new Error("External file path must not contain NUL");
12282
+ return this.runDatabaseOperation(async () => {
12283
+ const result = await this.client.registerExternalFile(sourcePath);
12284
+ expectOk(result, "register external file");
12285
+ const path = new TextDecoder("utf-8", { fatal: true }).decode(result.payload);
12286
+ if (!/^\/mnt\/external\/[^/]+\/[^/]+$/.test(path)) throw new Error("register external file returned an invalid path");
12287
+ return path;
12288
+ });
12270
12289
  }
12271
- if (options.storage === "persistent") validatePersistentDatabaseName(databaseName);
12272
- await this.runDatabaseOperation(async () => {
12273
- expectOk(await this.client.createDatabase(databaseName, options.storage === "persistent"), "create database");
12274
- });
12275
- }
12276
- async openDatabase(databaseName) {
12277
- validatePersistentDatabaseName(databaseName);
12278
- await this.runDatabaseOperation(async () => {
12279
- expectOk(await this.client.openDatabase(databaseName), "open database");
12280
- });
12281
- }
12282
- listDatabases() {
12283
- return this.runDatabaseOperation(async () => readDatabasesFromResult(await this.client.listDatabases()));
12284
- }
12285
- async registerExternalFile(name, url, size) {
12290
+ const [name, url, size] = args;
12286
12291
  if (!name) throw new Error("External file name must not be empty");
12287
12292
  if (name.includes("\0")) throw new Error("External file name must not contain NUL");
12288
12293
  if (name === "." || name === ".." || name.includes("/") || name.includes("\\")) {
@@ -12304,22 +12309,20 @@ var HyperDB = class _HyperDB {
12304
12309
  return path;
12305
12310
  });
12306
12311
  }
12307
- async removeExternalFile(path) {
12308
- if (!path) throw new Error("External file path must not be empty");
12309
- await this.runDatabaseOperation(async () => {
12310
- expectOk(await this.client.removeExternalFile(path), "remove external file");
12311
- });
12312
- }
12313
- async checkpointDatabase(databaseName) {
12314
- validateDatabaseName(databaseName);
12312
+ async removeExternalRegistration(path) {
12313
+ if (!path) throw new Error("External registration path must not be empty");
12314
+ if (path.includes("\0")) throw new Error("External registration path must not contain null bytes");
12315
12315
  await this.runDatabaseOperation(async () => {
12316
- expectOk(await this.client.checkpointDatabase(databaseName), "checkpoint database");
12316
+ expectOk(await this.client.removeExternalRegistration(path), "remove external registration");
12317
12317
  });
12318
12318
  }
12319
- async dropDatabase(databaseName) {
12320
- validateDatabaseName(databaseName);
12321
- await this.runDatabaseOperation(async () => {
12322
- expectOk(await this.client.dropDatabase(databaseName), "drop database");
12319
+ registerExternalDirectorySource(source, mode) {
12320
+ return this.runDatabaseOperation(async () => {
12321
+ const result = await this.client.registerExternalDirectory(source, mode);
12322
+ expectOk(result, "register external directory");
12323
+ const path = new TextDecoder("utf-8", { fatal: true }).decode(result.payload);
12324
+ if (!/^\/mnt\/external\/[^/]+\/[^/]+$/.test(path)) throw new Error("register external directory returned an invalid path");
12325
+ return path;
12323
12326
  });
12324
12327
  }
12325
12328
  async terminate() {
@@ -12393,110 +12396,113 @@ var HyperDBConnection = class {
12393
12396
  }
12394
12397
  await this.closing;
12395
12398
  }
12396
- async attachDatabase(databaseName, alias = databaseName) {
12397
- validateDatabaseName(databaseName);
12398
- validateDatabaseAlias(alias);
12399
- this.ensureAvailable();
12400
- try {
12401
- expectOk(await this.client.attachDatabase(this.connection, databaseName, alias), "attach database");
12402
- } finally {
12403
- this.active = false;
12404
- }
12405
- }
12406
- async detachDatabase(alias) {
12407
- validateDatabaseAlias(alias);
12408
- this.ensureAvailable();
12409
- try {
12410
- expectOk(await this.client.detachDatabase(this.connection, alias), "detach database");
12411
- } finally {
12412
- this.active = false;
12413
- }
12414
- }
12415
12399
  sendIPC(sql) {
12416
12400
  const connection = this;
12417
- return {
12418
- async *[Symbol.asyncIterator]() {
12401
+ const queryInfo = deferred();
12402
+ const result = (async function* () {
12403
+ let query;
12404
+ let terminal = false;
12405
+ let prefetched;
12406
+ let acquired = false;
12407
+ try {
12419
12408
  connection.ensureAvailable();
12420
- let query;
12421
- let terminal = false;
12422
- let prefetched;
12423
- try {
12424
- query = readHandleFromResult(await connection.client.startQuery(connection.connection, sql), "start query");
12425
- for (; ; ) {
12426
- let result = prefetched ? await prefetched : await connection.client.pollQuery(query);
12427
- prefetched = void 0;
12428
- while (result.state === "pending") {
12429
- await yieldToEventLoop();
12430
- result = await connection.client.pollQuery(query);
12431
- }
12432
- if (result.state === "chunk") {
12433
- prefetched = connection.client.pollQuery(query);
12434
- void prefetched.catch(() => void 0);
12435
- yield result.payload;
12436
- continue;
12437
- }
12438
- terminal = true;
12439
- if (result.state === "done") return;
12440
- throw readErrorFromResult(result, "poll query");
12409
+ acquired = true;
12410
+ query = readHandleFromResult(await connection.client.startQuery(connection.connection, sql), "start query");
12411
+ for (; ; ) {
12412
+ let result2 = prefetched ? await prefetched : await connection.client.pollQuery(query);
12413
+ prefetched = void 0;
12414
+ while (result2.state === "pending") {
12415
+ await yieldToEventLoop();
12416
+ result2 = await connection.client.pollQuery(query);
12441
12417
  }
12442
- } finally {
12443
- try {
12444
- if (query !== void 0) {
12445
- let cleanupError;
12446
- if (prefetched) {
12447
- try {
12448
- await prefetched;
12449
- } catch (error) {
12450
- cleanupError = error;
12451
- }
12452
- prefetched = void 0;
12453
- }
12454
- if (!terminal) {
12455
- try {
12456
- expectOk(await connection.client.cancelQuery(query), "cancel query");
12457
- } catch (error) {
12458
- cleanupError ??= error;
12459
- }
12418
+ if (result2.state === "chunk") {
12419
+ prefetched = connection.client.pollQuery(query);
12420
+ void prefetched.catch(() => void 0);
12421
+ yield result2.payload;
12422
+ continue;
12423
+ }
12424
+ terminal = true;
12425
+ if (result2.state === "done") return;
12426
+ throw readErrorFromResult(result2, "poll query");
12427
+ }
12428
+ } catch (error) {
12429
+ if (query === void 0 || !terminal) queryInfo.reject(error);
12430
+ throw error;
12431
+ } finally {
12432
+ try {
12433
+ if (query !== void 0) {
12434
+ let cleanupError;
12435
+ let info;
12436
+ if (prefetched) {
12437
+ try {
12438
+ await prefetched;
12439
+ } catch (error) {
12440
+ cleanupError = error;
12460
12441
  }
12442
+ prefetched = void 0;
12443
+ }
12444
+ if (!terminal) {
12461
12445
  try {
12462
- await connection.release(query);
12446
+ expectOk(await connection.client.cancelQuery(query), "cancel query");
12463
12447
  } catch (error) {
12464
12448
  cleanupError ??= error;
12465
12449
  }
12466
- if (cleanupError) throw cleanupError;
12467
12450
  }
12468
- } finally {
12469
- connection.active = false;
12451
+ try {
12452
+ info = decodeQueryInfo(await connection.finishQuery(query));
12453
+ } catch (error) {
12454
+ cleanupError ??= error;
12455
+ }
12456
+ if (cleanupError) {
12457
+ queryInfo.reject(cleanupError);
12458
+ throw cleanupError;
12459
+ }
12460
+ queryInfo.resolve(info);
12470
12461
  }
12462
+ } finally {
12463
+ if (acquired) connection.active = false;
12471
12464
  }
12472
12465
  }
12473
- };
12474
- }
12475
- async queryIPC(sql) {
12476
- const chunks = [];
12477
- let length = 0;
12478
- for await (const chunk of this.sendIPC(sql)) {
12479
- chunks.push(chunk);
12480
- length += chunk.byteLength;
12481
- }
12482
- const result = new Uint8Array(length);
12483
- let offset = 0;
12484
- for (const chunk of chunks) {
12485
- result.set(chunk, offset);
12486
- offset += chunk.byteLength;
12487
- }
12488
- return result;
12489
- }
12490
- async send(sql) {
12491
- const reader = await RecordBatchReader.from(this.sendIPC(sql));
12492
- if (!reader.isAsync() || !reader.isStream()) throw new Error("query did not produce an Arrow async stream");
12493
- return reader;
12494
- }
12495
- async query(sql) {
12496
- const reader = await this.send(sql);
12497
- const batches = [];
12498
- for await (const batch of reader) batches.push(batch);
12499
- return new Table(reader.schema, batches);
12466
+ })();
12467
+ return queryResult(result, queryInfo.promise);
12468
+ }
12469
+ queryIPC(sql) {
12470
+ const stream = this.sendIPC(sql);
12471
+ const result = (async () => {
12472
+ const chunks = [];
12473
+ let length = 0;
12474
+ for await (const chunk of stream.result) {
12475
+ chunks.push(chunk);
12476
+ length += chunk.byteLength;
12477
+ }
12478
+ const bytes = new Uint8Array(length);
12479
+ let offset = 0;
12480
+ for (const chunk of chunks) {
12481
+ bytes.set(chunk, offset);
12482
+ offset += chunk.byteLength;
12483
+ }
12484
+ return bytes;
12485
+ })();
12486
+ return queryResult(result, stream.info);
12487
+ }
12488
+ send(sql) {
12489
+ const stream = this.sendIPC(sql);
12490
+ const result = (async () => {
12491
+ const reader = await RecordBatchReader.from(stream.result);
12492
+ if (!reader.isAsync() || !reader.isStream()) throw new Error("query did not produce an Arrow async stream");
12493
+ return reader;
12494
+ })();
12495
+ return queryResult(result, stream.info);
12496
+ }
12497
+ query(sql) {
12498
+ const stream = this.send(sql);
12499
+ const result = (async () => {
12500
+ const reader = await stream.result;
12501
+ const batches = [];
12502
+ for await (const batch of reader) batches.push(batch);
12503
+ return new Table(reader.schema, batches);
12504
+ })();
12505
+ return queryResult(result, stream.info);
12500
12506
  }
12501
12507
  async insertArrowTable(table, options) {
12502
12508
  this.ensureAvailable();
@@ -12536,7 +12542,7 @@ var HyperDBConnection = class {
12536
12542
  }
12537
12543
  } finally {
12538
12544
  try {
12539
- if (query !== void 0) await this.release(query);
12545
+ if (query !== void 0) await this.finishQuery(query);
12540
12546
  } finally {
12541
12547
  if (acquire) this.active = false;
12542
12548
  }
@@ -12550,12 +12556,12 @@ var HyperDBConnection = class {
12550
12556
  }
12551
12557
  this.active = true;
12552
12558
  }
12553
- async release(query) {
12559
+ async finishQuery(query) {
12554
12560
  let delay = 1;
12555
12561
  for (; ; ) {
12556
- const result = await this.client.releaseQuery(query);
12557
- if (result.state === "ok") return;
12558
- if (result.state !== "pending") throw readErrorFromResult(result, "release query");
12562
+ const result = await this.client.finishQuery(query);
12563
+ if (result.state === "ok") return result.payload;
12564
+ if (result.state !== "pending") throw readErrorFromResult(result, "finish query");
12559
12565
  await new Promise((resolve) => setTimeout(resolve, delay));
12560
12566
  delay = Math.min(delay * 2, 32);
12561
12567
  }
@@ -12565,7 +12571,39 @@ var HyperDBConnection = class {
12565
12571
  await this.close();
12566
12572
  }
12567
12573
  };
12574
+
12575
+ // src/index.ts
12576
+ init_browser();
12577
+ var HyperDB2 = class _HyperDB extends HyperDB {
12578
+ static async create(options = {}) {
12579
+ const client = createBrowserClient(options);
12580
+ const database = new _HyperDB(client);
12581
+ try {
12582
+ await database.ready();
12583
+ await initializeHyperDB(client, options.settings);
12584
+ return database;
12585
+ } catch (error) {
12586
+ try {
12587
+ await client.terminate();
12588
+ } catch {
12589
+ }
12590
+ throw error;
12591
+ }
12592
+ }
12593
+ async registerExternalDirectory(handle, options = {}) {
12594
+ const mode = options.mode ?? "read";
12595
+ if (mode !== "read" && mode !== "readwrite") throw new Error(`Unsupported external directory mode: ${String(mode)}`);
12596
+ if (!handle || handle.kind !== "directory" || typeof handle.queryPermission !== "function" || typeof handle.requestPermission !== "function") {
12597
+ throw new Error("External directory handle must be a FileSystemDirectoryHandle");
12598
+ }
12599
+ let permission = await handle.queryPermission({ mode });
12600
+ if (permission !== "granted") permission = await handle.requestPermission({ mode });
12601
+ if (permission !== "granted") throw new Error(`External directory ${mode} permission was not granted`);
12602
+ return this.registerExternalDirectorySource(handle, mode);
12603
+ }
12604
+ };
12568
12605
  export {
12569
- HyperDB,
12570
- HyperDBConnection
12606
+ HyperDB2 as HyperDB,
12607
+ HyperDBConnection,
12608
+ HyperDBError
12571
12609
  };
@@ -0,0 +1,30 @@
1
+ import type { ExternalDirectoryBackend } from './wasm_bindings.js';
2
+ export declare class BrowserExternalDirectoryBackend implements ExternalDirectoryBackend {
3
+ private readonly directories;
4
+ private readonly paths;
5
+ private readonly files;
6
+ private readonly writers;
7
+ register(handle: unknown, mode: 'read' | 'readwrite'): {
8
+ readonly name: string;
9
+ readonly locator: string;
10
+ };
11
+ registered(path: string, locator: string): void;
12
+ removed(pathOrLocator: string): void;
13
+ list(locator: string): Promise<readonly string[]>;
14
+ open(locator: string, path: string): Promise<{
15
+ readonly locator: string;
16
+ readonly size: number;
17
+ }>;
18
+ read(locator: string, offset: number, length: number, expectedSize: number): Promise<Uint8Array> | undefined;
19
+ createDirectories(locator: string, path: string): Promise<void>;
20
+ openWriter(writer: number, locator: string, path: string): Promise<void>;
21
+ write(writer: number, offset: number, bytes: Uint8Array): Promise<void>;
22
+ finish(writer: number, size: number): Promise<void>;
23
+ abort(writer: number): Promise<void>;
24
+ shutdown(): void;
25
+ private getDirectory;
26
+ private getWritableDirectory;
27
+ private getOpenWriter;
28
+ private cleanupWriter;
29
+ private enqueue;
30
+ }