@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.
Files changed (75) hide show
  1. package/dist/adapters/fetch/index.d.ts +3 -2
  2. package/dist/adapters/fetch/index.js +2 -2
  3. package/dist/adapters/fetch/rpc-link.d.ts +8 -8
  4. package/dist/adapters/fetch/rpc-link.js +16 -13
  5. package/dist/adapters/fetch/transport.d.ts +29 -31
  6. package/dist/adapters/fetch/transport.js +49 -63
  7. package/dist/adapters/message-port/index.d.ts +4 -3
  8. package/dist/adapters/message-port/index.js +3 -3
  9. package/dist/adapters/message-port/message-port.d.ts +13 -13
  10. package/dist/adapters/message-port/message-port.js +35 -53
  11. package/dist/adapters/message-port/rpc-link.d.ts +8 -8
  12. package/dist/adapters/message-port/rpc-link.js +17 -14
  13. package/dist/adapters/message-port/transport.d.ts +31 -34
  14. package/dist/adapters/message-port/transport.js +38 -52
  15. package/dist/adapters/standard/codec.d.ts +10 -10
  16. package/dist/adapters/standard/index.d.ts +6 -5
  17. package/dist/adapters/standard/index.js +2 -4
  18. package/dist/adapters/standard/link.d.ts +31 -31
  19. package/dist/adapters/standard/link.js +101 -85
  20. package/dist/adapters/standard/plugin.d.ts +25 -31
  21. package/dist/adapters/standard/plugin.js +14 -16
  22. package/dist/adapters/standard/rpc-link-codec.d.ts +53 -60
  23. package/dist/adapters/standard/rpc-link-codec.js +98 -102
  24. package/dist/adapters/standard/transport.d.ts +7 -7
  25. package/dist/adapters/websocket/index.d.ts +3 -2
  26. package/dist/adapters/websocket/index.js +2 -2
  27. package/dist/adapters/websocket/rpc-link.d.ts +8 -8
  28. package/dist/adapters/websocket/rpc-link.js +16 -13
  29. package/dist/adapters/websocket/transport.d.ts +101 -100
  30. package/dist/adapters/websocket/transport.js +102 -130
  31. package/dist/async-iterator-object.d.ts +2 -2
  32. package/dist/async-iterator-object.js +27 -28
  33. package/dist/client-safe.d.ts +4 -6
  34. package/dist/client-safe.js +29 -34
  35. package/dist/client.d.ts +24 -26
  36. package/dist/client.js +35 -37
  37. package/dist/consts.d.ts +1 -1
  38. package/dist/consts.js +18 -41
  39. package/dist/dynamic-link.d.ts +6 -6
  40. package/dist/dynamic-link.js +16 -16
  41. package/dist/error-utils.d.ts +3 -4
  42. package/dist/error-utils.js +47 -51
  43. package/dist/error.d.ts +79 -79
  44. package/dist/error.js +154 -166
  45. package/dist/index.d.ts +14 -53
  46. package/dist/index.js +12 -40
  47. package/dist/plugins/batch.d.ts +122 -155
  48. package/dist/plugins/batch.js +320 -391
  49. package/dist/plugins/cookie-jar.d.ts +64 -61
  50. package/dist/plugins/cookie-jar.js +208 -245
  51. package/dist/plugins/dedupe.d.ts +49 -53
  52. package/dist/plugins/dedupe.js +168 -211
  53. package/dist/plugins/index.d.ts +9 -28
  54. package/dist/plugins/index.js +8 -23
  55. package/dist/plugins/request-compression.d.ts +38 -35
  56. package/dist/plugins/request-compression.js +131 -177
  57. package/dist/plugins/response-compression.d.ts +21 -18
  58. package/dist/plugins/response-compression.js +64 -88
  59. package/dist/plugins/retry-after.d.ts +31 -38
  60. package/dist/plugins/retry-after.js +60 -85
  61. package/dist/plugins/retry.d.ts +55 -55
  62. package/dist/plugins/retry.js +118 -137
  63. package/dist/plugins/timeout.d.ts +19 -17
  64. package/dist/plugins/timeout.js +41 -36
  65. package/dist/rpc-json-serializer.d.ts +84 -84
  66. package/dist/rpc-json-serializer.js +338 -383
  67. package/dist/rpc-serializer.d.ts +20 -21
  68. package/dist/rpc-serializer.js +97 -125
  69. package/dist/types.d.ts +19 -31
  70. package/dist/utils.d.ts +18 -29
  71. package/dist/utils.js +63 -47
  72. package/package.json +5 -5
  73. package/dist/adapters/standard/codec.js +0 -1
  74. package/dist/adapters/standard/transport.js +0 -1
  75. package/dist/types.js +0 -1
@@ -1,21 +1,24 @@
1
- import type { Value } from '@orpc/shared';
2
- import type { FetchLinkTransportOptions } from '../adapters/fetch/index.ts';
3
- import type { StandardLinkOptions, StandardLinkPlugin, StandardLinkTransportInterceptorOptions } from '../adapters/standard/index.ts';
4
- import type { ClientContext } from '../types.ts';
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
- * The link's `origin`, used to match cookies by host and scheme.
8
- * The plugin does nothing when it resolves to `undefined` or a relative origin, as in a browser.
9
- *
10
- * @example 'https://api.example.com'
11
- */
12
- origin: FetchLinkTransportOptions<T>['origin'];
13
- /**
14
- * Where cookies are stored. Pass a function to pick a jar per request, e.g. from the client context.
15
- *
16
- * @default a new `CookieJar` per plugin
17
- */
18
- jar?: Value<CookieJar, [options: StandardLinkTransportInterceptorOptions<T>]> | undefined;
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
- private readonly origin;
31
- private readonly jar;
32
- name: string;
33
- /**
34
- * Runs per call, before batching, so each call gets the cookies it would get on its own.
35
- */
36
- before: string[];
37
- constructor(options: NoInfer<CookieJarLinkPluginOptions<T>>);
38
- init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
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
- name: string;
42
- value: string;
43
- /**
44
- * The `Domain` attribute, or else the host that set the cookie.
45
- */
46
- domain: string;
47
- /**
48
- * True when the cookie has no `Domain` attribute, so it is sent to `domain` only, not its subdomains.
49
- */
50
- hostOnly: boolean;
51
- path: string;
52
- secure: boolean;
53
- /**
54
- * Expiry time in milliseconds since the Unix epoch, `undefined` for a session cookie that lives as long as the jar.
55
- */
56
- expires: number | undefined;
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
- * Keyed by domain, path, and name. Map order is creation order: replacing a cookie keeps its position.
66
- */
67
- private readonly cookies;
68
- /**
69
- * Stores a `Set-Cookie` header value received from `url`.
70
- * Cookies a browser would reject are ignored, and an expired cookie deletes the one it replaces.
71
- */
72
- setCookie(setCookie: string, url: string): void;
73
- /**
74
- * Returns the cookies a browser would send to `url`, longer paths first, then oldest first.
75
- * Without `url`, returns every stored cookie that has not expired.
76
- */
77
- getCookies(url?: string): StoredCookie[];
78
- /**
79
- * Removes every stored cookie.
80
- */
81
- clear(): void;
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 '@orpc/shared';
1
+ import { matchesHttpPathPrefix, toArray, value } from "@orpc/shared";
2
+ // src/plugins/cookie-jar.ts
2
3
  /**
3
- * Stores the cookies servers set and sends them back on later requests, like a browser,
4
- * for server-side rendering, scripts, and tests.
5
- *
6
- * @remarks
7
- * **Warning**: On a server, do not share one link between users: create a client per request, or pick a jar per request.
8
- *
9
- * @see {@link https://orpc.dev/docs/plugins/cookie-jar | Cookie Jar Plugin}
10
- */
11
- export class CookieJarLinkPlugin {
12
- origin;
13
- jar;
14
- name = '~cookie-jar';
15
- /**
16
- * Runs per call, before batching, so each call gets the cookies it would get on its own.
17
- */
18
- before = ['~batch'];
19
- constructor(options) {
20
- this.origin = options.origin;
21
- this.jar = options.jar ?? new CookieJar();
22
- }
23
- init(options) {
24
- const transportInterceptor = async ({ next, ...interceptorOptions }) => {
25
- const { path, request } = interceptorOptions;
26
- const origin = await value(this.origin, interceptorOptions, path);
27
- // No absolute origin, as in a browser
28
- if (origin === undefined || !/^[a-z][\d+\-.a-z]*:/i.test(origin)) {
29
- return next();
30
- }
31
- const jar = value(this.jar, interceptorOptions);
32
- const url = `${origin.replace(/\/$/, '')}${request.url}`;
33
- const cookie = mergeCookieHeader(request.headers.cookie, jar.getCookies(url));
34
- const response = await next(cookie === undefined
35
- ? interceptorOptions
36
- : {
37
- ...interceptorOptions,
38
- request: { ...request, headers: { ...request.headers, cookie } },
39
- });
40
- for (const setCookie of toArray(response.headers['set-cookie'])) {
41
- jar.setCookie(setCookie, url);
42
- }
43
- return response;
44
- };
45
- return {
46
- ...options,
47
- transportInterceptors: [...toArray(options.transportInterceptors), transportInterceptor],
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
- * 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
- export class CookieJar {
57
- /**
58
- * Keyed by domain, path, and name. Map order is creation order: replacing a cookie keeps its position.
59
- */
60
- cookies = 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) {
68
- return;
69
- }
70
- const target = new URL(url);
71
- const secureUrl = isSecureUrl(target);
72
- const hostOnly = parsed.domain === undefined;
73
- const domain = parsed.domain ?? target.hostname;
74
- const path = parsed.path ?? defaultPath(target.pathname);
75
- if (!domainMatch(target.hostname, domain) ||
76
- (/^__(?:secure|host)-/i.test(parsed.name) && !parsed.secure) ||
77
- (/^__host-/i.test(parsed.name) && (!hostOnly || parsed.path !== '/')) ||
78
- // Insecure origins can neither set nor overwrite secure cookies, see "leave secure cookies alone" in RFC 6265bis
79
- (!secureUrl &&
80
- (parsed.secure ||
81
- [...this.cookies.values()].some((cookie) => cookie.secure &&
82
- cookie.name === parsed.name &&
83
- (domainMatch(domain, cookie.domain) || domainMatch(cookie.domain, domain)) &&
84
- matchesHttpPathPrefix(path, cookie.path))))) {
85
- return;
86
- }
87
- const key = `${domain};${path};${parsed.name}`;
88
- if ((parsed.expires ?? Number.POSITIVE_INFINITY) <= Date.now()) {
89
- this.cookies.delete(key);
90
- return;
91
- }
92
- this.cookies.set(key, { ...parsed, domain, hostOnly, path });
93
- }
94
- /**
95
- * Returns the cookies a browser would send to `url`, longer paths first, then oldest first.
96
- * Without `url`, returns every stored cookie that has not expired.
97
- */
98
- getCookies(url) {
99
- const now = Date.now();
100
- for (const [key, cookie] of this.cookies) {
101
- if ((cookie.expires ?? Number.POSITIVE_INFINITY) <= now) {
102
- this.cookies.delete(key);
103
- }
104
- }
105
- const cookies = [...this.cookies.values()].map((cookie) => ({ ...cookie }));
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
- * Appends jar cookies to an existing `cookie` header, skipping names it already has.
129
- * Returns `undefined` when the jar adds nothing.
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
- const explicit = toArray(header)
133
- .flatMap((value) => value.split(';'))
134
- .map((pair) => pair.trim())
135
- .filter(Boolean);
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
- * Splits a cookie pair at the first `=`. A pair without `=` has an empty name, as in browsers and RFC 6265bis.
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
- const index = pair.indexOf('=');
147
- return [index === -1 ? '' : pair.slice(0, index).trim(), pair.slice(index + 1).trim()];
120
+ const index = pair.indexOf("=");
121
+ return [index === -1 ? "" : pair.slice(0, index).trim(), pair.slice(index + 1).trim()];
148
122
  }
149
123
  /**
150
- * Parses a `Set-Cookie` header value following [RFC 6265 Section 5.2](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2),
151
- * with the [RFC 6265bis](https://datatracker.ietf.org/doc/draft-ietf-httpbis-rfc6265bis/) changes browsers use, noted inline.
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
- // Control characters other than tab make the whole cookie invalid
155
- // oxlint-disable-next-line no-control-regex
156
- if (/[\x00-\x08\x0A-\x1F\x7F]/.test(setCookie)) {
157
- return undefined;
158
- }
159
- const [pair = '', ...attributes] = setCookie.split(';');
160
- const [name, value] = splitCookiePair(pair);
161
- if (!name && !value) {
162
- return undefined;
163
- }
164
- let domain;
165
- let path;
166
- let secure = false;
167
- let expires;
168
- let maxAgeExpires;
169
- // A repeated attribute overrides the earlier ones
170
- for (const attribute of attributes) {
171
- const index = attribute.indexOf('=');
172
- const attributeName = (index === -1 ? attribute : attribute.slice(0, index))
173
- .trim()
174
- .toLowerCase();
175
- const attributeValue = index === -1 ? '' : attribute.slice(index + 1).trim();
176
- switch (attributeName) {
177
- case 'expires': {
178
- expires = parseCookieDate(attributeValue) ?? expires;
179
- break;
180
- }
181
- case 'max-age': {
182
- if (/^-?\d+$/.test(attributeValue)) {
183
- maxAgeExpires = Date.now() + Number(attributeValue) * 1000;
184
- }
185
- break;
186
- }
187
- case 'domain': {
188
- domain = attributeValue.replace(/^\./, '').toLowerCase() || undefined;
189
- break;
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 = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
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
- * Parses a cookie date following [RFC 6265 Section 5.1.1](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.1),
207
- * always in UTC. Returns `undefined` when a part is missing or out of range, so the attribute is ignored.
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
- let time;
211
- let day;
212
- let month;
213
- let year;
214
- for (const token of value.split(/[\t\x20-\x2F\x3B-\x40\x5B-\x60\x7B-\x7E]+/)) {
215
- const digits = /^\d*/.exec(token)[0];
216
- const timeMatch = /^(\d{1,2}):(\d{1,2}):(\d{1,2})(?:\D|$)/.exec(token);
217
- const monthIndex = MONTHS.indexOf(token.slice(0, 3).toLowerCase());
218
- if (time === undefined && timeMatch) {
219
- time = [Number(timeMatch[1]), Number(timeMatch[2]), Number(timeMatch[3])];
220
- }
221
- else if (day === undefined && digits.length >= 1 && digits.length <= 2) {
222
- day = Number(digits);
223
- }
224
- else if (month === undefined && monthIndex !== -1) {
225
- month = monthIndex;
226
- }
227
- else if (year === undefined && digits.length >= 2 && digits.length <= 4) {
228
- year = Number(digits);
229
- year += year < 70 ? 2000 : year < 100 ? 1900 : 0;
230
- }
231
- }
232
- if (time === undefined || day === undefined || month === undefined || year === undefined) {
233
- return undefined;
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
- * Browsers also treat loopback hosts as secure, so `Secure` cookies work against a local server.
245
- */
212
+ * Browsers also treat loopback hosts as secure, so `Secure` cookies work against a local server.
213
+ */
246
214
  function isSecureUrl(url) {
247
- const host = url.hostname;
248
- return (url.protocol === 'https:' ||
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
- * [RFC 6265 Section 5.1.3](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3)
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
- // IPv6 hosts have no dots, so only IPv4 addresses need excluding
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
- * [RFC 6265 Section 5.1.4](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4)
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
- const index = path.lastIndexOf('/');
267
- return index <= 0 ? '/' : path.slice(0, index);
228
+ const index = path.lastIndexOf("/");
229
+ return index <= 0 ? "/" : path.slice(0, index);
268
230
  }
231
+ export { CookieJar, CookieJarLinkPlugin };