feat(frontend): scaffold SvelteKit with TS 7.0 (native-preview) and tsgo
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
||||
export interface Cookie {
|
||||
/** Cookie name */
|
||||
name: string;
|
||||
/** Cookie value */
|
||||
value: string;
|
||||
/** Cookie path */
|
||||
path?: string;
|
||||
/** Absolute expiration date for the cookie */
|
||||
expires?: Date;
|
||||
/**
|
||||
* Relative max age of the cookie in seconds from when the client receives it.
|
||||
* Note: when using with express's res.cookie() method, multiply maxAge by 1000 to convert to milliseconds.
|
||||
*/
|
||||
maxAge?: number;
|
||||
/**
|
||||
* Domain for the cookie,
|
||||
* may begin with "." to indicate the named domain or any subdomain of it
|
||||
*/
|
||||
domain?: string;
|
||||
/** Indicates that this cookie should only be sent over HTTPS */
|
||||
secure?: boolean;
|
||||
/** Indicates that this cookie should not be accessible to client-side JavaScript */
|
||||
httpOnly?: boolean;
|
||||
/** Indicates a cookie ought not to be sent along with cross-site requests */
|
||||
sameSite?: string;
|
||||
/** Indicates the cookie should be stored using partitioned storage */
|
||||
partitioned?: boolean;
|
||||
}
|
||||
|
||||
export interface CookieMap {
|
||||
[name: string]: Cookie;
|
||||
}
|
||||
|
||||
export interface Options {
|
||||
/**
|
||||
* Calls decodeURIComponent on each value.
|
||||
* @default true
|
||||
*/
|
||||
decodeValues?: boolean;
|
||||
/**
|
||||
* Return an object instead of an array.
|
||||
* @default false
|
||||
*/
|
||||
map?: boolean;
|
||||
/**
|
||||
* Suppress the warning that is logged when called on a request instead of a response.
|
||||
* @default false
|
||||
*/
|
||||
silent?: boolean;
|
||||
/**
|
||||
* Controls whether combined cookie strings are split.
|
||||
* - `true`: always split
|
||||
* - `false`: never split
|
||||
* - `"auto"`: split strings but not arrays
|
||||
* @default "auto"
|
||||
*/
|
||||
split?: boolean | "auto";
|
||||
}
|
||||
|
||||
/** Object with a `headers` property (e.g. Node.js IncomingMessage or fetch Response) */
|
||||
type ResponseLike = {
|
||||
headers:
|
||||
| { getSetCookie(): string[] }
|
||||
| { "set-cookie"?: string | readonly string[] }
|
||||
| Record<string, string | string[] | undefined>;
|
||||
};
|
||||
|
||||
type SetCookieInput = string | readonly string[] | ResponseLike;
|
||||
|
||||
/**
|
||||
* Parses set-cookie headers into objects.
|
||||
*/
|
||||
export function parseSetCookie(
|
||||
input: SetCookieInput,
|
||||
options: Options & { map: true }
|
||||
): CookieMap;
|
||||
export function parseSetCookie(
|
||||
input: SetCookieInput,
|
||||
options?: Options & { map?: false }
|
||||
): Cookie[];
|
||||
export function parseSetCookie(
|
||||
input: SetCookieInput,
|
||||
options?: Options
|
||||
): Cookie[] | CookieMap;
|
||||
|
||||
/**
|
||||
* Parses a single set-cookie header value string.
|
||||
* @deprecated Use `parseSetCookie` instead.
|
||||
*/
|
||||
export function parseString(
|
||||
setCookieValue: string,
|
||||
options?: Options
|
||||
): Cookie | null;
|
||||
|
||||
/**
|
||||
* Splits a combined set-cookie header string into individual set-cookie header strings.
|
||||
* @deprecated Use `parseSetCookie` with the `split` option instead.
|
||||
*/
|
||||
export function splitCookiesString(
|
||||
input: string | readonly string[] | undefined
|
||||
): string[];
|
||||
|
||||
/**
|
||||
* @deprecated Renamed to `parseSetCookie`. Kept for backward compatibility.
|
||||
*/
|
||||
export {
|
||||
parseSetCookie as parse,
|
||||
};
|
||||
|
||||
/**
|
||||
* Default export — the `parseSetCookie` function with additional properties for backward compatibility.
|
||||
*/
|
||||
declare const _default: typeof parseSetCookie & {
|
||||
parseSetCookie: typeof parseSetCookie;
|
||||
parse: typeof parseSetCookie;
|
||||
parseString: typeof parseString;
|
||||
splitCookiesString: typeof splitCookiesString;
|
||||
};
|
||||
export default _default;
|
||||
+265
@@ -0,0 +1,265 @@
|
||||
var defaultParseOptions = {
|
||||
decodeValues: true,
|
||||
map: false,
|
||||
silent: false,
|
||||
split: "auto", // auto = split strings but not arrays
|
||||
};
|
||||
|
||||
function isForbiddenKey(key) {
|
||||
return typeof key !== "string" || key in {};
|
||||
}
|
||||
|
||||
function createNullObj() {
|
||||
return Object.create(null);
|
||||
}
|
||||
|
||||
function isNonEmptyString(str) {
|
||||
return typeof str === "string" && !!str.trim();
|
||||
}
|
||||
|
||||
function parseString(setCookieValue, options) {
|
||||
var parts = setCookieValue.split(";").filter(isNonEmptyString);
|
||||
|
||||
var nameValuePairStr = parts.shift();
|
||||
var parsed = parseNameValuePair(nameValuePairStr);
|
||||
var name = parsed.name;
|
||||
var value = parsed.value;
|
||||
|
||||
options = options
|
||||
? Object.assign({}, defaultParseOptions, options)
|
||||
: defaultParseOptions;
|
||||
|
||||
if (isForbiddenKey(name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
value = options.decodeValues ? decodeURIComponent(value) : value; // decode cookie value
|
||||
} catch (e) {
|
||||
console.error(
|
||||
"set-cookie-parser: failed to decode cookie value. Set options.decodeValues=false to disable decoding.",
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
var cookie = createNullObj();
|
||||
cookie.name = name;
|
||||
cookie.value = value;
|
||||
|
||||
parts.forEach(function (part) {
|
||||
var sides = part.split("=");
|
||||
var key = sides.shift().trimLeft().toLowerCase();
|
||||
if (isForbiddenKey(key)) {
|
||||
return;
|
||||
}
|
||||
var value = sides.join("=");
|
||||
if (key === "expires") {
|
||||
cookie.expires = new Date(value);
|
||||
} else if (key === "max-age") {
|
||||
var n = parseInt(value, 10);
|
||||
if (!Number.isNaN(n)) cookie.maxAge = n;
|
||||
} else if (key === "secure") {
|
||||
cookie.secure = true;
|
||||
} else if (key === "httponly") {
|
||||
cookie.httpOnly = true;
|
||||
} else if (key === "samesite") {
|
||||
cookie.sameSite = value;
|
||||
} else if (key === "partitioned") {
|
||||
cookie.partitioned = true;
|
||||
} else if (key) {
|
||||
cookie[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
return cookie;
|
||||
}
|
||||
|
||||
function parseNameValuePair(nameValuePairStr) {
|
||||
// Parses name-value-pair according to rfc6265bis draft
|
||||
|
||||
var name = "";
|
||||
var value = "";
|
||||
var nameValueArr = nameValuePairStr.split("=");
|
||||
if (nameValueArr.length > 1) {
|
||||
name = nameValueArr.shift();
|
||||
value = nameValueArr.join("="); // everything after the first =, joined by a "=" if there was more than one part
|
||||
} else {
|
||||
value = nameValuePairStr;
|
||||
}
|
||||
|
||||
return { name: name, value: value };
|
||||
}
|
||||
|
||||
function parseSetCookie(input, options) {
|
||||
options = options
|
||||
? Object.assign({}, defaultParseOptions, options)
|
||||
: defaultParseOptions;
|
||||
|
||||
if (!input) {
|
||||
if (!options.map) {
|
||||
return [];
|
||||
} else {
|
||||
return createNullObj();
|
||||
}
|
||||
}
|
||||
|
||||
if (input.headers) {
|
||||
if (typeof input.headers.getSetCookie === "function") {
|
||||
// for fetch responses - they combine headers of the same type in the headers array,
|
||||
// but getSetCookie returns an uncombined array
|
||||
input = input.headers.getSetCookie();
|
||||
} else if (input.headers["set-cookie"]) {
|
||||
// fast-path for node.js (which automatically normalizes header names to lower-case)
|
||||
input = input.headers["set-cookie"];
|
||||
} else {
|
||||
// slow-path for other environments - see #25
|
||||
var sch =
|
||||
input.headers[
|
||||
Object.keys(input.headers).find(function (key) {
|
||||
return key.toLowerCase() === "set-cookie";
|
||||
})
|
||||
];
|
||||
// warn if called on a request-like object with a cookie header rather than a set-cookie header - see #34, 36
|
||||
if (!sch && input.headers.cookie && !options.silent) {
|
||||
console.warn(
|
||||
"Warning: set-cookie-parser appears to have been called on a request object. It is designed to parse Set-Cookie headers from responses, not Cookie headers from requests. Set the option {silent: true} to suppress this warning."
|
||||
);
|
||||
}
|
||||
input = sch;
|
||||
}
|
||||
}
|
||||
|
||||
var split = options.split;
|
||||
var isArray = Array.isArray(input);
|
||||
|
||||
if (split === "auto") {
|
||||
split = !isArray;
|
||||
}
|
||||
|
||||
if (!isArray) {
|
||||
input = [input];
|
||||
}
|
||||
|
||||
input = input.filter(isNonEmptyString);
|
||||
|
||||
if (split) {
|
||||
input = input.map(splitCookiesString).flat();
|
||||
}
|
||||
|
||||
if (!options.map) {
|
||||
return input
|
||||
.map(function (str) {
|
||||
return parseString(str, options);
|
||||
})
|
||||
.filter(Boolean);
|
||||
} else {
|
||||
var cookies = createNullObj();
|
||||
return input.reduce(function (cookies, str) {
|
||||
var cookie = parseString(str, options);
|
||||
if (cookie && !isForbiddenKey(cookie.name)) {
|
||||
cookies[cookie.name] = cookie;
|
||||
}
|
||||
return cookies;
|
||||
}, cookies);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
|
||||
that are within a single set-cookie field-value, such as in the Expires portion.
|
||||
|
||||
This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2
|
||||
Node.js does this for every header *except* set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128
|
||||
React Native's fetch does this for *every* header, including set-cookie.
|
||||
|
||||
Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25
|
||||
Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation
|
||||
*/
|
||||
function splitCookiesString(cookiesString) {
|
||||
if (Array.isArray(cookiesString)) {
|
||||
return cookiesString;
|
||||
}
|
||||
if (typeof cookiesString !== "string") {
|
||||
return [];
|
||||
}
|
||||
|
||||
var cookiesStrings = [];
|
||||
var pos = 0;
|
||||
var start;
|
||||
var ch;
|
||||
var lastComma;
|
||||
var nextStart;
|
||||
var cookiesSeparatorFound;
|
||||
|
||||
function skipWhitespace() {
|
||||
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
|
||||
pos += 1;
|
||||
}
|
||||
return pos < cookiesString.length;
|
||||
}
|
||||
|
||||
function notSpecialChar() {
|
||||
ch = cookiesString.charAt(pos);
|
||||
|
||||
return ch !== "=" && ch !== ";" && ch !== ",";
|
||||
}
|
||||
|
||||
while (pos < cookiesString.length) {
|
||||
start = pos;
|
||||
cookiesSeparatorFound = false;
|
||||
|
||||
while (skipWhitespace()) {
|
||||
ch = cookiesString.charAt(pos);
|
||||
if (ch === ",") {
|
||||
// ',' is a cookie separator if we have later first '=', not ';' or ','
|
||||
lastComma = pos;
|
||||
pos += 1;
|
||||
|
||||
skipWhitespace();
|
||||
nextStart = pos;
|
||||
|
||||
while (pos < cookiesString.length && notSpecialChar()) {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
// currently special character
|
||||
if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
|
||||
// we found cookies separator
|
||||
cookiesSeparatorFound = true;
|
||||
// pos is inside the next cookie, so back up and return it.
|
||||
pos = nextStart;
|
||||
cookiesStrings.push(cookiesString.substring(start, lastComma));
|
||||
start = pos;
|
||||
} else {
|
||||
// in param ',' or param separator ';',
|
||||
// we continue from that comma
|
||||
pos = lastComma + 1;
|
||||
}
|
||||
} else {
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!cookiesSeparatorFound || pos >= cookiesString.length) {
|
||||
cookiesStrings.push(cookiesString.substring(start, cookiesString.length));
|
||||
}
|
||||
}
|
||||
|
||||
return cookiesStrings;
|
||||
}
|
||||
|
||||
// named export for CJS
|
||||
parseSetCookie.parseSetCookie = parseSetCookie;
|
||||
// for backwards compatibility
|
||||
parseSetCookie.parse = parseSetCookie;
|
||||
parseSetCookie.parseString = parseString;
|
||||
parseSetCookie.splitCookiesString = splitCookiesString;
|
||||
|
||||
// EXPORTS
|
||||
// (this section is replaced by build-cjs.js)
|
||||
|
||||
// named export for ESM
|
||||
export { parseSetCookie };
|
||||
// for backwards compatibility
|
||||
export default parseSetCookie;
|
||||
export { parseSetCookie as parse, parseString, splitCookiesString };
|
||||
Reference in New Issue
Block a user