@decocms/blocks 8.0.0 → 8.1.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/bin/deco.js +13 -0
  2. package/package.json +39 -3
  3. package/src/cms/applySectionConventions.ts +26 -2
  4. package/src/cms/blockSource.ts +37 -0
  5. package/src/cms/client.ts +5 -0
  6. package/src/cms/deferredTrigger.ts +58 -0
  7. package/src/cms/draftSource.ts +171 -57
  8. package/src/cms/index.ts +32 -3
  9. package/src/cms/loader.ts +318 -59
  10. package/src/cms/registry.ts +22 -0
  11. package/src/cms/renderJson.ts +179 -0
  12. package/src/cms/resolve.ts +396 -95
  13. package/src/cms/schema.ts +9 -5
  14. package/src/cms/sectionLoaders.ts +106 -56
  15. package/src/hooks/Image.tsx +70 -6
  16. package/src/hooks/LiveControls.tsx +30 -8
  17. package/src/hooks/Stats.tsx +134 -0
  18. package/src/hooks/index.ts +5 -0
  19. package/src/hooks/useLoadMore.ts +100 -0
  20. package/src/index.ts +37 -0
  21. package/src/middleware/observability.ts +173 -16
  22. package/src/preview/DraftPreviewBadge.tsx +306 -0
  23. package/src/preview/decoMark.ts +11 -0
  24. package/src/preview/index.ts +8 -0
  25. package/src/protocol/assets.ts +124 -0
  26. package/src/protocol/canonical.ts +7 -0
  27. package/src/protocol/ciphertext.ts +6 -0
  28. package/src/protocol/client.ts +170 -0
  29. package/src/protocol/conformance/cases/assets.ts +81 -0
  30. package/src/protocol/conformance/cases/guards.ts +314 -0
  31. package/src/protocol/conformance/cases/reads.ts +168 -0
  32. package/src/protocol/conformance/cases/types.ts +9 -0
  33. package/src/protocol/conformance/cases/wire.ts +138 -0
  34. package/src/protocol/conformance/cases/writes.ts +345 -0
  35. package/src/protocol/conformance/contentHashFixtures.ts +79 -0
  36. package/src/protocol/conformance/context.ts +222 -0
  37. package/src/protocol/conformance/index.ts +140 -0
  38. package/src/protocol/errors.ts +142 -0
  39. package/src/protocol/index.ts +39 -0
  40. package/src/protocol/keys.ts +279 -0
  41. package/src/protocol/params.ts +67 -0
  42. package/src/protocol/secrets.ts +231 -0
  43. package/src/protocol/server/assets.ts +119 -0
  44. package/src/protocol/server/auth.ts +49 -0
  45. package/src/protocol/server/bodyCache.ts +84 -0
  46. package/src/protocol/server/content.ts +175 -0
  47. package/src/protocol/server/core.ts +112 -0
  48. package/src/protocol/server/handler.ts +102 -0
  49. package/src/protocol/server/http.ts +139 -0
  50. package/src/protocol/server/index.ts +11 -0
  51. package/src/protocol/server/methods/apply.ts +341 -0
  52. package/src/protocol/server/methods/read.ts +156 -0
  53. package/src/protocol/server/rpc.ts +136 -0
  54. package/src/protocol/storage/fs/hash.ts +21 -0
  55. package/src/protocol/storage/fs/index.ts +338 -0
  56. package/src/protocol/storage/fs/lock.ts +68 -0
  57. package/src/protocol/storage/fs/transaction.ts +183 -0
  58. package/src/protocol/storage/fs/unsupported.ts +15 -0
  59. package/src/protocol/storage/memory.ts +238 -0
  60. package/src/protocol/storage.ts +182 -0
  61. package/src/protocol/types.ts +172 -0
  62. package/src/sdk/abTesting.ts +60 -3
  63. package/src/sdk/analytics.ts +72 -2
  64. package/src/sdk/cacheHeaders.ts +210 -10
  65. package/src/sdk/cacheStorage.ts +287 -0
  66. package/src/sdk/cachedLoader.ts +143 -90
  67. package/src/sdk/crypto.ts +16 -2
  68. package/src/sdk/deepOmit.ts +52 -0
  69. package/src/sdk/encoding.ts +2 -5
  70. package/src/sdk/experiments.ts +314 -0
  71. package/src/sdk/fetchCache.ts +269 -0
  72. package/src/sdk/fetchTimeout.ts +58 -0
  73. package/src/sdk/flags.ts +38 -8
  74. package/src/sdk/index.ts +7 -0
  75. package/src/sdk/instrumentedFetch.ts +25 -2
  76. package/src/sdk/nonce.ts +46 -0
  77. package/src/sdk/normalizeUrls.ts +2 -2
  78. package/src/sdk/observability.ts +8 -1
  79. package/src/sdk/otel.ts +131 -25
  80. package/src/sdk/otelHttpLog.ts +4 -4
  81. package/src/sdk/otelHttpMeter.ts +39 -13
  82. package/src/sdk/otelHttpTracer.ts +4 -4
  83. package/src/sdk/redirects.ts +113 -5
  84. package/src/sdk/responseCache.ts +82 -0
  85. package/src/sdk/urlUtils.ts +49 -0
  86. package/src/sdk/useSuggestions.ts +1 -1
  87. package/src/v8/analytics.ts +50 -0
  88. package/src/v8/background.ts +107 -0
  89. package/src/v8/builtins/data.ts +23 -0
  90. package/src/v8/builtins/index.ts +26 -0
  91. package/src/v8/builtins/lazy.ts +23 -0
  92. package/src/v8/builtins/legacy.ts +65 -0
  93. package/src/v8/builtins/matchers.ts +31 -0
  94. package/src/v8/builtins/multivariate.ts +25 -0
  95. package/src/v8/builtins/secret.ts +82 -0
  96. package/src/v8/canonical.ts +117 -0
  97. package/src/v8/ciphertext.ts +158 -0
  98. package/src/v8/cli/builtins.ts +20 -0
  99. package/src/v8/cli/check/index.ts +774 -0
  100. package/src/v8/cli/check/messages.ts +168 -0
  101. package/src/v8/cli/content.ts +287 -0
  102. package/src/v8/cli/index.ts +17 -0
  103. package/src/v8/cli/log.ts +18 -0
  104. package/src/v8/cli/main.ts +12 -0
  105. package/src/v8/cli/root.ts +66 -0
  106. package/src/v8/cli/run.ts +236 -0
  107. package/src/v8/cli/schema/builtinSchemas.ts +236 -0
  108. package/src/v8/cli/schema/generate.ts +507 -0
  109. package/src/v8/cli/schema/index.ts +63 -0
  110. package/src/v8/cli/schema/tsProgram.ts +497 -0
  111. package/src/v8/cli/schema/typeToSchema.ts +689 -0
  112. package/src/v8/cli/serve/server.ts +303 -0
  113. package/src/v8/cli/watch.ts +86 -0
  114. package/src/v8/client.ts +147 -0
  115. package/src/v8/cms.ts +237 -0
  116. package/src/v8/content.ts +195 -0
  117. package/src/v8/draft.ts +136 -0
  118. package/src/v8/errors.ts +59 -0
  119. package/src/v8/fetch.ts +152 -0
  120. package/src/v8/identity.ts +65 -0
  121. package/src/v8/index.ts +39 -0
  122. package/src/v8/json.ts +74 -0
  123. package/src/v8/matchRoute.ts +204 -0
  124. package/src/v8/remoteLoader.ts +282 -0
  125. package/src/v8/resolver.ts +278 -0
  126. package/src/v8/secrets.ts +17 -0
  127. package/src/v8/telemetry.ts +533 -0
  128. package/src/v8/types.ts +240 -0
  129. package/src/cms/blockSource.test.ts +0 -76
  130. package/src/cms/client.browserBundle.test.ts +0 -44
  131. package/src/cms/draftSource.test.ts +0 -343
  132. package/src/cms/layoutCacheRace.test.ts +0 -181
  133. package/src/cms/loadDecofileDirectory.test.ts +0 -109
  134. package/src/cms/loader.test.ts +0 -200
  135. package/src/cms/registry.test.ts +0 -110
  136. package/src/cms/resolve.test.ts +0 -980
  137. package/src/cms/schema.test.ts +0 -356
  138. package/src/cms/sectionLoaderContext.test.ts +0 -111
  139. package/src/cms/sectionLoaders.test.ts +0 -533
  140. package/src/cms/sectionMixins.test.ts +0 -167
  141. package/src/cms/stickyFlags.test.ts +0 -117
  142. package/src/flags/flags.test.ts +0 -148
  143. package/src/hooks/Image.test.ts +0 -100
  144. package/src/hooks/RenderSection.test.tsx +0 -81
  145. package/src/matchers/builtins.test.ts +0 -318
  146. package/src/matchers/override.test.ts +0 -205
  147. package/src/middleware/hydrationContext.test.ts +0 -61
  148. package/src/middleware/observability.test.ts +0 -242
  149. package/src/middleware/validateSection.test.ts +0 -147
  150. package/src/sdk/abTesting.test.ts +0 -326
  151. package/src/sdk/cacheHeaders.test.ts +0 -115
  152. package/src/sdk/cachedLoader.test.ts +0 -122
  153. package/src/sdk/cachedLoaderModule.test.ts +0 -209
  154. package/src/sdk/cn.test.ts +0 -34
  155. package/src/sdk/composite.test.ts +0 -121
  156. package/src/sdk/cookie.test.ts +0 -108
  157. package/src/sdk/encoding.test.ts +0 -71
  158. package/src/sdk/env.test.ts +0 -33
  159. package/src/sdk/flags.test.ts +0 -91
  160. package/src/sdk/http.test.ts +0 -71
  161. package/src/sdk/inflightTimeout.test.ts +0 -35
  162. package/src/sdk/instrumentedFetch.test.ts +0 -559
  163. package/src/sdk/invoke.test.ts +0 -115
  164. package/src/sdk/logger.test.ts +0 -432
  165. package/src/sdk/otel.test.ts +0 -526
  166. package/src/sdk/otelAdapters.test.ts +0 -89
  167. package/src/sdk/otelHttpLog.test.ts +0 -457
  168. package/src/sdk/otelHttpMeter.test.ts +0 -292
  169. package/src/sdk/otelHttpTracer.test.ts +0 -474
  170. package/src/sdk/requestContextStorage.browser.test.ts +0 -29
  171. package/src/sdk/urlRedaction.test.ts +0 -73
  172. package/src/sdk/useDevice.test.ts +0 -130
  173. package/src/sdk/useScript.test.ts +0 -128
  174. package/src/sdk/useSuggestions.test.ts +0 -230
  175. package/tsconfig.json +0 -7
@@ -0,0 +1,170 @@
1
+ /**
2
+ * A small content-protocol client: one method per protocol method, plus
3
+ * batches. Uses `fetch`, so it runs in browsers, on Workers and on Node.
4
+ */
5
+ import { ContentProtocolError, ErrorCode, type RpcErrorObject } from "./errors";
6
+ import {
7
+ type DescribeResult,
8
+ type MethodName,
9
+ type Methods,
10
+ PROTOCOL_NAME,
11
+ PROTOCOL_VERSION,
12
+ type RpcId,
13
+ } from "./types";
14
+
15
+ export interface ContentClientOptions {
16
+ /** The endpoint URL, such as `http://127.0.0.1:4545/rpc`. */
17
+ endpoint: string | URL;
18
+ /** Sent as `Authorization: Bearer <token>`. */
19
+ token?: string;
20
+ /** Extra headers on every request. */
21
+ headers?: Record<string, string>;
22
+ /** A fetch implementation (defaults to the global one). */
23
+ fetch?: (input: Request) => Promise<Response>;
24
+ }
25
+
26
+ /** One call of a batch. */
27
+ export type BatchCall = {
28
+ [M in MethodName]: { method: M; params?: Methods[M]["params"] };
29
+ }[MethodName];
30
+
31
+ /** The outcome of one batch call: its result or its error. */
32
+ export type BatchOutcome =
33
+ | { ok: true; result: unknown }
34
+ | { ok: false; error: ContentProtocolError };
35
+
36
+ export interface ContentClient {
37
+ /** Throws Unsupported when the endpoint speaks another protocol or major version. */
38
+ describe(): Promise<Methods["describe"]["result"]>;
39
+ schemaGet(params?: Methods["schema.get"]["params"]): Promise<Methods["schema.get"]["result"]>;
40
+ blocksList(params?: Methods["blocks.list"]["params"]): Promise<Methods["blocks.list"]["result"]>;
41
+ blocksApply(
42
+ params: Methods["blocks.apply"]["params"],
43
+ ): Promise<Methods["blocks.apply"]["result"]>;
44
+ /** Calls one method by name. Throws `ContentProtocolError` on an error response. */
45
+ call<M extends MethodName>(
46
+ method: M,
47
+ params?: Methods[M]["params"],
48
+ ): Promise<Methods[M]["result"]>;
49
+ /** Runs several calls in one request; outcomes come back in order. */
50
+ batch(calls: BatchCall[]): Promise<BatchOutcome[]>;
51
+ }
52
+
53
+ type WireResponse = { id: RpcId | null; result?: unknown; error?: RpcErrorObject };
54
+
55
+ /**
56
+ * Decodes a JSON response body, gunzipping it when a custom fetch (such as an
57
+ * in-process handler) handed back the raw gzip bytes.
58
+ */
59
+ export async function readResponseJson(response: Response): Promise<unknown> {
60
+ let bytes = new Uint8Array(await response.arrayBuffer());
61
+ if (bytes[0] === 0x1f && bytes[1] === 0x8b) {
62
+ const stream = new Blob([bytes as BlobPart])
63
+ .stream()
64
+ .pipeThrough(
65
+ new DecompressionStream("gzip") as unknown as ReadableWritablePair<Uint8Array, Uint8Array>,
66
+ );
67
+ bytes = new Uint8Array(await new Response(stream).arrayBuffer());
68
+ }
69
+ return JSON.parse(new TextDecoder().decode(bytes));
70
+ }
71
+
72
+ /**
73
+ * Refuses an endpoint this client can't speak to: another protocol, or
74
+ * another major version (minors only add, so any minor is fine).
75
+ */
76
+ export function assertSupportedEndpoint(description: DescribeResult): DescribeResult {
77
+ const { protocol, version } = (description ?? {}) as Partial<DescribeResult>;
78
+ if (protocol !== PROTOCOL_NAME) {
79
+ throw new ContentProtocolError(
80
+ ErrorCode.Unsupported,
81
+ `the endpoint speaks ${JSON.stringify(protocol)}, not ${PROTOCOL_NAME}`,
82
+ );
83
+ }
84
+ if (version?.major !== PROTOCOL_VERSION.major) {
85
+ throw new ContentProtocolError(
86
+ ErrorCode.Unsupported,
87
+ `the endpoint speaks ${PROTOCOL_NAME} ${String(version?.major)}.x; this client speaks ${PROTOCOL_VERSION.major}.x`,
88
+ );
89
+ }
90
+ return description;
91
+ }
92
+
93
+ export function createContentClient(options: ContentClientOptions): ContentClient {
94
+ const doFetch = options.fetch ?? ((request: Request) => fetch(request));
95
+ let nextId = 1;
96
+
97
+ async function post(body: unknown): Promise<unknown> {
98
+ const headers: Record<string, string> = {
99
+ "content-type": "application/json",
100
+ accept: "application/json",
101
+ ...options.headers,
102
+ };
103
+ if (options.token !== undefined) headers.authorization = `Bearer ${options.token}`;
104
+ const response = await doFetch(
105
+ new Request(options.endpoint, { method: "POST", headers, body: JSON.stringify(body) }),
106
+ );
107
+ let payload: unknown;
108
+ try {
109
+ payload = await readResponseJson(response);
110
+ } catch {
111
+ throw new ContentProtocolError(
112
+ ErrorCode.Unavailable,
113
+ `the endpoint answered HTTP ${response.status} without a JSON-RPC body`,
114
+ );
115
+ }
116
+ if (!response.ok || (!Array.isArray(payload) && (payload as WireResponse)?.id === null)) {
117
+ const error = (payload as WireResponse)?.error;
118
+ if (error) throw ContentProtocolError.from(error);
119
+ throw new ContentProtocolError(
120
+ ErrorCode.Unavailable,
121
+ `the endpoint answered HTTP ${response.status}`,
122
+ );
123
+ }
124
+ return payload;
125
+ }
126
+
127
+ const call = async <M extends MethodName>(method: M, params?: Methods[M]["params"]) => {
128
+ const id = nextId++;
129
+ const response = (await post({
130
+ jsonrpc: "2.0",
131
+ id,
132
+ method,
133
+ params: params ?? {},
134
+ })) as WireResponse;
135
+ if (response.error) throw ContentProtocolError.from(response.error);
136
+ return response.result as Methods[M]["result"];
137
+ };
138
+
139
+ return {
140
+ call,
141
+ describe: async () => assertSupportedEndpoint(await call("describe")),
142
+ schemaGet: (params) => call("schema.get", params),
143
+ blocksList: (params) => call("blocks.list", params),
144
+ blocksApply: (params) => call("blocks.apply", params),
145
+ async batch(calls) {
146
+ const ids = calls.map(() => nextId++);
147
+ const responses = (await post(
148
+ calls.map((c, i) => ({
149
+ jsonrpc: "2.0",
150
+ id: ids[i],
151
+ method: c.method,
152
+ params: c.params ?? {},
153
+ })),
154
+ )) as WireResponse[];
155
+ const byId = new Map(responses.map((r) => [r.id, r]));
156
+ return ids.map((id): BatchOutcome => {
157
+ const response = byId.get(id);
158
+ if (!response) {
159
+ return {
160
+ ok: false,
161
+ error: new ContentProtocolError(ErrorCode.InternalError, "no response for this call"),
162
+ };
163
+ }
164
+ return response.error
165
+ ? { ok: false, error: ContentProtocolError.from(response.error) }
166
+ : { ok: true, result: response.result };
167
+ });
168
+ },
169
+ };
170
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Uploads beside the protocol: `PUT /assets/<name>`. These cases run only
3
+ * when the harness passes `assetsEndpoint`.
4
+ */
5
+ import { ErrorCode } from "../../errors";
6
+ import { assert, assertEqual, type ConformanceContext, rawErrorCode } from "../context";
7
+ import type { ConformanceCase } from "./types";
8
+
9
+ /** The smallest PNG header: enough bytes for an upload, never executed. */
10
+ const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
11
+
12
+ async function uploadsAccepted(ctx: ConformanceContext) {
13
+ if (ctx.options.assetsEndpoint === undefined) return ctx.skip("no assetsEndpoint");
14
+ const d = await ctx.describe();
15
+ if (d.readOnly || d.assets === null) return ctx.skip("the endpoint doesn't accept uploads");
16
+ return d;
17
+ }
18
+
19
+ const pathOf = (body: unknown) => (body as { path?: unknown } | null)?.path;
20
+
21
+ export const assetCases: ConformanceCase[] = [
22
+ {
23
+ id: "assets/upload",
24
+ title:
25
+ "an upload answers 201 with /assets/<name>; a taken name gets a suffix, never an overwrite",
26
+ async run(ctx) {
27
+ await uploadsAccepted(ctx);
28
+ const name = `${ctx.prefix}-banner.png`;
29
+ const first = await ctx.upload(name, PNG, "image/png");
30
+ assertEqual(first.status, 201, "HTTP status");
31
+ assertEqual(pathOf(first.body), `/assets/${name}`, "the stored path");
32
+ const second = await ctx.upload(name, PNG, "image/png");
33
+ assertEqual(second.status, 201, "HTTP status of the second upload");
34
+ const path = pathOf(second.body);
35
+ assert(
36
+ typeof path === "string" && path.startsWith("/assets/") && path !== `/assets/${name}`,
37
+ `a taken name gets another name, got ${String(path)}`,
38
+ );
39
+ assert(path.endsWith(".png"), "the suffix goes before the extension");
40
+ },
41
+ },
42
+ {
43
+ id: "assets/content-types",
44
+ title: "uploads refuse non-media types, SVG and an extension that doesn't match the type (415)",
45
+ async run(ctx) {
46
+ await uploadsAccepted(ctx);
47
+ const script = "<script>alert(document.cookie)</script>";
48
+ const refused: Array<[name: string, type: string]> = [
49
+ [`${ctx.prefix}-page.html`, "text/html"],
50
+ [`${ctx.prefix}-evil.html`, "image/png"],
51
+ [`${ctx.prefix}-evil.js`, "image/jpeg"],
52
+ [`${ctx.prefix}-logo.svg`, "image/svg+xml"],
53
+ [`${ctx.prefix}-data.json`, "application/json"],
54
+ ];
55
+ for (const [name, type] of refused) {
56
+ const response = await ctx.upload(name, script, type);
57
+ assertEqual(response.status, 415, `HTTP status of ${name} as ${type}`);
58
+ assertEqual(rawErrorCode(response.body), ErrorCode.InvalidRequest, "the error code");
59
+ }
60
+ },
61
+ },
62
+ {
63
+ id: "assets/read-only",
64
+ title: "an endpoint without uploads refuses them",
65
+ async run(ctx) {
66
+ if (ctx.options.assetsEndpoint === undefined) return ctx.skip("no assetsEndpoint");
67
+ const d = await ctx.describe();
68
+ if (!d.readOnly && d.assets !== null) return ctx.skip("the endpoint accepts uploads");
69
+ const response = await ctx.upload(`${ctx.prefix}-x.png`, PNG, "image/png");
70
+ assert(
71
+ response.status === 403 || response.status === 404,
72
+ `HTTP ${response.status} (expected 403 or 404)`,
73
+ );
74
+ const code = rawErrorCode(response.body);
75
+ assert(
76
+ code === ErrorCode.ReadOnly || code === ErrorCode.Unsupported,
77
+ `error ${String(code)} (expected ReadOnly or Unsupported)`,
78
+ );
79
+ },
80
+ },
81
+ ];
@@ -0,0 +1,314 @@
1
+ /**
2
+ * Limits, the secret guard and request-key idempotency.
3
+ */
4
+ import { encryptToCiphertext, publicKeyPemFromDer } from "../../ciphertext";
5
+ import { ErrorCode, type InvalidBlockData } from "../../errors";
6
+ import {
7
+ assert,
8
+ assertEqual,
9
+ type ConformanceContext,
10
+ expectError,
11
+ rawErrorCode,
12
+ } from "../context";
13
+ import type { ConformanceCase } from "./types";
14
+
15
+ async function writable(ctx: ConformanceContext) {
16
+ const d = await ctx.describe();
17
+ if (d.readOnly) return ctx.skip("read-only endpoint");
18
+ return d;
19
+ }
20
+
21
+ async function withIdempotency(ctx: ConformanceContext) {
22
+ const d = await writable(ctx);
23
+ if (!d.writes.idempotency) return ctx.skip("request keys aren't advertised");
24
+ return d;
25
+ }
26
+
27
+ /** A throwaway public key, for endpoints that don't report one. */
28
+ async function ephemeralPublicKey(): Promise<string> {
29
+ const pair = (await crypto.subtle.generateKey(
30
+ {
31
+ name: "RSA-OAEP",
32
+ modulusLength: 2048,
33
+ publicExponent: new Uint8Array([1, 0, 1]),
34
+ hash: "SHA-256",
35
+ },
36
+ true,
37
+ ["encrypt", "decrypt"],
38
+ )) as CryptoKeyPair;
39
+ return publicKeyPemFromDer(new Uint8Array(await crypto.subtle.exportKey("spki", pair.publicKey)));
40
+ }
41
+
42
+ /** A real ciphertext: encrypted with the endpoint's public key when it reports one. */
43
+ async function realCiphertext(ctx: ConformanceContext, value: string): Promise<string> {
44
+ const publicKey = (await ctx.describe()).secrets?.publicKey ?? (await ephemeralPublicKey());
45
+ return encryptToCiphertext(publicKey, value);
46
+ }
47
+
48
+ const key = (ctx: ConformanceContext, label: string) =>
49
+ `${ctx.prefix}-${label}-${Math.random().toString(36).slice(2)}`;
50
+
51
+ export const guardCases: ConformanceCase[] = [
52
+ {
53
+ id: "limits/ops-per-apply",
54
+ title: "more names than maxOpsPerApply is LimitExceeded",
55
+ async run(ctx) {
56
+ const d = await writable(ctx);
57
+ const names = Array.from(
58
+ { length: d.limits.maxOpsPerApply + 1 },
59
+ (_, i) => `${ctx.prefix}-op-${i}`,
60
+ );
61
+ await expectError(
62
+ ctx.client.blocksApply({ delete: names }),
63
+ ErrorCode.LimitExceeded,
64
+ "too many names",
65
+ );
66
+ },
67
+ },
68
+ {
69
+ id: "limits/block-bytes",
70
+ title: "an entry over maxBlockBytes is refused and nothing is written",
71
+ async run(ctx) {
72
+ const d = await writable(ctx);
73
+ if (d.limits.maxBlockBytes + 1024 > d.limits.maxRequestBytes)
74
+ return ctx.skip("the request limit is lower");
75
+ const big = ctx.name("big");
76
+ const small = ctx.name("small");
77
+ const error = await expectError(
78
+ ctx.client.blocksApply({
79
+ set: { [big]: { text: "x".repeat(d.limits.maxBlockBytes) }, [small]: { ok: true } },
80
+ }),
81
+ ErrorCode.InvalidBlock,
82
+ "an oversized entry",
83
+ );
84
+ const violations = (error.data as InvalidBlockData).violations;
85
+ assert(
86
+ violations.some((v) => v.name === big),
87
+ "the violation names the entry",
88
+ );
89
+ const list = await ctx.client.blocksList();
90
+ assert(!list.notModified && !(small in list.blocks), "nothing was written");
91
+ },
92
+ },
93
+ {
94
+ id: "limits/request-bytes",
95
+ title: "a body over maxRequestBytes is HTTP 413 with LimitExceeded",
96
+ async run(ctx) {
97
+ const d = await ctx.describe();
98
+ if (d.limits.maxRequestBytes > 64 * 1024 * 1024)
99
+ return ctx.skip("the request limit is too large to probe");
100
+ const body = JSON.stringify({
101
+ jsonrpc: "2.0",
102
+ id: 1,
103
+ method: "blocks.apply",
104
+ params: { set: { [ctx.name("huge")]: { text: "x".repeat(d.limits.maxRequestBytes) } } },
105
+ });
106
+ const response = await ctx.raw(body);
107
+ assertEqual(response.status, 413, "HTTP status");
108
+ assertEqual(rawErrorCode(response.body), ErrorCode.LimitExceeded, "the error code");
109
+ },
110
+ },
111
+ {
112
+ id: "limits/list-bytes",
113
+ title: "a block list over maxListBytes is LimitExceeded, never a partial map",
114
+ async run(ctx) {
115
+ const d = await writable(ctx);
116
+ const { maxListBytes, maxBlockBytes, maxOpsPerApply } = d.limits;
117
+ if (maxListBytes > 512 * 1024) return ctx.skip("maxListBytes is too large to probe");
118
+ const size = Math.min(maxBlockBytes, 64 * 1024) - 64;
119
+ const count = Math.ceil(maxListBytes / size) + 1;
120
+ if (count > maxOpsPerApply * 4) return ctx.skip("would need too many writes");
121
+ for (let i = 0; i < count; i++) {
122
+ await ctx.client.blocksApply({ set: { [ctx.name("fill")]: { text: "x".repeat(size) } } });
123
+ }
124
+ await expectError(ctx.client.blocksList(), ErrorCode.LimitExceeded, "an oversized list");
125
+ },
126
+ },
127
+ {
128
+ id: "limits/batch-response-bytes",
129
+ title:
130
+ "a batch response over maxBatchResponseBytes answers later reads with LimitExceeded, never a write",
131
+ async run(ctx) {
132
+ const d = await writable(ctx);
133
+ const { maxBatchResponseBytes, maxBlockBytes, maxListBytes } = d.limits;
134
+ if (maxBatchResponseBytes > 1024 * 1024)
135
+ return ctx.skip("maxBatchResponseBytes is too large to probe");
136
+ // Enough content that one list fits the budget but four don't.
137
+ const target = Math.floor(maxBatchResponseBytes / 3);
138
+ if (target > maxListBytes / 2) return ctx.skip("maxListBytes is lower than the probe needs");
139
+ const size = Math.min(maxBlockBytes, 64 * 1024) - 64;
140
+ for (let filled = 0; filled < target; filled += size) {
141
+ await ctx.client.blocksApply({ set: { [ctx.name("fill")]: { text: "x".repeat(size) } } });
142
+ }
143
+ const write = ctx.name("batch-write");
144
+ const req = (id: number, method: string, params: unknown) => ({
145
+ jsonrpc: "2.0",
146
+ id,
147
+ method,
148
+ params,
149
+ });
150
+ const response = await ctx.rpc([
151
+ req(1, "blocks.list", {}),
152
+ req(2, "blocks.list", {}),
153
+ req(3, "blocks.list", {}),
154
+ req(4, "blocks.list", {}),
155
+ req(5, "blocks.apply", { set: { [write]: { kept: true } } }),
156
+ req(6, "blocks.list", {}),
157
+ ]);
158
+ const items = response.body as Array<{
159
+ id: number;
160
+ result?: unknown;
161
+ error?: { code: number; data?: { limit?: string } };
162
+ }>;
163
+ assert(Array.isArray(items) && items.length === 6, "one response per call");
164
+ assert(!items[0].error, "the first read fits");
165
+ const over = items.filter((i) => i.error?.code === ErrorCode.LimitExceeded);
166
+ assert(over.length > 0, "a read over the budget is LimitExceeded");
167
+ assert(
168
+ over.every((i) => i.error?.data?.limit === "maxBatchResponseBytes"),
169
+ "the error names maxBatchResponseBytes",
170
+ );
171
+ assert(items[4].result !== undefined, "the write still answers with its result");
172
+ const list = await ctx.client.blocksList();
173
+ assert(!list.notModified && write in list.blocks, "the write landed");
174
+ },
175
+ },
176
+ {
177
+ id: "limits/schema-bytes",
178
+ title: "a schema over maxSchemaBytes is LimitExceeded for schema.get and for writes",
179
+ async run(ctx) {
180
+ if (!ctx.options.schemaOverLimit)
181
+ return ctx.skip("the harness serves a schema within limits");
182
+ await expectError(ctx.client.schemaGet(), ErrorCode.LimitExceeded, "schema.get");
183
+ const d = await ctx.describe();
184
+ if (d.readOnly) return;
185
+ const name = ctx.name("schema-limited");
186
+ await expectError(
187
+ ctx.client.blocksApply({ set: { [name]: { v: 1 } } }),
188
+ ErrorCode.LimitExceeded,
189
+ "a write checked against the schema",
190
+ );
191
+ const list = await ctx.client.blocksList();
192
+ assert(!list.notModified && !(name in list.blocks), "nothing was written");
193
+ },
194
+ },
195
+ {
196
+ id: "secrets/guard",
197
+ title: "a Secret field accepts only a secret block with a well-formed ciphertext",
198
+ async run(ctx) {
199
+ await writable(ctx);
200
+ const fixture = ctx.options.secretField;
201
+ if (!fixture) return ctx.skip("no secretField fixture");
202
+ const block = (value: unknown) => ({
203
+ __resolveType: fixture.blockType,
204
+ [fixture.field]: value,
205
+ });
206
+ const plain = ctx.name("plain-secret");
207
+ const error = await expectError(
208
+ ctx.client.blocksApply({ set: { [plain]: block("hunter2") } }),
209
+ ErrorCode.InvalidBlock,
210
+ "plain text in a Secret field",
211
+ );
212
+ const violation = (error.data as InvalidBlockData).violations[0];
213
+ assertEqual(violation?.name, plain, "the violation names the entry");
214
+ // Text that only looks like a ciphertext is plain text all the same.
215
+ for (const ciphertext of ["hunter2", "v1.hunter2", "v1.my-api-key_123", "v1.QUJD.ZGVm"]) {
216
+ await expectError(
217
+ ctx.client.blocksApply({
218
+ set: { [ctx.name("bad-cipher")]: block({ __resolveType: "secret", ciphertext }) },
219
+ }),
220
+ ErrorCode.InvalidBlock,
221
+ `the malformed ciphertext ${JSON.stringify(ciphertext)}`,
222
+ );
223
+ }
224
+ const ok = ctx.name("secret");
225
+ const ciphertext = await realCiphertext(ctx, "conformance-value");
226
+ await ctx.client.blocksApply({
227
+ set: { [ok]: block({ __resolveType: "secret", ciphertext }) },
228
+ });
229
+ const list = await ctx.client.blocksList();
230
+ assert(!list.notModified && ok in list.blocks, "an encrypted value is saved");
231
+ },
232
+ },
233
+ {
234
+ id: "idempotency/unadvertised",
235
+ title: "a request key is refused when the endpoint doesn't advertise receipts",
236
+ async run(ctx) {
237
+ const d = await writable(ctx);
238
+ if (d.writes.idempotency) return ctx.skip("request keys are advertised");
239
+ await expectError(
240
+ ctx.client.blocksApply({ requestKey: key(ctx, "k"), delete: [ctx.name()] }),
241
+ ErrorCode.Unsupported,
242
+ "a request key",
243
+ );
244
+ },
245
+ },
246
+ {
247
+ id: "idempotency/retry",
248
+ title: "retrying with the same key returns the original result, even after the content moved",
249
+ async run(ctx) {
250
+ await withIdempotency(ctx);
251
+ const name = ctx.name("retry");
252
+ const params = { requestKey: key(ctx, "retry"), set: { [name]: { v: 1 } } };
253
+ const first = await ctx.client.blocksApply(params);
254
+ await ctx.client.blocksApply({ set: { [ctx.name("advance")]: { v: 1 } } });
255
+ const second = await ctx.client.blocksApply(params);
256
+ assertEqual(second, first, "the replayed result");
257
+ const list = await ctx.client.blocksList();
258
+ assert(!list.notModified && list.versions[name] === first.versions[name], "written once");
259
+ },
260
+ },
261
+ {
262
+ id: "idempotency/different-params",
263
+ title: "reusing a key for a different request is Invalid params",
264
+ async run(ctx) {
265
+ await withIdempotency(ctx);
266
+ const requestKey = key(ctx, "reuse");
267
+ const name = ctx.name("reuse");
268
+ await ctx.client.blocksApply({ requestKey, set: { [name]: { v: 1 } } });
269
+ await expectError(
270
+ ctx.client.blocksApply({ requestKey, set: { [name]: { v: 2 } } }),
271
+ ErrorCode.InvalidParams,
272
+ "a different body",
273
+ );
274
+ },
275
+ },
276
+ {
277
+ id: "idempotency/simultaneous",
278
+ title: "simultaneous duplicates commit once and share one result",
279
+ async run(ctx) {
280
+ await withIdempotency(ctx);
281
+ const params = { requestKey: key(ctx, "dup"), set: { [ctx.name("dup")]: { v: 1 } } };
282
+ const results = await Promise.all([1, 2, 3].map(() => ctx.client.blocksApply(params)));
283
+ assertEqual(results[1], results[0], "the second result");
284
+ assertEqual(results[2], results[0], "the third result");
285
+ },
286
+ },
287
+ {
288
+ id: "idempotency/restart",
289
+ title: "receipts survive a server restart",
290
+ async run(ctx) {
291
+ await withIdempotency(ctx);
292
+ if (!ctx.options.restart) return ctx.skip("no restart hook");
293
+ const params = { requestKey: key(ctx, "restart"), set: { [ctx.name("restart")]: { v: 1 } } };
294
+ const first = await ctx.client.blocksApply(params);
295
+ await ctx.options.restart();
296
+ assertEqual(await ctx.client.blocksApply(params), first, "the replayed result");
297
+ },
298
+ },
299
+ {
300
+ id: "idempotency/tenant-isolation",
301
+ title: "one tenant's receipts never answer another tenant's requests",
302
+ async run(ctx) {
303
+ await withIdempotency(ctx);
304
+ if (!ctx.options.otherTenant) return ctx.skip("no otherTenant credentials");
305
+ const requestKey = key(ctx, "tenant");
306
+ const a = ctx.name("tenant-a");
307
+ const b = ctx.name("tenant-b");
308
+ await ctx.client.blocksApply({ requestKey, set: { [a]: { v: 1 } } });
309
+ const other = ctx.clientFor(ctx.options.otherTenant);
310
+ const result = await other.blocksApply({ requestKey, set: { [b]: { v: 1 } } });
311
+ assert(typeof result.versions[b] === "string", "the other tenant's write ran");
312
+ },
313
+ },
314
+ ];