@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/src/index.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import {
8
8
  adapterSyncError,
9
+ composeEvents,
9
10
  encodeSegmentBody,
10
11
  encodeSegmentStream,
11
12
  errorBody,
@@ -16,13 +17,17 @@ import {
16
17
  handleRemoteOperation,
17
18
  type RemoteOperationRegistry,
18
19
  handleSyncRequest,
20
+ RealtimeHub,
21
+ RingBufferEvents,
19
22
  SEGMENT_STREAM_THRESHOLD_BYTES,
20
23
  SSP2_CONTENT_TYPE,
21
24
  SyncError,
25
+ SyncularAdmin,
22
26
  type SyncularErrorRoute,
23
27
  type SyncServerConfig,
24
28
  } from '@syncular/server';
25
29
  import { Hono, type Context } from 'hono';
30
+ import { createSyncularAdminRoutes, type SyncularAdminGuards } from './admin';
26
31
 
27
32
  export * from './admin';
28
33
 
@@ -33,10 +38,53 @@ export interface SyncularHonoOptions {
33
38
  readonly authenticate: (
34
39
  request: Request,
35
40
  ) => Promise<{ actorId: string; partition: string } | null>;
41
+ /** Mount the admin console (see `SyncularHonoAdminOptions`). */
42
+ readonly admin?: SyncularHonoAdminOptions;
43
+ }
44
+
45
+ /**
46
+ * The admin console. The app mounts it at `path` and keeps an event ring of
47
+ * `ringCapacity` events, composed into `config.events` and into the
48
+ * realtime hub of `config.realtime`, so the console sees HTTP and socket
49
+ * rounds.
50
+ */
51
+ export interface SyncularHonoAdminOptions extends SyncularAdminGuards {
52
+ /** The mount path; default `/admin`. */
53
+ readonly path?: string;
54
+ /** Events the console keeps in memory; default 1000. */
55
+ readonly ringCapacity?: number;
36
56
  }
37
57
 
38
58
  export function createSyncularHono(options: SyncularHonoOptions): Hono {
39
59
  const app = new Hono();
60
+ let config = options.config;
61
+ const admin = options.admin;
62
+ if (admin !== undefined) {
63
+ const ring = new RingBufferEvents(
64
+ admin.ringCapacity !== undefined
65
+ ? { capacity: admin.ringCapacity }
66
+ : undefined,
67
+ );
68
+ config = {
69
+ ...config,
70
+ events:
71
+ config.events === undefined ? ring : composeEvents(config.events, ring),
72
+ };
73
+ if (config.realtime instanceof RealtimeHub) {
74
+ config.realtime.addEventSink(ring);
75
+ }
76
+ app.route(
77
+ admin.path ?? '/admin',
78
+ createSyncularAdminRoutes(SyncularAdmin.fromConfig(config, { ring }), {
79
+ authorize: admin.authorize,
80
+ ...(admin.authorizeActions !== undefined
81
+ ? { authorizeActions: admin.authorizeActions }
82
+ : {}),
83
+ ...(config.onError !== undefined ? { onError: config.onError } : {}),
84
+ ...(config.mapError !== undefined ? { mapError: config.mapError } : {}),
85
+ }),
86
+ );
87
+ }
40
88
  // A `SyncError` answers with its catalog status. Any other exception goes
41
89
  // to `config.onError` and answers 500 `sync.internal_error` (§10.2).
42
90
  const errorResponse = (
@@ -46,9 +94,9 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
46
94
  ): Response => {
47
95
  const sync = adapterSyncError(
48
96
  error,
49
- (error, context) => options.config.onError?.(error, context),
97
+ (error, context) => config.onError?.(error, context),
50
98
  route,
51
- (error, context) => options.config.mapError?.(error, context),
99
+ (error, context) => config.mapError?.(error, context),
52
100
  );
53
101
  const response = Response.json(errorBody(sync), {
54
102
  status: sync.httpStatus,
@@ -84,7 +132,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
84
132
  try {
85
133
  const bytes = new Uint8Array(await c.req.arrayBuffer());
86
134
  const out = await handleSyncRequest(bytes, {
87
- ...options.config,
135
+ ...config,
88
136
  ...auth,
89
137
  });
90
138
  return c.body(out.slice().buffer as ArrayBuffer, 200, {
@@ -110,7 +158,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
110
158
  const bytes = new Uint8Array(await c.req.arrayBuffer());
111
159
  const out = await handleRemoteOperation(
112
160
  bytes,
113
- { ...options.config, ...auth },
161
+ { ...config, ...auth },
114
162
  options.operations,
115
163
  );
116
164
  return c.body(out.slice().buffer as ArrayBuffer, 200, {
@@ -127,7 +175,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
127
175
  return errorResponse(c, new SyncError('sync.auth_required'));
128
176
  try {
129
177
  const result = await openSegmentDownload(
130
- { ...options.config, ...auth },
178
+ { ...config, ...auth },
131
179
  {
132
180
  segmentId: c.req.param('segmentId'),
133
181
  scopesHeader: c.req.header('x-syncular-scopes') ?? '{}',
@@ -185,7 +233,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
185
233
  const bytes = new Uint8Array(await c.req.arrayBuffer());
186
234
  const contentType = c.req.header('content-type')?.split(';')[0]?.trim();
187
235
  await handleBlobUpload(
188
- { ...options.config, ...auth },
236
+ { ...config, ...auth },
189
237
  {
190
238
  blobId: c.req.param('blobId'),
191
239
  bytes,
@@ -212,7 +260,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
212
260
  mediaType?: string;
213
261
  };
214
262
  const grant = await handleBlobUploadGrant(
215
- { ...options.config, ...auth },
263
+ { ...config, ...auth },
216
264
  {
217
265
  blobId: c.req.param('blobId'),
218
266
  byteLength: Number(body.byteLength ?? 0),
@@ -236,7 +284,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
236
284
  return errorResponse(c, new SyncError('sync.auth_required'));
237
285
  try {
238
286
  const result = await handleBlobDownload(
239
- { ...options.config, ...auth },
287
+ { ...config, ...auth },
240
288
  c.req.param('blobId'),
241
289
  );
242
290
  if (result.url !== undefined) {