Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove references to fetch polyfills #7230

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 6 additions & 0 deletions .changeset/remove-fetch-polyfill-references-in-adapters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@remix-run/architect": patch
"@remix-run/express": patch
---

remove references to fetch polyfills in node and arc adapters
20 changes: 7 additions & 13 deletions packages/remix-architect/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type {
AppLoadContext,
ServerBuild,
Response as NodeResponse,
} from "@remix-run/node";
import type { AppLoadContext, ServerBuild } from "@remix-run/node";
import {
Headers as NodeHeaders,
Request as NodeRequest,
createRequestHandler as createRemixRequestHandler,
readableStreamToString,
} from "@remix-run/node";
Expand Down Expand Up @@ -50,13 +44,13 @@ export function createRequestHandler({
let request = createRemixRequest(event);
let loadContext = await getLoadContext?.(event);

let response = (await handleRequest(request, loadContext)) as NodeResponse;
let response = await handleRequest(request, loadContext);

return sendRemixResponse(response);
};
}

export function createRemixRequest(event: APIGatewayProxyEventV2): NodeRequest {
export function createRemixRequest(event: APIGatewayProxyEventV2): Request {
let host = event.headers["x-forwarded-host"] || event.headers.host;
let search = event.rawQueryString.length ? `?${event.rawQueryString}` : "";
let scheme = process.env.ARC_SANDBOX ? "http" : "https";
Expand All @@ -68,7 +62,7 @@ export function createRemixRequest(event: APIGatewayProxyEventV2): NodeRequest {
// requests to contain a signal, so it can detect aborted requests
let controller = new AbortController();

return new NodeRequest(url.href, {
return new Request(url.href, {
method: event.requestContext.http.method,
headers: createRemixHeaders(event.headers, event.cookies),
signal: controller.signal,
Expand All @@ -84,8 +78,8 @@ export function createRemixRequest(event: APIGatewayProxyEventV2): NodeRequest {
export function createRemixHeaders(
requestHeaders: APIGatewayProxyEventHeaders,
requestCookies?: string[]
): NodeHeaders {
let headers = new NodeHeaders();
): Headers {
let headers = new Headers();

for (let [header, value] of Object.entries(requestHeaders)) {
if (value) {
Expand All @@ -101,7 +95,7 @@ export function createRemixHeaders(
}

export async function sendRemixResponse(
nodeResponse: NodeResponse
nodeResponse: Response
): Promise<APIGatewayProxyStructuredResultV2> {
let cookies: string[] = [];

Expand Down
30 changes: 11 additions & 19 deletions packages/remix-express/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import type * as express from "express";
import type {
AppLoadContext,
ServerBuild,
RequestInit as NodeRequestInit,
Response as NodeResponse,
} from "@remix-run/node";
import type { AppLoadContext, ServerBuild } from "@remix-run/node";
import {
createRequestHandler as createRemixRequestHandler,
Headers as NodeHeaders,
Request as NodeRequest,
createReadableStreamFromReadable,
writeReadableStreamToWritable,
} from "@remix-run/node";

Expand Down Expand Up @@ -54,10 +48,7 @@ export function createRequestHandler({
let request = createRemixRequest(req, res);
let loadContext = await getLoadContext?.(req, res);

let response = (await handleRequest(
request,
loadContext
)) as NodeResponse;
let response = await handleRequest(request, loadContext);

await sendRemixResponse(res, response);
} catch (error: unknown) {
Expand All @@ -70,8 +61,8 @@ export function createRequestHandler({

export function createRemixHeaders(
requestHeaders: express.Request["headers"]
): NodeHeaders {
let headers = new NodeHeaders();
): Headers {
let headers = new Headers();

for (let [key, values] of Object.entries(requestHeaders)) {
if (values) {
Expand All @@ -91,29 +82,30 @@ export function createRemixHeaders(
export function createRemixRequest(
req: express.Request,
res: express.Response
): NodeRequest {
): Request {
let url = new URL(`${req.protocol}://${req.get("host")}${req.url}`);

// Abort action/loaders once we can no longer write a response
let controller = new AbortController();
res.on("close", () => controller.abort());

let init: NodeRequestInit = {
let init: RequestInit = {
method: req.method,
headers: createRemixHeaders(req.headers),
signal: controller.signal,
};

if (req.method !== "GET" && req.method !== "HEAD") {
init.body = req;
init.body = createReadableStreamFromReadable(req);
(init as { duplex: "half" }).duplex = "half";
}

return new NodeRequest(url.href, init);
return new Request(url.href, init);
}

export async function sendRemixResponse(
res: express.Response,
nodeResponse: NodeResponse
nodeResponse: Response
): Promise<void> {
res.statusMessage = nodeResponse.statusText;
res.status(nodeResponse.status);
Expand Down
Loading