wiki-graph-sdk 0.7.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/LICENSE +201 -0
- package/README.md +32 -0
- package/dist/archives.cjs +2799 -0
- package/dist/archives.cjs.map +1 -0
- package/dist/archives.d.ts +249 -0
- package/dist/archives.js +2814 -0
- package/dist/archives.js.map +1 -0
- package/dist/conversions.cjs +1332 -0
- package/dist/conversions.cjs.map +1 -0
- package/dist/conversions.d.ts +37 -0
- package/dist/conversions.js +1308 -0
- package/dist/conversions.js.map +1 -0
- package/dist/data/editor/clue_reviewer.jinja +410 -0
- package/dist/data/editor/clue_reviewer_generator.jinja +428 -0
- package/dist/data/editor/revision_feedback.jinja +5 -0
- package/dist/data/editor/text_compressor.jinja +131 -0
- package/dist/data/guaranteed/response_intent_classifier.jinja +8 -0
- package/dist/data/output/epub/cover.xhtml.jinja +13 -0
- package/dist/data/output/epub/nav.xhtml.jinja +12 -0
- package/dist/data/output/epub/package.opf.xml.jinja +39 -0
- package/dist/data/output/epub/section.xhtml.jinja +12 -0
- package/dist/data/topologization/book_coherence_extraction.jinja +223 -0
- package/dist/data/topologization/chunk_extraction.jinja +233 -0
- package/dist/data/topologization/evidence_choice.jinja +38 -0
- package/dist/data/topologization/translate_chunks.jinja +138 -0
- package/dist/data/topologization/user_focused_extraction.jinja +378 -0
- package/dist/default-worker.cjs +1709 -0
- package/dist/default-worker.cjs.map +1 -0
- package/dist/default-worker.d.ts +10 -0
- package/dist/default-worker.js +1705 -0
- package/dist/default-worker.js.map +1 -0
- package/dist/embedding.cjs +1091 -0
- package/dist/embedding.cjs.map +1 -0
- package/dist/embedding.d.ts +26 -0
- package/dist/embedding.js +1062 -0
- package/dist/embedding.js.map +1 -0
- package/dist/entry-context.cjs +91 -0
- package/dist/entry-context.cjs.map +1 -0
- package/dist/entry-context.d.ts +20 -0
- package/dist/entry-context.js +68 -0
- package/dist/entry-context.js.map +1 -0
- package/dist/gc.cjs +731 -0
- package/dist/gc.cjs.map +1 -0
- package/dist/gc.d.ts +14 -0
- package/dist/gc.js +707 -0
- package/dist/gc.js.map +1 -0
- package/dist/index.cjs +4760 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +213 -0
- package/dist/index.js +4808 -0
- package/dist/index.js.map +1 -0
- package/dist/jobs-Dd5PMSId.d.ts +98 -0
- package/dist/llm.cjs +101 -0
- package/dist/llm.cjs.map +1 -0
- package/dist/llm.d.ts +7 -0
- package/dist/llm.js +79 -0
- package/dist/llm.js.map +1 -0
- package/dist/local-config.cjs +1024 -0
- package/dist/local-config.cjs.map +1 -0
- package/dist/local-config.d.ts +14 -0
- package/dist/local-config.js +989 -0
- package/dist/local-config.js.map +1 -0
- package/dist/maintenance.cjs +800 -0
- package/dist/maintenance.cjs.map +1 -0
- package/dist/maintenance.d.ts +24 -0
- package/dist/maintenance.js +778 -0
- package/dist/maintenance.js.map +1 -0
- package/dist/node-platform.cjs +758 -0
- package/dist/node-platform.cjs.map +1 -0
- package/dist/node-platform.d.ts +49 -0
- package/dist/node-platform.js +723 -0
- package/dist/node-platform.js.map +1 -0
- package/dist/planning.cjs +164 -0
- package/dist/planning.cjs.map +1 -0
- package/dist/planning.d.ts +43 -0
- package/dist/planning.js +137 -0
- package/dist/planning.js.map +1 -0
- package/dist/runtime-config.cjs +1155 -0
- package/dist/runtime-config.cjs.map +1 -0
- package/dist/runtime-config.d.ts +50 -0
- package/dist/runtime-config.js +1127 -0
- package/dist/runtime-config.js.map +1 -0
- package/dist/stage.cjs +1265 -0
- package/dist/stage.cjs.map +1 -0
- package/dist/stage.d.ts +16 -0
- package/dist/stage.js +1239 -0
- package/dist/stage.js.map +1 -0
- package/dist/wikispine.cjs +85 -0
- package/dist/wikispine.cjs.map +1 -0
- package/dist/wikispine.d.ts +6 -0
- package/dist/wikispine.js +63 -0
- package/dist/wikispine.js.map +1 -0
- package/dist/worker.cjs +738 -0
- package/dist/worker.cjs.map +1 -0
- package/dist/worker.d.ts +157 -0
- package/dist/worker.js +715 -0
- package/dist/worker.js.map +1 -0
- package/package.json +151 -0
|
@@ -0,0 +1,778 @@
|
|
|
1
|
+
import { fileURLToPath as __WIKIGRAPH_FILE_URL_TO_PATH__ } from "url";
|
|
2
|
+
import { resolve as __WIKIGRAPH_RESOLVE__ } from "path";
|
|
3
|
+
globalThis.__WIKIGRAPH_DATA_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL_TO_PATH__(new URL("./data", import.meta.url)));
|
|
4
|
+
|
|
5
|
+
// src/maintenance.ts
|
|
6
|
+
import {
|
|
7
|
+
migrateLegacySdpubToWikg,
|
|
8
|
+
parseWikiGraphLibraryUri,
|
|
9
|
+
upgradeWikiGraphMaintenanceTarget
|
|
10
|
+
} from "wiki-graph-core";
|
|
11
|
+
import { basename as basename2 } from "path";
|
|
12
|
+
|
|
13
|
+
// src/node-platform.ts
|
|
14
|
+
import * as asyncHooks from "async_hooks";
|
|
15
|
+
import * as crypto from "crypto";
|
|
16
|
+
import * as fs from "fs";
|
|
17
|
+
import * as fsPromises from "fs/promises";
|
|
18
|
+
import * as os from "os";
|
|
19
|
+
import * as pathModule from "path";
|
|
20
|
+
import * as process2 from "process";
|
|
21
|
+
import { Readable, Writable } from "stream";
|
|
22
|
+
import { pipeline } from "stream/promises";
|
|
23
|
+
import * as sqlite3 from "sqlite3";
|
|
24
|
+
import * as yauzl from "yauzl";
|
|
25
|
+
import * as yazl from "yazl";
|
|
26
|
+
import { Environment, Loader } from "nunjucks";
|
|
27
|
+
import {
|
|
28
|
+
getWikiGraphStorage,
|
|
29
|
+
installWikiGraphPlatform,
|
|
30
|
+
installWikiGraphStorage,
|
|
31
|
+
withWikiGraphStorage,
|
|
32
|
+
normalizeTemplateName
|
|
33
|
+
} from "wiki-graph-core/platform";
|
|
34
|
+
var nodeProcess = process2.default ?? process2;
|
|
35
|
+
var nodeSqlite3 = sqlite3.default ?? sqlite3;
|
|
36
|
+
var NODE_RESOURCE_KIND = /* @__PURE__ */ Symbol.for("wiki-graph-sdk.node-resource.v1");
|
|
37
|
+
var NodeFile = class {
|
|
38
|
+
constructor(path, name = pathModule.basename(path)) {
|
|
39
|
+
this.path = path;
|
|
40
|
+
this.identity = encodeNodeResourceIdentity("file", path);
|
|
41
|
+
this.name = name;
|
|
42
|
+
}
|
|
43
|
+
path;
|
|
44
|
+
kind = "file";
|
|
45
|
+
[NODE_RESOURCE_KIND] = "file";
|
|
46
|
+
identity;
|
|
47
|
+
name;
|
|
48
|
+
async openReader() {
|
|
49
|
+
const handle = await fsPromises.open(this.path, "r");
|
|
50
|
+
let closed = false;
|
|
51
|
+
try {
|
|
52
|
+
const stats = await handle.stat();
|
|
53
|
+
if (!stats.isFile())
|
|
54
|
+
throw new Error("File reader requires a regular file");
|
|
55
|
+
const { size } = stats;
|
|
56
|
+
return {
|
|
57
|
+
size,
|
|
58
|
+
close: async () => {
|
|
59
|
+
if (!closed) await handle.close();
|
|
60
|
+
closed = true;
|
|
61
|
+
},
|
|
62
|
+
read: async (offset, length) => {
|
|
63
|
+
if (closed) throw new Error("Cannot read from a closed FileReader");
|
|
64
|
+
assertFileRange(offset, length, size);
|
|
65
|
+
const buffer = Buffer.allocUnsafe(length);
|
|
66
|
+
let bytesRead = 0;
|
|
67
|
+
while (bytesRead < length) {
|
|
68
|
+
const result = await handle.read(
|
|
69
|
+
buffer,
|
|
70
|
+
bytesRead,
|
|
71
|
+
length - bytesRead,
|
|
72
|
+
offset + bytesRead
|
|
73
|
+
);
|
|
74
|
+
if (result.bytesRead === 0) {
|
|
75
|
+
throw new Error("File changed while reading its byte range");
|
|
76
|
+
}
|
|
77
|
+
bytesRead += result.bytesRead;
|
|
78
|
+
}
|
|
79
|
+
return new Uint8Array(buffer.buffer, buffer.byteOffset, bytesRead);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
} catch (error) {
|
|
83
|
+
await handle.close();
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async getLastModified() {
|
|
88
|
+
try {
|
|
89
|
+
return (await fsPromises.stat(this.path)).mtimeMs;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
|
|
92
|
+
return void 0;
|
|
93
|
+
}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async openWriter() {
|
|
98
|
+
await fsPromises.mkdir(pathModule.dirname(this.path), { recursive: true });
|
|
99
|
+
const temporary = `${this.path}.tmp-${crypto.randomUUID()}`;
|
|
100
|
+
const handle = await fsPromises.open(temporary, "wx");
|
|
101
|
+
let closed = false;
|
|
102
|
+
return {
|
|
103
|
+
write: async (data) => {
|
|
104
|
+
if (closed) throw new Error("Cannot write to a closed FileWriter");
|
|
105
|
+
await writeNodeFileHandle(handle, data);
|
|
106
|
+
},
|
|
107
|
+
writeAt: async (offset, data) => {
|
|
108
|
+
if (closed) throw new Error("Cannot write to a closed FileWriter");
|
|
109
|
+
assertFileOffset(offset);
|
|
110
|
+
await writeNodeFileHandle(handle, data, offset);
|
|
111
|
+
},
|
|
112
|
+
commit: async () => {
|
|
113
|
+
if (!closed) {
|
|
114
|
+
await handle.close();
|
|
115
|
+
await fsPromises.rename(temporary, this.path);
|
|
116
|
+
}
|
|
117
|
+
closed = true;
|
|
118
|
+
},
|
|
119
|
+
abort: async () => {
|
|
120
|
+
if (!closed) {
|
|
121
|
+
await handle.close();
|
|
122
|
+
await fsPromises.rm(temporary, { force: true });
|
|
123
|
+
}
|
|
124
|
+
closed = true;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
async function writeNodeFileHandle(handle, data, position) {
|
|
130
|
+
const buffer = Buffer.from(data);
|
|
131
|
+
let written = 0;
|
|
132
|
+
while (written < buffer.byteLength) {
|
|
133
|
+
const result = await handle.write(
|
|
134
|
+
buffer,
|
|
135
|
+
written,
|
|
136
|
+
buffer.byteLength - written,
|
|
137
|
+
position === void 0 ? null : position + written
|
|
138
|
+
);
|
|
139
|
+
if (result.bytesWritten === 0) {
|
|
140
|
+
throw new Error("File writer made no progress");
|
|
141
|
+
}
|
|
142
|
+
written += result.bytesWritten;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function assertFileRange(offset, length, size) {
|
|
146
|
+
assertFileOffset(offset);
|
|
147
|
+
if (!Number.isSafeInteger(length) || length < 0) {
|
|
148
|
+
throw new RangeError(
|
|
149
|
+
"File read length must be a non-negative safe integer"
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
if (offset > size || length > size - offset) {
|
|
153
|
+
throw new RangeError("File read range exceeds the file size");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function assertFileOffset(offset) {
|
|
157
|
+
if (!Number.isSafeInteger(offset) || offset < 0) {
|
|
158
|
+
throw new RangeError("File offset must be a non-negative safe integer");
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
var NodeDirectory = class _NodeDirectory {
|
|
162
|
+
constructor(path) {
|
|
163
|
+
this.path = path;
|
|
164
|
+
this.identity = encodeNodeResourceIdentity("directory", path);
|
|
165
|
+
this.name = pathModule.basename(path);
|
|
166
|
+
}
|
|
167
|
+
path;
|
|
168
|
+
kind = "directory";
|
|
169
|
+
[NODE_RESOURCE_KIND] = "directory";
|
|
170
|
+
identity;
|
|
171
|
+
name;
|
|
172
|
+
async getFile(name) {
|
|
173
|
+
assertChildName(name);
|
|
174
|
+
const file = new NodeFile(pathModule.join(this.path, name));
|
|
175
|
+
try {
|
|
176
|
+
return (await fsPromises.stat(file.path)).isFile() ? file : void 0;
|
|
177
|
+
} catch {
|
|
178
|
+
return void 0;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async getLastModified() {
|
|
182
|
+
try {
|
|
183
|
+
return (await fsPromises.stat(this.path)).mtimeMs;
|
|
184
|
+
} catch (error) {
|
|
185
|
+
if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
|
|
186
|
+
return void 0;
|
|
187
|
+
}
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async getDirectory(name) {
|
|
192
|
+
assertChildName(name);
|
|
193
|
+
const directory = new _NodeDirectory(pathModule.join(this.path, name));
|
|
194
|
+
try {
|
|
195
|
+
return (await fsPromises.stat(directory.path)).isDirectory() ? directory : void 0;
|
|
196
|
+
} catch {
|
|
197
|
+
return void 0;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
async list() {
|
|
201
|
+
const entries = await fsPromises.readdir(this.path, {
|
|
202
|
+
withFileTypes: true
|
|
203
|
+
});
|
|
204
|
+
return entries.map(
|
|
205
|
+
(entry) => entry.isDirectory() ? new _NodeDirectory(pathModule.join(this.path, entry.name)) : new NodeFile(pathModule.join(this.path, entry.name), entry.name)
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
async createFile(name) {
|
|
209
|
+
assertChildName(name);
|
|
210
|
+
await fsPromises.mkdir(this.path, { recursive: true });
|
|
211
|
+
const file = new NodeFile(pathModule.join(this.path, name));
|
|
212
|
+
await fsPromises.writeFile(file.path, "", { flag: "wx" });
|
|
213
|
+
return file;
|
|
214
|
+
}
|
|
215
|
+
async createDirectory(name) {
|
|
216
|
+
assertChildName(name);
|
|
217
|
+
await fsPromises.mkdir(this.path, { recursive: true });
|
|
218
|
+
const directory = new _NodeDirectory(pathModule.join(this.path, name));
|
|
219
|
+
await fsPromises.mkdir(directory.path);
|
|
220
|
+
return directory;
|
|
221
|
+
}
|
|
222
|
+
async remove(name, options) {
|
|
223
|
+
assertChildName(name);
|
|
224
|
+
await fsPromises.rm(pathModule.join(this.path, name), {
|
|
225
|
+
force: true,
|
|
226
|
+
recursive: options?.recursive === true
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
function encodeNodeResourceIdentity(kind, path) {
|
|
231
|
+
return `node-${kind}:${Buffer.from(pathModule.resolve(path), "utf8").toString("base64url")}`;
|
|
232
|
+
}
|
|
233
|
+
function decodeNodeResourceIdentity(identity, kind) {
|
|
234
|
+
const prefix = `node-${kind}:`;
|
|
235
|
+
if (!identity.startsWith(prefix)) {
|
|
236
|
+
return pathModule.isAbsolute(identity) ? identity : void 0;
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
return Buffer.from(identity.slice(prefix.length), "base64url").toString(
|
|
240
|
+
"utf8"
|
|
241
|
+
);
|
|
242
|
+
} catch {
|
|
243
|
+
return void 0;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function assertChildName(name) {
|
|
247
|
+
if (name.length === 0 || name === "." || name === ".." || pathModule.isAbsolute(name) || name.includes("/") || name.includes("\\")) {
|
|
248
|
+
throw new TypeError(`Directory child must be a relative name: ${name}`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
var NodeDatabaseConnection = class {
|
|
252
|
+
constructor(database) {
|
|
253
|
+
this.database = database;
|
|
254
|
+
}
|
|
255
|
+
database;
|
|
256
|
+
async close() {
|
|
257
|
+
await new Promise((resolve3, reject) => {
|
|
258
|
+
this.database.close((error) => {
|
|
259
|
+
if (error) reject(error);
|
|
260
|
+
else resolve3();
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
async execute(sql) {
|
|
265
|
+
await new Promise((resolve3, reject) => {
|
|
266
|
+
this.database.exec(sql, (error) => {
|
|
267
|
+
if (error) reject(error);
|
|
268
|
+
else resolve3();
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
async queryAll(sql, params = []) {
|
|
273
|
+
return await new Promise((resolve3, reject) => {
|
|
274
|
+
this.database.all(sql, [...params], (error, rows) => {
|
|
275
|
+
if (error) reject(error);
|
|
276
|
+
else resolve3(rows);
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
async queryOne(sql, params = []) {
|
|
281
|
+
return await new Promise((resolve3, reject) => {
|
|
282
|
+
this.database.get(sql, [...params], (error, row) => {
|
|
283
|
+
if (error) reject(error);
|
|
284
|
+
else resolve3(row);
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
async run(sql, params = []) {
|
|
289
|
+
await new Promise((resolve3, reject) => {
|
|
290
|
+
this.database.run(sql, [...params], (error) => {
|
|
291
|
+
if (error) reject(error);
|
|
292
|
+
else resolve3();
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
async function openNodeDatabase(file, options) {
|
|
298
|
+
const flags = (options.mode === "readonly" ? nodeSqlite3.OPEN_READONLY : nodeSqlite3.OPEN_READWRITE | (options.create ? nodeSqlite3.OPEN_CREATE : 0)) | nodeSqlite3.OPEN_FULLMUTEX;
|
|
299
|
+
return new NodeDatabaseConnection(await openNativeNodeDatabase(file, flags));
|
|
300
|
+
}
|
|
301
|
+
async function openNativeNodeDatabase(file, flags) {
|
|
302
|
+
const path = getBrandedNodeResourcePath(file, "file");
|
|
303
|
+
if (path === void 0) {
|
|
304
|
+
throw new TypeError("The Node database adapter requires a NodeFile");
|
|
305
|
+
}
|
|
306
|
+
return await new Promise((resolve3, reject) => {
|
|
307
|
+
const database = new nodeSqlite3.Database(path, flags, (error) => {
|
|
308
|
+
if (error) reject(error);
|
|
309
|
+
else resolve3(database);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
async function openNodeZip(file) {
|
|
314
|
+
const path = getBrandedNodeResourcePath(file, "file");
|
|
315
|
+
if (path === void 0) {
|
|
316
|
+
throw new TypeError("The Node ZIP adapter requires a NodeFile");
|
|
317
|
+
}
|
|
318
|
+
const zipFile = await new Promise((resolve3, reject) => {
|
|
319
|
+
yauzl.open(
|
|
320
|
+
path,
|
|
321
|
+
{ autoClose: false, lazyEntries: true },
|
|
322
|
+
(error, opened) => {
|
|
323
|
+
if (error || opened === void 0)
|
|
324
|
+
reject(error ?? new Error("Cannot open ZIP"));
|
|
325
|
+
else resolve3(opened);
|
|
326
|
+
}
|
|
327
|
+
);
|
|
328
|
+
});
|
|
329
|
+
const entries = await new Promise(
|
|
330
|
+
(resolve3, reject) => {
|
|
331
|
+
const discovered = /* @__PURE__ */ new Map();
|
|
332
|
+
const rejectAndClose = (error) => {
|
|
333
|
+
zipFile.close();
|
|
334
|
+
reject(
|
|
335
|
+
error instanceof Error ? error : new Error("Cannot scan ZIP central directory")
|
|
336
|
+
);
|
|
337
|
+
};
|
|
338
|
+
const resolveEntries = () => {
|
|
339
|
+
zipFile.off("error", rejectAndClose);
|
|
340
|
+
resolve3(discovered);
|
|
341
|
+
};
|
|
342
|
+
zipFile.on("entry", (entry) => {
|
|
343
|
+
if (!entry.fileName.endsWith("/")) {
|
|
344
|
+
discovered.set(entry.fileName, entry);
|
|
345
|
+
}
|
|
346
|
+
zipFile.readEntry();
|
|
347
|
+
});
|
|
348
|
+
zipFile.once("error", rejectAndClose);
|
|
349
|
+
zipFile.once("end", resolveEntries);
|
|
350
|
+
zipFile.readEntry();
|
|
351
|
+
}
|
|
352
|
+
);
|
|
353
|
+
let closed = false;
|
|
354
|
+
return {
|
|
355
|
+
close: () => {
|
|
356
|
+
if (!closed) zipFile.close();
|
|
357
|
+
closed = true;
|
|
358
|
+
return Promise.resolve();
|
|
359
|
+
},
|
|
360
|
+
copyEntry: async (name, target) => {
|
|
361
|
+
if (closed) throw new Error("Cannot read a closed ZIP archive");
|
|
362
|
+
const entry = entries.get(name);
|
|
363
|
+
if (entry === void 0) return false;
|
|
364
|
+
await copyNodeZipEntry(zipFile, entry, target);
|
|
365
|
+
return true;
|
|
366
|
+
},
|
|
367
|
+
getEntrySize: (name) => {
|
|
368
|
+
if (closed) throw new Error("Cannot read a closed ZIP archive");
|
|
369
|
+
return Promise.resolve(entries.get(name)?.uncompressedSize);
|
|
370
|
+
},
|
|
371
|
+
listEntries: () => Promise.resolve([...entries.keys()]),
|
|
372
|
+
readEntryRange: async (name, offset, length) => {
|
|
373
|
+
if (closed) throw new Error("Cannot read a closed ZIP archive");
|
|
374
|
+
const entry = entries.get(name);
|
|
375
|
+
if (entry === void 0) return void 0;
|
|
376
|
+
return await readNodeZipEntryRange(zipFile, entry, offset, length);
|
|
377
|
+
},
|
|
378
|
+
readEntry: async (name) => {
|
|
379
|
+
if (closed) throw new Error("Cannot read a closed ZIP archive");
|
|
380
|
+
const entry = entries.get(name);
|
|
381
|
+
if (entry === void 0) return void 0;
|
|
382
|
+
return await readNodeZipEntry(zipFile, entry);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
async function copyNodeZipEntry(zipFile, entry, target) {
|
|
387
|
+
const writer = await target.openWriter();
|
|
388
|
+
try {
|
|
389
|
+
const stream = await openNodeZipEntryStream(zipFile, entry);
|
|
390
|
+
await writeNodeStream(stream, writer);
|
|
391
|
+
await writer.commit();
|
|
392
|
+
} catch (error) {
|
|
393
|
+
await writer.abort().catch(() => void 0);
|
|
394
|
+
throw error;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
async function readNodeZipEntry(zipFile, entry) {
|
|
398
|
+
return await collectNodeStream(await openNodeZipEntryStream(zipFile, entry));
|
|
399
|
+
}
|
|
400
|
+
async function readNodeZipEntryRange(zipFile, entry, offset, length) {
|
|
401
|
+
assertFileRange(offset, length, entry.uncompressedSize);
|
|
402
|
+
if (length === 0) return new Uint8Array();
|
|
403
|
+
if (entry.compressionMethod !== 0) {
|
|
404
|
+
const content2 = await collectDecodedNodeStreamRange(
|
|
405
|
+
await openNodeZipEntryStream(zipFile, entry),
|
|
406
|
+
offset,
|
|
407
|
+
length
|
|
408
|
+
);
|
|
409
|
+
return new Uint8Array(
|
|
410
|
+
content2.buffer,
|
|
411
|
+
content2.byteOffset,
|
|
412
|
+
content2.byteLength
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
const content = await collectNodeStream(
|
|
416
|
+
await openNodeZipEntryStream(zipFile, entry, {
|
|
417
|
+
decompress: null,
|
|
418
|
+
decrypt: null,
|
|
419
|
+
end: offset + length,
|
|
420
|
+
start: offset
|
|
421
|
+
})
|
|
422
|
+
);
|
|
423
|
+
return new Uint8Array(content.buffer, content.byteOffset, content.byteLength);
|
|
424
|
+
}
|
|
425
|
+
async function collectDecodedNodeStreamRange(input, offset, length) {
|
|
426
|
+
const chunks = [];
|
|
427
|
+
const end = offset + length;
|
|
428
|
+
let position = 0;
|
|
429
|
+
let collected = 0;
|
|
430
|
+
for await (const value of input) {
|
|
431
|
+
const chunk = Buffer.isBuffer(value) ? value : Buffer.from(value);
|
|
432
|
+
const chunkEnd = position + chunk.byteLength;
|
|
433
|
+
if (chunkEnd > offset && position < end) {
|
|
434
|
+
const selected = chunk.subarray(
|
|
435
|
+
Math.max(0, offset - position),
|
|
436
|
+
Math.min(chunk.byteLength, end - position)
|
|
437
|
+
);
|
|
438
|
+
chunks.push(selected);
|
|
439
|
+
collected += selected.byteLength;
|
|
440
|
+
}
|
|
441
|
+
position = chunkEnd;
|
|
442
|
+
if (position >= end) break;
|
|
443
|
+
}
|
|
444
|
+
if (collected !== length) {
|
|
445
|
+
throw new Error("ZIP entry changed while reading its byte range");
|
|
446
|
+
}
|
|
447
|
+
return Buffer.concat(chunks, collected);
|
|
448
|
+
}
|
|
449
|
+
async function openNodeZipEntryStream(zipFile, entry, options) {
|
|
450
|
+
return await new Promise((resolve3, reject) => {
|
|
451
|
+
const callback = (error, opened) => {
|
|
452
|
+
if (error || opened === void 0) {
|
|
453
|
+
reject(error ?? new Error(`Cannot read ZIP entry: ${entry.fileName}`));
|
|
454
|
+
} else {
|
|
455
|
+
resolve3(opened);
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
if (options === void 0) zipFile.openReadStream(entry, callback);
|
|
459
|
+
else zipFile.openReadStream(entry, options, callback);
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
async function writeNodeZip(file, entries) {
|
|
463
|
+
const zipFile = new yazl.ZipFile();
|
|
464
|
+
const writer = await file.openWriter();
|
|
465
|
+
const outputStream = zipFile.outputStream;
|
|
466
|
+
const outputDone = writeNodeStream(outputStream, writer);
|
|
467
|
+
const entryState = { cancelled: false, complete: false };
|
|
468
|
+
let iterator;
|
|
469
|
+
try {
|
|
470
|
+
iterator = getNodeZipEntryIterator(entries);
|
|
471
|
+
const entriesDone = addNodeZipEntries(zipFile, iterator, entryState);
|
|
472
|
+
await Promise.race([entriesDone, outputDone]);
|
|
473
|
+
if (!entryState.complete) {
|
|
474
|
+
throw new Error("ZIP output ended before entry production completed");
|
|
475
|
+
}
|
|
476
|
+
zipFile.end();
|
|
477
|
+
await outputDone;
|
|
478
|
+
await writer.commit();
|
|
479
|
+
} catch (error) {
|
|
480
|
+
entryState.cancelled = true;
|
|
481
|
+
if (!entryState.complete && iterator !== void 0) {
|
|
482
|
+
closeNodeZipEntryIterator(iterator);
|
|
483
|
+
}
|
|
484
|
+
if (!outputStream.destroyed) {
|
|
485
|
+
outputStream.destroy(
|
|
486
|
+
error instanceof Error ? error : new Error("Cannot write ZIP archive")
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
await outputDone.catch(() => void 0);
|
|
490
|
+
await writer.abort().catch(() => void 0);
|
|
491
|
+
throw error;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
async function addNodeZipEntries(zipFile, iterator, state) {
|
|
495
|
+
while (!state.cancelled) {
|
|
496
|
+
const result = await iterator.next();
|
|
497
|
+
if (state.cancelled) return;
|
|
498
|
+
if (result.done) {
|
|
499
|
+
state.complete = true;
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
if ("file" in result.value) {
|
|
503
|
+
zipFile.addReadStream(
|
|
504
|
+
createNodeFileReadStream(result.value.file),
|
|
505
|
+
result.value.name,
|
|
506
|
+
{ compress: false }
|
|
507
|
+
);
|
|
508
|
+
} else if ("data" in result.value) {
|
|
509
|
+
zipFile.addBuffer(Buffer.from(result.value.data), result.value.name, {
|
|
510
|
+
compress: false
|
|
511
|
+
});
|
|
512
|
+
} else {
|
|
513
|
+
zipFile.addReadStream(
|
|
514
|
+
createNodeRangeReadStream(result.value),
|
|
515
|
+
result.value.name,
|
|
516
|
+
{ compress: false, size: result.value.size }
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function createNodeFileReadStream(file) {
|
|
522
|
+
return Readable.from(
|
|
523
|
+
(async function* () {
|
|
524
|
+
const reader = await file.openReader();
|
|
525
|
+
try {
|
|
526
|
+
for (let offset = 0; offset < reader.size; ) {
|
|
527
|
+
const chunk = await reader.read(
|
|
528
|
+
offset,
|
|
529
|
+
Math.min(64 * 1024, reader.size - offset)
|
|
530
|
+
);
|
|
531
|
+
offset += chunk.byteLength;
|
|
532
|
+
yield chunk;
|
|
533
|
+
}
|
|
534
|
+
} finally {
|
|
535
|
+
await reader.close();
|
|
536
|
+
}
|
|
537
|
+
})()
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
function getBrandedNodeResourcePath(resource, kind) {
|
|
541
|
+
if (typeof resource !== "object" || resource === null) return void 0;
|
|
542
|
+
const candidate = resource;
|
|
543
|
+
return candidate[NODE_RESOURCE_KIND] === kind && typeof candidate.path === "string" ? candidate.path : void 0;
|
|
544
|
+
}
|
|
545
|
+
function createNodeRangeReadStream(entry) {
|
|
546
|
+
return Readable.from(
|
|
547
|
+
(async function* () {
|
|
548
|
+
for (let offset = 0; offset < entry.size; ) {
|
|
549
|
+
const chunk = await entry.read(
|
|
550
|
+
offset,
|
|
551
|
+
Math.min(64 * 1024, entry.size - offset)
|
|
552
|
+
);
|
|
553
|
+
offset += chunk.byteLength;
|
|
554
|
+
yield chunk;
|
|
555
|
+
}
|
|
556
|
+
})()
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
function getNodeZipEntryIterator(entries) {
|
|
560
|
+
const asyncIterator = entries[Symbol.asyncIterator];
|
|
561
|
+
return asyncIterator === void 0 ? entries[Symbol.iterator]() : asyncIterator.call(entries);
|
|
562
|
+
}
|
|
563
|
+
function closeNodeZipEntryIterator(iterator) {
|
|
564
|
+
if (iterator.return === void 0) return;
|
|
565
|
+
try {
|
|
566
|
+
void Promise.resolve(iterator.return()).catch(() => void 0);
|
|
567
|
+
} catch {
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
async function writeNodeStream(input, writer) {
|
|
571
|
+
await pipeline(
|
|
572
|
+
input,
|
|
573
|
+
new Writable({
|
|
574
|
+
write: (chunk, _encoding, callback) => {
|
|
575
|
+
void writer.write(chunk).then(
|
|
576
|
+
() => callback(),
|
|
577
|
+
(error) => callback(
|
|
578
|
+
error instanceof Error ? error : new Error("Cannot stream ZIP archive")
|
|
579
|
+
)
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
})
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
async function collectNodeStream(input) {
|
|
586
|
+
return await new Promise((resolve3, reject) => {
|
|
587
|
+
const chunks = [];
|
|
588
|
+
input.on("data", (chunk) => chunks.push(chunk));
|
|
589
|
+
input.once("error", reject);
|
|
590
|
+
input.once("end", () => resolve3(Buffer.concat(chunks)));
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
var nodeWikiGraphPlatform = {
|
|
594
|
+
asyncContext: {
|
|
595
|
+
create: () => new asyncHooks.AsyncLocalStorage()
|
|
596
|
+
},
|
|
597
|
+
database: { open: openNodeDatabase },
|
|
598
|
+
lifecycle: {
|
|
599
|
+
instanceId: `node-process:${nodeProcess.pid}`,
|
|
600
|
+
isInstanceAlive: (instanceId) => {
|
|
601
|
+
const match = /^node-process:(\d+)$/u.exec(instanceId);
|
|
602
|
+
if (match === null) return Promise.resolve(void 0);
|
|
603
|
+
try {
|
|
604
|
+
nodeProcess.kill(Number(match[1]), 0);
|
|
605
|
+
return Promise.resolve(true);
|
|
606
|
+
} catch (error) {
|
|
607
|
+
if (typeof error === "object" && error !== null && "code" in error && error.code === "ESRCH") {
|
|
608
|
+
return Promise.resolve(false);
|
|
609
|
+
}
|
|
610
|
+
return Promise.resolve(true);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
},
|
|
614
|
+
resources: {
|
|
615
|
+
getDirectory: (identity) => {
|
|
616
|
+
const path = decodeNodeResourceIdentity(identity, "directory");
|
|
617
|
+
return Promise.resolve(
|
|
618
|
+
path === void 0 ? void 0 : new NodeDirectory(path)
|
|
619
|
+
);
|
|
620
|
+
},
|
|
621
|
+
getFile: (identity) => {
|
|
622
|
+
const path = decodeNodeResourceIdentity(identity, "file");
|
|
623
|
+
return Promise.resolve(
|
|
624
|
+
path === void 0 ? void 0 : new NodeFile(path)
|
|
625
|
+
);
|
|
626
|
+
},
|
|
627
|
+
resolveLegacyDirectory: (reference) => {
|
|
628
|
+
const path = decodeNodeResourceIdentity(reference, "directory");
|
|
629
|
+
return Promise.resolve(
|
|
630
|
+
path === void 0 ? void 0 : new NodeDirectory(path)
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
},
|
|
634
|
+
templates: {
|
|
635
|
+
createEnvironment: (options) => new Environment(new NodeTemplateLoader(), options)
|
|
636
|
+
},
|
|
637
|
+
zip: {
|
|
638
|
+
open: openNodeZip,
|
|
639
|
+
write: writeNodeZip
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
var NodeTemplateLoader = class extends Loader {
|
|
643
|
+
#dataDirectory;
|
|
644
|
+
constructor(dataDirectory) {
|
|
645
|
+
super();
|
|
646
|
+
this.#dataDirectory = dataDirectory;
|
|
647
|
+
}
|
|
648
|
+
getSource(templateName) {
|
|
649
|
+
const name = normalizeTemplateName(templateName);
|
|
650
|
+
const filePath = pathModule.join(
|
|
651
|
+
this.#dataDirectory ?? resolveNodeDataDirectory(),
|
|
652
|
+
name
|
|
653
|
+
);
|
|
654
|
+
return {
|
|
655
|
+
noCache: false,
|
|
656
|
+
path: name,
|
|
657
|
+
src: fs.readFileSync(filePath, "utf8")
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
function resolveNodeDataDirectory() {
|
|
662
|
+
const injected = globalThis.__WIKIGRAPH_DATA_DIR__;
|
|
663
|
+
if (typeof injected === "string" && injected !== "") return injected;
|
|
664
|
+
let directory = nodeProcess.cwd();
|
|
665
|
+
while (true) {
|
|
666
|
+
for (const candidate of [
|
|
667
|
+
pathModule.join(directory, "data"),
|
|
668
|
+
pathModule.join(directory, "packages", "core", "data")
|
|
669
|
+
]) {
|
|
670
|
+
try {
|
|
671
|
+
if (fs.statSync(candidate).isDirectory()) return candidate;
|
|
672
|
+
} catch {
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
const parent = pathModule.dirname(directory);
|
|
676
|
+
if (parent === directory)
|
|
677
|
+
throw new Error("Could not locate data directory");
|
|
678
|
+
directory = parent;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// src/runtime-path.ts
|
|
683
|
+
import { homedir as homedir2 } from "os";
|
|
684
|
+
import { isAbsolute as isAbsolute2, resolve as resolve2 } from "path";
|
|
685
|
+
|
|
686
|
+
// src/runtime-context.ts
|
|
687
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
688
|
+
var RUNTIME_CONTEXT_KEY = /* @__PURE__ */ Symbol.for("wiki-graph-sdk.runtime-context.v1");
|
|
689
|
+
var runtimeContext = getRuntimeContextStorage();
|
|
690
|
+
function getRuntimeContextStorage() {
|
|
691
|
+
const runtimeGlobal = globalThis;
|
|
692
|
+
const existing = runtimeGlobal[RUNTIME_CONTEXT_KEY];
|
|
693
|
+
if (existing !== void 0) {
|
|
694
|
+
return existing;
|
|
695
|
+
}
|
|
696
|
+
const storage = new AsyncLocalStorage2();
|
|
697
|
+
Object.defineProperty(runtimeGlobal, RUNTIME_CONTEXT_KEY, {
|
|
698
|
+
configurable: false,
|
|
699
|
+
enumerable: false,
|
|
700
|
+
value: storage,
|
|
701
|
+
writable: false
|
|
702
|
+
});
|
|
703
|
+
return storage;
|
|
704
|
+
}
|
|
705
|
+
function getWikiGraphSDKRuntimeContext() {
|
|
706
|
+
return runtimeContext.getStore() ?? {
|
|
707
|
+
cwd: process.cwd(),
|
|
708
|
+
env: process.env,
|
|
709
|
+
envPolicy: "production"
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// src/runtime-path.ts
|
|
714
|
+
function resolveWikiGraphRuntimePath(path) {
|
|
715
|
+
const context = getWikiGraphSDKRuntimeContext();
|
|
716
|
+
const environmentHome = context.env.HOME?.trim();
|
|
717
|
+
const home = environmentHome === void 0 || environmentHome === "" ? homedir2() : environmentHome;
|
|
718
|
+
if (path === "~") return home;
|
|
719
|
+
if (path.startsWith("~/")) return resolve2(home, path.slice(2));
|
|
720
|
+
return isAbsolute2(path) ? path : resolve2(context.cwd, path);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// src/maintenance.ts
|
|
724
|
+
var WikiGraphMaintenanceManager = class {
|
|
725
|
+
#runtime;
|
|
726
|
+
constructor(runtime) {
|
|
727
|
+
this.#runtime = runtime;
|
|
728
|
+
}
|
|
729
|
+
async upgrade(target) {
|
|
730
|
+
return await this.#runtime.run(
|
|
731
|
+
async () => {
|
|
732
|
+
if (target.kind === "home") {
|
|
733
|
+
return await upgradeWikiGraphMaintenanceTarget({ kind: "home" });
|
|
734
|
+
}
|
|
735
|
+
if (target.kind === "library") {
|
|
736
|
+
const parsed = parseWikiGraphLibraryUri(target.uri);
|
|
737
|
+
if (parsed === void 0 || parsed.kind === "archive") {
|
|
738
|
+
throw new Error(
|
|
739
|
+
`Invalid Wiki Graph library upgrade target: ${target.uri}`
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
return await upgradeWikiGraphMaintenanceTarget({
|
|
743
|
+
kind: "library",
|
|
744
|
+
target: parsed
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
const path = resolveWikiGraphRuntimePath(target.path);
|
|
748
|
+
return await upgradeWikiGraphMaintenanceTarget({
|
|
749
|
+
file: new NodeFile(path),
|
|
750
|
+
kind: "archive"
|
|
751
|
+
});
|
|
752
|
+
},
|
|
753
|
+
void 0,
|
|
754
|
+
{ skipHomeBootstrap: target.kind === "home" }
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
async migrateLegacy(inputPath, outputPath = defaultWikgOutputPath(inputPath)) {
|
|
758
|
+
return await this.#runtime.run(async () => {
|
|
759
|
+
const resolvedInputPath = resolveWikiGraphRuntimePath(inputPath);
|
|
760
|
+
const resolvedOutputPath = resolveWikiGraphRuntimePath(outputPath);
|
|
761
|
+
await migrateLegacySdpubToWikg(
|
|
762
|
+
new NodeFile(resolvedInputPath),
|
|
763
|
+
new NodeFile(resolvedOutputPath, basename2(resolvedOutputPath))
|
|
764
|
+
);
|
|
765
|
+
return {
|
|
766
|
+
inputPath: resolvedInputPath,
|
|
767
|
+
outputPath: resolvedOutputPath
|
|
768
|
+
};
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
function defaultWikgOutputPath(inputPath) {
|
|
773
|
+
return inputPath.toLowerCase().endsWith(".sdpub") ? `${inputPath.slice(0, -".sdpub".length)}.wikg` : `${inputPath}.wikg`;
|
|
774
|
+
}
|
|
775
|
+
export {
|
|
776
|
+
WikiGraphMaintenanceManager
|
|
777
|
+
};
|
|
778
|
+
//# sourceMappingURL=maintenance.js.map
|