@kevisual/router 0.0.38 → 0.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/router-browser.d.ts +3 -3
- package/dist/router-browser.js +2 -5
- package/dist/router-sign.js +1 -3
- package/dist/router-simple.d.ts +2 -2
- package/dist/router.d.ts +98 -4
- package/dist/router.js +243 -369
- package/package.json +6 -8
- package/src/result/error.ts +2 -5
- package/src/server/cookie.ts +543 -0
- package/src/server/server.ts +1 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@kevisual/router",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.40",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/router.js",
|
|
@@ -22,20 +22,18 @@
|
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@kevisual/local-proxy": "^0.0.8",
|
|
25
|
-
"@kevisual/query": "^0.0.
|
|
25
|
+
"@kevisual/query": "^0.0.32",
|
|
26
26
|
"@rollup/plugin-alias": "^6.0.0",
|
|
27
27
|
"@rollup/plugin-commonjs": "29.0.0",
|
|
28
28
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
29
29
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
30
|
-
"@types/
|
|
31
|
-
"@types/node": "^24.10.2",
|
|
30
|
+
"@types/node": "^25.0.3",
|
|
32
31
|
"@types/send": "^1.2.1",
|
|
33
32
|
"@types/ws": "^8.18.1",
|
|
34
33
|
"@types/xml2js": "^0.4.14",
|
|
35
34
|
"cookie": "^1.1.1",
|
|
36
|
-
"lodash-es": "^4.17.21",
|
|
37
35
|
"nanoid": "^5.1.6",
|
|
38
|
-
"rollup": "^4.53.
|
|
36
|
+
"rollup": "^4.53.5",
|
|
39
37
|
"rollup-plugin-dts": "^6.3.0",
|
|
40
38
|
"ts-loader": "^9.5.4",
|
|
41
39
|
"ts-node": "^10.9.2",
|
|
@@ -43,7 +41,7 @@
|
|
|
43
41
|
"typescript": "^5.9.3",
|
|
44
42
|
"ws": "npm:@kevisual/ws",
|
|
45
43
|
"xml2js": "^0.6.2",
|
|
46
|
-
"zod": "^4.1
|
|
44
|
+
"zod": "^4.2.1"
|
|
47
45
|
},
|
|
48
46
|
"repository": {
|
|
49
47
|
"type": "git",
|
|
@@ -52,7 +50,7 @@
|
|
|
52
50
|
"dependencies": {
|
|
53
51
|
"path-to-regexp": "^8.3.0",
|
|
54
52
|
"selfsigned": "^5.2.0",
|
|
55
|
-
"send": "^1.2.
|
|
53
|
+
"send": "^1.2.1"
|
|
56
54
|
},
|
|
57
55
|
"publishConfig": {
|
|
58
56
|
"access": "public"
|
package/src/result/error.ts
CHANGED
|
@@ -40,11 +40,8 @@ export class CustomError extends Error {
|
|
|
40
40
|
* @param err
|
|
41
41
|
* @returns
|
|
42
42
|
*/
|
|
43
|
-
static isError(
|
|
44
|
-
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
return false;
|
|
43
|
+
static isError(error: unknown): error is CustomError {
|
|
44
|
+
return error instanceof CustomError || (typeof error === 'object' && error !== null && 'code' in error);
|
|
48
45
|
}
|
|
49
46
|
parse(e?: CustomError) {
|
|
50
47
|
if (e) {
|
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RegExp to match cookie-name in RFC 6265 sec 4.1.1
|
|
3
|
+
* This refers out to the obsoleted definition of token in RFC 2616 sec 2.2
|
|
4
|
+
* which has been replaced by the token definition in RFC 7230 appendix B.
|
|
5
|
+
*
|
|
6
|
+
* cookie-name = token
|
|
7
|
+
* token = 1*tchar
|
|
8
|
+
* tchar = "!" / "#" / "$" / "%" / "&" / "'" /
|
|
9
|
+
* "*" / "+" / "-" / "." / "^" / "_" /
|
|
10
|
+
* "`" / "|" / "~" / DIGIT / ALPHA
|
|
11
|
+
*
|
|
12
|
+
* Note: Allowing more characters - https://github.com/jshttp/cookie/issues/191
|
|
13
|
+
* Allow same range as cookie value, except `=`, which delimits end of name.
|
|
14
|
+
*/
|
|
15
|
+
const cookieNameRegExp = /^[\u0021-\u003A\u003C\u003E-\u007E]+$/;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* RegExp to match cookie-value in RFC 6265 sec 4.1.1
|
|
19
|
+
*
|
|
20
|
+
* cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
|
|
21
|
+
* cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
|
|
22
|
+
* ; US-ASCII characters excluding CTLs,
|
|
23
|
+
* ; whitespace DQUOTE, comma, semicolon,
|
|
24
|
+
* ; and backslash
|
|
25
|
+
*
|
|
26
|
+
* Allowing more characters: https://github.com/jshttp/cookie/issues/191
|
|
27
|
+
* Comma, backslash, and DQUOTE are not part of the parsing algorithm.
|
|
28
|
+
*/
|
|
29
|
+
const cookieValueRegExp = /^[\u0021-\u003A\u003C-\u007E]*$/;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* RegExp to match domain-value in RFC 6265 sec 4.1.1
|
|
33
|
+
*
|
|
34
|
+
* domain-value = <subdomain>
|
|
35
|
+
* ; defined in [RFC1034], Section 3.5, as
|
|
36
|
+
* ; enhanced by [RFC1123], Section 2.1
|
|
37
|
+
* <subdomain> = <label> | <subdomain> "." <label>
|
|
38
|
+
* <label> = <let-dig> [ [ <ldh-str> ] <let-dig> ]
|
|
39
|
+
* Labels must be 63 characters or less.
|
|
40
|
+
* 'let-dig' not 'letter' in the first char, per RFC1123
|
|
41
|
+
* <ldh-str> = <let-dig-hyp> | <let-dig-hyp> <ldh-str>
|
|
42
|
+
* <let-dig-hyp> = <let-dig> | "-"
|
|
43
|
+
* <let-dig> = <letter> | <digit>
|
|
44
|
+
* <letter> = any one of the 52 alphabetic characters A through Z in
|
|
45
|
+
* upper case and a through z in lower case
|
|
46
|
+
* <digit> = any one of the ten digits 0 through 9
|
|
47
|
+
*
|
|
48
|
+
* Keep support for leading dot: https://github.com/jshttp/cookie/issues/173
|
|
49
|
+
*
|
|
50
|
+
* > (Note that a leading %x2E ("."), if present, is ignored even though that
|
|
51
|
+
* character is not permitted, but a trailing %x2E ("."), if present, will
|
|
52
|
+
* cause the user agent to ignore the attribute.)
|
|
53
|
+
*/
|
|
54
|
+
const domainValueRegExp =
|
|
55
|
+
/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* RegExp to match path-value in RFC 6265 sec 4.1.1
|
|
59
|
+
*
|
|
60
|
+
* path-value = <any CHAR except CTLs or ";">
|
|
61
|
+
* CHAR = %x01-7F
|
|
62
|
+
* ; defined in RFC 5234 appendix B.1
|
|
63
|
+
*/
|
|
64
|
+
const pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* RegExp to match max-age-value in RFC 6265 sec 5.6.2
|
|
68
|
+
*/
|
|
69
|
+
const maxAgeRegExp = /^-?\d+$/;
|
|
70
|
+
|
|
71
|
+
const __toString = Object.prototype.toString;
|
|
72
|
+
|
|
73
|
+
const NullObject = /* @__PURE__ */ (() => {
|
|
74
|
+
const C = function () {};
|
|
75
|
+
C.prototype = Object.create(null);
|
|
76
|
+
return C;
|
|
77
|
+
})() as unknown as { new (): any };
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Parse options.
|
|
81
|
+
*/
|
|
82
|
+
export interface ParseOptions {
|
|
83
|
+
/**
|
|
84
|
+
* Specifies a function that will be used to decode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
|
|
85
|
+
* Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode
|
|
86
|
+
* a previously-encoded cookie value into a JavaScript string.
|
|
87
|
+
*
|
|
88
|
+
* The default function is the global `decodeURIComponent`, wrapped in a `try..catch`. If an error
|
|
89
|
+
* is thrown it will return the cookie's original value. If you provide your own encode/decode
|
|
90
|
+
* scheme you must ensure errors are appropriately handled.
|
|
91
|
+
*
|
|
92
|
+
* @default decode
|
|
93
|
+
*/
|
|
94
|
+
decode?: (str: string) => string | undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Cookies object.
|
|
99
|
+
*/
|
|
100
|
+
export type Cookies = Record<string, string | undefined>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Parse a `Cookie` header.
|
|
104
|
+
*
|
|
105
|
+
* Parse the given cookie header string into an object
|
|
106
|
+
* The object has the various cookies as keys(names) => values
|
|
107
|
+
*/
|
|
108
|
+
export function parseCookie(str: string, options?: ParseOptions): Cookies {
|
|
109
|
+
const obj: Cookies = new NullObject();
|
|
110
|
+
const len = str.length;
|
|
111
|
+
// RFC 6265 sec 4.1.1, RFC 2616 2.2 defines a cookie name consists of one char minimum, plus '='.
|
|
112
|
+
if (len < 2) return obj;
|
|
113
|
+
|
|
114
|
+
const dec = options?.decode || decode;
|
|
115
|
+
let index = 0;
|
|
116
|
+
|
|
117
|
+
do {
|
|
118
|
+
const eqIdx = eqIndex(str, index, len);
|
|
119
|
+
if (eqIdx === -1) break; // No more cookie pairs.
|
|
120
|
+
|
|
121
|
+
const endIdx = endIndex(str, index, len);
|
|
122
|
+
|
|
123
|
+
if (eqIdx > endIdx) {
|
|
124
|
+
// backtrack on prior semicolon
|
|
125
|
+
index = str.lastIndexOf(";", eqIdx - 1) + 1;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const key = valueSlice(str, index, eqIdx);
|
|
130
|
+
|
|
131
|
+
// only assign once
|
|
132
|
+
if (obj[key] === undefined) {
|
|
133
|
+
obj[key] = dec(valueSlice(str, eqIdx + 1, endIdx));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
index = endIdx + 1;
|
|
137
|
+
} while (index < len);
|
|
138
|
+
|
|
139
|
+
return obj;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface StringifyOptions {
|
|
143
|
+
/**
|
|
144
|
+
* Specifies a function that will be used to encode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
|
|
145
|
+
* Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
|
|
146
|
+
* a value into a string suited for a cookie's value, and should mirror `decode` when parsing.
|
|
147
|
+
*
|
|
148
|
+
* @default encodeURIComponent
|
|
149
|
+
*/
|
|
150
|
+
encode?: (str: string) => string;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Stringifies an object into an HTTP `Cookie` header.
|
|
155
|
+
*/
|
|
156
|
+
export function stringifyCookie(
|
|
157
|
+
cookie: Cookies,
|
|
158
|
+
options?: StringifyOptions,
|
|
159
|
+
): string {
|
|
160
|
+
const enc = options?.encode || encodeURIComponent;
|
|
161
|
+
const cookieStrings: string[] = [];
|
|
162
|
+
|
|
163
|
+
for (const name of Object.keys(cookie)) {
|
|
164
|
+
const val = cookie[name];
|
|
165
|
+
if (val === undefined) continue;
|
|
166
|
+
|
|
167
|
+
if (!cookieNameRegExp.test(name)) {
|
|
168
|
+
throw new TypeError(`cookie name is invalid: ${name}`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const value = enc(val);
|
|
172
|
+
|
|
173
|
+
if (!cookieValueRegExp.test(value)) {
|
|
174
|
+
throw new TypeError(`cookie val is invalid: ${val}`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
cookieStrings.push(`${name}=${value}`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return cookieStrings.join("; ");
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Set-Cookie object.
|
|
185
|
+
*/
|
|
186
|
+
export interface SetCookie {
|
|
187
|
+
/**
|
|
188
|
+
* Specifies the name of the cookie.
|
|
189
|
+
*/
|
|
190
|
+
name: string;
|
|
191
|
+
/**
|
|
192
|
+
* Specifies the string to be the value for the cookie.
|
|
193
|
+
*/
|
|
194
|
+
value: string | undefined;
|
|
195
|
+
/**
|
|
196
|
+
* Specifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2).
|
|
197
|
+
*
|
|
198
|
+
* The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
|
|
199
|
+
* `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
|
|
200
|
+
* so if both are set, they should point to the same date and time.
|
|
201
|
+
*/
|
|
202
|
+
maxAge?: number;
|
|
203
|
+
/**
|
|
204
|
+
* Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1).
|
|
205
|
+
* When no expiration is set, clients consider this a "non-persistent cookie" and delete it when the current session is over.
|
|
206
|
+
*
|
|
207
|
+
* The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
|
|
208
|
+
* `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
|
|
209
|
+
* so if both are set, they should point to the same date and time.
|
|
210
|
+
*/
|
|
211
|
+
expires?: Date;
|
|
212
|
+
/**
|
|
213
|
+
* Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3).
|
|
214
|
+
* When no domain is set, clients consider the cookie to apply to the current domain only.
|
|
215
|
+
*/
|
|
216
|
+
domain?: string;
|
|
217
|
+
/**
|
|
218
|
+
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
|
|
219
|
+
* When no path is set, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
|
|
220
|
+
*/
|
|
221
|
+
path?: string;
|
|
222
|
+
/**
|
|
223
|
+
* Enables the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6).
|
|
224
|
+
* When enabled, clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
|
|
225
|
+
*/
|
|
226
|
+
httpOnly?: boolean;
|
|
227
|
+
/**
|
|
228
|
+
* Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
|
|
229
|
+
* When enabled, clients will only send the cookie back if the browser has an HTTPS connection.
|
|
230
|
+
*/
|
|
231
|
+
secure?: boolean;
|
|
232
|
+
/**
|
|
233
|
+
* Enables the [`Partitioned` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/).
|
|
234
|
+
* When enabled, clients will only send the cookie back when the current domain _and_ top-level domain matches.
|
|
235
|
+
*
|
|
236
|
+
* This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
237
|
+
* This also means clients may ignore this attribute until they understand it. More information
|
|
238
|
+
* about can be found in [the proposal](https://github.com/privacycg/CHIPS).
|
|
239
|
+
*/
|
|
240
|
+
partitioned?: boolean;
|
|
241
|
+
/**
|
|
242
|
+
* Specifies the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
|
|
243
|
+
*
|
|
244
|
+
* - `'low'` will set the `Priority` attribute to `Low`.
|
|
245
|
+
* - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
|
|
246
|
+
* - `'high'` will set the `Priority` attribute to `High`.
|
|
247
|
+
*
|
|
248
|
+
* More information about priority levels can be found in [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
|
|
249
|
+
*/
|
|
250
|
+
priority?: "low" | "medium" | "high";
|
|
251
|
+
/**
|
|
252
|
+
* Specifies the value for the [`SameSite` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
|
|
253
|
+
*
|
|
254
|
+
* - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
|
|
255
|
+
* - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
|
|
256
|
+
* - `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
|
|
257
|
+
* - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
|
|
258
|
+
*
|
|
259
|
+
* More information about enforcement levels can be found in [the specification](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
|
|
260
|
+
*/
|
|
261
|
+
sameSite?: boolean | "lax" | "strict" | "none";
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Backward compatibility serialize options.
|
|
266
|
+
*/
|
|
267
|
+
export type SerializeOptions = StringifyOptions &
|
|
268
|
+
Omit<SetCookie, "name" | "value">;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Serialize data into a cookie header.
|
|
272
|
+
*
|
|
273
|
+
* Serialize a name value pair into a cookie string suitable for
|
|
274
|
+
* http headers. An optional options object specifies cookie parameters.
|
|
275
|
+
*
|
|
276
|
+
* serialize('foo', 'bar', { httpOnly: true })
|
|
277
|
+
* => "foo=bar; httpOnly"
|
|
278
|
+
*/
|
|
279
|
+
export function stringifySetCookie(
|
|
280
|
+
cookie: SetCookie,
|
|
281
|
+
options?: StringifyOptions,
|
|
282
|
+
): string;
|
|
283
|
+
export function stringifySetCookie(
|
|
284
|
+
name: string,
|
|
285
|
+
val: string,
|
|
286
|
+
options?: SerializeOptions,
|
|
287
|
+
): string;
|
|
288
|
+
export function stringifySetCookie(
|
|
289
|
+
_name: string | SetCookie,
|
|
290
|
+
_val?: string | StringifyOptions,
|
|
291
|
+
_opts?: SerializeOptions,
|
|
292
|
+
): string {
|
|
293
|
+
const cookie =
|
|
294
|
+
typeof _name === "object"
|
|
295
|
+
? _name
|
|
296
|
+
: { ..._opts, name: _name, value: String(_val) };
|
|
297
|
+
const options = typeof _val === "object" ? _val : _opts;
|
|
298
|
+
const enc = options?.encode || encodeURIComponent;
|
|
299
|
+
|
|
300
|
+
if (!cookieNameRegExp.test(cookie.name)) {
|
|
301
|
+
throw new TypeError(`argument name is invalid: ${cookie.name}`);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const value = cookie.value ? enc(cookie.value) : "";
|
|
305
|
+
|
|
306
|
+
if (!cookieValueRegExp.test(value)) {
|
|
307
|
+
throw new TypeError(`argument val is invalid: ${cookie.value}`);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
let str = cookie.name + "=" + value;
|
|
311
|
+
|
|
312
|
+
if (cookie.maxAge !== undefined) {
|
|
313
|
+
if (!Number.isInteger(cookie.maxAge)) {
|
|
314
|
+
throw new TypeError(`option maxAge is invalid: ${cookie.maxAge}`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
str += "; Max-Age=" + cookie.maxAge;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (cookie.domain) {
|
|
321
|
+
if (!domainValueRegExp.test(cookie.domain)) {
|
|
322
|
+
throw new TypeError(`option domain is invalid: ${cookie.domain}`);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
str += "; Domain=" + cookie.domain;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (cookie.path) {
|
|
329
|
+
if (!pathValueRegExp.test(cookie.path)) {
|
|
330
|
+
throw new TypeError(`option path is invalid: ${cookie.path}`);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
str += "; Path=" + cookie.path;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (cookie.expires) {
|
|
337
|
+
if (!isDate(cookie.expires) || !Number.isFinite(cookie.expires.valueOf())) {
|
|
338
|
+
throw new TypeError(`option expires is invalid: ${cookie.expires}`);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
str += "; Expires=" + cookie.expires.toUTCString();
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (cookie.httpOnly) {
|
|
345
|
+
str += "; HttpOnly";
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (cookie.secure) {
|
|
349
|
+
str += "; Secure";
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (cookie.partitioned) {
|
|
353
|
+
str += "; Partitioned";
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (cookie.priority) {
|
|
357
|
+
const priority =
|
|
358
|
+
typeof cookie.priority === "string"
|
|
359
|
+
? cookie.priority.toLowerCase()
|
|
360
|
+
: undefined;
|
|
361
|
+
switch (priority) {
|
|
362
|
+
case "low":
|
|
363
|
+
str += "; Priority=Low";
|
|
364
|
+
break;
|
|
365
|
+
case "medium":
|
|
366
|
+
str += "; Priority=Medium";
|
|
367
|
+
break;
|
|
368
|
+
case "high":
|
|
369
|
+
str += "; Priority=High";
|
|
370
|
+
break;
|
|
371
|
+
default:
|
|
372
|
+
throw new TypeError(`option priority is invalid: ${cookie.priority}`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (cookie.sameSite) {
|
|
377
|
+
const sameSite =
|
|
378
|
+
typeof cookie.sameSite === "string"
|
|
379
|
+
? cookie.sameSite.toLowerCase()
|
|
380
|
+
: cookie.sameSite;
|
|
381
|
+
switch (sameSite) {
|
|
382
|
+
case true:
|
|
383
|
+
case "strict":
|
|
384
|
+
str += "; SameSite=Strict";
|
|
385
|
+
break;
|
|
386
|
+
case "lax":
|
|
387
|
+
str += "; SameSite=Lax";
|
|
388
|
+
break;
|
|
389
|
+
case "none":
|
|
390
|
+
str += "; SameSite=None";
|
|
391
|
+
break;
|
|
392
|
+
default:
|
|
393
|
+
throw new TypeError(`option sameSite is invalid: ${cookie.sameSite}`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return str;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Deserialize a `Set-Cookie` header into an object.
|
|
402
|
+
*
|
|
403
|
+
* deserialize('foo=bar; httpOnly')
|
|
404
|
+
* => { name: 'foo', value: 'bar', httpOnly: true }
|
|
405
|
+
*/
|
|
406
|
+
export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
|
|
407
|
+
const dec = options?.decode || decode;
|
|
408
|
+
const len = str.length;
|
|
409
|
+
const endIdx = endIndex(str, 0, len);
|
|
410
|
+
const eqIdx = eqIndex(str, 0, endIdx);
|
|
411
|
+
const setCookie: SetCookie =
|
|
412
|
+
eqIdx === -1
|
|
413
|
+
? { name: "", value: dec(valueSlice(str, 0, endIdx)) }
|
|
414
|
+
: {
|
|
415
|
+
name: valueSlice(str, 0, eqIdx),
|
|
416
|
+
value: dec(valueSlice(str, eqIdx + 1, endIdx)),
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
let index = endIdx + 1;
|
|
420
|
+
while (index < len) {
|
|
421
|
+
const endIdx = endIndex(str, index, len);
|
|
422
|
+
const eqIdx = eqIndex(str, index, endIdx);
|
|
423
|
+
const attr =
|
|
424
|
+
eqIdx === -1
|
|
425
|
+
? valueSlice(str, index, endIdx)
|
|
426
|
+
: valueSlice(str, index, eqIdx);
|
|
427
|
+
const val = eqIdx === -1 ? undefined : valueSlice(str, eqIdx + 1, endIdx);
|
|
428
|
+
|
|
429
|
+
switch (attr.toLowerCase()) {
|
|
430
|
+
case "httponly":
|
|
431
|
+
setCookie.httpOnly = true;
|
|
432
|
+
break;
|
|
433
|
+
case "secure":
|
|
434
|
+
setCookie.secure = true;
|
|
435
|
+
break;
|
|
436
|
+
case "partitioned":
|
|
437
|
+
setCookie.partitioned = true;
|
|
438
|
+
break;
|
|
439
|
+
case "domain":
|
|
440
|
+
setCookie.domain = val;
|
|
441
|
+
break;
|
|
442
|
+
case "path":
|
|
443
|
+
setCookie.path = val;
|
|
444
|
+
break;
|
|
445
|
+
case "max-age":
|
|
446
|
+
if (val && maxAgeRegExp.test(val)) setCookie.maxAge = Number(val);
|
|
447
|
+
break;
|
|
448
|
+
case "expires":
|
|
449
|
+
if (!val) break;
|
|
450
|
+
const date = new Date(val);
|
|
451
|
+
if (Number.isFinite(date.valueOf())) setCookie.expires = date;
|
|
452
|
+
break;
|
|
453
|
+
case "priority":
|
|
454
|
+
if (!val) break;
|
|
455
|
+
const priority = val.toLowerCase();
|
|
456
|
+
if (
|
|
457
|
+
priority === "low" ||
|
|
458
|
+
priority === "medium" ||
|
|
459
|
+
priority === "high"
|
|
460
|
+
) {
|
|
461
|
+
setCookie.priority = priority;
|
|
462
|
+
}
|
|
463
|
+
break;
|
|
464
|
+
case "samesite":
|
|
465
|
+
if (!val) break;
|
|
466
|
+
const sameSite = val.toLowerCase();
|
|
467
|
+
if (
|
|
468
|
+
sameSite === "lax" ||
|
|
469
|
+
sameSite === "strict" ||
|
|
470
|
+
sameSite === "none"
|
|
471
|
+
) {
|
|
472
|
+
setCookie.sameSite = sameSite;
|
|
473
|
+
}
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
index = endIdx + 1;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return setCookie;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Find the `;` character between `min` and `len` in str.
|
|
485
|
+
*/
|
|
486
|
+
function endIndex(str: string, min: number, len: number) {
|
|
487
|
+
const index = str.indexOf(";", min);
|
|
488
|
+
return index === -1 ? len : index;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Find the `=` character between `min` and `max` in str.
|
|
493
|
+
*/
|
|
494
|
+
function eqIndex(str: string, min: number, max: number) {
|
|
495
|
+
const index = str.indexOf("=", min);
|
|
496
|
+
return index < max ? index : -1;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Slice out a value between startPod to max.
|
|
501
|
+
*/
|
|
502
|
+
function valueSlice(str: string, min: number, max: number) {
|
|
503
|
+
let start = min;
|
|
504
|
+
let end = max;
|
|
505
|
+
|
|
506
|
+
do {
|
|
507
|
+
const code = str.charCodeAt(start);
|
|
508
|
+
if (code !== 0x20 /* */ && code !== 0x09 /* \t */) break;
|
|
509
|
+
} while (++start < end);
|
|
510
|
+
|
|
511
|
+
while (end > start) {
|
|
512
|
+
const code = str.charCodeAt(end - 1);
|
|
513
|
+
if (code !== 0x20 /* */ && code !== 0x09 /* \t */) break;
|
|
514
|
+
end--;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return str.slice(start, end);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* URL-decode string value. Optimized to skip native call when no %.
|
|
522
|
+
*/
|
|
523
|
+
function decode(str: string): string {
|
|
524
|
+
if (str.indexOf("%") === -1) return str;
|
|
525
|
+
|
|
526
|
+
try {
|
|
527
|
+
return decodeURIComponent(str);
|
|
528
|
+
} catch (e) {
|
|
529
|
+
return str;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Determine if value is a Date.
|
|
535
|
+
*/
|
|
536
|
+
function isDate(val: any): val is Date {
|
|
537
|
+
return __toString.call(val) === "[object Date]";
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Backward compatibility exports.
|
|
542
|
+
*/
|
|
543
|
+
export { stringifySetCookie as serialize, parseCookie as parse };
|
package/src/server/server.ts
CHANGED
|
@@ -3,7 +3,7 @@ import http from 'node:http';
|
|
|
3
3
|
import https from 'node:https';
|
|
4
4
|
import http2 from 'node:http2';
|
|
5
5
|
import { handleServer } from './handle-server.ts';
|
|
6
|
-
import * as cookie from 'cookie';
|
|
6
|
+
import * as cookie from './cookie.ts';
|
|
7
7
|
export type Listener = (...args: any[]) => void;
|
|
8
8
|
|
|
9
9
|
type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void;
|