@syncular/server-workers 0.38.0 → 0.39.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/dist/index.js CHANGED
@@ -29,7 +29,7 @@
29
29
  * `/sync`. An HTTP-only D1 deployment uses the `coordinator` option instead:
30
30
  * WebSockets are optional, the per-partition push queue is not.
31
31
  */
32
- import { D1ServerStorage, errorBody, SSP2_CONTENT_TYPE, SyncError, SyncularAdmin, } from '@syncular/server';
32
+ import { acceptsEncoding, D1ServerStorage, errorBody, SSP2_CONTENT_TYPE, SyncError, SyncularAdmin, varyOnAcceptEncoding, } from '@syncular/server';
33
33
  import { createSyncularAdminRoutes, createSyncularHono, } from '@syncular/server-hono';
34
34
  import { adminRequestForDurableObject, OPERATIONS_DO_REQUEST_PATH, REALTIME_DO_UPGRADE_PATH, REALTIME_DO_WAKE_PATH, SYNC_DO_REQUEST_PATH, writeIdentityHeaders, writeRequestIdentityHeaders, } from './realtime-do.js';
35
35
  export { D1ServerStorage } from '@syncular/server';
@@ -106,7 +106,8 @@ export function createWorkersFetchHandler(factoryOrOptions) {
106
106
  const error = new SyncError('sync.auth_required');
107
107
  return Response.json(errorBody(error), { status: error.httpStatus });
108
108
  }
109
- return forwardSyncRequest(request, coordinator.namespace, auth, partitionPath);
109
+ const forwarded = await forwardSyncRequest(request, coordinator.namespace, auth, partitionPath);
110
+ return withTransportEncoding(request, forwarded);
110
111
  }
111
112
  if (options.admin !== undefined) {
112
113
  const admin = await options.admin(env, ctx);
@@ -141,6 +142,32 @@ export function createWorkersFetchHandler(factoryOrOptions) {
141
142
  return app.fetch(request);
142
143
  };
143
144
  }
145
+ /**
146
+ * §1.3: the Durable Object answers `/sync` and `/operations` with identity
147
+ * bodies. The Worker is the outermost hop and the last place the client's
148
+ * `Accept-Encoding` is visible, so it declares `Content-Encoding: gzip` and
149
+ * leaves `encodeBody` at its default (`automatic`): workerd gzips the body
150
+ * natively as it leaves the edge, and these content types get no compression
151
+ * from the edge otherwise. A response that already carries an encoding was
152
+ * encoded by the DO, so it passes through untouched — re-encoding it would
153
+ * gzip the gzip.
154
+ */
155
+ function withTransportEncoding(request, response) {
156
+ if (response.status !== 200 ||
157
+ response.body === null ||
158
+ response.headers.has('content-encoding')) {
159
+ return response;
160
+ }
161
+ const headers = new Headers(response.headers);
162
+ // Both branches are negotiated per request, so caches must key on it.
163
+ varyOnAcceptEncoding(headers);
164
+ if (acceptsEncoding(request.headers.get('accept-encoding') ?? '', 'gzip')) {
165
+ // The encoded length is unknown until the edge writes it.
166
+ headers.delete('content-length');
167
+ headers.set('content-encoding', 'gzip');
168
+ }
169
+ return new Response(response.body, { status: 200, headers });
170
+ }
144
171
  /**
145
172
  * The Durable Object path for a request that must run on the partition FIFO:
146
173
  * `POST /sync` with the SSP2 content type and `POST /operations` (the DO
@@ -72,7 +72,7 @@
72
72
  * partition serialization may call `/__wake` after its own commit so sockets
73
73
  * re-pull. See `durableObjectRealtimeNotifier` for that caller side.
74
74
  */
75
- import { D1ServerStorage, type ServerStorage, type EraseScopeValueOptions, type EraseScopeValueResult, type PruneOptions, type RealtimeHubConfig, type RemoteOperationRegistry, type RemoteOperationResolver, type SyncRequestContext } from '@syncular/server';
75
+ import { D1ServerStorage, type EraseScopeValueOptions, type EraseScopeValueResult, type PruneOptions, type RealtimeHubConfig, type RemoteOperationRegistry, type RemoteOperationResolver, type ServerStorage, type SyncRequestContext } from '@syncular/server';
76
76
  export interface DurableObjectStateLike {
77
77
  acceptWebSocket(ws: WebSocketLike, tags?: string[]): void;
78
78
  getWebSockets(tag?: string): WebSocketLike[];
@@ -72,7 +72,7 @@
72
72
  * partition serialization may call `/__wake` after its own commit so sockets
73
73
  * re-pull. See `durableObjectRealtimeNotifier` for that caller side.
74
74
  */
75
- import { adapterSyncError, composeEvents, createRealtimeHub, D1ServerStorage, errorBody, eraseScopeValue, handleRemoteOperation, handleSyncRequest, pruneCommitLog, RingBufferEvents, SSP2_CONTENT_TYPE, SyncError, SyncularAdmin, } from '@syncular/server';
75
+ import { adapterSyncError, composeEvents, createRealtimeHub, D1ServerStorage, decodeRequestBody, errorBody, eraseScopeValue, handleRemoteOperation, handleSyncRequest, pruneCommitLog, RingBufferEvents, SSP2_CONTENT_TYPE, SyncError, SyncularAdmin, } from '@syncular/server';
76
76
  import { createSyncularAdminRoutes } from '@syncular/server-hono';
77
77
  import { decodeMessage } from '@syncular/core';
78
78
  function isAttachment(value) {
@@ -91,6 +91,15 @@ export const OPERATIONS_DO_REQUEST_PATH = '/__syncular_realtime/operations';
91
91
  export const OPERATIONS_CONTENT_TYPE = 'application/vnd.syncular.operations.v1+json';
92
92
  /** Prefix of forwarded admin routes; the admin route path follows it. */
93
93
  export const ADMIN_DO_PATH = '/__syncular_realtime/admin';
94
+ /**
95
+ * §1.3: a forwarded round may carry a gzip body (`decodeRequestBody` accepts
96
+ * it); any other declared `Content-Encoding` is refused with 415 instead of
97
+ * being parsed as SSP2 bytes.
98
+ */
99
+ function unsupportedEncoding(route) {
100
+ const error = new SyncError(route === 'sync' ? 'sync.invalid_request' : 'operation.invalid_request', 'unsupported content encoding');
101
+ return Response.json(errorBody(error), { status: 415 });
102
+ }
94
103
  /**
95
104
  * The base `SyncularRealtimeDO`. A host subclasses (or instantiates) it with a
96
105
  * `RealtimeDOConfig`. The class is platform-shaped: `state.acceptWebSocket` +
@@ -179,7 +188,9 @@ export class SyncularRealtimeHost {
179
188
  async fetch(request) {
180
189
  const url = new URL(request.url);
181
190
  if (url.pathname === SYNC_DO_REQUEST_PATH) {
182
- const bytes = new Uint8Array(await request.arrayBuffer());
191
+ const bytes = await decodeRequestBody(request);
192
+ if (bytes === undefined)
193
+ return unsupportedEncoding('sync');
183
194
  // A round without PUSH_COMMIT frames writes only its own client
184
195
  // record, so it runs beside the FIFO instead of queueing behind every
185
196
  // other client's round. An undecodable request takes the FIFO and
@@ -298,7 +309,9 @@ export class SyncularRealtimeHost {
298
309
  return Response.json(errorBody(error), { status: error.httpStatus });
299
310
  }
300
311
  try {
301
- const bytes = new Uint8Array(await request.arrayBuffer());
312
+ const bytes = await decodeRequestBody(request);
313
+ if (bytes === undefined)
314
+ return unsupportedEncoding('operations');
302
315
  const out = await handleRemoteOperation(bytes, this.#getHub().requestContextFor(identity), registry);
303
316
  return new Response(out.slice().buffer, {
304
317
  status: 200,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/server-workers",
3
- "version": "0.38.0",
3
+ "version": "0.39.0",
4
4
  "description": "Cloudflare Workers adapter for the Syncular sync server",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -45,9 +45,9 @@
45
45
  "!dist/**/*.test.d.ts"
46
46
  ],
47
47
  "dependencies": {
48
- "@syncular/core": "0.38.0",
49
- "@syncular/server": "0.38.0",
50
- "@syncular/server-hono": "0.38.0"
48
+ "@syncular/core": "0.39.0",
49
+ "@syncular/server": "0.39.0",
50
+ "@syncular/server-hono": "0.39.0"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@electric-sql/pglite": "^0.5.4",
package/src/index.ts CHANGED
@@ -30,6 +30,7 @@
30
30
  * WebSockets are optional, the per-partition push queue is not.
31
31
  */
32
32
  import {
33
+ acceptsEncoding,
33
34
  type D1Database,
34
35
  D1ServerStorage,
35
36
  errorBody,
@@ -38,6 +39,7 @@ import {
38
39
  SyncError,
39
40
  SyncularAdmin,
40
41
  type SyncServerConfig,
42
+ varyOnAcceptEncoding,
41
43
  } from '@syncular/server';
42
44
  import {
43
45
  createSyncularAdminRoutes,
@@ -255,12 +257,13 @@ export function createWorkersFetchHandler<Env = unknown>(
255
257
  const error = new SyncError('sync.auth_required');
256
258
  return Response.json(errorBody(error), { status: error.httpStatus });
257
259
  }
258
- return forwardSyncRequest(
260
+ const forwarded = await forwardSyncRequest(
259
261
  request,
260
262
  coordinator.namespace,
261
263
  auth,
262
264
  partitionPath,
263
265
  );
266
+ return withTransportEncoding(request, forwarded);
264
267
  }
265
268
  if (options.admin !== undefined) {
266
269
  const admin = await options.admin(env, ctx);
@@ -306,6 +309,35 @@ export function createWorkersFetchHandler<Env = unknown>(
306
309
  };
307
310
  }
308
311
 
312
+ /**
313
+ * §1.3: the Durable Object answers `/sync` and `/operations` with identity
314
+ * bodies. The Worker is the outermost hop and the last place the client's
315
+ * `Accept-Encoding` is visible, so it declares `Content-Encoding: gzip` and
316
+ * leaves `encodeBody` at its default (`automatic`): workerd gzips the body
317
+ * natively as it leaves the edge, and these content types get no compression
318
+ * from the edge otherwise. A response that already carries an encoding was
319
+ * encoded by the DO, so it passes through untouched — re-encoding it would
320
+ * gzip the gzip.
321
+ */
322
+ function withTransportEncoding(request: Request, response: Response): Response {
323
+ if (
324
+ response.status !== 200 ||
325
+ response.body === null ||
326
+ response.headers.has('content-encoding')
327
+ ) {
328
+ return response;
329
+ }
330
+ const headers = new Headers(response.headers);
331
+ // Both branches are negotiated per request, so caches must key on it.
332
+ varyOnAcceptEncoding(headers);
333
+ if (acceptsEncoding(request.headers.get('accept-encoding') ?? '', 'gzip')) {
334
+ // The encoded length is unknown until the edge writes it.
335
+ headers.delete('content-length');
336
+ headers.set('content-encoding', 'gzip');
337
+ }
338
+ return new Response(response.body, { status: 200, headers });
339
+ }
340
+
309
341
  /**
310
342
  * The Durable Object path for a request that must run on the partition FIFO:
311
343
  * `POST /sync` with the SSP2 content type and `POST /operations` (the DO
@@ -77,7 +77,7 @@ import {
77
77
  composeEvents,
78
78
  createRealtimeHub,
79
79
  D1ServerStorage,
80
- type ServerStorage,
80
+ decodeRequestBody,
81
81
  errorBody,
82
82
  eraseScopeValue,
83
83
  type EraseScopeValueOptions,
@@ -92,6 +92,7 @@ import {
92
92
  type RemoteOperationRegistry,
93
93
  type RemoteOperationResolver,
94
94
  RingBufferEvents,
95
+ type ServerStorage,
95
96
  SSP2_CONTENT_TYPE,
96
97
  SyncError,
97
98
  SyncularAdmin,
@@ -215,6 +216,19 @@ export const OPERATIONS_CONTENT_TYPE =
215
216
  /** Prefix of forwarded admin routes; the admin route path follows it. */
216
217
  export const ADMIN_DO_PATH = '/__syncular_realtime/admin';
217
218
 
219
+ /**
220
+ * §1.3: a forwarded round may carry a gzip body (`decodeRequestBody` accepts
221
+ * it); any other declared `Content-Encoding` is refused with 415 instead of
222
+ * being parsed as SSP2 bytes.
223
+ */
224
+ function unsupportedEncoding(route: 'sync' | 'operations'): Response {
225
+ const error = new SyncError(
226
+ route === 'sync' ? 'sync.invalid_request' : 'operation.invalid_request',
227
+ 'unsupported content encoding',
228
+ );
229
+ return Response.json(errorBody(error), { status: 415 });
230
+ }
231
+
218
232
  /**
219
233
  * The base `SyncularRealtimeDO`. A host subclasses (or instantiates) it with a
220
234
  * `RealtimeDOConfig`. The class is platform-shaped: `state.acceptWebSocket` +
@@ -338,7 +352,8 @@ export class SyncularRealtimeHost<
338
352
  async fetch(request: Request): Promise<Response> {
339
353
  const url = new URL(request.url);
340
354
  if (url.pathname === SYNC_DO_REQUEST_PATH) {
341
- const bytes = new Uint8Array(await request.arrayBuffer());
355
+ const bytes = await decodeRequestBody(request);
356
+ if (bytes === undefined) return unsupportedEncoding('sync');
342
357
  // A round without PUSH_COMMIT frames writes only its own client
343
358
  // record, so it runs beside the FIFO instead of queueing behind every
344
359
  // other client's round. An undecodable request takes the FIFO and
@@ -484,7 +499,8 @@ export class SyncularRealtimeHost<
484
499
  return Response.json(errorBody(error), { status: error.httpStatus });
485
500
  }
486
501
  try {
487
- const bytes = new Uint8Array(await request.arrayBuffer());
502
+ const bytes = await decodeRequestBody(request);
503
+ if (bytes === undefined) return unsupportedEncoding('operations');
488
504
  const out = await handleRemoteOperation(
489
505
  bytes,
490
506
  this.#getHub().requestContextFor(identity),