wiki-graph-sdk 0.9.0 → 0.9.2

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.
Files changed (51) hide show
  1. package/dist/archives.cjs +316 -142
  2. package/dist/archives.cjs.map +1 -1
  3. package/dist/archives.js +326 -148
  4. package/dist/archives.js.map +1 -1
  5. package/dist/conversions.cjs +268 -99
  6. package/dist/conversions.cjs.map +1 -1
  7. package/dist/conversions.js +274 -103
  8. package/dist/conversions.js.map +1 -1
  9. package/dist/default-worker.cjs +258 -101
  10. package/dist/default-worker.cjs.map +1 -1
  11. package/dist/default-worker.js +264 -105
  12. package/dist/default-worker.js.map +1 -1
  13. package/dist/embedding.cjs +214 -54
  14. package/dist/embedding.cjs.map +1 -1
  15. package/dist/embedding.js +214 -54
  16. package/dist/embedding.js.map +1 -1
  17. package/dist/gc.cjs +214 -54
  18. package/dist/gc.cjs.map +1 -1
  19. package/dist/gc.js +214 -54
  20. package/dist/gc.js.map +1 -1
  21. package/dist/index.cjs +354 -174
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.js +369 -184
  25. package/dist/index.js.map +1 -1
  26. package/dist/local-config.cjs +214 -54
  27. package/dist/local-config.cjs.map +1 -1
  28. package/dist/local-config.js +214 -54
  29. package/dist/local-config.js.map +1 -1
  30. package/dist/maintenance.cjs +264 -97
  31. package/dist/maintenance.cjs.map +1 -1
  32. package/dist/maintenance.js +270 -101
  33. package/dist/maintenance.js.map +1 -1
  34. package/dist/node-platform.cjs +214 -54
  35. package/dist/node-platform.cjs.map +1 -1
  36. package/dist/node-platform.d.ts +2 -0
  37. package/dist/node-platform.js +214 -54
  38. package/dist/node-platform.js.map +1 -1
  39. package/dist/runtime-config.cjs +214 -54
  40. package/dist/runtime-config.cjs.map +1 -1
  41. package/dist/runtime-config.js +214 -54
  42. package/dist/runtime-config.js.map +1 -1
  43. package/dist/stage.cjs +214 -54
  44. package/dist/stage.cjs.map +1 -1
  45. package/dist/stage.js +214 -54
  46. package/dist/stage.js.map +1 -1
  47. package/dist/worker.cjs +257 -96
  48. package/dist/worker.cjs.map +1 -1
  49. package/dist/worker.js +263 -100
  50. package/dist/worker.js.map +1 -1
  51. package/package.json +2 -2
@@ -69,6 +69,15 @@ var NodeFile = class {
69
69
  this.name = name;
70
70
  }
71
71
  path;
72
+ async getPhysicalIdentity() {
73
+ const value = await fsPromises.stat(this.path).catch((error) => {
74
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT")
75
+ return void 0;
76
+ throw error;
77
+ });
78
+ if (value === void 0) return void 0;
79
+ return `${value.dev}:${value.ino}`;
80
+ }
72
81
  kind = "file";
73
82
  [NODE_RESOURCE_KIND] = "file";
74
83
  identity;
@@ -193,6 +202,15 @@ var NodeDirectory = class _NodeDirectory {
193
202
  this.name = pathModule.basename(path);
194
203
  }
195
204
  path;
205
+ async getPhysicalIdentity() {
206
+ const value = await fsPromises.stat(this.path).catch((error) => {
207
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT")
208
+ return void 0;
209
+ throw error;
210
+ });
211
+ if (value === void 0) return void 0;
212
+ return `${value.dev}:${value.ino}`;
213
+ }
196
214
  kind = "directory";
197
215
  [NODE_RESOURCE_KIND] = "directory";
198
216
  identity;
@@ -202,8 +220,10 @@ var NodeDirectory = class _NodeDirectory {
202
220
  const file = new NodeFile(pathModule.join(this.path, name));
203
221
  try {
204
222
  return (await fsPromises.stat(file.path)).isFile() ? file : void 0;
205
- } catch {
206
- return void 0;
223
+ } catch (error) {
224
+ if (typeof error === "object" && error !== null && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"))
225
+ return void 0;
226
+ throw error;
207
227
  }
208
228
  }
209
229
  async getLastModified() {
@@ -221,8 +241,10 @@ var NodeDirectory = class _NodeDirectory {
221
241
  const directory = new _NodeDirectory(pathModule.join(this.path, name));
222
242
  try {
223
243
  return (await fsPromises.stat(directory.path)).isDirectory() ? directory : void 0;
224
- } catch {
225
- return void 0;
244
+ } catch (error) {
245
+ if (typeof error === "object" && error !== null && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"))
246
+ return void 0;
247
+ throw error;
226
248
  }
227
249
  }
228
250
  async list() {
@@ -338,14 +360,23 @@ async function openNativeNodeDatabase(file, flags) {
338
360
  });
339
361
  });
340
362
  }
341
- async function openNodeZip(file) {
363
+ async function openNodeZip(file, options = {}) {
342
364
  const path = getBrandedNodeResourcePath(file, "file");
343
365
  if (path === void 0) {
344
366
  throw new TypeError("The Node ZIP adapter requires a NodeFile");
345
367
  }
346
- const zipFile = await new Promise((resolve2, reject) => {
347
- yauzl.open(
348
- path,
368
+ const handle = await fsPromises.open(path, "r");
369
+ let size;
370
+ try {
371
+ size = (await handle.stat()).size;
372
+ } catch (error) {
373
+ await handle.close();
374
+ throw error;
375
+ }
376
+ const open2 = () => new Promise((resolve2, reject) => {
377
+ yauzl.fromRandomAccessReader(
378
+ new NodeZipSource(handle),
379
+ size,
349
380
  { autoClose: false, lazyEntries: true },
350
381
  (error, opened) => {
351
382
  if (error || opened === void 0)
@@ -354,63 +385,192 @@ async function openNodeZip(file) {
354
385
  }
355
386
  );
356
387
  });
357
- const entries = await new Promise(
358
- (resolve2, reject) => {
359
- const discovered = /* @__PURE__ */ new Map();
360
- const rejectAndClose = (error) => {
361
- zipFile.close();
362
- reject(
363
- error instanceof Error ? error : new Error("Cannot scan ZIP central directory")
364
- );
365
- };
366
- const resolveEntries = () => {
367
- zipFile.off("error", rejectAndClose);
368
- resolve2(discovered);
369
- };
370
- zipFile.on("entry", (entry) => {
371
- if (!entry.fileName.endsWith("/")) {
372
- discovered.set(entry.fileName, entry);
373
- }
374
- zipFile.readEntry();
375
- });
376
- zipFile.once("error", rejectAndClose);
377
- zipFile.once("end", resolveEntries);
378
- zipFile.readEntry();
379
- }
380
- );
388
+ try {
389
+ (await open2()).close();
390
+ } catch (error) {
391
+ await handle.close();
392
+ throw error;
393
+ }
381
394
  let closed = false;
395
+ let pending = Promise.resolve();
396
+ let closing;
397
+ const run = (operation) => {
398
+ if (closed)
399
+ return Promise.reject(new Error("Cannot read a closed ZIP archive"));
400
+ const result = pending.then(async () => {
401
+ const zip = await open2();
402
+ try {
403
+ return await operation(zip);
404
+ } finally {
405
+ zip.close();
406
+ }
407
+ });
408
+ pending = result.then(
409
+ () => void 0,
410
+ () => void 0
411
+ );
412
+ return result;
413
+ };
414
+ const withEntry = (name, operation) => run(async (zip) => {
415
+ let exact;
416
+ let alias;
417
+ let aliasCollision = false;
418
+ await scanNodeZip(zip, (entry) => {
419
+ if (entry.fileName === name) exact = entry;
420
+ if (normalizeNodeZipName(entry.fileName) === name) {
421
+ if (alias !== void 0 && alias.fileName !== entry.fileName)
422
+ aliasCollision = true;
423
+ alias = entry;
424
+ }
425
+ });
426
+ if ((exact === void 0 || options.entryNameMode === "normalized") && aliasCollision) {
427
+ let bestPosition = -1;
428
+ const candidates = await open2();
429
+ try {
430
+ await scanNodeZip(candidates, async (entry) => {
431
+ if (normalizeNodeZipName(entry.fileName) !== name) return;
432
+ const probe = await open2();
433
+ let position = 0;
434
+ try {
435
+ await scanNodeZip(probe, (candidate) => {
436
+ if (candidate.fileName === entry.fileName) return false;
437
+ position++;
438
+ });
439
+ } finally {
440
+ probe.close();
441
+ }
442
+ if (position >= bestPosition) {
443
+ bestPosition = position;
444
+ alias = entry;
445
+ }
446
+ });
447
+ return await operation(candidates, alias);
448
+ } finally {
449
+ candidates.close();
450
+ }
451
+ }
452
+ return await operation(
453
+ zip,
454
+ options.entryNameMode === "normalized" ? alias : exact ?? alias
455
+ );
456
+ });
382
457
  return {
383
458
  close: () => {
384
- if (!closed) zipFile.close();
385
459
  closed = true;
386
- return Promise.resolve();
460
+ closing ??= pending.then(() => handle.close());
461
+ return closing;
387
462
  },
388
- copyEntry: async (name, target) => {
389
- if (closed) throw new Error("Cannot read a closed ZIP archive");
390
- const entry = entries.get(name);
463
+ copyEntry: (name, target) => withEntry(name, async (zip, entry) => {
391
464
  if (entry === void 0) return false;
392
- await copyNodeZipEntry(zipFile, entry, target);
465
+ await copyNodeZipEntry(zip, entry, target);
393
466
  return true;
394
- },
395
- getEntrySize: (name) => {
396
- if (closed) throw new Error("Cannot read a closed ZIP archive");
397
- return Promise.resolve(entries.get(name)?.uncompressedSize);
398
- },
399
- listEntries: () => Promise.resolve([...entries.keys()]),
400
- readEntryRange: async (name, offset, length) => {
401
- if (closed) throw new Error("Cannot read a closed ZIP archive");
402
- const entry = entries.get(name);
467
+ }),
468
+ getEntrySize: (name) => withEntry(
469
+ name,
470
+ (_zip, entry) => Promise.resolve(entry?.uncompressedSize)
471
+ ),
472
+ listEntries: () => run(async (zip) => {
473
+ const names = /* @__PURE__ */ new Set();
474
+ await scanNodeZip(zip, (entry) => {
475
+ names.add(entry.fileName);
476
+ });
477
+ return [...names];
478
+ }),
479
+ readEntryRange: (name, offset, length) => withEntry(name, async (zip, entry) => {
403
480
  if (entry === void 0) return void 0;
404
- return await readNodeZipEntryRange(zipFile, entry, offset, length);
405
- },
406
- readEntry: async (name) => {
407
- if (closed) throw new Error("Cannot read a closed ZIP archive");
408
- const entry = entries.get(name);
481
+ return await readNodeZipEntryRange(zip, entry, offset, length);
482
+ }),
483
+ readEntry: (name) => withEntry(name, async (zip, entry) => {
409
484
  if (entry === void 0) return void 0;
410
- return await readNodeZipEntry(zipFile, entry);
411
- }
485
+ return await readNodeZipEntry(zip, entry);
486
+ })
412
487
  };
413
488
  }
489
+ var NodeZipSource = class extends yauzl.RandomAccessReader {
490
+ constructor(handle) {
491
+ super();
492
+ this.handle = handle;
493
+ }
494
+ handle;
495
+ read(buffer, offset, length, position, callback) {
496
+ const read2 = (written) => {
497
+ if (written === length) {
498
+ callback(null);
499
+ return;
500
+ }
501
+ fs.read(
502
+ this.handle.fd,
503
+ buffer,
504
+ offset + written,
505
+ length - written,
506
+ position + written,
507
+ (error, bytesRead) => {
508
+ if (error) callback(error);
509
+ else if (bytesRead === 0)
510
+ callback(new Error("Unexpected end of ZIP file"));
511
+ else read2(written + bytesRead);
512
+ }
513
+ );
514
+ };
515
+ read2(0);
516
+ }
517
+ _readStreamForRange(start, end) {
518
+ const handle = this.handle;
519
+ return Readable.from(
520
+ (async function* () {
521
+ for (let position = start; position < end; ) {
522
+ const buffer = Buffer.allocUnsafe(
523
+ Math.min(64 * 1024, end - position)
524
+ );
525
+ const { bytesRead } = await handle.read(
526
+ buffer,
527
+ 0,
528
+ buffer.length,
529
+ position
530
+ );
531
+ if (bytesRead === 0) return;
532
+ position += bytesRead;
533
+ yield buffer.subarray(0, bytesRead);
534
+ }
535
+ })()
536
+ );
537
+ }
538
+ };
539
+ function normalizeNodeZipName(name) {
540
+ const normalized = name.replaceAll("\\", "/").trim().split("/").filter((part) => part !== "" && part !== ".").join("/");
541
+ return normalized.split("/").includes("..") ? void 0 : normalized;
542
+ }
543
+ async function scanNodeZip(zip, visit) {
544
+ await new Promise((resolve2, reject) => {
545
+ const cleanup = () => {
546
+ zip.off("entry", onEntry);
547
+ zip.off("end", onEnd);
548
+ zip.off("error", onError);
549
+ };
550
+ const onError = (error) => {
551
+ cleanup();
552
+ reject(error);
553
+ };
554
+ const onEnd = () => {
555
+ cleanup();
556
+ resolve2();
557
+ };
558
+ const onEntry = (entry) => {
559
+ void Promise.resolve().then(() => entry.fileName.endsWith("/") ? void 0 : visit(entry)).then((keepReading) => {
560
+ if (keepReading === false) onEnd();
561
+ else zip.readEntry();
562
+ }).catch(
563
+ (error) => onError(
564
+ error instanceof Error ? error : new Error("Cannot scan ZIP", { cause: error })
565
+ )
566
+ );
567
+ };
568
+ zip.on("entry", onEntry);
569
+ zip.once("end", onEnd);
570
+ zip.once("error", onError);
571
+ zip.readEntry();
572
+ });
573
+ }
414
574
  async function copyNodeZipEntry(zipFile, entry, target) {
415
575
  const writer = await target.openWriter();
416
576
  try {