@orpc/client 2.0.0-beta.49 → 2.0.0-beta.50
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/adapters/fetch/index.d.ts +3 -2
- package/dist/adapters/fetch/index.js +2 -2
- package/dist/adapters/fetch/rpc-link.d.ts +8 -8
- package/dist/adapters/fetch/rpc-link.js +16 -13
- package/dist/adapters/fetch/transport.d.ts +29 -31
- package/dist/adapters/fetch/transport.js +49 -63
- package/dist/adapters/message-port/index.d.ts +4 -3
- package/dist/adapters/message-port/index.js +3 -3
- package/dist/adapters/message-port/message-port.d.ts +13 -13
- package/dist/adapters/message-port/message-port.js +35 -53
- package/dist/adapters/message-port/rpc-link.d.ts +8 -8
- package/dist/adapters/message-port/rpc-link.js +17 -14
- package/dist/adapters/message-port/transport.d.ts +31 -34
- package/dist/adapters/message-port/transport.js +38 -52
- package/dist/adapters/standard/codec.d.ts +10 -10
- package/dist/adapters/standard/index.d.ts +6 -5
- package/dist/adapters/standard/index.js +2 -4
- package/dist/adapters/standard/link.d.ts +31 -31
- package/dist/adapters/standard/link.js +101 -85
- package/dist/adapters/standard/plugin.d.ts +25 -31
- package/dist/adapters/standard/plugin.js +14 -16
- package/dist/adapters/standard/rpc-link-codec.d.ts +53 -60
- package/dist/adapters/standard/rpc-link-codec.js +98 -102
- package/dist/adapters/standard/transport.d.ts +7 -7
- package/dist/adapters/websocket/index.d.ts +3 -2
- package/dist/adapters/websocket/index.js +2 -2
- package/dist/adapters/websocket/rpc-link.d.ts +8 -8
- package/dist/adapters/websocket/rpc-link.js +16 -13
- package/dist/adapters/websocket/transport.d.ts +101 -100
- package/dist/adapters/websocket/transport.js +102 -130
- package/dist/async-iterator-object.d.ts +2 -2
- package/dist/async-iterator-object.js +27 -28
- package/dist/client-safe.d.ts +4 -6
- package/dist/client-safe.js +29 -34
- package/dist/client.d.ts +24 -26
- package/dist/client.js +35 -37
- package/dist/consts.d.ts +1 -1
- package/dist/consts.js +18 -41
- package/dist/dynamic-link.d.ts +6 -6
- package/dist/dynamic-link.js +16 -16
- package/dist/error-utils.d.ts +3 -4
- package/dist/error-utils.js +47 -51
- package/dist/error.d.ts +79 -79
- package/dist/error.js +154 -166
- package/dist/index.d.ts +14 -53
- package/dist/index.js +12 -40
- package/dist/plugins/batch.d.ts +122 -155
- package/dist/plugins/batch.js +320 -391
- package/dist/plugins/cookie-jar.d.ts +64 -61
- package/dist/plugins/cookie-jar.js +208 -245
- package/dist/plugins/dedupe.d.ts +49 -53
- package/dist/plugins/dedupe.js +168 -211
- package/dist/plugins/index.d.ts +9 -28
- package/dist/plugins/index.js +8 -23
- package/dist/plugins/request-compression.d.ts +38 -35
- package/dist/plugins/request-compression.js +131 -177
- package/dist/plugins/response-compression.d.ts +21 -18
- package/dist/plugins/response-compression.js +64 -88
- package/dist/plugins/retry-after.d.ts +31 -38
- package/dist/plugins/retry-after.js +60 -85
- package/dist/plugins/retry.d.ts +55 -55
- package/dist/plugins/retry.js +118 -137
- package/dist/plugins/timeout.d.ts +19 -17
- package/dist/plugins/timeout.js +41 -36
- package/dist/rpc-json-serializer.d.ts +84 -84
- package/dist/rpc-json-serializer.js +338 -383
- package/dist/rpc-serializer.d.ts +20 -21
- package/dist/rpc-serializer.js +97 -125
- package/dist/types.d.ts +19 -31
- package/dist/utils.d.ts +18 -29
- package/dist/utils.js +63 -47
- package/package.json +5 -5
- package/dist/adapters/standard/codec.js +0 -1
- package/dist/adapters/standard/transport.js +0 -1
- package/dist/types.js +0 -1
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import "../adapters/fetch/index.js";
|
|
2
|
+
import { StandardLinkPlugin } from "../adapters/standard/plugin.js";
|
|
3
|
+
import { StandardLinkOptions, StandardLinkTransportInterceptorOptions } from "../adapters/standard/link.js";
|
|
4
|
+
import "../adapters/standard/index.js";
|
|
5
|
+
import { FetchLinkTransportOptions } from "../adapters/fetch/transport.js";
|
|
6
|
+
import { ClientContext } from "../types.js";
|
|
7
|
+
import { Value } from "@orpc/shared";
|
|
5
8
|
export interface CookieJarLinkPluginOptions<T extends ClientContext> {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The link's `origin`, used to match cookies by host and scheme.
|
|
11
|
+
* The plugin does nothing when it resolves to `undefined` or a relative origin, as in a browser.
|
|
12
|
+
*
|
|
13
|
+
* @example 'https://api.example.com'
|
|
14
|
+
*/
|
|
15
|
+
origin: FetchLinkTransportOptions<T>["origin"];
|
|
16
|
+
/**
|
|
17
|
+
* Where cookies are stored. Pass a function to pick a jar per request, e.g. from the client context.
|
|
18
|
+
*
|
|
19
|
+
* @default a new `CookieJar` per plugin
|
|
20
|
+
*/
|
|
21
|
+
jar?: Value<CookieJar, [options: StandardLinkTransportInterceptorOptions<T>]> | undefined;
|
|
19
22
|
}
|
|
20
23
|
/**
|
|
21
24
|
* Stores the cookies servers set and sends them back on later requests, like a browser,
|
|
@@ -27,33 +30,33 @@ export interface CookieJarLinkPluginOptions<T extends ClientContext> {
|
|
|
27
30
|
* @see {@link https://orpc.dev/docs/plugins/cookie-jar | Cookie Jar Plugin}
|
|
28
31
|
*/
|
|
29
32
|
export declare class CookieJarLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
private readonly origin;
|
|
34
|
+
private readonly jar;
|
|
35
|
+
name: string;
|
|
36
|
+
/**
|
|
37
|
+
* Runs per call, before batching, so each call gets the cookies it would get on its own.
|
|
38
|
+
*/
|
|
39
|
+
before: string[];
|
|
40
|
+
constructor(options: NoInfer<CookieJarLinkPluginOptions<T>>);
|
|
41
|
+
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
39
42
|
}
|
|
40
43
|
export interface StoredCookie {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
44
|
+
name: string;
|
|
45
|
+
value: string;
|
|
46
|
+
/**
|
|
47
|
+
* The `Domain` attribute, or else the host that set the cookie.
|
|
48
|
+
*/
|
|
49
|
+
domain: string;
|
|
50
|
+
/**
|
|
51
|
+
* True when the cookie has no `Domain` attribute, so it is sent to `domain` only, not its subdomains.
|
|
52
|
+
*/
|
|
53
|
+
hostOnly: boolean;
|
|
54
|
+
path: string;
|
|
55
|
+
secure: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Expiry time in milliseconds since the Unix epoch, `undefined` for a session cookie that lives as long as the jar.
|
|
58
|
+
*/
|
|
59
|
+
expires: number | undefined;
|
|
57
60
|
}
|
|
58
61
|
/**
|
|
59
62
|
* An in-memory cookie store that follows the browser rules of [RFC 6265](https://datatracker.ietf.org/doc/html/rfc6265).
|
|
@@ -61,22 +64,22 @@ export interface StoredCookie {
|
|
|
61
64
|
* @see {@link https://orpc.dev/docs/plugins/cookie-jar#custom-jar | Cookie Jar Plugin - Custom Jar}
|
|
62
65
|
*/
|
|
63
66
|
export declare class CookieJar {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
67
|
+
/**
|
|
68
|
+
* Keyed by domain, path, and name. Map order is creation order: replacing a cookie keeps its position.
|
|
69
|
+
*/
|
|
70
|
+
private readonly cookies;
|
|
71
|
+
/**
|
|
72
|
+
* Stores a `Set-Cookie` header value received from `url`.
|
|
73
|
+
* Cookies a browser would reject are ignored, and an expired cookie deletes the one it replaces.
|
|
74
|
+
*/
|
|
75
|
+
setCookie(setCookie: string, url: string): void;
|
|
76
|
+
/**
|
|
77
|
+
* Returns the cookies a browser would send to `url`, longer paths first, then oldest first.
|
|
78
|
+
* Without `url`, returns every stored cookie that has not expired.
|
|
79
|
+
*/
|
|
80
|
+
getCookies(url?: string): StoredCookie[];
|
|
81
|
+
/**
|
|
82
|
+
* Removes every stored cookie.
|
|
83
|
+
*/
|
|
84
|
+
clear(): void;
|
|
85
|
+
}
|
|
@@ -1,268 +1,231 @@
|
|
|
1
|
-
import { matchesHttpPathPrefix, toArray, value } from
|
|
1
|
+
import { matchesHttpPathPrefix, toArray, value } from "@orpc/shared";
|
|
2
|
+
// src/plugins/cookie-jar.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
4
|
+
* Stores the cookies servers set and sends them back on later requests, like a browser,
|
|
5
|
+
* for server-side rendering, scripts, and tests.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* **Warning**: On a server, do not share one link between users: create a client per request, or pick a jar per request.
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.dev/docs/plugins/cookie-jar | Cookie Jar Plugin}
|
|
11
|
+
*/
|
|
12
|
+
var CookieJarLinkPlugin = class {
|
|
13
|
+
origin;
|
|
14
|
+
jar;
|
|
15
|
+
name = "~cookie-jar";
|
|
16
|
+
/**
|
|
17
|
+
* Runs per call, before batching, so each call gets the cookies it would get on its own.
|
|
18
|
+
*/
|
|
19
|
+
before = ["~batch"];
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.origin = options.origin;
|
|
22
|
+
this.jar = options.jar ?? new CookieJar();
|
|
23
|
+
}
|
|
24
|
+
init(options) {
|
|
25
|
+
const transportInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
26
|
+
const { path, request } = interceptorOptions;
|
|
27
|
+
const origin = await value(this.origin, interceptorOptions, path);
|
|
28
|
+
if (origin === void 0 || !/^[a-z][\d+\-.a-z]*:/i.test(origin)) return next();
|
|
29
|
+
const jar = value(this.jar, interceptorOptions);
|
|
30
|
+
const url = `${origin.replace(/\/$/, "")}${request.url}`;
|
|
31
|
+
const cookie = mergeCookieHeader(request.headers.cookie, jar.getCookies(url));
|
|
32
|
+
const response = await next(cookie === void 0 ? interceptorOptions : {
|
|
33
|
+
...interceptorOptions,
|
|
34
|
+
request: {
|
|
35
|
+
...request,
|
|
36
|
+
headers: {
|
|
37
|
+
...request.headers,
|
|
38
|
+
cookie
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
for (const setCookie of toArray(response.headers["set-cookie"])) jar.setCookie(setCookie, url);
|
|
43
|
+
return response;
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
...options,
|
|
47
|
+
transportInterceptors: [...toArray(options.transportInterceptors), transportInterceptor]
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
51
|
/**
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (url === undefined) {
|
|
107
|
-
return cookies;
|
|
108
|
-
}
|
|
109
|
-
const target = new URL(url);
|
|
110
|
-
const secureUrl = isSecureUrl(target);
|
|
111
|
-
return (cookies
|
|
112
|
-
.filter((cookie) => (cookie.hostOnly
|
|
113
|
-
? target.hostname === cookie.domain
|
|
114
|
-
: domainMatch(target.hostname, cookie.domain)) &&
|
|
115
|
-
matchesHttpPathPrefix(target.pathname, cookie.path) &&
|
|
116
|
-
(!cookie.secure || secureUrl))
|
|
117
|
-
// oxlint-disable-next-line unicorn/no-array-sort
|
|
118
|
-
.sort((a, b) => b.path.length - a.path.length));
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Removes every stored cookie.
|
|
122
|
-
*/
|
|
123
|
-
clear() {
|
|
124
|
-
this.cookies.clear();
|
|
125
|
-
}
|
|
126
|
-
}
|
|
52
|
+
* An in-memory cookie store that follows the browser rules of [RFC 6265](https://datatracker.ietf.org/doc/html/rfc6265).
|
|
53
|
+
*
|
|
54
|
+
* @see {@link https://orpc.dev/docs/plugins/cookie-jar#custom-jar | Cookie Jar Plugin - Custom Jar}
|
|
55
|
+
*/
|
|
56
|
+
var CookieJar = class {
|
|
57
|
+
/**
|
|
58
|
+
* Keyed by domain, path, and name. Map order is creation order: replacing a cookie keeps its position.
|
|
59
|
+
*/
|
|
60
|
+
cookies = /* @__PURE__ */ new Map();
|
|
61
|
+
/**
|
|
62
|
+
* Stores a `Set-Cookie` header value received from `url`.
|
|
63
|
+
* Cookies a browser would reject are ignored, and an expired cookie deletes the one it replaces.
|
|
64
|
+
*/
|
|
65
|
+
setCookie(setCookie, url) {
|
|
66
|
+
const parsed = parseSetCookie(setCookie);
|
|
67
|
+
if (!parsed) return;
|
|
68
|
+
const target = new URL(url);
|
|
69
|
+
const secureUrl = isSecureUrl(target);
|
|
70
|
+
const hostOnly = parsed.domain === void 0;
|
|
71
|
+
const domain = parsed.domain ?? target.hostname;
|
|
72
|
+
const path = parsed.path ?? defaultPath(target.pathname);
|
|
73
|
+
if (!domainMatch(target.hostname, domain) || /^__(?:secure|host)-/i.test(parsed.name) && !parsed.secure || /^__host-/i.test(parsed.name) && (!hostOnly || parsed.path !== "/") || !secureUrl && (parsed.secure || [...this.cookies.values()].some((cookie) => cookie.secure && cookie.name === parsed.name && (domainMatch(domain, cookie.domain) || domainMatch(cookie.domain, domain)) && matchesHttpPathPrefix(path, cookie.path)))) return;
|
|
74
|
+
const key = `${domain};${path};${parsed.name}`;
|
|
75
|
+
if ((parsed.expires ?? Number.POSITIVE_INFINITY) <= Date.now()) {
|
|
76
|
+
this.cookies.delete(key);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
this.cookies.set(key, {
|
|
80
|
+
...parsed,
|
|
81
|
+
domain,
|
|
82
|
+
hostOnly,
|
|
83
|
+
path
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns the cookies a browser would send to `url`, longer paths first, then oldest first.
|
|
88
|
+
* Without `url`, returns every stored cookie that has not expired.
|
|
89
|
+
*/
|
|
90
|
+
getCookies(url) {
|
|
91
|
+
const now = Date.now();
|
|
92
|
+
for (const [key, cookie] of this.cookies) if ((cookie.expires ?? Number.POSITIVE_INFINITY) <= now) this.cookies.delete(key);
|
|
93
|
+
const cookies = [...this.cookies.values()].map((cookie) => ({ ...cookie }));
|
|
94
|
+
if (url === void 0) return cookies;
|
|
95
|
+
const target = new URL(url);
|
|
96
|
+
const secureUrl = isSecureUrl(target);
|
|
97
|
+
return cookies.filter((cookie) => (cookie.hostOnly ? target.hostname === cookie.domain : domainMatch(target.hostname, cookie.domain)) && matchesHttpPathPrefix(target.pathname, cookie.path) && (!cookie.secure || secureUrl)).sort((a, b) => b.path.length - a.path.length);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Removes every stored cookie.
|
|
101
|
+
*/
|
|
102
|
+
clear() {
|
|
103
|
+
this.cookies.clear();
|
|
104
|
+
}
|
|
105
|
+
};
|
|
127
106
|
/**
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
107
|
+
* Appends jar cookies to an existing `cookie` header, skipping names it already has.
|
|
108
|
+
* Returns `undefined` when the jar adds nothing.
|
|
109
|
+
*/
|
|
131
110
|
function mergeCookieHeader(header, cookies) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const explicitNames = new Set(explicit.map((pair) => splitCookiePair(pair)[0]));
|
|
137
|
-
const pairs = cookies
|
|
138
|
-
.filter((cookie) => !explicitNames.has(cookie.name))
|
|
139
|
-
.map((cookie) => (cookie.name ? `${cookie.name}=${cookie.value}` : cookie.value));
|
|
140
|
-
return pairs.length ? [...explicit, ...pairs].join('; ') : undefined;
|
|
111
|
+
const explicit = toArray(header).flatMap((value) => value.split(";")).map((pair) => pair.trim()).filter(Boolean);
|
|
112
|
+
const explicitNames = new Set(explicit.map((pair) => splitCookiePair(pair)[0]));
|
|
113
|
+
const pairs = cookies.filter((cookie) => !explicitNames.has(cookie.name)).map((cookie) => cookie.name ? `${cookie.name}=${cookie.value}` : cookie.value);
|
|
114
|
+
return pairs.length ? [...explicit, ...pairs].join("; ") : void 0;
|
|
141
115
|
}
|
|
142
116
|
/**
|
|
143
|
-
|
|
144
|
-
|
|
117
|
+
* Splits a cookie pair at the first `=`. A pair without `=` has an empty name, as in browsers and RFC 6265bis.
|
|
118
|
+
*/
|
|
145
119
|
function splitCookiePair(pair) {
|
|
146
|
-
|
|
147
|
-
|
|
120
|
+
const index = pair.indexOf("=");
|
|
121
|
+
return [index === -1 ? "" : pair.slice(0, index).trim(), pair.slice(index + 1).trim()];
|
|
148
122
|
}
|
|
149
123
|
/**
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
124
|
+
* Parses a `Set-Cookie` header value following [RFC 6265 Section 5.2](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2),
|
|
125
|
+
* with the [RFC 6265bis](https://datatracker.ietf.org/doc/draft-ietf-httpbis-rfc6265bis/) changes browsers use, noted inline.
|
|
126
|
+
*/
|
|
153
127
|
function parseSetCookie(setCookie) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
case 'path': {
|
|
192
|
-
path = attributeValue.startsWith('/') ? attributeValue : undefined;
|
|
193
|
-
break;
|
|
194
|
-
}
|
|
195
|
-
case 'secure': {
|
|
196
|
-
secure = true;
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
// Max-Age takes precedence over Expires
|
|
202
|
-
return { name, value, domain, path, secure, expires: maxAgeExpires ?? expires };
|
|
128
|
+
if (/[\x00-\x08\x0A-\x1F\x7F]/.test(setCookie)) return;
|
|
129
|
+
const [pair = "", ...attributes] = setCookie.split(";");
|
|
130
|
+
const [name, value] = splitCookiePair(pair);
|
|
131
|
+
if (!name && !value) return;
|
|
132
|
+
let domain;
|
|
133
|
+
let path;
|
|
134
|
+
let secure = false;
|
|
135
|
+
let expires;
|
|
136
|
+
let maxAgeExpires;
|
|
137
|
+
for (const attribute of attributes) {
|
|
138
|
+
const index = attribute.indexOf("=");
|
|
139
|
+
const attributeName = (index === -1 ? attribute : attribute.slice(0, index)).trim().toLowerCase();
|
|
140
|
+
const attributeValue = index === -1 ? "" : attribute.slice(index + 1).trim();
|
|
141
|
+
switch (attributeName) {
|
|
142
|
+
case "expires":
|
|
143
|
+
expires = parseCookieDate(attributeValue) ?? expires;
|
|
144
|
+
break;
|
|
145
|
+
case "max-age":
|
|
146
|
+
if (/^-?\d+$/.test(attributeValue)) maxAgeExpires = Date.now() + Number(attributeValue) * 1e3;
|
|
147
|
+
break;
|
|
148
|
+
case "domain":
|
|
149
|
+
domain = attributeValue.replace(/^\./, "").toLowerCase() || void 0;
|
|
150
|
+
break;
|
|
151
|
+
case "path":
|
|
152
|
+
path = attributeValue.startsWith("/") ? attributeValue : void 0;
|
|
153
|
+
break;
|
|
154
|
+
case "secure": secure = true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
name,
|
|
159
|
+
value,
|
|
160
|
+
domain,
|
|
161
|
+
path,
|
|
162
|
+
secure,
|
|
163
|
+
expires: maxAgeExpires ?? expires
|
|
164
|
+
};
|
|
203
165
|
}
|
|
204
|
-
const MONTHS = [
|
|
166
|
+
const MONTHS = [
|
|
167
|
+
"jan",
|
|
168
|
+
"feb",
|
|
169
|
+
"mar",
|
|
170
|
+
"apr",
|
|
171
|
+
"may",
|
|
172
|
+
"jun",
|
|
173
|
+
"jul",
|
|
174
|
+
"aug",
|
|
175
|
+
"sep",
|
|
176
|
+
"oct",
|
|
177
|
+
"nov",
|
|
178
|
+
"dec"
|
|
179
|
+
];
|
|
205
180
|
/**
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
181
|
+
* Parses a cookie date following [RFC 6265 Section 5.1.1](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.1),
|
|
182
|
+
* always in UTC. Returns `undefined` when a part is missing or out of range, so the attribute is ignored.
|
|
183
|
+
*/
|
|
209
184
|
function parseCookieDate(value) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const [hour, minute, second] = time;
|
|
236
|
-
if (year < 1601 || hour > 23 || minute > 59 || second > 59) {
|
|
237
|
-
return undefined;
|
|
238
|
-
}
|
|
239
|
-
const date = Date.UTC(year, month, day, hour, minute, second);
|
|
240
|
-
// Rejects days the month does not have, such as 0 or 31 Feb
|
|
241
|
-
return new Date(date).getUTCDate() === day ? date : undefined;
|
|
185
|
+
let time;
|
|
186
|
+
let day;
|
|
187
|
+
let month;
|
|
188
|
+
let year;
|
|
189
|
+
for (const token of value.split(/[\t\x20-\x2F\x3B-\x40\x5B-\x60\x7B-\x7E]+/)) {
|
|
190
|
+
const digits = /^\d*/.exec(token)[0];
|
|
191
|
+
const timeMatch = /^(\d{1,2}):(\d{1,2}):(\d{1,2})(?:\D|$)/.exec(token);
|
|
192
|
+
const monthIndex = MONTHS.indexOf(token.slice(0, 3).toLowerCase());
|
|
193
|
+
if (time === void 0 && timeMatch) time = [
|
|
194
|
+
Number(timeMatch[1]),
|
|
195
|
+
Number(timeMatch[2]),
|
|
196
|
+
Number(timeMatch[3])
|
|
197
|
+
];
|
|
198
|
+
else if (day === void 0 && digits.length >= 1 && digits.length <= 2) day = Number(digits);
|
|
199
|
+
else if (month === void 0 && monthIndex !== -1) month = monthIndex;
|
|
200
|
+
else if (year === void 0 && digits.length >= 2 && digits.length <= 4) {
|
|
201
|
+
year = Number(digits);
|
|
202
|
+
year += year < 70 ? 2e3 : year < 100 ? 1900 : 0;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (time === void 0 || day === void 0 || month === void 0 || year === void 0) return;
|
|
206
|
+
const [hour, minute, second] = time;
|
|
207
|
+
if (year < 1601 || hour > 23 || minute > 59 || second > 59) return;
|
|
208
|
+
const date = Date.UTC(year, month, day, hour, minute, second);
|
|
209
|
+
return new Date(date).getUTCDate() === day ? date : void 0;
|
|
242
210
|
}
|
|
243
211
|
/**
|
|
244
|
-
|
|
245
|
-
|
|
212
|
+
* Browsers also treat loopback hosts as secure, so `Secure` cookies work against a local server.
|
|
213
|
+
*/
|
|
246
214
|
function isSecureUrl(url) {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
url.protocol === 'wss:' ||
|
|
250
|
-
host === 'localhost' ||
|
|
251
|
-
host.endsWith('.localhost') ||
|
|
252
|
-
host === '[::1]' ||
|
|
253
|
-
/^127(?:\.\d{1,3}){3}$/.test(host));
|
|
215
|
+
const host = url.hostname;
|
|
216
|
+
return url.protocol === "https:" || url.protocol === "wss:" || host === "localhost" || host.endsWith(".localhost") || host === "[::1]" || /^127(?:\.\d{1,3}){3}$/.test(host);
|
|
254
217
|
}
|
|
255
218
|
/**
|
|
256
|
-
|
|
257
|
-
|
|
219
|
+
* [RFC 6265 Section 5.1.3](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3)
|
|
220
|
+
*/
|
|
258
221
|
function domainMatch(host, domain) {
|
|
259
|
-
|
|
260
|
-
return host === domain || (host.endsWith(`.${domain}`) && !/^\d{1,3}(?:\.\d{1,3}){3}$/.test(host));
|
|
222
|
+
return host === domain || host.endsWith(`.${domain}`) && !/^\d{1,3}(?:\.\d{1,3}){3}$/.test(host);
|
|
261
223
|
}
|
|
262
224
|
/**
|
|
263
|
-
|
|
264
|
-
|
|
225
|
+
* [RFC 6265 Section 5.1.4](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4)
|
|
226
|
+
*/
|
|
265
227
|
function defaultPath(path) {
|
|
266
|
-
|
|
267
|
-
|
|
228
|
+
const index = path.lastIndexOf("/");
|
|
229
|
+
return index <= 0 ? "/" : path.slice(0, index);
|
|
268
230
|
}
|
|
231
|
+
export { CookieJar, CookieJarLinkPlugin };
|