@remix-run/server-runtime
Version:
Server runtime for Remix
56 lines (51 loc) • 1.93 kB
JavaScript
/**
* @remix-run/server-runtime v2.16.8
*
* Copyright (c) Remix Software Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var cookies = require('../cookies.js');
var sessions = require('../sessions.js');
/**
* Creates and returns a SessionStorage object that stores all session data
* directly in the session cookie itself.
*
* This has the advantage that no database or other backend services are
* needed, and can help to simplify some load-balanced scenarios. However, it
* also has the limitation that serialized session data may not exceed the
* browser's maximum cookie size. Trade-offs!
*
* @see https://remix.run/utils/sessions#createcookiesessionstorage
*/
const createCookieSessionStorageFactory = createCookie => ({
cookie: cookieArg
} = {}) => {
let cookie = cookies.isCookie(cookieArg) ? cookieArg : createCookie((cookieArg === null || cookieArg === void 0 ? void 0 : cookieArg.name) || "__session", cookieArg);
sessions.warnOnceAboutSigningSessionCookie(cookie);
return {
async getSession(cookieHeader, options) {
return sessions.createSession(cookieHeader && (await cookie.parse(cookieHeader, options)) || {});
},
async commitSession(session, options) {
let serializedCookie = await cookie.serialize(session.data, options);
if (serializedCookie.length > 4096) {
throw new Error("Cookie length will exceed browser maximum. Length: " + serializedCookie.length);
}
return serializedCookie;
},
async destroySession(_session, options) {
return cookie.serialize("", {
...options,
maxAge: undefined,
expires: new Date(0)
});
}
};
};
exports.createCookieSessionStorageFactory = createCookieSessionStorageFactory;