UNPKG

1.29 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 { createSessionStorage } from "../sessions.js";
12//#region lib/server-runtime/sessions/memoryStorage.ts
13/**
14* Creates and returns a simple in-memory SessionStorage object, mostly useful
15* for testing and as a reference implementation.
16*
17* Note: This storage does not scale beyond a single process, so it is not
18* suitable for most production scenarios.
19*/
20function createMemorySessionStorage({ cookie } = {}) {
21 let map = /* @__PURE__ */ new Map();
22 return createSessionStorage({
23 cookie,
24 async createData(data, expires) {
25 let id = Math.random().toString(36).substring(2, 10);
26 map.set(id, {
27 data,
28 expires
29 });
30 return id;
31 },
32 async readData(id) {
33 if (map.has(id)) {
34 let { data, expires } = map.get(id);
35 if (!expires || expires > /* @__PURE__ */ new Date()) return data;
36 if (expires) map.delete(id);
37 }
38 return null;
39 },
40 async updateData(id, data, expires) {
41 map.set(id, {
42 data,
43 expires
44 });
45 },
46 async deleteData(id) {
47 map.delete(id);
48 }
49 });
50}
51//#endregion
52export { createMemorySessionStorage };