251 lines
8.0 KiB
JavaScript
251 lines
8.0 KiB
JavaScript
"use strict";
|
|
var __create = Object.create;
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
}, __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from == "object" || typeof from == "function")
|
|
for (let key of __getOwnPropNames(from))
|
|
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
return to;
|
|
};
|
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
// If the importer is in node compatibility mode or this is not an ESM
|
|
// file that has been converted to a CommonJS file using a Babel-
|
|
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
|
|
mod
|
|
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
var cookies_exports = {};
|
|
__export(cookies_exports, {
|
|
Cookie: () => Cookie,
|
|
createCookieJar: () => createCookieJar,
|
|
parseCookie: () => parseCookie,
|
|
serializeCookie: () => serializeCookie
|
|
});
|
|
module.exports = __toCommonJS(cookies_exports);
|
|
var import_cookie = require("cookie"), import_fast_decode_uri_component = __toESM(require("fast-decode-uri-component")), import_utils = require('./utils.js'), import_error = require('./error.js');
|
|
const hashString = (str) => {
|
|
let hash = 2166136261;
|
|
const len = str.length;
|
|
for (let i = 0; i < len; i++)
|
|
hash ^= str.charCodeAt(i), hash = Math.imul(hash, 16777619);
|
|
return hash >>> 0;
|
|
};
|
|
class Cookie {
|
|
constructor(name, jar, initial = /* @__PURE__ */ Object.create(null)) {
|
|
this.name = name;
|
|
this.jar = jar;
|
|
this.initial = initial;
|
|
}
|
|
get cookie() {
|
|
return this.jar[this.name] ?? this.initial;
|
|
}
|
|
set cookie(jar) {
|
|
this.name in this.jar || (this.jar[this.name] = this.initial), this.jar[this.name] = jar, this.valueHash = void 0;
|
|
}
|
|
get setCookie() {
|
|
return this.name in this.jar || (this.jar[this.name] = this.initial), this.jar[this.name];
|
|
}
|
|
set setCookie(jar) {
|
|
this.cookie = jar;
|
|
}
|
|
get value() {
|
|
return this.cookie.value;
|
|
}
|
|
set value(value) {
|
|
const current = this.cookie.value;
|
|
if (current !== value) {
|
|
if (typeof current == "object" && current !== null && typeof value == "object" && value !== null)
|
|
try {
|
|
const valueStr = JSON.stringify(value), newHash = hashString(valueStr);
|
|
if (this.valueHash !== void 0 && this.valueHash !== newHash)
|
|
this.valueHash = newHash;
|
|
else {
|
|
if (JSON.stringify(current) === valueStr) {
|
|
this.valueHash = newHash;
|
|
return;
|
|
}
|
|
this.valueHash = newHash;
|
|
}
|
|
} catch {
|
|
}
|
|
this.name in this.jar || (this.jar[this.name] = { ...this.initial }), this.jar[this.name].value = value;
|
|
}
|
|
}
|
|
get expires() {
|
|
return this.cookie.expires;
|
|
}
|
|
set expires(expires) {
|
|
this.setCookie.expires = expires;
|
|
}
|
|
get maxAge() {
|
|
return this.cookie.maxAge;
|
|
}
|
|
set maxAge(maxAge) {
|
|
this.setCookie.maxAge = maxAge;
|
|
}
|
|
get domain() {
|
|
return this.cookie.domain;
|
|
}
|
|
set domain(domain) {
|
|
this.setCookie.domain = domain;
|
|
}
|
|
get path() {
|
|
return this.cookie.path;
|
|
}
|
|
set path(path) {
|
|
this.setCookie.path = path;
|
|
}
|
|
get secure() {
|
|
return this.cookie.secure;
|
|
}
|
|
set secure(secure) {
|
|
this.setCookie.secure = secure;
|
|
}
|
|
get httpOnly() {
|
|
return this.cookie.httpOnly;
|
|
}
|
|
set httpOnly(httpOnly) {
|
|
this.setCookie.httpOnly = httpOnly;
|
|
}
|
|
get sameSite() {
|
|
return this.cookie.sameSite;
|
|
}
|
|
set sameSite(sameSite) {
|
|
this.setCookie.sameSite = sameSite;
|
|
}
|
|
get priority() {
|
|
return this.cookie.priority;
|
|
}
|
|
set priority(priority) {
|
|
this.setCookie.priority = priority;
|
|
}
|
|
get partitioned() {
|
|
return this.cookie.partitioned;
|
|
}
|
|
set partitioned(partitioned) {
|
|
this.setCookie.partitioned = partitioned;
|
|
}
|
|
get secrets() {
|
|
return this.cookie.secrets;
|
|
}
|
|
set secrets(secrets) {
|
|
this.setCookie.secrets = secrets;
|
|
}
|
|
update(config) {
|
|
return this.setCookie = Object.assign(
|
|
this.cookie,
|
|
typeof config == "function" ? config(this.cookie) : config
|
|
), this;
|
|
}
|
|
set(config) {
|
|
return this.setCookie = Object.assign(
|
|
{
|
|
...this.initial,
|
|
value: this.value
|
|
},
|
|
typeof config == "function" ? config(this.cookie) : config
|
|
), this;
|
|
}
|
|
remove() {
|
|
if (this.value !== void 0)
|
|
return this.set({
|
|
expires: /* @__PURE__ */ new Date(0),
|
|
maxAge: 0,
|
|
value: ""
|
|
}), this;
|
|
}
|
|
toString() {
|
|
return typeof this.value == "object" ? JSON.stringify(this.value) : this.value?.toString() ?? "";
|
|
}
|
|
}
|
|
const createCookieJar = (set, store, initial) => (set.cookie || (set.cookie = /* @__PURE__ */ Object.create(null)), new Proxy(store, {
|
|
get(_, key) {
|
|
return key in store ? new Cookie(
|
|
key,
|
|
set.cookie,
|
|
Object.assign({}, initial ?? {}, store[key])
|
|
) : new Cookie(
|
|
key,
|
|
set.cookie,
|
|
Object.assign({}, initial)
|
|
);
|
|
}
|
|
})), parseCookie = async (set, cookieString, {
|
|
secrets,
|
|
sign,
|
|
...initial
|
|
} = /* @__PURE__ */ Object.create(null)) => {
|
|
if (!cookieString) return createCookieJar(set, /* @__PURE__ */ Object.create(null), initial);
|
|
const isStringKey = typeof secrets == "string";
|
|
sign && sign !== !0 && !Array.isArray(sign) && (sign = [sign]);
|
|
const jar = /* @__PURE__ */ Object.create(null), cookies = (0, import_cookie.parse)(cookieString);
|
|
for (const [name, v] of Object.entries(cookies)) {
|
|
if (v === void 0 || name === "__proto__" || name === "constructor" || name === "prototype")
|
|
continue;
|
|
let value = (0, import_fast_decode_uri_component.default)(v);
|
|
if (sign === !0 || sign?.includes(name)) {
|
|
if (!secrets)
|
|
throw new Error("No secret is provided to cookie plugin");
|
|
if (isStringKey) {
|
|
if (typeof value != "string")
|
|
throw new import_error.InvalidCookieSignature(name);
|
|
const temp = await (0, import_utils.unsignCookie)(value, secrets);
|
|
if (temp === !1) throw new import_error.InvalidCookieSignature(name);
|
|
value = temp;
|
|
} else {
|
|
let decoded = !1;
|
|
for (let i = 0; i < secrets.length; i++) {
|
|
if (typeof value != "string")
|
|
throw new import_error.InvalidCookieSignature(name);
|
|
const temp = await (0, import_utils.unsignCookie)(value, secrets[i]);
|
|
if (temp !== !1) {
|
|
decoded = !0, value = temp;
|
|
break;
|
|
}
|
|
}
|
|
if (!decoded) throw new import_error.InvalidCookieSignature(name);
|
|
}
|
|
}
|
|
if (value) {
|
|
const starts = value.charCodeAt(0), ends = value.charCodeAt(value.length - 1);
|
|
if (starts === 123 && ends === 125 || starts === 91 && ends === 93)
|
|
try {
|
|
value = JSON.parse(value);
|
|
} catch {
|
|
}
|
|
}
|
|
jar[name] = /* @__PURE__ */ Object.create(null), jar[name].value = value;
|
|
}
|
|
return createCookieJar(set, jar, initial);
|
|
}, serializeCookie = (cookies) => {
|
|
if (!cookies || !(0, import_utils.isNotEmpty)(cookies)) return;
|
|
const set = [];
|
|
for (const [key, property] of Object.entries(cookies)) {
|
|
if (!key || !property) continue;
|
|
const value = property.value;
|
|
value != null && set.push(
|
|
(0, import_cookie.serialize)(
|
|
key,
|
|
typeof value == "object" ? JSON.stringify(value) : value + "",
|
|
property
|
|
)
|
|
);
|
|
}
|
|
if (set.length !== 0)
|
|
return set.length === 1 ? set[0] : set;
|
|
};
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
Cookie,
|
|
createCookieJar,
|
|
parseCookie,
|
|
serializeCookie
|
|
});
|