@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,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The three read methods: `describe`, `schema.get` and `blocks.list`.
|
|
3
|
+
*/
|
|
4
|
+
import { publicKeyDerFromPem } from "../../ciphertext.js";
|
|
5
|
+
import { limitExceeded, notFound, unavailable } from "../../errors.js";
|
|
6
|
+
import { ASSETS_URL_PREFIX, PROTOCOL_NAME, PROTOCOL_VERSION, SCHEMA_FORMAT, } from "../../types.js";
|
|
7
|
+
import { loadCurrentContent } from "../content.js";
|
|
8
|
+
const DEFAULT_SERVER = { name: "deco-blocks", version: "unknown" };
|
|
9
|
+
/** The largest `.deco/secrets.pub` served (a 16384-bit key's PEM is under 3 KiB). */
|
|
10
|
+
const MAX_PUBLIC_KEY_BYTES = 16 * 1024;
|
|
11
|
+
/**
|
|
12
|
+
* The public key `describe` may serve: a single `PUBLIC KEY` PEM block of at
|
|
13
|
+
* most 16 KiB. Anything else (a private key committed by mistake, two
|
|
14
|
+
* blocks, stray text) is never broadcast to the site editor.
|
|
15
|
+
*/
|
|
16
|
+
function servablePublicKey(text) {
|
|
17
|
+
if (text === null)
|
|
18
|
+
return null;
|
|
19
|
+
if (new TextEncoder().encode(text).byteLength > MAX_PUBLIC_KEY_BYTES)
|
|
20
|
+
return null;
|
|
21
|
+
if (text.includes("PRIVATE KEY"))
|
|
22
|
+
return null;
|
|
23
|
+
return publicKeyDerFromPem(text) === null ? null : text;
|
|
24
|
+
}
|
|
25
|
+
export async function describe(core) {
|
|
26
|
+
const description = await core.description();
|
|
27
|
+
const stored = await core.storage.readSecretsPublicKey();
|
|
28
|
+
const publicKey = servablePublicKey(stored);
|
|
29
|
+
if (stored !== null && publicKey === null) {
|
|
30
|
+
core.options.onError?.(new Error(".deco/secrets.pub isn't a single PUBLIC KEY PEM block; describe reports no key"));
|
|
31
|
+
}
|
|
32
|
+
const readOnly = description.readOnly;
|
|
33
|
+
return {
|
|
34
|
+
protocol: PROTOCOL_NAME,
|
|
35
|
+
version: { major: PROTOCOL_VERSION.major, minor: PROTOCOL_VERSION.minor },
|
|
36
|
+
server: core.options.server ?? DEFAULT_SERVER,
|
|
37
|
+
kind: description.kind,
|
|
38
|
+
readOnly,
|
|
39
|
+
root: description.root,
|
|
40
|
+
schemaFormat: SCHEMA_FORMAT,
|
|
41
|
+
refs: description.refs,
|
|
42
|
+
writes: {
|
|
43
|
+
idempotency: core.storage.getReceipt ? description.idempotency : null,
|
|
44
|
+
schemaPreconditions: true,
|
|
45
|
+
},
|
|
46
|
+
pollIntervalMs: core.pollIntervalMs(description),
|
|
47
|
+
limits: core.limits(description),
|
|
48
|
+
preview: core.options.preview ?? null,
|
|
49
|
+
assets: readOnly || description.assets === null
|
|
50
|
+
? null
|
|
51
|
+
: {
|
|
52
|
+
dir: description.assets.dir,
|
|
53
|
+
urlPrefix: ASSETS_URL_PREFIX,
|
|
54
|
+
maxBytes: description.assets.maxBytes,
|
|
55
|
+
},
|
|
56
|
+
secrets: publicKey === null ? null : { publicKey },
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Reads and parses the schema, enforcing the schema byte limit. */
|
|
60
|
+
export async function readSchema(core, ref, maxSchemaBytes) {
|
|
61
|
+
const stored = await core.storage.readSchema({ ref });
|
|
62
|
+
if (stored === null)
|
|
63
|
+
return null;
|
|
64
|
+
if (new TextEncoder().encode(stored.text).byteLength > maxSchemaBytes) {
|
|
65
|
+
throw limitExceeded(`the schema is over ${maxSchemaBytes} bytes`, { limit: "maxSchemaBytes" });
|
|
66
|
+
}
|
|
67
|
+
return stored;
|
|
68
|
+
}
|
|
69
|
+
/** Parses schema text; a file caught mid-write is reported as Unavailable, never served torn. */
|
|
70
|
+
export function parseSchema(text) {
|
|
71
|
+
let schema;
|
|
72
|
+
try {
|
|
73
|
+
schema = JSON.parse(text);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
throw unavailable("the schema file is being written; retry shortly", 500);
|
|
77
|
+
}
|
|
78
|
+
if (typeof schema !== "object" || schema === null || Array.isArray(schema)) {
|
|
79
|
+
throw unavailable("the schema file doesn't hold a JSON object", 500);
|
|
80
|
+
}
|
|
81
|
+
return schema;
|
|
82
|
+
}
|
|
83
|
+
export async function schemaGet(core, params) {
|
|
84
|
+
const description = await core.description();
|
|
85
|
+
core.checkRef(description, params.ref);
|
|
86
|
+
const limits = core.limits(description);
|
|
87
|
+
const stored = await core.storage.readSchema({ ref: params.ref });
|
|
88
|
+
if (stored === null) {
|
|
89
|
+
throw notFound("no schema: neither .deco/schema.gen.json nor .deco/meta.gen.json exists");
|
|
90
|
+
}
|
|
91
|
+
if (params.ifNoneMatch !== undefined && params.ifNoneMatch === stored.version) {
|
|
92
|
+
return { notModified: true, version: stored.version };
|
|
93
|
+
}
|
|
94
|
+
if (new TextEncoder().encode(stored.text).byteLength > limits.maxSchemaBytes) {
|
|
95
|
+
throw limitExceeded(`the schema is over ${limits.maxSchemaBytes} bytes`, {
|
|
96
|
+
limit: "maxSchemaBytes",
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
notModified: false,
|
|
101
|
+
version: stored.version,
|
|
102
|
+
resolvedRef: stored.resolvedRef,
|
|
103
|
+
schema: parseSchema(stored.text),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export async function blocksList(core, params) {
|
|
107
|
+
const description = await core.description();
|
|
108
|
+
core.checkRef(description, params.ref);
|
|
109
|
+
const limits = core.limits(description);
|
|
110
|
+
const { snapshot, content } = await loadCurrentContent(core.storage, params.ref, { readAll: true, limits, cache: core.cache }, (s) => params.ifNoneMatch !== undefined && params.ifNoneMatch === s.revision);
|
|
111
|
+
if (content === null) {
|
|
112
|
+
return { notModified: true, revision: snapshot.revision, resolvedRef: snapshot.resolvedRef };
|
|
113
|
+
}
|
|
114
|
+
// Null-prototype maps, so an entry named like an Object.prototype key stays an entry.
|
|
115
|
+
const blocks = Object.create(null);
|
|
116
|
+
const versions = Object.create(null);
|
|
117
|
+
for (const [name, entry] of content.entries) {
|
|
118
|
+
if (entry.value === undefined)
|
|
119
|
+
continue;
|
|
120
|
+
blocks[name] = entry.value;
|
|
121
|
+
versions[name] = entry.version;
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
notModified: false,
|
|
125
|
+
revision: snapshot.revision,
|
|
126
|
+
resolvedRef: snapshot.resolvedRef,
|
|
127
|
+
blocks,
|
|
128
|
+
versions,
|
|
129
|
+
diagnostics: content.diagnostics,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=read.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read.js","sourceRoot":"","sources":["../../../../src/protocol/server/methods/read.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACvE,OAAO,EACL,iBAAiB,EAIjB,aAAa,EACb,gBAAgB,EAEhB,aAAa,GAEd,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,kBAAkB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ACtF,MAAM,MAAM,GAA4C,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC5E,MAAM,QAAQ,GAA2B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC7D,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;QAC5C,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS;YAAE,SAAS;QACxC,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC;QAC3B,QAAQ,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC;IACjC,CAAC;IACD,OAAO;QACL,WAAW,EAAE,KAAK;QAClB,QAAQ,EAAE,QAAQ,CAAC,QAAQ;QAC3B,WAAW,EAAE,QAAQ,CAAC,WAAW;QACjC,MAAM;QACN,QAAQ;QACR,WAAW,EAAE,OAAO,CAAC,WAAW;KACjC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Core } from "./core.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Runs a parsed request body (one request object or a batch array) and
|
|
4
|
+
* returns the serialized response body.
|
|
5
|
+
*/
|
|
6
|
+
export declare function dispatch(core: Core, body: unknown, scope: string, maxResponseBytes: number): Promise<string>;
|
|
7
|
+
//# sourceMappingURL=rpc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../../../src/protocol/server/rpc.ts"],"names":[],"mappings":"AAoBA,OAAO,EAAE,KAAK,IAAI,EAAmB,MAAM,WAAW,CAAC;AAuEvD;;;GAGG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,mBAwChG"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The JSON-RPC 2.0 layer: envelope validation, batches and dispatch.
|
|
3
|
+
*
|
|
4
|
+
* - Every request has an `id`; one without is rejected rather than run.
|
|
5
|
+
* - A batch runs in order, returns results in order, holds at most 10 calls,
|
|
6
|
+
* and isn't atomic.
|
|
7
|
+
* - The aggregate response is bounded: once it would pass
|
|
8
|
+
* `maxBatchResponseBytes`, later reads answer LimitExceeded instead.
|
|
9
|
+
* - A write's response is never replaced, alone or in a batch: the write has
|
|
10
|
+
* landed, and answering LimitExceeded would tell the client it hadn't.
|
|
11
|
+
*/
|
|
12
|
+
import { internalError, invalidRequest, limitExceeded, methodNotFound, } from "../errors.js";
|
|
13
|
+
import { validateParams } from "../params.js";
|
|
14
|
+
import { MAX_BATCH_CALLS, METHOD_NAMES } from "../types.js";
|
|
15
|
+
import { toProtocolError } from "./core.js";
|
|
16
|
+
import { blocksApply } from "./methods/apply.js";
|
|
17
|
+
import { blocksList, describe, schemaGet } from "./methods/read.js";
|
|
18
|
+
const utf8 = new TextEncoder();
|
|
19
|
+
const isMethod = (method) => METHOD_NAMES.includes(method);
|
|
20
|
+
/** Validates one request object; returns the error response's id on failure. */
|
|
21
|
+
function parseEnvelope(value) {
|
|
22
|
+
const isObject = typeof value === "object" && value !== null && !Array.isArray(value);
|
|
23
|
+
const raw = (isObject ? value : {});
|
|
24
|
+
const id = typeof raw.id === "string" || typeof raw.id === "number" ? raw.id : null;
|
|
25
|
+
if (!isObject)
|
|
26
|
+
return { error: invalidRequest("a request must be an object"), id: null };
|
|
27
|
+
if (raw.jsonrpc !== "2.0")
|
|
28
|
+
return { error: invalidRequest('jsonrpc must be "2.0"'), id };
|
|
29
|
+
if (id === null) {
|
|
30
|
+
return { error: invalidRequest("every request needs a string or number id"), id: null };
|
|
31
|
+
}
|
|
32
|
+
if (typeof raw.method !== "string")
|
|
33
|
+
return { error: invalidRequest("method must be a string"), id };
|
|
34
|
+
for (const key of Object.keys(raw)) {
|
|
35
|
+
if (!["jsonrpc", "id", "method", "params"].includes(key)) {
|
|
36
|
+
return { error: invalidRequest(`unknown request member "${key}"`), id };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (!isMethod(raw.method))
|
|
40
|
+
return { error: methodNotFound(raw.method), id };
|
|
41
|
+
return { id, method: raw.method, params: raw.params };
|
|
42
|
+
}
|
|
43
|
+
async function run(core, envelope, scope) {
|
|
44
|
+
switch (envelope.method) {
|
|
45
|
+
case "describe":
|
|
46
|
+
validateParams("describe", envelope.params);
|
|
47
|
+
return describe(core);
|
|
48
|
+
case "schema.get":
|
|
49
|
+
return schemaGet(core, validateParams("schema.get", envelope.params));
|
|
50
|
+
case "blocks.list":
|
|
51
|
+
return blocksList(core, validateParams("blocks.list", envelope.params));
|
|
52
|
+
case "blocks.apply":
|
|
53
|
+
return blocksApply(core, validateParams("blocks.apply", envelope.params), scope);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** True for a well-formed `blocks.apply` request. */
|
|
57
|
+
function isWrite(value) {
|
|
58
|
+
const envelope = parseEnvelope(value);
|
|
59
|
+
return !("error" in envelope) && envelope.method === "blocks.apply";
|
|
60
|
+
}
|
|
61
|
+
const errorResponse = (id, error) => JSON.stringify({ jsonrpc: "2.0", id, error: error.toJSON() });
|
|
62
|
+
async function call(core, value, scope) {
|
|
63
|
+
const envelope = parseEnvelope(value);
|
|
64
|
+
if ("error" in envelope)
|
|
65
|
+
return errorResponse(envelope.id, envelope.error);
|
|
66
|
+
try {
|
|
67
|
+
const result = await run(core, envelope, scope);
|
|
68
|
+
return JSON.stringify({ jsonrpc: "2.0", id: envelope.id, result });
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
const known = toProtocolError(error);
|
|
72
|
+
if (known)
|
|
73
|
+
return errorResponse(envelope.id, known);
|
|
74
|
+
core.options.onError?.(error);
|
|
75
|
+
return errorResponse(envelope.id, internalError());
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Runs a parsed request body (one request object or a batch array) and
|
|
80
|
+
* returns the serialized response body.
|
|
81
|
+
*/
|
|
82
|
+
export async function dispatch(core, body, scope, maxResponseBytes) {
|
|
83
|
+
if (!Array.isArray(body)) {
|
|
84
|
+
const response = await call(core, body, scope);
|
|
85
|
+
if (isWrite(body) || utf8.encode(response).byteLength <= maxResponseBytes)
|
|
86
|
+
return response;
|
|
87
|
+
return errorResponse(parseEnvelope(body).id, limitExceeded(`the response is over ${maxResponseBytes} bytes`, {
|
|
88
|
+
limit: "maxBatchResponseBytes",
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
if (body.length === 0)
|
|
92
|
+
return errorResponse(null, invalidRequest("an empty batch"));
|
|
93
|
+
if (body.length > MAX_BATCH_CALLS) {
|
|
94
|
+
return errorResponse(null, limitExceeded(`a batch holds at most ${MAX_BATCH_CALLS} calls`, { limit: "maxBatchCalls" }));
|
|
95
|
+
}
|
|
96
|
+
const responses = [];
|
|
97
|
+
let total = 2; // the brackets
|
|
98
|
+
for (const item of body) {
|
|
99
|
+
const envelope = parseEnvelope(item);
|
|
100
|
+
const id = envelope.id;
|
|
101
|
+
const overBudget = () => errorResponse(id, limitExceeded(`the batch response is over ${maxResponseBytes} bytes`, {
|
|
102
|
+
limit: "maxBatchResponseBytes",
|
|
103
|
+
}));
|
|
104
|
+
// Writes always run, so a batch never drops a save; reads stop once over budget.
|
|
105
|
+
const write = !("error" in envelope) && envelope.method === "blocks.apply";
|
|
106
|
+
let response = !write && total > maxResponseBytes ? overBudget() : await call(core, item, scope);
|
|
107
|
+
const bytes = utf8.encode(response).byteLength + (responses.length ? 1 : 0);
|
|
108
|
+
if (total + bytes > maxResponseBytes && !write)
|
|
109
|
+
response = overBudget();
|
|
110
|
+
total += utf8.encode(response).byteLength + (responses.length ? 1 : 0);
|
|
111
|
+
responses.push(response);
|
|
112
|
+
}
|
|
113
|
+
return `[${responses.join(",")}]`;
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=rpc.js.map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content`, as git computes it for `git hash-object`. */
|
|
2
|
+
export declare function gitBlobHash(content: Uint8Array | string): string;
|
|
3
|
+
/** A revision of a set of files: SHA-256 over the sorted `file\0version` listing. */
|
|
4
|
+
export declare function revisionOf(files: Iterable<{
|
|
5
|
+
file: string;
|
|
6
|
+
version: string;
|
|
7
|
+
}>): string;
|
|
8
|
+
//# sourceMappingURL=hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../../../src/protocol/storage/fs/hash.ts"],"names":[],"mappings":"AAOA,gFAAgF;AAChF,wBAAgB,WAAW,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,GAAG,MAAM,CAGhE;AAED,qFAAqF;AACrF,wBAAgB,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,GAAG,MAAM,CAMrF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content hashes for the filesystem storage. A file's version is its git blob
|
|
3
|
+
* hash (`sha1("blob <size>\0" + bytes)`), the same identifier GitHub uses, so
|
|
4
|
+
* versions read from a working tree match `git hash-object`.
|
|
5
|
+
*/
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
/** The git blob hash of `content`, as git computes it for `git hash-object`. */
|
|
8
|
+
export function gitBlobHash(content) {
|
|
9
|
+
const bytes = typeof content === "string" ? new TextEncoder().encode(content) : content;
|
|
10
|
+
return createHash("sha1").update(`blob ${bytes.byteLength}\0`).update(bytes).digest("hex");
|
|
11
|
+
}
|
|
12
|
+
/** A revision of a set of files: SHA-256 over the sorted `file\0version` listing. */
|
|
13
|
+
export function revisionOf(files) {
|
|
14
|
+
const listing = [...files]
|
|
15
|
+
.map(({ file, version }) => `${file}\0${version}`)
|
|
16
|
+
.sort()
|
|
17
|
+
.join("\n");
|
|
18
|
+
return createHash("sha256").update(listing).digest("hex");
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=hash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../../../src/protocol/storage/fs/hash.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,gFAAgF;AAChF,MAAM,UAAU,WAAW,CAAC,OAA4B;IACtD,MAAM,KAAK,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IACxF,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,QAAQ,KAAK,CAAC,UAAU,IAAI,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC7F,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,UAAU,CAAC,KAAkD;IAC3E,MAAM,OAAO,GAAG,CAAC,GAAG,KAAK,CAAC;SACvB,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,KAAK,OAAO,EAAE,CAAC;SACjD,IAAI,EAAE;SACN,IAAI,CAAC,IAAI,CAAC,CAAC;IACd,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC5D,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type ContentStorage } from "../../storage.ts";
|
|
2
|
+
import type { Limits } from "../../types.ts";
|
|
3
|
+
export interface FsStorageOptions {
|
|
4
|
+
/** The app root: the folder that contains `.deco/`. */
|
|
5
|
+
root: string;
|
|
6
|
+
/**
|
|
7
|
+
* The repository root, which `describe` reports paths relative to. Default:
|
|
8
|
+
* the nearest folder above `root` (or `root` itself) that contains `.git`,
|
|
9
|
+
* else `root`.
|
|
10
|
+
*/
|
|
11
|
+
repoRoot?: string;
|
|
12
|
+
/** Serve the content without accepting writes or uploads. */
|
|
13
|
+
readOnly?: boolean;
|
|
14
|
+
/** The upload folder, relative to `root` (default `public/assets`). */
|
|
15
|
+
assetsDir?: string;
|
|
16
|
+
/** The largest upload accepted, in bytes (default 25 MiB). */
|
|
17
|
+
assetsMaxBytes?: number;
|
|
18
|
+
/** Overrides the poll interval (default 2000 ms). */
|
|
19
|
+
pollIntervalMs?: number;
|
|
20
|
+
/** Lowers the protocol's limits. */
|
|
21
|
+
limits?: Partial<Limits>;
|
|
22
|
+
/** How long a commit waits for another process's lock (default 10 s). */
|
|
23
|
+
lockTimeoutMs?: number;
|
|
24
|
+
}
|
|
25
|
+
export type FsStorage = ContentStorage & {
|
|
26
|
+
/** The absolute app root. */
|
|
27
|
+
readonly root: string;
|
|
28
|
+
};
|
|
29
|
+
export declare function createFsStorage(options: FsStorageOptions): FsStorage;
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/protocol/storage/fs/index.ts"],"names":[],"mappings":"AAyBA,OAAO,EAEL,KAAK,cAAc,EASpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAK7C,MAAM,WAAW,gBAAgB;IAC/B,uDAAuD;IACvD,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6DAA6D;IAC7D,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,uEAAuE;IACvE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,8DAA8D;IAC9D,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,qDAAqD;IACrD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,oCAAoC;IACpC,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB,yEAAyE;IACzE,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,MAAM,SAAS,GAAG,cAAc,GAAG;IACvC,6BAA6B;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB,CAAC;AA8DF,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CA+MpE"}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@decocms/blocks/protocol/storage/fs`: the filesystem `ContentStorage`
|
|
3
|
+
* (Node only), which `deco serve` serves a working tree with.
|
|
4
|
+
*
|
|
5
|
+
* Inside the app root (the folder that contains `.deco/`):
|
|
6
|
+
* - `.deco/blocks/*.json` — the saved blocks; the only thing it writes.
|
|
7
|
+
* - `.deco/schema.gen.json`, falling back to `.deco/meta.gen.json` — read only.
|
|
8
|
+
* - `.deco/secrets.pub` — read only, reported by `describe`.
|
|
9
|
+
* - `.deco/index.ts` is code and is never written.
|
|
10
|
+
* - Uploads go to `public/assets` (or `assetsDir`), never overwriting a file.
|
|
11
|
+
*
|
|
12
|
+
* A file's version is its git blob hash, as `git hash-object` prints it.
|
|
13
|
+
* Commits are serialized per `.deco` folder (in process and across processes)
|
|
14
|
+
* and applied with staged files and atomic renames, rolled back on failure,
|
|
15
|
+
* with the folder fsynced before a commit returns.
|
|
16
|
+
*
|
|
17
|
+
* While committing it uses `.deco/.blocks.lock` and `.deco/.tx-*` folders;
|
|
18
|
+
* add both to `.gitignore`. A crashed process can leave them behind: a lock
|
|
19
|
+
* older than 30 s is taken over, and leftover `.tx-*` folders are removed by
|
|
20
|
+
* the next commit.
|
|
21
|
+
*/
|
|
22
|
+
import { lstat, mkdir, open, readdir, readFile, stat } from "node:fs/promises";
|
|
23
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
24
|
+
import { suffixedAssetName } from "../../assets.js";
|
|
25
|
+
import { isBlockFileName } from "../../keys.js";
|
|
26
|
+
import { StorageInvalidFileError, StorageNotFoundError, StorageUnavailableError, } from "../../storage.js";
|
|
27
|
+
import { gitBlobHash, revisionOf } from "./hash.js";
|
|
28
|
+
import { withCommitLock } from "./lock.js";
|
|
29
|
+
import { applyFileChange, sweepStaleTransactions } from "./transaction.js";
|
|
30
|
+
const DEFAULT_ASSETS_DIR = "public/assets";
|
|
31
|
+
const DEFAULT_ASSETS_MAX_BYTES = 25 * 1024 * 1024;
|
|
32
|
+
const toPosix = (path) => path.split(sep).join("/");
|
|
33
|
+
const isMissing = (error) => {
|
|
34
|
+
const code = error?.code;
|
|
35
|
+
return code === "ENOENT" || code === "ENOTDIR";
|
|
36
|
+
};
|
|
37
|
+
async function exists(path) {
|
|
38
|
+
try {
|
|
39
|
+
await lstat(path);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (isMissing(error))
|
|
44
|
+
return false;
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** The nearest folder at or above `start` that contains `.git`, else `start`. */
|
|
49
|
+
async function findRepoRoot(start) {
|
|
50
|
+
for (let dir = start;; dir = dirname(dir)) {
|
|
51
|
+
if (await exists(join(dir, ".git")))
|
|
52
|
+
return dir;
|
|
53
|
+
if (dirname(dir) === dir)
|
|
54
|
+
return start;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async function readTextOrNull(path) {
|
|
58
|
+
try {
|
|
59
|
+
return await readFile(path, "utf8");
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (isMissing(error))
|
|
63
|
+
return null;
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function readBytesOrNull(path) {
|
|
68
|
+
try {
|
|
69
|
+
return await readFile(path);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (isMissing(error))
|
|
73
|
+
return null;
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function assertBlockFile(file) {
|
|
78
|
+
if (!isBlockFileName(file))
|
|
79
|
+
throw new StorageInvalidFileError(file);
|
|
80
|
+
}
|
|
81
|
+
function wrapIoError(error) {
|
|
82
|
+
if (error instanceof StorageUnavailableError ||
|
|
83
|
+
error instanceof StorageNotFoundError ||
|
|
84
|
+
error instanceof StorageInvalidFileError)
|
|
85
|
+
throw error;
|
|
86
|
+
throw new StorageUnavailableError(`filesystem error: ${error.message}`);
|
|
87
|
+
}
|
|
88
|
+
export function createFsStorage(options) {
|
|
89
|
+
const root = resolve(options.root);
|
|
90
|
+
const decoDir = join(root, ".deco");
|
|
91
|
+
const blocksDir = join(decoDir, "blocks");
|
|
92
|
+
const assetsDir = resolve(root, options.assetsDir ?? DEFAULT_ASSETS_DIR);
|
|
93
|
+
const readOnly = options.readOnly ?? false;
|
|
94
|
+
const lockOptions = { timeoutMs: options.lockTimeoutMs ?? 10_000, staleMs: 30_000 };
|
|
95
|
+
let repoRoot;
|
|
96
|
+
let swept = false;
|
|
97
|
+
/** file -> stat fingerprint and its hash, so a poll only rehashes changed files. */
|
|
98
|
+
const hashCache = new Map();
|
|
99
|
+
async function hashFile(file) {
|
|
100
|
+
const path = join(blocksDir, file);
|
|
101
|
+
let info;
|
|
102
|
+
try {
|
|
103
|
+
info = await stat(path, { bigint: true });
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (isMissing(error))
|
|
107
|
+
return null;
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
if (!info.isFile())
|
|
111
|
+
return null;
|
|
112
|
+
const fingerprint = `${info.ino}:${info.size}:${info.mtimeNs}:${info.ctimeNs}`;
|
|
113
|
+
const cached = hashCache.get(file);
|
|
114
|
+
if (cached?.fingerprint === fingerprint) {
|
|
115
|
+
return { file, version: cached.version, size: Number(info.size) };
|
|
116
|
+
}
|
|
117
|
+
let bytes;
|
|
118
|
+
try {
|
|
119
|
+
bytes = await readFile(path);
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (isMissing(error))
|
|
123
|
+
return null;
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
const version = gitBlobHash(bytes);
|
|
127
|
+
hashCache.set(file, { fingerprint, version });
|
|
128
|
+
return { file, version, size: bytes.byteLength };
|
|
129
|
+
}
|
|
130
|
+
/** The current version of a file, read fresh (no cache), or `null` when absent. */
|
|
131
|
+
async function freshVersion(file) {
|
|
132
|
+
try {
|
|
133
|
+
return gitBlobHash(await readFile(join(blocksDir, file)));
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (isMissing(error))
|
|
137
|
+
return null;
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async function readSchema() {
|
|
142
|
+
for (const name of ["schema.gen.json", "meta.gen.json"]) {
|
|
143
|
+
const bytes = await readBytesOrNull(join(decoDir, name));
|
|
144
|
+
if (bytes !== null) {
|
|
145
|
+
return {
|
|
146
|
+
version: gitBlobHash(bytes),
|
|
147
|
+
text: new TextDecoder().decode(bytes),
|
|
148
|
+
resolvedRef: null,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
async function snapshot() {
|
|
155
|
+
if (!(await exists(decoDir))) {
|
|
156
|
+
throw new StorageNotFoundError(`no .deco folder in ${root}`);
|
|
157
|
+
}
|
|
158
|
+
let names;
|
|
159
|
+
try {
|
|
160
|
+
names = (await readdir(blocksDir, { withFileTypes: true }))
|
|
161
|
+
.filter((entry) => entry.isFile() && isBlockFileName(entry.name))
|
|
162
|
+
.map((entry) => entry.name);
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
if (isMissing(error))
|
|
166
|
+
names = [];
|
|
167
|
+
else
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
const files = (await Promise.all(names.map(hashFile))).filter((f) => f !== null);
|
|
171
|
+
files.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : 0));
|
|
172
|
+
const present = new Set(names);
|
|
173
|
+
for (const cached of hashCache.keys())
|
|
174
|
+
if (!present.has(cached))
|
|
175
|
+
hashCache.delete(cached);
|
|
176
|
+
return { revision: revisionOf(files), resolvedRef: null, files };
|
|
177
|
+
}
|
|
178
|
+
const storage = {
|
|
179
|
+
root,
|
|
180
|
+
async describe() {
|
|
181
|
+
repoRoot ??= options.repoRoot
|
|
182
|
+
? Promise.resolve(resolve(options.repoRoot))
|
|
183
|
+
: findRepoRoot(root);
|
|
184
|
+
const base = await repoRoot;
|
|
185
|
+
const relativeRoot = toPosix(relative(base, root)) || ".";
|
|
186
|
+
return {
|
|
187
|
+
kind: "working-tree",
|
|
188
|
+
root: relativeRoot,
|
|
189
|
+
readOnly,
|
|
190
|
+
refs: null,
|
|
191
|
+
assets: readOnly
|
|
192
|
+
? null
|
|
193
|
+
: {
|
|
194
|
+
dir: toPosix(relative(base, assetsDir)) || ".",
|
|
195
|
+
maxBytes: options.assetsMaxBytes ?? DEFAULT_ASSETS_MAX_BYTES,
|
|
196
|
+
},
|
|
197
|
+
idempotency: null,
|
|
198
|
+
pollIntervalMs: options.pollIntervalMs ?? 2000,
|
|
199
|
+
limits: options.limits,
|
|
200
|
+
};
|
|
201
|
+
},
|
|
202
|
+
async snapshot() {
|
|
203
|
+
try {
|
|
204
|
+
return await snapshot();
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
wrapIoError(error);
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
async readFiles(_snapshot, files) {
|
|
211
|
+
// A working tree has no past snapshots: read current bytes and report
|
|
212
|
+
// their version, so the core notices a file that changed meanwhile.
|
|
213
|
+
const out = Object.create(null);
|
|
214
|
+
await Promise.all(files.map(async (file) => {
|
|
215
|
+
assertBlockFile(file);
|
|
216
|
+
const bytes = await readBytesOrNull(join(blocksDir, file));
|
|
217
|
+
if (bytes !== null) {
|
|
218
|
+
out[file] = { text: new TextDecoder().decode(bytes), version: gitBlobHash(bytes) };
|
|
219
|
+
}
|
|
220
|
+
}));
|
|
221
|
+
return out;
|
|
222
|
+
},
|
|
223
|
+
readSchema,
|
|
224
|
+
readSecretsPublicKey: () => readTextOrNull(join(decoDir, "secrets.pub")),
|
|
225
|
+
async commit(attempt) {
|
|
226
|
+
if (readOnly)
|
|
227
|
+
throw new StorageUnavailableError("the storage is read-only");
|
|
228
|
+
if (attempt.receipt)
|
|
229
|
+
throw new StorageUnavailableError("the filesystem storage keeps no receipts");
|
|
230
|
+
for (const file of [
|
|
231
|
+
...Object.keys(attempt.put),
|
|
232
|
+
...attempt.delete,
|
|
233
|
+
...Object.keys(attempt.expected),
|
|
234
|
+
]) {
|
|
235
|
+
assertBlockFile(file);
|
|
236
|
+
}
|
|
237
|
+
if (!(await exists(decoDir)))
|
|
238
|
+
throw new StorageNotFoundError(`no .deco folder in ${root}`);
|
|
239
|
+
try {
|
|
240
|
+
return await withCommitLock(decoDir, lockOptions, async () => {
|
|
241
|
+
// Under the lock, any transaction folder left is from a crashed commit.
|
|
242
|
+
if (!swept) {
|
|
243
|
+
await sweepStaleTransactions(decoDir);
|
|
244
|
+
swept = true;
|
|
245
|
+
}
|
|
246
|
+
if (attempt.expectedSchemaVersion !== undefined) {
|
|
247
|
+
const schema = await readSchema();
|
|
248
|
+
if ((schema?.version ?? null) !== attempt.expectedSchemaVersion) {
|
|
249
|
+
return { status: "stale" };
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
for (const [file, expected] of Object.entries(attempt.expected)) {
|
|
253
|
+
if ((await freshVersion(file)) !== expected)
|
|
254
|
+
return { status: "stale" };
|
|
255
|
+
}
|
|
256
|
+
await applyFileChange(blocksDir, decoDir, { put: attempt.put, delete: attempt.delete });
|
|
257
|
+
const versions = {};
|
|
258
|
+
for (const [file, content] of Object.entries(attempt.put))
|
|
259
|
+
versions[file] = gitBlobHash(content);
|
|
260
|
+
const after = await snapshot();
|
|
261
|
+
return { status: "committed", revision: after.revision, versions };
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
wrapIoError(error);
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
async putAsset(name, body) {
|
|
269
|
+
if (readOnly)
|
|
270
|
+
throw new StorageUnavailableError("the storage is read-only");
|
|
271
|
+
if (!name || name.includes("/") || name.includes("\\") || name.startsWith(".")) {
|
|
272
|
+
throw new Error(`not an asset file name: ${JSON.stringify(name)}`);
|
|
273
|
+
}
|
|
274
|
+
await mkdir(assetsDir, { recursive: true });
|
|
275
|
+
let candidate = name;
|
|
276
|
+
for (let attempt = 0;; attempt++) {
|
|
277
|
+
const path = join(assetsDir, candidate);
|
|
278
|
+
if (!isAbsolute(path) || dirname(path) !== assetsDir)
|
|
279
|
+
throw new Error("invalid asset path");
|
|
280
|
+
try {
|
|
281
|
+
const handle = await open(path, "wx");
|
|
282
|
+
try {
|
|
283
|
+
await handle.writeFile(body);
|
|
284
|
+
await handle.sync();
|
|
285
|
+
}
|
|
286
|
+
finally {
|
|
287
|
+
await handle.close();
|
|
288
|
+
}
|
|
289
|
+
return { name: candidate };
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
if (error.code !== "EEXIST" || attempt > 20)
|
|
293
|
+
throw error;
|
|
294
|
+
candidate = suffixedAssetName(name);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
return storage;
|
|
300
|
+
}
|
|
301
|
+
//# sourceMappingURL=index.js.map
|