@expofp/config 0.0.0-experimental.cf063fc

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.
Files changed (44) hide show
  1. package/README.md +40 -0
  2. package/dist/index.d.ts +13 -0
  3. package/dist/index.js +12 -0
  4. package/dist/lib/apply-intents.d.ts +10 -0
  5. package/dist/lib/apply-intents.js +24 -0
  6. package/dist/lib/config-store.d.ts +8 -0
  7. package/dist/lib/config-store.js +16 -0
  8. package/dist/lib/debug-settings.d.ts +16 -0
  9. package/dist/lib/debug-settings.js +44 -0
  10. package/dist/lib/deprecated-url-params.d.ts +33 -0
  11. package/dist/lib/deprecated-url-params.js +72 -0
  12. package/dist/lib/json-schema.d.ts +67 -0
  13. package/dist/lib/json-schema.js +136 -0
  14. package/dist/lib/legacy-url.d.ts +162 -0
  15. package/dist/lib/legacy-url.js +416 -0
  16. package/dist/lib/load-config.d.ts +27 -0
  17. package/dist/lib/load-config.js +373 -0
  18. package/dist/lib/local-storage-codec.d.ts +44 -0
  19. package/dist/lib/local-storage-codec.js +76 -0
  20. package/dist/lib/normalize-fp-svg.d.ts +26 -0
  21. package/dist/lib/normalize-fp-svg.js +36 -0
  22. package/dist/lib/normalize-legacy-data.d.ts +13 -0
  23. package/dist/lib/normalize-legacy-data.js +90 -0
  24. package/dist/lib/rebooking.d.ts +19 -0
  25. package/dist/lib/rebooking.js +48 -0
  26. package/dist/lib/resource-urls.d.ts +52 -0
  27. package/dist/lib/resource-urls.js +203 -0
  28. package/dist/lib/serialize-config-resources.d.ts +46 -0
  29. package/dist/lib/serialize-config-resources.js +50 -0
  30. package/dist/lib/store-ref.d.ts +40 -0
  31. package/dist/lib/store-ref.js +56 -0
  32. package/dist/lib/strip-defaults.d.ts +27 -0
  33. package/dist/lib/strip-defaults.js +55 -0
  34. package/dist/lib/url-codec.d.ts +46 -0
  35. package/dist/lib/url-codec.js +165 -0
  36. package/dist/lib/url-intents.d.ts +40 -0
  37. package/dist/lib/url-intents.js +71 -0
  38. package/dist/lib/validate-flag.d.ts +10 -0
  39. package/dist/lib/validate-flag.js +36 -0
  40. package/dist/lib/validate-timing.d.ts +3 -0
  41. package/dist/lib/validate-timing.js +52 -0
  42. package/dist/lib/visit-resources.d.ts +99 -0
  43. package/dist/lib/visit-resources.js +209 -0
  44. package/package.json +36 -0
@@ -0,0 +1,209 @@
1
+ /**
2
+ * visit-resources — the one schema-guided resource walk, used for both jobs:
3
+ *
4
+ * - **Absolutization** (`documentRef`): every ref the config mints carries a
5
+ * `process` hook = "validate with my schema, then walk me with a handler
6
+ * that rebases relative paths onto the ref's own directory". Resolution
7
+ * therefore always settles documents whose asset URLs are absolute.
8
+ * - **Serialization** (`serializeConfigResources`): the same walk over the
9
+ * whole config with a storage handler — and a `$ref` is just another kind
10
+ * of asset, one whose handling resolves the document and recurses into it.
11
+ *
12
+ * The walk is value-driven and schema-guided: meta tags say what a string
13
+ * leaf holds (`asset`, `svgAsset`, `cssAsset`), and a
14
+ * `{ $ref }` object is an external document (its node's `refSchema` meta
15
+ * points at the document's schema).
16
+ * Which behaviors fire falls out of which hooks the handler provides — a
17
+ * handler without `document` leaves refs alone. `value` is mutated in place
18
+ * and must be an object or array.
19
+ */
20
+ import { getResolved, isResolved, resolve } from '@expofp/resolve';
21
+ import { deepClone } from '@expofp/utils';
22
+ import { refSchemaOf, toJsonSchema, typeMatches } from './json-schema.js';
23
+ import { collectCssUrls, collectSvgImageAssets, inlineCssImports, isAbsoluteUrl, refDirectory, rewriteCssUrls, rewriteSvgImageAssets, toAbsoluteUrl, } from './resource-urls.js';
24
+ import { isStoreRef, resolveUrl } from './store-ref.js';
25
+ import { timeValidation } from './validate-timing.js';
26
+ /**
27
+ * Gates every schema validation (`parse`) of the large expo documents —
28
+ * manifest, data.js, drawing/wayfinding payloads. The schemas are loose and
29
+ * transform-free, so a validating parse is also a faithful copy — which is
30
+ * exactly what {@link validateOrClone} exploits.
31
+ *
32
+ * OFF by default (parsing the large documents costs time); `loadConfig` flips it
33
+ * per page load via {@link setValidateManifest} from the `validateSchema` opt-in
34
+ * (`?validateSchema=1` / `efp-validateSchema`). Module-level, tracking the
35
+ * page-global URL/localStorage flag, so concurrent loads on a page agree.
36
+ */
37
+ let validateManifest = false;
38
+ /** Set by `loadConfig` from the `validateSchema` flag; gates the parses below. */
39
+ export function setValidateManifest(on) {
40
+ validateManifest = on;
41
+ }
42
+ /**
43
+ * An own, mutable copy of a document — validated when the flag is on.
44
+ *
45
+ * Every consumer of a resolved document needs its own copy anyway (`resolve`
46
+ * deep-freezes what it settles, and downstream code mutates in place), and a
47
+ * Zod parse of these loose, transform-free schemas already IS a fresh copy.
48
+ * So validation rides the copy that had to be made regardless: parse when
49
+ * validating, `deepClone` when not — one copy either way, never two.
50
+ */
51
+ export function validateOrClone(schema, value) {
52
+ if (validateManifest)
53
+ return timeValidation(() => schema.parse(value));
54
+ return deepClone(value);
55
+ }
56
+ /**
57
+ * Decorate a ref with the one processing rule for referenced documents: take
58
+ * an own copy — validated against `schema` when the flag is on (see
59
+ * `validateOrClone`) — then make every schema-tagged resource path absolute
60
+ * against the ref's own directory — the standard web rule for a live load.
61
+ * An optional `normalize` hook runs on the copy in between: it folds legacy
62
+ * shape quirks onto the canonical keys, so the schema-guided walk (and every
63
+ * consumer) sees the canonical shape (e.g. `normalizeFpSvgLayerAliases`).
64
+ *
65
+ * An offline copy instead references its saved documents by *relative*
66
+ * (page-relative, host-less) paths, and their assets already carry final
67
+ * serialized paths. Rebasing one of those onto the doc's own relative
68
+ * directory would double the path (`…/<doc-dir>/<doc-dir>/asset` → 404), so
69
+ * absolutization is skipped whenever the ref's directory is not itself
70
+ * absolute — the runtime then resolves the page-relative asset against the
71
+ * page, wherever the copy is mounted. (A rooted `/…` offline ref stays
72
+ * absolute, and its already-rooted assets pass through unchanged either way.)
73
+ *
74
+ * The rewrite runs on an own copy, so it never touches the fetch-cached raw
75
+ * document.
76
+ */
77
+ export function documentRef(ref, schema, normalize) {
78
+ const process = async (raw, refUrl) => {
79
+ const document = validateOrClone(schema, raw); // referenced documents are objects
80
+ normalize?.(document);
81
+ // A relative ref directory means an offline copy: the document's assets are
82
+ // already final serialized paths, so rebasing them here would double the path.
83
+ const base = refDirectory(refUrl);
84
+ const rebase = isAbsoluteUrl(base);
85
+ await visitResources(schema, document, {
86
+ // A store ref (`$/…` — normalized snapshots) expands by the fixed
87
+ // convention, wherever the document came from (store-ref.ts); other
88
+ // paths get the standard web rebase.
89
+ asset: (url) => (isStoreRef(url) ? resolveUrl(url) : rebase ? toAbsoluteUrl(url, base) : url),
90
+ });
91
+ return document;
92
+ };
93
+ return { ...ref, process };
94
+ }
95
+ /** Rewrite every schema-tagged resource of `value` through `handler`, in place. */
96
+ export async function visitResources(schema, value, handler, options) {
97
+ await walk(toJsonSchema(schema), value, unassignableRoot, handler, options);
98
+ }
99
+ async function walk(node, value, assign, handler, options) {
100
+ if (value === null || value === undefined)
101
+ return;
102
+ // follow unions down to the branch that fits the value
103
+ const branches = node?.anyOf ?? node?.oneOf;
104
+ if (branches) {
105
+ const branch = branches.find((candidate) => typeMatches(candidate, value));
106
+ return walk(branch, value, assign, handler, options);
107
+ }
108
+ // "$ref is an asset": an external document, when the handler wants them.
109
+ // Rooted refs are already local — honored verbatim, like every other
110
+ // rooted resource URL.
111
+ if (isRefShaped(value)) {
112
+ if (handler.ref) {
113
+ assign({ $ref: await handler.ref(value.$ref) });
114
+ return;
115
+ }
116
+ if (!handler.document || value.$ref.startsWith('/'))
117
+ return;
118
+ const refUrl = value.$ref;
119
+ try {
120
+ const documentSchema = node ? refSchemaOf(node) : undefined;
121
+ // Reuse the warm memo (eagerly-loaded refs settled processed documents);
122
+ // a cold ref is re-minted with its document schema — the walked value is
123
+ // a JSON copy, so the original decoration did not survive into it.
124
+ const resolved = isResolved(value)
125
+ ? getResolved(value)
126
+ : await resolve(documentSchema ? documentRef({ $ref: refUrl }, documentSchema) : { $ref: refUrl }, { signal: options?.signal });
127
+ const copy = { document: deepClone(resolved) };
128
+ if (documentSchema) {
129
+ await walk(toJsonSchema(documentSchema), copy.document, (v) => {
130
+ copy.document = v;
131
+ }, handler, options);
132
+ }
133
+ assign({ $ref: await handler.document(refUrl, copy.document) });
134
+ }
135
+ catch (error) {
136
+ if (!handler.documentError)
137
+ throw error;
138
+ handler.documentError(refUrl, error);
139
+ assign(undefined); // the field is dropped
140
+ }
141
+ return;
142
+ }
143
+ if (typeof value === 'string') {
144
+ if (node?.asset && value) {
145
+ assign(await handler.asset(value, node.optionalAsset));
146
+ }
147
+ else if (node?.svgAsset) {
148
+ const rewrites = new Map();
149
+ for (const url of collectSvgImageAssets(value)) {
150
+ rewrites.set(url, await handler.asset(url));
151
+ }
152
+ assign(rewriteSvgImageAssets(value, (url) => rewrites.get(url) ?? url));
153
+ }
154
+ else if (node?.cssAsset) {
155
+ // a handler that can fetch stylesheets gets @imports spliced in first,
156
+ // so the pass below localizes the assets they pull in (fonts etc.)
157
+ let css = value;
158
+ let external = new Set();
159
+ if (handler.stylesheetText) {
160
+ ({ css, external } = await inlineCssImports(css, handler.stylesheetText.bind(handler)));
161
+ }
162
+ const rewrites = new Map();
163
+ for (const url of collectCssUrls(css)) {
164
+ if (external.has(url))
165
+ continue; // a kept @import's URL stays remote
166
+ // Only scheme-qualified CSS urls are fetchable resources (legacy
167
+ // parity) — plus store refs (`$/…`), which a normalized snapshot
168
+ // carries there and the load must expand back symmetrically.
169
+ if (isStoreRef(url) || /^[a-z][a-z0-9+.-]*:/i.test(url)) {
170
+ rewrites.set(url, await handler.asset(url));
171
+ }
172
+ }
173
+ assign(rewriteCssUrls(css, (url) => rewrites.get(url) ?? url));
174
+ }
175
+ return;
176
+ }
177
+ if (Array.isArray(value)) {
178
+ for (let index = 0; index < value.length; index++) {
179
+ await walk(node?.items, value[index], (v) => {
180
+ value[index] = v;
181
+ }, handler, options);
182
+ }
183
+ return;
184
+ }
185
+ if (typeof value === 'object') {
186
+ const container = value;
187
+ // `z.record(...)` keys are dynamic, so the value schema rides on
188
+ // `additionalProperties`, not `properties` (e.g. `fpSvgLayerJsRefs`, whose
189
+ // per-layer `$ref` must resolve + localize like any other). Fall back to it
190
+ // so record values are walked, not skipped as untyped and left verbatim.
191
+ const additional = typeof node?.additionalProperties === 'object' ? node.additionalProperties : undefined;
192
+ for (const key of Object.keys(container)) {
193
+ await walk(node?.properties?.[key] ?? additional, container[key], (v) => {
194
+ if (v === undefined)
195
+ delete container[key];
196
+ else
197
+ container[key] = v;
198
+ }, handler, options);
199
+ }
200
+ }
201
+ }
202
+ /** Package-internal: `loadConfig`'s pointer-chain loop probes with it too. */
203
+ export function isRefShaped(value) {
204
+ return (typeof value === 'object' && value !== null && '$ref' in value && typeof value.$ref === 'string');
205
+ }
206
+ /** The root is an object/array container — leaves rewrite inside it. */
207
+ function unassignableRoot() {
208
+ throw new Error('visitResources: the top-level value must be an object or array');
209
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@expofp/config",
3
+ "version": "0.0.0-experimental.cf063fc",
4
+ "type": "module",
5
+ "description": "ExpoFP SDK internal: config layer and schemas",
6
+ "homepage": "https://developer.expofp.com/",
7
+ "license": "MIT",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "main": "./dist/index.js",
12
+ "module": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ "./package.json": "./package.json",
16
+ ".": {
17
+ "@expofp/source": "./src/index.ts",
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "default": "./dist/index.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "!**/*.tsbuildinfo",
26
+ "!**/*.map"
27
+ ],
28
+ "dependencies": {
29
+ "debug": "^4.4.3",
30
+ "tslib": "^2.3.0",
31
+ "zod": "4.4.3",
32
+ "@expofp/resolve": "0.0.0-experimental.cf063fc",
33
+ "@expofp/schema": "0.0.0-experimental.cf063fc",
34
+ "@expofp/utils": "0.0.0-experimental.cf063fc"
35
+ }
36
+ }