@tiqian/precompute 0.0.0-stage → 0.1.0-alpha.10
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 +373 -0
- package/README.md +210 -2
- package/lib/cache.d.ts +58 -0
- package/lib/cache.js +196 -0
- package/lib/canonical.d.ts +26 -0
- package/lib/canonical.js +82 -0
- package/lib/fonts.d.ts +177 -0
- package/lib/fonts.js +89 -0
- package/lib/load.d.ts +138 -0
- package/lib/load.js +38 -0
- package/lib/nixos.d.ts +26 -0
- package/lib/nixos.js +315 -0
- package/lib/persistence.d.ts +127 -0
- package/lib/persistence.js +294 -0
- package/lib/precompute-html-projection.d.ts +32 -0
- package/lib/precompute-html-projection.js +140 -0
- package/lib/precompute-html.d.ts +98 -0
- package/lib/precompute-html.js +324 -0
- package/lib/precompute.d.ts +323 -0
- package/lib/precompute.js +539 -0
- package/lib/sqlite-store.d.ts +57 -0
- package/lib/sqlite-store.js +365 -0
- package/lib/stable-stringify.d.ts +8 -0
- package/lib/stable-stringify.js +40 -0
- package/lib/transport.d.ts +74 -0
- package/lib/transport.js +165 -0
- package/lib/vite.d.ts +6 -0
- package/lib/vite.js +22 -0
- package/package.json +115 -4
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// The SQLite reference store (ADR 0052): one ready-made PersistentCacheStore
|
|
2
|
+
// over the host runtime's built-in SQLite. node:sqlite is probed first, then
|
|
3
|
+
// bun:sqlite; runtimes without either (including Deno, which ships no
|
|
4
|
+
// built-in sqlite module) reject the factory by name and their hosts bring
|
|
5
|
+
// their own store. Entry bytes stay opaque rows addressed by the SDK's
|
|
6
|
+
// address strings; write batches land in one transaction. The Article index
|
|
7
|
+
// (articles and their content-hash sets) lives in two tables of its own, so
|
|
8
|
+
// an entry prune never touches index rows.
|
|
9
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
10
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
11
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
12
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return path;
|
|
16
|
+
};
|
|
17
|
+
import { mkdirSync } from "node:fs";
|
|
18
|
+
import { dirname } from "node:path";
|
|
19
|
+
async function openDatabase(path) {
|
|
20
|
+
try {
|
|
21
|
+
const mod = await import("node:sqlite");
|
|
22
|
+
return new mod.DatabaseSync(path);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// Fall through to the next runtime's module.
|
|
26
|
+
}
|
|
27
|
+
// The bun specifier rides in a variable so TypeScript does not resolve it
|
|
28
|
+
// as a module this package cannot type; the probed module is structural.
|
|
29
|
+
const bunSpecifier = "bun:sqlite";
|
|
30
|
+
try {
|
|
31
|
+
const mod = await import(__rewriteRelativeImportExtension(bunSpecifier));
|
|
32
|
+
// bun names the constructor Database; node names it DatabaseSync.
|
|
33
|
+
const DatabaseConstructor = mod.Database ?? mod.DatabaseSync;
|
|
34
|
+
return new DatabaseConstructor(path);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// No probed module loaded.
|
|
38
|
+
}
|
|
39
|
+
throw new Error("SqliteCacheStoreUnavailable");
|
|
40
|
+
}
|
|
41
|
+
const isBytes = (value) => value instanceof Uint8Array;
|
|
42
|
+
// The file's structure version, kept in SQLite's user_version. Content
|
|
43
|
+
// versioning does not live here: record bytes are opaque and invalidate
|
|
44
|
+
// through the context fingerprint. A structure mismatch refuses by name;
|
|
45
|
+
// the file is a cache, so the host may delete it and rebuild.
|
|
46
|
+
const SCHEMA_VERSION = 2;
|
|
47
|
+
const CREATE_ENTRIES = "CREATE TABLE IF NOT EXISTS tiqian_cache_entries (" +
|
|
48
|
+
"address TEXT PRIMARY KEY, bytes BLOB NOT NULL);";
|
|
49
|
+
// Structure 2 adds the Article index tables (ADR 0052): articles tag keys
|
|
50
|
+
// to buckets, article hashes hold each article's full content-hash set.
|
|
51
|
+
// Contexts stay in their own columns because one file serves every lane.
|
|
52
|
+
const CREATE_ARTICLES = "CREATE TABLE IF NOT EXISTS tiqian_cache_articles (" +
|
|
53
|
+
"context TEXT NOT NULL, article_key TEXT NOT NULL, bucket TEXT NOT NULL, " +
|
|
54
|
+
"PRIMARY KEY (context, article_key));";
|
|
55
|
+
const CREATE_ARTICLE_HASHES = "CREATE TABLE IF NOT EXISTS tiqian_cache_article_hashes (" +
|
|
56
|
+
"context TEXT NOT NULL, article_key TEXT NOT NULL, content_hash TEXT NOT NULL, " +
|
|
57
|
+
"PRIMARY KEY (context, article_key, content_hash));";
|
|
58
|
+
function migrate(db) {
|
|
59
|
+
const row = db.prepare("PRAGMA user_version;").all()[0];
|
|
60
|
+
const version = row.user_version;
|
|
61
|
+
const current = typeof version === "number" ? version : 0;
|
|
62
|
+
if (current === SCHEMA_VERSION) {
|
|
63
|
+
// Idempotent: a versioned file from this store keeps its rows.
|
|
64
|
+
db.exec(CREATE_ENTRIES + CREATE_ARTICLES + CREATE_ARTICLE_HASHES);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (current === 0) {
|
|
68
|
+
// A fresh file (or one written before versioning): create and stamp.
|
|
69
|
+
db.exec(CREATE_ENTRIES + CREATE_ARTICLES + CREATE_ARTICLE_HASHES);
|
|
70
|
+
db.exec(`PRAGMA user_version = ${SCHEMA_VERSION};`);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (current === 1) {
|
|
74
|
+
// Structure 2 only adds the article tables; entry rows survive in
|
|
75
|
+
// place, so the migration is a pair of creates away from version 1.
|
|
76
|
+
db.exec(CREATE_ARTICLES + CREATE_ARTICLE_HASHES);
|
|
77
|
+
db.exec(`PRAGMA user_version = ${SCHEMA_VERSION};`);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
// A future version means a newer host wrote this file; an unknown past
|
|
81
|
+
// version has no migration yet. Neither is read on guesswork.
|
|
82
|
+
throw new Error(current > SCHEMA_VERSION
|
|
83
|
+
? "SqliteCacheStoreSchemaNewer"
|
|
84
|
+
: "SqliteCacheStoreSchemaUnknown");
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Opens (or creates) the database at `path`. The parent directory is created
|
|
88
|
+
* when missing; the file belongs to the caller's cache directory policy.
|
|
89
|
+
*
|
|
90
|
+
* `options.contexts` declares the full set of contexts this file serves: at
|
|
91
|
+
* open, rows of every other context drop (entries, article rows and hash
|
|
92
|
+
* rows) and the file rebuilds around the survivors. Omit it when the file
|
|
93
|
+
* serves contexts this caller cannot list. The rebuild also runs without a
|
|
94
|
+
* declaration whenever earlier deletes left free pages inside the file.
|
|
95
|
+
*/
|
|
96
|
+
export async function createSqliteCacheStore(path, options) {
|
|
97
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
98
|
+
const db = await openDatabase(path);
|
|
99
|
+
// A failed open must release the handle: an open sqlite file blocks
|
|
100
|
+
// deleting the cache directory on Windows.
|
|
101
|
+
try {
|
|
102
|
+
db.exec("PRAGMA journal_mode = WAL;");
|
|
103
|
+
// Hosts that prerender with worker processes open several connections to
|
|
104
|
+
// one file; a busy timeout turns their write contention into waiting
|
|
105
|
+
// instead of a failed build.
|
|
106
|
+
db.exec("PRAGMA busy_timeout = 5000;");
|
|
107
|
+
migrate(db);
|
|
108
|
+
db.exec("CREATE TEMP TABLE IF NOT EXISTS keep_list (address TEXT PRIMARY KEY);");
|
|
109
|
+
db.exec("CREATE TEMP TABLE IF NOT EXISTS keep_hashes (content_hash TEXT PRIMARY KEY);");
|
|
110
|
+
db.exec("CREATE TEMP TABLE IF NOT EXISTS keep_contexts (context TEXT PRIMARY KEY);");
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
db.close();
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
const readStatement = db.prepare("SELECT bytes FROM tiqian_cache_entries WHERE address = ?;");
|
|
117
|
+
const writeStatement = db.prepare("INSERT OR REPLACE INTO tiqian_cache_entries (address, bytes) VALUES (?, ?);");
|
|
118
|
+
const clearKeepStatement = db.prepare("DELETE FROM keep_list;");
|
|
119
|
+
const keepStatement = db.prepare("INSERT OR REPLACE INTO keep_list (address) VALUES (?);");
|
|
120
|
+
const clearKeepHashesStatement = db.prepare("DELETE FROM keep_hashes;");
|
|
121
|
+
const keepHashStatement = db.prepare("INSERT OR REPLACE INTO keep_hashes (content_hash) VALUES (?);");
|
|
122
|
+
const pruneStatement = db.prepare("DELETE FROM tiqian_cache_entries WHERE address LIKE ? AND address NOT IN " +
|
|
123
|
+
"(SELECT address FROM keep_list);");
|
|
124
|
+
// Bucket-scoped eviction in one pass over the index: the entry must sit
|
|
125
|
+
// under this bucket's rows (scope), miss the keep list and miss every
|
|
126
|
+
// other bucket's rows (cross-bucket shares survive). The row trim and the
|
|
127
|
+
// emptied-article sweep run after the entry delete, inside the same
|
|
128
|
+
// transaction, so the scope stays readable while it is computed from.
|
|
129
|
+
const pruneBucketEntriesStatement = db.prepare("DELETE FROM tiqian_cache_entries WHERE address LIKE ? AND address IN " +
|
|
130
|
+
"(SELECT ? || ':' || h.content_hash FROM tiqian_cache_article_hashes h " +
|
|
131
|
+
"JOIN tiqian_cache_articles a ON a.context = h.context " +
|
|
132
|
+
"AND a.article_key = h.article_key WHERE h.context = ? AND a.bucket = ?) " +
|
|
133
|
+
"AND address NOT IN (SELECT address FROM keep_list) AND address NOT IN " +
|
|
134
|
+
"(SELECT ? || ':' || h.content_hash FROM tiqian_cache_article_hashes h " +
|
|
135
|
+
"JOIN tiqian_cache_articles a ON a.context = h.context " +
|
|
136
|
+
"AND a.article_key = h.article_key WHERE h.context = ? AND a.bucket <> ?);");
|
|
137
|
+
const trimBucketHashesStatement = db.prepare("DELETE FROM tiqian_cache_article_hashes WHERE context = ? AND " +
|
|
138
|
+
"content_hash NOT IN (SELECT content_hash FROM keep_hashes) AND " +
|
|
139
|
+
"article_key IN (SELECT article_key FROM tiqian_cache_articles " +
|
|
140
|
+
"WHERE context = ? AND bucket = ?);");
|
|
141
|
+
const dropEmptiedArticlesStatement = db.prepare("DELETE FROM tiqian_cache_articles WHERE context = ? AND bucket = ? AND " +
|
|
142
|
+
"article_key NOT IN (SELECT article_key FROM tiqian_cache_article_hashes " +
|
|
143
|
+
"WHERE context = ?);");
|
|
144
|
+
// Superseded-context drop: an entry address is `context:hash`, so the
|
|
145
|
+
// context falls out of the address prefix; the article tables key by their
|
|
146
|
+
// own context column. An address without its colon parses to a context no
|
|
147
|
+
// keep list carries, so malformed rows go with the drop.
|
|
148
|
+
const clearKeepContextsStatement = db.prepare("DELETE FROM keep_contexts;");
|
|
149
|
+
const keepContextStatement = db.prepare("INSERT OR REPLACE INTO keep_contexts (context) VALUES (?);");
|
|
150
|
+
const dropForeignEntriesStatement = db.prepare("DELETE FROM tiqian_cache_entries WHERE substr(address, 1, " +
|
|
151
|
+
"instr(address, ':') - 1) NOT IN (SELECT context FROM keep_contexts);");
|
|
152
|
+
const dropForeignArticlesStatement = db.prepare("DELETE FROM tiqian_cache_articles WHERE context NOT IN " +
|
|
153
|
+
"(SELECT context FROM keep_contexts);");
|
|
154
|
+
const dropForeignHashesStatement = db.prepare("DELETE FROM tiqian_cache_article_hashes WHERE context NOT IN " +
|
|
155
|
+
"(SELECT context FROM keep_contexts);");
|
|
156
|
+
const freelistStatement = db.prepare("PRAGMA freelist_count;");
|
|
157
|
+
const deleteArticleStatement = db.prepare("DELETE FROM tiqian_cache_articles WHERE context = ? AND article_key = ?;");
|
|
158
|
+
const deleteArticleHashesStatement = db.prepare("DELETE FROM tiqian_cache_article_hashes WHERE context = ? AND article_key = ?;");
|
|
159
|
+
const insertArticleStatement = db.prepare("INSERT INTO tiqian_cache_articles (context, article_key, bucket) VALUES (?, ?, ?);");
|
|
160
|
+
const insertArticleHashStatement = db.prepare("INSERT INTO tiqian_cache_article_hashes (context, article_key, content_hash) " +
|
|
161
|
+
"VALUES (?, ?, ?);");
|
|
162
|
+
const readArticlesStatement = db.prepare("SELECT article_key, bucket FROM tiqian_cache_articles WHERE context = ? " +
|
|
163
|
+
"ORDER BY article_key;");
|
|
164
|
+
const readArticleHashesStatement = db.prepare("SELECT article_key, content_hash FROM tiqian_cache_article_hashes " +
|
|
165
|
+
"WHERE context = ? ORDER BY article_key, content_hash;");
|
|
166
|
+
let closed = false;
|
|
167
|
+
const guard = () => {
|
|
168
|
+
if (closed) {
|
|
169
|
+
throw new Error("SqliteCacheStoreClosed");
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
const runDropOtherContexts = (contexts) => {
|
|
173
|
+
// An empty keep list would drop the whole file; a host without lanes
|
|
174
|
+
// keeps its rows instead of losing them to a vacuous call.
|
|
175
|
+
if (contexts.length === 0)
|
|
176
|
+
return 0;
|
|
177
|
+
db.exec("BEGIN IMMEDIATE;");
|
|
178
|
+
try {
|
|
179
|
+
clearKeepContextsStatement.run();
|
|
180
|
+
for (const context of contexts) {
|
|
181
|
+
keepContextStatement.run(context);
|
|
182
|
+
}
|
|
183
|
+
const entriesRemoved = Number(dropForeignEntriesStatement.run().changes);
|
|
184
|
+
dropForeignArticlesStatement.run();
|
|
185
|
+
dropForeignHashesStatement.run();
|
|
186
|
+
db.exec("COMMIT;");
|
|
187
|
+
return entriesRemoved;
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
db.exec("ROLLBACK;");
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
const runCompact = () => {
|
|
195
|
+
const row = freelistStatement.all()[0];
|
|
196
|
+
const freelist = row.freelist_count;
|
|
197
|
+
if (typeof freelist !== "number" || freelist === 0)
|
|
198
|
+
return;
|
|
199
|
+
// Deleted rows sit on the file's free list until a rebuild moves the
|
|
200
|
+
// remaining rows into a fresh file. A concurrent connection can hold
|
|
201
|
+
// the lock the rebuild needs; the rows are already durable, so a
|
|
202
|
+
// failed rebuild waits for the next open instead of failing the host.
|
|
203
|
+
try {
|
|
204
|
+
db.exec("VACUUM;");
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
// The free list keeps the space; the next compact reclaims it.
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
// Init maintenance: declared contexts drop out whole, then one rebuild
|
|
211
|
+
// covers both the drop and whatever earlier deletes left behind.
|
|
212
|
+
if (options?.contexts !== undefined) {
|
|
213
|
+
runDropOtherContexts(options.contexts);
|
|
214
|
+
}
|
|
215
|
+
runCompact();
|
|
216
|
+
return {
|
|
217
|
+
read(addresses) {
|
|
218
|
+
guard();
|
|
219
|
+
const found = new Map();
|
|
220
|
+
for (const address of addresses) {
|
|
221
|
+
const rows = readStatement.all(address);
|
|
222
|
+
const row = rows[0];
|
|
223
|
+
if (row === undefined)
|
|
224
|
+
continue;
|
|
225
|
+
// One row per primary key; a row without bytes cannot occur through
|
|
226
|
+
// this store's writes, and an external edit is skipped rather than
|
|
227
|
+
// trusted.
|
|
228
|
+
const value = row.bytes;
|
|
229
|
+
if (isBytes(value)) {
|
|
230
|
+
found.set(address, value);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return found;
|
|
234
|
+
},
|
|
235
|
+
write(entries) {
|
|
236
|
+
guard();
|
|
237
|
+
db.exec("BEGIN IMMEDIATE;");
|
|
238
|
+
try {
|
|
239
|
+
for (const [address, blob] of entries) {
|
|
240
|
+
writeStatement.run(address, blob);
|
|
241
|
+
}
|
|
242
|
+
db.exec("COMMIT;");
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
db.exec("ROLLBACK;");
|
|
246
|
+
throw error;
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
prune(context, keep) {
|
|
250
|
+
guard();
|
|
251
|
+
db.exec("BEGIN IMMEDIATE;");
|
|
252
|
+
try {
|
|
253
|
+
clearKeepStatement.run();
|
|
254
|
+
for (const address of keep) {
|
|
255
|
+
keepStatement.run(address);
|
|
256
|
+
}
|
|
257
|
+
// Context fingerprints are hex, so the prefix match carries no LIKE
|
|
258
|
+
// wildcards of its own.
|
|
259
|
+
const outcome = pruneStatement.run(`${context}:%`);
|
|
260
|
+
db.exec("COMMIT;");
|
|
261
|
+
return Number(outcome.changes);
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
db.exec("ROLLBACK;");
|
|
265
|
+
throw error;
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
pruneBucket(context, bucket, keep) {
|
|
269
|
+
guard();
|
|
270
|
+
db.exec("BEGIN IMMEDIATE;");
|
|
271
|
+
try {
|
|
272
|
+
clearKeepStatement.run();
|
|
273
|
+
clearKeepHashesStatement.run();
|
|
274
|
+
for (const contentHash of keep) {
|
|
275
|
+
keepStatement.run(`${context}:${contentHash}`);
|
|
276
|
+
keepHashStatement.run(contentHash);
|
|
277
|
+
}
|
|
278
|
+
const outcome = pruneBucketEntriesStatement.run(`${context}:%`, context, context, bucket, context, context, bucket);
|
|
279
|
+
trimBucketHashesStatement.run(context, context, bucket);
|
|
280
|
+
dropEmptiedArticlesStatement.run(context, bucket, context);
|
|
281
|
+
db.exec("COMMIT;");
|
|
282
|
+
return Number(outcome.changes);
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
db.exec("ROLLBACK;");
|
|
286
|
+
throw error;
|
|
287
|
+
}
|
|
288
|
+
},
|
|
289
|
+
dropOtherContexts(contexts) {
|
|
290
|
+
guard();
|
|
291
|
+
return runDropOtherContexts(contexts);
|
|
292
|
+
},
|
|
293
|
+
compact() {
|
|
294
|
+
guard();
|
|
295
|
+
runCompact();
|
|
296
|
+
},
|
|
297
|
+
close() {
|
|
298
|
+
guard();
|
|
299
|
+
closed = true;
|
|
300
|
+
db.close();
|
|
301
|
+
},
|
|
302
|
+
recordArticles(context, articles) {
|
|
303
|
+
guard();
|
|
304
|
+
db.exec("BEGIN IMMEDIATE;");
|
|
305
|
+
try {
|
|
306
|
+
for (const article of articles) {
|
|
307
|
+
// The replacement drops the article's previous hashes first, so a
|
|
308
|
+
// shrunken article does not keep pins on entries it no longer
|
|
309
|
+
// references.
|
|
310
|
+
deleteArticleStatement.run(context, article.articleKey);
|
|
311
|
+
deleteArticleHashesStatement.run(context, article.articleKey);
|
|
312
|
+
insertArticleStatement.run(context, article.articleKey, article.bucket);
|
|
313
|
+
for (const contentHash of article.contentHashes) {
|
|
314
|
+
insertArticleHashStatement.run(context, article.articleKey, contentHash);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
db.exec("COMMIT;");
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
db.exec("ROLLBACK;");
|
|
321
|
+
throw error;
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
readArticles(context) {
|
|
325
|
+
guard();
|
|
326
|
+
const hashes = new Map();
|
|
327
|
+
for (const row of readArticleHashesStatement.all(context)) {
|
|
328
|
+
const { article_key: articleKey, content_hash: contentHash } = row;
|
|
329
|
+
const list = hashes.get(articleKey);
|
|
330
|
+
if (list === undefined) {
|
|
331
|
+
hashes.set(articleKey, [contentHash]);
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
list.push(contentHash);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return readArticlesStatement.all(context).map((row) => {
|
|
338
|
+
const { article_key: articleKey, bucket } = row;
|
|
339
|
+
return {
|
|
340
|
+
articleKey,
|
|
341
|
+
bucket,
|
|
342
|
+
contentHashes: hashes.get(articleKey) ?? [],
|
|
343
|
+
};
|
|
344
|
+
});
|
|
345
|
+
},
|
|
346
|
+
deleteArticles(context, articleKeys) {
|
|
347
|
+
guard();
|
|
348
|
+
db.exec("BEGIN IMMEDIATE;");
|
|
349
|
+
try {
|
|
350
|
+
let removed = 0;
|
|
351
|
+
for (const articleKey of articleKeys) {
|
|
352
|
+
deleteArticleHashesStatement.run(context, articleKey);
|
|
353
|
+
const outcome = deleteArticleStatement.run(context, articleKey);
|
|
354
|
+
removed += Number(outcome.changes);
|
|
355
|
+
}
|
|
356
|
+
db.exec("COMMIT;");
|
|
357
|
+
return removed;
|
|
358
|
+
}
|
|
359
|
+
catch (error) {
|
|
360
|
+
db.exec("ROLLBACK;");
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders one JSON value the way the Rust `stable_stringify` does. Inputs are
|
|
3
|
+
* snapshot artifacts: JSON values only, so `undefined` has no Rust
|
|
4
|
+
* counterpart and renders as `null`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function stableStringify(value: unknown): string;
|
|
7
|
+
/** Hex SHA-256 of the UTF-8 bytes of one string, the Rust `sha256_hex` form. */
|
|
8
|
+
export declare function sha256Hex(value: string): string;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// The TypeScript counterpart of the Rust `stable_stringify` that feeds every
|
|
2
|
+
// snapshot artifact hash (precompute/engine/src/schema.rs:52-80). Both sides
|
|
3
|
+
// must produce the same bytes for the same artifact tree: primitives render
|
|
4
|
+
// through JSON.stringify, arrays keep element order, and object keys sort by
|
|
5
|
+
// UTF-16 code units (Array.prototype.sort's default order). The digest uses
|
|
6
|
+
// the generated haxe.crypto.Sha256 entry, so Node, the browser, and Rust share
|
|
7
|
+
// one hash implementation.
|
|
8
|
+
//
|
|
9
|
+
// The cross-gold test test/stable-stringify-cross-gold.test.ts pins one
|
|
10
|
+
// artifact sha produced by the Rust renderer against this composition.
|
|
11
|
+
import { Sha256 } from "@tiqian/protocol/haxe/crypto/Sha256";
|
|
12
|
+
const textEncoder = new TextEncoder();
|
|
13
|
+
/**
|
|
14
|
+
* Renders one JSON value the way the Rust `stable_stringify` does. Inputs are
|
|
15
|
+
* snapshot artifacts: JSON values only, so `undefined` has no Rust
|
|
16
|
+
* counterpart and renders as `null`.
|
|
17
|
+
*/
|
|
18
|
+
export function stableStringify(value) {
|
|
19
|
+
const encoded = stableEncode(value);
|
|
20
|
+
return encoded === undefined ? "null" : encoded;
|
|
21
|
+
}
|
|
22
|
+
function stableEncode(value) {
|
|
23
|
+
if (value === null || typeof value !== "object")
|
|
24
|
+
return JSON.stringify(value);
|
|
25
|
+
if (Array.isArray(value))
|
|
26
|
+
return `[${value.map(stableStringify).join(",")}]`;
|
|
27
|
+
const record = value;
|
|
28
|
+
const entries = Object.keys(record)
|
|
29
|
+
.sort()
|
|
30
|
+
.map((key) => `${JSON.stringify(key)}:${stableStringify(record[key])}`);
|
|
31
|
+
return `{${entries.join(",")}}`;
|
|
32
|
+
}
|
|
33
|
+
/** Hex SHA-256 of the UTF-8 bytes of one string, the Rust `sha256_hex` form. */
|
|
34
|
+
export function sha256Hex(value) {
|
|
35
|
+
const digest = Sha256.make(textEncoder.encode(value));
|
|
36
|
+
let hex = "";
|
|
37
|
+
for (const byte of digest)
|
|
38
|
+
hex += byte.toString(16).padStart(2, "0");
|
|
39
|
+
return hex;
|
|
40
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type FinalizedSnapshotTables, type PreparedEntry, type SnapshotBundle, type SnapshotBundleData } from "./precompute.js";
|
|
2
|
+
export interface SnapshotTableFileTransportOptions {
|
|
3
|
+
/** The directory the table files live in; created on first write. */
|
|
4
|
+
readonly directory: string;
|
|
5
|
+
/** The URL path prefix the host serves the directory under. */
|
|
6
|
+
readonly urlPrefix?: string;
|
|
7
|
+
/** The file and URL extension; the bytes are the TIQTBL03 binary form. */
|
|
8
|
+
readonly extension?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface SnapshotTableFileTransport {
|
|
11
|
+
readonly directory: string;
|
|
12
|
+
urlFor(sha256: string): string;
|
|
13
|
+
/** The pattern build outputs are scanned with to find referenced tables. */
|
|
14
|
+
urlPattern(): RegExp;
|
|
15
|
+
/** Writes one frozen table; returns the URL the manifest should pin. */
|
|
16
|
+
write(finalized: FinalizedSnapshotTables): string;
|
|
17
|
+
read(sha256: string): Buffer | undefined;
|
|
18
|
+
listShas(): string[];
|
|
19
|
+
/** Deletes files no kept sha pins; returns the deleted file count. */
|
|
20
|
+
sweep(keepShas: ReadonlySet<string>): number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Serves one directory of frozen table files under `/<urlPrefix>/<sha><extension>`.
|
|
24
|
+
* A table name addresses immutable bytes, so responses cache forever and a
|
|
25
|
+
* name never serves two contents over the file's lifetime.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createSnapshotTableFileTransport(options: SnapshotTableFileTransportOptions): SnapshotTableFileTransport;
|
|
28
|
+
/** The paragraphs one bundle renders; both arrays absorb into one table. */
|
|
29
|
+
export interface SnapshotBundlePlan {
|
|
30
|
+
readonly paragraphs: readonly PreparedEntry[];
|
|
31
|
+
readonly fontContractParagraphs: readonly PreparedEntry[];
|
|
32
|
+
}
|
|
33
|
+
/** The data phase of one bundle, held until the table froze. */
|
|
34
|
+
export interface RenderedSnapshotData {
|
|
35
|
+
readonly data: SnapshotBundleData;
|
|
36
|
+
readonly fontContractOnly: boolean;
|
|
37
|
+
}
|
|
38
|
+
export interface SnapshotTableSession {
|
|
39
|
+
/**
|
|
40
|
+
* Absorbs one batch of prepared entries into the shared table. Absorb and
|
|
41
|
+
* data-phase calls share one mutable table until `finish`; both throw
|
|
42
|
+
* afterwards. Font-contract corpora never intern through their data phase,
|
|
43
|
+
* so both arrays of a plan absorb here.
|
|
44
|
+
*/
|
|
45
|
+
absorb(prepared: readonly PreparedEntry[]): void;
|
|
46
|
+
/** The data phase of one article; `undefined` when the plan holds nothing. */
|
|
47
|
+
renderData(plan: SnapshotBundlePlan, id: string): RenderedSnapshotData | undefined;
|
|
48
|
+
/** Freezes the union table once; every article's data phase must have run. */
|
|
49
|
+
finish(): FinalizedSnapshotTables;
|
|
50
|
+
/** Expands one held data phase against the frozen table. */
|
|
51
|
+
assemble(rendered: RenderedSnapshotData): SnapshotBundle;
|
|
52
|
+
/** Drops the table handle; safe to call more than once. */
|
|
53
|
+
close(): void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* One build's table session (ADR 0052 `BundleLayering`): absorbs and data
|
|
57
|
+
* phases interleave article by article, the table freezes once after the
|
|
58
|
+
* last one, and every bundle assembles against the same frozen rows. A
|
|
59
|
+
* per-article freeze instead would pin every intermediate union and grow
|
|
60
|
+
* the shipped bytes quadratically.
|
|
61
|
+
*/
|
|
62
|
+
export declare function createSnapshotTableSession(): SnapshotTableSession;
|
|
63
|
+
export interface StandaloneSnapshotBundle {
|
|
64
|
+
readonly bundle: SnapshotBundle;
|
|
65
|
+
/** The item's own frozen table; the host writes it through a transport. */
|
|
66
|
+
readonly tableFile: FinalizedSnapshotTables;
|
|
67
|
+
readonly fontContractOnly: boolean;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The per-item fallback: the item absorbs into and freezes its own table,
|
|
71
|
+
* written beside the session's so the manifest's pin stays servable. Returns
|
|
72
|
+
* `undefined` when the item produced neither snapshots nor font contracts.
|
|
73
|
+
*/
|
|
74
|
+
export declare function renderStandaloneSnapshotBundle(plan: SnapshotBundlePlan, id: string): StandaloneSnapshotBundle | undefined;
|
package/lib/transport.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Host-side table transport and bundle assembly (ADR 0052 `TableTransport`).
|
|
2
|
+
// The native package freezes content-addressed TIQTBL03 bytes; the host
|
|
3
|
+
// serves each file under one URL derived from its sha and never parses it.
|
|
4
|
+
// This module owns the two orderings a build must not get wrong: a per-item
|
|
5
|
+
// bundle freezes its own table, and a build session freezes once, strictly
|
|
6
|
+
// after every article's data phase.
|
|
7
|
+
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { absorbSnapshotTables, assembleFontContractBundle, assembleSnapshotBundle, closeSnapshotTables, createSnapshotTables, finalizeSnapshotTables, renderFontContractBundleData, renderSnapshotBundleData, } from "./precompute.js";
|
|
11
|
+
const SHA256_PATTERN = /^[0-9a-f]{64}$/u;
|
|
12
|
+
const normalizeDirectory = (directory) => directory instanceof URL ? fileURLToPath(directory) : path.resolve(directory);
|
|
13
|
+
/**
|
|
14
|
+
* Serves one directory of frozen table files under `/<urlPrefix>/<sha><extension>`.
|
|
15
|
+
* A table name addresses immutable bytes, so responses cache forever and a
|
|
16
|
+
* name never serves two contents over the file's lifetime.
|
|
17
|
+
*/
|
|
18
|
+
export function createSnapshotTableFileTransport(options) {
|
|
19
|
+
const directory = normalizeDirectory(options.directory);
|
|
20
|
+
const urlPrefix = `/${(options.urlPrefix ?? "tiqian-tables").replace(/^\/+|\/+$/gu, "")}`;
|
|
21
|
+
const extension = options.extension ?? ".tiqtbl";
|
|
22
|
+
return Object.freeze({
|
|
23
|
+
directory,
|
|
24
|
+
urlFor(sha256) {
|
|
25
|
+
return `${urlPrefix}/${sha256}${extension}`;
|
|
26
|
+
},
|
|
27
|
+
urlPattern() {
|
|
28
|
+
return new RegExp(`${urlPrefix.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")}/([0-9a-f]{64})${extension.replace(/\./gu, "\\.")}`, "gu");
|
|
29
|
+
},
|
|
30
|
+
write(finalized) {
|
|
31
|
+
mkdirSync(directory, { recursive: true });
|
|
32
|
+
writeFileSync(path.join(directory, `${finalized.sha256}${extension}`), finalized.bytes);
|
|
33
|
+
return `${urlPrefix}/${finalized.sha256}${extension}`;
|
|
34
|
+
},
|
|
35
|
+
read(sha256) {
|
|
36
|
+
if (!SHA256_PATTERN.test(sha256))
|
|
37
|
+
return undefined;
|
|
38
|
+
try {
|
|
39
|
+
return readFileSync(path.join(directory, `${sha256}${extension}`));
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
listShas() {
|
|
46
|
+
try {
|
|
47
|
+
return readdirSync(directory)
|
|
48
|
+
.filter((name) => name.endsWith(extension))
|
|
49
|
+
.map((name) => name.slice(0, -extension.length));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
sweep(keepShas) {
|
|
56
|
+
let swept = 0;
|
|
57
|
+
let names;
|
|
58
|
+
try {
|
|
59
|
+
names = readdirSync(directory);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
for (const name of names) {
|
|
65
|
+
if (!name.endsWith(extension))
|
|
66
|
+
continue;
|
|
67
|
+
if (keepShas.has(name.slice(0, -extension.length)))
|
|
68
|
+
continue;
|
|
69
|
+
rmSync(path.join(directory, name), { force: true });
|
|
70
|
+
swept += 1;
|
|
71
|
+
}
|
|
72
|
+
return swept;
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* One build's table session (ADR 0052 `BundleLayering`): absorbs and data
|
|
78
|
+
* phases interleave article by article, the table freezes once after the
|
|
79
|
+
* last one, and every bundle assembles against the same frozen rows. A
|
|
80
|
+
* per-article freeze instead would pin every intermediate union and grow
|
|
81
|
+
* the shipped bytes quadratically.
|
|
82
|
+
*/
|
|
83
|
+
export function createSnapshotTableSession() {
|
|
84
|
+
const tables = createSnapshotTables();
|
|
85
|
+
let finalized = null;
|
|
86
|
+
let closed = false;
|
|
87
|
+
const guardOpen = (call) => {
|
|
88
|
+
if (closed)
|
|
89
|
+
throw new Error(`SnapshotTableSessionClosed:${call}`);
|
|
90
|
+
if (finalized !== null)
|
|
91
|
+
throw new Error(`SnapshotTableSessionFrozen:${call}`);
|
|
92
|
+
};
|
|
93
|
+
return Object.freeze({
|
|
94
|
+
absorb(prepared) {
|
|
95
|
+
guardOpen("absorb");
|
|
96
|
+
absorbSnapshotTables(tables, prepared);
|
|
97
|
+
},
|
|
98
|
+
renderData(plan, id) {
|
|
99
|
+
guardOpen("renderData");
|
|
100
|
+
if (plan.paragraphs.length > 0) {
|
|
101
|
+
return Object.freeze({
|
|
102
|
+
data: renderSnapshotBundleData(plan.paragraphs, {
|
|
103
|
+
id,
|
|
104
|
+
fontContractParagraphs: plan.fontContractParagraphs,
|
|
105
|
+
snapshotTables: tables,
|
|
106
|
+
}),
|
|
107
|
+
fontContractOnly: false,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (plan.fontContractParagraphs.length === 0)
|
|
111
|
+
return undefined;
|
|
112
|
+
return Object.freeze({
|
|
113
|
+
data: renderFontContractBundleData(plan.fontContractParagraphs, {
|
|
114
|
+
id,
|
|
115
|
+
snapshotTables: tables,
|
|
116
|
+
}),
|
|
117
|
+
fontContractOnly: true,
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
finish() {
|
|
121
|
+
guardOpen("finish");
|
|
122
|
+
finalized = finalizeSnapshotTables(tables);
|
|
123
|
+
return finalized;
|
|
124
|
+
},
|
|
125
|
+
assemble(rendered) {
|
|
126
|
+
if (closed)
|
|
127
|
+
throw new Error("SnapshotTableSessionClosed:assemble");
|
|
128
|
+
if (finalized === null)
|
|
129
|
+
throw new Error("SnapshotTableSessionUnfrozen:assemble");
|
|
130
|
+
return rendered.fontContractOnly
|
|
131
|
+
? assembleFontContractBundle(rendered.data, tables)
|
|
132
|
+
: assembleSnapshotBundle(rendered.data, tables);
|
|
133
|
+
},
|
|
134
|
+
close() {
|
|
135
|
+
if (closed)
|
|
136
|
+
return;
|
|
137
|
+
closed = true;
|
|
138
|
+
closeSnapshotTables(tables);
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The per-item fallback: the item absorbs into and freezes its own table,
|
|
144
|
+
* written beside the session's so the manifest's pin stays servable. Returns
|
|
145
|
+
* `undefined` when the item produced neither snapshots nor font contracts.
|
|
146
|
+
*/
|
|
147
|
+
export function renderStandaloneSnapshotBundle(plan, id) {
|
|
148
|
+
const session = createSnapshotTableSession();
|
|
149
|
+
try {
|
|
150
|
+
session.absorb(plan.paragraphs);
|
|
151
|
+
session.absorb(plan.fontContractParagraphs);
|
|
152
|
+
const rendered = session.renderData(plan, id);
|
|
153
|
+
if (!rendered)
|
|
154
|
+
return undefined;
|
|
155
|
+
const tableFile = session.finish();
|
|
156
|
+
return Object.freeze({
|
|
157
|
+
bundle: session.assemble(rendered),
|
|
158
|
+
tableFile,
|
|
159
|
+
fontContractOnly: rendered.fontContractOnly,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
session.close();
|
|
164
|
+
}
|
|
165
|
+
}
|
package/lib/vite.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Derives the importable entry specifiers of this package from its manifest:
|
|
3
|
+
* the bare name plus one specifier per exports key. Reads the shipped
|
|
4
|
+
* manifest by default; a URL argument serves tests and other manifests.
|
|
5
|
+
*/
|
|
6
|
+
export declare const tiqianSsrExternals: (manifestUrl?: URL) => Promise<string[]>;
|