Skip to content

Commit

Permalink
Refactor runtime packages to conform to the Remix server runtime inte…
Browse files Browse the repository at this point in the history
…rface (#2359)

* feat(@remix-run/server-runtime): define interface for remix server runtimes

* refactor(@remix-run/server-runtime): organize exports

group types that should be re-exported by each remix server runtime

* refactor(@remix-run/server-runtime): factories for crypto-dependent interface functions

* docs(@remix-run/server-runtime): rewrite readme to explain the "remix server runtime interface"

* refactor(@remix-run/node): conform to the remix server runtime interface

instead of relying on magic exports

* refactor(@remix-run/node): implement crypto-dependent interface functions

* feat(@remix-run/cloudflare): create new server runtime package for cloudflare

* refactor(@remix-run/cloudflare): implement crypto-dependent interface functions

* refactor(remix-deno): conform to the remix server runtime interface

* refactor(remix-deno): implement crypto-dependent interface functions

* refactor(@remix-run/architect): use `@remix-run/node` instead of `@remix-run/server-runtime`

* refactor(@remix-run/express): use `@remix-run/node` instead of `@remix-run/server-runtime`

* refactor(@remix-run/netlify): use `@remix-run/node` instead of `@remix-run/server-runtime`

* refactor(@remix-run/vercel): use `@remix-run/node` instead of `@remix-run/server-runtime`

* refactor(@remix-run/cloudflare-workers): use `@remix-run/cloudflare` instead of `@remix-run/server-runtime`

* refactor(@remix-run/cloudflare-pages): use `@remix-run/cloudflare` instead of `@remix-run/server-runtime`

* refactor(server-runtimes,adapters): remove unused `platform` arg from `createRequestHandler`
  • Loading branch information
pcattori authored and mcansh committed Mar 21, 2022
1 parent 9489b17 commit 7736444
Show file tree
Hide file tree
Showing 66 changed files with 657 additions and 496 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"packages/create-remix",
"packages/remix",
"packages/remix-architect",
"packages/remix-cloudflare",
"packages/remix-cloudflare-pages",
"packages/remix-cloudflare-workers",
"packages/remix-dev",
Expand Down
10 changes: 8 additions & 2 deletions packages/remix-architect/__tests__/server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import fsp from "fs/promises";
import path from "path";
import lambdaTester from "lambda-tester";
import type { APIGatewayProxyEventV2 } from "aws-lambda";
import { createRequestHandler as createRemixRequestHandler } from "@remix-run/server-runtime";
import {
// This has been added as a global in node 15+
AbortController,
createRequestHandler as createRemixRequestHandler,
Response as NodeResponse,
} from "@remix-run/node";

Expand All @@ -18,7 +18,13 @@ import {

// We don't want to test that the remix server works here (that's what the
// puppetteer tests do), we just want to test the architect adapter
jest.mock("@remix-run/server-runtime");
jest.mock("@remix-run/node", () => {
let original = jest.requireActual("@remix-run/node");
return {
...original,
createRequestHandler: jest.fn()
};
});
let mockedCreateRequestHandler =
createRemixRequestHandler as jest.MockedFunction<
typeof createRemixRequestHandler
Expand Down
3 changes: 1 addition & 2 deletions packages/remix-architect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
},
"dependencies": {
"@architect/functions": "^5.0.2",
"@types/aws-lambda": "^8.10.82",
"@remix-run/node": "1.3.2",
"@remix-run/server-runtime": "1.3.2"
"@types/aws-lambda": "^8.10.82"
},
"devDependencies": {
"@types/architect__functions": "^3.13.6",
Expand Down
10 changes: 4 additions & 6 deletions packages/remix-architect/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AbortController,
Headers as NodeHeaders,
Request as NodeRequest,
createRequestHandler as createRemixRequestHandler,
} from "@remix-run/node";
import type {
APIGatewayProxyEventHeaders,
Expand All @@ -13,10 +14,8 @@ import type {
import type {
AppLoadContext,
ServerBuild,
ServerPlatform,
} from "@remix-run/server-runtime";
import { createRequestHandler as createRemixRequestHandler } from "@remix-run/server-runtime";
import type { Response as NodeResponse } from "@remix-run/node";
Response as NodeResponse,
} from "@remix-run/node";

import { isBinaryType } from "./binary-types";

Expand Down Expand Up @@ -46,8 +45,7 @@ export function createRequestHandler({
getLoadContext?: GetLoadContextFunction;
mode?: string;
}): APIGatewayProxyHandlerV2 {
let platform: ServerPlatform = {};
let handleRequest = createRemixRequestHandler(build, platform, mode);
let handleRequest = createRemixRequestHandler(build, mode);

return async (event, _context) => {
let abortController = new AbortController();
Expand Down
7 changes: 2 additions & 5 deletions packages/remix-architect/sessions/arcTableSessionStorage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as crypto from "crypto";
import type {
SessionStorage,
SessionIdStorageStrategy,
} from "@remix-run/server-runtime";
import { createSessionStorage } from "@remix-run/server-runtime";
import type { SessionStorage, SessionIdStorageStrategy } from "@remix-run/node";
import { createSessionStorage } from "@remix-run/node";
import arc from "@architect/functions";
import type { ArcTable } from "@architect/functions/tables";

Expand Down
13 changes: 0 additions & 13 deletions packages/remix-cloudflare-pages/globals.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/remix-cloudflare-pages/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
import { installGlobals } from "./globals";

export { createCloudflareKVSessionStorage } from "./sessions/cloudflareKVSessionStorage";

export type { createPagesFunctionHandlerParams } from "./worker";
export { createPagesFunctionHandler, createRequestHandler } from "./worker";

installGlobals();
2 changes: 1 addition & 1 deletion packages/remix-cloudflare-pages/magicExports/remix.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Re-export everything from this package that is available in `remix`.

export { createCloudflareKVSessionStorage } from "@remix-run/cloudflare-pages";
export { createCloudflareKVSessionStorage } from "@remix-run/cloudflare";
2 changes: 1 addition & 1 deletion packages/remix-cloudflare-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"url": "https://github.com/remix-run/remix/issues"
},
"dependencies": {
"@remix-run/server-runtime": "1.3.2"
"@remix-run/cloudflare": "1.3.2"
},
"peerDependencies": {
"@cloudflare/workers-types": "^3.2.0"
Expand Down
1 change: 0 additions & 1 deletion packages/remix-cloudflare-pages/responses.ts

This file was deleted.

This file was deleted.

3 changes: 1 addition & 2 deletions packages/remix-cloudflare-pages/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export function createRequestHandler<Env = any>({
getLoadContext,
mode,
}: createPagesFunctionHandlerParams<Env>): PagesFunction<Env> {
let platform = {};
let handleRequest = createRemixRequestHandler(build, platform, mode);
let handleRequest = createRemixRequestHandler(build, mode);

return (context) => {
let loadContext =
Expand Down
52 changes: 0 additions & 52 deletions packages/remix-cloudflare-workers/cookieSigning.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/remix-cloudflare-workers/globals.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/remix-cloudflare-workers/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { installGlobals } from "./globals";

export { createCloudflareKVSessionStorage } from "./sessions/cloudflareKVSessionStorage";

export type { GetLoadContextFunction, RequestHandler } from "./worker";
export {
createEventHandler,
createRequestHandler,
handleAsset,
} from "./worker";

installGlobals();
2 changes: 1 addition & 1 deletion packages/remix-cloudflare-workers/magicExports/remix.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Re-export everything from this package that is available in `remix`.

export { createCloudflareKVSessionStorage } from "@remix-run/cloudflare-workers";
export { createCloudflareKVSessionStorage } from "@remix-run/cloudflare";
2 changes: 1 addition & 1 deletion packages/remix-cloudflare-workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"dependencies": {
"@cloudflare/kv-asset-handler": "^0.1.3",
"@remix-run/server-runtime": "1.3.2"
"@remix-run/cloudflare": "1.3.2"
},
"peerDependencies": {
"@cloudflare/workers-types": "^2.2.2"
Expand Down
1 change: 0 additions & 1 deletion packages/remix-cloudflare-workers/responses.ts

This file was deleted.

8 changes: 3 additions & 5 deletions packages/remix-cloudflare-workers/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
import type {
AppLoadContext,
ServerBuild,
ServerPlatform,
} from "@remix-run/server-runtime";
import { createRequestHandler as createRemixRequestHandler } from "@remix-run/server-runtime";
} from "@remix-run/cloudflare";
import { createRequestHandler as createRemixRequestHandler } from "@remix-run/cloudflare";

/**
* A function that returns the value to use as `context` in route `loader` and
Expand Down Expand Up @@ -37,8 +36,7 @@ export function createRequestHandler({
getLoadContext?: GetLoadContextFunction;
mode?: string;
}) {
let platform: ServerPlatform = {};
let handleRequest = createRemixRequestHandler(build, platform, mode);
let handleRequest = createRemixRequestHandler(build, mode);

return (event: FetchEvent) => {
let loadContext =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { SignFunction, UnsignFunction } from "@remix-run/server-runtime";

const encoder = new TextEncoder();

export async function sign(value: string, secret: string): Promise<string> {
export const sign: SignFunction = async (value, secret) => {
let key = await crypto.subtle.importKey(
"raw",
encoder.encode(secret),
Expand All @@ -19,10 +21,7 @@ export async function sign(value: string, secret: string): Promise<string> {
return value + "." + hash;
}

export async function unsign(
cookie: string,
secret: string
): Promise<string | false> {
export const unsign: UnsignFunction = async (cookie, secret) => {
let key = await crypto.subtle.importKey(
"raw",
encoder.encode(secret),
Expand All @@ -49,4 +48,4 @@ function byteStringToUint8Array(byteString: string): Uint8Array {
}

return array;
}
}
12 changes: 12 additions & 0 deletions packages/remix-cloudflare/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://stackoverflow.com/a/59499895
export {};

declare global {
interface ProcessEnv {
NODE_ENV: "development" | "production" | "test";
}

interface WorkerGlobalScope {
process: { env: ProcessEnv };
}
}
13 changes: 13 additions & 0 deletions packages/remix-cloudflare/implementations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
createCookieFactory,
createCookieSessionStorageFactory,
createMemorySessionStorageFactory,
createSessionStorageFactory,
} from "@remix-run/server-runtime";

import { sign, unsign } from "./crypto";

export const createCookie = createCookieFactory({ sign, unsign });
export const createCookieSessionStorage = createCookieSessionStorageFactory(createCookie);
export const createSessionStorage = createSessionStorageFactory(createCookie);
export const createMemorySessionStorage = createMemorySessionStorageFactory(createSessionStorage);
Loading

0 comments on commit 7736444

Please sign in to comment.