UNPKG

@aziontech/opennextjs-azion

Version:
65 lines (64 loc) 2.63 kB
/** * This code was originally copied and modified from the @opennextjs/cloudflare repository. * Significant changes have been made to adapt it for use with Azion. */ import { Writable } from "node:stream"; // Response with null body status (101, 204, 205, or 304) cannot have a body. const NULL_BODY_STATUSES = new Set([101, 204, 205, 304]); // TODO: in the future move to the open-next aws package const handler = async (handler, converter) => async (request, env, // eslint-disable-next-line @typescript-eslint/no-explicit-any ctx, abortSignal) => { globalThis.process = process; // Set the environment variables for (const [key, value] of Object.entries(env)) { if (typeof value === "string") { process.env[key] = value; } } const internalEvent = await converter.convertFrom(request); const url = new URL(request.url); // @ts-ignore const { promise: promiseResponse, resolve: resolveResponse } = Promise.withResolvers(); const streamCreator = { writeHeaders(prelude) { const { statusCode, cookies, headers } = prelude; const responseHeaders = new Headers(headers); for (const cookie of cookies) { responseHeaders.append("Set-Cookie", cookie); } if (url.hostname === "localhost") { responseHeaders.set("Content-Encoding", "identity"); } // @ts-ignore const { readable, writable } = new TransformStream({ // @ts-ignore transform(chunk, controller) { controller.enqueue(Uint8Array.from(chunk.chunk ?? chunk)); }, }); const body = NULL_BODY_STATUSES.has(statusCode) ? null : readable; const response = new Response(body, { status: statusCode, headers: responseHeaders, }); resolveResponse(response); // @ts-ignore return Writable.fromWeb(writable); }, // This is for passing along the original abort signal from the initial Request you retrieve in your worker // Ensures that the response we pass to NextServer is aborted if the request is aborted // By doing this `request.signal.onabort` will work in route handlers abortSignal: abortSignal, }; ctx.waitUntil(handler(internalEvent, { streamCreator, waitUntil: ctx.waitUntil.bind(ctx), })); return promiseResponse; }; export default { wrapper: handler, name: "azion-wrapper-node", supportStreaming: true, };