UNPKG

4.01 kBJavaScriptView Raw
1/**
2 * react-router v8.5.3
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 { warnOnce } from "./warnings.js";
12import { createCookie, isCookie } from "./cookies.js";
13//#region lib/server-runtime/sessions.ts
14function flash(name) {
15 return `__flash_${name}__`;
16}
17/**
18* Creates a new Session object.
19*
20* Note: This function is typically not invoked directly by application code.
21* Instead, use a `SessionStorage` object's `getSession` method.
22*
23* @category Utils
24* @param initialData The initial data for the session.
25* @param id The identifier for the session. Defaults to an empty string for a
26* new session.
27* @returns A new {@link Session} object.
28*/
29const createSession = (initialData = {}, id = "") => {
30 let map = new Map(Object.entries(initialData));
31 return {
32 get id() {
33 return id;
34 },
35 get data() {
36 return Object.fromEntries(map);
37 },
38 has(name) {
39 return map.has(name) || map.has(flash(name));
40 },
41 get(name) {
42 if (map.has(name)) return map.get(name);
43 let flashName = flash(name);
44 if (map.has(flashName)) {
45 let value = map.get(flashName);
46 map.delete(flashName);
47 return value;
48 }
49 },
50 set(name, value) {
51 map.set(name, value);
52 },
53 flash(name, value) {
54 map.set(flash(name), value);
55 },
56 unset(name) {
57 map.delete(name);
58 map.delete(flash(name));
59 }
60 };
61};
62/**
63* Returns `true` if a value is a React Router {@link Session} object.
64*
65* @public
66* @category Utils
67* @mode framework
68* @mode data
69* @param object The value to check.
70* @returns `true` if the value is a React Router {@link Session} object;
71* otherwise, `false`.
72*/
73const isSession = (object) => {
74 return object != null && typeof object.id === "string" && typeof object.data !== "undefined" && typeof object.has === "function" && typeof object.get === "function" && typeof object.set === "function" && typeof object.flash === "function" && typeof object.unset === "function";
75};
76/**
77* Creates a SessionStorage object using a SessionIdStorageStrategy.
78*
79* Note: This is a low-level API that should only be used if none of the
80* existing session storage options meet your requirements.
81*
82* @category Utils
83* @param strategy The strategy used to store session identifiers and data.
84* @returns A {@link SessionStorage} object that persists session data using the
85* provided strategy.
86*/
87function createSessionStorage({ cookie: cookieArg, createData, readData, updateData, deleteData }) {
88 let cookie = isCookie(cookieArg) ? cookieArg : createCookie(cookieArg?.name || "__session", cookieArg);
89 warnOnceAboutSigningSessionCookie(cookie);
90 return {
91 async getSession(cookieHeader, options) {
92 let parsedId = cookieHeader && await cookie.parse(cookieHeader, options);
93 let id = typeof parsedId === "string" && parsedId ? parsedId : "";
94 return createSession((id ? await readData(id) : null) || {}, id);
95 },
96 async commitSession(session, options) {
97 let { id, data } = session;
98 let expires = options?.maxAge != null ? new Date(Date.now() + options.maxAge * 1e3) : options?.expires != null ? options.expires : cookie.expires;
99 if (id) await updateData(id, data, expires);
100 else id = await createData(data, expires);
101 return cookie.serialize(id, options);
102 },
103 async destroySession(session, options) {
104 if (session.id) await deleteData(session.id);
105 return cookie.serialize("", {
106 ...options,
107 maxAge: void 0,
108 expires: /* @__PURE__ */ new Date(0)
109 });
110 }
111 };
112}
113function warnOnceAboutSigningSessionCookie(cookie) {
114 warnOnce(cookie.isSigned, `The "${cookie.name}" cookie is not signed, but session cookies should be signed to prevent tampering on the client before they are sent back to the server. See https://reactrouter.com/explanation/sessions-and-cookies#signing-cookies for more information.`);
115}
116//#endregion
117export { createSession, createSessionStorage, isSession, warnOnceAboutSigningSessionCookie };