UNPKG

1.46 kBJavaScriptView Raw
1/**
2 * react-router v8.3.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 { createSessionStorage } from "../sessions.js";
12//#region lib/server-runtime/sessions/memoryStorage.ts
13/**
14* Creates and returns a simple in-memory SessionStorage object.
15*
16* Intended for local development and testing. It does not scale beyond a single
17* process, and all session data is lost when the server process stops/restarts.
18*
19* @public
20* @category Utils
21* @mode framework
22* @mode data
23* @param options Options for creating the in-memory session storage.
24* @returns A {@link SessionStorage} object that stores session data in memory.
25*/
26function createMemorySessionStorage({ cookie } = {}) {
27 let map = /* @__PURE__ */ new Map();
28 return createSessionStorage({
29 cookie,
30 async createData(data, expires) {
31 let id = crypto.randomUUID();
32 map.set(id, {
33 data,
34 expires
35 });
36 return id;
37 },
38 async readData(id) {
39 if (map.has(id)) {
40 let { data, expires } = map.get(id);
41 if (!expires || expires > /* @__PURE__ */ new Date()) return data;
42 if (expires) map.delete(id);
43 }
44 return null;
45 },
46 async updateData(id, data, expires) {
47 map.set(id, {
48 data,
49 expires
50 });
51 },
52 async deleteData(id) {
53 map.delete(id);
54 }
55 });
56}
57//#endregion
58export { createMemorySessionStorage };