@conloca/content-api 0.1.14 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/asset-manifest.d.ts +23 -0
- package/dist/asset-manifest.d.ts.map +1 -0
- package/dist/asset-manifest.js +73 -0
- package/dist/asset-operations.d.ts +159 -0
- package/dist/asset-operations.d.ts.map +1 -0
- package/dist/asset-operations.js +736 -0
- package/dist/asset-types.d.ts +56 -0
- package/dist/asset-types.d.ts.map +1 -0
- package/dist/asset-types.js +7 -0
- package/dist/base-content-index.d.ts +57 -0
- package/dist/base-content-index.d.ts.map +1 -0
- package/dist/base-content-index.js +179 -0
- package/dist/block-index.d.ts +9 -0
- package/dist/block-index.d.ts.map +1 -0
- package/dist/block-index.js +11 -0
- package/dist/blocks.d.ts +16 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +103 -0
- package/dist/cf-access.d.ts +31 -0
- package/dist/cf-access.d.ts.map +1 -0
- package/dist/cf-access.js +66 -0
- package/dist/component-registry.d.ts +50 -0
- package/dist/component-registry.d.ts.map +1 -0
- package/dist/component-registry.js +120 -0
- package/dist/component-registry.types-C0ctF9nh.d.mts +71 -0
- package/dist/component-registry.types-C0ctF9nh.d.mts.map +1 -0
- package/dist/component-registry.types.d.ts +11 -0
- package/dist/component-registry.types.d.ts.map +1 -0
- package/dist/component-registry.types.js +1 -0
- package/dist/content-api.interface.d.ts +64 -0
- package/dist/content-api.interface.d.ts.map +1 -0
- package/dist/content-api.interface.js +1 -0
- package/dist/content-filters.d.ts +31 -0
- package/dist/content-filters.d.ts.map +1 -0
- package/dist/content-filters.js +246 -0
- package/dist/content-index-D092_vk1.mjs +629 -0
- package/dist/content-index-D092_vk1.mjs.map +1 -0
- package/dist/content-index-D6IVMmAB.d.mts +127 -0
- package/dist/content-index-D6IVMmAB.d.mts.map +1 -0
- package/dist/content-index.d.ts +126 -0
- package/dist/content-index.d.ts.map +1 -0
- package/dist/content-index.js +418 -0
- package/dist/content-operations-DIbsxG5A.d.mts +354 -0
- package/dist/content-operations-DIbsxG5A.d.mts.map +1 -0
- package/dist/content-operations.d.ts +93 -0
- package/dist/content-operations.d.ts.map +1 -0
- package/dist/content-operations.js +275 -0
- package/dist/content-reader.d.ts +10 -0
- package/dist/content-reader.d.ts.map +1 -0
- package/dist/content-reader.js +13 -0
- package/dist/content-utils-fWNHTrPy.mjs +280 -0
- package/dist/content-utils-fWNHTrPy.mjs.map +1 -0
- package/dist/content-utils.d.ts +132 -0
- package/dist/content-utils.d.ts.map +1 -0
- package/dist/content-utils.js +352 -0
- package/dist/content-watcher.d.ts +43 -0
- package/dist/content-watcher.d.ts.map +1 -0
- package/dist/content-watcher.js +83 -0
- package/dist/data-DquNIEnQ.mjs +62 -0
- package/dist/data-DquNIEnQ.mjs.map +1 -0
- package/dist/data-index.d.ts +8 -0
- package/dist/data-index.d.ts.map +1 -0
- package/dist/data-index.js +7 -0
- package/dist/data.d.ts +43 -0
- package/dist/data.d.ts.map +1 -0
- package/dist/data.js +103 -0
- package/dist/etag-utils.d.ts +77 -0
- package/dist/etag-utils.d.ts.map +1 -0
- package/dist/etag-utils.js +160 -0
- package/dist/filesystem-content-api-B1KIQJfk.mjs +2 -0
- package/dist/filesystem-content-api-C0HIhqZi.mjs +1620 -0
- package/dist/filesystem-content-api-C0HIhqZi.mjs.map +1 -0
- package/dist/filesystem-content-api-DCdNi-co.d.mts +134 -0
- package/dist/filesystem-content-api-DCdNi-co.d.mts.map +1 -0
- package/dist/filesystem-content-api.d.ts +135 -0
- package/dist/filesystem-content-api.d.ts.map +1 -0
- package/dist/filesystem-content-api.js +2181 -0
- package/dist/git-operations.d.ts +49 -0
- package/dist/git-operations.d.ts.map +1 -0
- package/dist/git-operations.js +252 -0
- package/dist/header-parser.d.ts +37 -0
- package/dist/header-parser.d.ts.map +1 -0
- package/dist/header-parser.js +286 -0
- package/dist/in-memory-content-api.d.ts +35 -0
- package/dist/in-memory-content-api.d.ts.map +1 -0
- package/dist/in-memory-content-api.js +591 -0
- package/dist/index-DxVTFK66.d.mts +105 -0
- package/dist/index-DxVTFK66.d.mts.map +1 -0
- package/dist/index.d.mts +6 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.mjs +37 -5
- package/dist/index.mjs.map +1 -1
- package/dist/indexing.d.mts +152 -0
- package/dist/indexing.d.mts.map +1 -0
- package/dist/indexing.d.ts +5 -0
- package/dist/indexing.d.ts.map +1 -0
- package/dist/indexing.js +7 -0
- package/dist/indexing.mjs +255 -0
- package/dist/indexing.mjs.map +1 -0
- package/dist/json-whitespace-view.d.ts +32 -0
- package/dist/json-whitespace-view.d.ts.map +1 -0
- package/dist/json-whitespace-view.js +152 -0
- package/dist/middleware.d.ts +20 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +1031 -0
- package/dist/node.d.mts +28 -126
- package/dist/node.d.mts.map +1 -1
- package/dist/node.d.ts +25 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +24 -0
- package/dist/node.mjs +199 -2890
- package/dist/node.mjs.map +1 -1
- package/dist/reader.d.mts +15 -0
- package/dist/reader.d.mts.map +1 -0
- package/dist/reader.d.ts +15 -0
- package/dist/reader.d.ts.map +1 -0
- package/dist/reader.js +13 -0
- package/dist/reader.mjs +22 -0
- package/dist/reader.mjs.map +1 -0
- package/dist/schemas/index.d.mts +2 -90
- package/dist/schemas/index.d.ts +102 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +87 -0
- package/dist/schemas/index.mjs +2 -70
- package/dist/schemas-CFRD0FIQ.mjs +84 -0
- package/dist/schemas-CFRD0FIQ.mjs.map +1 -0
- package/dist/{types-BUG0Cfkg.mjs → site-8oaj1Q60.mjs} +20 -222
- package/dist/site-8oaj1Q60.mjs.map +1 -0
- package/dist/site-index-B_254bCc.d.mts +136 -0
- package/dist/site-index-B_254bCc.d.mts.map +1 -0
- package/dist/site-index.d.ts +60 -0
- package/dist/site-index.d.ts.map +1 -0
- package/dist/site-index.js +287 -0
- package/dist/site.d.ts +21 -0
- package/dist/site.d.ts.map +1 -0
- package/dist/site.js +168 -0
- package/dist/types-DOdldbfD.mjs +34 -0
- package/dist/types-DOdldbfD.mjs.map +1 -0
- package/dist/types-DjfhS272.d.mts +456 -0
- package/dist/types-DjfhS272.d.mts.map +1 -0
- package/dist/types.d.ts +453 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +36 -0
- package/dist/url-safe-base64.d.ts +12 -0
- package/dist/url-safe-base64.d.ts.map +1 -0
- package/dist/url-safe-base64.js +40 -0
- package/dist/url-validation.d.ts +14 -0
- package/dist/url-validation.d.ts.map +1 -0
- package/dist/url-validation.js +81 -0
- package/dist/utils/atomic-write.d.ts +6 -0
- package/dist/utils/atomic-write.d.ts.map +1 -0
- package/dist/utils/atomic-write.js +20 -0
- package/dist/utils.d.ts +46 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +70 -0
- package/dist/vite-plugin.d.ts +12 -0
- package/dist/vite-plugin.d.ts.map +1 -0
- package/dist/vite-plugin.js +29 -0
- package/dist/vxjson-qHrgLGip.mjs +555 -0
- package/dist/vxjson-qHrgLGip.mjs.map +1 -0
- package/dist/vxjson-serialize-geF6qvpn.mjs +34 -0
- package/dist/vxjson-serialize-geF6qvpn.mjs.map +1 -0
- package/dist/vxjson-serialize.d.mts +17 -0
- package/dist/vxjson-serialize.d.mts.map +1 -0
- package/dist/vxjson-serialize.d.ts +14 -0
- package/dist/vxjson-serialize.d.ts.map +1 -0
- package/dist/vxjson-serialize.js +50 -0
- package/dist/vxjson-serialize.mjs +2 -0
- package/dist/vxjson.d.ts +112 -0
- package/dist/vxjson.d.ts.map +1 -0
- package/dist/vxjson.js +391 -0
- package/dist/xxhash.d.ts +10 -0
- package/dist/xxhash.d.ts.map +1 -0
- package/dist/xxhash.js +31 -0
- package/package.json +88 -17
- package/dist/content-operations-BZLfJWqT.d.mts +0 -916
- package/dist/content-operations-BZLfJWqT.d.mts.map +0 -1
- package/dist/schemas/index.d.mts.map +0 -1
- package/dist/schemas/index.mjs.map +0 -1
- package/dist/types-BUG0Cfkg.mjs.map +0 -1
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
import { t as serializeVxjson } from "./vxjson-serialize-geF6qvpn.mjs";
|
|
2
|
+
import { createHash } from "crypto";
|
|
3
|
+
import xxhash from "xxhash-wasm";
|
|
4
|
+
//#region src/json-whitespace-view.ts
|
|
5
|
+
/**
|
|
6
|
+
* Zero-allocation JSON whitespace-skipping stream transform
|
|
7
|
+
* Skips whitespace outside of JSON strings, preserving whitespace inside strings
|
|
8
|
+
* Optimized to process multiple bytes at once when possible
|
|
9
|
+
*/
|
|
10
|
+
var JsonWhitespaceSkippingView = class {
|
|
11
|
+
downstream;
|
|
12
|
+
inString = false;
|
|
13
|
+
escaped = false;
|
|
14
|
+
singleByte = /* @__PURE__ */ new Uint8Array(1);
|
|
15
|
+
constructor(downstream) {
|
|
16
|
+
this.downstream = downstream;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Process a single byte from JSON stream
|
|
20
|
+
* @param byte - The byte to process
|
|
21
|
+
*/
|
|
22
|
+
updateByte(byte) {
|
|
23
|
+
let shouldInclude = false;
|
|
24
|
+
if (this.inString) {
|
|
25
|
+
shouldInclude = true;
|
|
26
|
+
if (this.escaped) this.escaped = false;
|
|
27
|
+
else if (byte === 92) this.escaped = true;
|
|
28
|
+
else if (byte === 34) this.inString = false;
|
|
29
|
+
} else if (byte === 34) {
|
|
30
|
+
this.inString = true;
|
|
31
|
+
shouldInclude = true;
|
|
32
|
+
} else if (byte !== 9 && byte !== 32 && byte !== 10 && byte !== 13) shouldInclude = true;
|
|
33
|
+
if (shouldInclude) {
|
|
34
|
+
this.singleByte[0] = byte;
|
|
35
|
+
this.downstream.update(this.singleByte);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Process a buffer range with simple batching
|
|
40
|
+
* Collects consecutive bytes that should be included
|
|
41
|
+
* @param buffer - The buffer to process
|
|
42
|
+
* @param start - Start position (inclusive)
|
|
43
|
+
* @param end - End position (exclusive)
|
|
44
|
+
*/
|
|
45
|
+
updateBuffer(buffer, start = 0, end = buffer.length) {
|
|
46
|
+
let pos = start;
|
|
47
|
+
let runStart = start;
|
|
48
|
+
while (pos < end) {
|
|
49
|
+
const byte = buffer[pos];
|
|
50
|
+
let shouldInclude = false;
|
|
51
|
+
if (this.inString) {
|
|
52
|
+
shouldInclude = true;
|
|
53
|
+
if (this.escaped) this.escaped = false;
|
|
54
|
+
else if (byte === 92) this.escaped = true;
|
|
55
|
+
else if (byte === 34) this.inString = false;
|
|
56
|
+
else {
|
|
57
|
+
pos++;
|
|
58
|
+
let searchPos = pos;
|
|
59
|
+
while (searchPos < end) {
|
|
60
|
+
const quotePos = buffer.indexOf(34, searchPos);
|
|
61
|
+
if (quotePos === -1 || quotePos >= end) {
|
|
62
|
+
pos = end;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
let backslashCount = 0;
|
|
66
|
+
let checkPos = quotePos - 1;
|
|
67
|
+
while (checkPos >= 0 && buffer[checkPos] === 92) {
|
|
68
|
+
backslashCount++;
|
|
69
|
+
checkPos--;
|
|
70
|
+
}
|
|
71
|
+
if (backslashCount % 2 === 0) {
|
|
72
|
+
pos = quotePos + 1;
|
|
73
|
+
this.inString = false;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
searchPos = quotePos + 1;
|
|
77
|
+
}
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
} else if (byte === 34) {
|
|
81
|
+
this.inString = true;
|
|
82
|
+
shouldInclude = true;
|
|
83
|
+
} else if (byte !== 9 && byte !== 32 && byte !== 10 && byte !== 13) shouldInclude = true;
|
|
84
|
+
if (shouldInclude) pos++;
|
|
85
|
+
else {
|
|
86
|
+
if (pos > runStart) this.downstream.update(buffer.subarray(runStart, pos));
|
|
87
|
+
pos++;
|
|
88
|
+
runStart = pos;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (pos > runStart) this.downstream.update(buffer.subarray(runStart, pos));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Reset the parser state
|
|
95
|
+
*/
|
|
96
|
+
reset() {
|
|
97
|
+
this.inString = false;
|
|
98
|
+
this.escaped = false;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/url-safe-base64.ts
|
|
103
|
+
/**
|
|
104
|
+
* Convert bytes to URL-safe base64
|
|
105
|
+
* Replaces + with -, / with _, and removes = padding
|
|
106
|
+
*/
|
|
107
|
+
function toUrlSafeBase64(bytes) {
|
|
108
|
+
const b64 = Buffer.from(bytes).toString("base64");
|
|
109
|
+
const buf = Buffer.from(b64, "ascii");
|
|
110
|
+
let write = 0;
|
|
111
|
+
for (let read = 0; read < buf.length; read++) {
|
|
112
|
+
const c = buf[read];
|
|
113
|
+
if (c === 43) buf[write++] = 45;
|
|
114
|
+
else if (c === 47) buf[write++] = 95;
|
|
115
|
+
else if (c === 61) {} else buf[write++] = c;
|
|
116
|
+
}
|
|
117
|
+
return buf.subarray(0, write).toString("ascii");
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Convert BigInt to URL-safe base64
|
|
121
|
+
* @param value - The BigInt value to convert
|
|
122
|
+
* @returns URL-safe base64 string representation
|
|
123
|
+
*/
|
|
124
|
+
function bigintToUrlSafeBase64(value) {
|
|
125
|
+
const hashBuffer = /* @__PURE__ */ new Uint8Array(8);
|
|
126
|
+
new DataView(hashBuffer.buffer).setBigUint64(0, value, false);
|
|
127
|
+
return toUrlSafeBase64(hashBuffer);
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/xxhash.ts
|
|
131
|
+
/**
|
|
132
|
+
* xxHash-64 for content etags — the single hashing chokepoint for this package.
|
|
133
|
+
*
|
|
134
|
+
* Uses `xxhash-wasm`, the one xxHash build with a purpose-built `workerd` export
|
|
135
|
+
* (the WASM is imported as a module, not instantiated from an ArrayBuffer —
|
|
136
|
+
* Cloudflare forbids the latter). It runs identically in workerd, the browser
|
|
137
|
+
* (SPA editor), and Node/bun, so every consumer shares one implementation with
|
|
138
|
+
* no per-target alias or shim.
|
|
139
|
+
*
|
|
140
|
+
* The WASM instance is created ONCE via top-level await, so the exported hashers
|
|
141
|
+
* are SYNCHRONOUS — the etag pipeline (`JsonWhitespaceSkippingView`) streams
|
|
142
|
+
* bytes through `update()` and reads `digest()` synchronously, and ESM guarantees
|
|
143
|
+
* this module is fully evaluated (WASM ready) before any importer runs.
|
|
144
|
+
*
|
|
145
|
+
* This module performs exactly one top-level WASM instantiation, avoiding
|
|
146
|
+
* workerd issue #1529, which requires two top-level WASM instantiations.
|
|
147
|
+
*
|
|
148
|
+
* XXH64, not XXH3 (xxhash-wasm ships XXH32/64): etags are opaque cache
|
|
149
|
+
* validators, so the exact xxHash variant is irrelevant — only speed and a
|
|
150
|
+
* stable 64-bit fingerprint matter.
|
|
151
|
+
*/
|
|
152
|
+
const api = await xxhash();
|
|
153
|
+
/** Stateful streaming XXH64 hasher (seed 0). */
|
|
154
|
+
function createXxh64() {
|
|
155
|
+
return api.create64(0n);
|
|
156
|
+
}
|
|
157
|
+
/** One-shot XXH64 of a buffer (seed 0). */
|
|
158
|
+
function xxh64(input) {
|
|
159
|
+
return api.h64Raw(input);
|
|
160
|
+
}
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/etag-utils.ts
|
|
163
|
+
/**
|
|
164
|
+
* Calculate content ETag using SHA256 from buffer view
|
|
165
|
+
* @param buffer - The file buffer view
|
|
166
|
+
* @param start - Start position (inclusive)
|
|
167
|
+
* @param end - End position (exclusive), or -1 for entire buffer
|
|
168
|
+
*/
|
|
169
|
+
function calculateContentEtagFromBuffer(buffer, start = 0, end = -1) {
|
|
170
|
+
const actualEnd = end === -1 ? buffer.length : end;
|
|
171
|
+
const hasher = createHash("sha256");
|
|
172
|
+
new JsonWhitespaceSkippingView(hasher).updateBuffer(buffer, start, actualEnd);
|
|
173
|
+
return toUrlSafeBase64(hasher.digest());
|
|
174
|
+
}
|
|
175
|
+
const dashCode = "-".charCodeAt(0);
|
|
176
|
+
const newlineCode = "\n".charCodeAt(0);
|
|
177
|
+
/**
|
|
178
|
+
* Find the content start position in MDX buffer (after second ---)
|
|
179
|
+
* @param buffer - The MDX content buffer
|
|
180
|
+
* @returns Position where content starts, or -1 if not found
|
|
181
|
+
*/
|
|
182
|
+
function findMdxContentStartPosition(buffer) {
|
|
183
|
+
let dashCount = 0;
|
|
184
|
+
let i = 0;
|
|
185
|
+
while (i < buffer.length - 4) if (buffer[i] === dashCode && buffer[i + 1] === dashCode && buffer[i + 2] === dashCode && buffer[i + 3] === newlineCode) {
|
|
186
|
+
dashCount++;
|
|
187
|
+
if (dashCount === 2) {
|
|
188
|
+
i += 4;
|
|
189
|
+
if (i < buffer.length && buffer[i] === newlineCode) return i + 1;
|
|
190
|
+
return i;
|
|
191
|
+
}
|
|
192
|
+
i += 4;
|
|
193
|
+
} else {
|
|
194
|
+
const nextDashPos = buffer.indexOf(dashCode, i + 1);
|
|
195
|
+
if (nextDashPos === -1) break;
|
|
196
|
+
i = nextDashPos;
|
|
197
|
+
}
|
|
198
|
+
return -1;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Calculate both ETags from an MDX buffer
|
|
202
|
+
*
|
|
203
|
+
* MDX files use YAML frontmatter which has different characteristics than JSON:
|
|
204
|
+
* - YAML does not require quotes around keys or string values
|
|
205
|
+
* - YAML allows multiple formatting styles (flow vs block style)
|
|
206
|
+
* - YAML field order is not significant for parsing
|
|
207
|
+
*
|
|
208
|
+
* Unlike VXJSON which requires "content" to be the last field and needs special
|
|
209
|
+
* parsing to validate structure, YAML frontmatter can be hashed as-is because:
|
|
210
|
+
* 1. The frontmatter boundaries are clearly delimited by ---
|
|
211
|
+
* 2. Different YAML formatting of the same data SHOULD produce different hashes
|
|
212
|
+
* (e.g., flow style [a,b] vs block style with dashes)
|
|
213
|
+
* 3. There's no requirement to validate field ordering
|
|
214
|
+
*
|
|
215
|
+
* This means we can directly hash the YAML bytes without any whitespace
|
|
216
|
+
* normalization or parsing, which is both simpler and more correct.
|
|
217
|
+
*
|
|
218
|
+
* @param buffer - The complete file buffer
|
|
219
|
+
* @param contentStartPos - Position where content starts (after second ---), or -1 if no content
|
|
220
|
+
*/
|
|
221
|
+
function calculateEtagsFromMdxBuffer(buffer, contentStartPos) {
|
|
222
|
+
if (contentStartPos === -1) {
|
|
223
|
+
const hasher = createXxh64();
|
|
224
|
+
hasher.update(buffer);
|
|
225
|
+
return {
|
|
226
|
+
metaEtag: bigintToUrlSafeBase64(hasher.digest()),
|
|
227
|
+
contentEtag: calculateContentEtagFromBuffer(/* @__PURE__ */ new Uint8Array(0))
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const metaHasher = createXxh64();
|
|
231
|
+
metaHasher.update(buffer.subarray(0, contentStartPos));
|
|
232
|
+
const metaEtag = bigintToUrlSafeBase64(metaHasher.digest());
|
|
233
|
+
const contentHasher = createHash("sha256");
|
|
234
|
+
contentHasher.update(buffer.subarray(contentStartPos));
|
|
235
|
+
return {
|
|
236
|
+
metaEtag,
|
|
237
|
+
contentEtag: toUrlSafeBase64(contentHasher.digest())
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Parse dual ETags from query string: "meta.content"
|
|
242
|
+
*/
|
|
243
|
+
function parseDualEtag(etag) {
|
|
244
|
+
const parts = etag.split(".");
|
|
245
|
+
if (parts.length !== 2) return null;
|
|
246
|
+
return {
|
|
247
|
+
meta: parts[0],
|
|
248
|
+
content: parts[1]
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/vxjson.ts
|
|
253
|
+
const textDecoder = new TextDecoder();
|
|
254
|
+
const CONTENT_MARKER = new TextEncoder().encode("\"content\":");
|
|
255
|
+
/**
|
|
256
|
+
* Calculate metadata ETag using xxHash from buffer view
|
|
257
|
+
* @param buffer - The file buffer view
|
|
258
|
+
* @param start - Start position (inclusive)
|
|
259
|
+
* @param end - End position (exclusive), or -1 for entire buffer
|
|
260
|
+
*/
|
|
261
|
+
function calculateMetaEtagFromBuffer(buffer, start = 0, end = -1) {
|
|
262
|
+
const actualEnd = end === -1 ? buffer.length : end;
|
|
263
|
+
const hasher = createXxh64();
|
|
264
|
+
new JsonWhitespaceSkippingView(hasher).updateBuffer(buffer, start, actualEnd);
|
|
265
|
+
return bigintToUrlSafeBase64(hasher.digest());
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Calculate both ETags from a VXJSON buffer using views
|
|
269
|
+
* Finds the "content" field position and validates it's the last field
|
|
270
|
+
* @param buffer - The complete file buffer
|
|
271
|
+
* @returns Result with ETags or validation error
|
|
272
|
+
*/
|
|
273
|
+
function calculateEtagsFromVXJSONBuffer(buffer, allowTruncated = false) {
|
|
274
|
+
let pos = 0;
|
|
275
|
+
let depth = 0;
|
|
276
|
+
let inString = false;
|
|
277
|
+
let escaped = false;
|
|
278
|
+
let contentStartPos = -1;
|
|
279
|
+
let foundContentAtRoot = false;
|
|
280
|
+
let expectingKey = false;
|
|
281
|
+
let expectingValue = false;
|
|
282
|
+
let keyStart = -1;
|
|
283
|
+
while (pos < buffer.length && (buffer[pos] === 32 || buffer[pos] === 9 || buffer[pos] === 10 || buffer[pos] === 13)) pos++;
|
|
284
|
+
if (pos >= buffer.length || buffer[pos] !== 123) return {
|
|
285
|
+
success: false,
|
|
286
|
+
error: "invalid_json"
|
|
287
|
+
};
|
|
288
|
+
depth = 1;
|
|
289
|
+
expectingKey = true;
|
|
290
|
+
pos++;
|
|
291
|
+
while (pos < buffer.length && depth > 0) {
|
|
292
|
+
const byte = buffer[pos];
|
|
293
|
+
if (inString) {
|
|
294
|
+
if (escaped) escaped = false;
|
|
295
|
+
else if (byte === 92) escaped = true;
|
|
296
|
+
else if (byte === 34) {
|
|
297
|
+
inString = false;
|
|
298
|
+
if (depth === 1 && expectingKey && keyStart >= 0) {
|
|
299
|
+
const keyLength = pos - keyStart + 1;
|
|
300
|
+
if (foundContentAtRoot) {
|
|
301
|
+
if (keyLength === 9) {
|
|
302
|
+
let isContent = true;
|
|
303
|
+
for (let i = 0; i < 7; i++) if (buffer[keyStart + 1 + i] !== CONTENT_MARKER[i + 1]) {
|
|
304
|
+
isContent = false;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
if (isContent) return {
|
|
308
|
+
success: false,
|
|
309
|
+
error: "invalid_json"
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
success: false,
|
|
314
|
+
error: "content_not_last"
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
if (keyLength === 9) {
|
|
318
|
+
let isContent = true;
|
|
319
|
+
for (let i = 0; i < 7; i++) if (buffer[keyStart + 1 + i] !== CONTENT_MARKER[i + 1]) {
|
|
320
|
+
isContent = false;
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
if (isContent) {
|
|
324
|
+
foundContentAtRoot = true;
|
|
325
|
+
contentStartPos = keyStart;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
expectingKey = false;
|
|
329
|
+
expectingValue = true;
|
|
330
|
+
keyStart = -1;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
} else if (byte === 34) {
|
|
334
|
+
inString = true;
|
|
335
|
+
if (depth === 1 && expectingKey) keyStart = pos;
|
|
336
|
+
} else if (byte === 123 || byte === 91) {
|
|
337
|
+
depth++;
|
|
338
|
+
if (depth === 1 && expectingValue) expectingValue = false;
|
|
339
|
+
} else if (byte === 125 || byte === 93) {
|
|
340
|
+
depth--;
|
|
341
|
+
if (depth === 0) break;
|
|
342
|
+
} else if (byte === 58 && depth === 1) {
|
|
343
|
+
if (!expectingValue) return {
|
|
344
|
+
success: false,
|
|
345
|
+
error: "invalid_json"
|
|
346
|
+
};
|
|
347
|
+
} else if (byte === 44 && depth === 1) {
|
|
348
|
+
expectingKey = true;
|
|
349
|
+
expectingValue = false;
|
|
350
|
+
} else if (depth === 1 && expectingValue && byte !== 32 && byte !== 9 && byte !== 10 && byte !== 13) expectingValue = false;
|
|
351
|
+
pos++;
|
|
352
|
+
}
|
|
353
|
+
const isTruncated = depth !== 0;
|
|
354
|
+
if (isTruncated && !allowTruncated) return {
|
|
355
|
+
success: false,
|
|
356
|
+
error: "invalid_json"
|
|
357
|
+
};
|
|
358
|
+
if (contentStartPos === -1) return {
|
|
359
|
+
success: true,
|
|
360
|
+
metaEtag: calculateMetaEtagFromBuffer(buffer),
|
|
361
|
+
contentStartPos: -1
|
|
362
|
+
};
|
|
363
|
+
if (isTruncated) return {
|
|
364
|
+
success: true,
|
|
365
|
+
metaEtag: calculateMetaEtagFromBuffer(buffer, 0, contentStartPos),
|
|
366
|
+
contentStartPos
|
|
367
|
+
};
|
|
368
|
+
let metaEnd = contentStartPos - 1;
|
|
369
|
+
while (metaEnd > 0) {
|
|
370
|
+
const byte = buffer[metaEnd];
|
|
371
|
+
if (byte === 44 || byte === 32 || byte === 9 || byte === 10 || byte === 13) metaEnd--;
|
|
372
|
+
else break;
|
|
373
|
+
}
|
|
374
|
+
const metaHasher = createXxh64();
|
|
375
|
+
const metaView = new JsonWhitespaceSkippingView(metaHasher);
|
|
376
|
+
const openBrace = new Uint8Array([123]);
|
|
377
|
+
const closeBrace = new Uint8Array([125]);
|
|
378
|
+
metaHasher.update(openBrace);
|
|
379
|
+
metaView.updateBuffer(buffer, 1, metaEnd);
|
|
380
|
+
metaHasher.update(closeBrace);
|
|
381
|
+
const metaEtag = bigintToUrlSafeBase64(metaHasher.digest());
|
|
382
|
+
const contentHasher = createHash("sha256");
|
|
383
|
+
const contentView = new JsonWhitespaceSkippingView(contentHasher);
|
|
384
|
+
contentHasher.update(openBrace);
|
|
385
|
+
contentView.updateBuffer(buffer, contentStartPos, buffer.length - 1);
|
|
386
|
+
contentHasher.update(closeBrace);
|
|
387
|
+
return {
|
|
388
|
+
success: true,
|
|
389
|
+
metaEtag,
|
|
390
|
+
contentEtag: toUrlSafeBase64(contentHasher.digest()),
|
|
391
|
+
contentStartPos
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* VXJSON (Variant JSON) module for parsing and serializing VXJSON files
|
|
396
|
+
*
|
|
397
|
+
* VXJSON is a specialized JSON format that guarantees:
|
|
398
|
+
* 1. The root is always an object (never an array or primitive)
|
|
399
|
+
* 2. The "content" field always comes last in the object
|
|
400
|
+
* 3. Field order is deterministic for consistent ETag calculation
|
|
401
|
+
* 4. Metadata fits within 4085 bytes, ensuring "content" field is found within first 4KB
|
|
402
|
+
* 5. First 4KB must either be complete JSON OR end exactly with '"content":'
|
|
403
|
+
*
|
|
404
|
+
* This format enables efficient 4KB parsing where only the first 4KB of a file
|
|
405
|
+
* needs to be read to extract all metadata and locate the content position.
|
|
406
|
+
* The constraint that 4KB reads must end with '"content":' ensures reliable parsing.
|
|
407
|
+
*
|
|
408
|
+
* This module centralizes all VXJSON-specific logic including:
|
|
409
|
+
* - Parsing partial buffers (first 4KB for indexing)
|
|
410
|
+
* - Finding content position for ETag calculation
|
|
411
|
+
* - Serializing with proper field ordering and size validation
|
|
412
|
+
* - Calculating ETags from buffers
|
|
413
|
+
*/
|
|
414
|
+
/**
|
|
415
|
+
* VXJSON static utilities for parsing and validating VXJSON files
|
|
416
|
+
*/
|
|
417
|
+
var VXJSON = class VXJSON {
|
|
418
|
+
/**
|
|
419
|
+
* Parse first 4KB of VXJSON content from buffer
|
|
420
|
+
* Used during indexing to extract metadata without loading full content
|
|
421
|
+
*
|
|
422
|
+
* VXJSON format requirement: first 4KB must either be complete JSON OR end with '"content":'
|
|
423
|
+
* This ensures reliable parsing and validation.
|
|
424
|
+
*/
|
|
425
|
+
static parse4KB(buffer, bytesRead) {
|
|
426
|
+
if (bytesRead < 4096 || buffer[bytesRead - 1] === 125) {
|
|
427
|
+
const partialContent = textDecoder.decode(buffer.subarray(0, bytesRead));
|
|
428
|
+
const data = JSON.parse(partialContent);
|
|
429
|
+
let contentStartPos = -1;
|
|
430
|
+
if (data.content !== void 0) {
|
|
431
|
+
const etagResult = calculateEtagsFromVXJSONBuffer(buffer.subarray(0, bytesRead));
|
|
432
|
+
if (etagResult.success) contentStartPos = etagResult.contentStartPos;
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
id: data.id,
|
|
436
|
+
created: data.created,
|
|
437
|
+
modified: data.modified,
|
|
438
|
+
publishAt: data.publishAt,
|
|
439
|
+
unpublishAt: data.unpublishAt,
|
|
440
|
+
previousPathnames: data.previousPathnames,
|
|
441
|
+
pathname: data.pathname,
|
|
442
|
+
name: data.name,
|
|
443
|
+
meta: data.meta || {},
|
|
444
|
+
content: data.content,
|
|
445
|
+
contentStartPos
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
const etagResult = calculateEtagsFromVXJSONBuffer(buffer.subarray(0, bytesRead), true);
|
|
449
|
+
if (!etagResult.success) throw new Error(`Invalid VXJSON format: ${etagResult.error}. This violates the VXJSON specification.`);
|
|
450
|
+
const contentStartPos = etagResult.contentStartPos;
|
|
451
|
+
let metadataEnd = contentStartPos;
|
|
452
|
+
while (metadataEnd > 0) {
|
|
453
|
+
const byte = buffer[metadataEnd - 1];
|
|
454
|
+
if (byte === 44) {
|
|
455
|
+
metadataEnd--;
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
if (byte === 32 || byte === 9 || byte === 10 || byte === 13) metadataEnd--;
|
|
459
|
+
else break;
|
|
460
|
+
}
|
|
461
|
+
const metadataBuffer = new Uint8Array(metadataEnd + 1);
|
|
462
|
+
metadataBuffer.set(buffer.subarray(0, metadataEnd));
|
|
463
|
+
metadataBuffer[metadataEnd] = 125;
|
|
464
|
+
const partialContent = textDecoder.decode(metadataBuffer);
|
|
465
|
+
const data = JSON.parse(partialContent);
|
|
466
|
+
return {
|
|
467
|
+
id: data.id,
|
|
468
|
+
created: data.created,
|
|
469
|
+
modified: data.modified,
|
|
470
|
+
publishAt: data.publishAt,
|
|
471
|
+
unpublishAt: data.unpublishAt,
|
|
472
|
+
previousPathnames: data.previousPathnames,
|
|
473
|
+
pathname: data.pathname,
|
|
474
|
+
name: data.name,
|
|
475
|
+
meta: data.meta || {},
|
|
476
|
+
contentStartPos
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Find the position of the "content" field in a VXJSON buffer
|
|
481
|
+
* Returns -1 if not found
|
|
482
|
+
*/
|
|
483
|
+
static findContentPosition(buffer) {
|
|
484
|
+
const etagResult = calculateEtagsFromVXJSONBuffer(buffer);
|
|
485
|
+
if (etagResult.success) return etagResult.contentStartPos;
|
|
486
|
+
return -1;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Calculate ETags for a VXJSON buffer
|
|
490
|
+
* Returns null if the buffer is not valid VXJSON
|
|
491
|
+
*/
|
|
492
|
+
static calculateEtagsOnly(buffer) {
|
|
493
|
+
const result = calculateEtagsFromVXJSONBuffer(buffer);
|
|
494
|
+
if (!result.success) return null;
|
|
495
|
+
return {
|
|
496
|
+
metaEtag: result.metaEtag,
|
|
497
|
+
contentEtag: result.contentEtag
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Create a dual ETag string from a VXJSON buffer
|
|
502
|
+
* Returns null if the buffer is not valid VXJSON
|
|
503
|
+
*/
|
|
504
|
+
static createEtag(buffer) {
|
|
505
|
+
const etags = VXJSON.calculateEtagsOnly(buffer);
|
|
506
|
+
if (!etags) return null;
|
|
507
|
+
return etags.contentEtag ? `${etags.metaEtag}.${etags.contentEtag}` : etags.metaEtag;
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Validate that a VXJSON buffer has the correct structure
|
|
511
|
+
* (content field must be last)
|
|
512
|
+
*/
|
|
513
|
+
static isValid(buffer) {
|
|
514
|
+
return calculateEtagsFromVXJSONBuffer(buffer).success;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Parse a complete VXJSON file from string
|
|
518
|
+
*/
|
|
519
|
+
static parse(content) {
|
|
520
|
+
return JSON.parse(content);
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Parse a complete VXJSON file from buffer
|
|
524
|
+
*/
|
|
525
|
+
static parseBuffer(buffer) {
|
|
526
|
+
const content = textDecoder.decode(buffer);
|
|
527
|
+
return VXJSON.parse(content);
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Calculate both ETags from a VXJSON buffer using views
|
|
531
|
+
* Finds the "content" field position and validates it's the last field
|
|
532
|
+
* @param buffer - The complete file buffer
|
|
533
|
+
* @returns Result with ETags or validation error
|
|
534
|
+
*/
|
|
535
|
+
static calculateETags = calculateEtagsFromVXJSONBuffer;
|
|
536
|
+
/**
|
|
537
|
+
* Calculate a simple single etag from the entire buffer
|
|
538
|
+
* Used for data files that don't follow the content-last convention
|
|
539
|
+
*/
|
|
540
|
+
static calculateSimpleEtag(buffer) {
|
|
541
|
+
return calculateMetaEtagFromBuffer(buffer);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Serialize a VXJSONFile to VXJSON format string. Delegates to the pure
|
|
545
|
+
* vxjson-serialize module — kept apart so spec-14 cores can import the
|
|
546
|
+
* serializer without this module's crypto/WASM ETag imports.
|
|
547
|
+
*/
|
|
548
|
+
static serialize(data) {
|
|
549
|
+
return serializeVxjson(data);
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
//#endregion
|
|
553
|
+
export { xxh64 as a, parseDualEtag as i, calculateEtagsFromMdxBuffer as n, findMdxContentStartPosition as r, VXJSON as t };
|
|
554
|
+
|
|
555
|
+
//# sourceMappingURL=vxjson-qHrgLGip.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vxjson-qHrgLGip.mjs","names":[],"sources":["../src/json-whitespace-view.ts","../src/url-safe-base64.ts","../src/xxhash.ts","../src/etag-utils.ts","../src/vxjson.ts"],"sourcesContent":["/**\n * Zero-allocation JSON whitespace-skipping stream transform\n * Skips whitespace outside of JSON strings, preserving whitespace inside strings\n * Optimized to process multiple bytes at once when possible\n */\nexport class JsonWhitespaceSkippingView {\n private inString = false;\n private escaped = false;\n private singleByte = new Uint8Array(1); // Reused allocation for single bytes\n\n constructor(private downstream: { update(data: Uint8Array): void }) {}\n\n /**\n * Process a single byte from JSON stream\n * @param byte - The byte to process\n */\n updateByte(byte: number): void {\n let shouldInclude = false;\n\n if (this.inString) {\n // Inside string: always include the byte\n shouldInclude = true;\n\n if (this.escaped) {\n this.escaped = false;\n } else if (byte === 0x5c) {\n // backslash\n this.escaped = true;\n } else if (byte === 0x22) {\n // quote\n this.inString = false;\n }\n } else {\n // Outside string: check for string start or skip whitespace\n if (byte === 0x22) {\n // 0x22 is the double quote character (\")\n // quote\n this.inString = true;\n shouldInclude = true;\n }\n // Check tab (0x09) first as it's the most common whitespace in our tab-indented VXJSON files\n // Then space (0x20), newline (0x0a), carriage return (0x0d)\n else if (byte !== 0x09 && byte !== 0x20 && byte !== 0x0a && byte !== 0x0d) {\n // Not whitespace, include it\n shouldInclude = true;\n }\n // Skip whitespace outside strings\n }\n\n if (shouldInclude) {\n this.singleByte[0] = byte;\n this.downstream.update(this.singleByte);\n }\n }\n\n /**\n * Process a buffer range with simple batching\n * Collects consecutive bytes that should be included\n * @param buffer - The buffer to process\n * @param start - Start position (inclusive)\n * @param end - End position (exclusive)\n */\n updateBuffer(buffer: Uint8Array, start = 0, end = buffer.length): void {\n let pos = start;\n let runStart = start;\n\n while (pos < end) {\n const byte = buffer[pos];\n let shouldInclude = false;\n\n if (this.inString) {\n // Inside string: always include\n shouldInclude = true;\n\n if (this.escaped) {\n this.escaped = false;\n } else if (byte === 0x5c) {\n // backslash\n this.escaped = true;\n } else if (byte === 0x22) {\n // quote - end of string\n this.inString = false;\n } else {\n // Regular character - use indexOf to skip to next quote\n pos++;\n let searchPos = pos;\n\n while (searchPos < end) {\n const quotePos = buffer.indexOf(0x22, searchPos);\n if (quotePos === -1 || quotePos >= end) {\n // No more quotes\n pos = end;\n break;\n }\n\n // Count consecutive backslashes before the quote\n let backslashCount = 0;\n let checkPos = quotePos - 1;\n while (checkPos >= 0 && buffer[checkPos] === 0x5c) {\n backslashCount++;\n checkPos--;\n }\n\n if (backslashCount % 2 === 0) {\n // Even backslashes (including 0) - quote is not escaped\n pos = quotePos + 1;\n this.inString = false;\n break;\n }\n // Odd backslashes - quote is escaped, continue searching\n searchPos = quotePos + 1;\n }\n continue; // Skip the pos++ at the bottom\n }\n } else {\n // Outside string\n if (byte === 0x22) {\n // quote - start of string\n this.inString = true;\n shouldInclude = true;\n // Check tab (0x09) first as it's the most common whitespace in our tab-indented VXJSON files\n // Then space (0x20), newline (0x0a), carriage return (0x0d)\n } else if (byte !== 0x09 && byte !== 0x20 && byte !== 0x0a && byte !== 0x0d) {\n // Not whitespace\n shouldInclude = true;\n }\n }\n\n if (shouldInclude) {\n // Continue the run\n pos++;\n } else {\n // End of run - send what we have\n if (pos > runStart) {\n this.downstream.update(buffer.subarray(runStart, pos));\n }\n pos++;\n runStart = pos;\n }\n }\n\n // Send any remaining bytes\n if (pos > runStart) {\n this.downstream.update(buffer.subarray(runStart, pos));\n }\n }\n\n /**\n * Reset the parser state\n */\n reset(): void {\n this.inString = false;\n this.escaped = false;\n }\n}\n","/**\n * Convert bytes to URL-safe base64\n * Replaces + with -, / with _, and removes = padding\n */\nexport function toUrlSafeBase64(bytes: Uint8Array): string {\n const b64 = Buffer.from(bytes).toString('base64');\n const buf = Buffer.from(b64, 'ascii');\n let write = 0;\n for (let read = 0; read < buf.length; read++) {\n const c = buf[read];\n if (c === 0x2b) {\n // '+'\n buf[write++] = 0x2d; // '-'\n } else if (c === 0x2f) {\n // '/'\n buf[write++] = 0x5f; // '_'\n } else if (c === 0x3d) {\n // '='\n // skip padding\n } else {\n buf[write++] = c;\n }\n }\n return buf.subarray(0, write).toString('ascii');\n}\n\n/**\n * Convert BigInt to URL-safe base64\n * @param value - The BigInt value to convert\n * @returns URL-safe base64 string representation\n */\nexport function bigintToUrlSafeBase64(value: bigint): string {\n // Convert BigInt to 8-byte buffer\n const hashBuffer = new Uint8Array(8);\n const dataView = new DataView(hashBuffer.buffer);\n dataView.setBigUint64(0, value, false); // big-endian\n return toUrlSafeBase64(hashBuffer);\n}\n","/**\n * xxHash-64 for content etags — the single hashing chokepoint for this package.\n *\n * Uses `xxhash-wasm`, the one xxHash build with a purpose-built `workerd` export\n * (the WASM is imported as a module, not instantiated from an ArrayBuffer —\n * Cloudflare forbids the latter). It runs identically in workerd, the browser\n * (SPA editor), and Node/bun, so every consumer shares one implementation with\n * no per-target alias or shim.\n *\n * The WASM instance is created ONCE via top-level await, so the exported hashers\n * are SYNCHRONOUS — the etag pipeline (`JsonWhitespaceSkippingView`) streams\n * bytes through `update()` and reads `digest()` synchronously, and ESM guarantees\n * this module is fully evaluated (WASM ready) before any importer runs.\n *\n * This module performs exactly one top-level WASM instantiation, avoiding\n * workerd issue #1529, which requires two top-level WASM instantiations.\n *\n * XXH64, not XXH3 (xxhash-wasm ships XXH32/64): etags are opaque cache\n * validators, so the exact xxHash variant is irrelevant — only speed and a\n * stable 64-bit fingerprint matter.\n */\nimport xxhash from 'xxhash-wasm';\n\n/** Streaming XXH64 hasher: the `update`/`digest` contract the etag pipeline drives. */\nexport interface Xxh64Hasher {\n update(input: Uint8Array): Xxh64Hasher;\n digest(): bigint;\n}\n\nconst api = await xxhash();\n\n/** Stateful streaming XXH64 hasher (seed 0). */\nexport function createXxh64(): Xxh64Hasher {\n return api.create64(0n);\n}\n\n/** One-shot XXH64 of a buffer (seed 0). */\nexport function xxh64(input: Uint8Array): bigint {\n return api.h64Raw(input);\n}\n","import { createHash } from 'crypto';\nimport { JsonWhitespaceSkippingView } from './json-whitespace-view';\nimport { bigintToUrlSafeBase64, toUrlSafeBase64 } from './url-safe-base64';\nimport { createXxh64 } from './xxhash';\n\n/**\n * Dual ETag system for content API\n * - Meta ETag: Based on non-derived LocaleManifest fields (using XXH64)\n * - Content ETag: Based on ContentProp.content (using SHA256)\n * Both skip whitespace to allow pretty-printing without affecting hashes\n *\n * Format: \"metaEtag.contentEtag\" in query strings\n */\n\n\n/**\n * Calculate metadata ETag (XXH64) from buffer view\n * @param buffer - The file buffer view\n * @param start - Start position (inclusive)\n * @param end - End position (exclusive), or -1 for entire buffer\n */\nexport function calculateMetaEtagFromBuffer(buffer: Uint8Array, start = 0, end = -1): string {\n const actualEnd = end === -1 ? buffer.length : end;\n const hasher = createXxh64();\n const jsonView = new JsonWhitespaceSkippingView(hasher);\n\n jsonView.updateBuffer(buffer, start, actualEnd);\n\n return bigintToUrlSafeBase64(hasher.digest());\n}\n\n/**\n * Calculate content ETag using SHA256 from buffer view\n * @param buffer - The file buffer view\n * @param start - Start position (inclusive)\n * @param end - End position (exclusive), or -1 for entire buffer\n */\nexport function calculateContentEtagFromBuffer(buffer: Uint8Array, start = 0, end = -1): string {\n const actualEnd = end === -1 ? buffer.length : end;\n const hasher = createHash('sha256');\n const jsonView = new JsonWhitespaceSkippingView(hasher);\n\n jsonView.updateBuffer(buffer, start, actualEnd);\n\n return toUrlSafeBase64(hasher.digest() as Uint8Array);\n}\n\nconst dashCode = '-'.charCodeAt(0); // 45\nconst newlineCode = '\\n'.charCodeAt(0); // 10\n\n/**\n * Find the content start position in MDX buffer (after second ---)\n * @param buffer - The MDX content buffer\n * @returns Position where content starts, or -1 if not found\n */\nexport function findMdxContentStartPosition(buffer: Uint8Array): number {\n let dashCount = 0;\n let i = 0;\n\n // Look for the second occurrence of \"---\\n\"\n while (i < buffer.length - 4) {\n if (\n buffer[i] === dashCode &&\n buffer[i + 1] === dashCode &&\n buffer[i + 2] === dashCode &&\n buffer[i + 3] === newlineCode\n ) {\n dashCount++;\n if (dashCount === 2) {\n // Found second ---, now look for the following newline to get content start\n i += 4; // Skip past \"---\\n\"\n if (i < buffer.length && buffer[i] === newlineCode) {\n return i + 1; // Return position after the empty line\n }\n return i; // Return position right after \"---\\n\"\n }\n i += 4; // Skip this \"---\\n\" and continue looking\n } else {\n // Use indexOf to skip ahead faster instead of incrementing by 1\n const nextDashPos = buffer.indexOf(dashCode, i + 1);\n if (nextDashPos === -1) {\n break; // No more dashes found\n }\n i = nextDashPos;\n }\n }\n\n return -1; // Not found\n}\n\n/**\n * Calculate both ETags from an MDX buffer\n *\n * MDX files use YAML frontmatter which has different characteristics than JSON:\n * - YAML does not require quotes around keys or string values\n * - YAML allows multiple formatting styles (flow vs block style)\n * - YAML field order is not significant for parsing\n *\n * Unlike VXJSON which requires \"content\" to be the last field and needs special\n * parsing to validate structure, YAML frontmatter can be hashed as-is because:\n * 1. The frontmatter boundaries are clearly delimited by ---\n * 2. Different YAML formatting of the same data SHOULD produce different hashes\n * (e.g., flow style [a,b] vs block style with dashes)\n * 3. There's no requirement to validate field ordering\n *\n * This means we can directly hash the YAML bytes without any whitespace\n * normalization or parsing, which is both simpler and more correct.\n *\n * @param buffer - The complete file buffer\n * @param contentStartPos - Position where content starts (after second ---), or -1 if no content\n */\nexport function calculateEtagsFromMdxBuffer(\n buffer: Uint8Array,\n contentStartPos: number,\n): {\n metaEtag: string;\n contentEtag: string;\n} {\n if (contentStartPos === -1) {\n // No content, only frontmatter - hash entire buffer as metadata\n const hasher = createXxh64();\n hasher.update(buffer);\n const metaEtag = bigintToUrlSafeBase64(hasher.digest());\n\n return {\n metaEtag,\n contentEtag: calculateContentEtagFromBuffer(new Uint8Array(0)), // empty content\n };\n }\n\n // For MDX, we hash the YAML frontmatter as-is (including the --- markers)\n // This is different from JSON where we need to parse and validate structure\n const metaHasher = createXxh64();\n metaHasher.update(buffer.subarray(0, contentStartPos));\n const metaEtag = bigintToUrlSafeBase64(metaHasher.digest());\n\n // Content is everything after the second ---\n const contentHasher = createHash('sha256');\n contentHasher.update(buffer.subarray(contentStartPos));\n const contentEtag = toUrlSafeBase64(contentHasher.digest() as Uint8Array);\n\n return {\n metaEtag,\n contentEtag,\n };\n}\n\n/**\n * Combined ETag structure\n */\nexport interface DualEtag {\n meta: string;\n content: string;\n}\n\n/**\n * Format dual ETags for query string: \"meta.content\"\n */\nexport function formatDualEtag(etags: DualEtag): string {\n return `${etags.meta}.${etags.content}`;\n}\n\n/**\n * Parse dual ETags from query string: \"meta.content\"\n */\nexport function parseDualEtag(etag: string): DualEtag | null {\n const parts = etag.split('.');\n if (parts.length !== 2) return null;\n\n return {\n meta: parts[0],\n content: parts[1],\n };\n}\n\n/**\n * Check if etag matches either meta or content\n */\nexport function matchesEtag(\n providedEtag: string,\n currentMeta: string,\n currentContent: string,\n): {\n matches: boolean;\n metaChanged: boolean;\n contentChanged: boolean;\n} {\n const parsed = parseDualEtag(providedEtag);\n if (!parsed) {\n return {\n matches: false,\n metaChanged: true,\n contentChanged: true,\n };\n }\n\n const metaChanged = parsed.meta !== currentMeta;\n const contentChanged = parsed.content !== currentContent;\n\n return {\n matches: !metaChanged && !contentChanged,\n metaChanged,\n contentChanged,\n };\n}\n","import { createHash } from 'crypto';\nimport { JsonWhitespaceSkippingView } from './json-whitespace-view';\nimport type { ContentData, ContentMeta, VXJSONFile } from './types';\nimport { bigintToUrlSafeBase64, toUrlSafeBase64 } from './url-safe-base64';\nimport { serializeVxjson } from './vxjson-serialize';\nimport { createXxh64 } from './xxhash';\n\n// Reusable TextDecoder instance - safe to reuse as it's stateless\nconst textDecoder = new TextDecoder();\n\n// Pre-allocated content marker for efficiency\nconst CONTENT_MARKER = new TextEncoder().encode('\"content\":');\n\n/**\n * Calculate metadata ETag using xxHash from buffer view\n * @param buffer - The file buffer view\n * @param start - Start position (inclusive)\n * @param end - End position (exclusive), or -1 for entire buffer\n */\nfunction calculateMetaEtagFromBuffer(buffer: Uint8Array, start = 0, end = -1): string {\n const actualEnd = end === -1 ? buffer.length : end;\n const hasher = createXxh64();\n const jsonView = new JsonWhitespaceSkippingView(hasher);\n\n jsonView.updateBuffer(buffer, start, actualEnd);\n\n return bigintToUrlSafeBase64(hasher.digest());\n}\n\n/**\n * Calculate both ETags from a VXJSON buffer using views\n * Finds the \"content\" field position and validates it's the last field\n * @param buffer - The complete file buffer\n * @returns Result with ETags or validation error\n */\nfunction calculateEtagsFromVXJSONBuffer(\n buffer: Uint8Array,\n allowTruncated = false,\n):\n | { success: true; metaEtag: string; contentEtag?: string; contentStartPos: number }\n | { success: false; error: 'content_not_last' | 'invalid_json' } {\n let pos = 0;\n let depth = 0;\n let inString = false;\n let escaped = false;\n let contentStartPos = -1;\n let foundContentAtRoot = false;\n let expectingKey = false; // At root level, are we expecting a key?\n let expectingValue = false; // At root level, are we expecting a value?\n let keyStart = -1;\n\n // Skip whitespace and find opening brace\n while (\n pos < buffer.length &&\n (buffer[pos] === 0x20 || buffer[pos] === 0x09 || buffer[pos] === 0x0a || buffer[pos] === 0x0d)\n ) {\n pos++;\n }\n\n if (pos >= buffer.length || buffer[pos] !== 0x7b) {\n // {\n return { success: false, error: 'invalid_json' };\n }\n\n depth = 1;\n expectingKey = true; // After { we expect a key\n pos++;\n\n // Parse JSON structure\n while (pos < buffer.length && depth > 0) {\n const byte = buffer[pos];\n\n if (inString) {\n if (escaped) {\n escaped = false;\n } else if (byte === 0x5c) {\n // backslash\n escaped = true;\n } else if (byte === 0x22) {\n // quote\n inString = false;\n\n // If we were in a root key, check if it's \"content\"\n if (depth === 1 && expectingKey && keyStart >= 0) {\n const keyLength = pos - keyStart + 1; // +1 to include current quote\n\n // First check if we already found content and this is any key after it\n if (foundContentAtRoot) {\n // Check if this is another \"content\" key (duplicate)\n if (keyLength === 9) {\n let isContent = true;\n for (let i = 0; i < 7; i++) {\n if (buffer[keyStart + 1 + i] !== CONTENT_MARKER[i + 1]) {\n isContent = false;\n break;\n }\n }\n if (isContent) {\n // Duplicate content key\n return { success: false, error: 'invalid_json' };\n }\n }\n // Any other key after content is an error\n return { success: false, error: 'content_not_last' };\n }\n\n // Check if this key is \"content\" (7 chars + 2 quotes = 9 total)\n if (keyLength === 9) {\n let isContent = true;\n // Compare \"content\" part (skip opening quote)\n for (let i = 0; i < 7; i++) {\n if (buffer[keyStart + 1 + i] !== CONTENT_MARKER[i + 1]) {\n isContent = false;\n break;\n }\n }\n\n if (isContent) {\n // Found \"content\" at root level for the first time\n foundContentAtRoot = true;\n contentStartPos = keyStart; // Start of the quoted key\n }\n }\n\n expectingKey = false;\n expectingValue = true; // After a key, we expect : then value\n keyStart = -1;\n }\n }\n } else {\n // Not in string\n if (byte === 0x22) {\n // quote\n inString = true;\n\n // Check if this is a root-level key\n if (depth === 1 && expectingKey) {\n keyStart = pos;\n }\n } else if (byte === 0x7b || byte === 0x5b) {\n // { or [\n depth++;\n if (depth === 1 && expectingValue) {\n expectingValue = false; // We got our value\n }\n } else if (byte === 0x7d || byte === 0x5d) {\n // } or ]\n depth--;\n if (depth === 0) {\n break; // Done parsing\n }\n } else if (byte === 0x3a && depth === 1) {\n // : at root level\n if (!expectingValue) {\n return { success: false, error: 'invalid_json' };\n }\n // After : we're still expecting a value\n } else if (byte === 0x2c && depth === 1) {\n // , at root level\n expectingKey = true;\n expectingValue = false;\n } else if (depth === 1 && expectingValue && byte !== 0x20 && byte !== 0x09 && byte !== 0x0a && byte !== 0x0d) {\n // Non-whitespace at root level when expecting value\n // Could be number, true, false, null\n expectingValue = false; // We're consuming the value\n }\n }\n\n pos++;\n }\n\n // For truncated JSON (4KB parsing), we can still succeed if we found the content position\n const isTruncated = depth !== 0;\n\n // If JSON is truncated but we don't allow it, return invalid_json\n if (isTruncated && !allowTruncated) {\n return { success: false, error: 'invalid_json' };\n }\n\n if (contentStartPos === -1) {\n // No content field found - this is valid for both complete and truncated JSON\n return {\n success: true,\n metaEtag: calculateMetaEtagFromBuffer(buffer),\n contentStartPos: -1,\n };\n }\n\n // Found content field - proceed with ETag calculation\n if (isTruncated) {\n // For truncated JSON, we can still calculate metadata ETag up to content position\n return {\n success: true,\n metaEtag: calculateMetaEtagFromBuffer(buffer, 0, contentStartPos),\n contentStartPos,\n // Don't calculate contentEtag for truncated content\n };\n }\n\n // Find the end of metadata (before \"content\":)\n let metaEnd = contentStartPos - 1;\n while (metaEnd > 0) {\n const byte = buffer[metaEnd];\n if (byte === 0x2c || byte === 0x20 || byte === 0x09 || byte === 0x0a || byte === 0x0d) {\n metaEnd--;\n } else {\n break;\n }\n }\n\n // Hash metadata section: wrap in { } and exclude content field\n const metaHasher = createXxh64();\n const metaView = new JsonWhitespaceSkippingView(metaHasher);\n const openBrace = new Uint8Array([0x7b]); // {\n const closeBrace = new Uint8Array([0x7d]); // }\n\n metaHasher.update(openBrace);\n metaView.updateBuffer(buffer, 1, metaEnd); // Skip opening brace, stop before content\n metaHasher.update(closeBrace);\n\n const metaEtag = bigintToUrlSafeBase64(metaHasher.digest());\n\n // Hash content section: wrap as {\"content\": ... }\n const contentHasher = createHash('sha256');\n const contentView = new JsonWhitespaceSkippingView(contentHasher);\n\n contentHasher.update(openBrace); // {\n contentView.updateBuffer(buffer, contentStartPos, buffer.length - 1); // content field to end, exclude final }\n contentHasher.update(closeBrace); // }\n\n const contentEtag = toUrlSafeBase64(contentHasher.digest() as Uint8Array);\n\n return {\n success: true,\n metaEtag,\n contentEtag,\n contentStartPos,\n };\n}\n\n/**\n * VXJSON (Variant JSON) module for parsing and serializing VXJSON files\n *\n * VXJSON is a specialized JSON format that guarantees:\n * 1. The root is always an object (never an array or primitive)\n * 2. The \"content\" field always comes last in the object\n * 3. Field order is deterministic for consistent ETag calculation\n * 4. Metadata fits within 4085 bytes, ensuring \"content\" field is found within first 4KB\n * 5. First 4KB must either be complete JSON OR end exactly with '\"content\":'\n *\n * This format enables efficient 4KB parsing where only the first 4KB of a file\n * needs to be read to extract all metadata and locate the content position.\n * The constraint that 4KB reads must end with '\"content\":' ensures reliable parsing.\n *\n * This module centralizes all VXJSON-specific logic including:\n * - Parsing partial buffers (first 4KB for indexing)\n * - Finding content position for ETag calculation\n * - Serializing with proper field ordering and size validation\n * - Calculating ETags from buffers\n */\n\n/**\n * VXJSON static utilities for parsing and validating VXJSON files\n */\nexport class VXJSON {\n /**\n * Parse first 4KB of VXJSON content from buffer\n * Used during indexing to extract metadata without loading full content\n *\n * VXJSON format requirement: first 4KB must either be complete JSON OR end with '\"content\":'\n * This ensures reliable parsing and validation.\n */\n static parse4KB(\n buffer: Uint8Array,\n bytesRead: number,\n ): {\n id?: string;\n created?: string;\n modified?: string;\n publishAt?: string;\n unpublishAt?: string;\n previousPathnames?: Record<string, string>;\n pathname?: string;\n name?: string;\n meta: ContentMeta;\n content?: ContentData;\n contentStartPos?: number; // Position where \"content\": starts in buffer\n } {\n // Complete JSON (either small file or ends with })\n if (bytesRead < 4096 || buffer[bytesRead - 1] === 0x7d) {\n // 0x7D = '}'\n const partialContent = textDecoder.decode(buffer.subarray(0, bytesRead));\n const data = JSON.parse(partialContent);\n\n // Find content position for ETag calculation\n let contentStartPos = -1;\n if (data.content !== undefined) {\n const etagResult = calculateEtagsFromVXJSONBuffer(buffer.subarray(0, bytesRead));\n if (etagResult.success) {\n contentStartPos = etagResult.contentStartPos;\n }\n }\n\n return {\n id: data.id,\n created: data.created,\n modified: data.modified,\n publishAt: data.publishAt,\n unpublishAt: data.unpublishAt,\n previousPathnames: data.previousPathnames,\n pathname: data.pathname,\n name: data.name,\n meta: data.meta || {},\n content: data.content,\n contentStartPos,\n };\n }\n\n // Truncated 4KB read - use calculateETags to validate and find content position\n const etagResult = calculateEtagsFromVXJSONBuffer(buffer.subarray(0, bytesRead), true); // Allow truncated for 4KB parsing\n\n if (!etagResult.success) {\n throw new Error(`Invalid VXJSON format: ${etagResult.error}. This violates the VXJSON specification.`);\n }\n\n const contentStartPos = etagResult.contentStartPos;\n\n // Find end of metadata by working backwards from content position\n let metadataEnd = contentStartPos;\n while (metadataEnd > 0) {\n const byte = buffer[metadataEnd - 1];\n if (byte === 0x2c) {\n // comma\n metadataEnd--;\n break;\n }\n if (byte === 0x20 || byte === 0x09 || byte === 0x0a || byte === 0x0d) {\n // whitespace\n metadataEnd--;\n } else {\n break;\n }\n }\n\n // Create metadata-only JSON by adding closing brace\n const metadataBuffer = new Uint8Array(metadataEnd + 1);\n metadataBuffer.set(buffer.subarray(0, metadataEnd));\n metadataBuffer[metadataEnd] = 0x7d; // }\n\n const partialContent = textDecoder.decode(metadataBuffer);\n\n const data = JSON.parse(partialContent);\n return {\n id: data.id,\n created: data.created,\n modified: data.modified,\n publishAt: data.publishAt,\n unpublishAt: data.unpublishAt,\n previousPathnames: data.previousPathnames,\n pathname: data.pathname,\n name: data.name,\n meta: data.meta || {},\n contentStartPos,\n };\n }\n\n /**\n * Find the position of the \"content\" field in a VXJSON buffer\n * Returns -1 if not found\n */\n static findContentPosition(buffer: Uint8Array): number {\n const etagResult = calculateEtagsFromVXJSONBuffer(buffer);\n if (etagResult.success) {\n return etagResult.contentStartPos;\n }\n return -1;\n }\n\n /**\n * Calculate ETags for a VXJSON buffer\n * Returns null if the buffer is not valid VXJSON\n */\n static calculateEtagsOnly(buffer: Uint8Array): { metaEtag: string; contentEtag?: string } | null {\n const result = calculateEtagsFromVXJSONBuffer(buffer);\n if (!result.success) {\n return null;\n }\n\n return {\n metaEtag: result.metaEtag,\n contentEtag: result.contentEtag,\n };\n }\n\n /**\n * Create a dual ETag string from a VXJSON buffer\n * Returns null if the buffer is not valid VXJSON\n */\n static createEtag(buffer: Uint8Array): string | null {\n const etags = VXJSON.calculateEtagsOnly(buffer);\n if (!etags) {\n return null;\n }\n\n return etags.contentEtag ? `${etags.metaEtag}.${etags.contentEtag}` : etags.metaEtag;\n }\n\n /**\n * Validate that a VXJSON buffer has the correct structure\n * (content field must be last)\n */\n static isValid(buffer: Uint8Array): boolean {\n const result = calculateEtagsFromVXJSONBuffer(buffer);\n return result.success;\n }\n\n /**\n * Parse a complete VXJSON file from string\n */\n static parse(content: string): VXJSONFile {\n return JSON.parse(content);\n }\n\n /**\n * Parse a complete VXJSON file from buffer\n */\n static parseBuffer(buffer: Uint8Array): VXJSONFile {\n const content = textDecoder.decode(buffer);\n return VXJSON.parse(content);\n }\n\n /**\n * Calculate both ETags from a VXJSON buffer using views\n * Finds the \"content\" field position and validates it's the last field\n * @param buffer - The complete file buffer\n * @returns Result with ETags or validation error\n */\n static calculateETags = calculateEtagsFromVXJSONBuffer;\n\n /**\n * Calculate a simple single etag from the entire buffer\n * Used for data files that don't follow the content-last convention\n */\n static calculateSimpleEtag(buffer: Uint8Array): string {\n return calculateMetaEtagFromBuffer(buffer);\n }\n\n /**\n * Serialize a VXJSONFile to VXJSON format string. Delegates to the pure\n * vxjson-serialize module — kept apart so spec-14 cores can import the\n * serializer without this module's crypto/WASM ETag imports.\n */\n static serialize(data: VXJSONFile): string {\n return serializeVxjson(data);\n }\n}\n"],"mappings":";;;;;;;;;AAKA,IAAa,6BAAb,MAAwC;CAKlB;CAJpB,WAAmB;CACnB,UAAkB;CAClB,6BAAqB,IAAI,WAAW,CAAC;CAErC,YAAY,YAAwD;EAAhD,KAAA,aAAA;CAAiD;;;;;CAMrE,WAAW,MAAoB;EAC7B,IAAI,gBAAgB;EAEpB,IAAI,KAAK,UAAU;GAEjB,gBAAgB;GAEhB,IAAI,KAAK,SACP,KAAK,UAAU;QACV,IAAI,SAAS,IAElB,KAAK,UAAU;QACV,IAAI,SAAS,IAElB,KAAK,WAAW;EAEpB,OAEE,IAAI,SAAS,IAAM;GAGjB,KAAK,WAAW;GAChB,gBAAgB;EAClB,OAGK,IAAI,SAAS,KAAQ,SAAS,MAAQ,SAAS,MAAQ,SAAS,IAEnE,gBAAgB;EAKpB,IAAI,eAAe;GACjB,KAAK,WAAW,KAAK;GACrB,KAAK,WAAW,OAAO,KAAK,UAAU;EACxC;CACF;;;;;;;;CASA,aAAa,QAAoB,QAAQ,GAAG,MAAM,OAAO,QAAc;EACrE,IA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|
+
//#region src/vxjson-serialize.ts
|
|
3
|
+
/**
|
|
4
|
+
* The canonical VXJSON serializer: metadata alphabetical, content last so
|
|
5
|
+
* the indexer reads all metadata from the first 4KB, 4KB position enforced.
|
|
6
|
+
*
|
|
7
|
+
* Lives apart from vxjson.ts on purpose: that module's ETag half imports
|
|
8
|
+
* node crypto and WASM-backed xxhash at module level, which spec 14 §A90
|
|
9
|
+
* bans inside registered functional cores. This file imports only sort-keys
|
|
10
|
+
* and types, so the SaaS Branch DO's commit-plan core can consume it via
|
|
11
|
+
* the dedicated `@conloca/content-api/vxjson-serialize` subpath — the A90
|
|
12
|
+
* checker resolves workspace imports to their built output and scans it.
|
|
13
|
+
*/
|
|
14
|
+
function serializeVxjson(data) {
|
|
15
|
+
const { content, ...metadataFields } = data;
|
|
16
|
+
if (content === void 0 || content === null) throw new Error("VXJSON format requires a \"content\" field");
|
|
17
|
+
const sortedMetadata = sortKeys(metadataFields, { deep: true });
|
|
18
|
+
const metadataJson = JSON.stringify(sortedMetadata, (_key, value) => {
|
|
19
|
+
if (value === "") return;
|
|
20
|
+
return value;
|
|
21
|
+
}, " ");
|
|
22
|
+
const wrappedContent = JSON.stringify({ c: sortKeys(content, { deep: true }) }, null, " ");
|
|
23
|
+
const contentJson = wrappedContent.slice(wrappedContent.indexOf(": ") + 2, -2);
|
|
24
|
+
const metadataWithoutClosing = metadataJson.slice(0, -2);
|
|
25
|
+
const contentFieldPrefix = ",\n \"content\": ";
|
|
26
|
+
const contentKeyPosition = metadataWithoutClosing.length + 1 + 2;
|
|
27
|
+
const maxContentKeyPosition = 4086;
|
|
28
|
+
if (contentKeyPosition > maxContentKeyPosition) throw new Error(`VXJSON format violation: metadata too large. "content" field would start at position ${contentKeyPosition}, but must start within ${maxContentKeyPosition} bytes. Reduce metadata size by ${contentKeyPosition - maxContentKeyPosition} bytes.`);
|
|
29
|
+
return `${metadataWithoutClosing}${contentFieldPrefix}${contentJson}\n}`;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { serializeVxjson as t };
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=vxjson-serialize-geF6qvpn.mjs.map
|