Skip to content

Commit

Permalink
bun
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <[email protected]>
  • Loading branch information
mcansh committed Sep 11, 2023
1 parent 21e8390 commit 16c06dc
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 73 deletions.
75 changes: 25 additions & 50 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,48 @@
import crypto from "node:crypto";
import { Readable } from "node:stream";
import type { EntryContext, HandleDataRequestFunction } from "@remix-run/node";
import { Response } from "@remix-run/node";
import type {
EntryContext,
HandleDataRequestFunction,
} from "@remix-run/server-runtime";
import { RemixServer } from "@remix-run/react";
import { createSecureHeaders } from "@mcansh/remix-secure-headers";
import { renderToPipeableStream } from "react-dom/server";
import { isPrefetch, preloadRouteAssets } from "remix-utils";
import isbot from "isbot";
import { renderToReadableStream } from "react-dom/server";

import { NonceContext } from "./components/nonce";

const ABORT_DELAY = 5_000;

export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
) {
preloadRouteAssets(remixContext, responseHeaders);
let callback = isbot(request.headers.get("user-agent"))
? "onAllReady"
: "onShellReady";

let nonce = applySecurityHeaders(responseHeaders);

return new Promise((resolve, reject) => {
let shellRendered = false;
let { pipe, abort } = renderToPipeableStream(
<NonceContext.Provider value={nonce}>
<RemixServer
context={remixContext}
url={request.url}
abortDelay={ABORT_DELAY}
/>
</NonceContext.Provider>,
{
nonce,
[callback]() {
shellRendered = true;
let body = new Readable.PassThrough();

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(Readable.toWeb(body) as ReadableStream, {
headers: responseHeaders,
status: responseStatusCode,
}),
);

pipe(body);
},
onShellError(error) {
reject(error);
},
onError(error) {
responseStatusCode = 500;
// Log streaming rendering errors from inside the shell.
// Don't log errors encountered during initial shell rendering since they'll
// reject and get logged in handleDocumentRequest.
if (shellRendered) {
console.error(error);
}
},
let body = await renderToReadableStream(
<NonceContext.Provider value={nonce}>
<RemixServer context={remixContext} url={request.url} />
</NonceContext.Provider>,
{
nonce,
onError(error, errorInfo) {
// log streaming render errors from inside the shell
console.error(error, errorInfo);
responseStatusCode = 500;
},
);
},
);

if (isbot(request.headers.get("user-agent"))) {
await body.allReady;
}

setTimeout(abort, ABORT_DELAY);
responseHeaders.set("Content-Type", "text/html");
return new Response(body, {
status: responseStatusCode,
headers: responseHeaders,
});
}

Expand Down
5 changes: 4 additions & 1 deletion app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as React from "react";
import type { LinksFunction, V2_MetaFunction } from "@remix-run/node";
import type {
LinksFunction,
V2_ServerRuntimeMetaFunction as V2_MetaFunction,
} from "@remix-run/server-runtime";
import {
isRouteErrorResponse,
Links,
Expand Down
2 changes: 1 addition & 1 deletion app/routes/[.well-known].avatar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DataFunctionArgs } from "@remix-run/node";
import type { DataFunctionArgs } from "@remix-run/server-runtime";

import { getCloudinaryURL } from "~/cloudinary.server";

Expand Down
2 changes: 1 addition & 1 deletion app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
json,
type HeadersFunction,
type LinksFunction,
} from "@remix-run/node";
} from "@remix-run/server-runtime";
import { useLoaderData } from "@remix-run/react";
import { cacheHeader } from "pretty-cache-header";

Expand Down
4 changes: 2 additions & 2 deletions app/routes/resume.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { HeadersFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import type { HeadersFunction } from "@remix-run/server-runtime";
import { json } from "@remix-run/server-runtime";
import type { V2_MetaFunction } from "@remix-run/react";
import { useLoaderData } from "@remix-run/react";
import { cacheHeader } from "pretty-cache-header";
Expand Down
Binary file modified bun.lockb
Binary file not shown.
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"author": "Logan McAnsh <[email protected]> (https://mcan.sh)",
"license": "MIT",
"sideEffects": false,
"type": "commonjs",
"type": "module",
"scripts": {
"build": "run-s type-check && remix build",
"clean": "./scripts/clean.mjs",
"dev": "remix dev",
"dev": "remix dev -c \"bun server.ts\"",
"type-check": "tsc",
"lint": "eslint --ignore-path .gitignore --cache --fix .",
"format": "prettier --ignore-path .gitignore --ignore-unknown --write .",
Expand All @@ -18,16 +18,14 @@
},
"dependencies": {
"@mcansh/remix-secure-headers": "^0.7.2",
"@remix-run/node": "1.19.3",
"@remix-run/react": "1.19.3",
"@remix-run/server-runtime": "1.19.3",
"cloudinary-build-url": "^0.2.4",
"clsx": "^2.0.0",
"compression": "^1.7.4",
"cross-env": "^7.0.3",
"fathom-client": "^3.5.0",
"is-ci": "^3.0.1",
"isbot": "^3.6.13",
"morgan": "^1.10.0",
"pretty-cache-header": "^1.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -38,14 +36,11 @@
"@cld-apis/types": "^0.1.6",
"@remix-run/dev": "1.19.3",
"@remix-run/eslint-config": "1.19.3",
"@remix-run/serve": "1.19.3",
"@types/node": "^20.6.0",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"@typescript/lib-dom": "npm:@types/web@^0.0.114",
"autoprefixer": "^10.4.15",
"aws-cdk-lib": "2.91.0",
"constructs": "10.2.69",
"bun-types": "latest",
"del": "^7.1.0",
"eslint": "^8.49.0",
"eslint-plugin-prefer-let": "^3.0.1",
Expand Down
6 changes: 3 additions & 3 deletions remix.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require("node:path");
import path from "node:path";

/** @type {import('@remix-run/dev').AppConfig}*/
module.exports = {
export default {
ignoredRouteFiles: ["**/.*"],
appDirectory: "app",
assetsBuildDirectory: "public/build",
Expand All @@ -10,7 +10,7 @@ module.exports = {
tailwind: true,
postcss: true,
serverBuildPath: "build/index.js",
serverModuleFormat: "cjs",
serverModuleFormat: "esm",
future: {
v2_dev: true,
v2_errorBoundary: true,
Expand Down
2 changes: 1 addition & 1 deletion remix.env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/// <reference types="@remix-run/dev" />
/// <reference types="@remix-run/node" />
/// <reference types="@remix-run/server-runtime" />
2 changes: 1 addition & 1 deletion scripts/clean.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function clean() {

let files = globSync("**/*", {
absolute: true,
ignore: ["node_modules/**/*", ".vercel/**/*"],
ignore: ["node_modules/**/*"],
nodir: true,
cwd,
});
Expand Down
23 changes: 23 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { ServerBuild } from "@remix-run/server-runtime";
import { createRequestHandler, logDevReady } from "@remix-run/server-runtime";
import path from "node:path";
import { type Serve } from "bun";

import * as build from "./build/index.js";

let serverBuild = build as unknown as ServerBuild;

if (Bun.env.NODE_ENV === "development") logDevReady(serverBuild);

let __dirname = path.dirname(new URL(import.meta.url).pathname);

export default {
port: Bun.env.PORT || 3000,
async fetch(request) {
let url = new URL(request.url);
let publicFilePath = path.join(__dirname, "public", url.pathname);
let file = Bun.file(publicFilePath);
if (await file.exists()) return new Response(file);
return createRequestHandler(serverBuild, Bun.env.NODE_ENV)(request);
},
} satisfies Serve;
10 changes: 6 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": [],
"exclude": ["./build", "./public/build"],
"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"allowJs": true,
Expand All @@ -9,17 +9,19 @@
"incremental": true,
"isolatedModules": true,
"jsx": "react-jsx",
"lib": ["DOM", "DOM.Iterable", "ES2019"],
"lib": ["DOM", "DOM.Iterable", "ES2022"],
"moduleResolution": "bundler",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ES2019",
"target": "ES2022",
"module": "ES2022",
"baseUrl": ".",
"paths": {
"~/*": ["./app/*"]
}
},
"types": ["bun-types"]
}
}

0 comments on commit 16c06dc

Please sign in to comment.