@decocms/blocks 8.1.0-next.1 → 8.1.0-next.3
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/bin/deco.js +5 -12
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol/assets.d.ts +46 -0
- package/dist/protocol/assets.d.ts.map +1 -0
- package/dist/protocol/assets.js +105 -0
- package/dist/protocol/assets.js.map +1 -0
- package/dist/protocol/canonical.d.ts +8 -0
- package/dist/protocol/canonical.d.ts.map +1 -0
- package/dist/protocol/canonical.js +8 -0
- package/dist/protocol/canonical.js.map +1 -0
- package/dist/protocol/ciphertext.d.ts +7 -0
- package/dist/protocol/ciphertext.d.ts.map +1 -0
- package/dist/protocol/ciphertext.js +7 -0
- package/dist/protocol/ciphertext.js.map +1 -0
- package/dist/protocol/client.d.ts +54 -0
- package/dist/protocol/client.d.ts.map +1 -0
- package/dist/protocol/client.js +104 -0
- package/dist/protocol/client.js.map +1 -0
- package/dist/protocol/conformance/cases/assets.d.ts +3 -0
- package/dist/protocol/conformance/cases/assets.d.ts.map +1 -0
- package/dist/protocol/conformance/cases/assets.js +71 -0
- package/dist/protocol/conformance/cases/assets.js.map +1 -0
- package/dist/protocol/conformance/cases/guards.d.ts +3 -0
- package/dist/protocol/conformance/cases/guards.d.ts.map +1 -0
- package/dist/protocol/conformance/cases/guards.js +265 -0
- package/dist/protocol/conformance/cases/guards.js.map +1 -0
- package/dist/protocol/conformance/cases/reads.d.ts +3 -0
- package/dist/protocol/conformance/cases/reads.d.ts.map +1 -0
- package/dist/protocol/conformance/cases/reads.js +140 -0
- package/dist/protocol/conformance/cases/reads.js.map +1 -0
- package/dist/protocol/conformance/cases/types.d.ts +9 -0
- package/dist/protocol/conformance/cases/types.d.ts.map +1 -0
- package/dist/protocol/conformance/cases/types.js +2 -0
- package/dist/protocol/conformance/cases/types.js.map +1 -0
- package/dist/protocol/conformance/cases/wire.d.ts +3 -0
- package/dist/protocol/conformance/cases/wire.d.ts.map +1 -0
- package/dist/protocol/conformance/cases/wire.js +130 -0
- package/dist/protocol/conformance/cases/wire.js.map +1 -0
- package/dist/protocol/conformance/cases/writes.d.ts +3 -0
- package/dist/protocol/conformance/cases/writes.d.ts.map +1 -0
- package/dist/protocol/conformance/cases/writes.js +278 -0
- package/dist/protocol/conformance/cases/writes.js.map +1 -0
- package/dist/protocol/conformance/contentHashFixtures.d.ts +51 -0
- package/dist/protocol/conformance/contentHashFixtures.d.ts.map +1 -0
- package/dist/protocol/conformance/contentHashFixtures.js +94 -0
- package/dist/protocol/conformance/contentHashFixtures.js.map +1 -0
- package/dist/protocol/conformance/context.d.ts +100 -0
- package/dist/protocol/conformance/context.d.ts.map +1 -0
- package/dist/protocol/conformance/context.js +155 -0
- package/dist/protocol/conformance/context.js.map +1 -0
- package/dist/protocol/conformance/index.d.ts +32 -0
- package/dist/protocol/conformance/index.d.ts.map +1 -0
- package/dist/protocol/conformance/index.js +90 -0
- package/dist/protocol/conformance/index.js.map +1 -0
- package/dist/protocol/errors.d.ts +85 -0
- package/dist/protocol/errors.d.ts.map +1 -0
- package/dist/protocol/errors.js +62 -0
- package/dist/protocol/errors.js.map +1 -0
- package/dist/protocol/index.d.ts +28 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +28 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/protocol/keys.d.ts +101 -0
- package/dist/protocol/keys.d.ts.map +1 -0
- package/dist/protocol/keys.js +223 -0
- package/dist/protocol/keys.js.map +1 -0
- package/dist/protocol/params.d.ts +10 -0
- package/dist/protocol/params.d.ts.map +1 -0
- package/dist/protocol/params.js +54 -0
- package/dist/protocol/params.js.map +1 -0
- package/dist/protocol/secrets.d.ts +22 -0
- package/dist/protocol/secrets.d.ts.map +1 -0
- package/dist/protocol/secrets.js +200 -0
- package/dist/protocol/secrets.js.map +1 -0
- package/dist/protocol/server/assets.d.ts +20 -0
- package/dist/protocol/server/assets.d.ts.map +1 -0
- package/dist/protocol/server/assets.js +77 -0
- package/dist/protocol/server/assets.js.map +1 -0
- package/dist/protocol/server/auth.d.ts +31 -0
- package/dist/protocol/server/auth.d.ts.map +1 -0
- package/dist/protocol/server/auth.js +32 -0
- package/dist/protocol/server/auth.js.map +1 -0
- package/dist/protocol/server/bodyCache.d.ts +28 -0
- package/dist/protocol/server/bodyCache.d.ts.map +1 -0
- package/dist/protocol/server/bodyCache.js +75 -0
- package/dist/protocol/server/bodyCache.js.map +1 -0
- package/dist/protocol/server/content.d.ts +44 -0
- package/dist/protocol/server/content.d.ts.map +1 -0
- package/dist/protocol/server/content.js +132 -0
- package/dist/protocol/server/content.js.map +1 -0
- package/dist/protocol/server/core.d.ts +63 -0
- package/dist/protocol/server/core.d.ts.map +1 -0
- package/dist/protocol/server/core.js +77 -0
- package/dist/protocol/server/core.js.map +1 -0
- package/dist/protocol/server/handler.d.ts +5 -0
- package/dist/protocol/server/handler.d.ts.map +1 -0
- package/dist/protocol/server/handler.js +70 -0
- package/dist/protocol/server/handler.js.map +1 -0
- package/dist/protocol/server/http.d.ts +24 -0
- package/dist/protocol/server/http.d.ts.map +1 -0
- package/dist/protocol/server/http.js +133 -0
- package/dist/protocol/server/http.js.map +1 -0
- package/dist/protocol/server/index.d.ts +11 -0
- package/dist/protocol/server/index.d.ts.map +1 -0
- package/dist/protocol/server/index.js +9 -0
- package/dist/protocol/server/index.js.map +1 -0
- package/dist/protocol/server/methods/apply.d.ts +4 -0
- package/dist/protocol/server/methods/apply.d.ts.map +1 -0
- package/dist/protocol/server/methods/apply.js +275 -0
- package/dist/protocol/server/methods/apply.js.map +1 -0
- package/dist/protocol/server/methods/read.d.ts +14 -0
- package/dist/protocol/server/methods/read.d.ts.map +1 -0
- package/dist/protocol/server/methods/read.js +132 -0
- package/dist/protocol/server/methods/read.js.map +1 -0
- package/dist/protocol/server/rpc.d.ts +7 -0
- package/dist/protocol/server/rpc.d.ts.map +1 -0
- package/dist/protocol/server/rpc.js +115 -0
- package/dist/protocol/server/rpc.js.map +1 -0
- package/dist/protocol/storage/fs/hash.d.ts +8 -0
- package/dist/protocol/storage/fs/hash.d.ts.map +1 -0
- package/dist/protocol/storage/fs/hash.js +20 -0
- package/dist/protocol/storage/fs/hash.js.map +1 -0
- package/dist/protocol/storage/fs/index.d.ts +30 -0
- package/dist/protocol/storage/fs/index.d.ts.map +1 -0
- package/dist/protocol/storage/fs/index.js +301 -0
- package/dist/protocol/storage/fs/index.js.map +1 -0
- package/dist/protocol/storage/fs/lock.d.ts +8 -0
- package/dist/protocol/storage/fs/lock.d.ts.map +1 -0
- package/dist/protocol/storage/fs/lock.js +61 -0
- package/dist/protocol/storage/fs/lock.js.map +1 -0
- package/dist/protocol/storage/fs/transaction.d.ts +19 -0
- package/dist/protocol/storage/fs/transaction.d.ts.map +1 -0
- package/dist/protocol/storage/fs/transaction.js +174 -0
- package/dist/protocol/storage/fs/transaction.js.map +1 -0
- package/dist/protocol/storage/fs/unsupported.d.ts +10 -0
- package/dist/protocol/storage/fs/unsupported.d.ts.map +1 -0
- package/dist/protocol/storage/fs/unsupported.js +4 -0
- package/dist/protocol/storage/fs/unsupported.js.map +1 -0
- package/dist/protocol/storage/memory.d.ts +40 -0
- package/dist/protocol/storage/memory.d.ts.map +1 -0
- package/dist/protocol/storage/memory.js +179 -0
- package/dist/protocol/storage/memory.js.map +1 -0
- package/dist/protocol/storage.d.ts +178 -0
- package/dist/protocol/storage.d.ts.map +1 -0
- package/dist/protocol/storage.js +29 -0
- package/dist/protocol/storage.js.map +1 -0
- package/dist/protocol/types.d.ts +194 -0
- package/dist/protocol/types.d.ts.map +1 -0
- package/dist/protocol/types.js +31 -0
- package/dist/protocol/types.js.map +1 -0
- package/dist/v8/analytics.d.ts +31 -0
- package/dist/v8/analytics.d.ts.map +1 -0
- package/dist/v8/analytics.js +46 -0
- package/dist/v8/analytics.js.map +1 -0
- package/dist/v8/background.d.ts +26 -0
- package/dist/v8/background.d.ts.map +1 -0
- package/dist/v8/background.js +95 -0
- package/dist/v8/background.js.map +1 -0
- package/dist/v8/builtins/data.d.ts +15 -0
- package/dist/v8/builtins/data.d.ts.map +1 -0
- package/dist/v8/builtins/data.js +18 -0
- package/dist/v8/builtins/data.js.map +1 -0
- package/dist/v8/builtins/index.d.ts +9 -0
- package/dist/v8/builtins/index.d.ts.map +1 -0
- package/dist/v8/builtins/index.js +24 -0
- package/dist/v8/builtins/index.js.map +1 -0
- package/dist/v8/builtins/lazy.d.ts +17 -0
- package/dist/v8/builtins/lazy.d.ts.map +1 -0
- package/dist/v8/builtins/lazy.js +13 -0
- package/dist/v8/builtins/lazy.js.map +1 -0
- package/dist/v8/builtins/legacy.d.ts +41 -0
- package/dist/v8/builtins/legacy.d.ts.map +1 -0
- package/dist/v8/builtins/legacy.js +62 -0
- package/dist/v8/builtins/legacy.js.map +1 -0
- package/dist/v8/builtins/matchers.d.ts +19 -0
- package/dist/v8/builtins/matchers.d.ts.map +1 -0
- package/dist/v8/builtins/matchers.js +24 -0
- package/dist/v8/builtins/matchers.js.map +1 -0
- package/dist/v8/builtins/multivariate.d.ts +15 -0
- package/dist/v8/builtins/multivariate.d.ts.map +1 -0
- package/dist/v8/builtins/multivariate.js +18 -0
- package/dist/v8/builtins/multivariate.js.map +1 -0
- package/dist/v8/builtins/secret.d.ts +7 -0
- package/dist/v8/builtins/secret.d.ts.map +1 -0
- package/dist/v8/builtins/secret.js +68 -0
- package/dist/v8/builtins/secret.js.map +1 -0
- package/dist/v8/canonical.d.ts +70 -0
- package/dist/v8/canonical.d.ts.map +1 -0
- package/dist/v8/canonical.js +130 -0
- package/dist/v8/canonical.js.map +1 -0
- package/dist/v8/ciphertext.d.ts +58 -0
- package/dist/v8/ciphertext.d.ts.map +1 -0
- package/dist/v8/ciphertext.js +134 -0
- package/dist/v8/ciphertext.js.map +1 -0
- package/dist/v8/cli/builtins.d.ts +11 -0
- package/dist/v8/cli/builtins.d.ts.map +1 -0
- package/dist/v8/cli/builtins.js +18 -0
- package/dist/v8/cli/builtins.js.map +1 -0
- package/dist/v8/cli/check/index.d.ts +41 -0
- package/dist/v8/cli/check/index.d.ts.map +1 -0
- package/dist/v8/cli/check/index.js +713 -0
- package/dist/v8/cli/check/index.js.map +1 -0
- package/dist/v8/cli/check/messages.d.ts +24 -0
- package/dist/v8/cli/check/messages.d.ts.map +1 -0
- package/dist/v8/cli/check/messages.js +150 -0
- package/dist/v8/cli/check/messages.js.map +1 -0
- package/dist/v8/cli/content.d.ts +60 -0
- package/dist/v8/cli/content.d.ts.map +1 -0
- package/dist/v8/cli/content.js +231 -0
- package/dist/v8/cli/content.js.map +1 -0
- package/dist/v8/cli/index.d.ts +18 -0
- package/dist/v8/cli/index.d.ts.map +1 -0
- package/dist/v8/cli/index.js +18 -0
- package/dist/v8/cli/index.js.map +1 -0
- package/dist/v8/cli/log.d.ts +9 -0
- package/dist/v8/cli/log.d.ts.map +1 -0
- package/dist/v8/cli/log.js +11 -0
- package/dist/v8/cli/log.js.map +1 -0
- package/dist/v8/cli/main.d.ts +2 -0
- package/dist/v8/cli/main.d.ts.map +1 -0
- package/dist/v8/cli/main.js +9 -0
- package/dist/v8/cli/main.js.map +1 -0
- package/dist/v8/cli/root.d.ts +28 -0
- package/dist/v8/cli/root.d.ts.map +1 -0
- package/dist/v8/cli/root.js +64 -0
- package/dist/v8/cli/root.js.map +1 -0
- package/dist/v8/cli/run.d.ts +13 -0
- package/dist/v8/cli/run.d.ts.map +1 -0
- package/dist/v8/cli/run.js +210 -0
- package/dist/v8/cli/run.js.map +1 -0
- package/dist/v8/cli/schema/builtinSchemas.d.ts +22 -0
- package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -0
- package/dist/v8/cli/schema/builtinSchemas.js +213 -0
- package/dist/v8/cli/schema/builtinSchemas.js.map +1 -0
- package/dist/v8/cli/schema/generate.d.ts +50 -0
- package/dist/v8/cli/schema/generate.d.ts.map +1 -0
- package/dist/v8/cli/schema/generate.js +415 -0
- package/dist/v8/cli/schema/generate.js.map +1 -0
- package/dist/v8/cli/schema/index.d.ts +21 -0
- package/dist/v8/cli/schema/index.d.ts.map +1 -0
- package/dist/v8/cli/schema/index.js +51 -0
- package/dist/v8/cli/schema/index.js.map +1 -0
- package/dist/v8/cli/schema/tsProgram.d.ts +126 -0
- package/dist/v8/cli/schema/tsProgram.d.ts.map +1 -0
- package/dist/v8/cli/schema/tsProgram.js +435 -0
- package/dist/v8/cli/schema/tsProgram.js.map +1 -0
- package/dist/v8/cli/schema/typeToSchema.d.ts +94 -0
- package/dist/v8/cli/schema/typeToSchema.d.ts.map +1 -0
- package/dist/v8/cli/schema/typeToSchema.js +752 -0
- package/dist/v8/cli/schema/typeToSchema.js.map +1 -0
- package/dist/v8/cli/serve/server.d.ts +31 -0
- package/dist/v8/cli/serve/server.d.ts.map +1 -0
- package/dist/v8/cli/serve/server.js +247 -0
- package/dist/v8/cli/serve/server.js.map +1 -0
- package/dist/v8/cli/watch.d.ts +20 -0
- package/dist/v8/cli/watch.d.ts.map +1 -0
- package/dist/v8/cli/watch.js +79 -0
- package/dist/v8/cli/watch.js.map +1 -0
- package/dist/v8/client.d.ts +20 -0
- package/dist/v8/client.d.ts.map +1 -0
- package/dist/v8/client.js +115 -0
- package/dist/v8/client.js.map +1 -0
- package/dist/v8/cms.d.ts +9 -0
- package/dist/v8/cms.d.ts.map +1 -0
- package/dist/v8/cms.js +217 -0
- package/dist/v8/cms.js.map +1 -0
- package/dist/v8/content.d.ts +51 -0
- package/dist/v8/content.d.ts.map +1 -0
- package/dist/v8/content.js +218 -0
- package/dist/v8/content.js.map +1 -0
- package/dist/v8/draft.d.ts +48 -0
- package/dist/v8/draft.d.ts.map +1 -0
- package/dist/v8/draft.js +195 -0
- package/dist/v8/draft.js.map +1 -0
- package/dist/v8/errors.d.ts +21 -0
- package/dist/v8/errors.d.ts.map +1 -0
- package/dist/v8/errors.js +39 -0
- package/dist/v8/errors.js.map +1 -0
- package/dist/v8/fetch.d.ts +26 -0
- package/dist/v8/fetch.d.ts.map +1 -0
- package/dist/v8/fetch.js +98 -0
- package/dist/v8/fetch.js.map +1 -0
- package/dist/v8/identity.d.ts +19 -0
- package/dist/v8/identity.d.ts.map +1 -0
- package/dist/v8/identity.js +54 -0
- package/dist/v8/identity.js.map +1 -0
- package/dist/v8/index.d.ts +11 -0
- package/dist/v8/index.d.ts.map +1 -0
- package/dist/v8/index.js +10 -0
- package/dist/v8/index.js.map +1 -0
- package/dist/v8/json.d.ts +19 -0
- package/dist/v8/json.d.ts.map +1 -0
- package/dist/v8/json.js +65 -0
- package/dist/v8/json.js.map +1 -0
- package/dist/v8/matchRoute.d.ts +30 -0
- package/dist/v8/matchRoute.d.ts.map +1 -0
- package/dist/v8/matchRoute.js +175 -0
- package/dist/v8/matchRoute.js.map +1 -0
- package/dist/v8/remoteLoader.d.ts +16 -0
- package/dist/v8/remoteLoader.d.ts.map +1 -0
- package/dist/v8/remoteLoader.js +427 -0
- package/dist/v8/remoteLoader.js.map +1 -0
- package/dist/v8/resolver.d.ts +34 -0
- package/dist/v8/resolver.d.ts.map +1 -0
- package/dist/v8/resolver.js +219 -0
- package/dist/v8/resolver.js.map +1 -0
- package/dist/v8/secrets.d.ts +9 -0
- package/dist/v8/secrets.d.ts.map +1 -0
- package/dist/v8/secrets.js +14 -0
- package/dist/v8/secrets.js.map +1 -0
- package/dist/v8/telemetry.d.ts +61 -0
- package/dist/v8/telemetry.d.ts.map +1 -0
- package/dist/v8/telemetry.js +460 -0
- package/dist/v8/telemetry.js.map +1 -0
- package/dist/v8/types.d.ts +226 -0
- package/dist/v8/types.d.ts.map +1 -0
- package/dist/v8/types.js +2 -0
- package/dist/v8/types.js.map +1 -0
- package/dist/v8/variants.d.ts +3 -0
- package/dist/v8/variants.d.ts.map +1 -0
- package/dist/v8/variants.js +65 -0
- package/dist/v8/variants.js.map +1 -0
- package/package.json +64 -18
- package/src/index.ts +1 -1
- package/src/protocol/canonical.ts +1 -1
- package/src/protocol/ciphertext.ts +1 -1
- package/src/protocol/client.ts +2 -2
- package/src/protocol/conformance/cases/assets.ts +3 -3
- package/src/protocol/conformance/cases/guards.ts +4 -4
- package/src/protocol/conformance/cases/reads.ts +4 -4
- package/src/protocol/conformance/cases/types.ts +1 -1
- package/src/protocol/conformance/cases/wire.ts +4 -4
- package/src/protocol/conformance/cases/writes.ts +4 -4
- package/src/protocol/conformance/contentHashFixtures.ts +57 -0
- package/src/protocol/conformance/context.ts +4 -4
- package/src/protocol/conformance/index.ts +15 -10
- package/src/protocol/index.ts +14 -7
- package/src/protocol/params.ts +2 -2
- package/src/protocol/secrets.ts +3 -3
- package/src/protocol/server/assets.ts +6 -6
- package/src/protocol/server/auth.ts +1 -1
- package/src/protocol/server/content.ts +5 -5
- package/src/protocol/server/core.ts +12 -6
- package/src/protocol/server/handler.ts +6 -6
- package/src/protocol/server/http.ts +1 -1
- package/src/protocol/server/index.ts +3 -3
- package/src/protocol/server/methods/apply.ts +9 -9
- package/src/protocol/server/methods/read.ts +5 -5
- package/src/protocol/server/rpc.ts +6 -6
- package/src/protocol/storage/fs/index.ts +7 -7
- package/src/protocol/storage/fs/lock.ts +1 -1
- package/src/protocol/storage/fs/unsupported.ts +2 -2
- package/src/protocol/storage/memory.ts +4 -4
- package/src/protocol/storage.ts +1 -1
- package/src/v8/analytics.ts +2 -2
- package/src/v8/builtins/data.ts +1 -1
- package/src/v8/builtins/index.ts +6 -6
- package/src/v8/builtins/lazy.ts +1 -1
- package/src/v8/builtins/legacy.ts +1 -1
- package/src/v8/builtins/multivariate.ts +1 -1
- package/src/v8/builtins/secret.ts +2 -2
- package/src/v8/canonical.ts +36 -0
- package/src/v8/cli/builtins.ts +2 -2
- package/src/v8/cli/check/index.ts +11 -11
- package/src/v8/cli/content.ts +6 -6
- package/src/v8/cli/index.ts +7 -7
- package/src/v8/cli/main.ts +1 -1
- package/src/v8/cli/run.ts +7 -7
- package/src/v8/cli/schema/builtinSchemas.ts +1 -1
- package/src/v8/cli/schema/generate.ts +8 -7
- package/src/v8/cli/schema/index.ts +4 -4
- package/src/v8/cli/schema/tsProgram.ts +18 -1
- package/src/v8/cli/schema/typeToSchema.ts +100 -4
- package/src/v8/cli/serve/server.ts +9 -9
- package/src/v8/client.ts +5 -5
- package/src/v8/cms.ts +23 -11
- package/src/v8/content.ts +58 -22
- package/src/v8/draft.ts +75 -6
- package/src/v8/errors.ts +1 -1
- package/src/v8/fetch.ts +1 -1
- package/src/v8/identity.ts +1 -1
- package/src/v8/index.ts +5 -5
- package/src/v8/matchRoute.ts +1 -1
- package/src/v8/remoteLoader.ts +246 -26
- package/src/v8/resolver.ts +6 -6
- package/src/v8/secrets.ts +1 -1
- package/src/v8/telemetry.ts +5 -5
- package/src/v8/types.ts +14 -1
- package/src/v8/variants.ts +61 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `matchRoute(url, items)`: finds the entry for a URL (see /next/routing and
|
|
3
|
+
* /next/router-internals).
|
|
4
|
+
*
|
|
5
|
+
* Paths compile into a segment trie, cached per array object: a lookup costs
|
|
6
|
+
* the URL's depth, not the number of routes. At every node the walk tries the
|
|
7
|
+
* literal child, then the parameter child, then a trailing `*` (a splat: one
|
|
8
|
+
* or more remaining segments), so exact paths win over parameters and
|
|
9
|
+
* parameters over a splat, per segment. Redirects are a second trie, checked
|
|
10
|
+
* first. Two entries that
|
|
11
|
+
* reach the same leaf are a conflict `deco check` reports; here the earlier
|
|
12
|
+
* one keeps it, so a lookup never throws. No regex, no sorting.
|
|
13
|
+
*/
|
|
14
|
+
import type { Match, Redirect, Route } from "./types.ts";
|
|
15
|
+
export declare function matchRoute<T extends Route>(url: string | URL | Request, items: {
|
|
16
|
+
routes: T[];
|
|
17
|
+
redirects?: Redirect[];
|
|
18
|
+
}): Match<T>;
|
|
19
|
+
/**
|
|
20
|
+
* The entries that reach a leaf an earlier entry already holds: two equal
|
|
21
|
+
* paths, or two templates of one shape (`/:slug/p` and `/:id/p`). `matchRoute`
|
|
22
|
+
* serves the earlier one; `deco check` reports each pair.
|
|
23
|
+
*/
|
|
24
|
+
export declare function findRouteConflicts<T extends {
|
|
25
|
+
path: string;
|
|
26
|
+
}>(entries: readonly T[]): {
|
|
27
|
+
entry: T;
|
|
28
|
+
other: T;
|
|
29
|
+
}[];
|
|
30
|
+
//# sourceMappingURL=matchRoute.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matchRoute.d.ts","sourceRoot":"","sources":["../../src/v8/matchRoute.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,EAAkB,KAAK,EAAE,QAAQ,EAAkB,KAAK,EAAE,MAAM,YAAY,CAAC;AAiBzF,wBAAgB,UAAU,CAAC,CAAC,SAAS,KAAK,EACxC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC3B,KAAK,EAAE;IAAE,MAAM,EAAE,CAAC,EAAE,CAAC;IAAC,SAAS,CAAC,EAAE,QAAQ,EAAE,CAAA;CAAE,GAC7C,KAAK,CAAC,CAAC,CAAC,CAwBV;AAsED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE;WAExD,CAAC;WAAS,CAAC;IAMtC"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
const routeTries = new WeakMap();
|
|
2
|
+
const redirectTries = new WeakMap();
|
|
3
|
+
const STATUSES = new Set([301, 302, 307, 308]);
|
|
4
|
+
export function matchRoute(url, items) {
|
|
5
|
+
const target = parseTarget(url);
|
|
6
|
+
if (target === null)
|
|
7
|
+
return { kind: "not-found" };
|
|
8
|
+
const redirects = Array.isArray(items?.redirects) ? items.redirects : [];
|
|
9
|
+
const redirectTrie = cached(redirectTries, redirects, () => redirects.flatMap((input) => {
|
|
10
|
+
const r = normalizeRedirect(input);
|
|
11
|
+
return r ? [[r.from, r]] : [];
|
|
12
|
+
}));
|
|
13
|
+
const redirect = lookup(redirectTrie, target.segments);
|
|
14
|
+
if (redirect !== null) {
|
|
15
|
+
const location = buildLocation(redirect, target.search);
|
|
16
|
+
return { kind: "redirect", location, status: redirect.value.status };
|
|
17
|
+
}
|
|
18
|
+
const routes = Array.isArray(items?.routes) ? items.routes : [];
|
|
19
|
+
const trie = cached(routeTries, routes, () => routes.filter((r) => typeof r?.path === "string").map((r) => [r.path, r]));
|
|
20
|
+
const found = lookup(trie, target.segments);
|
|
21
|
+
if (found === null)
|
|
22
|
+
return { kind: "not-found" };
|
|
23
|
+
return { kind: "match", entry: found.value, params: found.params };
|
|
24
|
+
}
|
|
25
|
+
/** The URL's path segments (percent-decoded) and its query string, with its `?`, or `""`. */
|
|
26
|
+
function parseTarget(url) {
|
|
27
|
+
try {
|
|
28
|
+
const raw = typeof url === "string" ? url : url instanceof URL ? url.href : url.url;
|
|
29
|
+
// A bare path stays a path: `//summer` is not a host named "summer".
|
|
30
|
+
const parsed = raw.startsWith("/") ? new URL(raw, "http://path.invalid") : new URL(raw);
|
|
31
|
+
const pathname = raw.startsWith("/") ? raw : parsed.pathname;
|
|
32
|
+
return { segments: splitPath(pathname).map(safeDecode), search: parsed.search };
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** `/a/b/`, `/a//b` and `/a/b?x` all name `["a", "b"]`; `/` names `[]`. */
|
|
39
|
+
function splitPath(path) {
|
|
40
|
+
const query = path.indexOf("?");
|
|
41
|
+
const hash = path.indexOf("#");
|
|
42
|
+
const end = Math.min(query === -1 ? path.length : query, hash === -1 ? path.length : hash);
|
|
43
|
+
return path
|
|
44
|
+
.slice(0, end)
|
|
45
|
+
.split("/")
|
|
46
|
+
.filter((segment) => segment.length > 0);
|
|
47
|
+
}
|
|
48
|
+
function safeDecode(segment) {
|
|
49
|
+
try {
|
|
50
|
+
return decodeURIComponent(segment);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return segment;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** The trie for an array, built once per array object. */
|
|
57
|
+
function cached(cache, key, entries) {
|
|
58
|
+
const hit = cache.get(key);
|
|
59
|
+
if (hit)
|
|
60
|
+
return hit;
|
|
61
|
+
const root = { literals: new Map() };
|
|
62
|
+
for (const [path, value] of entries())
|
|
63
|
+
insert(root, path, value);
|
|
64
|
+
cache.set(key, root);
|
|
65
|
+
return root;
|
|
66
|
+
}
|
|
67
|
+
/** Adds `path`; returns the value already holding its leaf (a conflict: the earlier keeps it). */
|
|
68
|
+
function insert(root, path, value) {
|
|
69
|
+
const names = [];
|
|
70
|
+
const segments = splitPath(path);
|
|
71
|
+
let node = root;
|
|
72
|
+
for (const [i, segment] of segments.entries()) {
|
|
73
|
+
if (segment === "*" && i === segments.length - 1) {
|
|
74
|
+
if (node.splat)
|
|
75
|
+
return node.splat.value;
|
|
76
|
+
node.splat = { value, names };
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
if (segment.startsWith(":") && segment.length > 1) {
|
|
80
|
+
names.push(segment.slice(1));
|
|
81
|
+
node = node.param ??= { literals: new Map() };
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const literal = safeDecode(segment);
|
|
85
|
+
const child = node.literals.get(literal) ?? { literals: new Map() };
|
|
86
|
+
node.literals.set(literal, child);
|
|
87
|
+
node = child;
|
|
88
|
+
}
|
|
89
|
+
if (node.leaf)
|
|
90
|
+
return node.leaf.value;
|
|
91
|
+
node.leaf = { value, names };
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The entries that reach a leaf an earlier entry already holds: two equal
|
|
96
|
+
* paths, or two templates of one shape (`/:slug/p` and `/:id/p`). `matchRoute`
|
|
97
|
+
* serves the earlier one; `deco check` reports each pair.
|
|
98
|
+
*/
|
|
99
|
+
export function findRouteConflicts(entries) {
|
|
100
|
+
const root = { literals: new Map() };
|
|
101
|
+
const conflicts = [];
|
|
102
|
+
for (const entry of entries) {
|
|
103
|
+
const other = insert(root, entry.path, entry);
|
|
104
|
+
if (other !== undefined)
|
|
105
|
+
conflicts.push({ entry, other });
|
|
106
|
+
}
|
|
107
|
+
return conflicts;
|
|
108
|
+
}
|
|
109
|
+
/** Walks the URL's segments: the literal child first, then the parameter child, then the splat. */
|
|
110
|
+
function lookup(node, segs, i = 0, vals = []) {
|
|
111
|
+
if (i === segs.length)
|
|
112
|
+
return node.leaf ? hit(node.leaf, vals, []) : null;
|
|
113
|
+
const literal = node.literals.get(segs[i]);
|
|
114
|
+
let found = literal ? lookup(literal, segs, i + 1, vals) : null;
|
|
115
|
+
// A parameter is one segment and never contains a slash, even an encoded one.
|
|
116
|
+
if (!found && node.param && !segs[i].includes("/")) {
|
|
117
|
+
found = lookup(node.param, segs, i + 1, [...vals, segs[i]]);
|
|
118
|
+
}
|
|
119
|
+
// A splat takes the rest (at least one segment, as there is one here).
|
|
120
|
+
return found ?? (node.splat ? hit(node.splat, vals, segs.slice(i)) : null);
|
|
121
|
+
}
|
|
122
|
+
function hit({ value, names }, values, rest) {
|
|
123
|
+
const params = Object.fromEntries(names.map((n, i) => [n, values[i]]));
|
|
124
|
+
if (rest.length > 0)
|
|
125
|
+
params["*"] = rest.join("/");
|
|
126
|
+
return { value, params, rest };
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Both redirect shapes: the flat built-in `{ from, to, permanent, status?,
|
|
130
|
+
* discardQueryParameters? }` and the site editor's legacy nested
|
|
131
|
+
* `{ redirect: { from, to, type } }` (see /next/studio-compatibility), where
|
|
132
|
+
* `permanent` is a 301 and anything else a 307.
|
|
133
|
+
*/
|
|
134
|
+
function normalizeRedirect(input) {
|
|
135
|
+
if (typeof input !== "object" || input === null)
|
|
136
|
+
return null;
|
|
137
|
+
const nested = input.redirect;
|
|
138
|
+
const flat = (nested ?? input);
|
|
139
|
+
if (typeof flat.from !== "string" || typeof flat.to !== "string")
|
|
140
|
+
return null;
|
|
141
|
+
let status = flat.permanent === true ? 301 : 302;
|
|
142
|
+
if (nested)
|
|
143
|
+
status = flat.type === "permanent" ? 301 : 307;
|
|
144
|
+
else if (STATUSES.has(flat.status))
|
|
145
|
+
status = flat.status;
|
|
146
|
+
const { from, to, discardQueryParameters } = flat;
|
|
147
|
+
return { from, to, permanent: status === 301 || status === 308, status, discardQueryParameters };
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Fills the redirect's `:name` segments with the parameters `from` captured,
|
|
151
|
+
* and a `*` segment with the splat's segments, then carries the request's
|
|
152
|
+
* query string over unless the redirect discards it. Each captured segment is
|
|
153
|
+
* re-encoded on its own and none is empty, so a `to` on this site can't turn
|
|
154
|
+
* into `//other.host` (or `/\other.host`).
|
|
155
|
+
*/
|
|
156
|
+
function buildLocation({ value: redirect, params, rest }, search) {
|
|
157
|
+
const { to } = redirect;
|
|
158
|
+
const hashAt = to.indexOf("#") === -1 ? to.length : to.indexOf("#");
|
|
159
|
+
const queryAt = to.indexOf("?") === -1 || to.indexOf("?") > hashAt ? hashAt : to.indexOf("?");
|
|
160
|
+
const filled = to
|
|
161
|
+
.slice(0, queryAt)
|
|
162
|
+
.split("/")
|
|
163
|
+
.map((s) => {
|
|
164
|
+
if (s === "*" && rest.length > 0)
|
|
165
|
+
return rest.map(encodeURIComponent).join("/");
|
|
166
|
+
return Object.hasOwn(params, s.slice(1)) && s[0] === ":"
|
|
167
|
+
? encodeURIComponent(params[s.slice(1)])
|
|
168
|
+
: s;
|
|
169
|
+
})
|
|
170
|
+
.join("/");
|
|
171
|
+
const carried = redirect.discardQueryParameters ? "" : search.slice(1);
|
|
172
|
+
const query = [to.slice(queryAt + 1, hashAt), carried].filter(Boolean).join("&");
|
|
173
|
+
return `${filled}${query ? `?${query}` : ""}${to.slice(hashAt)}`;
|
|
174
|
+
}
|
|
175
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,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;AACnE,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Loader, Snapshot } from "./types.ts";
|
|
2
|
+
interface RemoteLoaderOptions {
|
|
3
|
+
site?: string;
|
|
4
|
+
token?: string;
|
|
5
|
+
/** ms between release checks, used when `createCMS` has no `interval` of its own. */
|
|
6
|
+
interval?: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Hosted releases and drafts over a fallback. `createCMS` builds it for you
|
|
10
|
+
* when `site` and `token` are set. With either unset (a dev or test
|
|
11
|
+
* environment without the variables), it's a loader over the fallback alone.
|
|
12
|
+
*/
|
|
13
|
+
export declare function remoteLoader(fallback: Snapshot | Loader, options: RemoteLoaderOptions): Loader;
|
|
14
|
+
export declare function resetRemoteLoaders(): void;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=remoteLoader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remoteLoader.d.ts","sourceRoot":"","sources":["../../src/v8/remoteLoader.ts"],"names":[],"mappings":"AA6CA,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAkBnD,UAAU,mBAAmB;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qFAAqF;IACrF,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAmRD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,EAAE,OAAO,EAAE,mBAAmB,GAAG,MAAM,CAwB9F;AAED,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC"}
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `remoteLoader`: hosted releases and drafts from the Deco API, over a
|
|
3
|
+
* fallback (the content module, or any loader). See
|
|
4
|
+
* /next/hosted-releases-internals and /next/hosted-publishing.
|
|
5
|
+
*
|
|
6
|
+
* - `load()` never touches the network: it returns the newest release this
|
|
7
|
+
* process fetched, or the fallback's content until one is fetched.
|
|
8
|
+
* - `update()` asks the delivery API for the small production channel
|
|
9
|
+
* manifest; the CMS calls it on first use and then every interval, at an
|
|
10
|
+
* idle moment. A manifest older than the newest observed generation is
|
|
11
|
+
* ignored; a revision equal to the fallback's is served from the fallback
|
|
12
|
+
* without a download; anything else is fetched, verified against its
|
|
13
|
+
* content hash and swapped in whole. Any error keeps memory as it was.
|
|
14
|
+
* - `load(pointer)` loads a draft overlay (see /next/content-delivery#exact-draft-previews)
|
|
15
|
+
* and waits for it: it captures the production snapshot `load()` would
|
|
16
|
+
* return right now, fetches the overlay manifest the pointer's version names
|
|
17
|
+
* and only the changed-block assets not already cached, and layers their
|
|
18
|
+
* replacements and tombstones over the captured snapshot. It never fetches a
|
|
19
|
+
* production revision to align. An authorized manifest is cached only as long
|
|
20
|
+
* as its response's `max-age` (the grant's remaining life), so an expired
|
|
21
|
+
* grant stops working on a warm server too. Only pointers into this site's drafts on the
|
|
22
|
+
* delivery host are fetched; anything else is refused. Any failure rejects:
|
|
23
|
+
* a draft never falls back to published content.
|
|
24
|
+
* - In development (`NODE_ENV=development`), releases stay on the fallback so
|
|
25
|
+
* local files win; drafts still load.
|
|
26
|
+
* - A release, or any one draft asset, larger than `MAX_SNAPSHOT_BYTES` is
|
|
27
|
+
* refused while it downloads, before it's buffered whole (a Worker isolate
|
|
28
|
+
* has 128 MB); a draft whose manifest and changed blocks together exceed it
|
|
29
|
+
* is refused as each asset is counted.
|
|
30
|
+
* - Without `site` or `token` it's a plain loader over the fallback: no
|
|
31
|
+
* releases, no drafts beyond what the fallback serves.
|
|
32
|
+
*
|
|
33
|
+
* Instances are process-wide singletons, like `createCMS`'s.
|
|
34
|
+
*/
|
|
35
|
+
import { computeBlockHash, computeContentRevision, computeOverlayVersion, DRAFT_OVERLAY_FORMAT, } from "./canonical.js";
|
|
36
|
+
import { BoundedMap, isSnapshot } from "./content.js";
|
|
37
|
+
import { parseDraftPointer } from "./draft.js";
|
|
38
|
+
import { clearGlobals, contentIdentity, fnv1a } from "./identity.js";
|
|
39
|
+
import { isPlainObject } from "./json.js";
|
|
40
|
+
/** The hosted delivery origin: channel manifests, release assets and drafts. */
|
|
41
|
+
const HOSTED_DELIVERY_ORIGIN = "https://delivery.decocms.com";
|
|
42
|
+
const INSTANCE_PREFIX = "decocms.blocks.remote:";
|
|
43
|
+
const MANIFEST_FORMAT = 1;
|
|
44
|
+
const FETCH_TIMEOUT_MS = 10_000;
|
|
45
|
+
/** The largest release, or draft (manifest plus changed blocks), accepted, in bytes of JSON. */
|
|
46
|
+
const MAX_SNAPSHOT_BYTES = 64 * 1024 * 1024;
|
|
47
|
+
/** Draft caches, per site: authorized overlay manifests, and changed-block bytes. */
|
|
48
|
+
const CACHED_OVERLAYS = 16;
|
|
49
|
+
const CACHED_BLOCK_BYTES = 16 * 1024 * 1024;
|
|
50
|
+
/** Changed-block downloads in flight at once, per draft. */
|
|
51
|
+
const BLOCK_FETCH_CONCURRENCY = 8;
|
|
52
|
+
/** Overlay versions and block hashes: SHA-256, lowercase hex. */
|
|
53
|
+
const HASH_RE = /^[0-9a-f]{64}$/;
|
|
54
|
+
class RemoteLoader {
|
|
55
|
+
#site;
|
|
56
|
+
#token;
|
|
57
|
+
/** The `interval` this loader was created with; `createCMS` reads it. */
|
|
58
|
+
interval;
|
|
59
|
+
#fallback;
|
|
60
|
+
#fallbackRevision;
|
|
61
|
+
#current;
|
|
62
|
+
#generation = -1;
|
|
63
|
+
/** Authorized manifests, by overlay version and grant: a version alone unlocks nothing. */
|
|
64
|
+
#overlays = new BoundedMap(CACHED_OVERLAYS);
|
|
65
|
+
/** Verified changed blocks, by block hash, bounded by their bytes. */
|
|
66
|
+
#blocks = new BoundedMap(CACHED_BLOCK_BYTES, (b) => b.bytes);
|
|
67
|
+
#pendingBlocks = new Map();
|
|
68
|
+
constructor(fallback, options) {
|
|
69
|
+
this.#fallback = fallback;
|
|
70
|
+
this.#site = options.site;
|
|
71
|
+
this.#token = options.token;
|
|
72
|
+
this.interval = options.interval;
|
|
73
|
+
}
|
|
74
|
+
/** A hot reload hands the same instance new fallback content. */
|
|
75
|
+
adopt(fallback) {
|
|
76
|
+
if (fallback === this.#fallback)
|
|
77
|
+
return;
|
|
78
|
+
this.#fallback = fallback;
|
|
79
|
+
this.#fallbackRevision = undefined;
|
|
80
|
+
}
|
|
81
|
+
load(pointer) {
|
|
82
|
+
if (pointer)
|
|
83
|
+
return this.#draft(pointer);
|
|
84
|
+
if (this.#current !== undefined)
|
|
85
|
+
return Promise.resolve(this.#current);
|
|
86
|
+
return this.#loadFallback();
|
|
87
|
+
}
|
|
88
|
+
async update() {
|
|
89
|
+
if (isDevelopment())
|
|
90
|
+
return { updated: false };
|
|
91
|
+
const manifest = await this.#manifest();
|
|
92
|
+
if (manifest.generation < this.#generation)
|
|
93
|
+
return { updated: false };
|
|
94
|
+
// Best effort: a fallback that can't load (a KV key the deploy never wrote) is
|
|
95
|
+
// fixed by downloading the release, not by failing the check.
|
|
96
|
+
const fallbackRevision = await this.#fallbackRevisionNow().catch(() => undefined);
|
|
97
|
+
const served = this.#current !== undefined ? this.#current.revision : fallbackRevision;
|
|
98
|
+
if (served !== undefined && manifest.revision === served) {
|
|
99
|
+
// A new generation of the same content (a re-promotion, a rollback to
|
|
100
|
+
// it) still signals, so caches keyed on the release are invalidated.
|
|
101
|
+
const changed = this.#generation !== -1 && manifest.generation > this.#generation;
|
|
102
|
+
this.#generation = manifest.generation;
|
|
103
|
+
return { updated: changed };
|
|
104
|
+
}
|
|
105
|
+
let next;
|
|
106
|
+
if (fallbackRevision === undefined || manifest.revision !== fallbackRevision) {
|
|
107
|
+
next = await this.#release(manifest);
|
|
108
|
+
}
|
|
109
|
+
// A slower, earlier check must not undo a newer publish or rollback.
|
|
110
|
+
if (manifest.generation < this.#generation)
|
|
111
|
+
return { updated: false };
|
|
112
|
+
this.#generation = manifest.generation;
|
|
113
|
+
this.#current = next;
|
|
114
|
+
return { updated: true };
|
|
115
|
+
}
|
|
116
|
+
async #loadFallback() {
|
|
117
|
+
const fallback = this.#fallback;
|
|
118
|
+
const snapshot = isSnapshot(fallback) ? fallback : await fallback.load();
|
|
119
|
+
if (fallback === this.#fallback)
|
|
120
|
+
this.#fallbackRevision = snapshot.revision;
|
|
121
|
+
return snapshot;
|
|
122
|
+
}
|
|
123
|
+
async #fallbackRevisionNow() {
|
|
124
|
+
return this.#fallbackRevision ?? (await this.#loadFallback()).revision;
|
|
125
|
+
}
|
|
126
|
+
async #manifest() {
|
|
127
|
+
const site = encodeURIComponent(this.#site);
|
|
128
|
+
const response = await fetchWithTimeout(`${HOSTED_DELIVERY_ORIGIN}/sites/${site}/channels/production.json`, this.#auth());
|
|
129
|
+
if (!response.ok)
|
|
130
|
+
throw new Error(`channel manifest: HTTP ${response.status}`);
|
|
131
|
+
const body = await response.json();
|
|
132
|
+
const prefix = `/sites/${site}/revisions/`;
|
|
133
|
+
if (!isPlainObject(body) ||
|
|
134
|
+
body.format !== MANIFEST_FORMAT ||
|
|
135
|
+
!Number.isSafeInteger(body.generation) ||
|
|
136
|
+
typeof body.revision !== "string" ||
|
|
137
|
+
typeof body.snapshot !== "string" ||
|
|
138
|
+
!body.snapshot.startsWith(prefix) ||
|
|
139
|
+
!/^[\w.-]+\.json$/.test(body.snapshot.slice(prefix.length)) ||
|
|
140
|
+
body.snapshot.includes("..")) {
|
|
141
|
+
throw new Error("channel manifest: unexpected format or snapshot path");
|
|
142
|
+
}
|
|
143
|
+
return body;
|
|
144
|
+
}
|
|
145
|
+
async #release(manifest) {
|
|
146
|
+
const response = await fetchWithTimeout(`${HOSTED_DELIVERY_ORIGIN}${manifest.snapshot}`, this.#auth());
|
|
147
|
+
if (!response.ok)
|
|
148
|
+
throw new Error(`release ${manifest.revision}: HTTP ${response.status}`);
|
|
149
|
+
const [snapshot] = await readBoundedJson(response, `release ${manifest.revision}`, {
|
|
150
|
+
remaining: MAX_SNAPSHOT_BYTES,
|
|
151
|
+
});
|
|
152
|
+
if (!isSnapshot(snapshot) ||
|
|
153
|
+
snapshot.revision !== manifest.revision ||
|
|
154
|
+
(await computeContentRevision(snapshot.blocks)) !== manifest.revision) {
|
|
155
|
+
throw new Error(`release ${manifest.revision}: content doesn't match its revision`);
|
|
156
|
+
}
|
|
157
|
+
return snapshot;
|
|
158
|
+
}
|
|
159
|
+
async #draft(raw) {
|
|
160
|
+
const pointer = parseDraftPointer(raw);
|
|
161
|
+
if (pointer === null)
|
|
162
|
+
throw new Error("invalid draft pointer");
|
|
163
|
+
if (pointer.host !== new URL(HOSTED_DELIVERY_ORIGIN).host) {
|
|
164
|
+
throw new Error(`draft pointer names an unexpected host "${pointer.host}"`);
|
|
165
|
+
}
|
|
166
|
+
const site = `/sites/${encodeURIComponent(this.#site)}`;
|
|
167
|
+
const query = pointer.path.indexOf("?");
|
|
168
|
+
const pathname = query === -1 ? pointer.path : pointer.path.slice(0, query);
|
|
169
|
+
if (pathname !== `${site}/drafts`)
|
|
170
|
+
throw new Error("draft pointer names another site's drafts");
|
|
171
|
+
if (!HASH_RE.test(pointer.version))
|
|
172
|
+
throw new Error("draft pointer names no overlay version");
|
|
173
|
+
// The grant (the pointer's query) goes, opaque, on every draft asset read.
|
|
174
|
+
const grant = query === -1 ? "" : pointer.path.slice(query);
|
|
175
|
+
// Capture production once, before any network: this draft is layered over it.
|
|
176
|
+
const base = this.#current ?? (await this.#loadFallback());
|
|
177
|
+
const budget = { remaining: MAX_SNAPSHOT_BYTES };
|
|
178
|
+
const manifest = charge(budget, await this.#authorizedOverlay(`${site}/drafts/${pointer.version}.json${grant}`, pointer.version));
|
|
179
|
+
const hashes = [...new Set(Object.values(manifest.set))];
|
|
180
|
+
const values = new Map();
|
|
181
|
+
let next = 0;
|
|
182
|
+
const worker = async () => {
|
|
183
|
+
while (next < hashes.length) {
|
|
184
|
+
const hash = hashes[next++];
|
|
185
|
+
values.set(hash, await this.#block(`${site}/draft-blocks/${hash}.json${grant}`, hash, budget));
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
const workers = Math.min(BLOCK_FETCH_CONCURRENCY, hashes.length);
|
|
189
|
+
await Promise.all(Array.from({ length: workers }, worker));
|
|
190
|
+
return composeOverlay(base, manifest, values, pointer.version);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The manifest a pointer's grant authorizes: cached by version and grant
|
|
194
|
+
* until the response's `max-age` runs out (none, or 0, isn't cached), so an
|
|
195
|
+
* expired grant is asked about again. Concurrent callers share a download.
|
|
196
|
+
*/
|
|
197
|
+
async #authorizedOverlay(path, version) {
|
|
198
|
+
const asked = Date.now();
|
|
199
|
+
const cached = this.#overlays.get(path);
|
|
200
|
+
if (cached !== undefined) {
|
|
201
|
+
const overlay = await cached;
|
|
202
|
+
if (overlay.expiresAt >= asked)
|
|
203
|
+
return overlay;
|
|
204
|
+
if (this.#overlays.get(path) === cached)
|
|
205
|
+
this.#overlays.delete(path);
|
|
206
|
+
}
|
|
207
|
+
const pending = this.#overlay(path, version);
|
|
208
|
+
this.#overlays.set(path, pending);
|
|
209
|
+
const forget = () => {
|
|
210
|
+
if (this.#overlays.get(path) === pending)
|
|
211
|
+
this.#overlays.delete(path);
|
|
212
|
+
};
|
|
213
|
+
pending.then((overlay) => {
|
|
214
|
+
if (overlay.expiresAt <= Date.now())
|
|
215
|
+
forget();
|
|
216
|
+
}, forget);
|
|
217
|
+
return pending;
|
|
218
|
+
}
|
|
219
|
+
async #overlay(path, version) {
|
|
220
|
+
const response = await fetchWithTimeout(`${HOSTED_DELIVERY_ORIGIN}${path}`, this.#auth());
|
|
221
|
+
if (!response.ok)
|
|
222
|
+
throw new Error(`draft overlay ${version}: HTTP ${response.status}`);
|
|
223
|
+
const maxAge = cacheLifetimeMs(response.headers.get("cache-control"));
|
|
224
|
+
const [body, bytes] = await readBoundedJson(response, `draft overlay ${version}`, {
|
|
225
|
+
remaining: MAX_SNAPSHOT_BYTES,
|
|
226
|
+
});
|
|
227
|
+
const overlay = parseOverlay(body);
|
|
228
|
+
if ((await computeOverlayVersion(overlay)) !== version) {
|
|
229
|
+
throw new Error(`draft overlay ${version}: content doesn't match its version`);
|
|
230
|
+
}
|
|
231
|
+
return { value: overlay, bytes, expiresAt: Date.now() + maxAge };
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* A changed block, charged to this draft's budget: from the cache, or
|
|
235
|
+
* downloaded once and verified against its hash. Concurrent drafts share
|
|
236
|
+
* one download, which is bounded on its own and then charged to each of
|
|
237
|
+
* them, so one draft's budget never fails another. Only hashes an
|
|
238
|
+
* authorized manifest lists get here.
|
|
239
|
+
*/
|
|
240
|
+
async #block(path, hash, budget) {
|
|
241
|
+
const cached = this.#blocks.get(hash);
|
|
242
|
+
if (cached !== undefined)
|
|
243
|
+
return charge(budget, cached);
|
|
244
|
+
let pending = this.#pendingBlocks.get(hash);
|
|
245
|
+
if (pending === undefined) {
|
|
246
|
+
const download = (async () => {
|
|
247
|
+
const response = await fetchWithTimeout(`${HOSTED_DELIVERY_ORIGIN}${path}`, this.#auth());
|
|
248
|
+
if (!response.ok)
|
|
249
|
+
throw new Error(`draft block ${hash}: HTTP ${response.status}`);
|
|
250
|
+
const [value, bytes] = await readBoundedJson(response, `draft block ${hash}`, {
|
|
251
|
+
remaining: MAX_SNAPSHOT_BYTES,
|
|
252
|
+
});
|
|
253
|
+
if ((await computeBlockHash(value)) !== hash) {
|
|
254
|
+
throw new Error(`draft block ${hash}: content doesn't match its hash`);
|
|
255
|
+
}
|
|
256
|
+
const block = { value, bytes };
|
|
257
|
+
this.#blocks.set(hash, block);
|
|
258
|
+
return block;
|
|
259
|
+
})();
|
|
260
|
+
pending = download;
|
|
261
|
+
this.#pendingBlocks.set(hash, download);
|
|
262
|
+
void download.then(() => this.#pendingBlocks.delete(hash), () => this.#pendingBlocks.delete(hash));
|
|
263
|
+
}
|
|
264
|
+
return charge(budget, await pending);
|
|
265
|
+
}
|
|
266
|
+
#auth() {
|
|
267
|
+
return { authorization: `Bearer ${this.#token}` };
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/** A loader over the fallback alone: `remoteLoader` without `site` or `token`. */
|
|
271
|
+
class LocalLoader {
|
|
272
|
+
#fallback;
|
|
273
|
+
constructor(fallback) {
|
|
274
|
+
this.#fallback = fallback;
|
|
275
|
+
}
|
|
276
|
+
adopt(fallback) {
|
|
277
|
+
this.#fallback = fallback;
|
|
278
|
+
}
|
|
279
|
+
load(pointer) {
|
|
280
|
+
const fallback = this.#fallback;
|
|
281
|
+
return isSnapshot(fallback) ? Promise.resolve(fallback) : fallback.load(pointer);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Hosted releases and drafts over a fallback. `createCMS` builds it for you
|
|
286
|
+
* when `site` and `token` are set. With either unset (a dev or test
|
|
287
|
+
* environment without the variables), it's a loader over the fallback alone.
|
|
288
|
+
*/
|
|
289
|
+
export function remoteLoader(fallback, options) {
|
|
290
|
+
const { site, token } = options ?? {};
|
|
291
|
+
const hosted = Boolean(site && token);
|
|
292
|
+
const key = Symbol.for(`${INSTANCE_PREFIX}${contentIdentity(fallback)}` +
|
|
293
|
+
(hosted ? `|site:${site}|token:${fnv1a(token)}` : "|local"));
|
|
294
|
+
const store = globalThis;
|
|
295
|
+
const existing = store[key];
|
|
296
|
+
if (existing instanceof Object && typeof existing.adopt === "function") {
|
|
297
|
+
if (existing instanceof RemoteLoader && existing.interval !== options.interval) {
|
|
298
|
+
console.warn("[decocms/blocks] remoteLoader was called again for the same site with different options " +
|
|
299
|
+
"(interval); keeping the first instance's options.");
|
|
300
|
+
}
|
|
301
|
+
existing.adopt(fallback);
|
|
302
|
+
return existing;
|
|
303
|
+
}
|
|
304
|
+
const instance = hosted
|
|
305
|
+
? new RemoteLoader(fallback, { site: site, token: token, interval: options.interval })
|
|
306
|
+
: new LocalLoader(fallback);
|
|
307
|
+
store[key] = instance;
|
|
308
|
+
return instance;
|
|
309
|
+
}
|
|
310
|
+
export function resetRemoteLoaders() {
|
|
311
|
+
clearGlobals(INSTANCE_PREFIX);
|
|
312
|
+
}
|
|
313
|
+
/** Charges an asset's bytes to a draft's budget, and returns its value. */
|
|
314
|
+
function charge(budget, asset) {
|
|
315
|
+
budget.remaining -= asset.bytes;
|
|
316
|
+
if (budget.remaining < 0)
|
|
317
|
+
throw new Error(`draft: larger than ${MAX_SNAPSHOT_BYTES} bytes`);
|
|
318
|
+
return asset.value;
|
|
319
|
+
}
|
|
320
|
+
/** A response's private cache lifetime in ms: its `max-age`, or 0 without one or with `no-store`. */
|
|
321
|
+
function cacheLifetimeMs(header) {
|
|
322
|
+
if (header === null || /(?:^|,)\s*no-store\b/i.test(header))
|
|
323
|
+
return 0;
|
|
324
|
+
const seconds = /(?:^|,)\s*max-age=(\d+)/i.exec(header)?.[1];
|
|
325
|
+
return seconds === undefined ? 0 : Number(seconds) * 1000;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Parses a JSON body and returns it with its size, refusing it as soon as it
|
|
329
|
+
* would exceed the budget (charged as bytes arrive, so parallel reads share it).
|
|
330
|
+
*/
|
|
331
|
+
async function readBoundedJson(response, label, budget) {
|
|
332
|
+
const tooLarge = () => new Error(`${label}: larger than ${MAX_SNAPSHOT_BYTES} bytes`);
|
|
333
|
+
if (Number(response.headers.get("content-length")) > budget.remaining) {
|
|
334
|
+
await response.body?.cancel();
|
|
335
|
+
throw tooLarge();
|
|
336
|
+
}
|
|
337
|
+
if (response.body === null) {
|
|
338
|
+
const text = await response.text();
|
|
339
|
+
budget.remaining -= text.length;
|
|
340
|
+
if (budget.remaining < 0)
|
|
341
|
+
throw tooLarge();
|
|
342
|
+
return [JSON.parse(text), text.length];
|
|
343
|
+
}
|
|
344
|
+
const reader = response.body.getReader();
|
|
345
|
+
const decoder = new TextDecoder();
|
|
346
|
+
const parts = [];
|
|
347
|
+
let size = 0;
|
|
348
|
+
for (;;) {
|
|
349
|
+
const { done, value } = await reader.read();
|
|
350
|
+
if (done)
|
|
351
|
+
break;
|
|
352
|
+
size += value.byteLength;
|
|
353
|
+
budget.remaining -= value.byteLength;
|
|
354
|
+
if (budget.remaining < 0) {
|
|
355
|
+
await reader.cancel();
|
|
356
|
+
throw tooLarge();
|
|
357
|
+
}
|
|
358
|
+
parts.push(decoder.decode(value, { stream: true }));
|
|
359
|
+
}
|
|
360
|
+
parts.push(decoder.decode());
|
|
361
|
+
return [JSON.parse(parts.join("")), size];
|
|
362
|
+
}
|
|
363
|
+
/** Validates a draft overlay manifest; throws on an unknown format or any unexpected shape. */
|
|
364
|
+
function parseOverlay(body) {
|
|
365
|
+
const invalid = (why) => new Error(`draft overlay: ${why}`);
|
|
366
|
+
if (!isPlainObject(body) || body.format !== DRAFT_OVERLAY_FORMAT) {
|
|
367
|
+
throw invalid("unknown format");
|
|
368
|
+
}
|
|
369
|
+
if (Object.keys(body).some((key) => key !== "format" && key !== "set" && key !== "delete")) {
|
|
370
|
+
throw invalid("unexpected field");
|
|
371
|
+
}
|
|
372
|
+
const { set, delete: deleted } = body;
|
|
373
|
+
if (!isPlainObject(set))
|
|
374
|
+
throw invalid("set must be an object");
|
|
375
|
+
for (const hash of Object.values(set)) {
|
|
376
|
+
if (typeof hash !== "string" || !HASH_RE.test(hash))
|
|
377
|
+
throw invalid("set names a bad hash");
|
|
378
|
+
}
|
|
379
|
+
if (!Array.isArray(deleted) || deleted.some((name) => typeof name !== "string")) {
|
|
380
|
+
throw invalid("delete must be a list of names");
|
|
381
|
+
}
|
|
382
|
+
if (new Set(deleted).size !== deleted.length)
|
|
383
|
+
throw invalid("delete repeats a name");
|
|
384
|
+
if (deleted.some((name) => Object.hasOwn(set, name)))
|
|
385
|
+
throw invalid("set and delete overlap");
|
|
386
|
+
return body;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* The draft as a snapshot: the captured production entries, with the
|
|
390
|
+
* overlay's replacements and without its tombstones. A shallow copy: every
|
|
391
|
+
* unchanged entry is production's own object, and production is never
|
|
392
|
+
* mutated. Its revision is an opaque identity of the pair, not a content hash.
|
|
393
|
+
*/
|
|
394
|
+
function composeOverlay(base, overlay, values, version) {
|
|
395
|
+
const blocks = { ...base.blocks };
|
|
396
|
+
for (const name of overlay.delete)
|
|
397
|
+
delete blocks[name];
|
|
398
|
+
for (const [name, hash] of Object.entries(overlay.set)) {
|
|
399
|
+
// defineProperty, so an entry named "__proto__" stays an entry.
|
|
400
|
+
Object.defineProperty(blocks, name, {
|
|
401
|
+
value: values.get(hash),
|
|
402
|
+
enumerable: true,
|
|
403
|
+
writable: true,
|
|
404
|
+
configurable: true,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
const view = { revision: `${base.revision}~${version}`, blocks };
|
|
408
|
+
if (base.aliases !== undefined)
|
|
409
|
+
view.aliases = base.aliases;
|
|
410
|
+
return view;
|
|
411
|
+
}
|
|
412
|
+
function fetchWithTimeout(url, headers) {
|
|
413
|
+
const signal = typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function"
|
|
414
|
+
? AbortSignal.timeout(FETCH_TIMEOUT_MS)
|
|
415
|
+
: undefined;
|
|
416
|
+
return fetch(url, { headers, signal });
|
|
417
|
+
}
|
|
418
|
+
function isDevelopment() {
|
|
419
|
+
try {
|
|
420
|
+
// Written out so bundlers that define process.env.NODE_ENV replace it.
|
|
421
|
+
return process.env.NODE_ENV === "development";
|
|
422
|
+
}
|
|
423
|
+
catch {
|
|
424
|
+
return false;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
//# sourceMappingURL=remoteLoader.js.map
|