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