@conloca/astro-cms 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/README.md +259 -247
- package/dist/internal/acorn-default.mjs +11 -3
- package/dist/internal/page-handler.astro +137 -358
- package/dist/ts/node/content.d.ts +4 -0
- package/dist/ts/node/content.d.ts.map +1 -0
- package/dist/ts/node/content.js +3 -0
- package/dist/ts/node/content.js.map +1 -0
- package/dist/ts/node/index.d.ts +9 -0
- package/dist/ts/node/index.d.ts.map +1 -0
- package/dist/ts/node/index.js +14 -0
- package/dist/ts/node/index.js.map +1 -0
- package/dist/ts/src/asset-ref-base.d.ts +3 -0
- package/dist/ts/src/asset-ref-base.d.ts.map +1 -0
- package/dist/ts/src/asset-ref-base.js +48 -0
- package/dist/ts/src/asset-ref-base.js.map +1 -0
- package/dist/ts/src/checks.d.ts +7 -0
- package/dist/ts/src/checks.d.ts.map +1 -0
- package/dist/ts/src/checks.js +7 -0
- package/dist/ts/src/checks.js.map +1 -0
- package/dist/ts/src/cms-handler.d.ts +18 -0
- package/dist/ts/src/cms-handler.d.ts.map +1 -0
- package/dist/ts/src/cms-handler.js +245 -0
- package/dist/ts/src/cms-handler.js.map +1 -0
- package/dist/ts/src/collections.d.ts +160 -0
- package/dist/ts/src/collections.d.ts.map +1 -0
- package/dist/ts/src/collections.js +170 -0
- package/dist/ts/src/collections.js.map +1 -0
- package/dist/ts/src/components/HydrationWrapper.d.ts +18 -0
- package/dist/ts/src/components/HydrationWrapper.d.ts.map +1 -0
- package/dist/ts/src/components/HydrationWrapper.js +22 -0
- package/dist/ts/src/components/HydrationWrapper.js.map +1 -0
- package/dist/ts/src/components/RenderWithFragments.d.ts +27 -0
- package/dist/ts/src/components/RenderWithFragments.d.ts.map +1 -0
- package/dist/ts/src/components/RenderWithFragments.js +133 -0
- package/dist/ts/src/components/RenderWithFragments.js.map +1 -0
- package/dist/ts/src/components/RenderWithHydration.d.ts +21 -0
- package/dist/ts/src/components/RenderWithHydration.d.ts.map +1 -0
- package/dist/ts/src/components/RenderWithHydration.js +39 -0
- package/dist/ts/src/components/RenderWithHydration.js.map +1 -0
- package/dist/ts/src/components.d.ts +4 -0
- package/dist/ts/src/components.d.ts.map +1 -0
- package/dist/ts/src/components.js +4 -0
- package/dist/ts/src/components.js.map +1 -0
- package/dist/ts/src/discovery/astro-frontmatter.d.ts +18 -0
- package/dist/ts/src/discovery/astro-frontmatter.d.ts.map +1 -0
- package/dist/ts/src/discovery/astro-frontmatter.js +25 -0
- package/dist/ts/src/discovery/astro-frontmatter.js.map +1 -0
- package/dist/ts/src/discovery/discover.d.ts +43 -0
- package/dist/ts/src/discovery/discover.d.ts.map +1 -0
- package/dist/ts/src/discovery/discover.js +87 -0
- package/dist/ts/src/discovery/discover.js.map +1 -0
- package/dist/ts/src/discovery/extract-props-ts.d.ts +41 -0
- package/dist/ts/src/discovery/extract-props-ts.d.ts.map +1 -0
- package/dist/ts/src/discovery/extract-props-ts.js +232 -0
- package/dist/ts/src/discovery/extract-props-ts.js.map +1 -0
- package/dist/ts/src/discovery/merge-registry.d.ts +58 -0
- package/dist/ts/src/discovery/merge-registry.d.ts.map +1 -0
- package/dist/ts/src/discovery/merge-registry.js +333 -0
- package/dist/ts/src/discovery/merge-registry.js.map +1 -0
- package/dist/ts/src/discovery/registry-endpoint.d.ts +27 -0
- package/dist/ts/src/discovery/registry-endpoint.d.ts.map +1 -0
- package/dist/ts/src/discovery/registry-endpoint.js +76 -0
- package/dist/ts/src/discovery/registry-endpoint.js.map +1 -0
- package/dist/ts/src/discovery/scan-components.d.ts +94 -0
- package/dist/ts/src/discovery/scan-components.d.ts.map +1 -0
- package/dist/ts/src/discovery/scan-components.js +397 -0
- package/dist/ts/src/discovery/scan-components.js.map +1 -0
- package/dist/ts/src/discovery/scan-mdx.d.ts +63 -0
- package/dist/ts/src/discovery/scan-mdx.d.ts.map +1 -0
- package/dist/ts/src/discovery/scan-mdx.js +113 -0
- package/dist/ts/src/discovery/scan-mdx.js.map +1 -0
- package/dist/ts/src/discovery/scan-overrides.d.ts +71 -0
- package/dist/ts/src/discovery/scan-overrides.d.ts.map +1 -0
- package/dist/ts/src/discovery/scan-overrides.js +34 -0
- package/dist/ts/src/discovery/scan-overrides.js.map +1 -0
- package/dist/ts/src/editor-descriptors.d.ts +31 -0
- package/dist/ts/src/editor-descriptors.d.ts.map +1 -0
- package/dist/ts/src/editor-descriptors.js +84 -0
- package/dist/ts/src/editor-descriptors.js.map +1 -0
- package/dist/ts/src/handlers/collection-content-api.d.ts +32 -0
- package/dist/ts/src/handlers/collection-content-api.d.ts.map +1 -0
- package/dist/ts/src/handlers/collection-content-api.js +166 -0
- package/dist/ts/src/handlers/collection-content-api.js.map +1 -0
- package/dist/ts/src/handlers/page-render-core.d.ts +137 -0
- package/dist/ts/src/handlers/page-render-core.d.ts.map +1 -0
- package/dist/ts/src/handlers/page-render-core.js +315 -0
- package/dist/ts/src/handlers/page-render-core.js.map +1 -0
- package/dist/ts/src/hydration.d.ts +4 -0
- package/dist/ts/src/hydration.d.ts.map +1 -0
- package/dist/ts/src/hydration.js +5 -0
- package/dist/ts/src/hydration.js.map +1 -0
- package/dist/ts/src/index.d.ts +21 -0
- package/dist/ts/src/index.d.ts.map +1 -0
- package/dist/ts/src/index.js +45 -0
- package/dist/ts/src/index.js.map +1 -0
- package/dist/ts/src/internal/declared-collections.d.ts +30 -0
- package/dist/ts/src/internal/declared-collections.d.ts.map +1 -0
- package/dist/ts/src/internal/declared-collections.js +84 -0
- package/dist/ts/src/internal/declared-collections.js.map +1 -0
- package/dist/ts/src/internal/page-collection-schema.d.ts +44 -0
- package/dist/ts/src/internal/page-collection-schema.d.ts.map +1 -0
- package/dist/ts/src/internal/page-collection-schema.js +152 -0
- package/dist/ts/src/internal/page-collection-schema.js.map +1 -0
- package/dist/ts/src/lib/content-config-guard.d.ts +11 -0
- package/dist/ts/src/lib/content-config-guard.d.ts.map +1 -0
- package/dist/ts/src/lib/content-config-guard.js +41 -0
- package/dist/ts/src/lib/content-config-guard.js.map +1 -0
- package/dist/ts/src/lib/hydration-scanner.d.ts +47 -0
- package/dist/ts/src/lib/hydration-scanner.d.ts.map +1 -0
- package/dist/ts/src/lib/hydration-scanner.js +136 -0
- package/dist/ts/src/lib/hydration-scanner.js.map +1 -0
- package/dist/{lib/hydration-script.d.mts → ts/src/lib/hydration-script.d.ts} +7 -10
- package/dist/ts/src/lib/hydration-script.d.ts.map +1 -0
- package/dist/ts/src/lib/hydration-script.js +89 -0
- package/dist/ts/src/lib/hydration-script.js.map +1 -0
- package/dist/ts/src/lib/hydration-utils.d.ts +51 -0
- package/dist/ts/src/lib/hydration-utils.d.ts.map +1 -0
- package/dist/ts/src/lib/hydration-utils.js +84 -0
- package/dist/ts/src/lib/hydration-utils.js.map +1 -0
- package/dist/ts/src/lib/resolve-locale.d.ts +15 -0
- package/dist/ts/src/lib/resolve-locale.d.ts.map +1 -0
- package/dist/ts/src/lib/resolve-locale.js +16 -0
- package/dist/ts/src/lib/resolve-locale.js.map +1 -0
- package/dist/ts/src/lib/resolve-site-name.d.ts +8 -0
- package/dist/ts/src/lib/resolve-site-name.d.ts.map +1 -0
- package/dist/ts/src/lib/resolve-site-name.js +12 -0
- package/dist/ts/src/lib/resolve-site-name.js.map +1 -0
- package/dist/ts/src/lib/route-utils.d.ts +61 -0
- package/dist/ts/src/lib/route-utils.d.ts.map +1 -0
- package/dist/ts/src/lib/route-utils.js +96 -0
- package/dist/ts/src/lib/route-utils.js.map +1 -0
- package/dist/ts/src/lib/routing-config.d.ts +38 -0
- package/dist/ts/src/lib/routing-config.d.ts.map +1 -0
- package/dist/ts/src/lib/routing-config.js +106 -0
- package/dist/ts/src/lib/routing-config.js.map +1 -0
- package/dist/ts/src/lib/serialize-props.d.ts +6 -0
- package/dist/ts/src/lib/serialize-props.d.ts.map +1 -0
- package/dist/ts/src/lib/serialize-props.js +8 -0
- package/dist/ts/src/lib/serialize-props.js.map +1 -0
- package/dist/ts/src/lib/unknown-components.d.ts +21 -0
- package/dist/ts/src/lib/unknown-components.d.ts.map +1 -0
- package/dist/ts/src/lib/unknown-components.js +49 -0
- package/dist/ts/src/lib/unknown-components.js.map +1 -0
- package/dist/ts/src/lib/virtual-module-generators.d.ts +33 -0
- package/dist/ts/src/lib/virtual-module-generators.d.ts.map +1 -0
- package/dist/ts/src/lib/virtual-module-generators.js +51 -0
- package/dist/ts/src/lib/virtual-module-generators.js.map +1 -0
- package/dist/{withHydration-HIvO9ysY.d.mts → ts/src/lib/withHydration.d.ts} +7 -11
- package/dist/ts/src/lib/withHydration.d.ts.map +1 -0
- package/dist/ts/src/lib/withHydration.js +50 -0
- package/dist/ts/src/lib/withHydration.js.map +1 -0
- package/dist/ts/src/loader.d.ts +27 -0
- package/dist/ts/src/loader.d.ts.map +1 -0
- package/dist/ts/src/loader.js +118 -0
- package/dist/ts/src/loader.js.map +1 -0
- package/dist/ts/src/locales-helpers.d.ts +61 -0
- package/dist/ts/src/locales-helpers.d.ts.map +1 -0
- package/dist/ts/src/locales-helpers.js +50 -0
- package/dist/ts/src/locales-helpers.js.map +1 -0
- package/dist/ts/src/mime-types.d.ts +3 -0
- package/dist/ts/src/mime-types.d.ts.map +1 -0
- package/dist/ts/src/mime-types.js +20 -0
- package/dist/ts/src/mime-types.js.map +1 -0
- package/dist/ts/src/page-feed.d.ts +13 -0
- package/dist/ts/src/page-feed.d.ts.map +1 -0
- package/dist/ts/src/page-feed.js +20 -0
- package/dist/ts/src/page-feed.js.map +1 -0
- package/dist/ts/src/page-schemas.d.ts +6 -0
- package/dist/ts/src/page-schemas.d.ts.map +1 -0
- package/dist/ts/src/page-schemas.js +6 -0
- package/dist/ts/src/page-schemas.js.map +1 -0
- package/dist/ts/src/path-validation.d.ts +8 -0
- package/dist/ts/src/path-validation.d.ts.map +1 -0
- package/dist/ts/src/path-validation.js +13 -0
- package/dist/ts/src/path-validation.js.map +1 -0
- package/dist/ts/src/plugin-spa.d.ts +182 -0
- package/dist/ts/src/plugin-spa.d.ts.map +1 -0
- package/dist/ts/src/plugin-spa.js +1165 -0
- package/dist/ts/src/plugin-spa.js.map +1 -0
- package/dist/ts/src/puck-manifest/components-endpoint.d.ts +30 -0
- package/dist/ts/src/puck-manifest/components-endpoint.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/components-endpoint.js +54 -0
- package/dist/ts/src/puck-manifest/components-endpoint.js.map +1 -0
- package/dist/ts/src/puck-manifest/field-projection.d.ts +91 -0
- package/dist/ts/src/puck-manifest/field-projection.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/field-projection.js +422 -0
- package/dist/ts/src/puck-manifest/field-projection.js.map +1 -0
- package/dist/ts/src/puck-manifest/index.d.ts +4 -0
- package/dist/ts/src/puck-manifest/index.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/index.js +3 -0
- package/dist/ts/src/puck-manifest/index.js.map +1 -0
- package/dist/ts/src/puck-manifest/literal.d.ts +163 -0
- package/dist/ts/src/puck-manifest/literal.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/literal.js +354 -0
- package/dist/ts/src/puck-manifest/literal.js.map +1 -0
- package/dist/ts/src/puck-manifest/read-config.d.ts +42 -0
- package/dist/ts/src/puck-manifest/read-config.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/read-config.js +517 -0
- package/dist/ts/src/puck-manifest/read-config.js.map +1 -0
- package/dist/ts/src/puck-manifest/read-field.d.ts +7 -0
- package/dist/ts/src/puck-manifest/read-field.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/read-field.js +11 -0
- package/dist/ts/src/puck-manifest/read-field.js.map +1 -0
- package/dist/ts/src/puck-manifest/read-wrapper.d.ts +147 -0
- package/dist/ts/src/puck-manifest/read-wrapper.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/read-wrapper.js +889 -0
- package/dist/ts/src/puck-manifest/read-wrapper.js.map +1 -0
- package/dist/ts/src/puck-manifest/reading-budget.d.ts +50 -0
- package/dist/ts/src/puck-manifest/reading-budget.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/reading-budget.js +113 -0
- package/dist/ts/src/puck-manifest/reading-budget.js.map +1 -0
- package/dist/ts/src/puck-manifest/resolve-reference.d.ts +4 -0
- package/dist/ts/src/puck-manifest/resolve-reference.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/resolve-reference.js +4 -0
- package/dist/ts/src/puck-manifest/resolve-reference.js.map +1 -0
- package/dist/ts/src/puck-manifest/source-reference.d.ts +96 -0
- package/dist/ts/src/puck-manifest/source-reference.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/source-reference.js +424 -0
- package/dist/ts/src/puck-manifest/source-reference.js.map +1 -0
- package/dist/ts/src/puck-manifest/syntax.d.ts +29 -0
- package/dist/ts/src/puck-manifest/syntax.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/syntax.js +79 -0
- package/dist/ts/src/puck-manifest/syntax.js.map +1 -0
- package/dist/ts/src/puck-manifest/types.d.ts +145 -0
- package/dist/ts/src/puck-manifest/types.d.ts.map +1 -0
- package/dist/ts/src/puck-manifest/types.js +2 -0
- package/dist/ts/src/puck-manifest/types.js.map +1 -0
- package/dist/ts/src/safe-json-stringify.d.ts +13 -0
- package/dist/ts/src/safe-json-stringify.d.ts.map +1 -0
- package/dist/ts/src/safe-json-stringify.js +7 -0
- package/dist/ts/src/safe-json-stringify.js.map +1 -0
- package/dist/ts/src/sandbox-frame-handler.d.ts +19 -0
- package/dist/ts/src/sandbox-frame-handler.d.ts.map +1 -0
- package/dist/ts/src/sandbox-frame-handler.js +27 -0
- package/dist/ts/src/sandbox-frame-handler.js.map +1 -0
- package/dist/ts/src/site-styles/content-wrapper-endpoint.d.ts +73 -0
- package/dist/ts/src/site-styles/content-wrapper-endpoint.d.ts.map +1 -0
- package/dist/ts/src/site-styles/content-wrapper-endpoint.js +224 -0
- package/dist/ts/src/site-styles/content-wrapper-endpoint.js.map +1 -0
- package/dist/ts/src/site-styles/render-endpoint.d.ts +50 -0
- package/dist/ts/src/site-styles/render-endpoint.d.ts.map +1 -0
- package/dist/ts/src/site-styles/render-endpoint.js +264 -0
- package/dist/ts/src/site-styles/render-endpoint.js.map +1 -0
- package/dist/ts/src/site-styles/styles-endpoint.d.ts +59 -0
- package/dist/ts/src/site-styles/styles-endpoint.d.ts.map +1 -0
- package/dist/ts/src/site-styles/styles-endpoint.js +156 -0
- package/dist/ts/src/site-styles/styles-endpoint.js.map +1 -0
- package/dist/ts/src/sitemap-options.d.ts +21 -0
- package/dist/ts/src/sitemap-options.d.ts.map +1 -0
- package/dist/ts/src/sitemap-options.js +92 -0
- package/dist/ts/src/sitemap-options.js.map +1 -0
- package/dist/ts/src/tiptap-html-static.d.ts +15 -0
- package/dist/ts/src/tiptap-html-static.d.ts.map +1 -0
- package/dist/ts/src/tiptap-html-static.js +67 -0
- package/dist/ts/src/tiptap-html-static.js.map +1 -0
- package/dist/ts/src/types.d.ts +543 -0
- package/dist/ts/src/types.d.ts.map +1 -0
- package/dist/ts/src/types.js +2 -0
- package/dist/ts/src/types.js.map +1 -0
- package/dist/virtual-modules.d.ts +74 -0
- package/node/content.ts +14 -0
- package/node/index.ts +22 -0
- package/package.json +198 -56
- package/src/asset-ref-base.ts +48 -0
- package/src/checks.ts +10 -0
- package/src/cms-handler.ts +280 -0
- package/src/collections.ts +262 -0
- package/src/components/HydrationWrapper.tsx +41 -0
- package/src/components/RenderWithFragments.tsx +204 -0
- package/src/components/RenderWithHydration.tsx +55 -0
- package/src/components.ts +3 -0
- package/src/discovery/astro-frontmatter.ts +24 -0
- package/src/discovery/discover.ts +108 -0
- package/src/discovery/extract-props-ts.ts +255 -0
- package/src/discovery/merge-registry.ts +411 -0
- package/src/discovery/registry-endpoint.ts +80 -0
- package/src/discovery/scan-components.ts +458 -0
- package/src/discovery/scan-mdx.ts +190 -0
- package/src/discovery/scan-overrides.ts +92 -0
- package/src/editor-descriptors.ts +96 -0
- package/src/env.d.ts +31 -0
- package/src/handlers/collection-content-api.ts +196 -0
- package/src/handlers/page-handler.astro +320 -0
- package/src/handlers/page-render-core.ts +456 -0
- package/src/hydration.ts +7 -0
- package/src/index.ts +111 -0
- package/src/internal/acorn-default.mjs +37 -0
- package/src/internal/declared-collections.ts +98 -0
- package/src/internal/page-collection-schema.ts +210 -0
- package/src/lib/content-config-guard.ts +48 -0
- package/src/lib/hydration-scanner.ts +176 -0
- package/src/lib/hydration-script.ts +126 -0
- package/src/lib/hydration-utils.ts +120 -0
- package/src/lib/resolve-locale.ts +22 -0
- package/src/lib/resolve-site-name.ts +10 -0
- package/src/lib/route-utils.ts +106 -0
- package/src/lib/routing-config.ts +126 -0
- package/src/lib/serialize-props.ts +7 -0
- package/src/lib/unknown-components.ts +64 -0
- package/src/lib/virtual-module-generators.ts +58 -0
- package/src/lib/withHydration.ts +71 -0
- package/src/loader.ts +145 -0
- package/src/locales-helpers.ts +68 -0
- package/src/mime-types.ts +19 -0
- package/src/page-feed.ts +26 -0
- package/src/page-schemas.ts +19 -0
- package/src/path-validation.ts +13 -0
- package/src/plugin-spa.ts +1480 -0
- package/src/puck-manifest/components-endpoint.ts +55 -0
- package/src/puck-manifest/field-projection.ts +510 -0
- package/src/puck-manifest/index.ts +3 -0
- package/src/puck-manifest/literal.ts +459 -0
- package/src/puck-manifest/read-config.ts +577 -0
- package/src/puck-manifest/read-field.ts +19 -0
- package/src/puck-manifest/read-wrapper.ts +1056 -0
- package/src/puck-manifest/reading-budget.ts +127 -0
- package/src/puck-manifest/resolve-reference.ts +9 -0
- package/src/puck-manifest/source-reference.ts +484 -0
- package/src/puck-manifest/syntax.ts +104 -0
- package/src/puck-manifest/types.ts +139 -0
- package/src/safe-json-stringify.ts +17 -0
- package/src/sandbox-frame-handler.ts +31 -0
- package/src/site-styles/content-wrapper-endpoint.ts +327 -0
- package/src/site-styles/render-endpoint.ts +331 -0
- package/src/site-styles/styles-endpoint.ts +184 -0
- package/src/sitemap-options.ts +115 -0
- package/src/tiptap-html-static.ts +84 -0
- package/{dist/types-CVQNmPxF.d.mts → src/types.ts} +232 -79
- package/src/virtual-modules.d.ts +74 -0
- package/dist/collections.d.mts +0 -36
- package/dist/collections.d.mts.map +0 -1
- package/dist/collections.mjs +0 -65
- package/dist/collections.mjs.map +0 -1
- package/dist/components.d.mts +0 -46
- package/dist/components.d.mts.map +0 -1
- package/dist/components.mjs +0 -226
- package/dist/components.mjs.map +0 -1
- package/dist/hydration-scanner-B0NjYkZ_.mjs +0 -109
- package/dist/hydration-scanner-B0NjYkZ_.mjs.map +0 -1
- package/dist/hydration.d.mts +0 -4
- package/dist/hydration.mjs +0 -3
- package/dist/index.d.mts +0 -139
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -66
- package/dist/index.mjs.map +0 -1
- package/dist/internal/cms-handler.d.mts +0 -26
- package/dist/internal/cms-handler.d.mts.map +0 -1
- package/dist/internal/cms-handler.mjs +0 -267
- package/dist/internal/cms-handler.mjs.map +0 -1
- package/dist/lib/hydration-script.d.mts.map +0 -1
- package/dist/lib/hydration-script.mjs +0 -85
- package/dist/lib/hydration-script.mjs.map +0 -1
- package/dist/loader-By-AQWHc.mjs +0 -68
- package/dist/loader-By-AQWHc.mjs.map +0 -1
- package/dist/node/content.d.mts +0 -19
- package/dist/node/content.d.mts.map +0 -1
- package/dist/node/content.mjs +0 -4
- package/dist/node/index.d.mts +0 -52
- package/dist/node/index.d.mts.map +0 -1
- package/dist/node/index.mjs +0 -753
- package/dist/node/index.mjs.map +0 -1
- package/dist/serialize-props-Dhv_MnkY.mjs +0 -73
- package/dist/serialize-props-Dhv_MnkY.mjs.map +0 -1
- package/dist/types-CVQNmPxF.d.mts.map +0 -1
- package/dist/withHydration-EzyzBQ-Y.mjs +0 -49
- package/dist/withHydration-EzyzBQ-Y.mjs.map +0 -1
- package/dist/withHydration-HIvO9ysY.d.mts.map +0 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How much reading one config may cost, counted in object properties visited
|
|
5
|
+
* or copied, array items read and parts of computed names folded. The example
|
|
6
|
+
* site that ships beside this package costs about 1,400, and a real site config
|
|
7
|
+
* of 36 components about 3,800; a hundred components of fifty five-option
|
|
8
|
+
* selects each, 150,000; ten times that, 1,504,000, read in about three
|
|
9
|
+
* seconds. Far short of the billions a config that spreads one object into
|
|
10
|
+
* another, over and over, can ask for while still loading in an instant — at
|
|
11
|
+
* roughly a microsecond a unit, one is refused within a few seconds.
|
|
12
|
+
*/
|
|
13
|
+
export const MAX_WORK = 2_000_000;
|
|
14
|
+
|
|
15
|
+
/** Reading stopped because it cost more than `MAX_WORK`. Never a guess: the caller refuses the whole reading. */
|
|
16
|
+
export class WorkBudgetExceeded extends Error {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(
|
|
19
|
+
`reading the config visited more than ${MAX_WORK.toLocaleString('en-US')} object properties, array items ` +
|
|
20
|
+
'and parts of computed names, and stopped — usually objects spread into one another, or one large value ' +
|
|
21
|
+
'named in many places; spreading or naming each shared value fewer times keeps it readable',
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* How many values one manifest may hold, counted as its JSON carries them: an
|
|
28
|
+
* object named in ten places counts ten times. `MAX_WORK` bounds what is read,
|
|
29
|
+
* but a reading shares what it reads, so `{ a: X, b: X, … }` stacked seven
|
|
30
|
+
* levels deep is under a hundred properties to read and ten million values to
|
|
31
|
+
* send. The example site emits 851 and the 36-component config 2,668; a
|
|
32
|
+
* hundred components of fifty five-option selects each, 100,403; ten times
|
|
33
|
+
* that, 1,004,003.
|
|
34
|
+
*/
|
|
35
|
+
export const MAX_OUTPUT = 2_000_000;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* How many bytes of JSON, as UTF-8, one manifest may run to. Values alone do
|
|
39
|
+
* not bound it: a string two thousand characters long, named ten times over
|
|
40
|
+
* five levels, is a hundred thousand values and two hundred million bytes. The
|
|
41
|
+
* example site emits 10,000-odd and the 36-component config 43,000-odd; the
|
|
42
|
+
* ten-times config above, 11,071,812. A byte is never less than a character,
|
|
43
|
+
* so this also keeps the JSON string to a sixteenth of the longest a
|
|
44
|
+
* JavaScript runtime builds (V8's, about 536 million characters).
|
|
45
|
+
*/
|
|
46
|
+
export const MAX_OUTPUT_BYTES = 32_000_000;
|
|
47
|
+
|
|
48
|
+
/** The manifest would be bigger than the reader sends. Never trimmed: the caller refuses the whole reading. */
|
|
49
|
+
export class OutputBudgetExceeded extends Error {}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Refuses a reading whose JSON would hold more than `MAX_OUTPUT` values, or
|
|
53
|
+
* run to more than `MAX_OUTPUT_BYTES`. Counted without building that JSON:
|
|
54
|
+
* an object met again adds the size already counted for it, so the count costs
|
|
55
|
+
* the objects the reading made, not the copies it would serialize to.
|
|
56
|
+
*/
|
|
57
|
+
export function limitOutput(value: unknown): void {
|
|
58
|
+
const size = outputSize(value, new WeakMap());
|
|
59
|
+
if (size.values > MAX_OUTPUT) {
|
|
60
|
+
throw new OutputBudgetExceeded(
|
|
61
|
+
`the manifest would hold more than ${MAX_OUTPUT.toLocaleString('en-US')} values, so it was not produced — ` +
|
|
62
|
+
'usually an object that names another several times, stacked level on level, so each level multiplies ' +
|
|
63
|
+
'the whole; naming each shared object in fewer places keeps it readable',
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (size.bytes > MAX_OUTPUT_BYTES) {
|
|
67
|
+
throw new OutputBudgetExceeded(
|
|
68
|
+
`the manifest would run to more than ${MAX_OUTPUT_BYTES.toLocaleString('en-US')} bytes of JSON, ` +
|
|
69
|
+
'so it was not produced — usually a long string or key named in many places, or named by an object that ' +
|
|
70
|
+
'is itself named many times; naming it in fewer places keeps it readable',
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** What a value serializes to: how many JSON values, and how many bytes of `JSON.stringify` output as UTF-8. */
|
|
76
|
+
interface OutputSize {
|
|
77
|
+
values: number;
|
|
78
|
+
bytes: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A value's JSON size, by `JSON.stringify`'s own rules — an entry holding
|
|
83
|
+
* `undefined` or a function is left out of an object and written `null` in an
|
|
84
|
+
* array — each object's size remembered in `sizes`.
|
|
85
|
+
*/
|
|
86
|
+
function outputSize(value: unknown, sizes: WeakMap<object, OutputSize>): OutputSize {
|
|
87
|
+
if (typeof value !== 'object' || value === null) return { values: 1, bytes: jsonBytes(value) };
|
|
88
|
+
const known = sizes.get(value);
|
|
89
|
+
if (known) return known;
|
|
90
|
+
const array = Array.isArray(value);
|
|
91
|
+
// The opening bracket here, and after each entry a comma or the closing one.
|
|
92
|
+
const size: OutputSize = { values: 1, bytes: 1 };
|
|
93
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
94
|
+
const skipped = entry === undefined || typeof entry === 'function' || typeof entry === 'symbol';
|
|
95
|
+
if (skipped && !array) continue;
|
|
96
|
+
const inner = skipped ? { values: 1, bytes: 4 } : outputSize(entry, sizes);
|
|
97
|
+
size.values += inner.values;
|
|
98
|
+
// An object entry is `"key":value`.
|
|
99
|
+
size.bytes += inner.bytes + 1 + (array ? 0 : jsonBytes(key) + 1);
|
|
100
|
+
}
|
|
101
|
+
if (size.bytes === 1) size.bytes = 2;
|
|
102
|
+
sizes.set(value, size);
|
|
103
|
+
return size;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The UTF-8 bytes of a primitive's JSON, `null` standing in for what JSON cannot write. */
|
|
107
|
+
function jsonBytes(value: unknown): number {
|
|
108
|
+
return Buffer.byteLength(JSON.stringify(value) ?? 'null');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let work: number | undefined;
|
|
112
|
+
|
|
113
|
+
/** Shares a work budget across nested reads, and releases it even when a read throws. */
|
|
114
|
+
export function withWorkBudget<T>(read: () => T): T {
|
|
115
|
+
if (work !== undefined) return read();
|
|
116
|
+
work = 0;
|
|
117
|
+
try {
|
|
118
|
+
return read();
|
|
119
|
+
} finally {
|
|
120
|
+
work = undefined;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Counts one unit of work against the reading's budget, stopping the reading once it is spent. */
|
|
125
|
+
export function spendWork(): void {
|
|
126
|
+
if (work !== undefined && ++work > MAX_WORK) throw new WorkBudgetExceeded();
|
|
127
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Reference-reading entry points; source resolution never depends on JSON materialization. */
|
|
2
|
+
export { resolveLiteralValue, resolveLiteralValueReading } from './literal.js';
|
|
3
|
+
export {
|
|
4
|
+
type KeyLookup,
|
|
5
|
+
lookUpKey,
|
|
6
|
+
resolveCallExpression,
|
|
7
|
+
resolveObjectLiteral,
|
|
8
|
+
type SpreadSource,
|
|
9
|
+
} from './source-reference.js';
|
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CallExpression,
|
|
3
|
+
type ElementAccessExpression,
|
|
4
|
+
Node,
|
|
5
|
+
type ObjectLiteralExpression,
|
|
6
|
+
type PropertyAccessExpression,
|
|
7
|
+
SyntaxKind,
|
|
8
|
+
type Symbol as TsSymbol,
|
|
9
|
+
type ts,
|
|
10
|
+
VariableDeclarationKind,
|
|
11
|
+
} from 'ts-morph';
|
|
12
|
+
import { MAX_OUTPUT_BYTES, OutputBudgetExceeded, spendWork } from './reading-budget.js';
|
|
13
|
+
import {
|
|
14
|
+
asObjectLiteral,
|
|
15
|
+
literalPrimitive,
|
|
16
|
+
type NamedMember,
|
|
17
|
+
namedMember,
|
|
18
|
+
unwrapExpression,
|
|
19
|
+
writtenValue,
|
|
20
|
+
} from './syntax.js';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A backstop on the walk below, shared by all three things a name is followed
|
|
24
|
+
* to here: a component's object literal, a value, and a wrapper call.
|
|
25
|
+
*
|
|
26
|
+
* Barrels are nearly free: the compiler collapses a whole re-export chain into
|
|
27
|
+
* a single alias hop, so even a nine-deep stack of index files arrives in
|
|
28
|
+
* three. Renames are not — `const Hero = HeroBase` costs a hop of its own, and
|
|
29
|
+
* an imported one costs two — which leaves room for roughly a single
|
|
30
|
+
* cross-file rename beyond the barrel before the walk gives up and the caller
|
|
31
|
+
* reports the name unresolved. Reading a key out of an object — `LABELS.hero`,
|
|
32
|
+
* or a spread on the way to it — costs a hop like a rename does.
|
|
33
|
+
*
|
|
34
|
+
* There is a limit at all because the graph being walked comes from someone
|
|
35
|
+
* else's source, and a reader of arbitrary configs should not depend on the
|
|
36
|
+
* compiler to stop it.
|
|
37
|
+
*/
|
|
38
|
+
const MAX_HOPS = 5;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* What a caller is hunting for at the end of a name — the object literal a
|
|
42
|
+
* component is written as, or the value expression a name stands for.
|
|
43
|
+
*
|
|
44
|
+
* Handing this to the walk is what lets every reader here share one hop loop
|
|
45
|
+
* instead of each keeping its own copy of the same alias-following rules.
|
|
46
|
+
*
|
|
47
|
+
* The expression handed in may still be wearing `satisfies`, `as const` or
|
|
48
|
+
* parentheses, so an implementation has to unwrap before it looks at the kind.
|
|
49
|
+
* Both of the ones here do; one that forgets reads a wrapped value as absent
|
|
50
|
+
* and says nothing about why.
|
|
51
|
+
*/
|
|
52
|
+
type Reading<T extends Node> = (expression: Node) => T | undefined;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Follows an identifier to the object literal it names, across files and
|
|
56
|
+
* through barrel re-exports.
|
|
57
|
+
*
|
|
58
|
+
* Every real Puck config writes `components: { Hero, Badge }`, so the
|
|
59
|
+
* definitions live in per-component modules behind an index that re-exports
|
|
60
|
+
* them. Resolving that chain is the difference between reading a config and
|
|
61
|
+
* reading a list of names.
|
|
62
|
+
*
|
|
63
|
+
* A member access — `NESTED.fields` — is read by finding the object the left
|
|
64
|
+
* side stands for and looking the key up in it, never by asking the compiler
|
|
65
|
+
* what the key's declaration is: that answer comes from the TYPE, and a name
|
|
66
|
+
* typed `typeof Base` would be handed Base's value.
|
|
67
|
+
*
|
|
68
|
+
* Returns undefined when the chain cannot be followed — an npm package with no
|
|
69
|
+
* types, a parameter, or a component a wrapper builds (`withLayout(Inner)`,
|
|
70
|
+
* which both of our own sites use) — so the caller can record the component as
|
|
71
|
+
* unresolved instead of reporting it as having no fields.
|
|
72
|
+
*
|
|
73
|
+
* Known limit: when two barrels `export *` the same name, the compiler picks
|
|
74
|
+
* one and this walk relays that pick without noticing the collision.
|
|
75
|
+
*/
|
|
76
|
+
export function resolveObjectLiteral(node: Node): ObjectLiteralExpression | undefined {
|
|
77
|
+
// A caller holding `Flex: { ...Flex }` hands over the spread source, which is
|
|
78
|
+
// sometimes the literal itself rather than a name for one.
|
|
79
|
+
return fromExpression(node, MAX_HOPS, asObjectLiteral);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A call expression an identifier names — `withLayout(GridInternal)` behind
|
|
84
|
+
* `export const Grid = withLayout(GridInternal)`. The wrapper reader's entry.
|
|
85
|
+
*
|
|
86
|
+
* Same limits as the walk above: a name exported by two barrels resolves to
|
|
87
|
+
* the compiler's pick, and only a `const` is followed.
|
|
88
|
+
*/
|
|
89
|
+
export function resolveCallExpression(node: Node): CallExpression | undefined {
|
|
90
|
+
const unwrapped = unwrapExpression(node);
|
|
91
|
+
return (
|
|
92
|
+
unwrapped.asKind(SyntaxKind.CallExpression) ?? resolveValueExpression(unwrapped)?.asKind(SyntaxKind.CallExpression)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The expression a name was declared as, once every rename and member access
|
|
98
|
+
* between here and the declaration has been followed. Undefined when the name
|
|
99
|
+
* is not a `const`, or when the walk runs out of hops.
|
|
100
|
+
*/
|
|
101
|
+
export function resolveValueExpression(node: Node): Node | undefined {
|
|
102
|
+
return fromExpression(node, MAX_HOPS, terminalExpression);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The end of a chain of names: any expression that is not itself another name
|
|
107
|
+
* or a key read out of one. Returning undefined for those is what tells the
|
|
108
|
+
* walk to keep going.
|
|
109
|
+
*/
|
|
110
|
+
function terminalExpression(expression: Node): Node | undefined {
|
|
111
|
+
const unwrapped = unwrapExpression(expression);
|
|
112
|
+
return Node.isIdentifier(unwrapped) || isMemberAccess(unwrapped) ? undefined : unwrapped;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** `x.y` or `x['y']` — a key read out of whatever `x` stands for. */
|
|
116
|
+
function isMemberAccess(node: Node): node is PropertyAccessExpression | ElementAccessExpression {
|
|
117
|
+
return Node.isPropertyAccessExpression(node) || Node.isElementAccessExpression(node);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** What an expression amounts to, whether written in place, named, or read out of a named object. */
|
|
121
|
+
function fromExpression<T extends Node>(expression: Node, hops: number, reading: Reading<T>): T | undefined {
|
|
122
|
+
// `satisfies`, `as const` and parentheses keep the value one level down.
|
|
123
|
+
const read = reading(expression);
|
|
124
|
+
if (read) return read;
|
|
125
|
+
|
|
126
|
+
const unwrapped = unwrapExpression(expression);
|
|
127
|
+
if (Node.isIdentifier(unwrapped)) return fromSymbol(unwrapped.getSymbol(), hops, reading);
|
|
128
|
+
if (isMemberAccess(unwrapped)) return fromMember(unwrapped, hops, reading);
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Each declaration of a name is a candidate; when none of them reads, the name
|
|
134
|
+
* is an alias for another one — an import, a re-export — and the hunt
|
|
135
|
+
* continues at what it points at.
|
|
136
|
+
*
|
|
137
|
+
* The last candidate wins, because that is the declaration whose value the name
|
|
138
|
+
* carries by the time anything reads it.
|
|
139
|
+
*/
|
|
140
|
+
function fromSymbol<T extends Node>(symbol: TsSymbol | undefined, hops: number, reading: Reading<T>): T | undefined {
|
|
141
|
+
if (!symbol || hops <= 0) return undefined;
|
|
142
|
+
|
|
143
|
+
let found: T | undefined;
|
|
144
|
+
for (const declaration of symbol.getDeclarations()) {
|
|
145
|
+
// `components: { Hero }` writes a property and a value at once, and the
|
|
146
|
+
// checker reports the property — whose only declaration is this shorthand.
|
|
147
|
+
// The value it stands for is a separate symbol, and the only way onward: a
|
|
148
|
+
// property symbol is an alias for nothing, so there is nothing to fall back
|
|
149
|
+
// to when its value cannot be followed. Only the shorthand's own name
|
|
150
|
+
// reaches here: a member access never asks the checker for its symbol.
|
|
151
|
+
if (Node.isShorthandPropertyAssignment(declaration)) {
|
|
152
|
+
return fromSymbol(declaration.getValueSymbol(), hops - 1, reading);
|
|
153
|
+
}
|
|
154
|
+
const read = fromDeclaration(declaration, hops, reading);
|
|
155
|
+
if (read) found = read;
|
|
156
|
+
}
|
|
157
|
+
if (found) return found;
|
|
158
|
+
|
|
159
|
+
return fromSymbol(symbol.getAliasedSymbol(), hops - 1, reading);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* What a single declaration of a name settles on.
|
|
164
|
+
*
|
|
165
|
+
* Only `const` is trusted: a `let` or `var` holds whatever was assigned to it
|
|
166
|
+
* last, which is code this reader never sees, so its initializer describes the
|
|
167
|
+
* name at one moment rather than at the moment Puck reads it.
|
|
168
|
+
*/
|
|
169
|
+
function fromDeclaration<T extends Node>(declaration: Node, hops: number, reading: Reading<T>): T | undefined {
|
|
170
|
+
if (Node.isVariableDeclaration(declaration)) {
|
|
171
|
+
if (declaration.getVariableStatement()?.getDeclarationKind() !== VariableDeclarationKind.Const) return undefined;
|
|
172
|
+
const initializer = declaration.getInitializer();
|
|
173
|
+
if (!initializer) return undefined;
|
|
174
|
+
return fromExpression(initializer, hops - 1, reading);
|
|
175
|
+
}
|
|
176
|
+
// `export default { … }`, paired with `import Hero from './Hero'`: the import
|
|
177
|
+
// aliases the module's default, and this is what that default is written as.
|
|
178
|
+
if (Node.isExportAssignment(declaration)) {
|
|
179
|
+
return fromExpression(declaration.getExpression(), hops - 1, reading);
|
|
180
|
+
}
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* What `x.y` amounts to: the key `y` looked up in the object `x` stands for.
|
|
186
|
+
*
|
|
187
|
+
* A namespace import is the one left side the compiler's own answer is right
|
|
188
|
+
* for — `sections.Hero` names an export, and an export is a declaration, not a
|
|
189
|
+
* type's memory of one.
|
|
190
|
+
*/
|
|
191
|
+
function fromMember<T extends Node>(
|
|
192
|
+
access: PropertyAccessExpression | ElementAccessExpression,
|
|
193
|
+
hops: number,
|
|
194
|
+
reading: Reading<T>,
|
|
195
|
+
): T | undefined {
|
|
196
|
+
if (hops <= 0) return undefined;
|
|
197
|
+
if (Node.isPropertyAccessExpression(access) && namesModule(access.getExpression())) {
|
|
198
|
+
return fromSymbol(access.getNameNode().getSymbol(), hops, reading);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const key = Node.isPropertyAccessExpression(access)
|
|
202
|
+
? access.getName()
|
|
203
|
+
: expressionKey(access.getArgumentExpressionOrThrow());
|
|
204
|
+
if (key === undefined) return undefined;
|
|
205
|
+
|
|
206
|
+
const object = fromExpression(access.getExpression(), hops, asObjectLiteral);
|
|
207
|
+
const value = object && keyValue(object, key, hops - 1);
|
|
208
|
+
return value ? fromExpression(value, hops - 1, reading) : undefined;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Whether an expression is the name of a module — `import * as sections`, or a `namespace` block. */
|
|
212
|
+
function namesModule(expression: Node): boolean {
|
|
213
|
+
const identifier = unwrapExpression(expression).asKind(SyntaxKind.Identifier);
|
|
214
|
+
const symbol = identifier?.getSymbol();
|
|
215
|
+
const target = symbol?.isAlias() ? symbol.getAliasedSymbol() : symbol;
|
|
216
|
+
return (
|
|
217
|
+
target
|
|
218
|
+
?.getDeclarations()
|
|
219
|
+
.some((declaration) => Node.isSourceFile(declaration) || Node.isModuleDeclaration(declaration)) ?? false
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** What a literal says about one key: the node it is assigned, proven absent, or not knowable from here. */
|
|
224
|
+
export type KeyLookup = Node | 'absent' | 'unknown';
|
|
225
|
+
|
|
226
|
+
/** The literal a spread inside an object reaches, or undefined where it cannot be followed. */
|
|
227
|
+
export type SpreadSource = (expression: Node) => ObjectLiteralExpression | undefined;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The node an object literal assigns to one key, seen through the spreads it
|
|
231
|
+
* applies — undefined when the literal does not provably assign one.
|
|
232
|
+
*/
|
|
233
|
+
function keyValue(literal: ObjectLiteralExpression, key: string, hops: number): Node | undefined {
|
|
234
|
+
const found = walkKey(literal, key, hops, (expression) => fromExpression(expression, hops - 1, asObjectLiteral));
|
|
235
|
+
return found === 'absent' || found === 'unknown' ? undefined : found;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* What an object literal assigns to one key, by the runtime's own rule, walked
|
|
240
|
+
* in source order: the last write of the key wins. A spread that cannot be
|
|
241
|
+
* followed, a computed name that cannot be resolved, and a getter or method
|
|
242
|
+
* are all writes this walk cannot read — any of them may be the key, so the
|
|
243
|
+
* answer becomes unknown until a later plain write settles it. `__proto__`
|
|
244
|
+
* hands the object a prototype, so a key it never writes itself may still be
|
|
245
|
+
* inherited.
|
|
246
|
+
*
|
|
247
|
+
* Spreads lead wherever `spreadSource` takes them — by default to the names
|
|
248
|
+
* they stand for; a caller with a function's parameters in scope hands in its
|
|
249
|
+
* own, so `{ ...config }` reaches the argument the call passed.
|
|
250
|
+
*/
|
|
251
|
+
export function lookUpKey(
|
|
252
|
+
literal: ObjectLiteralExpression,
|
|
253
|
+
key: string,
|
|
254
|
+
spreadSource: SpreadSource = (expression) => fromExpression(expression, MAX_HOPS, asObjectLiteral),
|
|
255
|
+
): KeyLookup {
|
|
256
|
+
return walkKey(literal, key, MAX_HOPS, spreadSource);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** `lookUpKey`, spending one hop per spread followed so a spread cycle ends as unknown. */
|
|
260
|
+
function walkKey(literal: ObjectLiteralExpression, key: string, hops: number, spreadSource: SpreadSource): KeyLookup {
|
|
261
|
+
let found: KeyLookup = 'absent';
|
|
262
|
+
let inherits = false;
|
|
263
|
+
|
|
264
|
+
for (const property of literal.getProperties()) {
|
|
265
|
+
spendWork();
|
|
266
|
+
const spread = property.asKind(SyntaxKind.SpreadAssignment);
|
|
267
|
+
if (spread) {
|
|
268
|
+
const source = hops > 0 ? spreadSource(spread.getExpression()) : undefined;
|
|
269
|
+
const inherited = source ? walkKey(source, key, hops - 1, spreadSource) : 'unknown';
|
|
270
|
+
if (inherited !== 'absent') found = inherited;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const member = namedMember(property);
|
|
275
|
+
if (!member) {
|
|
276
|
+
found = 'unknown';
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
const name = propertyKey(member);
|
|
280
|
+
if (name === undefined) {
|
|
281
|
+
found = 'unknown';
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (setsPrototype(member)) {
|
|
285
|
+
inherits = true;
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (name !== key) continue;
|
|
289
|
+
|
|
290
|
+
if (Node.isPropertyAssignment(member)) found = member.getInitializerOrThrow();
|
|
291
|
+
else if (Node.isShorthandPropertyAssignment(member)) found = member.getNameNode();
|
|
292
|
+
else found = 'unknown';
|
|
293
|
+
}
|
|
294
|
+
return found === 'absent' && inherits ? 'unknown' : found;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Computed names currently being worked out. A name that leads back to the
|
|
299
|
+
* object it sits in — `const x = { [x.k]: 1 }` — asks for itself again before
|
|
300
|
+
* it has an answer, and is no key rather than a loop.
|
|
301
|
+
*/
|
|
302
|
+
const resolvingKeys = new Set<ts.Node>();
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* The written-down name of a member, or `undefined` when the source does not
|
|
306
|
+
* spell one out. A computed key is that key only when its expression stands
|
|
307
|
+
* for a `const` string or number — `[K]` beside `const K = 'max'`, or a sum of
|
|
308
|
+
* such parts like `['resolve' + 'Data']`. Any other `[k]` is skipped rather than
|
|
309
|
+
* read from its text: `"[k]"` would be an invented field name, the worst guess
|
|
310
|
+
* this module can make, because it looks exactly like a real one.
|
|
311
|
+
*
|
|
312
|
+
* Every shape a name can be written in is accepted — `resolveData: fn`,
|
|
313
|
+
* `{ resolveData }`, `resolveData() { … }`, `get fields() { … }` — because a
|
|
314
|
+
* caller that reads only assignments loses the rest, and a key it never sees is
|
|
315
|
+
* a key it cannot report as unread.
|
|
316
|
+
*/
|
|
317
|
+
export function propertyKey(member: NamedMember): string | undefined {
|
|
318
|
+
const name = member.getNameNode();
|
|
319
|
+
|
|
320
|
+
// getLiteralValue, not the text: it resolves escapes, so `'it\'s'` is a key
|
|
321
|
+
// named `it's` rather than one carrying a stray backslash.
|
|
322
|
+
const quoted = name.asKind(SyntaxKind.StringLiteral);
|
|
323
|
+
if (quoted) return quoted.getLiteralValue();
|
|
324
|
+
|
|
325
|
+
// The runtime spells a number written as a key the way `String` does: `{
|
|
326
|
+
// 1e21: … }` holds the key `1e+21`, `{ 0x10: … }` the key `16`.
|
|
327
|
+
const numeric = name.asKind(SyntaxKind.NumericLiteral);
|
|
328
|
+
if (numeric) return String(Number(numeric.compilerNode.text));
|
|
329
|
+
|
|
330
|
+
const identifier = name.asKind(SyntaxKind.Identifier);
|
|
331
|
+
if (identifier) return identifier.getText();
|
|
332
|
+
|
|
333
|
+
const computed = name.asKind(SyntaxKind.ComputedPropertyName);
|
|
334
|
+
if (computed) return expressionKey(computed.getExpression());
|
|
335
|
+
|
|
336
|
+
return undefined;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* The property key an expression spells — the `K` of `[K]` or of `obj[K]` —
|
|
341
|
+
* when it stands for a `const` string or number. Undefined for anything the
|
|
342
|
+
* reader would have to run to know.
|
|
343
|
+
*/
|
|
344
|
+
export function expressionKey(expression: Node): string | undefined {
|
|
345
|
+
if (resolvingKeys.has(expression.compilerNode)) return undefined;
|
|
346
|
+
resolvingKeys.add(expression.compilerNode);
|
|
347
|
+
try {
|
|
348
|
+
const key = constantKey(expression);
|
|
349
|
+
return key === undefined ? undefined : String(key);
|
|
350
|
+
} finally {
|
|
351
|
+
resolvingKeys.delete(expression.compilerNode);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* A `const` string or number, or a `+` of them folded the way the runtime folds
|
|
357
|
+
* it — written in place, or behind a `const` whose initializer is itself one:
|
|
358
|
+
* `const M = 'He' + 'ro'` makes `[M]` the key `Hero`, as `['He' + 'ro']` is.
|
|
359
|
+
*
|
|
360
|
+
* Folded with a stack of its own rather than by recursion: a sum twenty
|
|
361
|
+
* thousand parts long is one line of source, and recursing into it exhausts
|
|
362
|
+
* the call stack long before the parts run out. Each part, and each `+`, is
|
|
363
|
+
* work the reading pays for, so a key built by naming one sum from another,
|
|
364
|
+
* over and over, costs what folding it costs. A name folded past the length a
|
|
365
|
+
* whole manifest may run to is refused there, before it grows any longer.
|
|
366
|
+
*/
|
|
367
|
+
function constantKey(expression: Node, hops = MAX_CONST_HOPS): string | number | undefined {
|
|
368
|
+
// A `+` is met twice: first to queue its two sides, then to fold what they came to.
|
|
369
|
+
const pending: { node: Node; folding: boolean }[] = [{ node: expression, folding: false }];
|
|
370
|
+
const folded: (string | number)[] = [];
|
|
371
|
+
for (let next = pending.pop(); next; next = pending.pop()) {
|
|
372
|
+
spendWork();
|
|
373
|
+
const unwrapped = unwrapExpression(next.node);
|
|
374
|
+
const sum = unwrapped.asKind(SyntaxKind.BinaryExpression);
|
|
375
|
+
if (!sum) {
|
|
376
|
+
const part = constantPart(unwrapped, hops);
|
|
377
|
+
if (part === undefined) return undefined;
|
|
378
|
+
folded.push(part);
|
|
379
|
+
} else if (sum.getOperatorToken().getKind() !== SyntaxKind.PlusToken) {
|
|
380
|
+
return undefined;
|
|
381
|
+
} else if (next.folding) {
|
|
382
|
+
const right = folded.pop() as string | number;
|
|
383
|
+
const left = folded.pop() as string | number;
|
|
384
|
+
const joined = typeof left === 'number' && typeof right === 'number' ? left + right : `${left}${right}`;
|
|
385
|
+
// A character is never less than a byte of UTF-8, so a name this long is
|
|
386
|
+
// more than the manifest may carry however it is spelled.
|
|
387
|
+
if (typeof joined === 'string' && joined.length > MAX_OUTPUT_BYTES) {
|
|
388
|
+
throw new OutputBudgetExceeded(
|
|
389
|
+
`a computed name would run to more than ${MAX_OUTPUT_BYTES.toLocaleString('en-US')} characters, more ` +
|
|
390
|
+
'than a whole manifest may, so it was not produced — usually a string summed with itself through names ' +
|
|
391
|
+
'that each sum the one before; summing it fewer times keeps it readable',
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
folded.push(joined);
|
|
395
|
+
} else {
|
|
396
|
+
pending.push(
|
|
397
|
+
{ node: sum, folding: true },
|
|
398
|
+
{ node: sum.getRight(), folding: false },
|
|
399
|
+
{ node: sum.getLeft(), folding: false },
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return folded[0];
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** One part of a sum: a value written in place or named, or a `const` whose initializer is a key of its own. */
|
|
407
|
+
function constantPart(expression: Node, hops: number): string | number | undefined {
|
|
408
|
+
// A key only needs a primitive. Resolving its source must not invoke the JSON
|
|
409
|
+
// reader, which itself depends on these key and reference rules.
|
|
410
|
+
const target = resolveValueExpression(expression);
|
|
411
|
+
const value = target && literalPrimitive(target);
|
|
412
|
+
if (typeof value === 'string' || typeof value === 'number') return value;
|
|
413
|
+
const initializer = hops > 0 ? primitiveInitializer(expression) : undefined;
|
|
414
|
+
return initializer ? constantKey(initializer, hops - 1) : undefined;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* How many names a primitive is followed through here — a string key, or an
|
|
419
|
+
* `undefined` — before the reader gives up and treats it as unknown. Neither
|
|
420
|
+
* can be written to after its `const` is declared, which is why these two
|
|
421
|
+
* follow names on their own terms rather than through `resolve-reference`.
|
|
422
|
+
*/
|
|
423
|
+
const MAX_CONST_HOPS = 5;
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* The initializer of the `const` a name stands for, across an import or a
|
|
427
|
+
* re-export. Undefined for anything else — a `let`, a parameter, a global —
|
|
428
|
+
* because only a `const` holds what its declaration says.
|
|
429
|
+
*/
|
|
430
|
+
function primitiveInitializer(expression: Node): Node | undefined {
|
|
431
|
+
const identifier = expression.asKind(SyntaxKind.Identifier);
|
|
432
|
+
if (!identifier) return undefined;
|
|
433
|
+
// A shorthand's name is a property to the checker; the value it stands for
|
|
434
|
+
// is a symbol of its own.
|
|
435
|
+
const shorthand = identifier.getParentIfKind(SyntaxKind.ShorthandPropertyAssignment);
|
|
436
|
+
let symbol = shorthand?.getNameNode() === identifier ? shorthand.getValueSymbol() : identifier.getSymbol();
|
|
437
|
+
for (let hop = 0; symbol && hop < MAX_CONST_HOPS; hop++) {
|
|
438
|
+
const declarations = symbol.getDeclarations();
|
|
439
|
+
if (declarations.length === 0 || !symbol.isAlias()) {
|
|
440
|
+
const declaration =
|
|
441
|
+
declarations.length === 1 ? declarations[0]?.asKind(SyntaxKind.VariableDeclaration) : undefined;
|
|
442
|
+
if (declaration?.getVariableStatement()?.getDeclarationKind() !== VariableDeclarationKind.Const) return undefined;
|
|
443
|
+
return declaration.getInitializer();
|
|
444
|
+
}
|
|
445
|
+
symbol = symbol.getAliasedSymbol();
|
|
446
|
+
}
|
|
447
|
+
return undefined;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Whether an expression provably holds no value at all: `null`, `undefined`,
|
|
452
|
+
* `void 0`, or a `const` bound to one of them. Spread, such a value writes no
|
|
453
|
+
* key; written to a behaviour key, it leaves Puck nothing to run.
|
|
454
|
+
*
|
|
455
|
+
* `undefined` counts only as the global: a parameter or a local may be named
|
|
456
|
+
* `undefined` too, and holds whatever was bound to it.
|
|
457
|
+
*/
|
|
458
|
+
export function holdsNothing(node: Node, hops = MAX_CONST_HOPS): boolean {
|
|
459
|
+
const expression = unwrapExpression(node);
|
|
460
|
+
if (expression.isKind(SyntaxKind.NullKeyword) || expression.isKind(SyntaxKind.VoidExpression)) return true;
|
|
461
|
+
const identifier = expression.asKind(SyntaxKind.Identifier);
|
|
462
|
+
if (!identifier) return false;
|
|
463
|
+
if (identifier.getText() === 'undefined' && identifier.getSymbol()?.getDeclarations().length === 0) return true;
|
|
464
|
+
const initializer = hops > 0 ? primitiveInitializer(identifier) : undefined;
|
|
465
|
+
return initializer !== undefined && holdsNothing(initializer, hops - 1);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Whether a member hands its object a prototype rather than a key: `__proto__:
|
|
470
|
+
* base`, written as a plain name or a string. The object then inherits every
|
|
471
|
+
* key it does not write itself, which no reading of its text can see. Written
|
|
472
|
+
* computed — `['__proto__']: …` — or as a shorthand, it is an ordinary key.
|
|
473
|
+
*/
|
|
474
|
+
export function setsPrototype(member: NamedMember): boolean {
|
|
475
|
+
if (!member.isKind(SyntaxKind.PropertyAssignment)) return false;
|
|
476
|
+
const name = member.getNameNode();
|
|
477
|
+
return !name.isKind(SyntaxKind.ComputedPropertyName) && propertyKey(member) === '__proto__';
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** `__proto__: null` — a prototype set to nothing, which leaves the object nothing to inherit. */
|
|
481
|
+
export function inheritsNothing(member: NamedMember): boolean {
|
|
482
|
+
const value = writtenValue(member);
|
|
483
|
+
return value !== undefined && holdsNothing(value);
|
|
484
|
+
}
|