wiki-graph-sdk 0.9.0 → 0.9.1

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 +306 -135
  2. package/dist/archives.cjs.map +1 -1
  3. package/dist/archives.js +315 -140
  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 +343 -165
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.js +357 -174
  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
package/dist/stage.js CHANGED
@@ -85,6 +85,15 @@ var NodeFile = class {
85
85
  this.name = name;
86
86
  }
87
87
  path;
88
+ async getPhysicalIdentity() {
89
+ const value = await fsPromises.stat(this.path).catch((error) => {
90
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT")
91
+ return void 0;
92
+ throw error;
93
+ });
94
+ if (value === void 0) return void 0;
95
+ return `${value.dev}:${value.ino}`;
96
+ }
88
97
  kind = "file";
89
98
  [NODE_RESOURCE_KIND] = "file";
90
99
  identity;
@@ -209,6 +218,15 @@ var NodeDirectory = class _NodeDirectory {
209
218
  this.name = pathModule.basename(path);
210
219
  }
211
220
  path;
221
+ async getPhysicalIdentity() {
222
+ const value = await fsPromises.stat(this.path).catch((error) => {
223
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT")
224
+ return void 0;
225
+ throw error;
226
+ });
227
+ if (value === void 0) return void 0;
228
+ return `${value.dev}:${value.ino}`;
229
+ }
212
230
  kind = "directory";
213
231
  [NODE_RESOURCE_KIND] = "directory";
214
232
  identity;
@@ -218,8 +236,10 @@ var NodeDirectory = class _NodeDirectory {
218
236
  const file = new NodeFile(pathModule.join(this.path, name));
219
237
  try {
220
238
  return (await fsPromises.stat(file.path)).isFile() ? file : void 0;
221
- } catch {
222
- return void 0;
239
+ } catch (error) {
240
+ if (typeof error === "object" && error !== null && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"))
241
+ return void 0;
242
+ throw error;
223
243
  }
224
244
  }
225
245
  async getLastModified() {
@@ -237,8 +257,10 @@ var NodeDirectory = class _NodeDirectory {
237
257
  const directory = new _NodeDirectory(pathModule.join(this.path, name));
238
258
  try {
239
259
  return (await fsPromises.stat(directory.path)).isDirectory() ? directory : void 0;
240
- } catch {
241
- return void 0;
260
+ } catch (error) {
261
+ if (typeof error === "object" && error !== null && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"))
262
+ return void 0;
263
+ throw error;
242
264
  }
243
265
  }
244
266
  async list() {
@@ -354,14 +376,23 @@ async function openNativeNodeDatabase(file, flags) {
354
376
  });
355
377
  });
356
378
  }
357
- async function openNodeZip(file) {
379
+ async function openNodeZip(file, options = {}) {
358
380
  const path = getBrandedNodeResourcePath(file, "file");
359
381
  if (path === void 0) {
360
382
  throw new TypeError("The Node ZIP adapter requires a NodeFile");
361
383
  }
362
- const zipFile = await new Promise((resolve2, reject) => {
363
- yauzl.open(
364
- path,
384
+ const handle = await fsPromises.open(path, "r");
385
+ let size;
386
+ try {
387
+ size = (await handle.stat()).size;
388
+ } catch (error) {
389
+ await handle.close();
390
+ throw error;
391
+ }
392
+ const open2 = () => new Promise((resolve2, reject) => {
393
+ yauzl.fromRandomAccessReader(
394
+ new NodeZipSource(handle),
395
+ size,
365
396
  { autoClose: false, lazyEntries: true },
366
397
  (error, opened) => {
367
398
  if (error || opened === void 0)
@@ -370,63 +401,192 @@ async function openNodeZip(file) {
370
401
  }
371
402
  );
372
403
  });
373
- const entries = await new Promise(
374
- (resolve2, reject) => {
375
- const discovered = /* @__PURE__ */ new Map();
376
- const rejectAndClose = (error) => {
377
- zipFile.close();
378
- reject(
379
- error instanceof Error ? error : new Error("Cannot scan ZIP central directory")
380
- );
381
- };
382
- const resolveEntries = () => {
383
- zipFile.off("error", rejectAndClose);
384
- resolve2(discovered);
385
- };
386
- zipFile.on("entry", (entry) => {
387
- if (!entry.fileName.endsWith("/")) {
388
- discovered.set(entry.fileName, entry);
389
- }
390
- zipFile.readEntry();
391
- });
392
- zipFile.once("error", rejectAndClose);
393
- zipFile.once("end", resolveEntries);
394
- zipFile.readEntry();
395
- }
396
- );
404
+ try {
405
+ (await open2()).close();
406
+ } catch (error) {
407
+ await handle.close();
408
+ throw error;
409
+ }
397
410
  let closed = false;
411
+ let pending = Promise.resolve();
412
+ let closing;
413
+ const run = (operation) => {
414
+ if (closed)
415
+ return Promise.reject(new Error("Cannot read a closed ZIP archive"));
416
+ const result = pending.then(async () => {
417
+ const zip = await open2();
418
+ try {
419
+ return await operation(zip);
420
+ } finally {
421
+ zip.close();
422
+ }
423
+ });
424
+ pending = result.then(
425
+ () => void 0,
426
+ () => void 0
427
+ );
428
+ return result;
429
+ };
430
+ const withEntry = (name, operation) => run(async (zip) => {
431
+ let exact;
432
+ let alias;
433
+ let aliasCollision = false;
434
+ await scanNodeZip(zip, (entry) => {
435
+ if (entry.fileName === name) exact = entry;
436
+ if (normalizeNodeZipName(entry.fileName) === name) {
437
+ if (alias !== void 0 && alias.fileName !== entry.fileName)
438
+ aliasCollision = true;
439
+ alias = entry;
440
+ }
441
+ });
442
+ if ((exact === void 0 || options.entryNameMode === "normalized") && aliasCollision) {
443
+ let bestPosition = -1;
444
+ const candidates = await open2();
445
+ try {
446
+ await scanNodeZip(candidates, async (entry) => {
447
+ if (normalizeNodeZipName(entry.fileName) !== name) return;
448
+ const probe = await open2();
449
+ let position = 0;
450
+ try {
451
+ await scanNodeZip(probe, (candidate) => {
452
+ if (candidate.fileName === entry.fileName) return false;
453
+ position++;
454
+ });
455
+ } finally {
456
+ probe.close();
457
+ }
458
+ if (position >= bestPosition) {
459
+ bestPosition = position;
460
+ alias = entry;
461
+ }
462
+ });
463
+ return await operation(candidates, alias);
464
+ } finally {
465
+ candidates.close();
466
+ }
467
+ }
468
+ return await operation(
469
+ zip,
470
+ options.entryNameMode === "normalized" ? alias : exact ?? alias
471
+ );
472
+ });
398
473
  return {
399
474
  close: () => {
400
- if (!closed) zipFile.close();
401
475
  closed = true;
402
- return Promise.resolve();
476
+ closing ??= pending.then(() => handle.close());
477
+ return closing;
403
478
  },
404
- copyEntry: async (name, target) => {
405
- if (closed) throw new Error("Cannot read a closed ZIP archive");
406
- const entry = entries.get(name);
479
+ copyEntry: (name, target) => withEntry(name, async (zip, entry) => {
407
480
  if (entry === void 0) return false;
408
- await copyNodeZipEntry(zipFile, entry, target);
481
+ await copyNodeZipEntry(zip, entry, target);
409
482
  return true;
410
- },
411
- getEntrySize: (name) => {
412
- if (closed) throw new Error("Cannot read a closed ZIP archive");
413
- return Promise.resolve(entries.get(name)?.uncompressedSize);
414
- },
415
- listEntries: () => Promise.resolve([...entries.keys()]),
416
- readEntryRange: async (name, offset, length) => {
417
- if (closed) throw new Error("Cannot read a closed ZIP archive");
418
- const entry = entries.get(name);
483
+ }),
484
+ getEntrySize: (name) => withEntry(
485
+ name,
486
+ (_zip, entry) => Promise.resolve(entry?.uncompressedSize)
487
+ ),
488
+ listEntries: () => run(async (zip) => {
489
+ const names = /* @__PURE__ */ new Set();
490
+ await scanNodeZip(zip, (entry) => {
491
+ names.add(entry.fileName);
492
+ });
493
+ return [...names];
494
+ }),
495
+ readEntryRange: (name, offset, length) => withEntry(name, async (zip, entry) => {
419
496
  if (entry === void 0) return void 0;
420
- return await readNodeZipEntryRange(zipFile, entry, offset, length);
421
- },
422
- readEntry: async (name) => {
423
- if (closed) throw new Error("Cannot read a closed ZIP archive");
424
- const entry = entries.get(name);
497
+ return await readNodeZipEntryRange(zip, entry, offset, length);
498
+ }),
499
+ readEntry: (name) => withEntry(name, async (zip, entry) => {
425
500
  if (entry === void 0) return void 0;
426
- return await readNodeZipEntry(zipFile, entry);
427
- }
501
+ return await readNodeZipEntry(zip, entry);
502
+ })
428
503
  };
429
504
  }
505
+ var NodeZipSource = class extends yauzl.RandomAccessReader {
506
+ constructor(handle) {
507
+ super();
508
+ this.handle = handle;
509
+ }
510
+ handle;
511
+ read(buffer, offset, length, position, callback) {
512
+ const read2 = (written) => {
513
+ if (written === length) {
514
+ callback(null);
515
+ return;
516
+ }
517
+ fs.read(
518
+ this.handle.fd,
519
+ buffer,
520
+ offset + written,
521
+ length - written,
522
+ position + written,
523
+ (error, bytesRead) => {
524
+ if (error) callback(error);
525
+ else if (bytesRead === 0)
526
+ callback(new Error("Unexpected end of ZIP file"));
527
+ else read2(written + bytesRead);
528
+ }
529
+ );
530
+ };
531
+ read2(0);
532
+ }
533
+ _readStreamForRange(start, end) {
534
+ const handle = this.handle;
535
+ return Readable.from(
536
+ (async function* () {
537
+ for (let position = start; position < end; ) {
538
+ const buffer = Buffer.allocUnsafe(
539
+ Math.min(64 * 1024, end - position)
540
+ );
541
+ const { bytesRead } = await handle.read(
542
+ buffer,
543
+ 0,
544
+ buffer.length,
545
+ position
546
+ );
547
+ if (bytesRead === 0) return;
548
+ position += bytesRead;
549
+ yield buffer.subarray(0, bytesRead);
550
+ }
551
+ })()
552
+ );
553
+ }
554
+ };
555
+ function normalizeNodeZipName(name) {
556
+ const normalized = name.replaceAll("\\", "/").trim().split("/").filter((part) => part !== "" && part !== ".").join("/");
557
+ return normalized.split("/").includes("..") ? void 0 : normalized;
558
+ }
559
+ async function scanNodeZip(zip, visit) {
560
+ await new Promise((resolve2, reject) => {
561
+ const cleanup = () => {
562
+ zip.off("entry", onEntry);
563
+ zip.off("end", onEnd);
564
+ zip.off("error", onError);
565
+ };
566
+ const onError = (error) => {
567
+ cleanup();
568
+ reject(error);
569
+ };
570
+ const onEnd = () => {
571
+ cleanup();
572
+ resolve2();
573
+ };
574
+ const onEntry = (entry) => {
575
+ void Promise.resolve().then(() => entry.fileName.endsWith("/") ? void 0 : visit(entry)).then((keepReading) => {
576
+ if (keepReading === false) onEnd();
577
+ else zip.readEntry();
578
+ }).catch(
579
+ (error) => onError(
580
+ error instanceof Error ? error : new Error("Cannot scan ZIP", { cause: error })
581
+ )
582
+ );
583
+ };
584
+ zip.on("entry", onEntry);
585
+ zip.once("end", onEnd);
586
+ zip.once("error", onError);
587
+ zip.readEntry();
588
+ });
589
+ }
430
590
  async function copyNodeZipEntry(zipFile, entry, target) {
431
591
  const writer = await target.openWriter();
432
592
  try {