Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(getSession): use semaphore lock for unseal operation #612

Merged
merged 7 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import { getCookie, setCookie } from "./cookie";
type SessionDataT = Record<string, any>;
export type SessionData<T extends SessionDataT = SessionDataT> = T;

const getSessionPromise = Symbol("getSession");

export interface Session<T extends SessionDataT = SessionDataT> {
id: string;
createdAt: number;
data: SessionData<T>;
[getSessionPromise]?: Promise<Session<T>>;
}

export interface SessionConfig {
Expand Down Expand Up @@ -74,8 +77,10 @@ export async function getSession<T extends SessionDataT = SessionDataT>(
if (!event.context.sessions) {
event.context.sessions = Object.create(null);
}
if (event.context.sessions![sessionName]) {
return event.context.sessions![sessionName] as Session<T>;
// Wait for existing session to load
const existingSession = event.context.sessions![sessionName] as Session<T>;
if (existingSession) {
return existingSession[getSessionPromise] || existingSession;
}

// Prepare an empty session object and store in context
Expand Down Expand Up @@ -105,10 +110,15 @@ export async function getSession<T extends SessionDataT = SessionDataT>(
}
if (sealedSession) {
// Unseal session data from cookie
const unsealed = await unsealSession(event, config, sealedSession).catch(
() => {},
);
Object.assign(session, unsealed);
const promise = unsealSession(event, config, sealedSession)
.catch(() => {})
.then((unsealed) => {
Object.assign(session, unsealed);
delete event.context.sessions![sessionName][getSessionPromise];
return session as Session<T>;
});
event.context.sessions![sessionName][getSessionPromise] = promise;
await promise;
}

// New session store in response cookies
Expand Down
23 changes: 23 additions & 0 deletions test/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,27 @@ describe("session", () => {
session: { id: "1", data: { foo: "bar" } },
});
});

it("gets same session back (concurrent)", async () => {
router.use(
"/concurrent",
eventHandler(async (event) => {
const sessions = await Promise.all(
[1, 2, 3].map(() =>
useSession(event, sessionConfig).then((s) => ({
id: s.id,
data: s.data,
})),
),
);
return {
sessions,
};
}),
);
const result = await request.get("/concurrent").set("Cookie", cookie);
expect(result.body).toMatchObject({
sessions: [1, 2, 3].map(() => ({ id: "1", data: { foo: "bar" } })),
});
});
});
Loading