wiki-graph-sdk 0.7.0 → 0.8.0

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.
package/dist/worker.js CHANGED
@@ -4,8 +4,14 @@ globalThis.__WIKIGRAPH_DATA_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL
4
4
 
5
5
  // src/worker.ts
6
6
  import {
7
- runBuildJobWorker as runCoreBuildJobWorker
7
+ applyChapterJobArtifactFile,
8
+ runBuildJobWorker as runCoreBuildJobWorker,
9
+ writeChapterJobInputFile
8
10
  } from "wiki-graph-core/worker";
11
+ import {
12
+ resolveChapterPathReadonly,
13
+ WikiGraphArchiveFile as WikiGraphArchiveFile2
14
+ } from "wiki-graph-core";
9
15
 
10
16
  // src/node-platform.ts
11
17
  import * as asyncHooks from "async_hooks";
@@ -14,7 +20,7 @@ import * as fs from "fs";
14
20
  import * as fsPromises from "fs/promises";
15
21
  import * as os from "os";
16
22
  import * as pathModule from "path";
17
- import * as process from "process";
23
+ import * as process2 from "process";
18
24
  import { Readable, Writable } from "stream";
19
25
  import { pipeline } from "stream/promises";
20
26
  import * as sqlite3 from "sqlite3";
@@ -28,7 +34,7 @@ import {
28
34
  withWikiGraphStorage,
29
35
  normalizeTemplateName
30
36
  } from "wiki-graph-core/platform";
31
- var nodeProcess = process.default ?? process;
37
+ var nodeProcess = process2.default ?? process2;
32
38
  var nodeSqlite3 = sqlite3.default ?? sqlite3;
33
39
  var NODE_RESOURCE_KIND = /* @__PURE__ */ Symbol.for("wiki-graph-sdk.node-resource.v1");
34
40
  var NodeFile = class {
@@ -240,6 +246,14 @@ function decodeNodeResourceIdentity(identity, kind) {
240
246
  return void 0;
241
247
  }
242
248
  }
249
+ function getNodeResourcePath(resource) {
250
+ const brandedPath = getBrandedNodeResourcePath(resource, resource.kind);
251
+ if (brandedPath !== void 0) return brandedPath;
252
+ if (typeof resource === "object" && resource !== null && "path" in resource && typeof resource.path === "string") {
253
+ return resource.path;
254
+ }
255
+ throw new TypeError("Expected a Node File or Directory resource");
256
+ }
243
257
  function assertChildName(name) {
244
258
  if (name.length === 0 || name === "." || name === ".." || pathModule.isAbsolute(name) || name.includes("/") || name.includes("\\")) {
245
259
  throw new TypeError(`Directory child must be a relative name: ${name}`);
@@ -251,42 +265,42 @@ var NodeDatabaseConnection = class {
251
265
  }
252
266
  database;
253
267
  async close() {
254
- await new Promise((resolve2, reject) => {
268
+ await new Promise((resolve4, reject) => {
255
269
  this.database.close((error) => {
256
270
  if (error) reject(error);
257
- else resolve2();
271
+ else resolve4();
258
272
  });
259
273
  });
260
274
  }
261
275
  async execute(sql) {
262
- await new Promise((resolve2, reject) => {
276
+ await new Promise((resolve4, reject) => {
263
277
  this.database.exec(sql, (error) => {
264
278
  if (error) reject(error);
265
- else resolve2();
279
+ else resolve4();
266
280
  });
267
281
  });
268
282
  }
269
283
  async queryAll(sql, params = []) {
270
- return await new Promise((resolve2, reject) => {
284
+ return await new Promise((resolve4, reject) => {
271
285
  this.database.all(sql, [...params], (error, rows) => {
272
286
  if (error) reject(error);
273
- else resolve2(rows);
287
+ else resolve4(rows);
274
288
  });
275
289
  });
276
290
  }
277
291
  async queryOne(sql, params = []) {
278
- return await new Promise((resolve2, reject) => {
292
+ return await new Promise((resolve4, reject) => {
279
293
  this.database.get(sql, [...params], (error, row) => {
280
294
  if (error) reject(error);
281
- else resolve2(row);
295
+ else resolve4(row);
282
296
  });
283
297
  });
284
298
  }
285
299
  async run(sql, params = []) {
286
- await new Promise((resolve2, reject) => {
300
+ await new Promise((resolve4, reject) => {
287
301
  this.database.run(sql, [...params], (error) => {
288
302
  if (error) reject(error);
289
- else resolve2();
303
+ else resolve4();
290
304
  });
291
305
  });
292
306
  }
@@ -300,10 +314,10 @@ async function openNativeNodeDatabase(file, flags) {
300
314
  if (path === void 0) {
301
315
  throw new TypeError("The Node database adapter requires a NodeFile");
302
316
  }
303
- return await new Promise((resolve2, reject) => {
317
+ return await new Promise((resolve4, reject) => {
304
318
  const database = new nodeSqlite3.Database(path, flags, (error) => {
305
319
  if (error) reject(error);
306
- else resolve2(database);
320
+ else resolve4(database);
307
321
  });
308
322
  });
309
323
  }
@@ -312,19 +326,19 @@ async function openNodeZip(file) {
312
326
  if (path === void 0) {
313
327
  throw new TypeError("The Node ZIP adapter requires a NodeFile");
314
328
  }
315
- const zipFile = await new Promise((resolve2, reject) => {
329
+ const zipFile = await new Promise((resolve4, reject) => {
316
330
  yauzl.open(
317
331
  path,
318
332
  { autoClose: false, lazyEntries: true },
319
333
  (error, opened) => {
320
334
  if (error || opened === void 0)
321
335
  reject(error ?? new Error("Cannot open ZIP"));
322
- else resolve2(opened);
336
+ else resolve4(opened);
323
337
  }
324
338
  );
325
339
  });
326
340
  const entries = await new Promise(
327
- (resolve2, reject) => {
341
+ (resolve4, reject) => {
328
342
  const discovered = /* @__PURE__ */ new Map();
329
343
  const rejectAndClose = (error) => {
330
344
  zipFile.close();
@@ -334,7 +348,7 @@ async function openNodeZip(file) {
334
348
  };
335
349
  const resolveEntries = () => {
336
350
  zipFile.off("error", rejectAndClose);
337
- resolve2(discovered);
351
+ resolve4(discovered);
338
352
  };
339
353
  zipFile.on("entry", (entry) => {
340
354
  if (!entry.fileName.endsWith("/")) {
@@ -444,12 +458,12 @@ async function collectDecodedNodeStreamRange(input, offset, length) {
444
458
  return Buffer.concat(chunks, collected);
445
459
  }
446
460
  async function openNodeZipEntryStream(zipFile, entry, options) {
447
- return await new Promise((resolve2, reject) => {
461
+ return await new Promise((resolve4, reject) => {
448
462
  const callback = (error, opened) => {
449
463
  if (error || opened === void 0) {
450
464
  reject(error ?? new Error(`Cannot read ZIP entry: ${entry.fileName}`));
451
465
  } else {
452
- resolve2(opened);
466
+ resolve4(opened);
453
467
  }
454
468
  };
455
469
  if (options === void 0) zipFile.openReadStream(entry, callback);
@@ -580,11 +594,11 @@ async function writeNodeStream(input, writer) {
580
594
  );
581
595
  }
582
596
  async function collectNodeStream(input) {
583
- return await new Promise((resolve2, reject) => {
597
+ return await new Promise((resolve4, reject) => {
584
598
  const chunks = [];
585
599
  input.on("data", (chunk) => chunks.push(chunk));
586
600
  input.once("error", reject);
587
- input.once("end", () => resolve2(Buffer.concat(chunks)));
601
+ input.once("end", () => resolve4(Buffer.concat(chunks)));
588
602
  });
589
603
  }
590
604
  var nodeWikiGraphPlatform = {
@@ -685,6 +699,14 @@ function installNodeWikiGraphPlatform(stateRoot) {
685
699
  installWikiGraphPlatform(nodeWikiGraphPlatform);
686
700
  installWikiGraphStorage(createNodeWikiGraphStorage(stateRoot));
687
701
  }
702
+ function ensureNodeWikiGraphPlatform() {
703
+ installWikiGraphPlatform(nodeWikiGraphPlatform);
704
+ try {
705
+ getWikiGraphStorage();
706
+ } catch {
707
+ installWikiGraphStorage(createNodeWikiGraphStorage());
708
+ }
709
+ }
688
710
  async function withNodeWikiGraphStorage(stateRoot, operation) {
689
711
  return await withWikiGraphStorage(
690
712
  stateRoot === void 0 ? void 0 : createNodeWikiGraphStorage(stateRoot),
@@ -692,12 +714,309 @@ async function withNodeWikiGraphStorage(stateRoot, operation) {
692
714
  );
693
715
  }
694
716
 
717
+ // src/archive/target.ts
718
+ import { realpath, stat as stat2 } from "fs/promises";
719
+ import { basename as basename2, dirname as dirname2, isAbsolute as isAbsolute3, join as join2, relative, resolve as resolve3 } from "path";
720
+ import {
721
+ formatLocatedWikiGraphUri,
722
+ formatWikiGraphLibraryUri,
723
+ listWikiGraphLibraries,
724
+ listWikiGraphLibraryArchives,
725
+ parseLocatedWikiGraphUri,
726
+ parseWikiGraphLibraryUri,
727
+ resolveWikiGraphLibrary,
728
+ resolveWikiGraphLibraryArchiveFile
729
+ } from "wiki-graph-core";
730
+
731
+ // src/runtime-path.ts
732
+ import { homedir as homedir2 } from "os";
733
+ import { isAbsolute as isAbsolute2, resolve as resolve2 } from "path";
734
+
735
+ // src/runtime-context.ts
736
+ import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
737
+ var RUNTIME_CONTEXT_KEY = /* @__PURE__ */ Symbol.for("wiki-graph-sdk.runtime-context.v1");
738
+ var runtimeContext = getRuntimeContextStorage();
739
+ function getRuntimeContextStorage() {
740
+ const runtimeGlobal = globalThis;
741
+ const existing = runtimeGlobal[RUNTIME_CONTEXT_KEY];
742
+ if (existing !== void 0) {
743
+ return existing;
744
+ }
745
+ const storage = new AsyncLocalStorage2();
746
+ Object.defineProperty(runtimeGlobal, RUNTIME_CONTEXT_KEY, {
747
+ configurable: false,
748
+ enumerable: false,
749
+ value: storage,
750
+ writable: false
751
+ });
752
+ return storage;
753
+ }
754
+ function getWikiGraphSDKRuntimeContext() {
755
+ return runtimeContext.getStore() ?? {
756
+ cwd: process.cwd(),
757
+ env: process.env,
758
+ envPolicy: "production"
759
+ };
760
+ }
761
+
762
+ // src/runtime-path.ts
763
+ function resolveWikiGraphRuntimePath(path) {
764
+ const context = getWikiGraphSDKRuntimeContext();
765
+ const environmentHome = context.env.HOME?.trim();
766
+ const home = environmentHome === void 0 || environmentHome === "" ? homedir2() : environmentHome;
767
+ if (path === "~") return home;
768
+ if (path.startsWith("~/")) return resolve2(home, path.slice(2));
769
+ return isAbsolute2(path) ? path : resolve2(context.cwd, path);
770
+ }
771
+
772
+ // src/archive/target.ts
773
+ var WikiGraphArchiveOwnershipError = class extends Error {
774
+ code = "WIKI_GRAPH_ARCHIVE_OWNERSHIP_MISMATCH";
775
+ constructor(message) {
776
+ super(message);
777
+ this.name = "WikiGraphArchiveOwnershipError";
778
+ }
779
+ };
780
+ async function resolveWikiGraphArchiveLocation(target) {
781
+ if (target.kind === "standalone") {
782
+ return await resolveStandaloneLocation(target);
783
+ }
784
+ return await resolveLibraryLocation(target);
785
+ }
786
+ async function assertStandaloneWikiGraphArchivePath(inputPath) {
787
+ const path = resolveWikiGraphRuntimePath(inputPath);
788
+ const candidate = await canonicalPath(path);
789
+ const candidateIdentity = await readFileIdentity(path);
790
+ const libraries = await listWikiGraphLibraries();
791
+ const matches = await findMatchingMembers(
792
+ libraries,
793
+ candidate,
794
+ candidateIdentity
795
+ );
796
+ if (matches.length > 0) {
797
+ const match = requireUnambiguousManagedMatch(path, matches);
798
+ throw new WikiGraphArchiveOwnershipError(
799
+ `Standalone archive path belongs to registered library ${formatWikiGraphLibraryUri(
800
+ match.library.isDefault ? void 0 : match.library.publicId
801
+ )}: ${path}. Use the library UUID URI ${match.archive.uri} instead.`
802
+ );
803
+ }
804
+ for (const library of libraries) {
805
+ const rootPath = getNodeResourcePath(library.folder);
806
+ const root = await canonicalPath(rootPath);
807
+ if (!isWithin(root, candidate)) continue;
808
+ throw new WikiGraphArchiveOwnershipError(
809
+ `Standalone archive path belongs to registered library ${formatWikiGraphLibraryUri(
810
+ library.isDefault ? void 0 : library.publicId
811
+ )}: ${path}. Scan the library, then address the archive by its library UUID URI.`
812
+ );
813
+ }
814
+ return path;
815
+ }
816
+ async function resolveStandaloneLocation(target) {
817
+ if (target.path.startsWith("wikg://lib/")) {
818
+ throw new WikiGraphArchiveOwnershipError(
819
+ `A library archive URI cannot be used as a standalone path: ${target.path}`
820
+ );
821
+ }
822
+ const path = await assertStandaloneWikiGraphArchivePath(target.path);
823
+ const normalized = {
824
+ kind: "standalone",
825
+ path,
826
+ ...target.objectUri === void 0 ? {} : { objectUri: target.objectUri }
827
+ };
828
+ return {
829
+ archiveFile: new NodeFile(path),
830
+ archiveKey: path,
831
+ archivePath: path,
832
+ indexScope: { archiveKey: path, archivePath: path, kind: "archive-index" },
833
+ locatedUri: formatLocatedWikiGraphUri(path, target.objectUri),
834
+ target: normalized
835
+ };
836
+ }
837
+ async function resolveLibraryLocation(target) {
838
+ const parsedLocated = parseLocatedWikiGraphUri(target.uri);
839
+ const archiveUri = parsedLocated.archivePath ?? target.uri;
840
+ const parsed = parseWikiGraphLibraryUri(archiveUri);
841
+ if (parsed?.kind !== "archive") {
842
+ throw new WikiGraphArchiveOwnershipError(
843
+ `Library archive target must be a UUID archive URI: ${target.uri}`
844
+ );
845
+ }
846
+ await resolveWikiGraphLibrary(parsed);
847
+ const archiveFile = await resolveWikiGraphLibraryArchiveFile(archiveUri);
848
+ const normalized = {
849
+ kind: "library",
850
+ uri: target.uri
851
+ };
852
+ return {
853
+ archiveFile,
854
+ archiveKey: archiveUri,
855
+ archivePath: archiveUri,
856
+ indexScope: {
857
+ archiveKey: archiveUri,
858
+ archivePath: archiveUri,
859
+ kind: "archive-index"
860
+ },
861
+ libraryArchiveTarget: parsed,
862
+ locatedUri: target.uri,
863
+ publicArchiveUri: archiveUri,
864
+ target: normalized
865
+ };
866
+ }
867
+ async function canonicalPath(path) {
868
+ try {
869
+ return await realpath(path);
870
+ } catch {
871
+ const absolute = resolve3(path);
872
+ const parent = dirname2(absolute);
873
+ return parent === absolute ? absolute : join2(await canonicalPath(parent), basename2(absolute));
874
+ }
875
+ }
876
+ function isWithin(root, candidate) {
877
+ const child = relative(root, candidate);
878
+ return child === "" || !child.startsWith("..") && !isAbsolute3(child);
879
+ }
880
+ async function findMatchingMembers(libraries, candidate, candidateIdentity) {
881
+ const members = [];
882
+ for (const library of libraries) {
883
+ const archives = await listWikiGraphLibraryArchives({
884
+ isDefault: library.isDefault,
885
+ kind: "scope",
886
+ ...library.isDefault ? {} : { publicId: library.publicId }
887
+ });
888
+ for (const archive of archives) {
889
+ if (archive.file === void 0) continue;
890
+ members.push({
891
+ archive,
892
+ library,
893
+ memberPath: getNodeResourcePath(archive.file)
894
+ });
895
+ }
896
+ }
897
+ const exact = [];
898
+ for (const member of members) {
899
+ if (await canonicalPath(member.memberPath) === candidate) {
900
+ exact.push(member);
901
+ }
902
+ }
903
+ if (exact.length > 0 || candidateIdentity === void 0) return exact;
904
+ const identityMatches = [];
905
+ for (const member of members) {
906
+ if (await readFileIdentity(member.memberPath) === candidateIdentity) {
907
+ identityMatches.push(member);
908
+ }
909
+ }
910
+ return identityMatches;
911
+ }
912
+ function requireUnambiguousManagedMatch(path, matches) {
913
+ if (matches.length === 1) return matches[0];
914
+ throw new WikiGraphArchiveOwnershipError(
915
+ `Archive file identity is ambiguous across managed archives: ${path}. Use an explicit library UUID URI (${matches.map((match) => match.archive.uri).join(", ")}).`
916
+ );
917
+ }
918
+ async function readFileIdentity(path) {
919
+ try {
920
+ const value = await stat2(path);
921
+ return `${value.dev}:${value.ino}`;
922
+ } catch {
923
+ return void 0;
924
+ }
925
+ }
926
+
927
+ // src/archive/write.ts
928
+ import {
929
+ WikiGraphArchiveFile,
930
+ writeWikiGraphLibraryArchive
931
+ } from "wiki-graph-core";
932
+ async function writeWikiGraphArchiveLocation(location, operation, options = {}) {
933
+ const cacheOnly = options.searchIndexWritebackPolicy === "cache";
934
+ const write = async (archiveFile = location.archiveFile) => await new WikiGraphArchiveFile(archiveFile).write(operation, {
935
+ ...cacheOnly ? {} : { derivedStateKeys: [location.archiveKey, location.archivePath] },
936
+ ...options.invalidateContinuationCursors === void 0 ? {} : {
937
+ invalidateContinuationCursors: options.invalidateContinuationCursors
938
+ },
939
+ ...options.searchIndexWritebackPolicy === void 0 ? {} : { searchIndexWritebackPolicy: options.searchIndexWritebackPolicy }
940
+ });
941
+ if (location.libraryArchiveTarget === void 0 || cacheOnly || options.refreshLibraryIndex === false) {
942
+ return await write();
943
+ }
944
+ return await writeWikiGraphLibraryArchive({
945
+ additionalDerivedStateKeys: [location.archiveKey, location.archivePath],
946
+ ...options.invalidateContinuationCursors === void 0 ? {} : {
947
+ invalidateContinuationCursors: options.invalidateContinuationCursors
948
+ },
949
+ ...options.onIndexSyncError === void 0 ? {} : { onIndexSyncError: options.onIndexSyncError },
950
+ operation: async (archiveFile) => await write(archiveFile),
951
+ target: location.libraryArchiveTarget
952
+ });
953
+ }
954
+
695
955
  // src/worker.ts
696
956
  import {
697
- applyChapterJobArtifactFile,
957
+ applyChapterJobArtifactFile as applyChapterJobArtifactFile2,
698
958
  createLocalChapterJobFileExecutor,
699
- writeChapterJobInputFile
959
+ writeChapterJobInputFile as writeChapterJobInputFile2
700
960
  } from "wiki-graph-core/worker";
961
+ async function extractWikiGraphJobSnapshots(options) {
962
+ ensureNodeWikiGraphPlatform();
963
+ return await withNodeWikiGraphStorage(
964
+ options.stateDir,
965
+ async () => await new WikiGraphArchiveFile2(
966
+ new NodeFile(options.wikgPath)
967
+ ).readDocument(async (document) => {
968
+ const results = [];
969
+ for (const request of options.requests) {
970
+ options.signal?.throwIfAborted();
971
+ const chapterId = await resolveChapterPathReadonly(
972
+ document,
973
+ request.chapterPath
974
+ );
975
+ const revision = await writeChapterJobInputFile(
976
+ document,
977
+ chapterId,
978
+ request.kind,
979
+ new NodeFile(request.outputPath),
980
+ request.options
981
+ );
982
+ const result = { ...request, chapterId, revision };
983
+ results.push(result);
984
+ await options.onSnapshot?.(result);
985
+ }
986
+ options.signal?.throwIfAborted();
987
+ return results;
988
+ })
989
+ );
990
+ }
991
+ async function applyWikiGraphJobArtifacts(options) {
992
+ ensureNodeWikiGraphPlatform();
993
+ return await withNodeWikiGraphStorage(options.stateDir, async () => {
994
+ const location = await resolveWikiGraphArchiveLocation(options.archive);
995
+ let applied = 0;
996
+ await writeWikiGraphArchiveLocation(
997
+ location,
998
+ async (document) => {
999
+ for await (const artifact of options.artifacts) {
1000
+ options.signal?.throwIfAborted();
1001
+ const revision = await document.serials.getRevision(
1002
+ artifact.chapterId
1003
+ );
1004
+ await applyChapterJobArtifactFile(
1005
+ document,
1006
+ artifact.chapterId,
1007
+ artifact.kind,
1008
+ revision,
1009
+ new NodeFile(artifact.artifactPath)
1010
+ );
1011
+ applied += 1;
1012
+ }
1013
+ },
1014
+ { invalidateContinuationCursors: true }
1015
+ );
1016
+ options.signal?.throwIfAborted();
1017
+ return { applied };
1018
+ });
1019
+ }
701
1020
  async function runBuildJobWorker(options) {
702
1021
  installNodeWikiGraphPlatform();
703
1022
  const { stateDir, ...coreOptions } = options;
@@ -707,9 +1026,11 @@ async function runBuildJobWorker(options) {
707
1026
  );
708
1027
  }
709
1028
  export {
710
- applyChapterJobArtifactFile,
1029
+ applyChapterJobArtifactFile2 as applyChapterJobArtifactFile,
1030
+ applyWikiGraphJobArtifacts,
711
1031
  createLocalChapterJobFileExecutor,
1032
+ extractWikiGraphJobSnapshots,
712
1033
  runBuildJobWorker,
713
- writeChapterJobInputFile
1034
+ writeChapterJobInputFile2 as writeChapterJobInputFile
714
1035
  };
715
1036
  //# sourceMappingURL=worker.js.map