@tiqian/precompute 0.0.0-stage → 0.1.0-alpha.9
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 +65 -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 +322 -0
- package/lib/precompute.js +540 -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,540 @@
|
|
|
1
|
+
// The precompute entry over the native addon (ADR 0050). The exported
|
|
2
|
+
// surface matches the former `@tiqian/web-component/precompute` module; every
|
|
3
|
+
// computation runs in Rust behind registry handles, and this file owns the
|
|
4
|
+
// object shapes the js API promises. File reading and URL resolution stay
|
|
5
|
+
// here: the crate never touches the filesystem.
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { readFile } from "node:fs/promises";
|
|
9
|
+
import { resolve } from "node:path";
|
|
10
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
11
|
+
import { addon } from "./load.js";
|
|
12
|
+
import { renderPreparedParagraph as sharedRenderPreparedParagraph, renderPreparedParagraphArtifact as sharedRenderPreparedParagraphArtifact, } from "@tiqian/web-component/prepared-dom";
|
|
13
|
+
import { Revision } from "@tiqian/protocol/org/tiqian/protocol/Revision";
|
|
14
|
+
import { SnapshotValueStyle } from "@tiqian/shared/org/tiqian/shared/SnapshotValueStyle";
|
|
15
|
+
import { ValueStyleInterner } from "@tiqian/shared/org/tiqian/shared/ValueStyleInterner";
|
|
16
|
+
import { sha256Hex, stableStringify } from "./stable-stringify.js";
|
|
17
|
+
import { createCacheBridge } from "./cache.js";
|
|
18
|
+
const SHARED_RUNTIME_STYLE = readFileSync(new URL(import.meta.resolve("@tiqian/core/styles.css")), "utf8");
|
|
19
|
+
const URL_SCHEME_PREFIX = /^[a-z][a-z0-9+.-]*:/iu;
|
|
20
|
+
const WINDOWS_DRIVE_ABSOLUTE_PATH = /^[a-z]:[\\/]/iu;
|
|
21
|
+
/**
|
|
22
|
+
* Parses a JSON string produced by the shared Rust emitters; parsing happens
|
|
23
|
+
* only at this boundary.
|
|
24
|
+
*/
|
|
25
|
+
function parse(json) {
|
|
26
|
+
return JSON.parse(json);
|
|
27
|
+
}
|
|
28
|
+
function toBuffer(source) {
|
|
29
|
+
return Buffer.isBuffer(source) ? source : Buffer.from(source);
|
|
30
|
+
}
|
|
31
|
+
// WindowsDrivePathBeforeUrlScheme: a drive letter is syntactically a valid
|
|
32
|
+
// URL scheme, but Node callers pass path.resolve()/path.join() results here
|
|
33
|
+
// as ordinary local sources. File paths must win before URL handling.
|
|
34
|
+
function isExplicitUrlString(source) {
|
|
35
|
+
return URL_SCHEME_PREFIX.test(source) && !WINDOWS_DRIVE_ABSOLUTE_PATH.test(source);
|
|
36
|
+
}
|
|
37
|
+
async function readSource(source) {
|
|
38
|
+
if (source instanceof Uint8Array)
|
|
39
|
+
return source;
|
|
40
|
+
if (source instanceof ArrayBuffer)
|
|
41
|
+
return new Uint8Array(source);
|
|
42
|
+
if (source instanceof URL) {
|
|
43
|
+
if (source.protocol !== "file:")
|
|
44
|
+
throw new Error(`RemoteFontSourceNotSupported:${source.href}`);
|
|
45
|
+
return new Uint8Array(await readFile(fileURLToPath(source)));
|
|
46
|
+
}
|
|
47
|
+
if (typeof source === "string") {
|
|
48
|
+
if (isExplicitUrlString(source)) {
|
|
49
|
+
const url = new URL(source);
|
|
50
|
+
if (url.protocol !== "file:")
|
|
51
|
+
throw new Error(`RemoteFontSourceNotSupported:${source}`);
|
|
52
|
+
return new Uint8Array(await readFile(fileURLToPath(url)));
|
|
53
|
+
}
|
|
54
|
+
return new Uint8Array(await readFile(source));
|
|
55
|
+
}
|
|
56
|
+
throw new Error("UnsupportedFontSource");
|
|
57
|
+
}
|
|
58
|
+
function stylesheetFileUrl(source) {
|
|
59
|
+
if (source instanceof URL) {
|
|
60
|
+
if (source.protocol !== "file:") {
|
|
61
|
+
throw new Error(`RemoteFontStylesheetNotSupported:${source.href}`);
|
|
62
|
+
}
|
|
63
|
+
return source;
|
|
64
|
+
}
|
|
65
|
+
if (typeof source !== "string" || !source.trim()) {
|
|
66
|
+
throw new Error("UnsupportedFontStylesheetSource");
|
|
67
|
+
}
|
|
68
|
+
if (isExplicitUrlString(source)) {
|
|
69
|
+
const url = new URL(source);
|
|
70
|
+
if (url.protocol !== "file:")
|
|
71
|
+
throw new Error(`RemoteFontStylesheetNotSupported:${source}`);
|
|
72
|
+
return url;
|
|
73
|
+
}
|
|
74
|
+
return pathToFileURL(resolve(source));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Resolves the face inputs of the create calls. Explicit faces carry their
|
|
78
|
+
* bytes; the stylesheet lane reads the CSS here, parses it in Rust and reads
|
|
79
|
+
* the font files the parse resolved. `sourceOrder` accumulates across
|
|
80
|
+
* stylesheets, the js order.
|
|
81
|
+
*/
|
|
82
|
+
export async function resolveFaces(options) {
|
|
83
|
+
const explicitFaces = Array.from(options.faces ?? []);
|
|
84
|
+
const stylesheets = Array.from(options.fontStylesheets ?? []);
|
|
85
|
+
if (explicitFaces.length > 0 && stylesheets.length > 0) {
|
|
86
|
+
throw new Error("ConflictingBuildFontSources");
|
|
87
|
+
}
|
|
88
|
+
const sources = [];
|
|
89
|
+
if (explicitFaces.length > 0) {
|
|
90
|
+
for (const face of explicitFaces) {
|
|
91
|
+
sources.push(toBuffer(await readSource(face.source)));
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
faces: explicitFaces.map((face, index) => ({
|
|
95
|
+
family: face.family,
|
|
96
|
+
publicUrl: face.publicUrl,
|
|
97
|
+
font: index,
|
|
98
|
+
faceIndex: face.faceIndex ?? null,
|
|
99
|
+
weight: face.weight,
|
|
100
|
+
style: face.style,
|
|
101
|
+
unicodeRange: face.unicodeRange ?? null,
|
|
102
|
+
sourceOrder: null,
|
|
103
|
+
})),
|
|
104
|
+
sources,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
// Stylesheet lane: the wrapper reads the CSS, Rust parses it and resolves
|
|
108
|
+
// every asset URL; `sourceOrder` accumulates across stylesheets.
|
|
109
|
+
const stylesheetFaces = [];
|
|
110
|
+
for (const stylesheet of stylesheets) {
|
|
111
|
+
if (!stylesheet || typeof stylesheet !== "object")
|
|
112
|
+
throw new Error("InvalidFontStylesheet");
|
|
113
|
+
const fileUrl = stylesheetFileUrl(stylesheet.source);
|
|
114
|
+
const css = await readFile(fileURLToPath(fileUrl), "utf8");
|
|
115
|
+
stylesheetFaces.push(...parse(addon.parseBuildFontStylesheet(css, fileUrl.href, stylesheet.publicUrl ?? null)));
|
|
116
|
+
}
|
|
117
|
+
if (stylesheetFaces.length === 0)
|
|
118
|
+
throw new Error("MissingBuildFontSource");
|
|
119
|
+
for (const face of stylesheetFaces) {
|
|
120
|
+
sources.push(toBuffer(await readFile(face.source)));
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
faces: stylesheetFaces.map((face, index) => ({
|
|
124
|
+
family: face.family,
|
|
125
|
+
publicUrl: face.publicUrl,
|
|
126
|
+
font: index,
|
|
127
|
+
faceIndex: null,
|
|
128
|
+
weight: face.weight ?? undefined,
|
|
129
|
+
style: face.style,
|
|
130
|
+
unicodeRange: face.unicodeRange,
|
|
131
|
+
sourceOrder: index,
|
|
132
|
+
})),
|
|
133
|
+
sources,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/** Name constants of {@link CacheWriteBudget}. */
|
|
137
|
+
export const CacheWriteBudget = Object.freeze({
|
|
138
|
+
Tight: "tight",
|
|
139
|
+
Normal: "normal",
|
|
140
|
+
Generous: "generous",
|
|
141
|
+
});
|
|
142
|
+
const writeBudgetCodes = new Map([
|
|
143
|
+
["tight", 0],
|
|
144
|
+
["normal", 1],
|
|
145
|
+
["generous", 2],
|
|
146
|
+
]);
|
|
147
|
+
function writeBudgetCode(value) {
|
|
148
|
+
const code = writeBudgetCodes.get(value);
|
|
149
|
+
if (code === undefined) {
|
|
150
|
+
throw new TypeError(`UnknownCacheWriteBudget:${String(value)}`);
|
|
151
|
+
}
|
|
152
|
+
return code;
|
|
153
|
+
}
|
|
154
|
+
let defaultCacheWriteBudget = CacheWriteBudget.Normal;
|
|
155
|
+
/**
|
|
156
|
+
* Sets the process default every later `createPrecomputer` call uses when its
|
|
157
|
+
* options name no tier. Precomputers already created keep theirs.
|
|
158
|
+
*/
|
|
159
|
+
export function setCacheWriteBudget(budget) {
|
|
160
|
+
writeBudgetCode(budget);
|
|
161
|
+
defaultCacheWriteBudget = budget;
|
|
162
|
+
}
|
|
163
|
+
const precomputerHandles = new WeakMap();
|
|
164
|
+
/**
|
|
165
|
+
* Internal: the native registry handle of a precomputer created by this
|
|
166
|
+
* package, for the HTML preparer entry in `precompute-html.ts`.
|
|
167
|
+
*/
|
|
168
|
+
export function precomputerHandle(precomputer) {
|
|
169
|
+
return precomputerHandles.get(precomputer) ?? null;
|
|
170
|
+
}
|
|
171
|
+
function freezeEntry(entry) {
|
|
172
|
+
return Object.freeze(entry);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Replays the shared prepared-DOM renderer in Node to recompute the html field
|
|
176
|
+
* and the renderArtifactSha256 field for one prepared entry. The options replay
|
|
177
|
+
* the exact set precomputer.rs builds in render_options (precomputer.rs L305-327)
|
|
178
|
+
* before handing the plan to render_prepared_paragraph_artifact_from_plan:
|
|
179
|
+
* semantics, inlineBoxes, renderTextSpans and sourceText are supplied, while
|
|
180
|
+
* cjkStrongSemantics and styleClassFor stay unset. The options value carries a
|
|
181
|
+
* cast because the shared module declares its option types in a normalized
|
|
182
|
+
* form; the runtime values entry.semantics, entry.inlineBoxes and
|
|
183
|
+
* entry.renderTextSpans carry back are the raw objects the Rust renderer fed the
|
|
184
|
+
* shared renderer, so the replay is byte-faithful.
|
|
185
|
+
*/
|
|
186
|
+
function computeRenderArtifact(entry) {
|
|
187
|
+
if (!isSharedPlanInput(entry.plan)) {
|
|
188
|
+
throw new TypeError("prepareParagraph returned a prepared entry without a renderable plan");
|
|
189
|
+
}
|
|
190
|
+
const rendered = sharedRenderPreparedParagraphArtifact(entry.plan, entry.typography.locale, {
|
|
191
|
+
sourceText: entry.sourceText,
|
|
192
|
+
semantics: entry.semantics,
|
|
193
|
+
renderTextSpans: entry.renderTextSpans,
|
|
194
|
+
inlineBoxes: entry.inlineBoxes,
|
|
195
|
+
});
|
|
196
|
+
return {
|
|
197
|
+
html: rendered.html,
|
|
198
|
+
renderArtifactSha256: createHash("sha256")
|
|
199
|
+
.update(stableStringify(rendered.artifact))
|
|
200
|
+
.digest("hex"),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Overwrites the render fields of a prepared entry with the Node-computed
|
|
205
|
+
* values, then freezes the entry. Unsupported entries pass through untouched;
|
|
206
|
+
* the font-contract lanes keep their own freezeEntry call and never reach this
|
|
207
|
+
* helper.
|
|
208
|
+
*/
|
|
209
|
+
export function backfillRenderFields(entry) {
|
|
210
|
+
if (entry.status !== "prepared") {
|
|
211
|
+
return freezeEntry(entry);
|
|
212
|
+
}
|
|
213
|
+
const { html, renderArtifactSha256 } = computeRenderArtifact(entry);
|
|
214
|
+
return freezeEntry({ ...entry, html, renderArtifactSha256 });
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Creates a precomputer from explicit faces or a host font stylesheet. The
|
|
218
|
+
* typography is normalized in Rust; every named validation error of the js
|
|
219
|
+
* implementation is thrown at the same step.
|
|
220
|
+
*/
|
|
221
|
+
export async function createPrecomputer(options) {
|
|
222
|
+
const typographyJson = JSON.stringify(options.typography ?? {});
|
|
223
|
+
// Typography validates before any font file is read, the js order; the
|
|
224
|
+
// native create normalizes the same value again behind its boundary.
|
|
225
|
+
addon.normalizeTypography(typographyJson);
|
|
226
|
+
// The tier validates before any font IO for the same reason.
|
|
227
|
+
const budgetCode = writeBudgetCode(options.cacheWriteBudget ?? defaultCacheWriteBudget);
|
|
228
|
+
const { faces, sources } = await resolveFaces(options);
|
|
229
|
+
const handle = addon.createPrecomputer(typographyJson, faces, sources, budgetCode);
|
|
230
|
+
const info = parse(addon.precomputerInfo(handle));
|
|
231
|
+
const precomputer = {
|
|
232
|
+
typography: Object.freeze(info.typography),
|
|
233
|
+
renderFontFamilies: Object.freeze(info.renderFontFamilies),
|
|
234
|
+
cache: createCacheBridge(handle),
|
|
235
|
+
async prepareParagraph(input) {
|
|
236
|
+
return backfillRenderFields(parse(addon.prepareParagraph(handle, JSON.stringify(input ?? {}))));
|
|
237
|
+
},
|
|
238
|
+
async prepareParagraphs(inputs) {
|
|
239
|
+
return parse(addon.prepareParagraphs(handle, JSON.stringify(Array.from(inputs ?? [])))).map(backfillRenderFields);
|
|
240
|
+
},
|
|
241
|
+
async prepareFontContract(input) {
|
|
242
|
+
// A contract entry joins the same corpus as the snapshot entries, and
|
|
243
|
+
// the corpus gate requires the derived render fields on every member, so
|
|
244
|
+
// this lane derives them the same way.
|
|
245
|
+
return backfillRenderFields(parse(addon.prepareFontContract(handle, JSON.stringify(input ?? {}))));
|
|
246
|
+
},
|
|
247
|
+
async prepareFontContracts(inputs) {
|
|
248
|
+
return parse(addon.prepareFontContracts(handle, JSON.stringify(Array.from(inputs ?? [])))).map(backfillRenderFields);
|
|
249
|
+
},
|
|
250
|
+
close() {
|
|
251
|
+
addon.closePrecomputer(handle);
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
precomputerHandles.set(precomputer, handle);
|
|
255
|
+
return Object.freeze(precomputer);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Narrows the public `unknown` plan input to what the shared renderer accepts.
|
|
259
|
+
* The structural check stays coarse on purpose: the shared module owns plan
|
|
260
|
+
* validation, and this gate only rejects values no plan form can take.
|
|
261
|
+
*/
|
|
262
|
+
function isSharedPlanInput(value) {
|
|
263
|
+
return typeof value === "string" || (typeof value === "object" && value !== null);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Widens the local typography interface to the record shape the shared
|
|
267
|
+
* renderer accepts. Interfaces carry no implicit index signature, so the
|
|
268
|
+
* spread produces a structurally identical anonymous type that does.
|
|
269
|
+
*/
|
|
270
|
+
function toSharedTypography(typography) {
|
|
271
|
+
return { ...typography };
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Renders one prepared plan to prepared DOM. The implementation is the exact
|
|
275
|
+
* browser-shared module `@tiqian/web-component` ships, so server embedders and the
|
|
276
|
+
* browser agree on every revision.
|
|
277
|
+
*/
|
|
278
|
+
export function renderPreparedParagraph(plan, typography) {
|
|
279
|
+
if (!isSharedPlanInput(plan)) {
|
|
280
|
+
throw new TypeError("renderPreparedParagraph expects a plan JSON string or object");
|
|
281
|
+
}
|
|
282
|
+
return sharedRenderPreparedParagraph(plan, toSharedTypography(typography));
|
|
283
|
+
}
|
|
284
|
+
/** The named plain-text gate of the snapshot lane. */
|
|
285
|
+
export function snapshotPlainTextIssue(text) {
|
|
286
|
+
return parse(addon.snapshotPlainTextIssue(text));
|
|
287
|
+
}
|
|
288
|
+
function freezeBundle(bundle) {
|
|
289
|
+
return Object.freeze({
|
|
290
|
+
...bundle,
|
|
291
|
+
renderFontFamilies: Object.freeze([...bundle.renderFontFamilies]),
|
|
292
|
+
fontPreloads: Object.freeze([...bundle.fontPreloads]),
|
|
293
|
+
rootAttributes: Object.freeze({ ...bundle.rootAttributes }),
|
|
294
|
+
entries: Object.freeze(bundle.entries.map((entry) => Object.freeze({ ...entry }))),
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
/** Creates the empty, unfrozen table set of one build. */
|
|
298
|
+
export function createSnapshotTables() {
|
|
299
|
+
return {
|
|
300
|
+
kind: "snapshot-tables",
|
|
301
|
+
handle: addon.createSnapshotTables(),
|
|
302
|
+
valueStyles: new ValueStyleInterner(),
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
/** Restores a previous build's frozen table so a rebuild extends the union. */
|
|
306
|
+
export function restoreSnapshotTables(bytes) {
|
|
307
|
+
return {
|
|
308
|
+
kind: "snapshot-tables",
|
|
309
|
+
handle: addon.restoreSnapshotTables(bytes),
|
|
310
|
+
valueStyles: new ValueStyleInterner(),
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Absorbs one batch of prepared entries into the table; returns the absorbed
|
|
315
|
+
* entry count. Absorb and data-phase calls share one mutable table and may
|
|
316
|
+
* interleave in any order; both throw once the table froze.
|
|
317
|
+
*/
|
|
318
|
+
export function absorbSnapshotTables(tables, prepared) {
|
|
319
|
+
return addon.absorbSnapshotTables(tables.handle, JSON.stringify(Array.from(prepared ?? [])));
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Absorbs table-scoped render metadata; value styles append to the TS-side
|
|
323
|
+
* interner in first-seen order, and re-absorbing an existing declaration
|
|
324
|
+
* keeps its index. The native table rows stay untouched, so the table hash
|
|
325
|
+
* does not change.
|
|
326
|
+
*/
|
|
327
|
+
export function absorbSnapshotTablesMetadata(tables, metadata) {
|
|
328
|
+
const rows = metadata?.valueStyles;
|
|
329
|
+
if (!Array.isArray(rows))
|
|
330
|
+
return;
|
|
331
|
+
for (const row of rows) {
|
|
332
|
+
if (typeof row !== "string")
|
|
333
|
+
throw new Error("SnapshotTableValueStylesInvalid");
|
|
334
|
+
tables.valueStyles.intern(row);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Freezes the rows and returns `{bytes, sha256}`; hosts serve the binary
|
|
339
|
+
* verbatim under the sha address. The handle stays usable for assembly.
|
|
340
|
+
*/
|
|
341
|
+
export function finalizeSnapshotTables(tables) {
|
|
342
|
+
const file = addon.finalizeSnapshotTables(tables.handle);
|
|
343
|
+
return Object.freeze({ bytes: file.bytes, sha256: file.sha256 });
|
|
344
|
+
}
|
|
345
|
+
/** Drops the table set once every bundle assembled. */
|
|
346
|
+
export function closeSnapshotTables(tables) {
|
|
347
|
+
addon.closeSnapshotTables(tables.handle);
|
|
348
|
+
}
|
|
349
|
+
const SNAPSHOT_SCHEMA = Revision.SNAPSHOT_SCHEMA;
|
|
350
|
+
const LAYOUT_REVISION = Revision.LAYOUT_REVISION;
|
|
351
|
+
const RENDER_REVISION = Revision.RENDER_REVISION;
|
|
352
|
+
const DEFAULT_LOCALE = "zh-Hans";
|
|
353
|
+
/** `PLAIN_PARAGRAPH_SELECTOR`: canonical plain paragraphs of a snapshot root. */
|
|
354
|
+
const PLAIN_PARAGRAPH_SELECTOR = ":is(p, li)[data-tq-snapshot-key]";
|
|
355
|
+
/** `RUNTIME_PARAGRAPH_SELECTOR`: every live paragraph of a semantic root. */
|
|
356
|
+
const RUNTIME_PARAGRAPH_SELECTOR = ":is(p, li):not([data-tiqian-skip])";
|
|
357
|
+
/** `preparedLocale`: a string typography is its locale, an object carries it. */
|
|
358
|
+
function preparedLocale(typography) {
|
|
359
|
+
if (typeof typography === "string")
|
|
360
|
+
return typography;
|
|
361
|
+
if (typography !== null && typeof typography === "object") {
|
|
362
|
+
const locale = typography.locale;
|
|
363
|
+
if (typeof locale === "string")
|
|
364
|
+
return locale;
|
|
365
|
+
if (typeof locale === "number")
|
|
366
|
+
return String(locale);
|
|
367
|
+
if (typeof locale === "boolean")
|
|
368
|
+
return String(locale);
|
|
369
|
+
}
|
|
370
|
+
return DEFAULT_LOCALE;
|
|
371
|
+
}
|
|
372
|
+
/** The corpus gates of the data phase, in the order the Rust port ran them. */
|
|
373
|
+
function bundleCorpus(entries, fontContractEntries) {
|
|
374
|
+
if (entries.length === 0)
|
|
375
|
+
throw new Error("MissingPreparedParagraphs");
|
|
376
|
+
const corpus = [...entries, ...fontContractEntries];
|
|
377
|
+
if (corpus.some((entry) => entry?.status !== "prepared")) {
|
|
378
|
+
throw new Error("SnapshotTemplateContainsUnsupportedParagraph");
|
|
379
|
+
}
|
|
380
|
+
if (corpus.some((entry) => {
|
|
381
|
+
const row = entry;
|
|
382
|
+
return row.schema !== SNAPSHOT_SCHEMA ||
|
|
383
|
+
row.layoutRevision !== LAYOUT_REVISION ||
|
|
384
|
+
row.renderRevision !== RENDER_REVISION ||
|
|
385
|
+
typeof row.renderArtifactSha256 !== "string";
|
|
386
|
+
})) {
|
|
387
|
+
throw new Error("SnapshotTemplateContainsStalePreparedParagraph");
|
|
388
|
+
}
|
|
389
|
+
const seen = new Set();
|
|
390
|
+
for (const entry of corpus) {
|
|
391
|
+
const key = entry.key ?? null;
|
|
392
|
+
const rendered = stableStringify(key);
|
|
393
|
+
if (seen.has(rendered))
|
|
394
|
+
throw new Error("DuplicateSnapshotKey");
|
|
395
|
+
seen.add(rendered);
|
|
396
|
+
}
|
|
397
|
+
return corpus;
|
|
398
|
+
}
|
|
399
|
+
/** The exact render font families every corpus entry must share. */
|
|
400
|
+
function renderFontFamiliesOf(entries) {
|
|
401
|
+
const families = entries[0]?.renderFontFamilies;
|
|
402
|
+
if (!Array.isArray(families) || families.length === 0) {
|
|
403
|
+
throw new Error("MissingExactRenderFontFamilies");
|
|
404
|
+
}
|
|
405
|
+
for (const family of families) {
|
|
406
|
+
if (typeof family !== "string" || family.trim().length === 0) {
|
|
407
|
+
throw new Error("MissingExactRenderFontFamilies");
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return families;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Renders one prepared entry with the shared renderer and hashes its artifact
|
|
414
|
+
* tree with the same stable encoding the Rust renderer used, so the entry
|
|
415
|
+
* keeps its byte identity when the render loop moves here.
|
|
416
|
+
*/
|
|
417
|
+
function renderBundleEntry(entry, styleClassFor) {
|
|
418
|
+
const row = entry;
|
|
419
|
+
const plan = row.plan;
|
|
420
|
+
if (plan === undefined || plan === null)
|
|
421
|
+
throw new Error("InvalidPreparedParagraphGeometry");
|
|
422
|
+
const planText = typeof plan === "string" ? plan : JSON.stringify(plan);
|
|
423
|
+
const rendered = sharedRenderPreparedParagraphArtifact(planText, preparedLocale(row.typography), {
|
|
424
|
+
semantics: row.semantics,
|
|
425
|
+
inlineBoxes: row.inlineBoxes,
|
|
426
|
+
renderTextSpans: row.renderTextSpans,
|
|
427
|
+
sourceText: typeof row.sourceText === "string" ? row.sourceText : undefined,
|
|
428
|
+
styleClassFor,
|
|
429
|
+
});
|
|
430
|
+
return {
|
|
431
|
+
...row,
|
|
432
|
+
html: rendered.html,
|
|
433
|
+
renderArtifactSha256: sha256Hex(stableStringify(rendered.artifact)),
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* The data phase over one entry list: gates, per-entry render with the shared
|
|
438
|
+
* renderer, and the class indexes this bundle minted. First-paint rules are
|
|
439
|
+
* appended at assembly from these indexes.
|
|
440
|
+
*/
|
|
441
|
+
function renderBundleData(preparedParagraphs, options, supportedParagraphSelector) {
|
|
442
|
+
const entries = Array.from(preparedParagraphs ?? []);
|
|
443
|
+
const fontContractEntries = Array.from(options.fontContractParagraphs ?? []);
|
|
444
|
+
const corpus = bundleCorpus(entries, fontContractEntries);
|
|
445
|
+
const families = renderFontFamiliesOf(entries);
|
|
446
|
+
const familySignature = stableStringify(families);
|
|
447
|
+
for (const entry of corpus) {
|
|
448
|
+
const entryFamilies = entry.renderFontFamilies;
|
|
449
|
+
const signature = entryFamilies === undefined ? "" : stableStringify(entryFamilies);
|
|
450
|
+
if (signature !== familySignature)
|
|
451
|
+
throw new Error("SnapshotRenderFontFamilyConflict");
|
|
452
|
+
}
|
|
453
|
+
const id = typeof options.id === "string" ? options.id.trim() : "";
|
|
454
|
+
if (id.length === 0)
|
|
455
|
+
throw new Error("MissingSnapshotTemplateId");
|
|
456
|
+
if (!/^[A-Za-z][A-Za-z0-9_-]*$/u.test(id))
|
|
457
|
+
throw new Error("InvalidSnapshotTemplateId");
|
|
458
|
+
const paragraphSelector = typeof options.paragraphSelector === "string"
|
|
459
|
+
? options.paragraphSelector
|
|
460
|
+
: supportedParagraphSelector;
|
|
461
|
+
if (paragraphSelector !== supportedParagraphSelector) {
|
|
462
|
+
throw new Error("UnsupportedSnapshotParagraphSelector");
|
|
463
|
+
}
|
|
464
|
+
const interner = options.snapshotTables.valueStyles;
|
|
465
|
+
const usedValueStyleIndexes = [];
|
|
466
|
+
const styleClassFor = (declaration) => {
|
|
467
|
+
const index = interner.intern(declaration);
|
|
468
|
+
if (!usedValueStyleIndexes.includes(index))
|
|
469
|
+
usedValueStyleIndexes.push(index);
|
|
470
|
+
return SnapshotValueStyle.snapshotValueStyleClass(index);
|
|
471
|
+
};
|
|
472
|
+
const renderedEntries = entries.map((entry) => renderBundleEntry(entry, styleClassFor));
|
|
473
|
+
return Object.freeze({
|
|
474
|
+
kind: "snapshot-bundle-data",
|
|
475
|
+
id,
|
|
476
|
+
usedValueStyleIndexes: Object.freeze([...usedValueStyleIndexes]),
|
|
477
|
+
json: JSON.stringify({
|
|
478
|
+
id,
|
|
479
|
+
paragraphSelector,
|
|
480
|
+
renderFontFamilies: families,
|
|
481
|
+
renderedEntries,
|
|
482
|
+
fontContractEntries,
|
|
483
|
+
}),
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* The data phase for plain snapshot bundles: renders the bodies with the
|
|
488
|
+
* shared TS renderer and mints `tqv-` classes into the build's TS interner.
|
|
489
|
+
*/
|
|
490
|
+
export function renderSnapshotBundleData(preparedParagraphs, options) {
|
|
491
|
+
return renderBundleData(preparedParagraphs, options, PLAIN_PARAGRAPH_SELECTOR);
|
|
492
|
+
}
|
|
493
|
+
/** The data phase for font-contract bundles. */
|
|
494
|
+
export function renderFontContractBundleData(preparedParagraphs, options) {
|
|
495
|
+
return renderBundleData(preparedParagraphs, options, RUNTIME_PARAGRAPH_SELECTOR);
|
|
496
|
+
}
|
|
497
|
+
function assembleWireValues(data, tables) {
|
|
498
|
+
return [
|
|
499
|
+
data.json,
|
|
500
|
+
JSON.stringify({ snapshotTables: tables.handle }),
|
|
501
|
+
];
|
|
502
|
+
}
|
|
503
|
+
/** `escapeAttribute`: ampersand, quote, and left angle, in that order. */
|
|
504
|
+
function escapeBundleAttribute(value) {
|
|
505
|
+
return value
|
|
506
|
+
.replaceAll("&", "&")
|
|
507
|
+
.replaceAll('"', """)
|
|
508
|
+
.replaceAll("<", "<");
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* The first-paint rules of the value-style classes this bundle used, in
|
|
512
|
+
* first-mint order. The native assembly owns the templates and the manifest;
|
|
513
|
+
* the declarations live in the shared layer, so the rules append here in the
|
|
514
|
+
* byte form the Rust assembly emitted.
|
|
515
|
+
*/
|
|
516
|
+
function valueStyleRules(data, tables) {
|
|
517
|
+
let rules = "";
|
|
518
|
+
for (const index of data.usedValueStyleIndexes) {
|
|
519
|
+
const declaration = tables.valueStyles.at(index);
|
|
520
|
+
if (typeof declaration !== "string")
|
|
521
|
+
throw new Error("SnapshotTableValueStyleMissing");
|
|
522
|
+
rules += `tiqian-prose[snapshot-ref="${escapeBundleAttribute(data.id)}"] ` +
|
|
523
|
+
`[data-tq-rendered="true"] .${SnapshotValueStyle.snapshotValueStyleClass(index)}{${declaration}}`;
|
|
524
|
+
}
|
|
525
|
+
return rules;
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* The assembly phase: compacts the manifest against the frozen table, builds
|
|
529
|
+
* the templates and the first-paint style. Requires finalizeSnapshotTables.
|
|
530
|
+
*/
|
|
531
|
+
export function assembleSnapshotBundle(data, tables) {
|
|
532
|
+
const [dataJson, optionsJson] = assembleWireValues(data, tables);
|
|
533
|
+
const bundle = parse(addon.assembleSnapshotBundle(dataJson, optionsJson, SHARED_RUNTIME_STYLE));
|
|
534
|
+
return freezeBundle({ ...bundle, initialStyle: bundle.initialStyle + valueStyleRules(data, tables) });
|
|
535
|
+
}
|
|
536
|
+
/** The assembly phase for font-contract bundles. */
|
|
537
|
+
export function assembleFontContractBundle(data, tables) {
|
|
538
|
+
const [dataJson, optionsJson] = assembleWireValues(data, tables);
|
|
539
|
+
return freezeBundle(parse(addon.assembleFontContractBundle(dataJson, optionsJson, SHARED_RUNTIME_STYLE)));
|
|
540
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { ArticleIndexRecord, PersistentCacheStore } from "./persistence.js";
|
|
2
|
+
/** One sqlite-backed store plus its maintenance entries. */
|
|
3
|
+
export interface SqliteCacheStore extends PersistentCacheStore {
|
|
4
|
+
/**
|
|
5
|
+
* Drops entries of one context that are not in the keep list. Returns the
|
|
6
|
+
* number of rows removed; other contexts are untouched.
|
|
7
|
+
*/
|
|
8
|
+
prune(context: string, keep: readonly string[]): number;
|
|
9
|
+
/**
|
|
10
|
+
* Bucket-scoped eviction (ADR 0052 BucketIntersectionEviction): within the
|
|
11
|
+
* entries this bucket's article rows attribute, keeps the keep list and
|
|
12
|
+
* everything other buckets' rows reference; trims the bucket's hash rows
|
|
13
|
+
* to the list and drops emptied article rows. Returns the entry count
|
|
14
|
+
* removed; other buckets, unattributed entries and other contexts stay.
|
|
15
|
+
*/
|
|
16
|
+
pruneBucket(context: string, bucket: string, keep: readonly string[]): number;
|
|
17
|
+
/**
|
|
18
|
+
* Deletes every entry, article row and hash row that belongs to a context
|
|
19
|
+
* outside `contexts`. A context fingerprint rotates when the engine or its
|
|
20
|
+
* inputs change, and the old context's rows are unreachable afterwards. A
|
|
21
|
+
* host serving a fixed context set usually declares it at open
|
|
22
|
+
* (createSqliteCacheStore options.contexts) instead of calling this; the
|
|
23
|
+
* method stays for stores shared beyond what one open call can list. An
|
|
24
|
+
* empty list removes nothing. Returns the entry count removed.
|
|
25
|
+
*/
|
|
26
|
+
dropOtherContexts(contexts: readonly string[]): number;
|
|
27
|
+
/**
|
|
28
|
+
* Rebuilds the file when deleted rows left free pages inside it, returning
|
|
29
|
+
* the space to the filesystem. Reads the free-page count first, so a file
|
|
30
|
+
* without free pages returns at once.
|
|
31
|
+
*/
|
|
32
|
+
compact(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Records article-index rows (ADR 0052 Article layer), replacing the rows
|
|
35
|
+
* of the same article keys.
|
|
36
|
+
*/
|
|
37
|
+
recordArticles(context: string, articles: readonly ArticleIndexRecord[]): void;
|
|
38
|
+
/** Reads every article row of one context, ordered by article key. */
|
|
39
|
+
readArticles(context: string): ArticleIndexRecord[];
|
|
40
|
+
/** Deletes article rows by key; returns the row count removed. */
|
|
41
|
+
deleteArticles(context: string, articleKeys: readonly string[]): number;
|
|
42
|
+
/** Closes the database; later calls report by name. */
|
|
43
|
+
close(): void;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Opens (or creates) the database at `path`. The parent directory is created
|
|
47
|
+
* when missing; the file belongs to the caller's cache directory policy.
|
|
48
|
+
*
|
|
49
|
+
* `options.contexts` declares the full set of contexts this file serves: at
|
|
50
|
+
* open, rows of every other context drop (entries, article rows and hash
|
|
51
|
+
* rows) and the file rebuilds around the survivors. Omit it when the file
|
|
52
|
+
* serves contexts this caller cannot list. The rebuild also runs without a
|
|
53
|
+
* declaration whenever earlier deletes left free pages inside the file.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createSqliteCacheStore(path: string, options?: {
|
|
56
|
+
contexts?: readonly string[];
|
|
57
|
+
}): Promise<SqliteCacheStore>;
|