Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: RemixSite can toggle between ApiG and Edge via flag
- Loading branch information
Showing
7 changed files
with
868 additions
and
557 deletions.
There are no files selected for viewing
5 changes: 0 additions & 5 deletions
5
packages/resources/assets/RemixSite/server-lambda-stub/index.js
This file was deleted.
Oops, something went wrong.
169 changes: 169 additions & 0 deletions
169
packages/resources/assets/RemixSite/server/apig-server-template.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// This is a custom APIGatewayV2 Lambda handler which imports the Remix server | ||
// build and performs the Remix server rendering. | ||
|
||
// We have to ensure that our polyfills are imported prior to any other modules | ||
// which may depend on them; | ||
import "./polyfills.js"; | ||
|
||
import { | ||
Headers as NodeHeaders, | ||
Request as NodeRequest, | ||
createRequestHandler as createRemixRequestHandler, | ||
readableStreamToString, | ||
installGlobals, | ||
} from "@remix-run/node"; | ||
|
||
// Import the server build that was produced by `remix build`; | ||
import * as remixServerBuild from "./index.js"; | ||
|
||
/** | ||
* Common binary MIME types | ||
*/ | ||
const binaryTypes = [ | ||
"application/octet-stream", | ||
// Docs | ||
"application/epub+zip", | ||
"application/msword", | ||
"application/pdf", | ||
"application/rtf", | ||
"application/vnd.amazon.ebook", | ||
"application/vnd.ms-excel", | ||
"application/vnd.ms-powerpoint", | ||
"application/vnd.openxmlformats-officedocument.presentationml.presentation", | ||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", | ||
// Fonts | ||
"font/otf", | ||
"font/woff", | ||
"font/woff2", | ||
// Images | ||
"image/bmp", | ||
"image/gif", | ||
"image/jpeg", | ||
"image/png", | ||
"image/tiff", | ||
"image/vnd.microsoft.icon", | ||
"image/webp", | ||
// Audio | ||
"audio/3gpp", | ||
"audio/aac", | ||
"audio/basic", | ||
"audio/mpeg", | ||
"audio/ogg", | ||
"audio/wavaudio/webm", | ||
"audio/x-aiff", | ||
"audio/x-midi", | ||
"audio/x-wav", | ||
// Video | ||
"video/3gpp", | ||
"video/mp2t", | ||
"video/mpeg", | ||
"video/ogg", | ||
"video/quicktime", | ||
"video/webm", | ||
"video/x-msvideo", | ||
// Archives | ||
"application/java-archive", | ||
"application/vnd.apple.installer+xml", | ||
"application/x-7z-compressed", | ||
"application/x-apple-diskimage", | ||
"application/x-bzip", | ||
"application/x-bzip2", | ||
"application/x-gzip", | ||
"application/x-java-archive", | ||
"application/x-rar-compressed", | ||
"application/x-tar", | ||
"application/x-zip", | ||
"application/zip", | ||
]; | ||
|
||
function isBinaryType(contentType) { | ||
if (!contentType) return false; | ||
return binaryTypes.some((t) => contentType.includes(t)); | ||
} | ||
|
||
function createRemixRequest(event) { | ||
let host = event.headers["x-forwarded-host"] || event.headers.host; | ||
let search = event.rawQueryString.length ? `?${event.rawQueryString}` : ""; | ||
let scheme = "https"; | ||
let url = new URL(event.rawPath + search, `${scheme}://${host}`); | ||
let isFormData = event.headers["content-type"]?.includes( | ||
"multipart/form-data" | ||
); | ||
|
||
return new NodeRequest(url.href, { | ||
method: event.requestContext.http.method, | ||
headers: createRemixHeaders(event.headers, event.cookies), | ||
body: | ||
event.body && event.isBase64Encoded | ||
? isFormData | ||
? Buffer.from(event.body, "base64") | ||
: Buffer.from(event.body, "base64").toString() | ||
: event.body, | ||
}); | ||
} | ||
|
||
function createRemixHeaders(requestHeaders, requestCookies) { | ||
let headers = new NodeHeaders(); | ||
|
||
for (let [header, value] of Object.entries(requestHeaders)) { | ||
if (value) { | ||
headers.append(header, value); | ||
} | ||
} | ||
|
||
if (requestCookies) { | ||
headers.append("Cookie", requestCookies.join("; ")); | ||
} | ||
|
||
return headers; | ||
} | ||
|
||
async function sendRemixResponse(nodeResponse) { | ||
let cookies = []; | ||
|
||
// Arc/AWS API Gateway will send back set-cookies outside of response headers. | ||
for (let [key, values] of Object.entries(nodeResponse.headers.raw())) { | ||
if (key.toLowerCase() === "set-cookie") { | ||
for (let value of values) { | ||
cookies.push(value); | ||
} | ||
} | ||
} | ||
|
||
if (cookies.length) { | ||
nodeResponse.headers.delete("Set-Cookie"); | ||
} | ||
|
||
let contentType = nodeResponse.headers.get("Content-Type"); | ||
let isBase64Encoded = isBinaryType(contentType); | ||
let body; | ||
|
||
if (nodeResponse.body) { | ||
if (isBase64Encoded) { | ||
body = await readableStreamToString(nodeResponse.body, "base64"); | ||
} else { | ||
body = await nodeResponse.text(); | ||
} | ||
} | ||
|
||
return { | ||
statusCode: nodeResponse.status, | ||
headers: Object.fromEntries(nodeResponse.headers.entries()), | ||
cookies, | ||
body, | ||
isBase64Encoded, | ||
}; | ||
} | ||
|
||
const createAPIGatewayV2RequestHandler = (build) => { | ||
const requestHandler = createRemixRequestHandler(build, process.env.NODE_ENV); | ||
|
||
return async (event) => { | ||
const request = createRemixRequest(event); | ||
const response = await requestHandler(request); | ||
return sendRemixResponse(response); | ||
}; | ||
}; | ||
|
||
export const handler = createAPIGatewayV2RequestHandler(remixServerBuild); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { installGlobals } from "@remix-run/node"; | ||
|
||
installGlobals(); |
Oops, something went wrong.