Skip to content

Commit

Permalink
feat(remix-server-runtime): use an internal hash for the id in `creat…
Browse files Browse the repository at this point in the history
…eMemorySessionStorage` (#7227)
  • Loading branch information
brophdawg11 authored Aug 22, 2023
1 parent bd289fa commit 68055c4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/memory-session-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/server-runtime": patch
---

Update `createMemorySessionStorage` to use an internal hash value instead of an integer for the session `id`
11 changes: 11 additions & 0 deletions packages/remix-server-runtime/__tests__/sessions-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ describe("In-memory session storage", () => {

expect(session.get("user")).toEqual("mjackson");
});

it("uses random hash keys as session ids", async () => {
let { getSession, commitSession } = createMemorySessionStorage({
cookie: { secrets: ["secret1"] },
});
let session = await getSession();
session.set("user", "mjackson");
let setCookie = await commitSession(session);
session = await getSession(getCookieFromSetCookie(setCookie));
expect(session.id).toMatch(/^[a-z0-9]{8}$/);
});
});

describe("Cookie session storage", () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/remix-server-runtime/sessions/memoryStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const createMemorySessionStorageFactory =
<Data = SessionData, FlashData = Data>({
cookie,
}: MemorySessionStorageOptions = {}): SessionStorage<Data, FlashData> => {
let uniqueId = 0;
let map = new Map<
string,
{ data: FlashSessionData<Data, FlashData>; expires?: Date }
Expand All @@ -46,7 +45,7 @@ export const createMemorySessionStorageFactory =
return createSessionStorage({
cookie,
async createData(data, expires) {
let id = (++uniqueId).toString();
let id = Math.random().toString(36).substring(2, 10);
map.set(id, { data, expires });
return id;
},
Expand Down

0 comments on commit 68055c4

Please sign in to comment.