Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/runtime/plugin-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@
},
"./cache": {
"types": "./dist/types/cache/index.d.ts",
"default": "./dist/esm/cache/index.mjs"
"node": {
"import": "./dist/esm-node/cache/index.mjs",
"require": "./dist/cjs/cache/index.js"
},
"default": "./dist/cjs/cache/index.js"
},
"./config-routes": {
"types": "./dist/types/exports/config-routes.d.ts",
Expand Down
142 changes: 140 additions & 2 deletions packages/server/prod-server/src/netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,137 @@ import type { BaseEnv, ProdServerOptions } from './types';

export type { ProdServerOptions, BaseEnv } from './types';

type NetlifyEvent = {
body?: string | null;
headers?: Record<string, string | undefined>;
httpMethod?: string;
isBase64Encoded?: boolean;
multiValueHeaders?: Record<string, Array<string | undefined> | undefined>;
path?: string;
queryStringParameters?: Record<string, string | undefined> | null;
rawQuery?: string;
rawUrl?: string;
};

type NetlifyResponse = {
body: string;
headers: Record<string, string>;
isBase64Encoded: boolean;
multiValueHeaders?: Record<string, string[]>;
statusCode: number;
};

const isRequest = (value: unknown): value is Request => {
return value instanceof Request;
};

const isTextResponse = (contentType: string | null) => {
if (!contentType) {
return true;
}

return (
contentType.startsWith('text/') ||
contentType.includes('application/json') ||
contentType.includes('application/javascript') ||
contentType.includes('application/xml') ||
contentType.includes('application/x-www-form-urlencoded') ||
contentType.includes('image/svg+xml')
);
};

const getRequestUrl = (event: NetlifyEvent) => {
if (event.rawUrl) {
return event.rawUrl;
}

const headers = event.headers ?? {};
const protocol = headers['x-forwarded-proto'] ?? 'https';
const host = headers['x-forwarded-host'] ?? headers.host ?? 'localhost';
const pathname = event.path ?? '/';

if (event.rawQuery) {
return `${protocol}://${host}${pathname}?${event.rawQuery}`;
}

const search = new URLSearchParams();
for (const [key, value] of Object.entries(event.queryStringParameters ?? {})) {
if (value !== undefined) {
search.append(key, value);
}
}

const query = search.toString();
return `${protocol}://${host}${pathname}${query ? `?${query}` : ''}`;
};

const createWebRequest = (event: NetlifyEvent): Request => {
const headers = new Headers();

for (const [key, values] of Object.entries(event.multiValueHeaders ?? {})) {
if (!values) {
continue;
}

for (const value of values) {
if (value !== undefined) {
headers.append(key, value);
}
}
}

for (const [key, value] of Object.entries(event.headers ?? {})) {
if (value !== undefined && !headers.has(key)) {
headers.set(key, value);
}
}

const method = event.httpMethod ?? 'GET';
const init: RequestInit & { duplex?: 'half' } = {
headers,
method,
};

if (!(method === 'GET' || method === 'HEAD') && event.body) {
init.body = event.isBase64Encoded
? Buffer.from(event.body, 'base64')
: event.body;
init.duplex = 'half';
}

return new Request(getRequestUrl(event), init);
};

const createNetlifyResponse = async (
response: Response,
): Promise<NetlifyResponse> => {
const headers: Record<string, string> = {};
for (const [key, value] of response.headers.entries()) {
if (key !== 'set-cookie') {
headers[key] = value;
}
}

const setCookie =
typeof response.headers.getSetCookie === 'function'
? response.headers.getSetCookie()
: [];

const contentType = response.headers.get('content-type');
const buffer = Buffer.from(await response.arrayBuffer());
const isBase64Encoded = !isTextResponse(contentType);

return {
statusCode: response.status,
headers,
body: isBase64Encoded ? buffer.toString('base64') : buffer.toString('utf8'),
isBase64Encoded,
...(setCookie.length > 0
? { multiValueHeaders: { 'set-cookie': setCookie } }
: {}),
};
};

export const createNetlifyFunction = async (options: ProdServerOptions) => {
await loadServerEnv(options);

Expand All @@ -35,7 +166,14 @@ export const createNetlifyFunction = async (options: ProdServerOptions) => {

await applyPlugins(server, options);
await server.init();
return (request: Request, context: unknown) => {
return server.handle(request, context);
return async (requestOrEvent: Request | NetlifyEvent, context: unknown) => {
if (isRequest(requestOrEvent)) {
return server.handle(requestOrEvent, context);
}

const request = createWebRequest(requestOrEvent);
const response = await server.handle(request, context);

return createNetlifyResponse(response);
};
};