This commit is contained in:
nucther committed 2025-12-01 23:23:05 +07:00
commit d282e2bcae
3481 files changed
+568488

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
export declare type CookieAttributes = BaseCookieAttributes & SameSiteCookieAttributes;
interface BaseCookieAttributes {
/**
* A number will be interpreted as days from time of creation
*/
expires?: Date | number;
/**
* Hosts to which the cookie will be sent
*/
domain?: string;
/**
* The cookie will only be included in an HTTP request if the request
* path matches (or is a subdirectory of) the cookie's path attribute.
*/
path?: string;
/**
* If enabled, the cookie will only be included in an HTTP request
* if it is transmitted over a secure channel (typically HTTP over TLS).
*/
secure?: boolean;
}
declare type SameSiteCookieAttributes = LaxStrictSameSiteCookieAttributes | NoneSameSiteCookieAttributes;
interface LaxStrictSameSiteCookieAttributes {
/**
* Only send the cookie if the request originates from the same website the
* cookie is from. This provides some protection against cross-site request
* forgery attacks (CSRF).
*
* The strict mode witholds the cookie from any kind of cross-site usage
* (including inbound links from external sites). The lax mode witholds
* the cookie on cross-domain subrequests (e.g. images or frames), but
* sends it whenever a user navigates safely from an external site (e.g.
* by following a link).
*/
sameSite?: 'strict' | 'lax';
}
/**
* Cookies with `SameSite=None` must also specify 'Secure'
*/
interface NoneSameSiteCookieAttributes {
sameSite: 'none';
secure: true;
}
export {};
+2
View File
@@ -0,0 +1,2 @@
"use strict";
exports.__esModule = true;
+12
View File
@@ -0,0 +1,12 @@
import { CookieAttributes } from './CookieAttributes';
export { CookieAttributes };
export declare function encode(name: string, value: string, attributes: CookieAttributes): string;
export declare function parse(cookieString: string): {
[name: string]: string;
};
export declare function getAll(): {
[name: string]: string;
};
export declare function get(name: string): string | undefined;
export declare function set(name: string, value: string, attributes?: CookieAttributes): void;
export declare function remove(name: string, attributes?: CookieAttributes): void;
+82
View File
@@ -0,0 +1,82 @@
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
exports.__esModule = true;
function stringifyAttribute(name, value) {
if (!value) {
return '';
}
var stringified = '; ' + name;
if (value === true) {
return stringified; // boolean attributes shouldn't have a value
}
return stringified + '=' + value;
}
function stringifyAttributes(attributes) {
if (typeof attributes.expires === 'number') {
var expires = new Date();
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
attributes.expires = expires;
}
return stringifyAttribute('Expires', attributes.expires ? attributes.expires.toUTCString() : '')
+ stringifyAttribute('Domain', attributes.domain)
+ stringifyAttribute('Path', attributes.path)
+ stringifyAttribute('Secure', attributes.secure)
+ stringifyAttribute('SameSite', attributes.sameSite);
}
function encode(name, value, attributes) {
return encodeURIComponent(name)
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent) // allowed special characters
.replace(/\(/g, '%28').replace(/\)/g, '%29') // replace opening and closing parens
+ '=' + encodeURIComponent(value)
// allowed special characters
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent)
+ stringifyAttributes(attributes);
}
exports.encode = encode;
function parse(cookieString) {
var result = {};
var cookies = cookieString ? cookieString.split('; ') : [];
var rdecode = /(%[\dA-F]{2})+/gi;
for (var i = 0; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var cookie = parts.slice(1).join('=');
if (cookie.charAt(0) === '"') {
cookie = cookie.slice(1, -1);
}
try {
var name_1 = parts[0].replace(rdecode, decodeURIComponent);
result[name_1] = cookie.replace(rdecode, decodeURIComponent);
}
catch (e) {
// ignore cookies with invalid name/value encoding
}
}
return result;
}
exports.parse = parse;
function getAll() {
return parse(document.cookie);
}
exports.getAll = getAll;
function get(name) {
return getAll()[name];
}
exports.get = get;
function set(name, value, attributes) {
document.cookie = encode(name, value, __assign({ path: '/' }, attributes));
}
exports.set = set;
function remove(name, attributes) {
set(name, '', __assign(__assign({}, attributes), { expires: -1 }));
}
exports.remove = remove;