@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.
Files changed (161) hide show
  1. package/dist/protocol/client.d.ts +0 -2
  2. package/dist/protocol/client.d.ts.map +1 -1
  3. package/dist/protocol/client.js +0 -2
  4. package/dist/protocol/client.js.map +1 -1
  5. package/dist/protocol/conformance/cases/guards.d.ts.map +1 -1
  6. package/dist/protocol/conformance/cases/guards.js +10 -173
  7. package/dist/protocol/conformance/cases/guards.js.map +1 -1
  8. package/dist/protocol/conformance/cases/reads.d.ts.map +1 -1
  9. package/dist/protocol/conformance/cases/reads.js +4 -49
  10. package/dist/protocol/conformance/cases/reads.js.map +1 -1
  11. package/dist/protocol/conformance/cases/writes.d.ts.map +1 -1
  12. package/dist/protocol/conformance/cases/writes.js +3 -21
  13. package/dist/protocol/conformance/cases/writes.js.map +1 -1
  14. package/dist/protocol/conformance/context.d.ts +1 -26
  15. package/dist/protocol/conformance/context.d.ts.map +1 -1
  16. package/dist/protocol/conformance/context.js +4 -13
  17. package/dist/protocol/conformance/context.js.map +1 -1
  18. package/dist/protocol/conformance/index.js +3 -3
  19. package/dist/protocol/errors.d.ts +5 -9
  20. package/dist/protocol/errors.d.ts.map +1 -1
  21. package/dist/protocol/errors.js +4 -4
  22. package/dist/protocol/errors.js.map +1 -1
  23. package/dist/protocol/index.d.ts +1 -1
  24. package/dist/protocol/index.d.ts.map +1 -1
  25. package/dist/protocol/index.js +1 -1
  26. package/dist/protocol/index.js.map +1 -1
  27. package/dist/protocol/params.d.ts.map +1 -1
  28. package/dist/protocol/params.js +0 -4
  29. package/dist/protocol/params.js.map +1 -1
  30. package/dist/protocol/server/assets.d.ts +2 -3
  31. package/dist/protocol/server/assets.d.ts.map +1 -1
  32. package/dist/protocol/server/assets.js +2 -9
  33. package/dist/protocol/server/assets.js.map +1 -1
  34. package/dist/protocol/server/content.d.ts +2 -3
  35. package/dist/protocol/server/content.d.ts.map +1 -1
  36. package/dist/protocol/server/content.js +10 -22
  37. package/dist/protocol/server/content.js.map +1 -1
  38. package/dist/protocol/server/core.d.ts +8 -11
  39. package/dist/protocol/server/core.d.ts.map +1 -1
  40. package/dist/protocol/server/core.js +8 -24
  41. package/dist/protocol/server/core.js.map +1 -1
  42. package/dist/protocol/server/handler.d.ts.map +1 -1
  43. package/dist/protocol/server/handler.js +11 -23
  44. package/dist/protocol/server/handler.js.map +1 -1
  45. package/dist/protocol/server/http.d.ts +6 -4
  46. package/dist/protocol/server/http.d.ts.map +1 -1
  47. package/dist/protocol/server/http.js +1 -18
  48. package/dist/protocol/server/http.js.map +1 -1
  49. package/dist/protocol/server/index.d.ts +0 -1
  50. package/dist/protocol/server/index.d.ts.map +1 -1
  51. package/dist/protocol/server/index.js.map +1 -1
  52. package/dist/protocol/server/methods/apply.d.ts +2 -2
  53. package/dist/protocol/server/methods/apply.d.ts.map +1 -1
  54. package/dist/protocol/server/methods/apply.js +23 -71
  55. package/dist/protocol/server/methods/apply.js.map +1 -1
  56. package/dist/protocol/server/methods/read.d.ts +0 -6
  57. package/dist/protocol/server/methods/read.d.ts.map +1 -1
  58. package/dist/protocol/server/methods/read.js +4 -31
  59. package/dist/protocol/server/methods/read.js.map +1 -1
  60. package/dist/protocol/server/rpc.d.ts +1 -1
  61. package/dist/protocol/server/rpc.d.ts.map +1 -1
  62. package/dist/protocol/server/rpc.js +9 -39
  63. package/dist/protocol/server/rpc.js.map +1 -1
  64. package/dist/protocol/storage/fs/index.d.ts +0 -3
  65. package/dist/protocol/storage/fs/index.d.ts.map +1 -1
  66. package/dist/protocol/storage/fs/index.js +0 -5
  67. package/dist/protocol/storage/fs/index.js.map +1 -1
  68. package/dist/protocol/storage/memory.d.ts +1 -6
  69. package/dist/protocol/storage/memory.d.ts.map +1 -1
  70. package/dist/protocol/storage/memory.js +1 -30
  71. package/dist/protocol/storage/memory.js.map +1 -1
  72. package/dist/protocol/storage.d.ts +7 -50
  73. package/dist/protocol/storage.d.ts.map +1 -1
  74. package/dist/protocol/storage.js +1 -1
  75. package/dist/protocol/storage.js.map +1 -1
  76. package/dist/protocol/types.d.ts +0 -34
  77. package/dist/protocol/types.d.ts.map +1 -1
  78. package/dist/protocol/types.js +0 -10
  79. package/dist/protocol/types.js.map +1 -1
  80. package/dist/v8/canonical.d.ts +0 -7
  81. package/dist/v8/canonical.d.ts.map +1 -1
  82. package/dist/v8/canonical.js +0 -9
  83. package/dist/v8/canonical.js.map +1 -1
  84. package/dist/v8/cli/content.d.ts +24 -3
  85. package/dist/v8/cli/content.d.ts.map +1 -1
  86. package/dist/v8/cli/content.js +70 -5
  87. package/dist/v8/cli/content.js.map +1 -1
  88. package/dist/v8/cli/run.js +1 -1
  89. package/dist/v8/cli/run.js.map +1 -1
  90. package/dist/v8/cms.d.ts.map +1 -1
  91. package/dist/v8/cms.js +18 -25
  92. package/dist/v8/cms.js.map +1 -1
  93. package/dist/v8/content.d.ts +5 -7
  94. package/dist/v8/content.d.ts.map +1 -1
  95. package/dist/v8/content.js +47 -45
  96. package/dist/v8/content.js.map +1 -1
  97. package/dist/v8/draft.d.ts +1 -1
  98. package/dist/v8/draft.js +2 -2
  99. package/dist/v8/draft.js.map +1 -1
  100. package/dist/v8/draftChanges.d.ts +52 -11
  101. package/dist/v8/draftChanges.d.ts.map +1 -1
  102. package/dist/v8/draftChanges.js +115 -38
  103. package/dist/v8/draftChanges.js.map +1 -1
  104. package/dist/v8/identity.d.ts +0 -2
  105. package/dist/v8/identity.d.ts.map +1 -1
  106. package/dist/v8/identity.js +0 -10
  107. package/dist/v8/identity.js.map +1 -1
  108. package/dist/v8/remoteLoader.d.ts +2 -4
  109. package/dist/v8/remoteLoader.d.ts.map +1 -1
  110. package/dist/v8/remoteLoader.js +106 -87
  111. package/dist/v8/remoteLoader.js.map +1 -1
  112. package/dist/v8/settings.d.ts +0 -7
  113. package/dist/v8/settings.d.ts.map +1 -1
  114. package/dist/v8/settings.js +0 -10
  115. package/dist/v8/settings.js.map +1 -1
  116. package/dist/v8/telemetry.d.ts +7 -3
  117. package/dist/v8/telemetry.d.ts.map +1 -1
  118. package/dist/v8/telemetry.js +28 -69
  119. package/dist/v8/telemetry.js.map +1 -1
  120. package/dist/v8/types.d.ts +46 -18
  121. package/dist/v8/types.d.ts.map +1 -1
  122. package/package.json +1 -1
  123. package/src/protocol/client.ts +0 -3
  124. package/src/protocol/conformance/cases/guards.ts +10 -188
  125. package/src/protocol/conformance/cases/reads.ts +7 -65
  126. package/src/protocol/conformance/cases/writes.ts +7 -36
  127. package/src/protocol/conformance/context.ts +6 -31
  128. package/src/protocol/conformance/index.ts +3 -3
  129. package/src/protocol/errors.ts +5 -10
  130. package/src/protocol/index.ts +0 -2
  131. package/src/protocol/params.ts +0 -4
  132. package/src/protocol/server/assets.ts +2 -12
  133. package/src/protocol/server/content.ts +10 -24
  134. package/src/protocol/server/core.ts +12 -34
  135. package/src/protocol/server/handler.ts +11 -30
  136. package/src/protocol/server/http.ts +1 -19
  137. package/src/protocol/server/index.ts +0 -1
  138. package/src/protocol/server/methods/apply.ts +22 -82
  139. package/src/protocol/server/methods/read.ts +4 -36
  140. package/src/protocol/server/rpc.ts +7 -47
  141. package/src/protocol/storage/fs/index.ts +0 -8
  142. package/src/protocol/storage/memory.ts +1 -36
  143. package/src/protocol/storage.ts +7 -39
  144. package/src/protocol/types.ts +0 -37
  145. package/src/v8/canonical.ts +0 -11
  146. package/src/v8/cli/content.ts +85 -5
  147. package/src/v8/cli/run.ts +1 -1
  148. package/src/v8/cms.ts +18 -26
  149. package/src/v8/content.ts +55 -48
  150. package/src/v8/draft.ts +2 -2
  151. package/src/v8/draftChanges.ts +132 -41
  152. package/src/v8/identity.ts +0 -10
  153. package/src/v8/remoteLoader.ts +111 -106
  154. package/src/v8/settings.ts +1 -15
  155. package/src/v8/telemetry.ts +33 -69
  156. package/src/v8/types.ts +47 -16
  157. package/dist/protocol/server/auth.d.ts +0 -31
  158. package/dist/protocol/server/auth.d.ts.map +0 -1
  159. package/dist/protocol/server/auth.js +0 -32
  160. package/dist/protocol/server/auth.js.map +0 -1
  161. package/src/protocol/server/auth.ts +0 -49
@@ -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 { limitExceeded, unavailable } from "../errors.ts";
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, Limits } from "../types.ts";
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, limits, cache }: Options,
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 maxBlockBytes is unreadable, like invalid JSON: it's
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; only the
71
- // aggregate (maxListBytes) answers LimitExceeded.
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 ${limits.maxBlockBytes} bytes`,
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, limits.maxBlockBytes);
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({ ref });
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 { DEFAULT_LIMITS, type DecoMeta, type Limits } from "../types.ts";
20
- import type { AuthOptions } from "./auth.ts";
13
+ import type { DecoMeta } from "../types.ts";
21
14
  import { BodyCache } from "./bodyCache.ts";
22
15
 
23
- export interface ContentHandlerOptions extends AuthOptions {
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 missing or invalid bearer token (401)
8
- * and a body over the size limit (413), which apply to the whole batch, 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). Transport security that depends on where it runs — CORS,
16
- * `Host` checks, Chrome's local-network preflight — belongs to the server
17
- * that mounts it (`deco serve`).
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 { assertAuthOptions, authenticate } from "./auth.ts";
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, limits.maxRequestBytes);
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 ${limits.maxRequestBytes} bytes`, {
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
- const response = await dispatch(core, body, auth.scope, limits.maxBatchResponseBytes);
100
- return jsonResponse(request, response);
81
+ return jsonResponse(request, await dispatch(core, body));
101
82
  };
102
83
  }
@@ -1,11 +1,9 @@
1
1
  /**
2
2
  * HTTP plumbing for the content handler: bounded body reads (gzip request
3
- * bodies included), gzip responses and bearer-token checks. Web-standard APIs
3
+ * bodies included) and gzip responses. Web-standard APIs
4
4
  * only (Request, Response, CompressionStream), so it runs on Node, Bun,
5
5
  * Workers and Deno.
6
6
  */
7
- import { sha256Hex } from "../canonical.ts";
8
-
9
7
  /** The body was larger than allowed (after decompression). */
10
8
  export class BodyTooLargeError extends Error {
11
9
  constructor(readonly limit: number) {
@@ -121,19 +119,3 @@ export function isJsonContentType(request: Request): boolean {
121
119
  const type = request.headers.get("content-type");
122
120
  return type !== null && type.split(";")[0]!.trim().toLowerCase() === "application/json";
123
121
  }
124
-
125
- /** The bearer token of the request, or `null` without one. */
126
- export function bearerToken(request: Request): string | null {
127
- const header = request.headers.get("authorization");
128
- if (!header) return null;
129
- const match = /^Bearer\s+(\S+)\s*$/i.exec(header);
130
- return match?.[1] ?? null;
131
- }
132
-
133
- /** Compares two strings in time independent of where they differ. */
134
- export async function timingSafeEqualStrings(a: string, b: string): Promise<boolean> {
135
- const [ha, hb] = await Promise.all([sha256Hex(a), sha256Hex(b)]);
136
- let diff = 0;
137
- for (let i = 0; i < ha.length; i++) diff |= ha.charCodeAt(i) ^ hb.charCodeAt(i);
138
- return diff === 0;
139
- }
@@ -6,6 +6,5 @@
6
6
  * and Deno. (`deco serve`'s asset uploads use `./assets`, which isn't public.)
7
7
  */
8
8
 
9
- export type { AuthOptions, AuthorizeResult } from "./auth.ts";
10
9
  export type { ContentHandlerOptions } from "./core.ts";
11
10
  export { type ContentHandler, createContentHandler } from "./handler.ts";
@@ -7,27 +7,23 @@
7
7
  * 3. `set` wins when a name is in both `set` and `delete`.
8
8
  * 4. Validated first: names, value shapes, sizes and the secret guard are
9
9
  * checked before anything is written, and every violation is reported.
10
- * 5. Preconditions are optional: a failed `ifMatch` (or `ifSchemaMatch`)
11
- * writes nothing and returns a Conflict; without them the last writer wins.
10
+ * 5. Preconditions are optional: a failed `ifMatch` writes nothing and
11
+ * returns a Conflict; without one the last writer wins.
12
12
  *
13
13
  * 6. Two spellings of one name are one entry: a guard compares against the
14
14
  * entry under any spelling, and the result reports `null` for every other
15
15
  * spelling the commit deleted.
16
16
  *
17
- * A commit attempt that finds storage moved (files, the schema, or a request
18
- * key reserved meanwhile) is retried against a new snapshot after a jittered
17
+ * A commit attempt that finds storage moved (files or the schema) is retried against a new snapshot after a jittered
19
18
  * backoff, rechecking every guard, up to `maxCommitAttempts` times.
20
19
  */
21
- import { applyRequestDigest, canonicalJson } from "../../canonical.ts";
22
20
  import {
23
21
  type BlockViolation,
24
22
  conflict,
25
23
  invalidBlock,
26
- invalidParams,
27
24
  limitExceeded,
28
25
  readOnly,
29
26
  unavailable,
30
- unsupported,
31
27
  type VersionMismatch,
32
28
  } from "../../errors.ts";
33
29
  import {
@@ -41,11 +37,10 @@ import {
41
37
  spellingKey,
42
38
  } from "../../keys.ts";
43
39
  import { checkSecrets } from "../../secrets.ts";
44
- import type { StorageDescription, StoredReceipt } from "../../storage.ts";
45
- import type { BlocksApplyParams, BlocksApplyResult, DecoMeta, Limits } from "../../types.ts";
40
+ import type { BlocksApplyParams, BlocksApplyResult, DecoMeta } from "../../types.ts";
46
41
  import { type LoadedContent, type LoadedEntry, loadCurrentContent } from "../content.ts";
47
- import type { Core } from "../core.ts";
48
- import { parseSchema, readSchema } from "./read.ts";
42
+ import { type Core, MAX_BLOCK_BYTES, MAX_OPS_PER_APPLY } from "../core.ts";
43
+ import { parseSchema } from "./read.ts";
49
44
 
50
45
  const utf8 = new TextEncoder();
51
46
 
@@ -56,13 +51,13 @@ interface NormalizedApply {
56
51
  ifMatch: Array<[name: string, version: string | null]>;
57
52
  }
58
53
 
59
- function normalize(params: BlocksApplyParams, limits: Limits): NormalizedApply {
54
+ function normalize(params: BlocksApplyParams): NormalizedApply {
60
55
  const set = Object.entries(params.set ?? {});
61
56
  const setNames = new Set(set.map(([name]) => name));
62
57
  const deleteNames = [...new Set(params.delete ?? [])].filter((name) => !setNames.has(name));
63
58
  const ops = set.length + deleteNames.length;
64
- if (ops > limits.maxOpsPerApply) {
65
- throw limitExceeded(`${ops} names in one blocks.apply; the limit is ${limits.maxOpsPerApply}`, {
59
+ if (ops > MAX_OPS_PER_APPLY) {
60
+ throw limitExceeded(`${ops} names in one blocks.apply; the limit is ${MAX_OPS_PER_APPLY}`, {
66
61
  limit: "maxOpsPerApply",
67
62
  });
68
63
  }
@@ -70,7 +65,7 @@ function normalize(params: BlocksApplyParams, limits: Limits): NormalizedApply {
70
65
  }
71
66
 
72
67
  /** Checks everything that doesn't depend on stored content. */
73
- function validateStatic(apply: NormalizedApply, limits: Limits, meta: DecoMeta | null) {
68
+ function validateStatic(apply: NormalizedApply, meta: DecoMeta | null) {
74
69
  const violations: BlockViolation[] = [];
75
70
  const bodies = new Map<string, string>();
76
71
  const bySpelling = new Map<string, string>();
@@ -91,11 +86,11 @@ function validateStatic(apply: NormalizedApply, limits: Limits, meta: DecoMeta |
91
86
  continue;
92
87
  }
93
88
  const body = serializeBlock(value);
94
- if (utf8.encode(body).byteLength > limits.maxBlockBytes) {
89
+ if (utf8.encode(body).byteLength > MAX_BLOCK_BYTES) {
95
90
  violations.push({
96
91
  name,
97
92
  rule: "too-large",
98
- message: `the entry is over ${limits.maxBlockBytes} bytes`,
93
+ message: `the entry is over ${MAX_BLOCK_BYTES} bytes`,
99
94
  });
100
95
  continue;
101
96
  }
@@ -231,12 +226,8 @@ function resultFromVersions(
231
226
  return { revision, versions };
232
227
  }
233
228
 
234
- function receiptResult(apply: NormalizedApply, receipt: StoredReceipt): BlocksApplyResult {
235
- return resultFromVersions(apply, receipt.revision, receipt.versions, receipt.deleted ?? []);
236
- }
237
-
238
- async function loadSchema(core: Core, params: BlocksApplyParams, limits: Limits) {
239
- const stored = await readSchema(core, params.ref, limits.maxSchemaBytes);
229
+ async function loadSchema(core: Core) {
230
+ const stored = await core.storage.readSchema();
240
231
  if (stored === null) return { version: null, meta: null };
241
232
  if (core.parsedSchema?.version !== stored.version) {
242
233
  core.parsedSchema = { version: stored.version, meta: parseSchema(stored.text) };
@@ -244,67 +235,19 @@ async function loadSchema(core: Core, params: BlocksApplyParams, limits: Limits)
244
235
  return { version: stored.version, meta: core.parsedSchema.meta };
245
236
  }
246
237
 
247
- function checkFeatures(description: StorageDescription, core: Core, params: BlocksApplyParams) {
248
- if (description.readOnly) throw readOnly();
249
- core.checkRef(description, params.ref);
250
- if (params.requestKey !== undefined && (!description.idempotency || !core.storage.getReceipt)) {
251
- throw unsupported("this endpoint doesn't support request keys; omit requestKey");
252
- }
253
- }
254
-
255
238
  export async function blocksApply(
256
239
  core: Core,
257
240
  params: BlocksApplyParams,
258
- scope: string,
259
241
  ): Promise<BlocksApplyResult> {
260
242
  const description = await core.description();
261
- checkFeatures(description, core, params);
262
- if (params.requestKey === undefined) return applyOnce(core, params, description, null);
263
-
264
- const digest = await applyRequestDigest(params);
265
- const key = canonicalJson([scope, description.root, params.ref ?? null, params.requestKey]);
266
- // Simultaneous duplicates: wait for the first, then find its receipt.
267
- for (let pending = core.inflight.get(key); pending; pending = core.inflight.get(key)) {
268
- await pending.catch(() => {});
269
- }
270
- const run = applyOnce(core, params, description, { key, digest });
271
- core.inflight.set(key, run);
272
- try {
273
- return await run;
274
- } finally {
275
- if (core.inflight.get(key) === run) core.inflight.delete(key);
276
- }
277
- }
278
-
279
- async function applyOnce(
280
- core: Core,
281
- params: BlocksApplyParams,
282
- description: StorageDescription,
283
- receipt: { key: string; digest: string } | null,
284
- ): Promise<BlocksApplyResult> {
285
- const limits = core.limits(description);
286
- const apply = normalize(params, limits);
243
+ if (description.readOnly) throw readOnly();
244
+ const apply = normalize(params);
287
245
 
288
246
  for (let attempt = 1; attempt <= core.maxCommitAttempts; attempt++) {
289
- // Receipts for completed identical requests resolve before any guard is rechecked.
290
- if (receipt) {
291
- const stored = await core.storage.getReceipt!(receipt.key);
292
- if (stored) {
293
- if (stored.digest !== receipt.digest) {
294
- throw invalidParams("requestKey was already used for a different request");
295
- }
296
- return receiptResult(apply, stored);
297
- }
298
- }
299
-
300
- const schema = await loadSchema(core, params, limits);
301
- if (params.ifSchemaMatch !== undefined && params.ifSchemaMatch !== schema.version) {
302
- throw conflict({ schema: { expected: params.ifSchemaMatch, actual: schema.version } });
303
- }
304
- const { violations, bodies } = validateStatic(apply, limits, schema.meta);
305
- const { snapshot, content } = await loadCurrentContent(core.storage, params.ref, {
247
+ const schema = await loadSchema(core);
248
+ const { violations, bodies } = validateStatic(apply, schema.meta);
249
+ const { snapshot, content } = await loadCurrentContent(core.storage, {
306
250
  readAll: false,
307
- limits,
308
251
  cache: core.cache,
309
252
  });
310
253
  const bySpelling = entriesBySpelling(content!);
@@ -315,19 +258,16 @@ async function applyOnce(
315
258
  if (mismatches) throw conflict({ entries: mismatches });
316
259
 
317
260
  const { put, delete: deletes, expected } = plan(content!, bySpelling, apply, bodies);
318
- if (Object.keys(put).length === 0 && deletes.length === 0 && !receipt) {
261
+ if (Object.keys(put).length === 0 && deletes.length === 0) {
319
262
  return resultFromVersions(apply, snapshot.revision, {}, []);
320
263
  }
321
- // The schema the guards and the secret check used must still be current at commit time.
322
- const dependsOnSchema = params.ifSchemaMatch !== undefined || apply.set.length > 0;
264
+ // The schema the secret check used must still be current at commit time.
323
265
  const result = await core.storage.commit({
324
- ref: params.ref,
325
266
  base: snapshot,
326
267
  put,
327
268
  delete: deletes,
328
269
  expected,
329
- ...(dependsOnSchema ? { expectedSchemaVersion: schema.version } : {}),
330
- receipt: receipt ?? undefined,
270
+ ...(apply.set.length > 0 ? { expectedSchemaVersion: schema.version } : {}),
331
271
  });
332
272
  if (result.status === "committed") {
333
273
  return resultFromVersions(apply, result.revision, result.versions, deletes);
@@ -2,7 +2,7 @@
2
2
  * The three read methods: `describe`, `schema.get` and `blocks.list`.
3
3
  */
4
4
  import { publicKeyDerFromPem } from "../../ciphertext.ts";
5
- import { limitExceeded, unavailable } from "../../errors.ts";
5
+ import { unavailable } from "../../errors.ts";
6
6
  import {
7
7
  ASSETS_URL_PREFIX,
8
8
  type BlocksListResult,
@@ -52,13 +52,7 @@ export async function describe(core: Core): Promise<DescribeResult> {
52
52
  readOnly,
53
53
  root: description.root,
54
54
  schemaFormat: SCHEMA_FORMAT,
55
- refs: description.refs,
56
- writes: {
57
- idempotency: core.storage.getReceipt ? description.idempotency : null,
58
- schemaPreconditions: true,
59
- },
60
55
  pollIntervalMs: core.pollIntervalMs(description),
61
- limits: core.limits(description),
62
56
  preview: core.options.preview ?? null,
63
57
  assets:
64
58
  readOnly || description.assets === null
@@ -72,20 +66,6 @@ export async function describe(core: Core): Promise<DescribeResult> {
72
66
  };
73
67
  }
74
68
 
75
- /** Reads and parses the schema, enforcing the schema byte limit. */
76
- export async function readSchema(
77
- core: Core,
78
- ref: string | undefined,
79
- maxSchemaBytes: number,
80
- ): Promise<{ version: string; resolvedRef: string | null; text: string } | null> {
81
- const stored = await core.storage.readSchema({ ref });
82
- if (stored === null) return null;
83
- if (new TextEncoder().encode(stored.text).byteLength > maxSchemaBytes) {
84
- throw limitExceeded(`the schema is over ${maxSchemaBytes} bytes`, { limit: "maxSchemaBytes" });
85
- }
86
- return stored;
87
- }
88
-
89
69
  /** Parses schema text; a file caught mid-write is reported as Unavailable, never served torn. */
90
70
  export function parseSchema(text: string): DecoMeta {
91
71
  let schema: unknown;
@@ -101,24 +81,16 @@ export function parseSchema(text: string): DecoMeta {
101
81
  }
102
82
 
103
83
  export async function schemaGet(core: Core, params: ReadParams): Promise<SchemaGetResult> {
104
- const description = await core.description();
105
- core.checkRef(description, params.ref);
106
- const limits = core.limits(description);
107
- const stored = await core.storage.readSchema({ ref: params.ref });
84
+ const stored = await core.storage.readSchema();
108
85
  if (stored === null) {
109
86
  // No schema yet is a state, not an error. The snapshot still refuses a
110
87
  // site with no .deco folder (NotFound) and reports the resolved branch.
111
- const snapshot = await core.storage.snapshot({ ref: params.ref });
88
+ const snapshot = await core.storage.snapshot();
112
89
  return { notModified: false, version: null, resolvedRef: snapshot.resolvedRef, schema: null };
113
90
  }
114
91
  if (params.ifNoneMatch !== undefined && params.ifNoneMatch === stored.version) {
115
92
  return { notModified: true, version: stored.version };
116
93
  }
117
- if (new TextEncoder().encode(stored.text).byteLength > limits.maxSchemaBytes) {
118
- throw limitExceeded(`the schema is over ${limits.maxSchemaBytes} bytes`, {
119
- limit: "maxSchemaBytes",
120
- });
121
- }
122
94
  return {
123
95
  notModified: false,
124
96
  version: stored.version,
@@ -128,13 +100,9 @@ export async function schemaGet(core: Core, params: ReadParams): Promise<SchemaG
128
100
  }
129
101
 
130
102
  export async function blocksList(core: Core, params: ReadParams): Promise<BlocksListResult> {
131
- const description = await core.description();
132
- core.checkRef(description, params.ref);
133
- const limits = core.limits(description);
134
103
  const { snapshot, content } = await loadCurrentContent(
135
104
  core.storage,
136
- params.ref,
137
- { readAll: true, limits, cache: core.cache },
105
+ { readAll: true, cache: core.cache },
138
106
  (s) => params.ifNoneMatch !== undefined && params.ifNoneMatch === s.revision,
139
107
  );
140
108
  if (content === null) {