|
| 1 | +export type GlobalMiddlewares = |
| 2 | + import('@app-builder/core/middleware-config').MiddlewareConfig['GlobalMiddlewares']; |
| 3 | + |
| 4 | +import type { |
| 5 | + DataReturnType, |
| 6 | + ExecutionEnvironment, |
| 7 | + ExecutionEnvironmentQueueItem, |
| 8 | + Expand, |
| 9 | + MergeMiddlewareContext, |
| 10 | + MiddlewareFunction, |
| 11 | + MiddlewareObject, |
| 12 | + NextFunction, |
| 13 | + RemixDataFunctionArgs, |
| 14 | + ServerFunction, |
| 15 | +} from './middleware-types'; |
| 16 | + |
| 17 | +let globalMiddlewares: readonly MiddlewareObject[] = []; |
| 18 | + |
| 19 | +function createMiddlewareCallbable( |
| 20 | + object: MiddlewareObject, |
| 21 | + env: ExecutionEnvironment, |
| 22 | + globalEnv: ExecutionEnvironment | undefined, |
| 23 | + next: NextFunction, |
| 24 | +) { |
| 25 | + // Create a env specific to middleware queue to not pollute the global request one |
| 26 | + const middlewareEnv = createExecutionEnvironment(env); |
| 27 | + middlewareEnv.ctx.context = { ...globalEnv?.ctx.context }; |
| 28 | + |
| 29 | + const finalMiddleware: NextFunction = async (args): Promise<DataReturnType<any, any>> => { |
| 30 | + middlewareEnv.ctx.context = { ...middlewareEnv.ctx.context, ...args?.context }; |
| 31 | + return object.fn(middlewareEnv.ctx, next); |
| 32 | + }; |
| 33 | + |
| 34 | + return createMiddlewareChain(object.deps, middlewareEnv, globalEnv, finalMiddleware); |
| 35 | +} |
| 36 | + |
| 37 | +function createServerFunctionCallable( |
| 38 | + middlewares: readonly MiddlewareObject[], |
| 39 | + env: ExecutionEnvironment, |
| 40 | + fn: ServerFunction<any, any>, |
| 41 | +) { |
| 42 | + const final: NextFunction = async (args): Promise<DataReturnType<any, any>> => { |
| 43 | + env.ctx.context = { ...env.ctx.context, ...args?.context }; |
| 44 | + const res = await fn(env.ctx); |
| 45 | + const data = |
| 46 | + res instanceof Object && '__dataObject' in res && res.__dataObject ? res.data : res; |
| 47 | + |
| 48 | + return { |
| 49 | + data, |
| 50 | + __context: env.ctx.context, |
| 51 | + __headers: [...(args?.headers ?? []), ...(res.headers ?? [])], |
| 52 | + }; |
| 53 | + }; |
| 54 | + |
| 55 | + const globalEnv = createExecutionEnvironment(env); |
| 56 | + |
| 57 | + const routeMiddlewareChain = createMiddlewareChain(middlewares, env, globalEnv, final); |
| 58 | + const globalMiddlewareChain = createMiddlewareChain( |
| 59 | + globalMiddlewares, |
| 60 | + globalEnv, |
| 61 | + undefined, |
| 62 | + (nextArgs) => { |
| 63 | + globalEnv.ctx.context = { ...globalEnv.ctx.context, ...nextArgs?.context }; |
| 64 | + return routeMiddlewareChain(); |
| 65 | + }, |
| 66 | + ); |
| 67 | + |
| 68 | + return globalMiddlewareChain; |
| 69 | +} |
| 70 | + |
| 71 | +function createMiddlewareChain( |
| 72 | + middlewares: readonly MiddlewareObject[], |
| 73 | + env: ExecutionEnvironment, |
| 74 | + globalEnv: ExecutionEnvironment | undefined, |
| 75 | + final: NextFunction, |
| 76 | +): NextFunction { |
| 77 | + return middlewares.reduceRight<NextFunction>((nextFn, middleware) => { |
| 78 | + return async (args) => { |
| 79 | + env.ctx.context = { ...env.ctx.context, ...args?.context }; |
| 80 | + |
| 81 | + const queueItem = env.queue.get(middleware.fn); |
| 82 | + if (queueItem) { |
| 83 | + return nextFn(queueItem.data); |
| 84 | + } |
| 85 | + |
| 86 | + const callable = createMiddlewareCallbable(middleware, env, globalEnv, (nextArgs) => { |
| 87 | + env.queue.set(middleware.fn, { data: nextArgs }); |
| 88 | + return nextFn(nextArgs); |
| 89 | + }); |
| 90 | + |
| 91 | + return callable(); |
| 92 | + }; |
| 93 | + }, final); |
| 94 | +} |
| 95 | + |
| 96 | +function createExecutionEnvironment( |
| 97 | + ...args: [ExecutionEnvironment] | [Request, Record<string, string>] |
| 98 | +): ExecutionEnvironment { |
| 99 | + if (args[0] instanceof Request) { |
| 100 | + return { |
| 101 | + queue: new Map<Function, ExecutionEnvironmentQueueItem>(), |
| 102 | + ctx: { context: {}, request: args[0], params: args[1] ?? {} }, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + return { |
| 107 | + queue: args[0].queue, |
| 108 | + ctx: { ...args[0].ctx, context: {} }, |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +function buildResponse(ret: DataReturnType<any, any>) { |
| 113 | + const headers = new Headers(); |
| 114 | + for (const [key, value] of ret.__headers) { |
| 115 | + headers.append(key, value); |
| 116 | + } |
| 117 | + return Response.json(ret.data, { headers }); |
| 118 | +} |
| 119 | + |
| 120 | +export function createServerFn<T extends readonly MiddlewareObject[], Ret>( |
| 121 | + middlewares: readonly [...T], |
| 122 | + fn: ServerFunction<Expand<MergeMiddlewareContext<[...GlobalMiddlewares, ...T]>>, Ret>, |
| 123 | +) { |
| 124 | + return async (args: RemixDataFunctionArgs): Promise<Response> => { |
| 125 | + const env = createExecutionEnvironment(args.request, args.params); |
| 126 | + const middlewareChain = createServerFunctionCallable(middlewares, env, fn); |
| 127 | + |
| 128 | + const ret = await middlewareChain(); |
| 129 | + return buildResponse(ret); |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +export function createMiddleware<T extends readonly MiddlewareObject[], TOutContext>( |
| 134 | + middlewares: readonly [...T], |
| 135 | + fn: MiddlewareFunction<Expand<MergeMiddlewareContext<T>>, TOutContext>, |
| 136 | +): MiddlewareObject<any, TOutContext> { |
| 137 | + return { deps: middlewares, fn: fn as any }; |
| 138 | +} |
| 139 | + |
| 140 | +export function createMiddlewareWithGlobalContext< |
| 141 | + T extends readonly MiddlewareObject[], |
| 142 | + TOutContext, |
| 143 | +>( |
| 144 | + middlewares: readonly [...T], |
| 145 | + fn: MiddlewareFunction<Expand<MergeMiddlewareContext<[...GlobalMiddlewares, ...T]>>, TOutContext>, |
| 146 | +): MiddlewareObject<any, TOutContext> { |
| 147 | + return createMiddleware(middlewares, fn as any); |
| 148 | +} |
| 149 | + |
| 150 | +export function setGlobalMiddlewares<T extends readonly MiddlewareObject[]>(...middlewares: T) { |
| 151 | + globalMiddlewares = middlewares; |
| 152 | + return middlewares; |
| 153 | +} |
0 commit comments