UNPKG

3.33 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 { 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*/
23const createSession = (initialData = {}, id = "") => {
24 let map = new Map(Object.entries(initialData));
25 return {
26 get id() {
27 return id;
28 },
29 get data() {
30 return Object.fromEntries(map);
31 },
32 has(name) {
33 return map.has(name) || map.has(flash(name));
34 },
35 get(name) {
36 if (map.has(name)) return map.get(name);
37 let flashName = flash(name);
38 if (map.has(flashName)) {
39 let value = map.get(flashName);
40 map.delete(flashName);
41 return value;
42 }
43 },
44 set(name, value) {
45 map.set(name, value);
46 },
47 flash(name, value) {
48 map.set(flash(name), value);
49 },
50 unset(name) {
51 map.delete(name);
52 }
53 };
54};
55/**
56* Returns true if an object is a React Router session.
57*
58* @see https://reactrouter.com/api/utils/isSession
59*/
60const isSession = (object) => {
61 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";
62};
63/**
64* Creates a SessionStorage object using a SessionIdStorageStrategy.
65*
66* Note: This is a low-level API that should only be used if none of the
67* existing session storage options meet your requirements.
68*/
69function createSessionStorage({ cookie: cookieArg, createData, readData, updateData, deleteData }) {
70 let cookie = isCookie(cookieArg) ? cookieArg : createCookie(cookieArg?.name || "__session", cookieArg);
71 warnOnceAboutSigningSessionCookie(cookie);
72 return {
73 async getSession(cookieHeader, options) {
74 let id = cookieHeader && await cookie.parse(cookieHeader, options);
75 return createSession(id && await readData(id) || {}, id || "");
76 },
77 async commitSession(session, options) {
78 let { id, data } = session;
79 let expires = options?.maxAge != null ? new Date(Date.now() + options.maxAge * 1e3) : options?.expires != null ? options.expires : cookie.expires;
80 if (id) await updateData(id, data, expires);
81 else id = await createData(data, expires);
82 return cookie.serialize(id, options);
83 },
84 async destroySession(session, options) {
85 await deleteData(session.id);
86 return cookie.serialize("", {
87 ...options,
88 maxAge: void 0,
89 expires: /* @__PURE__ */ new Date(0)
90 });
91 }
92 };
93}
94function warnOnceAboutSigningSessionCookie(cookie) {
95 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.`);
96}
97//#endregion
98export { createSession, createSessionStorage, isSession, warnOnceAboutSigningSessionCookie };