hono-sess
Version:
A Simple Session Middleware for Hono
82 lines • 2.42 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cookie = void 0;
const cookie_1 = require("cookie");
class Cookie {
path = '/';
_expires = null;
originalMaxAge = null;
httpOnly;
partitioned;
secure;
domain;
sameSite;
priority;
constructor(options) {
if (options) {
if (typeof options !== 'object') {
throw new TypeError('argument options must be a object');
}
for (const key in options) {
if (key !== 'data') {
const k = key;
this[k] = options[k];
}
}
}
if (this.originalMaxAge === undefined || this.originalMaxAge === null) {
this.originalMaxAge = this.maxAge ?? null;
}
}
get expires() {
return this._expires || undefined;
}
set expires(date) {
this._expires = date;
this.originalMaxAge = this.maxAge ?? null;
}
set maxAge(ms) {
if (ms && typeof ms !== 'number' && !(ms instanceof Date)) {
throw new TypeError('maxAge must be a number or Date');
}
if (ms instanceof Date) {
console.warn('maxAge as Date; pass number of milliseconds instead');
}
if (typeof ms === 'number') {
this._expires = new Date(Date.now() + ms);
this.originalMaxAge = ms;
}
else {
this._expires = null;
this.originalMaxAge = null;
}
}
get maxAge() {
if (!this._expires || !this.originalMaxAge) {
return undefined;
}
const remaining = this._expires.getTime() - Date.now();
return Math.max(0, remaining);
}
get data() {
return {
originalMaxAge: this.originalMaxAge,
partitioned: this.partitioned,
expires: this._expires ?? undefined,
secure: this.secure === 'auto' ? undefined : this.secure,
httpOnly: this.httpOnly,
domain: this.domain,
path: this.path,
sameSite: this.sameSite,
priority: this.priority,
};
}
serialize(name, val) {
return cookie_1.default.serialize(name, val, this.data);
}
toJSON() {
return this.data;
}
}
exports.Cookie = Cookie;
//# sourceMappingURL=cookie.js.map