@syncular/server-hono 0.30.16 → 0.32.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/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 = (
@@ -44,7 +92,12 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
44
92
  error: unknown,
45
93
  route: SyncularErrorRoute = 'sync',
46
94
  ): Response => {
47
- const sync = adapterSyncError(error, options.config.onError, route);
95
+ const sync = adapterSyncError(
96
+ error,
97
+ (error, context) => config.onError?.(error, context),
98
+ route,
99
+ (error, context) => config.mapError?.(error, context),
100
+ );
48
101
  const response = Response.json(errorBody(sync), {
49
102
  status: sync.httpStatus,
50
103
  });
@@ -79,7 +132,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
79
132
  try {
80
133
  const bytes = new Uint8Array(await c.req.arrayBuffer());
81
134
  const out = await handleSyncRequest(bytes, {
82
- ...options.config,
135
+ ...config,
83
136
  ...auth,
84
137
  });
85
138
  return c.body(out.slice().buffer as ArrayBuffer, 200, {
@@ -105,7 +158,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
105
158
  const bytes = new Uint8Array(await c.req.arrayBuffer());
106
159
  const out = await handleRemoteOperation(
107
160
  bytes,
108
- { ...options.config, ...auth },
161
+ { ...config, ...auth },
109
162
  options.operations,
110
163
  );
111
164
  return c.body(out.slice().buffer as ArrayBuffer, 200, {
@@ -122,7 +175,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
122
175
  return errorResponse(c, new SyncError('sync.auth_required'));
123
176
  try {
124
177
  const result = await openSegmentDownload(
125
- { ...options.config, ...auth },
178
+ { ...config, ...auth },
126
179
  {
127
180
  segmentId: c.req.param('segmentId'),
128
181
  scopesHeader: c.req.header('x-syncular-scopes') ?? '{}',
@@ -180,7 +233,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
180
233
  const bytes = new Uint8Array(await c.req.arrayBuffer());
181
234
  const contentType = c.req.header('content-type')?.split(';')[0]?.trim();
182
235
  await handleBlobUpload(
183
- { ...options.config, ...auth },
236
+ { ...config, ...auth },
184
237
  {
185
238
  blobId: c.req.param('blobId'),
186
239
  bytes,
@@ -207,7 +260,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
207
260
  mediaType?: string;
208
261
  };
209
262
  const grant = await handleBlobUploadGrant(
210
- { ...options.config, ...auth },
263
+ { ...config, ...auth },
211
264
  {
212
265
  blobId: c.req.param('blobId'),
213
266
  byteLength: Number(body.byteLength ?? 0),
@@ -231,7 +284,7 @@ export function createSyncularHono(options: SyncularHonoOptions): Hono {
231
284
  return errorResponse(c, new SyncError('sync.auth_required'));
232
285
  try {
233
286
  const result = await handleBlobDownload(
234
- { ...options.config, ...auth },
287
+ { ...config, ...auth },
235
288
  c.req.param('blobId'),
236
289
  );
237
290
  if (result.url !== undefined) {