@syncular/server 0.58.0 → 0.60.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -36
- package/dist/admin.d.ts +32 -20
- package/dist/admin.js +46 -40
- package/dist/blob-handlers.d.ts +25 -31
- package/dist/blob-handlers.js +63 -41
- package/dist/blob-store.d.ts +42 -24
- package/dist/blob-store.js +0 -0
- package/dist/bucket-blob-store.d.ts +12 -8
- package/dist/bucket-blob-store.js +54 -45
- package/dist/bucket-segment-store.js +18 -16
- package/dist/content-encoding.d.ts +5 -2
- package/dist/content-encoding.js +42 -13
- package/dist/context.d.ts +19 -5
- package/dist/context.js +19 -1
- package/dist/d1-storage.d.ts +14 -98
- package/dist/d1-storage.js +249 -1119
- package/dist/errors.js +16 -0
- package/dist/events.d.ts +36 -18
- package/dist/frame-bytes.js +24 -6
- package/dist/handler.js +503 -208
- package/dist/local-authority.d.ts +2 -0
- package/dist/local-authority.js +24 -12
- package/dist/object-bucket.d.ts +2 -0
- package/dist/operations-realtime.js +69 -50
- package/dist/operations.d.ts +5 -2
- package/dist/operations.js +20 -29
- package/dist/pg-executor-pglite.d.ts +5 -6
- package/dist/pg-executor-pglite.js +22 -14
- package/dist/pg-executor.d.ts +4 -1
- package/dist/postgres-storage.d.ts +24 -19
- package/dist/postgres-storage.js +420 -262
- package/dist/pull.d.ts +70 -47
- package/dist/pull.js +270 -301
- package/dist/push.d.ts +12 -3
- package/dist/push.js +361 -173
- package/dist/realtime.d.ts +85 -39
- package/dist/realtime.js +355 -219
- package/dist/relational-rows.d.ts +21 -66
- package/dist/relational-rows.js +28 -109
- package/dist/s3-blob-store.d.ts +11 -12
- package/dist/s3-blob-store.js +20 -13
- package/dist/s3-object-bucket.d.ts +8 -1
- package/dist/s3-object-bucket.js +16 -3
- package/dist/schema.d.ts +14 -0
- package/dist/schema.js +23 -0
- package/dist/scopes.d.ts +6 -2
- package/dist/scopes.js +22 -4
- package/dist/seed.js +10 -16
- package/dist/segment-download.d.ts +4 -1
- package/dist/segment-download.js +22 -6
- package/dist/signed-url.d.ts +2 -2
- package/dist/signed-url.js +9 -2
- package/dist/sigv4.d.ts +8 -2
- package/dist/sigv4.js +8 -4
- package/dist/sqlite-blob-store.d.ts +1 -1
- package/dist/sqlite-blob-store.js +23 -22
- package/dist/sqlite-dialect.d.ts +164 -84
- package/dist/sqlite-dialect.js +1145 -150
- package/dist/sqlite-lease-store.js +13 -18
- package/dist/sqlite-storage.d.ts +9 -62
- package/dist/sqlite-storage.js +303 -1123
- package/dist/storage-query.d.ts +30 -1
- package/dist/storage-query.js +68 -0
- package/dist/storage.d.ts +185 -63
- package/dist/storage.js +15 -0
- package/package.json +2 -2
- package/src/admin.ts +99 -57
- package/src/blob-handlers.ts +91 -72
- package/src/blob-store.ts +0 -0
- package/src/bucket-blob-store.ts +75 -48
- package/src/bucket-segment-store.ts +20 -15
- package/src/content-encoding.ts +48 -16
- package/src/context.ts +33 -6
- package/src/d1-storage.ts +350 -1700
- package/src/errors.ts +16 -0
- package/src/events.ts +37 -18
- package/src/frame-bytes.ts +26 -6
- package/src/handler.ts +654 -261
- package/src/local-authority.ts +27 -14
- package/src/object-bucket.ts +2 -0
- package/src/operations-realtime.ts +85 -61
- package/src/operations.ts +44 -42
- package/src/pg-executor-pglite.ts +27 -23
- package/src/pg-executor.ts +5 -1
- package/src/postgres-storage.ts +557 -354
- package/src/pull.ts +406 -440
- package/src/push.ts +466 -220
- package/src/realtime.ts +447 -290
- package/src/relational-rows.ts +43 -121
- package/src/s3-blob-store.ts +23 -13
- package/src/s3-object-bucket.ts +20 -2
- package/src/schema.ts +31 -0
- package/src/scopes.ts +23 -3
- package/src/seed.ts +13 -20
- package/src/segment-download.ts +33 -7
- package/src/signed-url.ts +10 -4
- package/src/sigv4.ts +17 -4
- package/src/sqlite-blob-store.ts +27 -26
- package/src/sqlite-dialect.ts +1851 -199
- package/src/sqlite-lease-store.ts +12 -23
- package/src/sqlite-storage.ts +390 -1821
- package/src/storage-query.ts +102 -0
- package/src/storage.ts +215 -67
package/src/bucket-blob-store.ts
CHANGED
|
@@ -29,10 +29,17 @@
|
|
|
29
29
|
*
|
|
30
30
|
* The orphan sweep is the ONLY operation that LISTs: `sweepOrphans` pages a
|
|
31
31
|
* prefix listing over the `blob/` prefix (an admin/GC path, off the hot path)
|
|
32
|
-
* to find candidates, reads each object's
|
|
33
|
-
* the
|
|
32
|
+
* to find candidates, reads each object's upload time from its metadata (or
|
|
33
|
+
* the backend's last-modified time for an object a presigned PUT wrote), and
|
|
34
|
+
* DELETEs the old ones the reference index does not name at that moment.
|
|
34
35
|
*/
|
|
35
|
-
import type {
|
|
36
|
+
import type {
|
|
37
|
+
BlobRecord,
|
|
38
|
+
BlobStore,
|
|
39
|
+
BlobStoreStats,
|
|
40
|
+
BlobStream,
|
|
41
|
+
} from './blob-store';
|
|
42
|
+
import type { BucketObject } from './object-bucket';
|
|
36
43
|
import type { ObjectBucket } from './object-bucket';
|
|
37
44
|
|
|
38
45
|
export interface BucketBlobStoreOptions {
|
|
@@ -215,41 +222,27 @@ export class BucketBlobStore implements BlobStore {
|
|
|
215
222
|
};
|
|
216
223
|
const objectKey = this.objectKeyFor(partition, blobId);
|
|
217
224
|
// Idempotent re-put (same content-address ⇒ same key): count each distinct
|
|
218
|
-
// blob once
|
|
219
|
-
//
|
|
225
|
+
// blob once. A re-upload rewrites the object so its metadata carries the
|
|
226
|
+
// re-upload time (the sweep's grace clock, §5.9.2) and keeps a recorded
|
|
227
|
+
// media type.
|
|
220
228
|
const preexisting = await this.#bucket.head(objectKey);
|
|
221
229
|
const storedMeta = preexisting?.metadata[BLOB_META_KEY];
|
|
222
230
|
const existing =
|
|
223
231
|
storedMeta === undefined
|
|
224
232
|
? undefined
|
|
225
233
|
: parseMetaJson(base64urlToUtf8(storedMeta), blobId, this.#label);
|
|
226
|
-
|
|
227
|
-
existing !== undefined &&
|
|
228
|
-
(existing.mediaType !== undefined || mediaType === undefined)
|
|
229
|
-
) {
|
|
230
|
-
return {
|
|
231
|
-
blobId,
|
|
232
|
-
partition,
|
|
233
|
-
byteLength: existing.byteLength,
|
|
234
|
-
...(existing.mediaType !== undefined
|
|
235
|
-
? { mediaType: existing.mediaType }
|
|
236
|
-
: {}),
|
|
237
|
-
createdAtMs: existing.createdAtMs,
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
// A re-upload that names the media type an earlier upload left out
|
|
241
|
-
// rewrites the object under the same precondition, keeping its age.
|
|
234
|
+
const storedType = existing?.mediaType ?? mediaType;
|
|
242
235
|
const meta: BlobMeta = {
|
|
243
236
|
byteLength: bytes.length,
|
|
244
|
-
...(
|
|
245
|
-
createdAtMs: existing?.createdAtMs ?? nowMs,
|
|
237
|
+
...(storedType !== undefined ? { mediaType: storedType } : {}),
|
|
238
|
+
createdAtMs: Math.max(existing?.createdAtMs ?? nowMs, nowMs),
|
|
246
239
|
};
|
|
247
240
|
// Conditional write: of two concurrent uploads of the same blob exactly
|
|
248
|
-
// one lands
|
|
249
|
-
//
|
|
241
|
+
// one lands per pre-state and the blob counts once. The loser re-reads
|
|
242
|
+
// and writes over the winner.
|
|
250
243
|
const written = await this.#bucket.put(objectKey, {
|
|
251
244
|
body: bytes,
|
|
252
|
-
contentType:
|
|
245
|
+
contentType: storedType ?? 'application/octet-stream',
|
|
253
246
|
metadata: { [BLOB_META_KEY]: utf8ToBase64url(metaToJson(meta)) },
|
|
254
247
|
...(preexisting === undefined
|
|
255
248
|
? { createOnly: true }
|
|
@@ -258,7 +251,11 @@ export class BucketBlobStore implements BlobStore {
|
|
|
258
251
|
if (!written) return this.put(partition, blobId, bytes, nowMs, mediaType);
|
|
259
252
|
if (preexisting === undefined)
|
|
260
253
|
await this.#bumpStats({ count: 1, bytes: bytes.length });
|
|
261
|
-
return {
|
|
254
|
+
return {
|
|
255
|
+
...record,
|
|
256
|
+
...(storedType !== undefined ? { mediaType: storedType } : {}),
|
|
257
|
+
createdAtMs: meta.createdAtMs,
|
|
258
|
+
};
|
|
262
259
|
}
|
|
263
260
|
|
|
264
261
|
async delete(partition: string, blobId: string): Promise<void> {
|
|
@@ -275,38 +272,67 @@ export class BucketBlobStore implements BlobStore {
|
|
|
275
272
|
partition: string,
|
|
276
273
|
blobId: string,
|
|
277
274
|
): Promise<{ record: BlobRecord; bytes: Uint8Array } | undefined> {
|
|
278
|
-
|
|
279
|
-
const object = await this.#bucket.get(this.objectKeyFor(partition, blobId));
|
|
275
|
+
const object = await this.#object(partition, blobId);
|
|
280
276
|
if (object === undefined) return undefined;
|
|
281
277
|
const bytes = new Uint8Array(await object.arrayBuffer());
|
|
278
|
+
return {
|
|
279
|
+
record: this.#record(partition, blobId, object, bytes.length),
|
|
280
|
+
bytes,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** The record plus the object GET's body, never buffered (see `get`). */
|
|
285
|
+
async open(
|
|
286
|
+
partition: string,
|
|
287
|
+
blobId: string,
|
|
288
|
+
): Promise<BlobStream | undefined> {
|
|
289
|
+
const object = await this.#object(partition, blobId);
|
|
290
|
+
if (object === undefined || object.body === null) return undefined;
|
|
291
|
+
const record = this.#record(partition, blobId, object, object.size ?? 0);
|
|
292
|
+
return { record, body: object.body, byteLength: record.byteLength };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async #object(
|
|
296
|
+
partition: string,
|
|
297
|
+
blobId: string,
|
|
298
|
+
): Promise<BucketObject | undefined> {
|
|
299
|
+
if (!BLOB_ID_PATTERN.test(blobId)) return undefined;
|
|
300
|
+
return this.#bucket.get(this.objectKeyFor(partition, blobId));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** An object without Syncular metadata was written by a presigned PUT. */
|
|
304
|
+
#record(
|
|
305
|
+
partition: string,
|
|
306
|
+
blobId: string,
|
|
307
|
+
object: BucketObject,
|
|
308
|
+
byteLength: number,
|
|
309
|
+
): BlobRecord {
|
|
282
310
|
const rawMeta = object.metadata[BLOB_META_KEY];
|
|
283
311
|
const meta =
|
|
284
312
|
rawMeta !== undefined
|
|
285
313
|
? parseMetaJson(base64urlToUtf8(rawMeta), blobId, this.#label)
|
|
286
|
-
: { byteLength
|
|
314
|
+
: { byteLength, createdAtMs: 0 };
|
|
287
315
|
return {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
createdAtMs: meta.createdAtMs,
|
|
294
|
-
},
|
|
295
|
-
bytes,
|
|
316
|
+
blobId,
|
|
317
|
+
partition,
|
|
318
|
+
byteLength: meta.byteLength,
|
|
319
|
+
...(meta.mediaType !== undefined ? { mediaType: meta.mediaType } : {}),
|
|
320
|
+
createdAtMs: meta.createdAtMs,
|
|
296
321
|
};
|
|
297
322
|
}
|
|
298
323
|
|
|
299
324
|
/**
|
|
300
325
|
* §5.9.2 orphan sweep — the ONLY LISTing operation. Page the `{prefix}blob/
|
|
301
|
-
* {partition}/` prefix, HEAD each object for its
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
326
|
+
* {partition}/` prefix, HEAD each object for its upload time (the metadata
|
|
327
|
+
* `createdAtMs`, else the backend's last-modified time; an object with
|
|
328
|
+
* neither is skipped), and DELETE the ones older than `olderThanMs` that
|
|
329
|
+
* `isReferenced` denies right before the delete. Returns the deleted
|
|
330
|
+
* blobIds. Off the hot path — a host-scheduled GC job.
|
|
305
331
|
*/
|
|
306
332
|
async sweepOrphans(
|
|
307
333
|
partition: string,
|
|
308
334
|
olderThanMs: number,
|
|
309
|
-
|
|
335
|
+
isReferenced: (blobId: string) => Promise<boolean>,
|
|
310
336
|
): Promise<string[]> {
|
|
311
337
|
const listPrefix = `${this.#prefix}blob/${partition}/`;
|
|
312
338
|
const swept: string[] = [];
|
|
@@ -321,18 +347,19 @@ export class BucketBlobStore implements BlobStore {
|
|
|
321
347
|
if (!rest.startsWith('sha256/')) continue;
|
|
322
348
|
const blobId = `sha256:${rest.slice('sha256/'.length)}`;
|
|
323
349
|
if (!BLOB_ID_PATTERN.test(blobId)) continue;
|
|
324
|
-
if (referencedBlobIds.has(blobId)) continue;
|
|
325
350
|
const head = await this.#bucket.head(key);
|
|
326
351
|
if (head === undefined) continue;
|
|
327
352
|
const rawMeta = head.metadata[BLOB_META_KEY];
|
|
328
|
-
const
|
|
353
|
+
const uploadedAtMs =
|
|
329
354
|
rawMeta !== undefined
|
|
330
355
|
? parseMetaJson(base64urlToUtf8(rawMeta), blobId, this.#label)
|
|
331
|
-
|
|
332
|
-
|
|
356
|
+
.createdAtMs
|
|
357
|
+
: head.lastModifiedMs;
|
|
358
|
+
if (uploadedAtMs === undefined || uploadedAtMs >= olderThanMs) continue;
|
|
359
|
+
if (await isReferenced(blobId)) continue;
|
|
333
360
|
await this.#bucket.delete(key);
|
|
334
361
|
swept.push(blobId);
|
|
335
|
-
deletedBytes +=
|
|
362
|
+
deletedBytes += head.size ?? 0;
|
|
336
363
|
}
|
|
337
364
|
} while (cursor !== undefined);
|
|
338
365
|
if (swept.length > 0) {
|
|
@@ -58,7 +58,6 @@ import {
|
|
|
58
58
|
mergeSegmentRecord,
|
|
59
59
|
publicationRecord,
|
|
60
60
|
publicationsForRecord,
|
|
61
|
-
segmentBytesEqual,
|
|
62
61
|
segmentIdFor,
|
|
63
62
|
selectSegmentPublication,
|
|
64
63
|
} from './segment-store';
|
|
@@ -440,23 +439,29 @@ export class BucketSegmentStore implements SegmentStore {
|
|
|
440
439
|
}
|
|
441
440
|
|
|
442
441
|
/**
|
|
443
|
-
* Write the immutable bytes object
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
*
|
|
442
|
+
* Write the immutable bytes object unless it is stored already. A HEAD
|
|
443
|
+
* comes first, so republishing a segment (every bootstrap pull of an
|
|
444
|
+
* unchanged window) moves no bytes. The write is create-only, so two first
|
|
445
|
+
* publishers cannot overwrite each other. A stored object under the same
|
|
446
|
+
* content address with a different size is the hash collision §5.1
|
|
447
|
+
* requires to fail loudly; equal size under one SHA-256 address is the same
|
|
448
|
+
* content.
|
|
447
449
|
*/
|
|
448
450
|
async #putBytes(segmentId: string, bytes: Uint8Array): Promise<void> {
|
|
449
451
|
const key = this.objectKeyFor(segmentId);
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
452
|
+
const head = await this.#bucket.head(key);
|
|
453
|
+
if (
|
|
454
|
+
head === undefined &&
|
|
455
|
+
(await this.#bucket.put(key, {
|
|
456
|
+
body: bytes,
|
|
457
|
+
contentType: 'application/octet-stream',
|
|
458
|
+
createOnly: true,
|
|
459
|
+
}))
|
|
460
|
+
) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const stored = head ?? (await this.#bucket.head(key));
|
|
464
|
+
if (stored !== undefined && stored.size !== bytes.length) {
|
|
460
465
|
throw new Error(
|
|
461
466
|
`${this.#label}: content-address collision at ${segmentId} (§5.1)`,
|
|
462
467
|
);
|
package/src/content-encoding.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* §1.3 transport compression for
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* §1.3 transport compression for the HTTP bodies the adapters serve —
|
|
3
|
+
* `<mount>/sync`, `<mount>/operations` and segment downloads (blob downloads
|
|
4
|
+
* stream identity, §5.9.5): negotiate `Content-Encoding` from the request's
|
|
5
5
|
* `Accept-Encoding` (zstd preferred, gzip fallback, identity when the client
|
|
6
6
|
* offers neither), feature-detecting the runtime's codecs, zero
|
|
7
7
|
* dependencies. Bun supplies zstd + gzip natively; every other runtime
|
|
@@ -167,31 +167,63 @@ export function encodeStream(
|
|
|
167
167
|
* `Content-Encoding: gzip` (§1.3: clients MAY compress a request body and
|
|
168
168
|
* servers MUST accept it). Returns `undefined` for any other declared
|
|
169
169
|
* encoding — the adapter answers 415 rather than parse bytes it cannot read.
|
|
170
|
-
* A gzip body that does not decompress is a `sync.invalid_request`.
|
|
170
|
+
* A gzip body that does not decompress is a `sync.invalid_request`. One read
|
|
171
|
+
* loop counts the decoded bytes of identity and gzip bodies alike and stops
|
|
172
|
+
* with `sync.limit_exceeded` past `maxBytes` (§1.7 `maxRequestBytes`), so a
|
|
173
|
+
* small compressed body never inflates past the cap.
|
|
171
174
|
*/
|
|
172
175
|
export async function decodeRequestBody(
|
|
173
176
|
request: Request,
|
|
177
|
+
maxBytes: number,
|
|
174
178
|
): Promise<Uint8Array | undefined> {
|
|
175
179
|
const encoding = request.headers
|
|
176
180
|
.get('content-encoding')
|
|
177
181
|
?.trim()
|
|
178
182
|
.toLowerCase();
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
+
const identity =
|
|
184
|
+
encoding === undefined || encoding === '' || encoding === 'identity';
|
|
185
|
+
if (
|
|
186
|
+
!identity &&
|
|
187
|
+
(encoding !== 'gzip' || typeof DecompressionStream !== 'function')
|
|
188
|
+
) {
|
|
183
189
|
return undefined;
|
|
184
190
|
}
|
|
185
191
|
if (request.body === null) {
|
|
192
|
+
if (identity) return new Uint8Array(0);
|
|
186
193
|
throw syncError('sync.invalid_request', 'malformed gzip request body');
|
|
187
194
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
195
|
+
const reader = (
|
|
196
|
+
identity
|
|
197
|
+
? request.body
|
|
198
|
+
: request.body.pipeThrough(new DecompressionStream('gzip'))
|
|
199
|
+
).getReader();
|
|
200
|
+
const chunks: Uint8Array[] = [];
|
|
201
|
+
let total = 0;
|
|
202
|
+
for (;;) {
|
|
203
|
+
let step: Awaited<ReturnType<typeof reader.read>>;
|
|
204
|
+
try {
|
|
205
|
+
step = await reader.read();
|
|
206
|
+
} catch (error) {
|
|
207
|
+
if (identity) throw error;
|
|
208
|
+
throw syncError('sync.invalid_request', 'malformed gzip request body');
|
|
209
|
+
}
|
|
210
|
+
if (step.done) break;
|
|
211
|
+
total += step.value.length;
|
|
212
|
+
if (total > maxBytes) {
|
|
213
|
+
await reader.cancel().catch(() => {});
|
|
214
|
+
throw syncError(
|
|
215
|
+
'sync.limit_exceeded',
|
|
216
|
+
'request body exceeds the server limit (§1.7)',
|
|
217
|
+
JSON.stringify({ limit: 'maxRequestBytes', max: maxBytes }),
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
chunks.push(step.value);
|
|
221
|
+
}
|
|
222
|
+
const bytes = new Uint8Array(total);
|
|
223
|
+
let offset = 0;
|
|
224
|
+
for (const chunk of chunks) {
|
|
225
|
+
bytes.set(chunk, offset);
|
|
226
|
+
offset += chunk.length;
|
|
196
227
|
}
|
|
228
|
+
return bytes;
|
|
197
229
|
}
|
package/src/context.ts
CHANGED
|
@@ -58,7 +58,7 @@ export type ResolverOutage = typeof RESOLVER_OUTAGE;
|
|
|
58
58
|
/**
|
|
59
59
|
* Host callback: actor → allowed scopes (§3.2 step 3). Resolved at most
|
|
60
60
|
* once per request and memoized (§3.4 step 1). `'*'` means "any value for
|
|
61
|
-
* this variable". A throwing resolver revokes
|
|
61
|
+
* this variable". A throwing resolver revokes units and rejects
|
|
62
62
|
* writes with `sync.forbidden` — fail loud, never leak. Returning
|
|
63
63
|
* `RESOLVER_OUTAGE` instead opts the request into lease authorization
|
|
64
64
|
* (§7.3.3) — a deliberate outage signal, not a failure.
|
|
@@ -82,22 +82,37 @@ export interface ServerLimits {
|
|
|
82
82
|
readonly inlineSegmentMaxBytes: number;
|
|
83
83
|
/** §6.11 declared-reference cascade cap per commit (reference default). */
|
|
84
84
|
readonly maxCascadeOperationsPerCommit: number;
|
|
85
|
+
/**
|
|
86
|
+
* §1.7 cap on a request body's decoded bytes: an HTTP body after gzip
|
|
87
|
+
* decoding, and a socket round's assembled request (§8.7).
|
|
88
|
+
*/
|
|
89
|
+
readonly maxRequestBytes: number;
|
|
90
|
+
/** §1.7 cap on `UNIT` frames per request: the units of a traveling list. */
|
|
91
|
+
readonly maxSubscriptionsPerRequest: number;
|
|
92
|
+
/** Cap on concurrent registered-query watches per watch session. */
|
|
93
|
+
readonly maxWatchesPerSession: number;
|
|
85
94
|
}
|
|
86
95
|
|
|
87
96
|
export const DEFAULT_LIMITS: ServerLimits = {
|
|
88
97
|
maxOperationsPerRequest: 500,
|
|
89
98
|
inlineSegmentMaxBytes: 256 * 1024,
|
|
90
99
|
maxCascadeOperationsPerCommit: 1_000,
|
|
100
|
+
// The size above which adapters already refuse to buffer a segment body.
|
|
101
|
+
maxRequestBytes: 16 * 1024 * 1024,
|
|
102
|
+
// One unit per pushable operation: the existing per-request cap.
|
|
103
|
+
maxSubscriptionsPerRequest: 500,
|
|
104
|
+
// Every commit reruns a session's affected watches one after another.
|
|
105
|
+
maxWatchesPerSession: 100,
|
|
91
106
|
};
|
|
92
107
|
|
|
93
108
|
/** Receives applied commits for realtime fanout (§8.2). */
|
|
94
109
|
export interface RealtimeNotifier {
|
|
95
110
|
notifyCommit(partition: string, commit: StoredCommit): void | Promise<void>;
|
|
96
111
|
/**
|
|
97
|
-
* A pull changed the client's
|
|
98
|
-
* re-registers that client's live sockets, so a client that runs its
|
|
99
|
-
* rounds over HTTP while a socket is open receives deltas for the
|
|
100
|
-
*
|
|
112
|
+
* A pull changed the client's registration list or live set (§8.1). A
|
|
113
|
+
* hub re-registers that client's live sockets, so a client that runs its
|
|
114
|
+
* rounds over HTTP while a socket is open receives deltas for the units
|
|
115
|
+
* that joined.
|
|
101
116
|
*/
|
|
102
117
|
notifySubscriptions?(
|
|
103
118
|
partition: string,
|
|
@@ -235,8 +250,20 @@ export function clockOf(ctx: SyncServerConfig): () => number {
|
|
|
235
250
|
return ctx.clock ?? Date.now;
|
|
236
251
|
}
|
|
237
252
|
|
|
253
|
+
/**
|
|
254
|
+
* The configured limits over the defaults. A cap that is not a positive safe
|
|
255
|
+
* integer (`NaN`, `0`, `Infinity`) would disable or jam it, so it throws;
|
|
256
|
+
* `inlineSegmentMaxBytes` is a threshold and may be `0` (inline nothing).
|
|
257
|
+
*/
|
|
238
258
|
export function limitsOf(ctx: SyncServerConfig): ServerLimits {
|
|
239
|
-
|
|
259
|
+
const limits: ServerLimits = { ...DEFAULT_LIMITS, ...ctx.limits };
|
|
260
|
+
for (const [name, value] of Object.entries(limits)) {
|
|
261
|
+
const min = name === 'inlineSegmentMaxBytes' ? 0 : 1;
|
|
262
|
+
if (!Number.isSafeInteger(value) || value < min) {
|
|
263
|
+
throw new Error(`ServerLimits.${name} is out of range`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return limits;
|
|
240
267
|
}
|
|
241
268
|
|
|
242
269
|
/** Refresh the registry after host authentication and return log continuity. */
|