From fab8c58299294d12b8a9b34b2e466396f05d1c3c Mon Sep 17 00:00:00 2001 From: Pedro Cattori Date: Fri, 18 Mar 2022 21:08:26 -0500 Subject: [PATCH] refactor(remix-deno): implement crypto-dependent interface functions --- .../{cookieSigning.ts => crypto.ts} | 9 ++++----- .../deps/@remix-run/server-runtime.ts | 7 ++++++- templates/deno-ts/remix-deno/globals.ts | 20 ------------------- .../deno-ts/remix-deno/implementations.ts | 13 ++++++++++++ templates/deno-ts/remix-deno/index.ts | 6 ++++-- .../remix-deno/sessions/fileStorage.ts | 2 +- 6 files changed, 28 insertions(+), 29 deletions(-) rename templates/deno-ts/remix-deno/{cookieSigning.ts => crypto.ts} (84%) delete mode 100644 templates/deno-ts/remix-deno/globals.ts create mode 100644 templates/deno-ts/remix-deno/implementations.ts diff --git a/templates/deno-ts/remix-deno/cookieSigning.ts b/templates/deno-ts/remix-deno/crypto.ts similarity index 84% rename from templates/deno-ts/remix-deno/cookieSigning.ts rename to templates/deno-ts/remix-deno/crypto.ts index 89bacdf5203..0ffa07f0ee5 100644 --- a/templates/deno-ts/remix-deno/cookieSigning.ts +++ b/templates/deno-ts/remix-deno/crypto.ts @@ -1,6 +1,8 @@ +import type { SignFunction, UnsignFunction } from "./deps/@remix-run/server-runtime.ts" + const encoder = new TextEncoder(); -export async function sign(value: string, secret: string): Promise { +export const sign: SignFunction = async (value, secret) => { const key = await crypto.subtle.importKey( "raw", encoder.encode(secret), @@ -19,10 +21,7 @@ export async function sign(value: string, secret: string): Promise { return value + "." + hash; } -export async function unsign( - cookie: string, - secret: string -): Promise { +export const unsign: UnsignFunction = async (cookie, secret) => { const key = await crypto.subtle.importKey( "raw", encoder.encode(secret), diff --git a/templates/deno-ts/remix-deno/deps/@remix-run/server-runtime.ts b/templates/deno-ts/remix-deno/deps/@remix-run/server-runtime.ts index 7ab327f9621..76abdb5fa29 100644 --- a/templates/deno-ts/remix-deno/deps/@remix-run/server-runtime.ts +++ b/templates/deno-ts/remix-deno/deps/@remix-run/server-runtime.ts @@ -2,8 +2,13 @@ export type { ServerBuild, SessionIdStorageStrategy, SessionStorage, + Sign, + Unsign, } from "https://esm.sh/@remix-run/server-runtime?pin=v68"; export { + createCookieFactory, + createCookieSessionStorageFactory, + createMemorySessionStorageFactory, + createSessionStorageFactory, createRequestHandler, - createSessionStorage, } from "https://esm.sh/@remix-run/server-runtime?pin=v68"; \ No newline at end of file diff --git a/templates/deno-ts/remix-deno/globals.ts b/templates/deno-ts/remix-deno/globals.ts deleted file mode 100644 index fcfe5de8a12..00000000000 --- a/templates/deno-ts/remix-deno/globals.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { sign, unsign } from "./cookieSigning.ts"; - -declare global { - interface ProcessEnv { - NODE_ENV: "development" | "production" | "test"; - } - interface Process { - env: ProcessEnv; - } - - // This is here because we need it in our node setup, - // but not in an actual deno project. - // deno-lint-ignore no-var - var process: Process; -} - -export function installGlobals() { - window.sign = sign; - window.unsign = unsign; -} diff --git a/templates/deno-ts/remix-deno/implementations.ts b/templates/deno-ts/remix-deno/implementations.ts new file mode 100644 index 00000000000..6f289c193c7 --- /dev/null +++ b/templates/deno-ts/remix-deno/implementations.ts @@ -0,0 +1,13 @@ +import { + createCookieFactory, + createCookieSessionStorageFactory, + createMemorySessionStorageFactory, + createSessionStorageFactory, +} from "./deps/@remix-run/server-runtime.ts"; + +import { sign, unsign } from "./crypto.ts"; + +export const createCookie = createCookieFactory({ sign, unsign }); +export const createCookieSessionStorage = createCookieSessionStorageFactory(createCookie); +export const createSessionStorage = createSessionStorageFactory(createCookie); +export const createMemorySessionStorage = createMemorySessionStorageFactory(createSessionStorage); diff --git a/templates/deno-ts/remix-deno/index.ts b/templates/deno-ts/remix-deno/index.ts index 688500db2c7..41de32c86b8 100644 --- a/templates/deno-ts/remix-deno/index.ts +++ b/templates/deno-ts/remix-deno/index.ts @@ -1,4 +1,3 @@ -export { installGlobals } from './globals.ts' export { createFileSessionStorage } from './sessions/fileStorage.ts' export { createRequestHandler, createRequestHandlerWithStaticFiles, serveStaticFiles } from './server.ts' @@ -6,8 +5,11 @@ export { createCookie, createCookieSessionStorage, createMemorySessionStorage, - createSession, createSessionStorage, +} from './implementations.ts'; + +export { + createSession, isCookie, isSession, json, diff --git a/templates/deno-ts/remix-deno/sessions/fileStorage.ts b/templates/deno-ts/remix-deno/sessions/fileStorage.ts index 47d6608d732..2fa51686f43 100644 --- a/templates/deno-ts/remix-deno/sessions/fileStorage.ts +++ b/templates/deno-ts/remix-deno/sessions/fileStorage.ts @@ -3,7 +3,7 @@ import type { SessionStorage, SessionIdStorageStrategy, } from "../deps/@remix-run/server-runtime.ts"; -import { createSessionStorage } from "../deps/@remix-run/server-runtime.ts"; +import { createSessionStorage } from "../implementations.ts"; interface FileSessionStorageOptions { /**