@syncular/server-hono 0.57.0 → 0.59.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/admin.js +12 -0
- package/dist/index.js +21 -19
- package/package.json +3 -3
- package/src/admin.ts +14 -0
- package/src/index.ts +22 -21
package/dist/admin.js
CHANGED
|
@@ -44,6 +44,18 @@ export function createSyncularAdminRoutes(admin, options) {
|
|
|
44
44
|
app.onError((error) => jsonError(error));
|
|
45
45
|
/** Run the host guards; actions also need `authorizeActions`. */
|
|
46
46
|
async function guard(request, partition) {
|
|
47
|
+
// A cookie-authenticated operator's browser must not run an action for
|
|
48
|
+
// another site. The origin check compares hosts, so a proxy that ends TLS
|
|
49
|
+
// in front of the server still passes it.
|
|
50
|
+
if (request.method === 'POST') {
|
|
51
|
+
const site = request.headers.get('sec-fetch-site');
|
|
52
|
+
const origin = request.headers.get('origin');
|
|
53
|
+
if ((site !== null && site !== 'same-origin' && site !== 'none') ||
|
|
54
|
+
(origin !== null &&
|
|
55
|
+
URL.parse(origin)?.host !== new URL(request.url).host)) {
|
|
56
|
+
throw new SyncError('sync.forbidden', 'cross-site admin request');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
47
59
|
const context = { partition, request };
|
|
48
60
|
if (!(await options.authorize(context))) {
|
|
49
61
|
throw new SyncError('sync.auth_required');
|
package/dist/index.js
CHANGED
|
@@ -4,13 +4,15 @@
|
|
|
4
4
|
* Mounts the §1.1 routes including sync, registered operations, segments, and
|
|
5
5
|
* blobs. Realtime upgrades are runtime-specific and stay with the host.
|
|
6
6
|
*/
|
|
7
|
-
import { adapterSyncError, composeEvents, decodeRequestBody, encodeBody, encodeStream, errorBody, handleBlobDownload, handleBlobUpload, handleBlobUploadGrant, openSegmentDownload, handleRemoteOperation, handleSyncRequest, RealtimeHub, RingBufferEvents, SEGMENT_STREAM_THRESHOLD_BYTES, SSP2_CONTENT_TYPE, SyncError, SyncularAdmin, varyOnAcceptEncoding, } from '@syncular/server';
|
|
7
|
+
import { adapterSyncError, composeEvents, decodeRequestBody, encodeBody, encodeStream, errorBody, handleBlobDownload, handleBlobUpload, handleBlobUploadGrant, openSegmentDownload, handleRemoteOperation, handleSyncRequest, limitsOf, RealtimeHub, RingBufferEvents, SEGMENT_STREAM_THRESHOLD_BYTES, SSP2_CONTENT_TYPE, SyncError, SyncularAdmin, varyOnAcceptEncoding, } from '@syncular/server';
|
|
8
8
|
import { Hono } from 'hono';
|
|
9
9
|
import { createSyncularAdminRoutes } from './admin.js';
|
|
10
10
|
export * from './admin.js';
|
|
11
11
|
export function createSyncularHono(options) {
|
|
12
12
|
const app = new Hono();
|
|
13
13
|
let config = options.config;
|
|
14
|
+
// Validates every configured limit once, at creation.
|
|
15
|
+
const { maxRequestBytes } = limitsOf(config);
|
|
14
16
|
const admin = options.admin;
|
|
15
17
|
if (admin !== undefined) {
|
|
16
18
|
const ring = new RingBufferEvents(admin.ringCapacity !== undefined
|
|
@@ -46,7 +48,7 @@ export function createSyncularHono(options) {
|
|
|
46
48
|
const unsupportedEncoding = (c, route) => c.json(errorBody(new SyncError(route === 'sync'
|
|
47
49
|
? 'sync.invalid_request'
|
|
48
50
|
: 'operation.invalid_request', 'unsupported content encoding')), 415);
|
|
49
|
-
// §1.3: every buffered body — a sync round, an operation result, a
|
|
51
|
+
// §1.3: every buffered body — a sync round, an operation result, a
|
|
50
52
|
// segment — is encoded per the request's `Accept-Encoding` (zstd preferred,
|
|
51
53
|
// gzip fallback, identity otherwise) and carries `Vary: Accept-Encoding`.
|
|
52
54
|
// Headers are built through the Hono context so host middleware headers
|
|
@@ -84,7 +86,7 @@ export function createSyncularHono(options) {
|
|
|
84
86
|
if (auth === null)
|
|
85
87
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
86
88
|
try {
|
|
87
|
-
const bytes = await decodeRequestBody(c.req.raw);
|
|
89
|
+
const bytes = await decodeRequestBody(c.req.raw, maxRequestBytes);
|
|
88
90
|
if (bytes === undefined)
|
|
89
91
|
return unsupportedEncoding(c, 'sync');
|
|
90
92
|
const out = await handleSyncRequest(bytes, {
|
|
@@ -111,7 +113,7 @@ export function createSyncularHono(options) {
|
|
|
111
113
|
if (auth === null)
|
|
112
114
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
113
115
|
try {
|
|
114
|
-
const bytes = await decodeRequestBody(c.req.raw);
|
|
116
|
+
const bytes = await decodeRequestBody(c.req.raw, maxRequestBytes);
|
|
115
117
|
if (bytes === undefined)
|
|
116
118
|
return unsupportedEncoding(c, 'operations');
|
|
117
119
|
const out = await handleRemoteOperation(bytes, { ...config, ...auth }, options.operations);
|
|
@@ -169,16 +171,7 @@ export function createSyncularHono(options) {
|
|
|
169
171
|
if (auth === null)
|
|
170
172
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
171
173
|
try {
|
|
172
|
-
|
|
173
|
-
const contentType = c.req.header('content-type')?.split(';')[0]?.trim();
|
|
174
|
-
await handleBlobUpload({ ...config, ...auth }, {
|
|
175
|
-
blobId: c.req.param('blobId'),
|
|
176
|
-
bytes,
|
|
177
|
-
...(contentType !== undefined &&
|
|
178
|
-
contentType !== 'application/octet-stream'
|
|
179
|
-
? { mediaType: contentType }
|
|
180
|
-
: {}),
|
|
181
|
-
});
|
|
174
|
+
await handleBlobUpload({ ...config, ...auth }, c.req.param('blobId'), c.req.raw);
|
|
182
175
|
return c.body(null, 200);
|
|
183
176
|
}
|
|
184
177
|
catch (error) {
|
|
@@ -214,13 +207,22 @@ export function createSyncularHono(options) {
|
|
|
214
207
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
215
208
|
try {
|
|
216
209
|
const result = await handleBlobDownload({ ...config, ...auth }, c.req.param('blobId'));
|
|
217
|
-
if (
|
|
218
|
-
return c.json({ url: result.url, urlExpiresAtMs: result.urlExpiresAtMs }, 200);
|
|
210
|
+
if ('url' in result) {
|
|
211
|
+
return c.json({ url: result.url, urlExpiresAtMs: result.urlExpiresAtMs }, 200, result.headers);
|
|
219
212
|
}
|
|
220
213
|
if (c.req.header('if-none-match') === result.headers.ETag) {
|
|
214
|
+
await result.body.cancel();
|
|
221
215
|
return c.body(null, 304, result.headers);
|
|
222
216
|
}
|
|
223
|
-
|
|
217
|
+
// §5.9.5: blob bytes are relayed as stored with identity encoding, so
|
|
218
|
+
// no request recompresses them and no body is buffered in the host.
|
|
219
|
+
return new Response(result.body, {
|
|
220
|
+
status: 200,
|
|
221
|
+
headers: c.newResponse(null, 200, {
|
|
222
|
+
...result.headers,
|
|
223
|
+
'Content-Length': String(result.byteLength),
|
|
224
|
+
}).headers,
|
|
225
|
+
});
|
|
224
226
|
}
|
|
225
227
|
catch (error) {
|
|
226
228
|
return errorResponse(c, error, 'blobs');
|
|
@@ -229,8 +231,8 @@ export function createSyncularHono(options) {
|
|
|
229
231
|
return app;
|
|
230
232
|
}
|
|
231
233
|
/**
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
+
* The buffered routes — sync, operations and segments — encode their own
|
|
235
|
+
* body (§1.3) and declare `Content-Encoding`. workerd encodes every
|
|
234
236
|
* response that declares one again unless `encodeBody: 'manual'` marks the
|
|
235
237
|
* body as encoded, so a Workers client would decode gzip inside gzip and fail
|
|
236
238
|
* §5.1 verification (SYNCULAR-WORKERS-SEGMENT-ENCODING-001). Other runtimes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server-hono",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.59.0",
|
|
4
4
|
"description": "Hono adapter for the Syncular sync server",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -45,11 +45,11 @@
|
|
|
45
45
|
"!dist/**/*.test.d.ts"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@syncular/server": "0.
|
|
48
|
+
"@syncular/server": "0.59.0",
|
|
49
49
|
"hono": "^4.13.11"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@syncular/core": "0.
|
|
52
|
+
"@syncular/core": "0.59.0",
|
|
53
53
|
"happy-dom": "^20.0.0"
|
|
54
54
|
}
|
|
55
55
|
}
|
package/src/admin.ts
CHANGED
|
@@ -127,6 +127,20 @@ export function createSyncularAdminRoutes(
|
|
|
127
127
|
|
|
128
128
|
/** Run the host guards; actions also need `authorizeActions`. */
|
|
129
129
|
async function guard(request: Request, partition: string): Promise<void> {
|
|
130
|
+
// A cookie-authenticated operator's browser must not run an action for
|
|
131
|
+
// another site. The origin check compares hosts, so a proxy that ends TLS
|
|
132
|
+
// in front of the server still passes it.
|
|
133
|
+
if (request.method === 'POST') {
|
|
134
|
+
const site = request.headers.get('sec-fetch-site');
|
|
135
|
+
const origin = request.headers.get('origin');
|
|
136
|
+
if (
|
|
137
|
+
(site !== null && site !== 'same-origin' && site !== 'none') ||
|
|
138
|
+
(origin !== null &&
|
|
139
|
+
URL.parse(origin)?.host !== new URL(request.url).host)
|
|
140
|
+
) {
|
|
141
|
+
throw new SyncError('sync.forbidden', 'cross-site admin request');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
130
144
|
const context = { partition, request };
|
|
131
145
|
if (!(await options.authorize(context))) {
|
|
132
146
|
throw new SyncError('sync.auth_required');
|
package/src/index.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
type RemoteOperationRegistry,
|
|
20
20
|
type RemoteOperationResolver,
|
|
21
21
|
handleSyncRequest,
|
|
22
|
+
limitsOf,
|
|
22
23
|
RealtimeHub,
|
|
23
24
|
RingBufferEvents,
|
|
24
25
|
SEGMENT_STREAM_THRESHOLD_BYTES,
|
|
@@ -61,6 +62,8 @@ export interface SyncularHonoAdminOptions extends SyncularAdminGuards {
|
|
|
61
62
|
export function createSyncularHono(options: SyncularHonoOptions): Hono {
|
|
62
63
|
const app = new Hono();
|
|
63
64
|
let config = options.config;
|
|
65
|
+
// Validates every configured limit once, at creation.
|
|
66
|
+
const { maxRequestBytes } = limitsOf(config);
|
|
64
67
|
const admin = options.admin;
|
|
65
68
|
if (admin !== undefined) {
|
|
66
69
|
const ring = new RingBufferEvents(
|
|
@@ -123,7 +126,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
|
|
|
123
126
|
),
|
|
124
127
|
415,
|
|
125
128
|
);
|
|
126
|
-
// §1.3: every buffered body — a sync round, an operation result, a
|
|
129
|
+
// §1.3: every buffered body — a sync round, an operation result, a
|
|
127
130
|
// segment — is encoded per the request's `Accept-Encoding` (zstd preferred,
|
|
128
131
|
// gzip fallback, identity otherwise) and carries `Vary: Accept-Encoding`.
|
|
129
132
|
// Headers are built through the Hono context so host middleware headers
|
|
@@ -175,7 +178,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
|
|
|
175
178
|
if (auth === null)
|
|
176
179
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
177
180
|
try {
|
|
178
|
-
const bytes = await decodeRequestBody(c.req.raw);
|
|
181
|
+
const bytes = await decodeRequestBody(c.req.raw, maxRequestBytes);
|
|
179
182
|
if (bytes === undefined) return unsupportedEncoding(c, 'sync');
|
|
180
183
|
const out = await handleSyncRequest(bytes, {
|
|
181
184
|
...config,
|
|
@@ -201,7 +204,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
|
|
|
201
204
|
if (auth === null)
|
|
202
205
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
203
206
|
try {
|
|
204
|
-
const bytes = await decodeRequestBody(c.req.raw);
|
|
207
|
+
const bytes = await decodeRequestBody(c.req.raw, maxRequestBytes);
|
|
205
208
|
if (bytes === undefined) return unsupportedEncoding(c, 'operations');
|
|
206
209
|
const out = await handleRemoteOperation(
|
|
207
210
|
bytes,
|
|
@@ -267,18 +270,10 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
|
|
|
267
270
|
if (auth === null)
|
|
268
271
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
269
272
|
try {
|
|
270
|
-
const bytes = new Uint8Array(await c.req.arrayBuffer());
|
|
271
|
-
const contentType = c.req.header('content-type')?.split(';')[0]?.trim();
|
|
272
273
|
await handleBlobUpload(
|
|
273
274
|
{ ...config, ...auth },
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
bytes,
|
|
277
|
-
...(contentType !== undefined &&
|
|
278
|
-
contentType !== 'application/octet-stream'
|
|
279
|
-
? { mediaType: contentType }
|
|
280
|
-
: {}),
|
|
281
|
-
},
|
|
275
|
+
c.req.param('blobId'),
|
|
276
|
+
c.req.raw,
|
|
282
277
|
);
|
|
283
278
|
return c.body(null, 200);
|
|
284
279
|
} catch (error) {
|
|
@@ -324,20 +319,26 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
|
|
|
324
319
|
{ ...config, ...auth },
|
|
325
320
|
c.req.param('blobId'),
|
|
326
321
|
);
|
|
327
|
-
if (
|
|
322
|
+
if ('url' in result) {
|
|
328
323
|
return c.json(
|
|
329
324
|
{ url: result.url, urlExpiresAtMs: result.urlExpiresAtMs },
|
|
330
325
|
200,
|
|
326
|
+
result.headers,
|
|
331
327
|
);
|
|
332
328
|
}
|
|
333
329
|
if (c.req.header('if-none-match') === result.headers.ETag) {
|
|
330
|
+
await result.body.cancel();
|
|
334
331
|
return c.body(null, 304, result.headers);
|
|
335
332
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
333
|
+
// §5.9.5: blob bytes are relayed as stored with identity encoding, so
|
|
334
|
+
// no request recompresses them and no body is buffered in the host.
|
|
335
|
+
return new Response(result.body, {
|
|
336
|
+
status: 200,
|
|
337
|
+
headers: c.newResponse(null, 200, {
|
|
338
|
+
...result.headers,
|
|
339
|
+
'Content-Length': String(result.byteLength),
|
|
340
|
+
}).headers,
|
|
341
|
+
});
|
|
341
342
|
} catch (error) {
|
|
342
343
|
return errorResponse(c, error, 'blobs');
|
|
343
344
|
}
|
|
@@ -346,8 +347,8 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
|
|
|
346
347
|
return app;
|
|
347
348
|
}
|
|
348
349
|
/**
|
|
349
|
-
*
|
|
350
|
-
*
|
|
350
|
+
* The buffered routes — sync, operations and segments — encode their own
|
|
351
|
+
* body (§1.3) and declare `Content-Encoding`. workerd encodes every
|
|
351
352
|
* response that declares one again unless `encodeBody: 'manual'` marks the
|
|
352
353
|
* body as encoded, so a Workers client would decode gzip inside gzip and fail
|
|
353
354
|
* §5.1 verification (SYNCULAR-WORKERS-SEGMENT-ENCODING-001). Other runtimes
|