@conloca/content-api 0.1.15 → 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 +164 -0
- package/dist/asset-operations.d.ts.map +1 -0
- package/dist/asset-operations.js +784 -0
- package/dist/asset-operations.js.map +1 -0
- package/dist/asset-types.d.ts +72 -0
- package/dist/asset-types.d.ts.map +1 -0
- package/dist/asset-types.js +13 -0
- 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 +63 -0
- package/dist/base-content-index.d.ts.map +1 -0
- package/dist/base-content-index.js +200 -0
- package/dist/base-content-index.js.map +1 -0
- package/dist/cf-access.d.ts +31 -0
- package/dist/cf-access.d.ts.map +1 -0
- package/dist/cf-access.js +67 -0
- package/dist/cf-access.js.map +1 -0
- package/dist/component-registry.d.ts +50 -0
- package/dist/component-registry.d.ts.map +1 -0
- package/dist/component-registry.js +121 -0
- package/dist/component-registry.js.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 +2 -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 +64 -0
- package/dist/content-api.interface.d.ts.map +1 -0
- package/dist/content-api.interface.js +2 -0
- package/dist/content-api.interface.js.map +1 -0
- package/dist/content-filters.d.ts +31 -0
- package/dist/content-filters.d.ts.map +1 -0
- package/dist/content-filters.js +247 -0
- package/dist/content-filters.js.map +1 -0
- package/dist/content-index.d.ts +184 -0
- package/dist/content-index.d.ts.map +1 -0
- package/dist/content-index.js +546 -0
- package/dist/content-index.js.map +1 -0
- package/dist/content-operations.d.ts +157 -0
- package/dist/content-operations.d.ts.map +1 -0
- package/dist/content-operations.js +359 -0
- 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 +10 -0
- package/dist/content-reader.d.ts.map +1 -0
- package/dist/content-reader.js +14 -0
- package/dist/content-reader.js.map +1 -0
- package/dist/content-utils.d.ts +175 -0
- package/dist/content-utils.d.ts.map +1 -0
- package/dist/content-utils.js +421 -0
- package/dist/content-utils.js.map +1 -0
- package/dist/content-watcher.d.ts +43 -0
- package/dist/content-watcher.d.ts.map +1 -0
- package/dist/content-watcher.js +86 -0
- package/dist/content-watcher.js.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 +8 -0
- package/dist/data-index.js.map +1 -0
- package/dist/data.d.ts +35 -0
- package/dist/data.d.ts.map +1 -0
- package/dist/data.js +104 -0
- 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 +77 -0
- package/dist/etag-utils.d.ts.map +1 -0
- package/dist/etag-utils.js +161 -0
- package/dist/etag-utils.js.map +1 -0
- package/dist/filesystem-content-api.d.ts +162 -0
- package/dist/filesystem-content-api.d.ts.map +1 -0
- package/dist/filesystem-content-api.js +2307 -0
- 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 +49 -0
- package/dist/git-operations.d.ts.map +1 -0
- package/dist/git-operations.js +256 -0
- package/dist/git-operations.js.map +1 -0
- package/dist/header-parser.d.ts +37 -0
- package/dist/header-parser.d.ts.map +1 -0
- package/dist/header-parser.js +233 -0
- package/dist/header-parser.js.map +1 -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 +609 -0
- package/dist/in-memory-content-api.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/indexing.d.ts +5 -0
- package/dist/indexing.d.ts.map +1 -0
- package/dist/indexing.js +8 -0
- package/dist/indexing.js.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 +153 -0
- package/dist/json-whitespace-view.js.map +1 -0
- package/dist/middleware.d.ts +20 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +1082 -0
- package/dist/middleware.js.map +1 -0
- package/dist/node.d.ts +32 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +27 -0
- 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 +15 -0
- package/dist/reader.d.ts.map +1 -0
- package/dist/reader.js +14 -0
- package/dist/reader.js.map +1 -0
- package/dist/schemas/index.d.ts +102 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +88 -0
- package/dist/schemas/index.js.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 +291 -0
- package/dist/site-index.js.map +1 -0
- package/dist/site.d.ts +23 -0
- package/dist/site.d.ts.map +1 -0
- package/dist/site.js +180 -0
- package/dist/site.js.map +1 -0
- package/dist/types.d.ts +641 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +45 -0
- package/dist/types.js.map +1 -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 +41 -0
- package/dist/url-safe-base64.js.map +1 -0
- package/dist/url-validation.d.ts +28 -0
- package/dist/url-validation.d.ts.map +1 -0
- package/dist/url-validation.js +123 -0
- package/dist/url-validation.js.map +1 -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 +21 -0
- package/dist/utils/atomic-write.js.map +1 -0
- package/dist/utils.d.ts +46 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +71 -0
- package/dist/utils.js.map +1 -0
- package/dist/vite-plugin.d.ts +12 -0
- package/dist/vite-plugin.d.ts.map +1 -0
- package/dist/vite-plugin.js +30 -0
- package/dist/vite-plugin.js.map +1 -0
- package/dist/vxjson-serialize.d.ts +13 -0
- package/dist/vxjson-serialize.d.ts.map +1 -0
- package/dist/vxjson-serialize.js +53 -0
- package/dist/vxjson-serialize.js.map +1 -0
- package/dist/vxjson.d.ts +113 -0
- package/dist/vxjson.d.ts.map +1 -0
- package/dist/vxjson.js +381 -0
- package/dist/vxjson.js.map +1 -0
- package/dist/xxhash.d.ts +10 -0
- package/dist/xxhash.d.ts.map +1 -0
- package/dist/xxhash.js +35 -0
- package/dist/xxhash.js.map +1 -0
- package/package.json +132 -34
- 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/component-registry.types-CBGONnYn.d.mts +0 -71
- package/dist/component-registry.types-CBGONnYn.d.mts.map +0 -1
- package/dist/content-operations-Dv67amz3.d.mts +0 -852
- package/dist/content-operations-Dv67amz3.d.mts.map +0 -1
- package/dist/data-Db1Iv9T0.mjs +0 -60
- package/dist/data-Db1Iv9T0.mjs.map +0 -1
- package/dist/filesystem-content-api-C1TM0GLo.d.mts +0 -119
- package/dist/filesystem-content-api-C1TM0GLo.d.mts.map +0 -1
- package/dist/filesystem-content-api-kxKY5IUw.mjs +0 -2678
- package/dist/filesystem-content-api-kxKY5IUw.mjs.map +0 -1
- package/dist/index.d.mts +0 -79
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -47
- package/dist/index.mjs.map +0 -1
- package/dist/node.d.mts +0 -378
- package/dist/node.d.mts.map +0 -1
- package/dist/node.mjs +0 -2618
- package/dist/node.mjs.map +0 -1
- package/dist/reader.d.mts +0 -18
- 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 -90
- package/dist/schemas/index.d.mts.map +0 -1
- package/dist/schemas/index.mjs +0 -70
- package/dist/schemas/index.mjs.map +0 -1
- package/dist/site-vNtJ5jCh.mjs +0 -538
- package/dist/site-vNtJ5jCh.mjs.map +0 -1
- package/dist/types-DWt912tu.mjs +0 -33
- package/dist/types-DWt912tu.mjs.map +0 -1
package/src/data.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { ContentAPI } from './content-api.interface.js';
|
|
2
|
+
import type { DataIndex } from './data-index.js';
|
|
3
|
+
import type { ContentManifest, ContentMeta, CreateResult } from './types.js';
|
|
4
|
+
|
|
5
|
+
export interface DataFilters {
|
|
6
|
+
collection?: string;
|
|
7
|
+
locales?: string[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface CreateDataInput {
|
|
11
|
+
collection: string;
|
|
12
|
+
name: string;
|
|
13
|
+
meta?: Partial<ContentMeta>;
|
|
14
|
+
locales: {
|
|
15
|
+
[locale: string]: {
|
|
16
|
+
publishAt?: string | null | false;
|
|
17
|
+
unpublishAt?: string | null | false;
|
|
18
|
+
meta?: Partial<ContentMeta>;
|
|
19
|
+
content: {
|
|
20
|
+
data: Record<string, unknown>;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class Data {
|
|
27
|
+
constructor(
|
|
28
|
+
private api: ContentAPI,
|
|
29
|
+
private dataIndex: DataIndex,
|
|
30
|
+
) {}
|
|
31
|
+
|
|
32
|
+
// Content discovery
|
|
33
|
+
*listContent(filters?: DataFilters): Generator<ContentManifest> {
|
|
34
|
+
// Choose the most efficient method based on filters
|
|
35
|
+
if (filters?.collection) {
|
|
36
|
+
if (filters.locales && filters.locales.length > 0) {
|
|
37
|
+
// Filter by locales after getting collection entries
|
|
38
|
+
for (const manifest of this.dataIndex.getManifestsByCollection(filters.collection)) {
|
|
39
|
+
if (filters.locales.some((locale) => manifest.locales[locale])) {
|
|
40
|
+
yield manifest;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
yield* this.dataIndex.getManifestsByCollection(filters.collection);
|
|
45
|
+
}
|
|
46
|
+
} else if (filters?.locales && filters.locales.length > 0) {
|
|
47
|
+
// For multiple locales, we need to check each
|
|
48
|
+
for (const manifest of this.dataIndex.getAllManifests()) {
|
|
49
|
+
if (filters.locales.some((locale) => manifest.locales[locale])) {
|
|
50
|
+
yield manifest;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
yield* this.dataIndex.getAllManifests();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Get all data collections
|
|
59
|
+
get collections(): Set<string> {
|
|
60
|
+
return this.dataIndex.collections;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Data operations (returns null if not found)
|
|
64
|
+
getByName(collection: string, name: string, locale?: string): ContentManifest | null {
|
|
65
|
+
return this.dataIndex.getByName(collection, name, locale);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Check if data name is available, in any letter case as getNameConflict checks it
|
|
69
|
+
isNameAvailable(collection: string, name: string, excludeId?: string): boolean {
|
|
70
|
+
return this.getNameConflict(collection, name, excludeId) === null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Get the ID of content that conflicts with a data name
|
|
74
|
+
getNameConflict(collection: string, name: string, excludeId?: string): string | null {
|
|
75
|
+
// In any letter case: those names share one file on a case-insensitive disk.
|
|
76
|
+
for (const existing of this.dataIndex.getAllByNameInAnyCase(collection, name)) {
|
|
77
|
+
if (existing.id !== excludeId) return existing.id;
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Check if data name is valid
|
|
83
|
+
isDataNameValid(dataName: string): boolean {
|
|
84
|
+
// Data names must not be empty
|
|
85
|
+
if (!dataName || dataName.trim() === '') {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Data names must not contain dots (would interfere with locale suffixes)
|
|
90
|
+
if (dataName.includes('.')) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Data names must not contain path separators
|
|
95
|
+
if (dataName.includes('/') || dataName.includes('\\')) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Data names should only contain alphanumeric, hyphens, underscores
|
|
100
|
+
return /^[a-zA-Z0-9_-]+$/.test(dataName);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Create data entry with name validation
|
|
104
|
+
async create(input: CreateDataInput): Promise<CreateResult> {
|
|
105
|
+
// Validate data name
|
|
106
|
+
if (!this.isDataNameValid(input.name)) {
|
|
107
|
+
return {
|
|
108
|
+
success: false,
|
|
109
|
+
reason: 'invalid_name',
|
|
110
|
+
error: new Error(`Invalid data name: ${input.name}`),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Check if name is already taken in any locale
|
|
115
|
+
const existing = this.getByName(input.collection, input.name);
|
|
116
|
+
if (existing) {
|
|
117
|
+
return {
|
|
118
|
+
success: false,
|
|
119
|
+
reason: 'name_taken',
|
|
120
|
+
error: new Error(`Data name ${input.name} already exists in collection ${input.collection}`),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Create the content
|
|
125
|
+
return this.api.createContent({
|
|
126
|
+
kind: 'data',
|
|
127
|
+
collection: input.collection,
|
|
128
|
+
type: 'json',
|
|
129
|
+
name: input.name,
|
|
130
|
+
meta: input.meta,
|
|
131
|
+
locales: input.locales,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/* oxlint-disable anti-slop/no-unknown-parameters -- This module is the parser boundary for untrusted build/module descriptor values. */
|
|
2
|
+
/* oxlint-disable anti-slop/no-unsafe-dictionary-type -- The temporary dictionary exists only while this parser validates every field before returning domain types. */
|
|
3
|
+
import type { DataBindingConfig, EditorDataBindings } from './types.js';
|
|
4
|
+
|
|
5
|
+
type PageFeed = NonNullable<DataBindingConfig['pages']>;
|
|
6
|
+
|
|
7
|
+
/** A plain object, not null and not an array. */
|
|
8
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
9
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function isString(value: unknown): value is string {
|
|
13
|
+
return typeof value === 'string';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isPageSort(value: unknown): value is NonNullable<PageFeed['sort']> {
|
|
17
|
+
return value === 'date-desc' || value === 'date-asc' || value === 'title';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A route's page feed with only the fields a feed declares, or undefined when any of them is malformed. */
|
|
21
|
+
function readPageFeed(raw: unknown): PageFeed | undefined {
|
|
22
|
+
if (!isRecord(raw)) return undefined;
|
|
23
|
+
const { prefix, sort, limit } = raw;
|
|
24
|
+
if (!isString(prefix)) return undefined;
|
|
25
|
+
if (sort !== undefined && !isPageSort(sort)) return undefined;
|
|
26
|
+
if (limit !== undefined && !(typeof limit === 'number' && limit > 0)) return undefined;
|
|
27
|
+
return {
|
|
28
|
+
prefix,
|
|
29
|
+
...(sort === undefined ? {} : { sort }),
|
|
30
|
+
...(limit === undefined ? {} : { limit }),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** One route's bindings with only the fields a binding declares, or undefined when any of them is malformed. */
|
|
35
|
+
function readRouteBindings(raw: unknown): DataBindingConfig | undefined {
|
|
36
|
+
if (!isRecord(raw)) return undefined;
|
|
37
|
+
const { collections, locale, pages } = raw;
|
|
38
|
+
if (collections !== undefined && !(Array.isArray(collections) && collections.every(isString))) return undefined;
|
|
39
|
+
if (locale !== undefined && !isString(locale)) return undefined;
|
|
40
|
+
const feed = pages === undefined ? undefined : readPageFeed(pages);
|
|
41
|
+
if (pages !== undefined && feed === undefined) return undefined;
|
|
42
|
+
return {
|
|
43
|
+
...(collections === undefined ? {} : { collections }),
|
|
44
|
+
...(locale === undefined ? {} : { locale }),
|
|
45
|
+
...(feed === undefined ? {} : { pages: feed }),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Reads a site's data bindings off a channel the editor does not control (a
|
|
51
|
+
* build's environment, or a host app that passes them in), or undefined when
|
|
52
|
+
* the value is not bindings at all. A malformed route is dropped and its
|
|
53
|
+
* neighbours kept: one bad route must not cost every other page its data
|
|
54
|
+
* context.
|
|
55
|
+
*/
|
|
56
|
+
export function readEditorDataBindings(raw: unknown): EditorDataBindings | undefined {
|
|
57
|
+
if (!isRecord(raw)) return undefined;
|
|
58
|
+
const { siteName, locale, routes } = raw;
|
|
59
|
+
if (!isString(siteName) || !isString(locale) || !isRecord(routes)) return undefined;
|
|
60
|
+
const readRoutes: Record<string, DataBindingConfig> = {};
|
|
61
|
+
for (const [collection, route] of Object.entries(routes)) {
|
|
62
|
+
const bindings = readRouteBindings(route);
|
|
63
|
+
if (bindings !== undefined) readRoutes[collection] = bindings;
|
|
64
|
+
}
|
|
65
|
+
return { siteName, locale, routes: readRoutes };
|
|
66
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { JsonWhitespaceSkippingView } from './json-whitespace-view.js';
|
|
3
|
+
import { bigintToUrlSafeBase64, toUrlSafeBase64 } from './url-safe-base64.js';
|
|
4
|
+
import { createXxh64 } from './xxhash.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Dual ETag system for content API
|
|
8
|
+
* - Meta ETag: Based on non-derived LocaleManifest fields (using XXH64)
|
|
9
|
+
* - Content ETag: Based on ContentProp.content (using SHA256)
|
|
10
|
+
* Both skip whitespace to allow pretty-printing without affecting hashes
|
|
11
|
+
*
|
|
12
|
+
* Format: "metaEtag.contentEtag" in query strings
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Calculate metadata ETag (XXH64) from buffer view
|
|
17
|
+
* @param buffer - The file buffer view
|
|
18
|
+
* @param start - Start position (inclusive)
|
|
19
|
+
* @param end - End position (exclusive), or -1 for entire buffer
|
|
20
|
+
*/
|
|
21
|
+
export function calculateMetaEtagFromBuffer(buffer: Uint8Array, start = 0, end = -1): string {
|
|
22
|
+
const actualEnd = end === -1 ? buffer.length : end;
|
|
23
|
+
const hasher = createXxh64();
|
|
24
|
+
const jsonView = new JsonWhitespaceSkippingView(hasher);
|
|
25
|
+
|
|
26
|
+
jsonView.updateBuffer(buffer, start, actualEnd);
|
|
27
|
+
|
|
28
|
+
return bigintToUrlSafeBase64(hasher.digest());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Calculate content ETag using SHA256 from buffer view
|
|
33
|
+
* @param buffer - The file buffer view
|
|
34
|
+
* @param start - Start position (inclusive)
|
|
35
|
+
* @param end - End position (exclusive), or -1 for entire buffer
|
|
36
|
+
*/
|
|
37
|
+
export function calculateContentEtagFromBuffer(buffer: Uint8Array, start = 0, end = -1): string {
|
|
38
|
+
const actualEnd = end === -1 ? buffer.length : end;
|
|
39
|
+
const hasher = createHash('sha256');
|
|
40
|
+
const jsonView = new JsonWhitespaceSkippingView(hasher);
|
|
41
|
+
|
|
42
|
+
jsonView.updateBuffer(buffer, start, actualEnd);
|
|
43
|
+
|
|
44
|
+
return toUrlSafeBase64(hasher.digest() as Uint8Array);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const dashCode = '-'.charCodeAt(0); // 45
|
|
48
|
+
const newlineCode = '\n'.charCodeAt(0); // 10
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Find the content start position in MDX buffer (after second ---)
|
|
52
|
+
* @param buffer - The MDX content buffer
|
|
53
|
+
* @returns Position where content starts, or -1 if not found
|
|
54
|
+
*/
|
|
55
|
+
export function findMdxContentStartPosition(buffer: Uint8Array): number {
|
|
56
|
+
let dashCount = 0;
|
|
57
|
+
let i = 0;
|
|
58
|
+
|
|
59
|
+
// Look for the second occurrence of "---\n"
|
|
60
|
+
while (i < buffer.length - 4) {
|
|
61
|
+
if (
|
|
62
|
+
buffer[i] === dashCode &&
|
|
63
|
+
buffer[i + 1] === dashCode &&
|
|
64
|
+
buffer[i + 2] === dashCode &&
|
|
65
|
+
buffer[i + 3] === newlineCode
|
|
66
|
+
) {
|
|
67
|
+
dashCount++;
|
|
68
|
+
if (dashCount === 2) {
|
|
69
|
+
// Found second ---, now look for the following newline to get content start
|
|
70
|
+
i += 4; // Skip past "---\n"
|
|
71
|
+
if (i < buffer.length && buffer[i] === newlineCode) {
|
|
72
|
+
return i + 1; // Return position after the empty line
|
|
73
|
+
}
|
|
74
|
+
return i; // Return position right after "---\n"
|
|
75
|
+
}
|
|
76
|
+
i += 4; // Skip this "---\n" and continue looking
|
|
77
|
+
} else {
|
|
78
|
+
// Use indexOf to skip ahead faster instead of incrementing by 1
|
|
79
|
+
const nextDashPos = buffer.indexOf(dashCode, i + 1);
|
|
80
|
+
if (nextDashPos === -1) {
|
|
81
|
+
break; // No more dashes found
|
|
82
|
+
}
|
|
83
|
+
i = nextDashPos;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return -1; // Not found
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Calculate both ETags from an MDX buffer
|
|
92
|
+
*
|
|
93
|
+
* MDX files use YAML frontmatter which has different characteristics than JSON:
|
|
94
|
+
* - YAML does not require quotes around keys or string values
|
|
95
|
+
* - YAML allows multiple formatting styles (flow vs block style)
|
|
96
|
+
* - YAML field order is not significant for parsing
|
|
97
|
+
*
|
|
98
|
+
* Unlike VXJSON which requires "content" to be the last field and needs special
|
|
99
|
+
* parsing to validate structure, YAML frontmatter can be hashed as-is because:
|
|
100
|
+
* 1. The frontmatter boundaries are clearly delimited by ---
|
|
101
|
+
* 2. Different YAML formatting of the same data SHOULD produce different hashes
|
|
102
|
+
* (e.g., flow style [a,b] vs block style with dashes)
|
|
103
|
+
* 3. There's no requirement to validate field ordering
|
|
104
|
+
*
|
|
105
|
+
* This means we can directly hash the YAML bytes without any whitespace
|
|
106
|
+
* normalization or parsing, which is both simpler and more correct.
|
|
107
|
+
*
|
|
108
|
+
* @param buffer - The complete file buffer
|
|
109
|
+
* @param contentStartPos - Position where content starts (after second ---), or -1 if no content
|
|
110
|
+
*/
|
|
111
|
+
export function calculateEtagsFromMdxBuffer(
|
|
112
|
+
buffer: Uint8Array,
|
|
113
|
+
contentStartPos: number,
|
|
114
|
+
): {
|
|
115
|
+
metaEtag: string;
|
|
116
|
+
contentEtag: string;
|
|
117
|
+
} {
|
|
118
|
+
if (contentStartPos === -1) {
|
|
119
|
+
// No content, only frontmatter - hash entire buffer as metadata
|
|
120
|
+
const hasher = createXxh64();
|
|
121
|
+
hasher.update(buffer);
|
|
122
|
+
const metaEtag = bigintToUrlSafeBase64(hasher.digest());
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
metaEtag,
|
|
126
|
+
contentEtag: calculateContentEtagFromBuffer(new Uint8Array(0)), // empty content
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// For MDX, we hash the YAML frontmatter as-is (including the --- markers)
|
|
131
|
+
// This is different from JSON where we need to parse and validate structure
|
|
132
|
+
const metaHasher = createXxh64();
|
|
133
|
+
metaHasher.update(buffer.subarray(0, contentStartPos));
|
|
134
|
+
const metaEtag = bigintToUrlSafeBase64(metaHasher.digest());
|
|
135
|
+
|
|
136
|
+
// Content is everything after the second ---
|
|
137
|
+
const contentHasher = createHash('sha256');
|
|
138
|
+
contentHasher.update(buffer.subarray(contentStartPos));
|
|
139
|
+
const contentEtag = toUrlSafeBase64(contentHasher.digest() as Uint8Array);
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
metaEtag,
|
|
143
|
+
contentEtag,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Combined ETag structure
|
|
149
|
+
*/
|
|
150
|
+
export interface DualEtag {
|
|
151
|
+
meta: string;
|
|
152
|
+
content: string;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Format dual ETags for query string: "meta.content"
|
|
157
|
+
*/
|
|
158
|
+
export function formatDualEtag(etags: DualEtag): string {
|
|
159
|
+
return `${etags.meta}.${etags.content}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Parse dual ETags from query string: "meta.content"
|
|
164
|
+
*/
|
|
165
|
+
export function parseDualEtag(etag: string): DualEtag | null {
|
|
166
|
+
const parts = etag.split('.');
|
|
167
|
+
if (parts.length !== 2) return null;
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
meta: parts[0],
|
|
171
|
+
content: parts[1],
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Check if etag matches either meta or content
|
|
177
|
+
*/
|
|
178
|
+
export function matchesEtag(
|
|
179
|
+
providedEtag: string,
|
|
180
|
+
currentMeta: string,
|
|
181
|
+
currentContent: string,
|
|
182
|
+
): {
|
|
183
|
+
matches: boolean;
|
|
184
|
+
metaChanged: boolean;
|
|
185
|
+
contentChanged: boolean;
|
|
186
|
+
} {
|
|
187
|
+
const parsed = parseDualEtag(providedEtag);
|
|
188
|
+
if (!parsed) {
|
|
189
|
+
return {
|
|
190
|
+
matches: false,
|
|
191
|
+
metaChanged: true,
|
|
192
|
+
contentChanged: true,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const metaChanged = parsed.meta !== currentMeta;
|
|
197
|
+
const contentChanged = parsed.content !== currentContent;
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
matches: !metaChanged && !contentChanged,
|
|
201
|
+
metaChanged,
|
|
202
|
+
contentChanged,
|
|
203
|
+
};
|
|
204
|
+
}
|