Skip to content

Commit

Permalink
refactor(@remix-run/cloudflare): implement crypto-dependent interface…
Browse files Browse the repository at this point in the history
… functions
  • Loading branch information
pcattori committed Mar 19, 2022
1 parent afcb21e commit 27bf12e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
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;
}
}
9 changes: 9 additions & 0 deletions packages/remix-cloudflare/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare global {
interface ProcessEnv {
NODE_ENV: "development" | "production" | "test";
}

interface WorkerGlobalScope {
process: { env: ProcessEnv };
}
}
20 changes: 0 additions & 20 deletions packages/remix-cloudflare/globals.ts

This file was deleted.

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);
5 changes: 4 additions & 1 deletion packages/remix-cloudflare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ export {
createCookie,
createCookieSessionStorage,
createMemorySessionStorage,
createSessionStorage,
} from './implementations';

export {
createRequestHandler,
createSession,
createSessionStorage,
isCookie,
isSession,
json,
Expand Down
3 changes: 2 additions & 1 deletion packages/remix-cloudflare/sessions/KVSessionStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type {
SessionStorage,
SessionIdStorageStrategy,
} from "@remix-run/server-runtime";
import { createSessionStorage } from "@remix-run/server-runtime";

import { createSessionStorage } from "../implementations";

interface KVSessionStorageOptions {
/**
Expand Down

0 comments on commit 27bf12e

Please sign in to comment.