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