|
| 1 | +import type fastify from 'fastify'; |
| 2 | +import { registerFastifyRoutes } from './route-register.js'; |
| 3 | + |
| 4 | +import * as system from '../system/system-index.js'; |
| 5 | + |
| 6 | +import { ADMIN_ROUTES } from './endpoints/admin.js'; |
| 7 | +import { CHECKPOINT_ROUTES } from './endpoints/checkpointing.js'; |
| 8 | +import { DEV_ROUTES } from './endpoints/dev.js'; |
| 9 | +import { SYNC_RULES_ROUTES } from './endpoints/sync-rules.js'; |
| 10 | +import { SYNC_STREAM_ROUTES } from './endpoints/sync-stream.js'; |
| 11 | +import { createRequestQueueHook, CreateRequestQueueParams } from './hooks.js'; |
| 12 | +import { RouteDefinition } from './router.js'; |
| 13 | + |
| 14 | +/** |
| 15 | + * A list of route definitions to be registered as endpoints. |
| 16 | + * Supplied concurrency limits will be applied to the grouped routes. |
| 17 | + */ |
| 18 | +export type RouteRegistrationOptions = { |
| 19 | + routes: RouteDefinition[]; |
| 20 | + queueOptions: CreateRequestQueueParams; |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * HTTP routes separated by API and Sync stream categories. |
| 25 | + * This allows for separate concurrency limits. |
| 26 | + */ |
| 27 | +export type RouteDefinitions = { |
| 28 | + api?: Partial<RouteRegistrationOptions>; |
| 29 | + syncStream?: Partial<RouteRegistrationOptions>; |
| 30 | +}; |
| 31 | + |
| 32 | +export type FastifyServerConfig = { |
| 33 | + system: system.CorePowerSyncSystem; |
| 34 | + routes?: RouteDefinitions; |
| 35 | +}; |
| 36 | + |
| 37 | +export const DEFAULT_ROUTE_OPTIONS = { |
| 38 | + api: { |
| 39 | + routes: [...ADMIN_ROUTES, ...CHECKPOINT_ROUTES, ...DEV_ROUTES, ...SYNC_RULES_ROUTES], |
| 40 | + queueOptions: { |
| 41 | + concurrency: 10, |
| 42 | + max_queue_depth: 20 |
| 43 | + } |
| 44 | + }, |
| 45 | + syncStream: { |
| 46 | + routes: [...SYNC_STREAM_ROUTES], |
| 47 | + queueOptions: { |
| 48 | + concurrency: 200, |
| 49 | + max_queue_depth: 0 |
| 50 | + } |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Registers default routes on a Fastify server. Consumers can optionally configure |
| 56 | + * concurrency queue limits or override routes. |
| 57 | + */ |
| 58 | +export function configureFastifyServer(server: fastify.FastifyInstance, options: FastifyServerConfig) { |
| 59 | + const { system, routes = DEFAULT_ROUTE_OPTIONS } = options; |
| 60 | + /** |
| 61 | + * Fastify creates an encapsulated context for each `.register` call. |
| 62 | + * Creating a separate context here to separate the concurrency limits for Admin APIs |
| 63 | + * and Sync Streaming routes. |
| 64 | + * https://github.com/fastify/fastify/blob/main/docs/Reference/Encapsulation.md |
| 65 | + */ |
| 66 | + server.register(async function (childContext) { |
| 67 | + registerFastifyRoutes( |
| 68 | + childContext, |
| 69 | + async () => { |
| 70 | + return { |
| 71 | + user_id: undefined, |
| 72 | + system: system |
| 73 | + }; |
| 74 | + }, |
| 75 | + routes.api?.routes ?? DEFAULT_ROUTE_OPTIONS.api.routes |
| 76 | + ); |
| 77 | + // Limit the active concurrent requests |
| 78 | + childContext.addHook( |
| 79 | + 'onRequest', |
| 80 | + createRequestQueueHook(routes.api?.queueOptions ?? DEFAULT_ROUTE_OPTIONS.api.queueOptions) |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + // Create a separate context for concurrency queueing |
| 85 | + server.register(async function (childContext) { |
| 86 | + registerFastifyRoutes( |
| 87 | + childContext, |
| 88 | + async () => { |
| 89 | + return { |
| 90 | + user_id: undefined, |
| 91 | + system: system |
| 92 | + }; |
| 93 | + }, |
| 94 | + routes.syncStream?.routes ?? DEFAULT_ROUTE_OPTIONS.syncStream.routes |
| 95 | + ); |
| 96 | + // Limit the active concurrent requests |
| 97 | + childContext.addHook( |
| 98 | + 'onRequest', |
| 99 | + createRequestQueueHook(routes.syncStream?.queueOptions ?? DEFAULT_ROUTE_OPTIONS.syncStream.queueOptions) |
| 100 | + ); |
| 101 | + }); |
| 102 | +} |
0 commit comments