@sapphire/plugin-api
Version:
Plugin for @sapphire/framework to expose a REST API
101 lines (98 loc) • 3.72 kB
JavaScript
;
var tldts = require('tldts');
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var _CookieStore = class _CookieStore extends Map {
constructor(request, response, secure, domainOverwrite) {
super();
__publicField(this, "request");
__publicField(this, "response");
__publicField(this, "domain");
__publicField(this, "secure");
this.request = request;
this.response = response;
const { cookie = "" } = request.headers;
const pairs = cookie.split(";");
for (const pair of pairs) {
const index = pair.indexOf("=");
if (index === -1) continue;
const key = decodeURIComponent(pair.slice(0, index).trim());
const value = decodeURIComponent(pair.slice(index + 1).trim());
this.set(key, value);
}
const [splitHost] = this.request.headers.host?.split(":") ?? [""];
this.domain = domainOverwrite ?? this.getHostDomain(splitHost);
if (this.request.socket.remoteAddress === this.domain) {
throw new Error("The connection must be established from the domain name (i.e., not an IP address)");
}
this.secure = secure;
}
add(name, value, options) {
this.insert(name, this.prepare(name, value, options));
}
remove(name) {
this.add(name, "", { expires: /* @__PURE__ */ new Date(0) });
}
insert(name, entry) {
let set = this.response.getHeader("Set-Cookie");
if (set === void 0) {
set = [];
} else if (!Array.isArray(set)) {
set = [set.toString()];
}
set = set.filter((i) => i.slice(0, i.indexOf("=")) !== name);
set.push(entry);
this.response.setHeader("Set-Cookie", set);
}
prepare(name, value, { expires, maxAge, domain, path, httpOnly } = {}) {
const now = /* @__PURE__ */ new Date();
if (expires === void 0) {
expires = now;
}
name = _CookieStore.encodeCookieOctet(name);
value = _CookieStore.encodeCookieOctet(value);
let entry = `${name}=${value}`;
if (expires !== now) {
entry += `; Expires=${expires.toUTCString()}`;
} else if (maxAge) {
entry += `; Max-Age=${maxAge}`;
}
domain = (domain ?? this.domain).toLowerCase();
entry += `; Domain=${domain}`;
entry += `; Path=${path ?? "/"}`;
if (this.secure) {
entry += `; Secure`;
}
if (httpOnly ?? true) {
entry += `; HttpOnly`;
}
return entry;
}
/**
* Parses a host using the {@linkplain https://github.com/remusao/tldts tldts} library to extract the domain.
* This is used for the domain of the cookie
* @param host The hot to parse
* @returns Either the host in all lower case or the parsed domain, ready for use on cookies
*/
getHostDomain(host) {
const lowercaseHost = host.toLowerCase();
const tldParsedInfo = tldts.getDomain(lowercaseHost);
if (!tldParsedInfo) return lowercaseHost;
return `.${tldParsedInfo}`;
}
static encodeCookieOctet(value) {
if (_CookieStore.octetRegExp.test(value)) {
throw new Error(`Invalid character in value`);
}
return encodeURIComponent(value);
}
};
__name(_CookieStore, "CookieStore");
// RFC 6265 4.1.1. Syntax
__publicField(_CookieStore, "octetRegExp", /[^\x21\x23-\x2B\x2D-\x3A\x3C-\x5B\x5D-\x7E]/g);
var CookieStore = _CookieStore;
exports.CookieStore = CookieStore;
//# sourceMappingURL=CookieStore.cjs.map
//# sourceMappingURL=CookieStore.cjs.map