ducjs 4.0.1 → 4.0.3

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.
Binary file
@@ -404,7 +404,7 @@ export function applyDeltaChangeset(base_state, changeset) {
404
404
  return v3;
405
405
  }
406
406
 
407
- function __wbg_adapter_12(arg0, arg1, arg2) {
407
+ function __wbg_adapter_14(arg0, arg1, arg2) {
408
408
  wasm.closure101_externref_shim(arg0, arg1, arg2);
409
409
  }
410
410
 
@@ -1408,7 +1408,7 @@ function __wbg_get_imports() {
1408
1408
  };
1409
1409
  imports.wbg.__wbindgen_cast_9f0223d8557fdf20 = function(arg0, arg1) {
1410
1410
  // Cast intrinsic for `Closure(Closure { dtor_idx: 100, function: Function { arguments: [Externref], shim_idx: 101, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1411
- const ret = makeMutClosure(arg0, arg1, 100, __wbg_adapter_12);
1411
+ const ret = makeMutClosure(arg0, arg1, 100, __wbg_adapter_14);
1412
1412
  return ret;
1413
1413
  };
1414
1414
  imports.wbg.__wbindgen_cast_cb9088102bce6b30 = function(arg0, arg1) {
Binary file
@@ -4,6 +4,17 @@ export type OpfsChunkImporter = {
4
4
  export type DucStreamImportOptions = {
5
5
  compressedChunkSize?: number;
6
6
  writeBufferSize?: number;
7
+ progressIntervalMs?: number;
8
+ onProgress?: (progress: DucStreamImportProgress) => void;
9
+ };
10
+ export type DucStreamImportProgress = {
11
+ phase: "streaming" | "complete";
12
+ compressedBytes: number;
13
+ uncompressedBytes: number;
14
+ elapsedMs: number;
15
+ readWaitMs: number;
16
+ processingMs: number;
17
+ opfsWriteMs: number;
7
18
  };
8
19
  /**
9
20
  * Incrementally copy raw SQLite or decompress a compressed DUC stream into an
@@ -12,8 +12,9 @@ const DEFAULT_COMPRESSED_CHUNK_SIZE = 64 * 1024;
12
12
  const DEFAULT_WRITE_BUFFER_SIZE = 1024 * 1024;
13
13
  const SQLITE_HEADER = new TextEncoder().encode("SQLite format 3\0");
14
14
  class BufferedImporterWriter {
15
- constructor(importer, bufferSize) {
15
+ constructor(importer, bufferSize, onWrite) {
16
16
  this.importer = importer;
17
+ this.onWrite = onWrite;
17
18
  this.offset = 0;
18
19
  this.totalBytes = 0;
19
20
  if (!Number.isSafeInteger(bufferSize) || bufferSize <= 0) {
@@ -37,10 +38,19 @@ class BufferedImporterWriter {
37
38
  this.flush();
38
39
  return this.totalBytes;
39
40
  }
41
+ get pendingByteLength() {
42
+ return this.offset;
43
+ }
44
+ get writtenByteLength() {
45
+ return this.totalBytes;
46
+ }
40
47
  flush() {
48
+ var _a;
41
49
  if (this.offset === 0)
42
50
  return;
51
+ const startedAt = performance.now();
43
52
  this.importer.writeChunk(this.buffer.subarray(0, this.offset));
53
+ (_a = this.onWrite) === null || _a === void 0 ? void 0 : _a.call(this, this.offset, performance.now() - startedAt);
44
54
  this.totalBytes += this.offset;
45
55
  this.offset = 0;
46
56
  }
@@ -51,12 +61,42 @@ class BufferedImporterWriter {
51
61
  */
52
62
  export function importDucStreamToOpfs(stream_1, importer_1) {
53
63
  return __awaiter(this, arguments, void 0, function* (stream, importer, options = {}) {
54
- var _a, _b;
64
+ var _a, _b, _c;
55
65
  const compressedChunkSize = (_a = options.compressedChunkSize) !== null && _a !== void 0 ? _a : DEFAULT_COMPRESSED_CHUNK_SIZE;
56
66
  if (!Number.isSafeInteger(compressedChunkSize) || compressedChunkSize <= 0) {
57
67
  throw new RangeError("compressedChunkSize must be a positive safe integer");
58
68
  }
59
- const writer = new BufferedImporterWriter(importer, (_b = options.writeBufferSize) !== null && _b !== void 0 ? _b : DEFAULT_WRITE_BUFFER_SIZE);
69
+ const startedAt = performance.now();
70
+ const progressIntervalMs = (_b = options.progressIntervalMs) !== null && _b !== void 0 ? _b : 500;
71
+ if (!Number.isFinite(progressIntervalMs) || progressIntervalMs <= 0) {
72
+ throw new RangeError("progressIntervalMs must be a positive number");
73
+ }
74
+ let lastProgressAt = startedAt;
75
+ let compressedBytes = 0;
76
+ let readWaitMs = 0;
77
+ let processingMs = 0;
78
+ let opfsWriteMs = 0;
79
+ const writer = new BufferedImporterWriter(importer, (_c = options.writeBufferSize) !== null && _c !== void 0 ? _c : DEFAULT_WRITE_BUFFER_SIZE, (_byteLength, elapsedMs) => {
80
+ opfsWriteMs += elapsedMs;
81
+ });
82
+ const emitProgress = (phase) => {
83
+ if (!options.onProgress)
84
+ return;
85
+ const now = performance.now();
86
+ if (phase === "streaming" && now - lastProgressAt < progressIntervalMs) {
87
+ return;
88
+ }
89
+ options.onProgress({
90
+ phase,
91
+ compressedBytes,
92
+ uncompressedBytes: writer.writtenByteLength + writer.pendingByteLength,
93
+ elapsedMs: now - startedAt,
94
+ readWaitMs,
95
+ processingMs,
96
+ opfsWriteMs,
97
+ });
98
+ lastProgressAt = now;
99
+ };
60
100
  const decompressor = new Decompress((chunk) => writer.write(chunk));
61
101
  const reader = stream.getReader();
62
102
  const header = new Uint8Array(SQLITE_HEADER.byteLength);
@@ -77,11 +117,15 @@ export function importDucStreamToOpfs(stream_1, importer_1) {
77
117
  };
78
118
  try {
79
119
  while (true) {
120
+ const readStartedAt = performance.now();
80
121
  const { done, value } = yield reader.read();
122
+ readWaitMs += performance.now() - readStartedAt;
81
123
  if (done)
82
124
  break;
83
125
  if (!(value === null || value === void 0 ? void 0 : value.byteLength))
84
126
  continue;
127
+ compressedBytes += value.byteLength;
128
+ const processingStartedAt = performance.now();
85
129
  let valueOffset = 0;
86
130
  if (isRawSqlite === null) {
87
131
  const headerBytes = Math.min(header.byteLength - headerLength, value.byteLength);
@@ -96,7 +140,10 @@ export function importDucStreamToOpfs(stream_1, importer_1) {
96
140
  if (isRawSqlite !== null && valueOffset < value.byteLength) {
97
141
  writeDetected(value.subarray(valueOffset));
98
142
  }
143
+ processingMs += performance.now() - processingStartedAt;
144
+ emitProgress("streaming");
99
145
  }
146
+ const finalProcessingStartedAt = performance.now();
100
147
  if (isRawSqlite === null) {
101
148
  isRawSqlite = false;
102
149
  writeCompressed(header.subarray(0, headerLength));
@@ -104,7 +151,10 @@ export function importDucStreamToOpfs(stream_1, importer_1) {
104
151
  if (!isRawSqlite) {
105
152
  decompressor.push(new Uint8Array(), true);
106
153
  }
107
- return writer.finish();
154
+ const writtenBytes = writer.finish();
155
+ processingMs += performance.now() - finalProcessingStartedAt;
156
+ emitProgress("complete");
157
+ return writtenBytes;
108
158
  }
109
159
  finally {
110
160
  reader.releaseLock();
package/dist/wasi.js CHANGED
@@ -39,32 +39,45 @@ const isSuccessfulWasiExit = (error) => {
39
39
  || (candidate === null || candidate === void 0 ? void 0 : candidate.exitCode) === 0
40
40
  || (candidate === null || candidate === void 0 ? void 0 : candidate.message) === "Exited with status 0";
41
41
  };
42
+ let wasiBinaryReady = null;
42
43
  /**
43
- * Resolve the WASI binary path.
44
- * In development, it's in the ducjs dist directory.
45
- * In production, it should be bundled or downloaded.
44
+ * Load the WASI binary from a local package/path or a remote URL.
46
45
  */
47
- function resolveWasiBinary() {
48
- var _a;
49
- // Check for explicit override
50
- const env = (_a = globalThis.process) === null || _a === void 0 ? void 0 : _a.env;
51
- if (env === null || env === void 0 ? void 0 : env.DUCJS_WASI_PATH) {
52
- return env.DUCJS_WASI_PATH;
53
- }
54
- // Try to find it relative to ducjs package
55
- const candidates = [
56
- // Relative to this file (if in node_modules)
57
- resolve(_dirname, "..", "node_modules", "ducjs", "dist", "ducjs_wasi.wasm"),
58
- // Relative to ducjs dist (if running from source)
59
- resolve(_dirname, "..", "ducjs", "dist", "ducjs_wasi.wasm"),
60
- // Direct path from ducjs package
61
- resolve(_dirname, "ducjs_wasi.wasm"),
62
- ];
63
- for (const candidate of candidates) {
64
- if (existsSync(candidate))
65
- return candidate;
66
- }
67
- throw new Error(`ducjs WASI binary not found. Set DUCJS_WASI_PATH or ensure ducjs/dist/ducjs_wasi.wasm exists. Tried: ${candidates.join(", ")}`);
46
+ function loadWasiBinary() {
47
+ if (wasiBinaryReady)
48
+ return wasiBinaryReady;
49
+ wasiBinaryReady = (() => __awaiter(this, void 0, void 0, function* () {
50
+ var _a;
51
+ const env = (_a = globalThis.process) === null || _a === void 0 ? void 0 : _a.env;
52
+ if (env === null || env === void 0 ? void 0 : env.DUCJS_WASI_PATH) {
53
+ return readFileSync(env.DUCJS_WASI_PATH);
54
+ }
55
+ if (env === null || env === void 0 ? void 0 : env.DUCJS_WASI_URL) {
56
+ const response = yield fetch(env.DUCJS_WASI_URL);
57
+ if (!response.ok) {
58
+ throw new Error(`Failed to download ducjs WASI binary: ${response.status} ${response.statusText}`);
59
+ }
60
+ const binary = new Uint8Array(yield response.arrayBuffer());
61
+ if (binary.byteLength === 0) {
62
+ throw new Error("Downloaded ducjs WASI binary is empty");
63
+ }
64
+ return binary;
65
+ }
66
+ const candidates = [
67
+ resolve(_dirname, "..", "node_modules", "ducjs", "dist", "ducjs_wasi.wasm"),
68
+ resolve(_dirname, "..", "ducjs", "dist", "ducjs_wasi.wasm"),
69
+ resolve(_dirname, "ducjs_wasi.wasm"),
70
+ ];
71
+ for (const candidate of candidates) {
72
+ if (existsSync(candidate))
73
+ return readFileSync(candidate);
74
+ }
75
+ throw new Error(`ducjs WASI binary not found. Set DUCJS_WASI_PATH, set DUCJS_WASI_URL, or ensure ducjs/dist/ducjs_wasi.wasm exists. Tried: ${candidates.join(", ")}`);
76
+ }))().catch((error) => {
77
+ wasiBinaryReady = null;
78
+ throw error;
79
+ });
80
+ return wasiBinaryReady;
68
81
  }
69
82
  /**
70
83
  * Stream multiple files into a single .duc file using the WASI binary.
@@ -182,7 +195,7 @@ const writeExternalFileSidecars = (state, manifestDir) => {
182
195
  return manifestPath;
183
196
  };
184
197
  const runWasi = (args, preopens) => __awaiter(void 0, void 0, void 0, function* () {
185
- const wasmBinary = readFileSync(resolveWasiBinary());
198
+ const wasmBinary = yield loadWasiBinary();
186
199
  const wasi = new WASI({
187
200
  version: "preview1",
188
201
  args,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ducjs",
3
- "version": "4.0.1",
3
+ "version": "4.0.3",
4
4
  "description": "SQLite-backed project-state format for physical-engineering work before execution.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",