Skip to content

Commit

Permalink
feat: remove references to fetch polyfills (#7230)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-ebey authored Aug 22, 2023
1 parent 50cdfec commit 84738f1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
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
34 changes: 15 additions & 19 deletions packages/remix-express/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// IDK why this is needed when it's in the tsconfig..........
// YAY PROJECT REFERENCES!
/// <reference lib="DOM.Iterable" />

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 +52,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 +65,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 +86,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

0 comments on commit 84738f1

Please sign in to comment.