@decocms/blocks 8.1.0-next.5 → 8.1.0-next.7
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/cli/content.d.ts +24 -3
- package/dist/v8/cli/content.d.ts.map +1 -1
- package/dist/v8/cli/content.js +70 -5
- package/dist/v8/cli/content.js.map +1 -1
- package/dist/v8/cli/run.js +1 -1
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +18 -25
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +5 -7
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +47 -45
- 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 +52 -11
- package/dist/v8/draftChanges.d.ts.map +1 -1
- package/dist/v8/draftChanges.js +115 -38
- package/dist/v8/draftChanges.js.map +1 -1
- package/dist/v8/identity.d.ts +0 -2
- package/dist/v8/identity.d.ts.map +1 -1
- package/dist/v8/identity.js +0 -10
- package/dist/v8/identity.js.map +1 -1
- package/dist/v8/remoteLoader.d.ts +2 -4
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +106 -87
- package/dist/v8/remoteLoader.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 +7 -3
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +28 -69
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +46 -18
- 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/cli/content.ts +85 -5
- package/src/v8/cli/run.ts +1 -1
- package/src/v8/cms.ts +18 -26
- package/src/v8/content.ts +55 -48
- package/src/v8/draft.ts +2 -2
- package/src/v8/draftChanges.ts +132 -41
- package/src/v8/identity.ts +0 -10
- package/src/v8/remoteLoader.ts +111 -106
- package/src/v8/settings.ts +1 -15
- package/src/v8/telemetry.ts +33 -69
- package/src/v8/types.ts +47 -16
- 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,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* `blocks.apply`'s own size guards and the secret guard.
|
|
3
3
|
*/
|
|
4
4
|
import { encryptToCiphertext, publicKeyPemFromDer } from "../../ciphertext.ts";
|
|
5
5
|
import { ErrorCode, type InvalidBlockData } from "../../errors.ts";
|
|
6
|
+
import { MAX_BLOCK_BYTES, MAX_OPS_PER_APPLY, MAX_REQUEST_BYTES } from "../../server/core.ts";
|
|
6
7
|
import {
|
|
7
8
|
assert,
|
|
8
9
|
assertEqual,
|
|
@@ -18,12 +19,6 @@ async function writable(ctx: ConformanceContext) {
|
|
|
18
19
|
return d;
|
|
19
20
|
}
|
|
20
21
|
|
|
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
22
|
/** A throwaway public key, for endpoints that don't report one. */
|
|
28
23
|
async function ephemeralPublicKey(): Promise<string> {
|
|
29
24
|
const pair = (await crypto.subtle.generateKey(
|
|
@@ -45,17 +40,14 @@ async function realCiphertext(ctx: ConformanceContext, value: string): Promise<s
|
|
|
45
40
|
return encryptToCiphertext(publicKey, value);
|
|
46
41
|
}
|
|
47
42
|
|
|
48
|
-
const key = (ctx: ConformanceContext, label: string) =>
|
|
49
|
-
`${ctx.prefix}-${label}-${Math.random().toString(36).slice(2)}`;
|
|
50
|
-
|
|
51
43
|
export const guardCases: ConformanceCase[] = [
|
|
52
44
|
{
|
|
53
45
|
id: "limits/ops-per-apply",
|
|
54
|
-
title: "more names than
|
|
46
|
+
title: "more names than one blocks.apply takes is LimitExceeded",
|
|
55
47
|
async run(ctx) {
|
|
56
|
-
|
|
48
|
+
await writable(ctx);
|
|
57
49
|
const names = Array.from(
|
|
58
|
-
{ length:
|
|
50
|
+
{ length: MAX_OPS_PER_APPLY + 1 },
|
|
59
51
|
(_, i) => `${ctx.prefix}-op-${i}`,
|
|
60
52
|
);
|
|
61
53
|
await expectError(
|
|
@@ -67,16 +59,14 @@ export const guardCases: ConformanceCase[] = [
|
|
|
67
59
|
},
|
|
68
60
|
{
|
|
69
61
|
id: "limits/block-bytes",
|
|
70
|
-
title: "an entry over
|
|
62
|
+
title: "an entry over the block size limit is refused and nothing is written",
|
|
71
63
|
async run(ctx) {
|
|
72
|
-
|
|
73
|
-
if (d.limits.maxBlockBytes + 1024 > d.limits.maxRequestBytes)
|
|
74
|
-
return ctx.skip("the request limit is lower");
|
|
64
|
+
await writable(ctx);
|
|
75
65
|
const big = ctx.name("big");
|
|
76
66
|
const small = ctx.name("small");
|
|
77
67
|
const error = await expectError(
|
|
78
68
|
ctx.client.blocksApply({
|
|
79
|
-
set: { [big]: { text: "x".repeat(
|
|
69
|
+
set: { [big]: { text: "x".repeat(MAX_BLOCK_BYTES) }, [small]: { ok: true } },
|
|
80
70
|
}),
|
|
81
71
|
ErrorCode.InvalidBlock,
|
|
82
72
|
"an oversized entry",
|
|
@@ -92,106 +82,19 @@ export const guardCases: ConformanceCase[] = [
|
|
|
92
82
|
},
|
|
93
83
|
{
|
|
94
84
|
id: "limits/request-bytes",
|
|
95
|
-
title: "a body over
|
|
85
|
+
title: "a body over the request size limit is HTTP 413 with LimitExceeded",
|
|
96
86
|
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
87
|
const body = JSON.stringify({
|
|
101
88
|
jsonrpc: "2.0",
|
|
102
89
|
id: 1,
|
|
103
90
|
method: "blocks.apply",
|
|
104
|
-
params: { set: { [ctx.name("huge")]: { text: "x".repeat(
|
|
91
|
+
params: { set: { [ctx.name("huge")]: { text: "x".repeat(MAX_REQUEST_BYTES) } } },
|
|
105
92
|
});
|
|
106
93
|
const response = await ctx.raw(body);
|
|
107
94
|
assertEqual(response.status, 413, "HTTP status");
|
|
108
95
|
assertEqual(rawErrorCode(response.body), ErrorCode.LimitExceeded, "the error code");
|
|
109
96
|
},
|
|
110
97
|
},
|
|
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
98
|
{
|
|
196
99
|
id: "secrets/guard",
|
|
197
100
|
title: "a Secret field accepts only a secret block with a well-formed ciphertext",
|
|
@@ -230,85 +133,4 @@ export const guardCases: ConformanceCase[] = [
|
|
|
230
133
|
assert(!list.notModified && ok in list.blocks, "an encrypted value is saved");
|
|
231
134
|
},
|
|
232
135
|
},
|
|
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
136
|
];
|
|
@@ -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) {
|