All files / server/proxy index.ts

71.42% Statements 10/14
0% Branches 0/2
100% Functions 3/3
71.42% Lines 10/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44            1x           1x 53x     53x 53x   53x                         53x   53x 53x     53x          
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { ReasonPhrases, StatusCodes } from 'http-status-codes';
import { log } from '@/logger';
 
const createMiddleware =
  (
    middleware: readonly ((
      request: NextRequest,
      response: NextResponse,
    ) => Response | PromiseLike<Response>)[],
  ) =>
  async (request: Readonly<NextRequest>): Promise<Response> => {
    const modifiedResponse = new NextResponse();
 
    // stele:landmark proxy-short-circuit
    try {
      const tasks = middleware.map(async (handler) => handler(request, modifiedResponse));
 
      await Promise.all(tasks);
    } catch (error) {
      if (error instanceof NextResponse) {
        return error;
      }
 
      log.error(ReasonPhrases.INTERNAL_SERVER_ERROR, { cause: error });
 
      return new NextResponse(ReasonPhrases.INTERNAL_SERVER_ERROR, {
        status: StatusCodes.INTERNAL_SERVER_ERROR,
      });
    }
 
    const response = NextResponse.next({ request });
 
    for (const [key, value] of modifiedResponse.headers.entries()) {
      response.headers.set(key, value);
    }
 
    return response;
  };
 
export default createMiddleware;
export * from './content-security-policy';