@decocms/blocks 8.1.0-next.5 → 8.1.0-next.6
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/protocol/client.d.ts +0 -2
- package/dist/protocol/client.d.ts.map +1 -1
- package/dist/protocol/client.js +0 -2
- package/dist/protocol/client.js.map +1 -1
- package/dist/protocol/conformance/cases/guards.d.ts.map +1 -1
- package/dist/protocol/conformance/cases/guards.js +10 -173
- package/dist/protocol/conformance/cases/guards.js.map +1 -1
- package/dist/protocol/conformance/cases/reads.d.ts.map +1 -1
- package/dist/protocol/conformance/cases/reads.js +4 -49
- 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 +3 -21
- package/dist/protocol/conformance/cases/writes.js.map +1 -1
- package/dist/protocol/conformance/context.d.ts +1 -26
- package/dist/protocol/conformance/context.d.ts.map +1 -1
- package/dist/protocol/conformance/context.js +4 -13
- package/dist/protocol/conformance/context.js.map +1 -1
- package/dist/protocol/conformance/index.js +3 -3
- package/dist/protocol/errors.d.ts +5 -9
- package/dist/protocol/errors.d.ts.map +1 -1
- package/dist/protocol/errors.js +4 -4
- package/dist/protocol/errors.js.map +1 -1
- package/dist/protocol/index.d.ts +1 -1
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +1 -1
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/params.d.ts.map +1 -1
- package/dist/protocol/params.js +0 -4
- package/dist/protocol/params.js.map +1 -1
- package/dist/protocol/server/assets.d.ts +2 -3
- package/dist/protocol/server/assets.d.ts.map +1 -1
- package/dist/protocol/server/assets.js +2 -9
- package/dist/protocol/server/assets.js.map +1 -1
- package/dist/protocol/server/content.d.ts +2 -3
- package/dist/protocol/server/content.d.ts.map +1 -1
- package/dist/protocol/server/content.js +10 -22
- package/dist/protocol/server/content.js.map +1 -1
- package/dist/protocol/server/core.d.ts +8 -11
- package/dist/protocol/server/core.d.ts.map +1 -1
- package/dist/protocol/server/core.js +8 -24
- package/dist/protocol/server/core.js.map +1 -1
- package/dist/protocol/server/handler.d.ts.map +1 -1
- package/dist/protocol/server/handler.js +11 -23
- package/dist/protocol/server/handler.js.map +1 -1
- package/dist/protocol/server/http.d.ts +6 -4
- package/dist/protocol/server/http.d.ts.map +1 -1
- package/dist/protocol/server/http.js +1 -18
- package/dist/protocol/server/http.js.map +1 -1
- package/dist/protocol/server/index.d.ts +0 -1
- package/dist/protocol/server/index.d.ts.map +1 -1
- package/dist/protocol/server/index.js.map +1 -1
- package/dist/protocol/server/methods/apply.d.ts +2 -2
- package/dist/protocol/server/methods/apply.d.ts.map +1 -1
- package/dist/protocol/server/methods/apply.js +23 -71
- package/dist/protocol/server/methods/apply.js.map +1 -1
- package/dist/protocol/server/methods/read.d.ts +0 -6
- package/dist/protocol/server/methods/read.d.ts.map +1 -1
- package/dist/protocol/server/methods/read.js +4 -31
- package/dist/protocol/server/methods/read.js.map +1 -1
- package/dist/protocol/server/rpc.d.ts +1 -1
- package/dist/protocol/server/rpc.d.ts.map +1 -1
- package/dist/protocol/server/rpc.js +9 -39
- package/dist/protocol/server/rpc.js.map +1 -1
- package/dist/protocol/storage/fs/index.d.ts +0 -3
- package/dist/protocol/storage/fs/index.d.ts.map +1 -1
- package/dist/protocol/storage/fs/index.js +0 -5
- package/dist/protocol/storage/fs/index.js.map +1 -1
- package/dist/protocol/storage/memory.d.ts +1 -6
- package/dist/protocol/storage/memory.d.ts.map +1 -1
- package/dist/protocol/storage/memory.js +1 -30
- package/dist/protocol/storage/memory.js.map +1 -1
- package/dist/protocol/storage.d.ts +7 -50
- package/dist/protocol/storage.d.ts.map +1 -1
- package/dist/protocol/storage.js +1 -1
- package/dist/protocol/storage.js.map +1 -1
- package/dist/protocol/types.d.ts +0 -34
- package/dist/protocol/types.d.ts.map +1 -1
- package/dist/protocol/types.js +0 -10
- package/dist/protocol/types.js.map +1 -1
- package/dist/v8/canonical.d.ts +0 -7
- package/dist/v8/canonical.d.ts.map +1 -1
- package/dist/v8/canonical.js +0 -9
- package/dist/v8/canonical.js.map +1 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +2 -6
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +2 -3
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +3 -3
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/draft.d.ts +1 -1
- package/dist/v8/draft.js +2 -2
- package/dist/v8/draft.js.map +1 -1
- package/dist/v8/draftChanges.d.ts +23 -5
- package/dist/v8/draftChanges.d.ts.map +1 -1
- package/dist/v8/draftChanges.js +74 -19
- package/dist/v8/draftChanges.js.map +1 -1
- package/dist/v8/settings.d.ts +0 -7
- package/dist/v8/settings.d.ts.map +1 -1
- package/dist/v8/settings.js +0 -10
- package/dist/v8/settings.js.map +1 -1
- package/dist/v8/telemetry.d.ts +4 -0
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +49 -14
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +4 -7
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/protocol/client.ts +0 -3
- package/src/protocol/conformance/cases/guards.ts +10 -188
- package/src/protocol/conformance/cases/reads.ts +7 -65
- package/src/protocol/conformance/cases/writes.ts +7 -36
- package/src/protocol/conformance/context.ts +6 -31
- package/src/protocol/conformance/index.ts +3 -3
- package/src/protocol/errors.ts +5 -10
- package/src/protocol/index.ts +0 -2
- package/src/protocol/params.ts +0 -4
- package/src/protocol/server/assets.ts +2 -12
- package/src/protocol/server/content.ts +10 -24
- package/src/protocol/server/core.ts +12 -34
- package/src/protocol/server/handler.ts +11 -30
- package/src/protocol/server/http.ts +1 -19
- package/src/protocol/server/index.ts +0 -1
- package/src/protocol/server/methods/apply.ts +22 -82
- package/src/protocol/server/methods/read.ts +4 -36
- package/src/protocol/server/rpc.ts +7 -47
- package/src/protocol/storage/fs/index.ts +0 -8
- package/src/protocol/storage/memory.ts +1 -36
- package/src/protocol/storage.ts +7 -39
- package/src/protocol/types.ts +0 -37
- package/src/v8/canonical.ts +0 -11
- package/src/v8/cms.ts +1 -6
- package/src/v8/content.ts +3 -4
- package/src/v8/draft.ts +2 -2
- package/src/v8/draftChanges.ts +81 -26
- package/src/v8/settings.ts +1 -15
- package/src/v8/telemetry.ts +58 -13
- package/src/v8/types.ts +4 -7
- package/dist/protocol/server/auth.d.ts +0 -31
- package/dist/protocol/server/auth.d.ts.map +0 -1
- package/dist/protocol/server/auth.js +0 -32
- package/dist/protocol/server/auth.js.map +0 -1
- package/src/protocol/server/auth.ts +0 -49
|
@@ -1,24 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `describe`, `schema.get` and `blocks.list`, including conditional reads.
|
|
3
3
|
*/
|
|
4
|
-
import { ErrorCode } from "../../errors.ts";
|
|
5
4
|
import { PROTOCOL_NAME, SCHEMA_FORMAT } from "../../types.ts";
|
|
6
|
-
import { assert, assertEqual
|
|
5
|
+
import { assert, assertEqual } from "../context.ts";
|
|
7
6
|
import type { ConformanceCase } from "./types.ts";
|
|
8
7
|
|
|
9
|
-
const LIMIT_KEYS = [
|
|
10
|
-
"maxOpsPerApply",
|
|
11
|
-
"maxBlockBytes",
|
|
12
|
-
"maxRequestBytes",
|
|
13
|
-
"maxListBytes",
|
|
14
|
-
"maxSchemaBytes",
|
|
15
|
-
"maxBatchResponseBytes",
|
|
16
|
-
] as const;
|
|
17
|
-
|
|
18
8
|
export const readCases: ConformanceCase[] = [
|
|
19
9
|
{
|
|
20
10
|
id: "describe/shape",
|
|
21
|
-
title: "describe reports the protocol, its version, kind, root
|
|
11
|
+
title: "describe reports the protocol, its version, kind, root and features",
|
|
22
12
|
async run(ctx) {
|
|
23
13
|
const d = await ctx.client.describe();
|
|
24
14
|
assertEqual(d.protocol, PROTOCOL_NAME, "protocol");
|
|
@@ -29,14 +19,7 @@ export const readCases: ConformanceCase[] = [
|
|
|
29
19
|
assert(typeof d.readOnly === "boolean", "readOnly");
|
|
30
20
|
assert(typeof d.root === "string" && !d.root.startsWith("/"), "root is relative");
|
|
31
21
|
assertEqual(d.schemaFormat, SCHEMA_FORMAT, "schemaFormat");
|
|
32
|
-
assert(d.refs === null || typeof d.refs.default === "string", "refs");
|
|
33
|
-
assert(
|
|
34
|
-
d.writes.idempotency === null || d.writes.idempotency.retentionMs > 0,
|
|
35
|
-
"writes.idempotency",
|
|
36
|
-
);
|
|
37
|
-
assert(typeof d.writes.schemaPreconditions === "boolean", "writes.schemaPreconditions");
|
|
38
22
|
assert(d.pollIntervalMs > 0, "pollIntervalMs");
|
|
39
|
-
for (const key of LIMIT_KEYS) assert(d.limits[key] > 0, `limits.${key}`);
|
|
40
23
|
assert(
|
|
41
24
|
d.preview === null || /^https?:\/\//.test(String(d.preview?.url)),
|
|
42
25
|
"preview is null or { url } with an http(s) URL",
|
|
@@ -80,13 +63,12 @@ export const readCases: ConformanceCase[] = [
|
|
|
80
63
|
async run(ctx) {
|
|
81
64
|
if (ctx.options.hasSchema !== false) return ctx.skip("the endpoint has a schema");
|
|
82
65
|
const result = await ctx.client.schemaGet();
|
|
83
|
-
const d = await ctx.describe();
|
|
84
66
|
assertEqual(
|
|
85
67
|
result,
|
|
86
68
|
{
|
|
87
69
|
notModified: false,
|
|
88
70
|
version: null,
|
|
89
|
-
resolvedRef:
|
|
71
|
+
resolvedRef: result.notModified ? null : result.resolvedRef,
|
|
90
72
|
schema: null,
|
|
91
73
|
},
|
|
92
74
|
"the no-schema result",
|
|
@@ -108,8 +90,10 @@ export const readCases: ConformanceCase[] = [
|
|
|
108
90
|
Object.keys(first.blocks).sort(),
|
|
109
91
|
"one version per entry",
|
|
110
92
|
);
|
|
111
|
-
|
|
112
|
-
|
|
93
|
+
assert(
|
|
94
|
+
first.resolvedRef === null || typeof first.resolvedRef === "string",
|
|
95
|
+
"resolvedRef is a branch or null",
|
|
96
|
+
);
|
|
113
97
|
const again = await ctx.client.blocksList({ ifNoneMatch: first.revision });
|
|
114
98
|
assertEqual(
|
|
115
99
|
again,
|
|
@@ -141,46 +125,4 @@ export const readCases: ConformanceCase[] = [
|
|
|
141
125
|
}
|
|
142
126
|
},
|
|
143
127
|
},
|
|
144
|
-
{
|
|
145
|
-
id: "refs/unsupported",
|
|
146
|
-
title: "a ref on an endpoint without branches is Unsupported",
|
|
147
|
-
async run(ctx) {
|
|
148
|
-
const d = await ctx.describe();
|
|
149
|
-
if (d.refs !== null) return ctx.skip("the endpoint has branches");
|
|
150
|
-
await expectError(
|
|
151
|
-
ctx.client.blocksList({ ref: "main" }),
|
|
152
|
-
ErrorCode.Unsupported,
|
|
153
|
-
"blocks.list ref",
|
|
154
|
-
);
|
|
155
|
-
await expectError(
|
|
156
|
-
ctx.client.schemaGet({ ref: "main" }),
|
|
157
|
-
ErrorCode.Unsupported,
|
|
158
|
-
"schema.get ref",
|
|
159
|
-
);
|
|
160
|
-
if (!d.readOnly) {
|
|
161
|
-
await expectError(
|
|
162
|
-
ctx.client.blocksApply({ ref: "main", delete: [ctx.name()] }),
|
|
163
|
-
ErrorCode.Unsupported,
|
|
164
|
-
"blocks.apply ref",
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
|
-
},
|
|
169
|
-
{
|
|
170
|
-
id: "auth/token",
|
|
171
|
-
title: "a missing or wrong bearer token is HTTP 401 with Unauthorized",
|
|
172
|
-
async run(ctx) {
|
|
173
|
-
if (ctx.options.token === undefined) return ctx.skip("the endpoint has no token");
|
|
174
|
-
const body = JSON.stringify({ jsonrpc: "2.0", id: 1, method: "describe" });
|
|
175
|
-
for (const authorization of ["", "Bearer wrong-token"]) {
|
|
176
|
-
const response = await ctx.raw(body, { headers: { authorization } });
|
|
177
|
-
assertEqual(response.status, 401, `HTTP status with "${authorization}"`);
|
|
178
|
-
assertEqual(
|
|
179
|
-
(response.body as { error?: { code: number } }).error?.code,
|
|
180
|
-
ErrorCode.Unauthorized,
|
|
181
|
-
"the error code",
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
},
|
|
185
|
-
},
|
|
186
128
|
];
|
|
@@ -273,48 +273,19 @@ export const writeCases: ConformanceCase[] = [
|
|
|
273
273
|
assert(!(from in after.blocks) && to in after.blocks, "the entry moved");
|
|
274
274
|
},
|
|
275
275
|
},
|
|
276
|
-
{
|
|
277
|
-
id: "apply/schema-precondition",
|
|
278
|
-
title: "ifSchemaMatch rejects a write when the schema changed",
|
|
279
|
-
async run(ctx) {
|
|
280
|
-
const d = await writable(ctx);
|
|
281
|
-
const name = ctx.name("schema-guarded");
|
|
282
|
-
if (!d.writes.schemaPreconditions) {
|
|
283
|
-
await expectError(
|
|
284
|
-
ctx.client.blocksApply({ set: { [name]: {} }, ifSchemaMatch: "x" }),
|
|
285
|
-
ErrorCode.Unsupported,
|
|
286
|
-
"an unadvertised guard",
|
|
287
|
-
);
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
if (ctx.options.hasSchema === false) return ctx.skip("no schema");
|
|
291
|
-
const { version } = await ctx.client.schemaGet();
|
|
292
|
-
assert(version !== null, "the endpoint has a schema");
|
|
293
|
-
const error = await expectError(
|
|
294
|
-
ctx.client.blocksApply({ set: { [name]: {} }, ifSchemaMatch: `${version}-old` }),
|
|
295
|
-
ErrorCode.Conflict,
|
|
296
|
-
"a stale schema version",
|
|
297
|
-
);
|
|
298
|
-
assertEqual(
|
|
299
|
-
(error.data as { schema?: unknown }).schema,
|
|
300
|
-
{ expected: `${version}-old`, actual: version },
|
|
301
|
-
"the conflict's schema versions",
|
|
302
|
-
);
|
|
303
|
-
assert(!(name in (await list(ctx)).blocks), "nothing was written");
|
|
304
|
-
await ctx.client.blocksApply({ set: { [name]: {} }, ifSchemaMatch: version });
|
|
305
|
-
},
|
|
306
|
-
},
|
|
307
276
|
{
|
|
308
277
|
id: "apply/unknown-params",
|
|
309
278
|
title: "an unknown parameter is refused, so an unknown guard never becomes an unguarded write",
|
|
310
279
|
async run(ctx) {
|
|
311
280
|
await writable(ctx);
|
|
312
281
|
const name = ctx.name("unknown-param");
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
282
|
+
for (const param of ["ifUnmodifiedSince", "ifSchemaMatch", "requestKey", "ref"]) {
|
|
283
|
+
await expectError(
|
|
284
|
+
ctx.client.call("blocks.apply", { set: { [name]: {} }, [param]: "x" } as never),
|
|
285
|
+
ErrorCode.InvalidParams,
|
|
286
|
+
`the unknown parameter ${param}`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
318
289
|
assert(!(name in (await list(ctx)).blocks), "nothing was written");
|
|
319
290
|
},
|
|
320
291
|
},
|
|
@@ -7,12 +7,13 @@ import { type ContentClient, createContentClient, readResponseJson } from "../cl
|
|
|
7
7
|
import { ContentProtocolError } from "../errors.ts";
|
|
8
8
|
import type { DescribeResult } from "../types.ts";
|
|
9
9
|
|
|
10
|
+
/** Names deleted per `blocks.apply` when a case cleans up. */
|
|
11
|
+
const CLEANUP_CHUNK = 100;
|
|
12
|
+
|
|
10
13
|
export interface ConformanceOptions {
|
|
11
14
|
/** The endpoint URL, such as `http://localhost:4545/rpc`. */
|
|
12
15
|
endpoint: string | URL;
|
|
13
|
-
/**
|
|
14
|
-
token?: string;
|
|
15
|
-
/** Extra headers on every request (an auth cookie, a tenant header). */
|
|
16
|
+
/** Extra headers on every request (such as the host's auth cookie). */
|
|
16
17
|
headers?: Record<string, string>;
|
|
17
18
|
/** A fetch implementation (defaults to the global one); use it to call a handler in process. */
|
|
18
19
|
fetch?: (request: Request) => Promise<Response>;
|
|
@@ -25,26 +26,11 @@ export interface ConformanceOptions {
|
|
|
25
26
|
secretsPublicKey?: string;
|
|
26
27
|
/** Whether the endpoint has a schema (default true). `false` checks the `schema: null` result instead. */
|
|
27
28
|
hasSchema?: boolean;
|
|
28
|
-
/**
|
|
29
|
-
* The endpoint serves a schema larger than its `limits.maxSchemaBytes`, so
|
|
30
|
-
* reading it and writes that depend on it must be LimitExceeded. Run such
|
|
31
|
-
* an endpoint with only the `limits/schema-bytes` case (pass a filter to
|
|
32
|
-
* `runConformance` or `defineConformanceSuite`): every other case expects
|
|
33
|
-
* a schema it can read.
|
|
34
|
-
*/
|
|
35
|
-
schemaOverLimit?: boolean;
|
|
36
29
|
/**
|
|
37
30
|
* The URL prefix uploads are served under (`PUT <assetsEndpoint><name>`),
|
|
38
31
|
* such as `http://localhost:4545/assets/`. Leave out to skip the upload cases.
|
|
39
32
|
*/
|
|
40
33
|
assetsEndpoint?: string | URL;
|
|
41
|
-
/**
|
|
42
|
-
* Restarts the server, keeping its durable state, to check that request-key
|
|
43
|
-
* receipts survive a restart. Leave out to skip that case.
|
|
44
|
-
*/
|
|
45
|
-
restart?: () => Promise<void>;
|
|
46
|
-
/** Credentials of another tenant on the same storage, to check receipt isolation. */
|
|
47
|
-
otherTenant?: { token?: string; headers?: Record<string, string> };
|
|
48
34
|
/** Prefix of every name the suite creates (default `conformance-<random>`). */
|
|
49
35
|
namePrefix?: string;
|
|
50
36
|
}
|
|
@@ -83,11 +69,6 @@ export class ConformanceContext {
|
|
|
83
69
|
this.client = createContentClient(options);
|
|
84
70
|
}
|
|
85
71
|
|
|
86
|
-
/** A client with other credentials (for tenant isolation). */
|
|
87
|
-
clientFor(credentials: { token?: string; headers?: Record<string, string> }): ContentClient {
|
|
88
|
-
return createContentClient({ ...this.options, ...credentials });
|
|
89
|
-
}
|
|
90
|
-
|
|
91
72
|
async describe(): Promise<DescribeResult> {
|
|
92
73
|
this.describeResult ??= await this.client.describe();
|
|
93
74
|
return this.describeResult;
|
|
@@ -112,10 +93,8 @@ export class ConformanceContext {
|
|
|
112
93
|
this.created.clear();
|
|
113
94
|
const description = await this.describe();
|
|
114
95
|
if (description.readOnly) return;
|
|
115
|
-
for (let i = 0; i < names.length; i +=
|
|
116
|
-
await this.client
|
|
117
|
-
.blocksApply({ delete: names.slice(i, i + description.limits.maxOpsPerApply) })
|
|
118
|
-
.catch(() => {});
|
|
96
|
+
for (let i = 0; i < names.length; i += CLEANUP_CHUNK) {
|
|
97
|
+
await this.client.blocksApply({ delete: names.slice(i, i + CLEANUP_CHUNK) }).catch(() => {});
|
|
119
98
|
}
|
|
120
99
|
}
|
|
121
100
|
|
|
@@ -133,9 +112,6 @@ export class ConformanceContext {
|
|
|
133
112
|
...this.options.headers,
|
|
134
113
|
...init.headers,
|
|
135
114
|
};
|
|
136
|
-
if (this.options.token !== undefined && !("authorization" in headers)) {
|
|
137
|
-
headers.authorization = `Bearer ${this.options.token}`;
|
|
138
|
-
}
|
|
139
115
|
for (const [key, value] of Object.entries(headers)) if (value === "") delete headers[key];
|
|
140
116
|
const method = init.method ?? "POST";
|
|
141
117
|
const doFetch = this.options.fetch ?? ((request: Request) => fetch(request));
|
|
@@ -163,7 +139,6 @@ export class ConformanceContext {
|
|
|
163
139
|
...this.options.headers,
|
|
164
140
|
"content-type": contentType,
|
|
165
141
|
};
|
|
166
|
-
if (this.options.token !== undefined) headers.authorization = `Bearer ${this.options.token}`;
|
|
167
142
|
const doFetch = this.options.fetch ?? ((request: Request) => fetch(request));
|
|
168
143
|
const url = new URL(encodeURIComponent(name), new URL(String(base)).href.replace(/\/?$/, "/"));
|
|
169
144
|
const response = await doFetch(
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
* import { describe, it } from "vitest";
|
|
11
11
|
* import { defineConformanceSuite } from "@decocms/blocks/protocol/conformance";
|
|
12
12
|
*
|
|
13
|
-
* defineConformanceSuite({ describe, it }, { endpoint: "http://localhost:4545/rpc"
|
|
13
|
+
* defineConformanceSuite({ describe, it }, { endpoint: "http://localhost:4545/rpc" });
|
|
14
14
|
* ```
|
|
15
15
|
*
|
|
16
16
|
* or collect a report with `runConformance(options)`. The suite writes only
|
|
17
17
|
* names under its own prefix and deletes them after each case (uploads, which
|
|
18
18
|
* the protocol can't delete, stay in the asset folder). Cases that
|
|
19
|
-
* don't apply to an endpoint (a read-only one, one without
|
|
20
|
-
*
|
|
19
|
+
* don't apply to an endpoint (a read-only one, one without a schema) skip
|
|
20
|
+
* themselves. Browser-safe: it only needs `fetch`.
|
|
21
21
|
*/
|
|
22
22
|
import { assetCases } from "./cases/assets.ts";
|
|
23
23
|
import { guardCases } from "./cases/guards.ts";
|
package/src/protocol/errors.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Content-protocol errors. Every failure travels as a JSON-RPC 2.0 `error`
|
|
3
|
-
* object (HTTP 200), except a
|
|
4
|
-
*
|
|
3
|
+
* object (HTTP 200), except a body over the size limit (413), which applies
|
|
4
|
+
* to the whole batch. `Unauthorized` and `Forbidden` are for the server that
|
|
5
|
+
* mounts the handler and authenticates its callers (Studio's session); the
|
|
6
|
+
* handler itself never raises them.
|
|
5
7
|
*/
|
|
6
8
|
|
|
7
9
|
export const ErrorCode = {
|
|
@@ -41,8 +43,7 @@ export interface VersionMismatch {
|
|
|
41
43
|
|
|
42
44
|
/** `error.data` of a Conflict (-32002). */
|
|
43
45
|
export interface ConflictData {
|
|
44
|
-
entries
|
|
45
|
-
schema?: { expected: string; actual: string | null };
|
|
46
|
+
entries: Record<string, VersionMismatch>;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
/** One rule a `blocks.apply` breaks. */
|
|
@@ -134,9 +135,3 @@ export const parseError = () => new ContentProtocolError(ErrorCode.ParseError, "
|
|
|
134
135
|
|
|
135
136
|
export const internalError = () =>
|
|
136
137
|
new ContentProtocolError(ErrorCode.InternalError, "internal error");
|
|
137
|
-
|
|
138
|
-
export const unauthorized = () =>
|
|
139
|
-
new ContentProtocolError(ErrorCode.Unauthorized, "missing or invalid bearer token");
|
|
140
|
-
|
|
141
|
-
export const forbidden = () =>
|
|
142
|
-
new ContentProtocolError(ErrorCode.Forbidden, "not allowed for this project");
|
package/src/protocol/index.ts
CHANGED
package/src/protocol/params.ts
CHANGED
|
@@ -13,14 +13,10 @@ import type { BlocksApplyParams, MethodName, ReadParams } from "./types.ts";
|
|
|
13
13
|
const opaque = z.string().min(1).max(1024);
|
|
14
14
|
|
|
15
15
|
const readParams = z.strictObject({
|
|
16
|
-
ref: z.string().min(1).max(255).optional(),
|
|
17
16
|
ifNoneMatch: opaque.optional(),
|
|
18
17
|
});
|
|
19
18
|
|
|
20
19
|
const applyParams = z.strictObject({
|
|
21
|
-
ref: z.string().min(1).max(255).optional(),
|
|
22
|
-
requestKey: z.string().min(1).max(256).optional(),
|
|
23
|
-
ifSchemaMatch: opaque.optional(),
|
|
24
20
|
// Entry shapes are checked later and reported as InvalidBlock, all at once.
|
|
25
21
|
set: z.record(z.string(), z.unknown()).optional(),
|
|
26
22
|
delete: z.array(z.string()).optional(),
|
|
@@ -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
|
|
5
|
+
* `PUT /assets/<name>`; 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
|
|
@@ -21,21 +21,16 @@ import {
|
|
|
21
21
|
import {
|
|
22
22
|
type ContentProtocolError,
|
|
23
23
|
ErrorCode,
|
|
24
|
-
forbidden,
|
|
25
24
|
invalidRequest,
|
|
26
25
|
limitExceeded,
|
|
27
26
|
readOnly,
|
|
28
|
-
unauthorized,
|
|
29
27
|
unsupported,
|
|
30
28
|
} from "../errors.ts";
|
|
31
29
|
import type { ContentStorage } from "../storage.ts";
|
|
32
30
|
import { ASSETS_URL_PREFIX } from "../types.ts";
|
|
33
|
-
import { type AuthOptions, assertAuthOptions, authenticate } from "./auth.ts";
|
|
34
31
|
import { BodyEncodingError, BodyTooLargeError, jsonResponse, readBody } from "./http.ts";
|
|
35
32
|
|
|
36
33
|
const STATUS: Record<number, number> = {
|
|
37
|
-
[ErrorCode.Unauthorized]: 401,
|
|
38
|
-
[ErrorCode.Forbidden]: 403,
|
|
39
34
|
[ErrorCode.ReadOnly]: 403,
|
|
40
35
|
[ErrorCode.Unsupported]: 404,
|
|
41
36
|
[ErrorCode.LimitExceeded]: 413,
|
|
@@ -44,13 +39,12 @@ const STATUS: Record<number, number> = {
|
|
|
44
39
|
|
|
45
40
|
export type AssetHandler = (request: Request) => Promise<Response>;
|
|
46
41
|
|
|
47
|
-
export type AssetHandlerOptions =
|
|
42
|
+
export type AssetHandlerOptions = AssetTypeOptions;
|
|
48
43
|
|
|
49
44
|
export function createAssetHandler(
|
|
50
45
|
storage: ContentStorage,
|
|
51
46
|
options: AssetHandlerOptions = {},
|
|
52
47
|
): AssetHandler {
|
|
53
|
-
assertAuthOptions(options);
|
|
54
48
|
const fail = (request: Request, error: ContentProtocolError, status?: number) =>
|
|
55
49
|
jsonResponse(
|
|
56
50
|
request,
|
|
@@ -69,10 +63,6 @@ export function createAssetHandler(
|
|
|
69
63
|
},
|
|
70
64
|
);
|
|
71
65
|
}
|
|
72
|
-
const auth = await authenticate(request, options);
|
|
73
|
-
if (!auth.ok)
|
|
74
|
-
return fail(request, auth.reason === "unauthorized" ? unauthorized() : forbidden());
|
|
75
|
-
|
|
76
66
|
const description = await storage.describe();
|
|
77
67
|
if (description.readOnly) return fail(request, readOnly());
|
|
78
68
|
if (description.assets === null || !storage.putAsset) {
|
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
* Shared by `blocks.list` (which needs every body) and `blocks.apply` (which
|
|
5
5
|
* needs bodies only to break ties between spellings).
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import { unavailable } from "../errors.ts";
|
|
8
8
|
import { entryHasPath, fullyDecodeFileName, isBlockFileName, resolveSpellings } from "../keys.ts";
|
|
9
9
|
import type { ContentStorage, StorageFile, StorageSnapshot } from "../storage.ts";
|
|
10
|
-
import type { Diagnostic
|
|
10
|
+
import type { Diagnostic } from "../types.ts";
|
|
11
11
|
import { type BodyCache, type ParsedBody, parseBody } from "./bodyCache.ts";
|
|
12
|
+
import { MAX_BLOCK_BYTES } from "./core.ts";
|
|
12
13
|
|
|
13
14
|
export interface LoadedEntry {
|
|
14
15
|
file: string;
|
|
@@ -38,14 +39,13 @@ export interface LoadedContent {
|
|
|
38
39
|
interface Options {
|
|
39
40
|
/** Read every body (`blocks.list`), not only the ones that break ties. */
|
|
40
41
|
readAll: boolean;
|
|
41
|
-
limits: Limits;
|
|
42
42
|
cache: BodyCache;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
async function loadContent(
|
|
46
46
|
storage: ContentStorage,
|
|
47
47
|
snapshot: StorageSnapshot,
|
|
48
|
-
{ readAll,
|
|
48
|
+
{ readAll, cache }: Options,
|
|
49
49
|
): Promise<LoadedContent> {
|
|
50
50
|
const files = snapshot.files.filter((f) => isBlockFileName(f.file));
|
|
51
51
|
const groups = new Map<string, StorageFile[]>();
|
|
@@ -60,35 +60,27 @@ async function loadContent(
|
|
|
60
60
|
let moved = false;
|
|
61
61
|
const parsed = new Map<string, ParsedBody>();
|
|
62
62
|
const toRead: StorageFile[] = [];
|
|
63
|
-
let knownBytes = 0;
|
|
64
63
|
for (const group of groups.values()) {
|
|
65
64
|
if (!readAll && group.length < 2) continue;
|
|
66
65
|
for (const file of group) {
|
|
67
|
-
// An existing file over
|
|
66
|
+
// An existing file over MAX_BLOCK_BYTES is unreadable, like invalid JSON: it's
|
|
68
67
|
// left out of the map and reported as a `too-large` diagnostic, so one
|
|
69
68
|
// oversized hand edit can't take the whole editor down. The map plus its
|
|
70
|
-
// diagnostics still accounts for every file in the snapshot
|
|
71
|
-
|
|
72
|
-
if (file.size !== undefined && file.size > limits.maxBlockBytes) {
|
|
69
|
+
// diagnostics still accounts for every file in the snapshot.
|
|
70
|
+
if (file.size !== undefined && file.size > MAX_BLOCK_BYTES) {
|
|
73
71
|
parsed.set(file.file, {
|
|
74
72
|
ok: false,
|
|
75
73
|
kind: "too-large",
|
|
76
74
|
bytes: file.size,
|
|
77
|
-
message: `the file is over ${
|
|
75
|
+
message: `the file is over ${MAX_BLOCK_BYTES} bytes`,
|
|
78
76
|
});
|
|
79
77
|
continue;
|
|
80
78
|
}
|
|
81
|
-
knownBytes += file.size ?? 0;
|
|
82
79
|
const hit = cache.get(file.file, file.version);
|
|
83
80
|
if (hit) parsed.set(file.file, hit);
|
|
84
81
|
else toRead.push(file);
|
|
85
82
|
}
|
|
86
83
|
}
|
|
87
|
-
if (readAll && knownBytes > limits.maxListBytes) {
|
|
88
|
-
throw limitExceeded(`the block list is over ${limits.maxListBytes} bytes`, {
|
|
89
|
-
limit: "maxListBytes",
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
84
|
if (toRead.length > 0) {
|
|
93
85
|
const bodies = await storage.readFiles(
|
|
94
86
|
snapshot,
|
|
@@ -100,7 +92,7 @@ async function loadContent(
|
|
|
100
92
|
moved = true; // vanished since the snapshot
|
|
101
93
|
continue;
|
|
102
94
|
}
|
|
103
|
-
const body = parseBody(read.text,
|
|
95
|
+
const body = parseBody(read.text, MAX_BLOCK_BYTES);
|
|
104
96
|
// Cache under the version of the bytes actually read, never the snapshot's.
|
|
105
97
|
cache.set(file.file, read.version, body);
|
|
106
98
|
if (read.version !== file.version) {
|
|
@@ -127,11 +119,6 @@ async function loadContent(
|
|
|
127
119
|
bytes += body.bytes;
|
|
128
120
|
candidates.push({ ...file, hasPath: entryHasPath(body.value), value: body.value });
|
|
129
121
|
}
|
|
130
|
-
if (readAll && bytes > limits.maxListBytes) {
|
|
131
|
-
throw limitExceeded(`the block list is over ${limits.maxListBytes} bytes`, {
|
|
132
|
-
limit: "maxListBytes",
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
122
|
|
|
136
123
|
const entries = new Map<string, LoadedEntry>();
|
|
137
124
|
for (const { name, winner, shadowed } of resolveSpellings(candidates).values()) {
|
|
@@ -161,12 +148,11 @@ const MAX_SNAPSHOT_READS = 3;
|
|
|
161
148
|
*/
|
|
162
149
|
export async function loadCurrentContent(
|
|
163
150
|
storage: ContentStorage,
|
|
164
|
-
ref: string | undefined,
|
|
165
151
|
options: Options,
|
|
166
152
|
shortCircuit?: (snapshot: StorageSnapshot) => boolean,
|
|
167
153
|
): Promise<{ snapshot: StorageSnapshot; content: LoadedContent | null }> {
|
|
168
154
|
for (let read = 1; read <= MAX_SNAPSHOT_READS; read++) {
|
|
169
|
-
const snapshot = await storage.snapshot(
|
|
155
|
+
const snapshot = await storage.snapshot();
|
|
170
156
|
if (shortCircuit?.(snapshot)) return { snapshot, content: null };
|
|
171
157
|
const content = await loadContent(storage, snapshot, options);
|
|
172
158
|
if (!content.moved) return { snapshot, content };
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The shared state of one content handler: its storage, options and caches.
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
ContentProtocolError,
|
|
6
|
-
invalidBlock,
|
|
7
|
-
notFound,
|
|
8
|
-
unavailable,
|
|
9
|
-
unsupported,
|
|
10
|
-
} from "../errors.ts";
|
|
4
|
+
import { ContentProtocolError, invalidBlock, notFound, unavailable } from "../errors.ts";
|
|
11
5
|
import { blockNameFromFile } from "../keys.ts";
|
|
12
6
|
import {
|
|
13
7
|
type ContentStorage,
|
|
@@ -16,15 +10,21 @@ import {
|
|
|
16
10
|
StorageNotFoundError,
|
|
17
11
|
StorageUnavailableError,
|
|
18
12
|
} from "../storage.ts";
|
|
19
|
-
import {
|
|
20
|
-
import type { AuthOptions } from "./auth.ts";
|
|
13
|
+
import type { DecoMeta } from "../types.ts";
|
|
21
14
|
import { BodyCache } from "./bodyCache.ts";
|
|
22
15
|
|
|
23
|
-
|
|
16
|
+
const MiB = 1024 * 1024;
|
|
17
|
+
|
|
18
|
+
/** `blocks.apply`'s own guards: names (set plus delete) in one call. Internal, not advertised. */
|
|
19
|
+
export const MAX_OPS_PER_APPLY = 500;
|
|
20
|
+
/** Bytes of one stored entry. */
|
|
21
|
+
export const MAX_BLOCK_BYTES = 1 * MiB;
|
|
22
|
+
/** Bytes of one HTTP request body, uncompressed. */
|
|
23
|
+
export const MAX_REQUEST_BYTES = 8 * MiB;
|
|
24
|
+
|
|
25
|
+
export interface ContentHandlerOptions {
|
|
24
26
|
/** Reported by `describe`, such as `{ name: "deco-cli", version: "8.0.0" }`. */
|
|
25
27
|
server?: { name: string; version: string };
|
|
26
|
-
/** Lowers the protocol's default limits. Higher values are ignored. */
|
|
27
|
-
limits?: Partial<Limits>;
|
|
28
28
|
/** Overrides the poll interval the storage suggests. */
|
|
29
29
|
pollIntervalMs?: number;
|
|
30
30
|
/** The app the site editor previews, reported by `describe` (`{ url }`). */
|
|
@@ -48,8 +48,6 @@ const DEFAULT_RETRY_DELAY = { minMs: 50, maxMs: 200 };
|
|
|
48
48
|
|
|
49
49
|
export class Core {
|
|
50
50
|
readonly cache: BodyCache;
|
|
51
|
-
/** Requests in flight per scoped request key, so simultaneous duplicates commit once. */
|
|
52
|
-
readonly inflight = new Map<string, Promise<unknown>>();
|
|
53
51
|
/** The last parsed schema, by version: the secret guard reads it on every write. */
|
|
54
52
|
parsedSchema: { version: string; meta: DecoMeta } | null = null;
|
|
55
53
|
|
|
@@ -75,31 +73,11 @@ export class Core {
|
|
|
75
73
|
return this.storage.describe();
|
|
76
74
|
}
|
|
77
75
|
|
|
78
|
-
/** The effective limits: the lowest of the defaults, the storage's and the options'. */
|
|
79
|
-
limits(description: StorageDescription): Limits {
|
|
80
|
-
const out = { ...DEFAULT_LIMITS };
|
|
81
|
-
for (const source of [description.limits, this.options.limits]) {
|
|
82
|
-
if (!source) continue;
|
|
83
|
-
for (const key of Object.keys(out) as (keyof Limits)[]) {
|
|
84
|
-
const value = source[key];
|
|
85
|
-
if (typeof value === "number" && value > 0 && value < out[key]) out[key] = value;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return out;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
76
|
pollIntervalMs(description: StorageDescription): number {
|
|
92
77
|
return (
|
|
93
78
|
this.options.pollIntervalMs ?? description.pollIntervalMs ?? DEFAULT_POLL_MS[description.kind]
|
|
94
79
|
);
|
|
95
80
|
}
|
|
96
|
-
|
|
97
|
-
/** Refuses a `ref` on a storage without branches. */
|
|
98
|
-
checkRef(description: StorageDescription, ref: string | undefined): void {
|
|
99
|
-
if (ref !== undefined && description.refs === null) {
|
|
100
|
-
throw unsupported("this endpoint has no branches; omit ref");
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
81
|
}
|
|
104
82
|
|
|
105
83
|
/** Maps storage errors to protocol errors; anything unknown is rethrown. */
|
|
@@ -4,29 +4,21 @@
|
|
|
4
4
|
*
|
|
5
5
|
* `POST <endpoint>` with `Content-Type: application/json` and a body that's
|
|
6
6
|
* one request object or a batch array. Errors come back in the JSON-RPC
|
|
7
|
-
* `error` object with HTTP 200, except a
|
|
8
|
-
* and
|
|
9
|
-
* requests that aren't the protocol at all: a method other than POST (405),
|
|
7
|
+
* `error` object with HTTP 200, except a body over the size limit (413),
|
|
8
|
+
* which applies to the whole batch, and requests that aren't the protocol at all: a method other than POST (405),
|
|
10
9
|
* or a body that isn't JSON or uses an unsupported encoding (415). Those carry
|
|
11
10
|
* a JSON-RPC error body too, with a `null` id.
|
|
12
11
|
* Responses are gzip-compressed when the request accepts it.
|
|
13
12
|
*
|
|
14
13
|
* The handler is path-agnostic: mount it where the endpoint lives (`/rpc` on
|
|
15
|
-
* the local server).
|
|
16
|
-
*
|
|
17
|
-
* that mounts it
|
|
14
|
+
* the local server). The handler doesn't authenticate: who may call it, and
|
|
15
|
+
* transport security that depends on where it runs — CORS, `Host` checks,
|
|
16
|
+
* Chrome's local-network preflight — belong to the server that mounts it
|
|
17
|
+
* (`deco serve` on loopback; Studio behind its own session).
|
|
18
18
|
*/
|
|
19
|
-
import {
|
|
20
|
-
type ContentProtocolError,
|
|
21
|
-
forbidden,
|
|
22
|
-
invalidRequest,
|
|
23
|
-
limitExceeded,
|
|
24
|
-
parseError,
|
|
25
|
-
unauthorized,
|
|
26
|
-
} from "../errors.ts";
|
|
19
|
+
import { type ContentProtocolError, invalidRequest, limitExceeded, parseError } from "../errors.ts";
|
|
27
20
|
import type { ContentStorage } from "../storage.ts";
|
|
28
|
-
import {
|
|
29
|
-
import { type ContentHandlerOptions, Core } from "./core.ts";
|
|
21
|
+
import { type ContentHandlerOptions, Core, MAX_REQUEST_BYTES } from "./core.ts";
|
|
30
22
|
import {
|
|
31
23
|
BodyEncodingError,
|
|
32
24
|
BodyTooLargeError,
|
|
@@ -45,21 +37,12 @@ export function createContentHandler(
|
|
|
45
37
|
storage: ContentStorage,
|
|
46
38
|
options: ContentHandlerOptions = {},
|
|
47
39
|
): ContentHandler {
|
|
48
|
-
assertAuthOptions(options);
|
|
49
40
|
const core = new Core(storage, options);
|
|
50
41
|
|
|
51
42
|
return async (request) => {
|
|
52
43
|
if (request.method !== "POST") {
|
|
53
44
|
return jsonResponse(request, errorBody(invalidRequest("use POST")), 405, { allow: "POST" });
|
|
54
45
|
}
|
|
55
|
-
const auth = await authenticate(request, options);
|
|
56
|
-
if (!auth.ok) {
|
|
57
|
-
return auth.reason === "unauthorized"
|
|
58
|
-
? jsonResponse(request, errorBody(unauthorized()), 401, {
|
|
59
|
-
"www-authenticate": 'Bearer realm="deco-content"',
|
|
60
|
-
})
|
|
61
|
-
: jsonResponse(request, errorBody(forbidden()));
|
|
62
|
-
}
|
|
63
46
|
if (!isJsonContentType(request)) {
|
|
64
47
|
return jsonResponse(
|
|
65
48
|
request,
|
|
@@ -68,16 +51,15 @@ export function createContentHandler(
|
|
|
68
51
|
);
|
|
69
52
|
}
|
|
70
53
|
|
|
71
|
-
const limits = core.limits(await core.description());
|
|
72
54
|
let bytes: Uint8Array;
|
|
73
55
|
try {
|
|
74
|
-
bytes = await readBody(request,
|
|
56
|
+
bytes = await readBody(request, MAX_REQUEST_BYTES);
|
|
75
57
|
} catch (error) {
|
|
76
58
|
if (error instanceof BodyTooLargeError) {
|
|
77
59
|
return jsonResponse(
|
|
78
60
|
request,
|
|
79
61
|
errorBody(
|
|
80
|
-
limitExceeded(`the request body is over ${
|
|
62
|
+
limitExceeded(`the request body is over ${MAX_REQUEST_BYTES} bytes`, {
|
|
81
63
|
limit: "maxRequestBytes",
|
|
82
64
|
}),
|
|
83
65
|
),
|
|
@@ -96,7 +78,6 @@ export function createContentHandler(
|
|
|
96
78
|
} catch {
|
|
97
79
|
return jsonResponse(request, errorBody(parseError()));
|
|
98
80
|
}
|
|
99
|
-
|
|
100
|
-
return jsonResponse(request, response);
|
|
81
|
+
return jsonResponse(request, await dispatch(core, body));
|
|
101
82
|
};
|
|
102
83
|
}
|