@decocms/blocks 8.1.0-next.3 → 8.1.0-next.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/protocol/client.d.ts +1 -1
- package/dist/protocol/conformance/cases/reads.d.ts.map +1 -1
- package/dist/protocol/conformance/cases/reads.js +22 -6
- package/dist/protocol/conformance/cases/reads.js.map +1 -1
- package/dist/protocol/conformance/cases/writes.d.ts.map +1 -1
- package/dist/protocol/conformance/cases/writes.js +1 -0
- package/dist/protocol/conformance/cases/writes.js.map +1 -1
- package/dist/protocol/conformance/contentHashFixtures.d.ts +0 -29
- package/dist/protocol/conformance/contentHashFixtures.d.ts.map +1 -1
- package/dist/protocol/conformance/contentHashFixtures.js +0 -30
- package/dist/protocol/conformance/contentHashFixtures.js.map +1 -1
- package/dist/protocol/conformance/context.d.ts +3 -3
- package/dist/protocol/conformance/context.d.ts.map +1 -1
- package/dist/protocol/conformance/index.d.ts +1 -1
- package/dist/protocol/conformance/index.d.ts.map +1 -1
- package/dist/protocol/conformance/index.js +2 -2
- package/dist/protocol/conformance/index.js.map +1 -1
- package/dist/protocol/index.d.ts +3 -5
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +3 -5
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/secrets.d.ts.map +1 -1
- package/dist/protocol/secrets.js +9 -2
- package/dist/protocol/secrets.js.map +1 -1
- package/dist/protocol/server/assets.d.ts +1 -1
- package/dist/protocol/server/assets.js +1 -1
- package/dist/protocol/server/methods/read.d.ts.map +1 -1
- package/dist/protocol/server/methods/read.js +5 -2
- package/dist/protocol/server/methods/read.js.map +1 -1
- package/dist/protocol/types.d.ts +23 -0
- package/dist/protocol/types.d.ts.map +1 -1
- package/dist/protocol/types.js +10 -0
- package/dist/protocol/types.js.map +1 -1
- package/dist/v8/boundedJson.d.ts +11 -0
- package/dist/v8/boundedJson.d.ts.map +1 -0
- package/dist/v8/boundedJson.js +44 -0
- package/dist/v8/boundedJson.js.map +1 -0
- package/dist/v8/builtins/data.d.ts +21 -5
- package/dist/v8/builtins/data.d.ts.map +1 -1
- package/dist/v8/builtins/data.js +47 -6
- package/dist/v8/builtins/data.js.map +1 -1
- package/dist/v8/builtins/index.d.ts.map +1 -1
- package/dist/v8/builtins/index.js +2 -3
- package/dist/v8/builtins/index.js.map +1 -1
- package/dist/v8/canonical.d.ts +0 -28
- package/dist/v8/canonical.d.ts.map +1 -1
- package/dist/v8/canonical.js +0 -19
- package/dist/v8/canonical.js.map +1 -1
- package/dist/v8/cli/check/index.d.ts +5 -0
- package/dist/v8/cli/check/index.d.ts.map +1 -1
- package/dist/v8/cli/check/index.js +34 -2
- package/dist/v8/cli/check/index.js.map +1 -1
- package/dist/v8/cli/index.d.ts +2 -1
- package/dist/v8/cli/index.d.ts.map +1 -1
- package/dist/v8/cli/index.js +2 -1
- package/dist/v8/cli/index.js.map +1 -1
- package/dist/v8/cli/run.d.ts.map +1 -1
- package/dist/v8/cli/run.js +3 -10
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.js +48 -28
- package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
- package/dist/v8/cli/schema/generate.d.ts +3 -1
- package/dist/v8/cli/schema/generate.d.ts.map +1 -1
- package/dist/v8/cli/schema/generate.js +2 -1
- package/dist/v8/cli/schema/generate.js.map +1 -1
- package/dist/v8/cli/schema/tsProgram.d.ts +10 -0
- package/dist/v8/cli/schema/tsProgram.d.ts.map +1 -1
- package/dist/v8/cli/schema/tsProgram.js +31 -0
- package/dist/v8/cli/schema/tsProgram.js.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.d.ts.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.js +15 -1
- package/dist/v8/cli/schema/typeToSchema.js.map +1 -1
- package/dist/v8/cli/serve/server.d.ts +1 -5
- package/dist/v8/cli/serve/server.d.ts.map +1 -1
- package/dist/v8/cli/serve/server.js +76 -62
- package/dist/v8/cli/serve/server.js.map +1 -1
- package/dist/v8/cms.d.ts +5 -2
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +187 -25
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +22 -6
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +78 -40
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/draft.d.ts +9 -12
- package/dist/v8/draft.d.ts.map +1 -1
- package/dist/v8/draft.js +30 -4
- package/dist/v8/draft.js.map +1 -1
- package/dist/v8/draftChanges.d.ts +26 -0
- package/dist/v8/draftChanges.d.ts.map +1 -0
- package/dist/v8/draftChanges.js +102 -0
- package/dist/v8/draftChanges.js.map +1 -0
- package/dist/v8/hosts.d.ts +55 -0
- package/dist/v8/hosts.d.ts.map +1 -0
- package/dist/v8/hosts.js +197 -0
- package/dist/v8/hosts.js.map +1 -0
- package/dist/v8/index.d.ts +4 -3
- package/dist/v8/index.d.ts.map +1 -1
- package/dist/v8/index.js +3 -2
- package/dist/v8/index.js.map +1 -1
- package/dist/v8/remoteLoader.d.ts +1 -1
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +32 -257
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/settings.d.ts +34 -0
- package/dist/v8/settings.d.ts.map +1 -0
- package/dist/v8/settings.js +143 -0
- package/dist/v8/settings.js.map +1 -0
- package/dist/v8/telemetry.d.ts +13 -3
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +31 -32
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +74 -9
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +3 -3
- package/src/protocol/client.ts +1 -1
- package/src/protocol/conformance/cases/reads.ts +24 -6
- package/src/protocol/conformance/cases/writes.ts +1 -0
- package/src/protocol/conformance/contentHashFixtures.ts +0 -57
- package/src/protocol/conformance/context.ts +3 -3
- package/src/protocol/conformance/index.ts +2 -7
- package/src/protocol/index.ts +2 -8
- package/src/protocol/secrets.ts +10 -2
- package/src/protocol/server/assets.ts +1 -1
- package/src/protocol/server/methods/read.ts +5 -2
- package/src/protocol/types.ts +21 -1
- package/src/v8/boundedJson.ts +47 -0
- package/src/v8/builtins/data.ts +56 -7
- package/src/v8/builtins/index.ts +2 -3
- package/src/v8/canonical.ts +0 -36
- package/src/v8/cli/check/index.ts +48 -2
- package/src/v8/cli/index.ts +2 -1
- package/src/v8/cli/run.ts +4 -9
- package/src/v8/cli/schema/builtinSchemas.ts +49 -28
- package/src/v8/cli/schema/generate.ts +4 -1
- package/src/v8/cli/schema/tsProgram.ts +27 -0
- package/src/v8/cli/schema/typeToSchema.ts +18 -1
- package/src/v8/cli/serve/server.ts +78 -77
- package/src/v8/cms.ts +234 -30
- package/src/v8/content.ts +87 -43
- package/src/v8/draft.ts +31 -9
- package/src/v8/draftChanges.ts +117 -0
- package/src/v8/hosts.ts +184 -0
- package/src/v8/index.ts +6 -8
- package/src/v8/remoteLoader.ts +39 -290
- package/src/v8/settings.ts +178 -0
- package/src/v8/telemetry.ts +43 -40
- package/src/v8/types.ts +68 -9
package/src/protocol/index.ts
CHANGED
|
@@ -14,10 +14,8 @@
|
|
|
14
14
|
*
|
|
15
15
|
* The SDK's runtime never imports the protocol, so it never reaches an app
|
|
16
16
|
* bundle. The canonical hash it re-exports is the SDK's own dependency-free
|
|
17
|
-
* leaf module (see ./canonical.ts)
|
|
18
|
-
* (
|
|
19
|
-
* preview grants and the draft lifecycle are outside the protocol: overlays
|
|
20
|
-
* are delivery assets (see /next/content-delivery#exact-draft-previews).
|
|
17
|
+
* leaf module (see ./canonical.ts). Draft previews and the draft lifecycle
|
|
18
|
+
* are outside the protocol (see /next/content-delivery#draft-previews).
|
|
21
19
|
*/
|
|
22
20
|
export {
|
|
23
21
|
APPLY_DIGEST_DOMAIN,
|
|
@@ -25,11 +23,7 @@ export {
|
|
|
25
23
|
CanonicalJsonError,
|
|
26
24
|
CONTENT_HASH_FORMAT,
|
|
27
25
|
canonicalJson,
|
|
28
|
-
computeBlockHash,
|
|
29
26
|
computeContentRevision,
|
|
30
|
-
computeOverlayVersion,
|
|
31
|
-
DRAFT_OVERLAY_FORMAT,
|
|
32
|
-
type DraftOverlay,
|
|
33
27
|
sha256Hex,
|
|
34
28
|
} from "./canonical.ts";
|
|
35
29
|
export {
|
package/src/protocol/secrets.ts
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
* entry must carry a well-formed `ciphertext`, whatever its field.
|
|
10
10
|
*
|
|
11
11
|
* The schema marks a `Secret` field with `"format": "secret"` — the contract
|
|
12
|
-
* `deco schema` emits for the `Secret` type.
|
|
12
|
+
* `deco schema` emits for the `Secret` type. A legacy secret loader block
|
|
13
|
+
* (`website/loaders/secret.ts`) is left alone. Browser-safe.
|
|
13
14
|
*/
|
|
14
15
|
import { parseCiphertext } from "./ciphertext.ts";
|
|
15
16
|
import type { BlockViolation } from "./errors.ts";
|
|
@@ -23,6 +24,12 @@ export const SECRET_FORMAT = "secret";
|
|
|
23
24
|
|
|
24
25
|
const MULTIVARIATE_TYPES = new Set(["multivariate", "website/flags/multivariate.ts"]);
|
|
25
26
|
const LAZY_TYPE = "lazy";
|
|
27
|
+
/**
|
|
28
|
+
* The legacy (v7) secret loader. Its `encrypted` prop is a string marked
|
|
29
|
+
* `"format": "secret"` that already holds the site's own ciphertext, so it
|
|
30
|
+
* isn't a `Secret` field; its blocks are migrated, not guarded.
|
|
31
|
+
*/
|
|
32
|
+
const LEGACY_SECRET_LOADER = /(^|\/)loaders\/secret\.ts$/;
|
|
26
33
|
const MAX_DEPTH = 512;
|
|
27
34
|
|
|
28
35
|
type Json = Record<string, unknown>;
|
|
@@ -180,7 +187,8 @@ class SecretWalker {
|
|
|
180
187
|
'a "secret" block must carry a well-formed "ciphertext" (v1.<wrappedKey>.<iv>.<ciphertext>)',
|
|
181
188
|
);
|
|
182
189
|
}
|
|
183
|
-
const definition =
|
|
190
|
+
const definition =
|
|
191
|
+
this.meta && !LEGACY_SECRET_LOADER.test(type) ? blockIndex(this.meta).get(type) : undefined;
|
|
184
192
|
this.walkObject(block, definition, pointer, depth);
|
|
185
193
|
}
|
|
186
194
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* `createAssetHandler(storage)`: uploads beside the content protocol.
|
|
3
3
|
*
|
|
4
4
|
* Uploads aren't one of the four methods. The site editor sends each file to
|
|
5
|
-
* `PUT /assets/<name>` with the same
|
|
5
|
+
* `PUT /assets/<name>` with the same auth as the protocol; on that path the
|
|
6
6
|
* file's own image, video, font or PDF content type is accepted instead of
|
|
7
7
|
* JSON, and the name's extension must match it (`banner.html` sent as
|
|
8
8
|
* `image/png` is refused; a name without an extension gets the type's). SVG
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The three read methods: `describe`, `schema.get` and `blocks.list`.
|
|
3
3
|
*/
|
|
4
4
|
import { publicKeyDerFromPem } from "../../ciphertext.ts";
|
|
5
|
-
import { limitExceeded,
|
|
5
|
+
import { limitExceeded, unavailable } from "../../errors.ts";
|
|
6
6
|
import {
|
|
7
7
|
ASSETS_URL_PREFIX,
|
|
8
8
|
type BlocksListResult,
|
|
@@ -106,7 +106,10 @@ export async function schemaGet(core: Core, params: ReadParams): Promise<SchemaG
|
|
|
106
106
|
const limits = core.limits(description);
|
|
107
107
|
const stored = await core.storage.readSchema({ ref: params.ref });
|
|
108
108
|
if (stored === null) {
|
|
109
|
-
|
|
109
|
+
// No schema yet is a state, not an error. The snapshot still refuses a
|
|
110
|
+
// site with no .deco folder (NotFound) and reports the resolved branch.
|
|
111
|
+
const snapshot = await core.storage.snapshot({ ref: params.ref });
|
|
112
|
+
return { notModified: false, version: null, resolvedRef: snapshot.resolvedRef, schema: null };
|
|
110
113
|
}
|
|
111
114
|
if (params.ifNoneMatch !== undefined && params.ifNoneMatch === stored.version) {
|
|
112
115
|
return { notModified: true, version: stored.version };
|
package/src/protocol/types.ts
CHANGED
|
@@ -10,6 +10,17 @@ export const PROTOCOL_VERSION = { major: 1, minor: 0 } as const;
|
|
|
10
10
|
|
|
11
11
|
export const SCHEMA_FORMAT = "deco-meta@1";
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* The Blocks major a generated schema declares, as its top-level
|
|
15
|
+
* `blocksMajor`. `deco schema` writes it and `deco check` requires it. It is
|
|
16
|
+
* the package's major version, not the full version string, and it is the
|
|
17
|
+
* only signal a host such as the site editor should read to tell a v8 site
|
|
18
|
+
* from a v7 one: the schema's file name (`schema.gen.json` or
|
|
19
|
+
* `meta.gen.json`) doesn't tell. A schema without it, or with another value,
|
|
20
|
+
* is not a v8 schema.
|
|
21
|
+
*/
|
|
22
|
+
export const BLOCKS_MAJOR = 8;
|
|
23
|
+
|
|
13
24
|
/** The path the local server (`deco serve`) mounts the endpoint at. */
|
|
14
25
|
export const RPC_PATH = "/rpc";
|
|
15
26
|
|
|
@@ -89,9 +100,18 @@ export interface ReadParams {
|
|
|
89
100
|
|
|
90
101
|
export type SchemaGetParams = ReadParams;
|
|
91
102
|
|
|
103
|
+
/**
|
|
104
|
+
* `schema: null` (with `version: null`) means the site has no schema yet:
|
|
105
|
+
* neither `.deco/schema.gen.json` nor `.deco/meta.gen.json` exists. It's a
|
|
106
|
+
* normal state, not an error: `blocks.list` and `blocks.apply` work without a
|
|
107
|
+
* schema, so a client still lists and edits the blocks (without typed forms)
|
|
108
|
+
* and keeps polling until a schema appears. There's no version to send as
|
|
109
|
+
* `ifNoneMatch`, so the poll reads it unconditionally; the answer is tiny.
|
|
110
|
+
*/
|
|
92
111
|
export type SchemaGetResult =
|
|
93
112
|
| { notModified: true; version: string }
|
|
94
|
-
| { notModified: false; version: string; resolvedRef: string | null; schema: DecoMeta }
|
|
113
|
+
| { notModified: false; version: string; resolvedRef: string | null; schema: DecoMeta }
|
|
114
|
+
| { notModified: false; version: null; resolvedRef: string | null; schema: null };
|
|
95
115
|
|
|
96
116
|
export type BlocksListParams = ReadParams;
|
|
97
117
|
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network reads the SDK makes on its own (hosted releases, draft changes):
|
|
3
|
+
* a timeout on the whole request, and a JSON body refused as soon as it
|
|
4
|
+
* would exceed a size, before it's buffered whole (a Worker isolate has
|
|
5
|
+
* 128 MB).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** An abort signal that fires after `ms`, where the runtime has `AbortSignal.timeout`. */
|
|
9
|
+
export function timeoutSignal(ms: number): AbortSignal | undefined {
|
|
10
|
+
return typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function"
|
|
11
|
+
? AbortSignal.timeout(ms)
|
|
12
|
+
: undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Parses a JSON body, refusing it once it passes `maxBytes`. */
|
|
16
|
+
export async function readBoundedJson(
|
|
17
|
+
response: Response,
|
|
18
|
+
label: string,
|
|
19
|
+
maxBytes: number,
|
|
20
|
+
): Promise<unknown> {
|
|
21
|
+
const tooLarge = () => new Error(`${label}: larger than ${maxBytes} bytes`);
|
|
22
|
+
if (Number(response.headers.get("content-length")) > maxBytes) {
|
|
23
|
+
await response.body?.cancel();
|
|
24
|
+
throw tooLarge();
|
|
25
|
+
}
|
|
26
|
+
if (response.body === null) {
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
if (text.length > maxBytes) throw tooLarge();
|
|
29
|
+
return JSON.parse(text);
|
|
30
|
+
}
|
|
31
|
+
const reader = response.body.getReader();
|
|
32
|
+
const decoder = new TextDecoder();
|
|
33
|
+
const parts: string[] = [];
|
|
34
|
+
let size = 0;
|
|
35
|
+
for (;;) {
|
|
36
|
+
const { done, value } = await reader.read();
|
|
37
|
+
if (done) break;
|
|
38
|
+
size += value.byteLength;
|
|
39
|
+
if (size > maxBytes) {
|
|
40
|
+
await reader.cancel();
|
|
41
|
+
throw tooLarge();
|
|
42
|
+
}
|
|
43
|
+
parts.push(decoder.decode(value, { stream: true }));
|
|
44
|
+
}
|
|
45
|
+
parts.push(decoder.decode());
|
|
46
|
+
return JSON.parse(parts.join(""));
|
|
47
|
+
}
|
package/src/v8/builtins/data.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Analytics, Redirect, Telemetry } from "../types.ts";
|
|
1
|
+
import type { Analytics, CMSSettings, Redirect, Telemetry } from "../types.ts";
|
|
2
2
|
|
|
3
3
|
/** The hosted Deco CMS collector: where page views go when `collector` is left out. */
|
|
4
4
|
export const HOSTED_ANALYTICS_COLLECTOR = "https://d.lilstts.com/events";
|
|
@@ -12,12 +12,61 @@ export const page = <P>(props: P): P => props;
|
|
|
12
12
|
/** `redirect`: returns its arguments as saved. */
|
|
13
13
|
export const redirect = (props: Redirect): Redirect => props;
|
|
14
14
|
|
|
15
|
-
/** `telemetry
|
|
16
|
-
export const
|
|
15
|
+
/** The `telemetry` section's defaults (sample rates before code's caps). */
|
|
16
|
+
export const TELEMETRY_DEFAULTS: Readonly<Required<Telemetry>> = Object.freeze({
|
|
17
|
+
enabled: true,
|
|
18
|
+
metrics: true,
|
|
19
|
+
errorSampleRate: 0.05,
|
|
20
|
+
traceSampleRate: 0,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/** The well-known saved block that holds the CMS settings, and its built-in type. */
|
|
24
|
+
export const SETTINGS_BLOCK = "CMS";
|
|
25
|
+
export const SETTINGS_TYPE = "cms-settings";
|
|
26
|
+
|
|
27
|
+
/** The `telemetry` section with its defaults filled in; rates as saved, before any cap. */
|
|
28
|
+
export function telemetrySection(section: unknown): Required<Telemetry> {
|
|
29
|
+
const t = isRecord(section) ? section : {};
|
|
30
|
+
return {
|
|
31
|
+
enabled: typeof t.enabled === "boolean" ? t.enabled : TELEMETRY_DEFAULTS.enabled,
|
|
32
|
+
metrics: typeof t.metrics === "boolean" ? t.metrics : TELEMETRY_DEFAULTS.metrics,
|
|
33
|
+
errorSampleRate: rate(t.errorSampleRate, TELEMETRY_DEFAULTS.errorSampleRate),
|
|
34
|
+
traceSampleRate: rate(t.traceSampleRate, TELEMETRY_DEFAULTS.traceSampleRate),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The `analytics` section with its defaults filled in. */
|
|
39
|
+
export function analyticsSection(section: unknown): Required<Analytics> {
|
|
40
|
+
const a = isRecord(section) ? section : {};
|
|
41
|
+
return {
|
|
42
|
+
collector:
|
|
43
|
+
typeof a.collector === "string" && a.collector !== ""
|
|
44
|
+
? a.collector
|
|
45
|
+
: HOSTED_ANALYTICS_COLLECTOR,
|
|
46
|
+
enabled: typeof a.enabled === "boolean" ? a.enabled : true,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
17
49
|
|
|
18
|
-
/**
|
|
19
|
-
|
|
50
|
+
/**
|
|
51
|
+
* `cms-settings`: the type of the well-known saved block `CMS`. Returns its
|
|
52
|
+
* input with the telemetry and analytics defaults filled in; `preview` stays
|
|
53
|
+
* as saved, since its default depends on code (`cms.settings()` applies it).
|
|
54
|
+
*/
|
|
55
|
+
export const cmsSettings = (
|
|
56
|
+
props: CMSSettings = {},
|
|
57
|
+
): CMSSettings & { telemetry: Required<Telemetry>; analytics: Required<Analytics> } => ({
|
|
20
58
|
...props,
|
|
21
|
-
|
|
22
|
-
|
|
59
|
+
telemetry: telemetrySection(props.telemetry),
|
|
60
|
+
analytics: analyticsSection(props.analytics),
|
|
23
61
|
});
|
|
62
|
+
|
|
63
|
+
/** A sample rate between 0 and 1, or `fallback` when it isn't a number. */
|
|
64
|
+
export function rate(value: unknown, fallback: number): number {
|
|
65
|
+
return typeof value === "number" && Number.isFinite(value)
|
|
66
|
+
? Math.min(1, Math.max(0, value))
|
|
67
|
+
: fallback;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
71
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
72
|
+
}
|
package/src/v8/builtins/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BlockFunction } from "../types.ts";
|
|
2
|
-
import {
|
|
2
|
+
import { cmsSettings, page, redirect, SETTINGS_TYPE } from "./data.ts";
|
|
3
3
|
import { lazy } from "./lazy.ts";
|
|
4
4
|
import { always, date, never } from "./matchers.ts";
|
|
5
5
|
import { multivariate } from "./multivariate.ts";
|
|
@@ -17,8 +17,7 @@ export const builtIns: Readonly<Record<string, BlockFunction>> = Object.freeze({
|
|
|
17
17
|
date,
|
|
18
18
|
page,
|
|
19
19
|
redirect,
|
|
20
|
-
|
|
21
|
-
analytics,
|
|
20
|
+
[SETTINGS_TYPE]: cmsSettings,
|
|
22
21
|
secret: secretBlock(),
|
|
23
22
|
});
|
|
24
23
|
|
package/src/v8/canonical.ts
CHANGED
|
@@ -115,39 +115,3 @@ export const APPLY_DIGEST_DOMAIN = "deco-content/blocks.apply@1\n";
|
|
|
115
115
|
export function applyRequestDigest(params: object): Promise<string> {
|
|
116
116
|
return sha256Hex(APPLY_DIGEST_DOMAIN + canonicalJson(params));
|
|
117
117
|
}
|
|
118
|
-
|
|
119
|
-
/** The version of the draft overlay manifest format below. */
|
|
120
|
-
export const DRAFT_OVERLAY_FORMAT = 1;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* A draft overlay manifest (see /next/content-delivery#exact-draft-previews):
|
|
124
|
-
* the complete, cumulative set of a draft's changes, never a patch over an
|
|
125
|
-
* earlier version. `set` maps each changed entry's name to the block hash of
|
|
126
|
-
* its JSON; `delete` names the entries the draft removed (tombstones). The two
|
|
127
|
-
* are disjoint. There is no base revision: a preview server layers it over the
|
|
128
|
-
* production content it already has.
|
|
129
|
-
*/
|
|
130
|
-
export interface DraftOverlay {
|
|
131
|
-
format: typeof DRAFT_OVERLAY_FORMAT;
|
|
132
|
-
set: Record<string, string>;
|
|
133
|
-
delete: string[];
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* The block hash of one changed entry: SHA-256 over its canonical JSON,
|
|
138
|
-
* lowercase hex. A draft block asset is stored under it
|
|
139
|
-
* (`/sites/<site>/draft-blocks/<block-hash>.json`), and the SDK checks every
|
|
140
|
-
* asset it downloads hashes to its name.
|
|
141
|
-
*/
|
|
142
|
-
export function computeBlockHash(entry: unknown): Promise<string> {
|
|
143
|
-
return sha256Hex(canonicalJson(entry));
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* The overlay version: SHA-256 over the manifest's canonical JSON, lowercase
|
|
148
|
-
* hex. The manifest is stored under it (`/sites/<site>/drafts/<version>.json`)
|
|
149
|
-
* and a draft pointer names it after its `@`.
|
|
150
|
-
*/
|
|
151
|
-
export function computeOverlayVersion(overlay: DraftOverlay): Promise<string> {
|
|
152
|
-
return sha256Hex(canonicalJson(overlay));
|
|
153
|
-
}
|
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
import fs from "node:fs";
|
|
21
21
|
import { Ajv, type ValidateFunction } from "ajv";
|
|
22
22
|
import { isWellFormedCiphertext } from "../../../protocol/secrets.ts";
|
|
23
|
+
import { BLOCKS_MAJOR } from "../../../protocol/types.ts";
|
|
24
|
+
import { SETTINGS_BLOCK, SETTINGS_TYPE } from "../../builtins/data.ts";
|
|
23
25
|
import { isBlock, own } from "../../json.ts";
|
|
24
26
|
import { findRouteConflicts } from "../../matchRoute.ts";
|
|
25
27
|
import { isBuiltIn, storesPlainVariants } from "../builtins.ts";
|
|
@@ -70,6 +72,12 @@ function blockDefinitionName(schema: Json): string | null {
|
|
|
70
72
|
return e && e.length === 1 && typeof schema.title === "string" && !schema.format ? e[0] : null;
|
|
71
73
|
}
|
|
72
74
|
|
|
75
|
+
/** Prerelease built-ins folded into the CMS settings block, with no alias. */
|
|
76
|
+
const FOLDED_BUILT_INS: Record<string, string> = {
|
|
77
|
+
telemetry: `the built-in is now the telemetry section of the "${SETTINGS_BLOCK}" block (type "${SETTINGS_TYPE}"); see /next/renames-and-migrations#site-settings`,
|
|
78
|
+
analytics: `the built-in is now the analytics section of the "${SETTINGS_BLOCK}" block (type "${SETTINGS_TYPE}"); see /next/renames-and-migrations#site-settings`,
|
|
79
|
+
};
|
|
80
|
+
|
|
73
81
|
// ---------------------------------------------------------------------------
|
|
74
82
|
// The validation document: the schema, with every field also accepting a block
|
|
75
83
|
// ---------------------------------------------------------------------------
|
|
@@ -481,7 +489,13 @@ class Checker {
|
|
|
481
489
|
|
|
482
490
|
if (type === null) {
|
|
483
491
|
if (!Object.hasOwn(this.saved.blocks, name)) {
|
|
484
|
-
this.report(
|
|
492
|
+
this.report(
|
|
493
|
+
ctx,
|
|
494
|
+
path,
|
|
495
|
+
Object.hasOwn(FOLDED_BUILT_INS, name)
|
|
496
|
+
? `unknown block type "${name}": ${FOLDED_BUILT_INS[name]}`
|
|
497
|
+
: `unknown block type "${name}"`,
|
|
498
|
+
);
|
|
485
499
|
return;
|
|
486
500
|
}
|
|
487
501
|
// A reference to a saved block: check what it resolves to against the field.
|
|
@@ -650,6 +664,15 @@ class Checker {
|
|
|
650
664
|
this.report(ctx, "", "missing __resolveType: a saved block names the function it calls");
|
|
651
665
|
continue;
|
|
652
666
|
}
|
|
667
|
+
if (name === SETTINGS_BLOCK && entry.__resolveType !== SETTINGS_TYPE) {
|
|
668
|
+
this.report(
|
|
669
|
+
ctx,
|
|
670
|
+
"",
|
|
671
|
+
`"${SETTINGS_BLOCK}" is the name of the CMS settings block (type "${SETTINGS_TYPE}"); ` +
|
|
672
|
+
"this one is ignored as settings, so rename it",
|
|
673
|
+
"warning",
|
|
674
|
+
);
|
|
675
|
+
}
|
|
653
676
|
this.checkBlock(ctx, entry, "", null);
|
|
654
677
|
}
|
|
655
678
|
problems.push(...this.routeProblems());
|
|
@@ -745,11 +768,34 @@ function readSchemaFile(file: string): DecoMeta {
|
|
|
745
768
|
} catch {
|
|
746
769
|
throw new CliError(`no ${file}; run deco schema first`);
|
|
747
770
|
}
|
|
771
|
+
let meta: DecoMeta;
|
|
748
772
|
try {
|
|
749
|
-
|
|
773
|
+
meta = JSON.parse(text) as DecoMeta;
|
|
750
774
|
} catch (error) {
|
|
751
775
|
throw new CliError(`${file}: invalid JSON: ${(error as Error).message}`);
|
|
752
776
|
}
|
|
777
|
+
assertBlocksMajor(meta, file);
|
|
778
|
+
return meta;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* A schema must declare `"blocksMajor": 8`: the site editor treats a site as
|
|
783
|
+
* v8 only on that field, so a schema without it would edit as a v7 site.
|
|
784
|
+
*/
|
|
785
|
+
export function assertBlocksMajor(meta: unknown, file: string): void {
|
|
786
|
+
const found =
|
|
787
|
+
typeof meta === "object" && meta !== null && !Array.isArray(meta)
|
|
788
|
+
? (meta as { blocksMajor?: unknown }).blocksMajor
|
|
789
|
+
: undefined;
|
|
790
|
+
if (found === BLOCKS_MAJOR) return;
|
|
791
|
+
const what =
|
|
792
|
+
found === undefined
|
|
793
|
+
? 'has no top-level "blocksMajor"'
|
|
794
|
+
: `has "blocksMajor": ${JSON.stringify(found)}`;
|
|
795
|
+
throw new CliError(
|
|
796
|
+
`${file} ${what}; expected "blocksMajor": ${BLOCKS_MAJOR}. ` +
|
|
797
|
+
"It was written by an older or different deco schema: run deco schema to regenerate it, then commit it.",
|
|
798
|
+
);
|
|
753
799
|
}
|
|
754
800
|
|
|
755
801
|
/** `deco check`. Returns the exit code: 0 when everything fits (warnings aside), 1 otherwise. */
|
package/src/v8/cli/index.ts
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
*
|
|
7
7
|
* The four commands and their options, plus what the one-time migration
|
|
8
8
|
* (the `deco-v7-to-v8-migration` skill) reads: the built-in block names, the alias
|
|
9
|
-
* table
|
|
9
|
+
* table, the saved-blocks reader and the host-pattern parser.
|
|
10
10
|
*/
|
|
11
|
+
export { formatHostPattern, type HostPattern, parseHostPattern } from "../hosts.ts";
|
|
11
12
|
export { BUILT_IN_BLOCKS, LEGACY_ALIASES } from "./builtins.ts";
|
|
12
13
|
export { type CheckOptions, check } from "./check/index.ts";
|
|
13
14
|
export { type ContentOptions, content, readSavedBlocks, type SavedBlocks } from "./content.ts";
|
package/src/v8/cli/run.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { reportSchema, schema, writeSchema } from "./schema/index.ts";
|
|
|
10
10
|
import { serve, startServer } from "./serve/server.ts";
|
|
11
11
|
import { watchFiles, watchTree } from "./watch.ts";
|
|
12
12
|
|
|
13
|
-
type FlagKind = "string" | "boolean"
|
|
13
|
+
type FlagKind = "string" | "boolean";
|
|
14
14
|
|
|
15
15
|
const COMMANDS: Record<string, { usage: string; flags: Record<string, FlagKind> }> = {
|
|
16
16
|
schema: {
|
|
@@ -24,14 +24,12 @@ const COMMANDS: Record<string, { usage: string; flags: Record<string, FlagKind>
|
|
|
24
24
|
check: { usage: "deco check [--root <dir>]", flags: { root: "string" } },
|
|
25
25
|
serve: {
|
|
26
26
|
usage:
|
|
27
|
-
"deco serve [--root <dir>] [--port <n>] [--host <addr>] [--preview <host:port|url>] [--
|
|
27
|
+
"deco serve [--root <dir>] [--port <n>] [--host <addr>] [--preview <host:port|url>] [--assets <dir>] [--read-only]",
|
|
28
28
|
flags: {
|
|
29
29
|
root: "string",
|
|
30
30
|
port: "string",
|
|
31
31
|
host: "string",
|
|
32
32
|
preview: "string",
|
|
33
|
-
token: "string",
|
|
34
|
-
"allow-origin": "list",
|
|
35
33
|
assets: "string",
|
|
36
34
|
"read-only": "boolean",
|
|
37
35
|
},
|
|
@@ -44,7 +42,7 @@ Commands:
|
|
|
44
42
|
schema turn the types in .deco/index.ts into .deco/schema.gen.json
|
|
45
43
|
content turn .deco/blocks into the content module, .deco/blocks.gen.ts
|
|
46
44
|
check check that every saved block fits the schema (writes nothing)
|
|
47
|
-
serve serve .deco to the site editor on this machine
|
|
45
|
+
serve serve .deco to the site editor on this machine, at http://localhost:4545/rpc
|
|
48
46
|
|
|
49
47
|
${Object.values(COMMANDS)
|
|
50
48
|
.map((c) => ` ${c.usage}`)
|
|
@@ -78,8 +76,7 @@ export function parseFlags(command: string, args: string[]): ParsedFlags {
|
|
|
78
76
|
throw new CliError(`--${name} needs a value`);
|
|
79
77
|
i++;
|
|
80
78
|
}
|
|
81
|
-
|
|
82
|
-
else flags[name] = value;
|
|
79
|
+
flags[name] = value;
|
|
83
80
|
}
|
|
84
81
|
return flags;
|
|
85
82
|
}
|
|
@@ -213,8 +210,6 @@ export async function runCli(argv: string[], options: RunOptions = {}): Promise<
|
|
|
213
210
|
port,
|
|
214
211
|
host: flags.host as string | undefined,
|
|
215
212
|
preview: flags.preview as string | undefined,
|
|
216
|
-
token: flags.token as string | undefined,
|
|
217
|
-
allowOrigins: flags["allow-origin"] as string[] | undefined,
|
|
218
213
|
assets: flags.assets as string | undefined,
|
|
219
214
|
readOnly: Boolean(flags["read-only"]),
|
|
220
215
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The forms of the
|
|
2
|
+
* The forms of the nine built-in blocks (spec: built-in-blocks, and the types
|
|
3
3
|
* in api-reference › Types). They're always in the schema, unless the block
|
|
4
4
|
* map declares the same key, which replaces the built-in.
|
|
5
5
|
*/
|
|
@@ -15,8 +15,7 @@ export const BUILT_IN_GROUPS: Record<string, ManifestGroup> = {
|
|
|
15
15
|
date: "matchers",
|
|
16
16
|
page: "pages",
|
|
17
17
|
redirect: "redirects",
|
|
18
|
-
|
|
19
|
-
analytics: "content",
|
|
18
|
+
"cms-settings": "content",
|
|
20
19
|
secret: "loaders",
|
|
21
20
|
};
|
|
22
21
|
|
|
@@ -36,8 +35,11 @@ const BUILT_IN_DOCS: Record<string, { title: string; description: string; icon?:
|
|
|
36
35
|
},
|
|
37
36
|
page: { title: "Page", description: "A page at a URL" },
|
|
38
37
|
redirect: { title: "Redirect", description: "Sends one URL to another" },
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
"cms-settings": {
|
|
39
|
+
title: "CMS settings",
|
|
40
|
+
description: "Preview hosts, telemetry and analytics: the saved block named CMS",
|
|
41
|
+
icon: "settings",
|
|
42
|
+
},
|
|
41
43
|
secret: { title: "Secret", description: "An encrypted value, decrypted on the server" },
|
|
42
44
|
};
|
|
43
45
|
|
|
@@ -157,34 +159,53 @@ function builtInProps(name: string, ctx: BuiltInContext): any {
|
|
|
157
159
|
discardQueryParameters: { type: "boolean", title: "Discard query parameters" },
|
|
158
160
|
},
|
|
159
161
|
};
|
|
160
|
-
case "
|
|
162
|
+
case "cms-settings":
|
|
161
163
|
return {
|
|
162
164
|
type: "object",
|
|
163
165
|
properties: {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
166
|
+
preview: {
|
|
167
|
+
type: "object",
|
|
168
|
+
title: "Preview",
|
|
169
|
+
properties: {
|
|
170
|
+
hosts: {
|
|
171
|
+
type: "array",
|
|
172
|
+
title: "Hosts",
|
|
173
|
+
description:
|
|
174
|
+
"Host patterns drafts may be previewed on, such as staging.example.com or *.example.com; empty turns previews off. Without it, every host (or the hosts code allows).",
|
|
175
|
+
items: { type: "string" },
|
|
176
|
+
},
|
|
177
|
+
},
|
|
172
178
|
},
|
|
173
|
-
|
|
174
|
-
type: "
|
|
175
|
-
title: "
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
+
telemetry: {
|
|
180
|
+
type: "object",
|
|
181
|
+
title: "Telemetry",
|
|
182
|
+
properties: {
|
|
183
|
+
enabled: { type: "boolean", title: "Enabled", default: true },
|
|
184
|
+
metrics: { type: "boolean", title: "Metrics", default: true },
|
|
185
|
+
errorSampleRate: {
|
|
186
|
+
type: "number",
|
|
187
|
+
title: "Error sample rate",
|
|
188
|
+
minimum: 0,
|
|
189
|
+
maximum: 1,
|
|
190
|
+
default: 0.05,
|
|
191
|
+
},
|
|
192
|
+
traceSampleRate: {
|
|
193
|
+
type: "number",
|
|
194
|
+
title: "Trace sample rate",
|
|
195
|
+
minimum: 0,
|
|
196
|
+
maximum: 1,
|
|
197
|
+
default: 0,
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
analytics: {
|
|
202
|
+
type: "object",
|
|
203
|
+
title: "Analytics",
|
|
204
|
+
properties: {
|
|
205
|
+
collector: { type: "string", title: "Collector" },
|
|
206
|
+
enabled: { type: "boolean", title: "Enabled", default: true },
|
|
207
|
+
},
|
|
179
208
|
},
|
|
180
|
-
},
|
|
181
|
-
};
|
|
182
|
-
case "analytics":
|
|
183
|
-
return {
|
|
184
|
-
type: "object",
|
|
185
|
-
properties: {
|
|
186
|
-
collector: { type: "string", title: "Collector" },
|
|
187
|
-
enabled: { type: "boolean", title: "Enabled", default: true },
|
|
188
209
|
},
|
|
189
210
|
};
|
|
190
211
|
case "secret":
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import fs from "node:fs";
|
|
14
14
|
import path from "node:path";
|
|
15
|
-
import { SCHEMA_FORMAT } from "../../../protocol/types.ts";
|
|
15
|
+
import { BLOCKS_MAJOR, SCHEMA_FORMAT } from "../../../protocol/types.ts";
|
|
16
16
|
import { BUILT_IN_BLOCKS, LEGACY_ALIASES } from "../builtins.ts";
|
|
17
17
|
import { CliError, type DecoPaths, packageVersion } from "../root.ts";
|
|
18
18
|
import {
|
|
@@ -51,6 +51,8 @@ type ManifestEntry = { $ref: string; namespace: string };
|
|
|
51
51
|
|
|
52
52
|
export interface DecoMeta {
|
|
53
53
|
major: 1;
|
|
54
|
+
/** The Blocks major that generated this schema; `deco check` requires it. */
|
|
55
|
+
blocksMajor: typeof BLOCKS_MAJOR;
|
|
54
56
|
version: string;
|
|
55
57
|
namespace: string;
|
|
56
58
|
site: string;
|
|
@@ -424,6 +426,7 @@ export async function generateSchema(paths: DecoPaths): Promise<SchemaResult> {
|
|
|
424
426
|
|
|
425
427
|
const meta: DecoMeta = {
|
|
426
428
|
major: 1,
|
|
429
|
+
blocksMajor: BLOCKS_MAJOR,
|
|
427
430
|
version: packageVersion(),
|
|
428
431
|
namespace: "site",
|
|
429
432
|
site: siteName(paths.root),
|
|
@@ -338,6 +338,33 @@ export class TsNode {
|
|
|
338
338
|
return texts;
|
|
339
339
|
}
|
|
340
340
|
|
|
341
|
+
/**
|
|
342
|
+
* The `@format` on the type alias this annotation names: `Color`,
|
|
343
|
+
* `Color | null` and `Color[]` (then `items` is set) all give `Color`'s.
|
|
344
|
+
* Read from the alias's declaration, since the checker drops an alias of a
|
|
345
|
+
* primitive (`type Color = string` is just `string`).
|
|
346
|
+
*/
|
|
347
|
+
getAliasFormat(): { format: string; items: boolean } | undefined {
|
|
348
|
+
const ts = this.ctx.ts;
|
|
349
|
+
const checker = this.ctx.checker;
|
|
350
|
+
const node = this.compilerNode as TS.TypeNode;
|
|
351
|
+
for (const member of ts.isUnionTypeNode(node) ? node.types : [node]) {
|
|
352
|
+
const items = ts.isArrayTypeNode(member);
|
|
353
|
+
const target = items ? member.elementType : member;
|
|
354
|
+
if (!ts.isTypeReferenceNode(target)) continue;
|
|
355
|
+
let symbol = checker.getSymbolAtLocation(target.typeName);
|
|
356
|
+
if (symbol && symbol.flags & ts.SymbolFlags.Alias) symbol = checker.getAliasedSymbol(symbol);
|
|
357
|
+
const decl = symbol?.declarations?.find(ts.isTypeAliasDeclaration);
|
|
358
|
+
if (!decl) continue;
|
|
359
|
+
for (const tag of ts.getJSDocTags(decl)) {
|
|
360
|
+
if (tag.tagName.text !== "format") continue;
|
|
361
|
+
const format = ts.getTextOfJSDocComment(tag.comment)?.trim();
|
|
362
|
+
if (format) return { format, items };
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return undefined;
|
|
366
|
+
}
|
|
367
|
+
|
|
341
368
|
/** Whether `other` is this node or inside it. */
|
|
342
369
|
contains(other: TsNode): boolean {
|
|
343
370
|
const a = this.compilerNode;
|
|
@@ -231,6 +231,20 @@ function applyWidgetFormat(schema: any, typeHint: string): void {
|
|
|
231
231
|
}
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
+
/**
|
|
235
|
+
* `@format` written on a type alias (`/** @format color *\/ type Color = string`)
|
|
236
|
+
* reaches every field of that type, a literal select included; a field's own
|
|
237
|
+
* `@format` still wins, since its tags apply afterwards.
|
|
238
|
+
*/
|
|
239
|
+
function applyAliasFormat(
|
|
240
|
+
schema: any,
|
|
241
|
+
alias: { format: string; items: boolean } | undefined,
|
|
242
|
+
): void {
|
|
243
|
+
if (!alias) return;
|
|
244
|
+
const target = alias.items ? schema.items : schema;
|
|
245
|
+
if (target?.type === "string") target.format ??= FORMAT_ALIASES[alias.format] ?? alias.format;
|
|
246
|
+
}
|
|
247
|
+
|
|
234
248
|
// ---------------------------------------------------------------------------
|
|
235
249
|
// Recognizing the framework's own types
|
|
236
250
|
// ---------------------------------------------------------------------------
|
|
@@ -703,7 +717,10 @@ export function typeToJsonSchema(
|
|
|
703
717
|
}
|
|
704
718
|
|
|
705
719
|
const schema = fieldSchema(propType, typeHint, ctx, visited);
|
|
706
|
-
if (typeNode)
|
|
720
|
+
if (typeNode) {
|
|
721
|
+
orderEnumByHint(schema, typeNode.getAliasDeclarationTexts().join("\n"));
|
|
722
|
+
applyAliasFormat(schema, typeNode.getAliasFormat());
|
|
723
|
+
}
|
|
707
724
|
if (schema.anyOf && schema.anyOf[0]?.$ref === resolvableRef().$ref) {
|
|
708
725
|
// A block-ref field: nullability lives on the wrapper.
|
|
709
726
|
if (isNullableHint(optional, typeHint)) schema.nullable = true;
|