UNPKG

1.68 kBJavaScriptView Raw
1/**
2 * react-router v8.0.0
3 *
4 * Copyright (c) Remix Software Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE.md file in the root directory of this source tree.
8 *
9 * @license MIT
10 */
11import { createCookie, isCookie } from "../cookies.js";
12import { createSession, warnOnceAboutSigningSessionCookie } from "../sessions.js";
13//#region lib/server-runtime/sessions/cookieStorage.ts
14/**
15* Creates and returns a SessionStorage object that stores all session data
16* directly in the session cookie itself.
17*
18* This has the advantage that no database or other backend services are
19* needed, and can help to simplify some load-balanced scenarios. However, it
20* also has the limitation that serialized session data may not exceed the
21* browser's maximum cookie size. Trade-offs!
22*/
23function createCookieSessionStorage({ cookie: cookieArg } = {}) {
24 let cookie = isCookie(cookieArg) ? cookieArg : createCookie(cookieArg?.name || "__session", cookieArg);
25 warnOnceAboutSigningSessionCookie(cookie);
26 return {
27 async getSession(cookieHeader, options) {
28 return createSession(cookieHeader && await cookie.parse(cookieHeader, options) || {});
29 },
30 async commitSession(session, options) {
31 let serializedCookie = await cookie.serialize(session.data, options);
32 if (serializedCookie.length > 4096) throw new Error("Cookie length will exceed browser maximum. Length: " + serializedCookie.length);
33 return serializedCookie;
34 },
35 async destroySession(_session, options) {
36 return cookie.serialize("", {
37 ...options,
38 maxAge: void 0,
39 expires: /* @__PURE__ */ new Date(0)
40 });
41 }
42 };
43}
44//#endregion
45export { createCookieSessionStorage };