@conloca/content-api 0.1.16 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/asset-constraints.d.ts +16 -0
- package/dist/asset-constraints.d.ts.map +1 -0
- package/dist/asset-constraints.js +29 -0
- package/dist/asset-constraints.js.map +1 -0
- package/dist/asset-metadata-store.d.ts +60 -0
- package/dist/asset-metadata-store.d.ts.map +1 -0
- package/dist/asset-metadata-store.js +335 -0
- package/dist/asset-metadata-store.js.map +1 -0
- package/dist/asset-operations.d.ts +18 -13
- package/dist/asset-operations.d.ts.map +1 -1
- package/dist/asset-operations.js +302 -254
- package/dist/asset-operations.js.map +1 -0
- package/dist/asset-types.d.ts +23 -7
- package/dist/asset-types.d.ts.map +1 -1
- package/dist/asset-types.js +8 -2
- package/dist/asset-types.js.map +1 -0
- package/dist/asset-usage.d.ts +10 -0
- package/dist/asset-usage.d.ts.map +1 -0
- package/dist/asset-usage.js +49 -0
- package/dist/asset-usage.js.map +1 -0
- package/dist/base-content-index.d.ts +8 -2
- package/dist/base-content-index.d.ts.map +1 -1
- package/dist/base-content-index.js +24 -3
- package/dist/base-content-index.js.map +1 -0
- package/dist/cf-access.js +1 -0
- package/dist/cf-access.js.map +1 -0
- package/dist/component-registry.d.ts +1 -1
- package/dist/component-registry.d.ts.map +1 -1
- package/dist/component-registry.js +2 -1
- package/dist/component-registry.js.map +1 -0
- package/dist/component-registry.types.js +1 -0
- package/dist/component-registry.types.js.map +1 -0
- package/dist/component-vocabulary.d.ts +33 -0
- package/dist/component-vocabulary.d.ts.map +1 -0
- package/dist/component-vocabulary.js +105 -0
- package/dist/component-vocabulary.js.map +1 -0
- package/dist/content-api.interface.d.ts +5 -5
- package/dist/content-api.interface.d.ts.map +1 -1
- package/dist/content-api.interface.js +1 -0
- package/dist/content-api.interface.js.map +1 -0
- package/dist/content-filters.d.ts +2 -2
- package/dist/content-filters.d.ts.map +1 -1
- package/dist/content-filters.js +9 -8
- package/dist/content-filters.js.map +1 -0
- package/dist/content-index.d.ts +74 -16
- package/dist/content-index.d.ts.map +1 -1
- package/dist/content-index.js +183 -55
- package/dist/content-index.js.map +1 -0
- package/dist/content-operations.d.ts +71 -7
- package/dist/content-operations.d.ts.map +1 -1
- package/dist/content-operations.js +166 -82
- package/dist/content-operations.js.map +1 -0
- package/dist/content-path.d.ts +59 -0
- package/dist/content-path.d.ts.map +1 -0
- package/dist/content-path.js +88 -0
- package/dist/content-path.js.map +1 -0
- package/dist/content-reader.d.ts +2 -2
- package/dist/content-reader.d.ts.map +1 -1
- package/dist/content-reader.js +2 -1
- package/dist/content-reader.js.map +1 -0
- package/dist/content-utils.d.ts +53 -10
- package/dist/content-utils.d.ts.map +1 -1
- package/dist/content-utils.js +103 -34
- package/dist/content-utils.js.map +1 -0
- package/dist/content-watcher.d.ts +3 -3
- package/dist/content-watcher.d.ts.map +1 -1
- package/dist/content-watcher.js +13 -10
- package/dist/content-watcher.js.map +1 -0
- package/dist/data-index.d.ts +1 -1
- package/dist/data-index.d.ts.map +1 -1
- package/dist/data-index.js +2 -1
- package/dist/data-index.js.map +1 -0
- package/dist/data.d.ts +5 -13
- package/dist/data.d.ts.map +1 -1
- package/dist/data.js +7 -6
- package/dist/data.js.map +1 -0
- package/dist/editor-data-bindings.d.ts +10 -0
- package/dist/editor-data-bindings.d.ts.map +1 -0
- package/dist/editor-data-bindings.js +67 -0
- package/dist/editor-data-bindings.js.map +1 -0
- package/dist/etag-utils.d.ts.map +1 -1
- package/dist/etag-utils.js +5 -4
- package/dist/etag-utils.js.map +1 -0
- package/dist/filesystem-content-api.d.ts +45 -18
- package/dist/filesystem-content-api.d.ts.map +1 -1
- package/dist/filesystem-content-api.js +407 -281
- package/dist/filesystem-content-api.js.map +1 -0
- package/dist/filesystem-reader-backend.d.ts +46 -0
- package/dist/filesystem-reader-backend.d.ts.map +1 -0
- package/dist/filesystem-reader-backend.js +119 -0
- package/dist/filesystem-reader-backend.js.map +1 -0
- package/dist/fragment-index.d.ts +8 -0
- package/dist/fragment-index.d.ts.map +1 -0
- package/dist/fragment-index.js +8 -0
- package/dist/fragment-index.js.map +1 -0
- package/dist/fragments.d.ts +16 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +104 -0
- package/dist/fragments.js.map +1 -0
- package/dist/git-operations.d.ts.map +1 -1
- package/dist/git-operations.js +5 -1
- package/dist/git-operations.js.map +1 -0
- package/dist/header-parser.d.ts +4 -4
- package/dist/header-parser.d.ts.map +1 -1
- package/dist/header-parser.js +8 -61
- package/dist/header-parser.js.map +1 -0
- package/dist/in-memory-content-api.d.ts +7 -7
- package/dist/in-memory-content-api.d.ts.map +1 -1
- package/dist/in-memory-content-api.js +74 -56
- package/dist/in-memory-content-api.js.map +1 -0
- package/dist/index.d.ts +19 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -9
- package/dist/index.js.map +1 -0
- package/dist/indexing.d.ts +4 -4
- package/dist/indexing.d.ts.map +1 -1
- package/dist/indexing.js +6 -5
- package/dist/indexing.js.map +1 -0
- package/dist/json-whitespace-view.js +1 -0
- package/dist/json-whitespace-view.js.map +1 -0
- package/dist/middleware.d.ts +6 -6
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +170 -119
- package/dist/middleware.js.map +1 -0
- package/dist/node.d.ts +24 -17
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +17 -14
- package/dist/node.js.map +1 -0
- package/dist/page-references.d.ts +21 -0
- package/dist/page-references.d.ts.map +1 -0
- package/dist/page-references.js +50 -0
- package/dist/page-references.js.map +1 -0
- package/dist/read-date.d.ts +3 -0
- package/dist/read-date.d.ts.map +1 -0
- package/dist/read-date.js +9 -0
- package/dist/read-date.js.map +1 -0
- package/dist/reader-backend.d.ts +54 -0
- package/dist/reader-backend.d.ts.map +1 -0
- package/dist/reader-backend.js +2 -0
- package/dist/reader-backend.js.map +1 -0
- package/dist/reader.d.ts +8 -8
- package/dist/reader.d.ts.map +1 -1
- package/dist/reader.js +8 -7
- package/dist/reader.js.map +1 -0
- package/dist/schemas/index.d.ts +6 -6
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +9 -8
- package/dist/schemas/index.js.map +1 -0
- package/dist/site-index.d.ts +3 -3
- package/dist/site-index.d.ts.map +1 -1
- package/dist/site-index.js +9 -5
- package/dist/site-index.js.map +1 -0
- package/dist/site.d.ts +5 -3
- package/dist/site.d.ts.map +1 -1
- package/dist/site.js +14 -2
- package/dist/site.js.map +1 -0
- package/dist/types.d.ts +203 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/url-safe-base64.js +1 -0
- package/dist/url-safe-base64.js.map +1 -0
- package/dist/url-validation.d.ts +14 -0
- package/dist/url-validation.d.ts.map +1 -1
- package/dist/url-validation.js +56 -14
- package/dist/url-validation.js.map +1 -0
- package/dist/utils/atomic-write.js +1 -0
- package/dist/utils/atomic-write.js.map +1 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +2 -1
- package/dist/utils.js.map +1 -0
- package/dist/vite-plugin.d.ts +2 -2
- package/dist/vite-plugin.js +5 -4
- package/dist/vite-plugin.js.map +1 -0
- package/dist/vxjson-serialize.d.ts +5 -6
- package/dist/vxjson-serialize.d.ts.map +1 -1
- package/dist/vxjson-serialize.js +13 -10
- package/dist/vxjson-serialize.js.map +1 -0
- package/dist/vxjson.d.ts +4 -3
- package/dist/vxjson.d.ts.map +1 -1
- package/dist/vxjson.js +10 -20
- package/dist/vxjson.js.map +1 -0
- package/dist/xxhash.d.ts.map +1 -1
- package/dist/xxhash.js +6 -2
- package/dist/xxhash.js.map +1 -0
- package/package.json +92 -56
- package/src/asset-constraints.ts +33 -0
- package/src/asset-metadata-store.ts +401 -0
- package/src/asset-operations.ts +914 -0
- package/src/asset-types.ts +74 -0
- package/src/asset-usage.ts +47 -0
- package/src/base-content-index.ts +227 -0
- package/src/cf-access.ts +85 -0
- package/src/component-registry.ts +145 -0
- package/src/component-registry.types.ts +12 -0
- package/src/component-vocabulary.ts +87 -0
- package/src/content-api.interface.ts +94 -0
- package/src/content-filters.ts +278 -0
- package/src/content-index.ts +642 -0
- package/src/content-operations.ts +497 -0
- package/src/content-path.ts +113 -0
- package/src/content-reader.ts +18 -0
- package/src/content-utils.ts +474 -0
- package/src/content-watcher.ts +122 -0
- package/src/data-index.ts +7 -0
- package/src/data.ts +134 -0
- package/src/editor-data-bindings.ts +66 -0
- package/src/etag-utils.ts +204 -0
- package/src/filesystem-content-api.ts +2685 -0
- package/src/filesystem-reader-backend.ts +135 -0
- package/src/fragment-index.ts +7 -0
- package/src/fragments.ts +113 -0
- package/src/git-operations.ts +340 -0
- package/src/header-parser.ts +272 -0
- package/src/in-memory-content-api.ts +760 -0
- package/src/index.ts +45 -0
- package/src/indexing.ts +8 -0
- package/src/json-whitespace-view.ts +155 -0
- package/src/middleware.ts +1341 -0
- package/src/node.ts +40 -0
- package/src/page-references.ts +76 -0
- package/src/read-date.ts +7 -0
- package/src/reader-backend.ts +54 -0
- package/src/reader.ts +15 -0
- package/src/schemas/index.ts +113 -0
- package/src/site-index.ts +330 -0
- package/src/site.ts +198 -0
- package/src/types.ts +759 -0
- package/src/url-safe-base64.ts +38 -0
- package/src/url-validation.ts +121 -0
- package/src/utils/atomic-write.ts +20 -0
- package/src/utils.ts +84 -0
- package/src/vite-plugin.ts +45 -0
- package/src/vxjson-serialize.ts +66 -0
- package/src/vxjson.ts +442 -0
- package/src/xxhash.ts +43 -0
- package/dist/asset-manifest.d.ts +0 -23
- package/dist/asset-manifest.d.ts.map +0 -1
- package/dist/asset-manifest.js +0 -73
- package/dist/block-index.d.ts +0 -9
- package/dist/block-index.d.ts.map +0 -1
- package/dist/block-index.js +0 -11
- package/dist/blocks.d.ts +0 -16
- package/dist/blocks.d.ts.map +0 -1
- package/dist/blocks.js +0 -103
- package/dist/component-registry.types-C0ctF9nh.d.mts +0 -71
- package/dist/component-registry.types-C0ctF9nh.d.mts.map +0 -1
- package/dist/content-index-D092_vk1.mjs +0 -629
- package/dist/content-index-D092_vk1.mjs.map +0 -1
- package/dist/content-index-D6IVMmAB.d.mts +0 -127
- package/dist/content-index-D6IVMmAB.d.mts.map +0 -1
- package/dist/content-operations-DIbsxG5A.d.mts +0 -354
- package/dist/content-operations-DIbsxG5A.d.mts.map +0 -1
- package/dist/content-utils-fWNHTrPy.mjs +0 -280
- package/dist/content-utils-fWNHTrPy.mjs.map +0 -1
- package/dist/data-DquNIEnQ.mjs +0 -62
- package/dist/data-DquNIEnQ.mjs.map +0 -1
- package/dist/filesystem-content-api-B1KIQJfk.mjs +0 -2
- package/dist/filesystem-content-api-C0HIhqZi.mjs +0 -1620
- package/dist/filesystem-content-api-C0HIhqZi.mjs.map +0 -1
- package/dist/filesystem-content-api-DCdNi-co.d.mts +0 -134
- package/dist/filesystem-content-api-DCdNi-co.d.mts.map +0 -1
- package/dist/index-DxVTFK66.d.mts +0 -105
- package/dist/index-DxVTFK66.d.mts.map +0 -1
- package/dist/index.d.mts +0 -80
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -78
- package/dist/index.mjs.map +0 -1
- package/dist/indexing.d.mts +0 -152
- package/dist/indexing.d.mts.map +0 -1
- package/dist/indexing.mjs +0 -255
- package/dist/indexing.mjs.map +0 -1
- package/dist/node.d.mts +0 -373
- package/dist/node.d.mts.map +0 -1
- package/dist/node.mjs +0 -2572
- package/dist/node.mjs.map +0 -1
- package/dist/reader.d.mts +0 -15
- package/dist/reader.d.mts.map +0 -1
- package/dist/reader.mjs +0 -22
- package/dist/reader.mjs.map +0 -1
- package/dist/schemas/index.d.mts +0 -2
- package/dist/schemas/index.mjs +0 -2
- package/dist/schemas-CFRD0FIQ.mjs +0 -84
- package/dist/schemas-CFRD0FIQ.mjs.map +0 -1
- package/dist/site-8oaj1Q60.mjs +0 -366
- package/dist/site-8oaj1Q60.mjs.map +0 -1
- package/dist/site-index-B_254bCc.d.mts +0 -136
- package/dist/site-index-B_254bCc.d.mts.map +0 -1
- package/dist/types-DOdldbfD.mjs +0 -34
- package/dist/types-DOdldbfD.mjs.map +0 -1
- package/dist/types-DjfhS272.d.mts +0 -456
- package/dist/types-DjfhS272.d.mts.map +0 -1
- package/dist/vxjson-qHrgLGip.mjs +0 -555
- package/dist/vxjson-qHrgLGip.mjs.map +0 -1
- package/dist/vxjson-serialize-geF6qvpn.mjs +0 -34
- package/dist/vxjson-serialize-geF6qvpn.mjs.map +0 -1
- package/dist/vxjson-serialize.d.mts +0 -17
- package/dist/vxjson-serialize.d.mts.map +0 -1
- package/dist/vxjson-serialize.mjs +0 -2
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-safe asset type definitions for @conloca/content-api
|
|
3
|
+
*
|
|
4
|
+
* These types are safe to import in browser environments.
|
|
5
|
+
* The Node.js implementation that stores them on disk is in asset-metadata-store.ts
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The directory under the content root that holds one metadata record per
|
|
10
|
+
* asset. It sits with the content, which is committed but never served, and
|
|
11
|
+
* never inside the media folder, which a site publishes as it is.
|
|
12
|
+
*/
|
|
13
|
+
export const ASSET_METADATA_DIR = '.asset-metadata';
|
|
14
|
+
|
|
15
|
+
export interface AssetEntry {
|
|
16
|
+
filename: string;
|
|
17
|
+
originalName: string;
|
|
18
|
+
mimeType: string;
|
|
19
|
+
size: number;
|
|
20
|
+
width?: number;
|
|
21
|
+
height?: number;
|
|
22
|
+
alt?: string;
|
|
23
|
+
uploadedAt: string;
|
|
24
|
+
/** Only ever set by a host that knows who is signed in — never taken from an upload request */
|
|
25
|
+
uploadedBy?: string;
|
|
26
|
+
/** Folder path relative to assets root, default '/' */
|
|
27
|
+
folder?: string;
|
|
28
|
+
/** Tags for categorization/filtering */
|
|
29
|
+
tags?: string[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Asset usage reference - tracks where an asset is used */
|
|
33
|
+
export interface AssetUsage {
|
|
34
|
+
page: string;
|
|
35
|
+
field: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Folder listing result */
|
|
39
|
+
export interface FolderListing {
|
|
40
|
+
assets: AssetEntry[];
|
|
41
|
+
folders: { name: string; path: string }[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Folder tree node for hierarchical folder view */
|
|
45
|
+
export interface FolderTreeNode {
|
|
46
|
+
name: string;
|
|
47
|
+
path: string;
|
|
48
|
+
assetCount: number;
|
|
49
|
+
children: FolderTreeNode[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The metadata a person writes about one asset, stored in that asset's own record */
|
|
53
|
+
export interface AssetMetadata {
|
|
54
|
+
alt?: string;
|
|
55
|
+
tags?: string[];
|
|
56
|
+
uploadedAt?: string; // If uploaded via CMS
|
|
57
|
+
uploadedBy?: string;
|
|
58
|
+
originalName?: string; // Original filename before sanitization
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A metadata update as the sidebar sends it: absent keys are left alone, null
|
|
63
|
+
* clears the stored field. Undefined would be dropped by JSON.stringify on the
|
|
64
|
+
* way over, silently keeping the old value.
|
|
65
|
+
*/
|
|
66
|
+
export type AssetMetadataPatch = { [K in keyof AssetMetadata]?: AssetMetadata[K] | null };
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The pre-0.2 shared manifest, keyed by path relative to the assets root. Read
|
|
70
|
+
* only by the one-shot migration onto per-asset records; nothing writes it.
|
|
71
|
+
*/
|
|
72
|
+
export interface AssetManifestData {
|
|
73
|
+
[relativePath: string]: AssetMetadata;
|
|
74
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AssetUsage } from './asset-types.js';
|
|
2
|
+
import type { ContentAPI } from './content-api.interface.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Every page, fragment and data entry language whose meta or content links to an asset. The editor
|
|
6
|
+
* links an asset as `/assets/<path>`; only that link standing whole counts, so `/assets/trip/beach.png`
|
|
7
|
+
* is no use of `beach.png` and neither is `/assets/my-beach.png` or `/assets/beach.png.bak`. A full URL
|
|
8
|
+
* such as `https://…/assets/beach.png` never counts, even one to this site, whose address is not known here.
|
|
9
|
+
*/
|
|
10
|
+
export async function findAssetUsage(api: ContentAPI, path: string): Promise<AssetUsage[]> {
|
|
11
|
+
const link = `/assets/${path.replace(/^\/+|\/+$/g, '')}`;
|
|
12
|
+
const whole = new RegExp(`(?<![\\w.~%/-])${link.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![\\w./-])`);
|
|
13
|
+
const usage: AssetUsage[] = [];
|
|
14
|
+
const severalSites = Object.keys(api.sitesConfig.sites).length > 1;
|
|
15
|
+
// Listed first: reading an entry can re-index it, which must not disturb the listing being walked
|
|
16
|
+
for (const manifest of [...api.listAllContent()]) {
|
|
17
|
+
for (const locale of Object.keys(manifest.locales)) {
|
|
18
|
+
const entry = await api.getLocalized(manifest.id, locale);
|
|
19
|
+
if (!entry) continue;
|
|
20
|
+
const { meta, content, pathname, name } = entry.localized;
|
|
21
|
+
const field = fieldWith({ meta, content }, whole);
|
|
22
|
+
if (field) {
|
|
23
|
+
const where = pathname ?? (name ? `${entry.collection}/${name}` : entry.id);
|
|
24
|
+
const site = severalSites && entry.site ? `${entry.site}: ` : '';
|
|
25
|
+
usage.push({ page: `${site}${where} (${locale})`, field });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return usage;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The path of the first string field in `value` that `pattern` matches, e.g. `meta.ogImage`. */
|
|
33
|
+
function fieldWith(value: unknown, pattern: RegExp, path = ''): string | null {
|
|
34
|
+
if (typeof value === 'string') return pattern.test(value) ? path : null;
|
|
35
|
+
if (Array.isArray(value)) {
|
|
36
|
+
for (const [i, item] of value.entries()) {
|
|
37
|
+
const found = fieldWith(item, pattern, `${path}[${i}]`);
|
|
38
|
+
if (found) return found;
|
|
39
|
+
}
|
|
40
|
+
} else if (value && typeof value === 'object') {
|
|
41
|
+
for (const [key, item] of Object.entries(value)) {
|
|
42
|
+
const found = fieldWith(item, pattern, path ? `${path}.${key}` : key);
|
|
43
|
+
if (found) return found;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { localesOf } from './content-utils.js';
|
|
2
|
+
import type { ContentData, ContentIdentity, ContentManifest, LocaleVersion } from './types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base class for content indexes (fragments, data)
|
|
6
|
+
* Maintains collection->name->locale lookups with V8-optimized structures
|
|
7
|
+
*/
|
|
8
|
+
export class BaseContentIndex {
|
|
9
|
+
// Primary index: id -> ContentManifest
|
|
10
|
+
protected index: Map<string, ContentManifest> = new Map();
|
|
11
|
+
|
|
12
|
+
// Content cache: id -> locale -> content (for small files)
|
|
13
|
+
protected contentCache: Map<string, Record<string, ContentData | null>> = new Map();
|
|
14
|
+
|
|
15
|
+
// Name index: collection -> name -> ContentManifest
|
|
16
|
+
protected nameIndex: Map<string, Map<string, ContentManifest>> = new Map();
|
|
17
|
+
|
|
18
|
+
// Collections used
|
|
19
|
+
readonly collections: Set<string> = new Set();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Add or update content in the index
|
|
23
|
+
* Merges manifest locales and updates content cache if provided
|
|
24
|
+
*/
|
|
25
|
+
addContent(identity: ContentIdentity, localeVersion: LocaleVersion, content?: ContentData | null): void {
|
|
26
|
+
// Get existing manifest
|
|
27
|
+
const existing = this.index.get(identity.id);
|
|
28
|
+
const locale = localeVersion.locale;
|
|
29
|
+
|
|
30
|
+
if (existing) {
|
|
31
|
+
// Update single locale
|
|
32
|
+
const existingLocale = existing.locales[locale];
|
|
33
|
+
if (!existingLocale || existingLocale.etag !== localeVersion.etag) {
|
|
34
|
+
existing.locales[locale] = localeVersion;
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
// New content - create ContentManifest with single locale
|
|
38
|
+
const newManifest: ContentManifest = {
|
|
39
|
+
id: identity.id,
|
|
40
|
+
type: identity.type,
|
|
41
|
+
kind: identity.kind,
|
|
42
|
+
site: identity.site,
|
|
43
|
+
collection: identity.collection,
|
|
44
|
+
locales: {
|
|
45
|
+
[locale]: localeVersion,
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
this.index.set(identity.id, newManifest);
|
|
50
|
+
this.collections.add(identity.collection);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Update content cache if provided
|
|
54
|
+
if (content !== undefined) {
|
|
55
|
+
const existingCache = this.contentCache.get(identity.id);
|
|
56
|
+
if (existingCache) {
|
|
57
|
+
// Update single locale content
|
|
58
|
+
existingCache[locale] = content;
|
|
59
|
+
} else {
|
|
60
|
+
this.contentCache.set(identity.id, { [locale]: content });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Update name index
|
|
65
|
+
const manifestToIndex = existing || this.index.get(identity.id)!;
|
|
66
|
+
if (localeVersion.name) {
|
|
67
|
+
if (!this.nameIndex.has(identity.collection)) {
|
|
68
|
+
this.nameIndex.set(identity.collection, new Map());
|
|
69
|
+
}
|
|
70
|
+
this.nameIndex.get(identity.collection)!.set(localeVersion.name, manifestToIndex);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Remove a specific locale from an entry
|
|
76
|
+
*/
|
|
77
|
+
removeLocale(id: string, locale: string): void {
|
|
78
|
+
const manifest = this.index.get(id);
|
|
79
|
+
const localeVersion = manifest?.locales[locale];
|
|
80
|
+
if (!manifest || !localeVersion) return;
|
|
81
|
+
|
|
82
|
+
if (
|
|
83
|
+
localeVersion.name &&
|
|
84
|
+
!Object.entries(manifest.locales).some(
|
|
85
|
+
([otherLocale, otherVersion]) => otherLocale !== locale && otherVersion?.name === localeVersion.name,
|
|
86
|
+
)
|
|
87
|
+
) {
|
|
88
|
+
const collectionMap = this.nameIndex.get(manifest.collection);
|
|
89
|
+
if (collectionMap?.get(localeVersion.name)?.id === id) {
|
|
90
|
+
collectionMap.delete(localeVersion.name);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Remove locale from manifest
|
|
95
|
+
manifest.locales[locale] = undefined;
|
|
96
|
+
|
|
97
|
+
// Remove from content cache
|
|
98
|
+
const contentCache = this.contentCache.get(id);
|
|
99
|
+
if (contentCache) {
|
|
100
|
+
delete contentCache[locale];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// If no defined locales left, remove the entire entry
|
|
104
|
+
const remainingLocales = Object.values(manifest.locales).filter((v) => v !== undefined);
|
|
105
|
+
if (remainingLocales.length === 0) {
|
|
106
|
+
this.removeEntry(id);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Remove an entry from the index
|
|
112
|
+
*/
|
|
113
|
+
removeEntry(id: string): void {
|
|
114
|
+
const manifest = this.index.get(id);
|
|
115
|
+
if (!manifest) return;
|
|
116
|
+
|
|
117
|
+
// Remove from name index
|
|
118
|
+
for (const localeVersion of localesOf(manifest)) {
|
|
119
|
+
if (localeVersion.name) {
|
|
120
|
+
const collectionMap = this.nameIndex.get(manifest.collection);
|
|
121
|
+
if (collectionMap) {
|
|
122
|
+
collectionMap.delete(localeVersion.name);
|
|
123
|
+
}
|
|
124
|
+
break; // All locales have same name
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Remove from primary index
|
|
129
|
+
this.index.delete(id);
|
|
130
|
+
|
|
131
|
+
// Remove from content cache
|
|
132
|
+
this.contentCache.delete(id);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Get cached content for a specific locale
|
|
137
|
+
*/
|
|
138
|
+
getCachedContent(id: string, locale: string): ContentData | null {
|
|
139
|
+
const localeContent = this.contentCache.get(id);
|
|
140
|
+
return localeContent?.[locale] || null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Get content manifest
|
|
145
|
+
*/
|
|
146
|
+
getManifest(id: string): ContentManifest | null {
|
|
147
|
+
return this.index.get(id) || null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Find entry by name
|
|
152
|
+
*/
|
|
153
|
+
getByName(collection: string, name: string, locale?: string): ContentManifest | null {
|
|
154
|
+
const collectionMap = this.nameIndex.get(collection);
|
|
155
|
+
if (!collectionMap) return null;
|
|
156
|
+
|
|
157
|
+
const manifest = collectionMap.get(name);
|
|
158
|
+
if (!manifest) return null;
|
|
159
|
+
|
|
160
|
+
// If specific locale requested, check it exists
|
|
161
|
+
if (locale && !manifest.locales[locale]) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return manifest;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Every entry of `collection` named `name` in some locale, in any letter case: their files
|
|
170
|
+
* ('Hero.en.mdx', 'hero.en.mdx') are one file on a case-insensitive disk, the default on macOS
|
|
171
|
+
* and Windows.
|
|
172
|
+
*/
|
|
173
|
+
*getAllByNameInAnyCase(collection: string, name: string): Generator<ContentManifest> {
|
|
174
|
+
const folded = name.toLowerCase();
|
|
175
|
+
for (const [other, manifest] of this.nameIndex.get(collection) ?? []) {
|
|
176
|
+
if (other.toLowerCase() === folded) yield manifest;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Generator for all manifests
|
|
182
|
+
*/
|
|
183
|
+
*getAllManifests(): Generator<ContentManifest> {
|
|
184
|
+
for (const manifest of this.index.values()) {
|
|
185
|
+
yield manifest;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Generator for manifests by locale
|
|
191
|
+
*/
|
|
192
|
+
*getManifestsByLocale(locale: string): Generator<ContentManifest> {
|
|
193
|
+
for (const manifest of this.index.values()) {
|
|
194
|
+
if (manifest.locales[locale]) {
|
|
195
|
+
yield manifest;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Generator for manifests by collection
|
|
202
|
+
*/
|
|
203
|
+
*getManifestsByCollection(collection: string): Generator<ContentManifest> {
|
|
204
|
+
for (const manifest of this.index.values()) {
|
|
205
|
+
if (manifest.collection === collection) {
|
|
206
|
+
yield manifest;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Get the count of indexed content entries (unique IDs)
|
|
213
|
+
*/
|
|
214
|
+
get entryCount(): number {
|
|
215
|
+
return this.index.size;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Clear all indexes
|
|
220
|
+
*/
|
|
221
|
+
clear(): void {
|
|
222
|
+
this.index.clear();
|
|
223
|
+
this.contentCache.clear();
|
|
224
|
+
this.nameIndex.clear();
|
|
225
|
+
this.collections.clear();
|
|
226
|
+
}
|
|
227
|
+
}
|
package/src/cf-access.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cloudflare Access JWT validation utility
|
|
3
|
+
*
|
|
4
|
+
* Validates JWT tokens from CF Access for API authorization and git commit attribution.
|
|
5
|
+
* When CF_ACCESS_TEAM_NAME and CF_ACCESS_AUD env vars are set, requests must have valid tokens.
|
|
6
|
+
* When env vars are not set (local dev), validation is skipped.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createRemoteJWKSet, jwtVerify } from 'jose';
|
|
10
|
+
|
|
11
|
+
const CF_ACCESS_TEAM_NAME = process.env.CF_ACCESS_TEAM_NAME;
|
|
12
|
+
const CF_ACCESS_AUD = process.env.CF_ACCESS_AUD;
|
|
13
|
+
|
|
14
|
+
// JWKS with built-in caching (handles key rotation automatically)
|
|
15
|
+
const JWKS = CF_ACCESS_TEAM_NAME
|
|
16
|
+
? createRemoteJWKSet(new URL(`https://${CF_ACCESS_TEAM_NAME}.cloudflareaccess.com/cdn-cgi/access/certs`))
|
|
17
|
+
: null;
|
|
18
|
+
|
|
19
|
+
export interface CFAccessUser {
|
|
20
|
+
email?: string;
|
|
21
|
+
sub?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface CFAccessResult {
|
|
25
|
+
valid: boolean;
|
|
26
|
+
required: boolean;
|
|
27
|
+
user?: CFAccessUser;
|
|
28
|
+
error?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Extract CF Access JWT token from request.
|
|
33
|
+
* Checks Cf-Access-Jwt-Assertion header first, falls back to CF_Authorization cookie.
|
|
34
|
+
*/
|
|
35
|
+
export function extractCFAccessToken(request: Request): string | null {
|
|
36
|
+
// Header is primary (always present when behind CF Access)
|
|
37
|
+
const headerToken = request.headers.get('Cf-Access-Jwt-Assertion');
|
|
38
|
+
if (headerToken) return headerToken;
|
|
39
|
+
|
|
40
|
+
// Cookie is fallback (browser requests)
|
|
41
|
+
const cookies = request.headers.get('Cookie') || '';
|
|
42
|
+
const match = cookies.match(/CF_Authorization=([^;]+)/);
|
|
43
|
+
return match ? match[1] : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Validate CF Access JWT from request.
|
|
48
|
+
*
|
|
49
|
+
* Returns { valid: true, required: false } when env vars not set (local dev mode).
|
|
50
|
+
* Returns { valid: true, required: true, user } when token is valid.
|
|
51
|
+
* Returns { valid: false, required: true, error } when token is invalid or missing.
|
|
52
|
+
*/
|
|
53
|
+
export async function validateCFAccessRequest(request: Request): Promise<CFAccessResult> {
|
|
54
|
+
// If CF Access not configured, skip validation (local dev)
|
|
55
|
+
if (!CF_ACCESS_TEAM_NAME || !CF_ACCESS_AUD || !JWKS) {
|
|
56
|
+
return { valid: true, required: false };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const token = extractCFAccessToken(request);
|
|
60
|
+
if (!token) {
|
|
61
|
+
return { valid: false, required: true, error: 'No token provided' };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const { payload } = await jwtVerify(token, JWKS, {
|
|
66
|
+
issuer: `https://${CF_ACCESS_TEAM_NAME}.cloudflareaccess.com`,
|
|
67
|
+
audience: CF_ACCESS_AUD,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
valid: true,
|
|
72
|
+
required: true,
|
|
73
|
+
user: {
|
|
74
|
+
email: typeof payload.email === 'string' && payload.email ? payload.email : undefined,
|
|
75
|
+
sub: typeof payload.sub === 'string' && payload.sub ? payload.sub : undefined,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return {
|
|
80
|
+
valid: false,
|
|
81
|
+
required: true,
|
|
82
|
+
error: error instanceof Error ? error.message : 'Validation failed',
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { mkdir, readFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import sortKeys from 'sort-keys';
|
|
4
|
+
import type {
|
|
5
|
+
ComponentEntry,
|
|
6
|
+
ComponentEntryWithId,
|
|
7
|
+
ComponentRegistry as ComponentRegistryType,
|
|
8
|
+
} from './component-registry.types.js';
|
|
9
|
+
import { atomicWriteFile } from './utils/atomic-write.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Component Registry for managing linked and unlinked components
|
|
13
|
+
*
|
|
14
|
+
* Registry is stored at {canvasDir}/components/registry.json
|
|
15
|
+
* Component IDs must follow the format: vx-{uuid}
|
|
16
|
+
*/
|
|
17
|
+
export class ComponentRegistry {
|
|
18
|
+
private registryPath: string;
|
|
19
|
+
private registry: ComponentRegistryType = {};
|
|
20
|
+
private initialized = false;
|
|
21
|
+
|
|
22
|
+
constructor(canvasDir: string) {
|
|
23
|
+
this.registryPath = join(canvasDir, 'components', 'registry.json');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Initialize the registry by loading from disk or creating new
|
|
28
|
+
*/
|
|
29
|
+
async initialize(): Promise<void> {
|
|
30
|
+
if (this.initialized) return;
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const content = await readFile(this.registryPath, 'utf-8');
|
|
34
|
+
this.registry = JSON.parse(content);
|
|
35
|
+
} catch (error: any) {
|
|
36
|
+
if (error.code === 'ENOENT') {
|
|
37
|
+
// File doesn't exist, create empty registry
|
|
38
|
+
await this.save();
|
|
39
|
+
} else {
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
this.initialized = true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Validate component ID format (vx-{uuid})
|
|
49
|
+
*/
|
|
50
|
+
private validateComponentId(id: string): void {
|
|
51
|
+
const uuidRegex = /^vx-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
52
|
+
if (!uuidRegex.test(id)) {
|
|
53
|
+
throw new Error('Invalid component ID format. Expected: vx-{uuid}');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Save registry to disk atomically
|
|
59
|
+
*/
|
|
60
|
+
private async save(): Promise<void> {
|
|
61
|
+
// Ensure directory exists
|
|
62
|
+
await mkdir(dirname(this.registryPath), { recursive: true });
|
|
63
|
+
|
|
64
|
+
// Sort keys for consistent output
|
|
65
|
+
const sorted = sortKeys(this.registry, { deep: true });
|
|
66
|
+
const content = JSON.stringify(sorted, null, 2);
|
|
67
|
+
|
|
68
|
+
await atomicWriteFile(this.registryPath, content);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Register a new component or update existing
|
|
73
|
+
*/
|
|
74
|
+
async register(id: string, entry: ComponentEntry): Promise<void> {
|
|
75
|
+
await this.initialize();
|
|
76
|
+
this.validateComponentId(id);
|
|
77
|
+
|
|
78
|
+
this.registry[id] = {
|
|
79
|
+
displayName: entry.displayName,
|
|
80
|
+
path: entry.path,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
await this.save();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Get a component entry by ID
|
|
88
|
+
*/
|
|
89
|
+
async get(id: string): Promise<ComponentEntry | null> {
|
|
90
|
+
await this.initialize();
|
|
91
|
+
return this.registry[id] || null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Link a component to a path
|
|
96
|
+
*/
|
|
97
|
+
async link(id: string, path: string): Promise<void> {
|
|
98
|
+
await this.initialize();
|
|
99
|
+
|
|
100
|
+
const entry = this.registry[id];
|
|
101
|
+
if (!entry) {
|
|
102
|
+
throw new Error(`Component ${id} not found in registry`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
entry.path = path;
|
|
106
|
+
await this.save();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Unlink a component (set path to null)
|
|
111
|
+
*/
|
|
112
|
+
async unlink(id: string): Promise<void> {
|
|
113
|
+
await this.initialize();
|
|
114
|
+
|
|
115
|
+
const entry = this.registry[id];
|
|
116
|
+
if (!entry) {
|
|
117
|
+
throw new Error(`Component ${id} not found in registry`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
entry.path = null;
|
|
121
|
+
await this.save();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* List all components
|
|
126
|
+
*/
|
|
127
|
+
async list(): Promise<ComponentEntryWithId[]> {
|
|
128
|
+
await this.initialize();
|
|
129
|
+
|
|
130
|
+
return Object.entries(this.registry).map(([id, entry]) => ({
|
|
131
|
+
id,
|
|
132
|
+
...entry,
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Delete a component from registry
|
|
138
|
+
*/
|
|
139
|
+
async delete(id: string): Promise<void> {
|
|
140
|
+
await this.initialize();
|
|
141
|
+
|
|
142
|
+
delete this.registry[id];
|
|
143
|
+
await this.save();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface ComponentEntry {
|
|
2
|
+
displayName: string;
|
|
3
|
+
path: string | null;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface ComponentEntryWithId extends ComponentEntry {
|
|
7
|
+
id: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ComponentRegistry {
|
|
11
|
+
[componentId: string]: ComponentEntry;
|
|
12
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Resource limits shared by build and runtime metadata readers; no renderer or platform dependencies. */
|
|
2
|
+
export const COMPONENT_MANIFEST_LIMITS = { bytes: 4_000_000, nodes: 100_000, depth: 64 } as const;
|
|
3
|
+
export const VOCABULARY_PAGE_LIMITS = { bytes: 256_000, nodes: 10_000, depth: 64 } as const;
|
|
4
|
+
export const VOCABULARY_SAMPLE_CAP = 50;
|
|
5
|
+
export const VOCABULARY_SAMPLE_NODES = 50_000;
|
|
6
|
+
export const VOCABULARY_OUTPUT_BYTES = 256_000;
|
|
7
|
+
export const VOCABULARY_READ_CONCURRENCY = 4;
|
|
8
|
+
|
|
9
|
+
/** Work and wire-size limits for JSON received by the metadata reader. */
|
|
10
|
+
export interface JsonLimits {
|
|
11
|
+
nodes: number;
|
|
12
|
+
bytes: number;
|
|
13
|
+
depth: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class JsonBudgetExceeded extends Error {
|
|
17
|
+
constructor(
|
|
18
|
+
public readonly reason: string,
|
|
19
|
+
public readonly visitedNodes: number,
|
|
20
|
+
) {
|
|
21
|
+
super(reason);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Measures JSON without recursively serializing the entire input or expanding an unbounded traversal stack. */
|
|
26
|
+
export function measureJson(value: unknown, limits: JsonLimits): { nodes: number; bytes: number } {
|
|
27
|
+
const pending = [{ value, depth: 0 }];
|
|
28
|
+
let nodes = 0;
|
|
29
|
+
let bytes = 0;
|
|
30
|
+
const fail = (reason: string): never => {
|
|
31
|
+
throw new JsonBudgetExceeded(reason, nodes);
|
|
32
|
+
};
|
|
33
|
+
const stringBytes = (text: string): number => {
|
|
34
|
+
let count = 2; // JSON quotes.
|
|
35
|
+
for (let i = 0; i < text.length; i++) {
|
|
36
|
+
const code = text.charCodeAt(i);
|
|
37
|
+
if (code === 34 || code === 92) count += 2;
|
|
38
|
+
else if (code < 32) count += code === 8 || code === 9 || code === 10 || code === 12 || code === 13 ? 2 : 6;
|
|
39
|
+
else if (code < 128) count++;
|
|
40
|
+
else if (code < 2048) count += 2;
|
|
41
|
+
else if (code >= 0xd800 && code <= 0xdbff) {
|
|
42
|
+
const low = text.charCodeAt(i + 1);
|
|
43
|
+
if (low >= 0xdc00 && low <= 0xdfff) {
|
|
44
|
+
count += 4;
|
|
45
|
+
i++;
|
|
46
|
+
} else count += 6; // JSON escapes unpaired surrogates.
|
|
47
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) count += 6;
|
|
48
|
+
else count += 3;
|
|
49
|
+
if (count > limits.bytes) return fail('bytes');
|
|
50
|
+
}
|
|
51
|
+
return count;
|
|
52
|
+
};
|
|
53
|
+
while (pending.length) {
|
|
54
|
+
const entry = pending.pop();
|
|
55
|
+
if (!entry) break;
|
|
56
|
+
if (++nodes > limits.nodes) fail('nodes');
|
|
57
|
+
if (entry.depth > limits.depth) fail('depth');
|
|
58
|
+
const current = entry.value;
|
|
59
|
+
if (current === null) bytes += 4;
|
|
60
|
+
else if (typeof current === 'string') bytes += stringBytes(current);
|
|
61
|
+
else if (typeof current === 'boolean') bytes += current ? 4 : 5;
|
|
62
|
+
else if (typeof current === 'number' && Number.isFinite(current)) bytes += String(current).length;
|
|
63
|
+
else if (Array.isArray(current)) {
|
|
64
|
+
if (nodes + pending.length + current.length > limits.nodes) fail('nodes');
|
|
65
|
+
bytes += 2 + Math.max(0, current.length - 1);
|
|
66
|
+
for (let i = current.length - 1; i >= 0; i--) pending.push({ value: current[i], depth: entry.depth + 1 });
|
|
67
|
+
} else if (isRecord(current)) {
|
|
68
|
+
const keys = Object.keys(current);
|
|
69
|
+
if (nodes + pending.length + keys.length * 2 > limits.nodes) fail('nodes');
|
|
70
|
+
nodes += keys.length;
|
|
71
|
+
bytes += 2 + Math.max(0, keys.length - 1);
|
|
72
|
+
for (const key of keys) {
|
|
73
|
+
bytes += stringBytes(key) + 1;
|
|
74
|
+
if (bytes > limits.bytes) fail('bytes');
|
|
75
|
+
pending.push({ value: current[key], depth: entry.depth + 1 });
|
|
76
|
+
}
|
|
77
|
+
} else fail('non-json value');
|
|
78
|
+
if (bytes > limits.bytes) fail('bytes');
|
|
79
|
+
}
|
|
80
|
+
return { nodes, bytes };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
84
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
85
|
+
const prototype = Object.getPrototypeOf(value);
|
|
86
|
+
return prototype === Object.prototype || prototype === null;
|
|
87
|
+
}
|