next-client-cookies
Version:
SSR and client support for Next.js v13 cookies (app directory)
70 lines • 2.37 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var secure_exports = {};
__export(secure_exports, {
storeSecureCookies: () => storeSecureCookies,
useSecureCookies: () => useSecureCookies
});
module.exports = __toCommonJS(secure_exports);
const storage = typeof global === "object" ? global.__COOKIES_STORAGE__ = global.__COOKIES_STORAGE__ || /* @__PURE__ */ new Map() : null;
const storeSecureCookies = /* @__PURE__ */ __name((secureValue) => {
let value;
do {
value = Math.random();
} while (storage?.has(value));
if (storage) {
storage.set(value, {
value: secureValue,
time: Date.now()
});
setCleanupTimeout();
}
return value;
}, "storeSecureCookies");
const useSecureCookies = /* @__PURE__ */ __name((value) => storage?.get(value)?.value, "useSecureCookies");
let timeout = null;
const CLEANUP_TTL_MS = 5e3;
const setCleanupTimeout = /* @__PURE__ */ __name(() => {
if (timeout) {
return;
}
timeout = setTimeout(cleanup, CLEANUP_TTL_MS * 2);
}, "setCleanupTimeout");
const cleanup = /* @__PURE__ */ __name(() => {
clearTimeout(timeout);
timeout = null;
if (!storage)
return;
const now = Date.now();
for (const [key, { time }] of storage.entries()) {
if (now - time > CLEANUP_TTL_MS) {
storage.delete(key);
}
}
if (storage.size) {
setCleanupTimeout();
}
}, "cleanup");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
storeSecureCookies,
useSecureCookies
});
//# sourceMappingURL=secure.js.map