@syncular/server-hono 0.31.0 → 0.32.1
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-page.d.ts +15 -16
- package/dist/admin-page.js +1626 -493
- package/dist/admin.d.ts +39 -12
- package/dist/admin.js +268 -203
- package/dist/index.d.ts +15 -0
- package/dist/index.js +31 -8
- package/package.json +4 -3
- package/src/admin-page.ts +1626 -493
- package/src/admin.ts +403 -218
- package/src/index.ts +56 -8
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { type RemoteOperationRegistry, type SyncServerConfig } from '@syncular/server';
|
|
8
8
|
import { Hono } from 'hono';
|
|
9
|
+
import { type SyncularAdminGuards } from './admin.js';
|
|
9
10
|
export * from './admin.js';
|
|
10
11
|
export interface SyncularHonoOptions {
|
|
11
12
|
readonly config: SyncServerConfig;
|
|
@@ -15,5 +16,19 @@ export interface SyncularHonoOptions {
|
|
|
15
16
|
actorId: string;
|
|
16
17
|
partition: string;
|
|
17
18
|
} | null>;
|
|
19
|
+
/** Mount the admin console (see `SyncularHonoAdminOptions`). */
|
|
20
|
+
readonly admin?: SyncularHonoAdminOptions;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The admin console. The app mounts it at `path` and keeps an event ring of
|
|
24
|
+
* `ringCapacity` events, composed into `config.events` and into the
|
|
25
|
+
* realtime hub of `config.realtime`, so the console sees HTTP and socket
|
|
26
|
+
* rounds.
|
|
27
|
+
*/
|
|
28
|
+
export interface SyncularHonoAdminOptions extends SyncularAdminGuards {
|
|
29
|
+
/** The mount path; default `/admin`. */
|
|
30
|
+
readonly path?: string;
|
|
31
|
+
/** Events the console keeps in memory; default 1000. */
|
|
32
|
+
readonly ringCapacity?: number;
|
|
18
33
|
}
|
|
19
34
|
export declare function createSyncularHono(options: SyncularHonoOptions): Hono;
|
package/dist/index.js
CHANGED
|
@@ -4,15 +4,38 @@
|
|
|
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, encodeSegmentBody, encodeSegmentStream, errorBody, handleBlobDownload, handleBlobUpload, handleBlobUploadGrant, openSegmentDownload, handleRemoteOperation, handleSyncRequest, SEGMENT_STREAM_THRESHOLD_BYTES, SSP2_CONTENT_TYPE, SyncError, } from '@syncular/server';
|
|
7
|
+
import { adapterSyncError, composeEvents, encodeSegmentBody, encodeSegmentStream, errorBody, handleBlobDownload, handleBlobUpload, handleBlobUploadGrant, openSegmentDownload, handleRemoteOperation, handleSyncRequest, RealtimeHub, RingBufferEvents, SEGMENT_STREAM_THRESHOLD_BYTES, SSP2_CONTENT_TYPE, SyncError, SyncularAdmin, } from '@syncular/server';
|
|
8
8
|
import { Hono } from 'hono';
|
|
9
|
+
import { createSyncularAdminRoutes } from './admin.js';
|
|
9
10
|
export * from './admin.js';
|
|
10
11
|
export function createSyncularHono(options) {
|
|
11
12
|
const app = new Hono();
|
|
13
|
+
let config = options.config;
|
|
14
|
+
const admin = options.admin;
|
|
15
|
+
if (admin !== undefined) {
|
|
16
|
+
const ring = new RingBufferEvents(admin.ringCapacity !== undefined
|
|
17
|
+
? { capacity: admin.ringCapacity }
|
|
18
|
+
: undefined);
|
|
19
|
+
config = {
|
|
20
|
+
...config,
|
|
21
|
+
events: config.events === undefined ? ring : composeEvents(config.events, ring),
|
|
22
|
+
};
|
|
23
|
+
if (config.realtime instanceof RealtimeHub) {
|
|
24
|
+
config.realtime.addEventSink(ring);
|
|
25
|
+
}
|
|
26
|
+
app.route(admin.path ?? '/admin', createSyncularAdminRoutes(SyncularAdmin.fromConfig(config, { ring }), {
|
|
27
|
+
authorize: admin.authorize,
|
|
28
|
+
...(admin.authorizeActions !== undefined
|
|
29
|
+
? { authorizeActions: admin.authorizeActions }
|
|
30
|
+
: {}),
|
|
31
|
+
...(config.onError !== undefined ? { onError: config.onError } : {}),
|
|
32
|
+
...(config.mapError !== undefined ? { mapError: config.mapError } : {}),
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
12
35
|
// A `SyncError` answers with its catalog status. Any other exception goes
|
|
13
36
|
// to `config.onError` and answers 500 `sync.internal_error` (§10.2).
|
|
14
37
|
const errorResponse = (c, error, route = 'sync') => {
|
|
15
|
-
const sync = adapterSyncError(error, (error, context) =>
|
|
38
|
+
const sync = adapterSyncError(error, (error, context) => config.onError?.(error, context), route, (error, context) => config.mapError?.(error, context));
|
|
16
39
|
const response = Response.json(errorBody(sync), {
|
|
17
40
|
status: sync.httpStatus,
|
|
18
41
|
});
|
|
@@ -37,7 +60,7 @@ export function createSyncularHono(options) {
|
|
|
37
60
|
try {
|
|
38
61
|
const bytes = new Uint8Array(await c.req.arrayBuffer());
|
|
39
62
|
const out = await handleSyncRequest(bytes, {
|
|
40
|
-
...
|
|
63
|
+
...config,
|
|
41
64
|
...auth,
|
|
42
65
|
});
|
|
43
66
|
return c.body(out.slice().buffer, 200, {
|
|
@@ -61,7 +84,7 @@ export function createSyncularHono(options) {
|
|
|
61
84
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
62
85
|
try {
|
|
63
86
|
const bytes = new Uint8Array(await c.req.arrayBuffer());
|
|
64
|
-
const out = await handleRemoteOperation(bytes, { ...
|
|
87
|
+
const out = await handleRemoteOperation(bytes, { ...config, ...auth }, options.operations);
|
|
65
88
|
return c.body(out.slice().buffer, 200, {
|
|
66
89
|
'Content-Type': 'application/vnd.syncular.operations.v1+json',
|
|
67
90
|
});
|
|
@@ -75,7 +98,7 @@ export function createSyncularHono(options) {
|
|
|
75
98
|
if (auth === null)
|
|
76
99
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
77
100
|
try {
|
|
78
|
-
const result = await openSegmentDownload({ ...
|
|
101
|
+
const result = await openSegmentDownload({ ...config, ...auth }, {
|
|
79
102
|
segmentId: c.req.param('segmentId'),
|
|
80
103
|
scopesHeader: c.req.header('x-syncular-scopes') ?? '{}',
|
|
81
104
|
});
|
|
@@ -128,7 +151,7 @@ export function createSyncularHono(options) {
|
|
|
128
151
|
try {
|
|
129
152
|
const bytes = new Uint8Array(await c.req.arrayBuffer());
|
|
130
153
|
const contentType = c.req.header('content-type')?.split(';')[0]?.trim();
|
|
131
|
-
await handleBlobUpload({ ...
|
|
154
|
+
await handleBlobUpload({ ...config, ...auth }, {
|
|
132
155
|
blobId: c.req.param('blobId'),
|
|
133
156
|
bytes,
|
|
134
157
|
...(contentType !== undefined &&
|
|
@@ -149,7 +172,7 @@ export function createSyncularHono(options) {
|
|
|
149
172
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
150
173
|
try {
|
|
151
174
|
const body = (await c.req.json().catch(() => ({})));
|
|
152
|
-
const grant = await handleBlobUploadGrant({ ...
|
|
175
|
+
const grant = await handleBlobUploadGrant({ ...config, ...auth }, {
|
|
153
176
|
blobId: c.req.param('blobId'),
|
|
154
177
|
byteLength: Number(body.byteLength ?? 0),
|
|
155
178
|
...(typeof body.mediaType === 'string'
|
|
@@ -170,7 +193,7 @@ export function createSyncularHono(options) {
|
|
|
170
193
|
if (auth === null)
|
|
171
194
|
return errorResponse(c, new SyncError('sync.auth_required'));
|
|
172
195
|
try {
|
|
173
|
-
const result = await handleBlobDownload({ ...
|
|
196
|
+
const result = await handleBlobDownload({ ...config, ...auth }, c.req.param('blobId'));
|
|
174
197
|
if (result.url !== undefined) {
|
|
175
198
|
return c.json({ url: result.url, urlExpiresAtMs: result.urlExpiresAtMs }, 200);
|
|
176
199
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server-hono",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.1",
|
|
4
4
|
"description": "Hono adapter for the Syncular sync server",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -45,10 +45,11 @@
|
|
|
45
45
|
"!dist/**/*.test.d.ts"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@syncular/server": "0.
|
|
48
|
+
"@syncular/server": "0.32.1",
|
|
49
49
|
"hono": "^4.13.7"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@syncular/core": "0.
|
|
52
|
+
"@syncular/core": "0.32.1",
|
|
53
|
+
"happy-dom": "^20.0.0"
|
|
53
54
|
}
|
|
54
55
|
}
|