@gjsify/fetch 0.0.2

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 (58) hide show
  1. package/README.md +9 -0
  2. package/lib/cjs/body.js +284 -0
  3. package/lib/cjs/errors/abort-error.js +28 -0
  4. package/lib/cjs/errors/base.js +36 -0
  5. package/lib/cjs/errors/fetch-error.js +40 -0
  6. package/lib/cjs/headers.js +231 -0
  7. package/lib/cjs/index.js +246 -0
  8. package/lib/cjs/request.js +306 -0
  9. package/lib/cjs/response.js +162 -0
  10. package/lib/cjs/types/index.js +17 -0
  11. package/lib/cjs/types/system-error.js +16 -0
  12. package/lib/cjs/utils/blob-from.js +124 -0
  13. package/lib/cjs/utils/get-search.js +30 -0
  14. package/lib/cjs/utils/is-redirect.js +26 -0
  15. package/lib/cjs/utils/is.js +47 -0
  16. package/lib/cjs/utils/multipart-parser.js +372 -0
  17. package/lib/cjs/utils/referrer.js +172 -0
  18. package/lib/esm/body.js +255 -0
  19. package/lib/esm/errors/abort-error.js +9 -0
  20. package/lib/esm/errors/base.js +17 -0
  21. package/lib/esm/errors/fetch-error.js +21 -0
  22. package/lib/esm/headers.js +202 -0
  23. package/lib/esm/index.js +224 -0
  24. package/lib/esm/request.js +281 -0
  25. package/lib/esm/response.js +133 -0
  26. package/lib/esm/types/index.js +1 -0
  27. package/lib/esm/types/system-error.js +1 -0
  28. package/lib/esm/utils/blob-from.js +101 -0
  29. package/lib/esm/utils/get-search.js +11 -0
  30. package/lib/esm/utils/is-redirect.js +7 -0
  31. package/lib/esm/utils/is.js +28 -0
  32. package/lib/esm/utils/multipart-parser.js +353 -0
  33. package/lib/esm/utils/referrer.js +153 -0
  34. package/package.json +53 -0
  35. package/src/body.ts +415 -0
  36. package/src/errors/abort-error.ts +10 -0
  37. package/src/errors/base.ts +20 -0
  38. package/src/errors/fetch-error.ts +26 -0
  39. package/src/headers.ts +279 -0
  40. package/src/index.spec.ts +13 -0
  41. package/src/index.ts +367 -0
  42. package/src/request.ts +396 -0
  43. package/src/response.ts +197 -0
  44. package/src/test.mts +6 -0
  45. package/src/types/index.ts +1 -0
  46. package/src/types/system-error.ts +11 -0
  47. package/src/utils/blob-from.ts +168 -0
  48. package/src/utils/get-search.ts +9 -0
  49. package/src/utils/is-redirect.ts +11 -0
  50. package/src/utils/is.ts +88 -0
  51. package/src/utils/multipart-parser.ts +448 -0
  52. package/src/utils/referrer.ts +350 -0
  53. package/test.gjs.js +34758 -0
  54. package/test.gjs.mjs +53177 -0
  55. package/test.node.js +1226 -0
  56. package/test.node.mjs +6294 -0
  57. package/tsconfig.json +19 -0
  58. package/tsconfig.types.json +8 -0
@@ -0,0 +1,372 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var multipart_parser_exports = {};
19
+ __export(multipart_parser_exports, {
20
+ toFormData: () => toFormData
21
+ });
22
+ module.exports = __toCommonJS(multipart_parser_exports);
23
+ var import_blob_from = require("./blob-from.js");
24
+ var import_esm_min = require("formdata-polyfill/esm.min.js");
25
+ var S = /* @__PURE__ */ ((S2) => {
26
+ S2[S2["START_BOUNDARY"] = 0] = "START_BOUNDARY";
27
+ S2[S2["HEADER_FIELD_START"] = 1] = "HEADER_FIELD_START";
28
+ S2[S2["HEADER_FIELD"] = 2] = "HEADER_FIELD";
29
+ S2[S2["HEADER_VALUE_START"] = 3] = "HEADER_VALUE_START";
30
+ S2[S2["HEADER_VALUE"] = 4] = "HEADER_VALUE";
31
+ S2[S2["HEADER_VALUE_ALMOST_DONE"] = 5] = "HEADER_VALUE_ALMOST_DONE";
32
+ S2[S2["HEADERS_ALMOST_DONE"] = 6] = "HEADERS_ALMOST_DONE";
33
+ S2[S2["PART_DATA_START"] = 7] = "PART_DATA_START";
34
+ S2[S2["PART_DATA"] = 8] = "PART_DATA";
35
+ S2[S2["END"] = 9] = "END";
36
+ return S2;
37
+ })(S || {});
38
+ let f = 1;
39
+ const F = {
40
+ PART_BOUNDARY: f,
41
+ LAST_BOUNDARY: f *= 2
42
+ };
43
+ const LF = 10;
44
+ const CR = 13;
45
+ const SPACE = 32;
46
+ const HYPHEN = 45;
47
+ const COLON = 58;
48
+ const A = 97;
49
+ const Z = 122;
50
+ const lower = (c) => c | 32;
51
+ const noop = (...args) => {
52
+ };
53
+ class MultipartParser {
54
+ index = 0;
55
+ flags = 0;
56
+ boundary;
57
+ lookbehind;
58
+ state = 0 /* START_BOUNDARY */;
59
+ onHeaderEnd = noop;
60
+ onHeaderField = noop;
61
+ onHeadersEnd = noop;
62
+ onHeaderValue = noop;
63
+ onPartBegin = noop;
64
+ onPartData = noop;
65
+ onPartEnd = noop;
66
+ boundaryChars = {};
67
+ /**
68
+ * @param boundary
69
+ */
70
+ constructor(boundary) {
71
+ boundary = "\r\n--" + boundary;
72
+ const ui8a = new Uint8Array(boundary.length);
73
+ for (let i = 0; i < boundary.length; i++) {
74
+ ui8a[i] = boundary.charCodeAt(i);
75
+ this.boundaryChars[ui8a[i]] = true;
76
+ }
77
+ this.boundary = ui8a;
78
+ this.lookbehind = new Uint8Array(this.boundary.length + 8);
79
+ }
80
+ /**
81
+ * @param data
82
+ */
83
+ write(data) {
84
+ let i = 0;
85
+ const length_ = data.length;
86
+ let previousIndex = this.index;
87
+ let { lookbehind, boundary, boundaryChars, index, state, flags } = this;
88
+ const boundaryLength = this.boundary.length;
89
+ const boundaryEnd = boundaryLength - 1;
90
+ const bufferLength = data.length;
91
+ let c;
92
+ let cl;
93
+ const mark = (name) => {
94
+ this[name + "Mark"] = i;
95
+ };
96
+ const clear = (name) => {
97
+ delete this[name + "Mark"];
98
+ };
99
+ const callback = (callbackSymbol, start, end, ui8a) => {
100
+ if (start === void 0 || start !== end) {
101
+ this[callbackSymbol](ui8a && ui8a.subarray(start, end));
102
+ }
103
+ };
104
+ const dataCallback = (name, clear2 = false) => {
105
+ const markSymbol = name + "Mark";
106
+ if (!(markSymbol in this)) {
107
+ return;
108
+ }
109
+ if (clear2) {
110
+ callback(name, this[markSymbol], i, data);
111
+ delete this[markSymbol];
112
+ } else {
113
+ callback(name, this[markSymbol], data.length, data);
114
+ this[markSymbol] = 0;
115
+ }
116
+ };
117
+ for (i = 0; i < length_; i++) {
118
+ c = data[i];
119
+ switch (state) {
120
+ case 0 /* START_BOUNDARY */:
121
+ if (index === boundary.length - 2) {
122
+ if (c === HYPHEN) {
123
+ flags |= F.LAST_BOUNDARY;
124
+ } else if (c !== CR) {
125
+ return;
126
+ }
127
+ index++;
128
+ break;
129
+ } else if (index - 1 === boundary.length - 2) {
130
+ if (flags & F.LAST_BOUNDARY && c === HYPHEN) {
131
+ state = 9 /* END */;
132
+ flags = 0;
133
+ } else if (!(flags & F.LAST_BOUNDARY) && c === LF) {
134
+ index = 0;
135
+ callback("onPartBegin");
136
+ state = 1 /* HEADER_FIELD_START */;
137
+ } else {
138
+ return;
139
+ }
140
+ break;
141
+ }
142
+ if (c !== boundary[index + 2]) {
143
+ index = -2;
144
+ }
145
+ if (c === boundary[index + 2]) {
146
+ index++;
147
+ }
148
+ break;
149
+ case 1 /* HEADER_FIELD_START */:
150
+ state = 2 /* HEADER_FIELD */;
151
+ mark("onHeaderField");
152
+ index = 0;
153
+ case 2 /* HEADER_FIELD */:
154
+ if (c === CR) {
155
+ clear("onHeaderField");
156
+ state = 6 /* HEADERS_ALMOST_DONE */;
157
+ break;
158
+ }
159
+ index++;
160
+ if (c === HYPHEN) {
161
+ break;
162
+ }
163
+ if (c === COLON) {
164
+ if (index === 1) {
165
+ return;
166
+ }
167
+ dataCallback("onHeaderField", true);
168
+ state = 3 /* HEADER_VALUE_START */;
169
+ break;
170
+ }
171
+ cl = lower(c);
172
+ if (cl < A || cl > Z) {
173
+ return;
174
+ }
175
+ break;
176
+ case 3 /* HEADER_VALUE_START */:
177
+ if (c === SPACE) {
178
+ break;
179
+ }
180
+ mark("onHeaderValue");
181
+ state = 4 /* HEADER_VALUE */;
182
+ case 4 /* HEADER_VALUE */:
183
+ if (c === CR) {
184
+ dataCallback("onHeaderValue", true);
185
+ callback("onHeaderEnd");
186
+ state = 5 /* HEADER_VALUE_ALMOST_DONE */;
187
+ }
188
+ break;
189
+ case 5 /* HEADER_VALUE_ALMOST_DONE */:
190
+ if (c !== LF) {
191
+ return;
192
+ }
193
+ state = 1 /* HEADER_FIELD_START */;
194
+ break;
195
+ case 6 /* HEADERS_ALMOST_DONE */:
196
+ if (c !== LF) {
197
+ return;
198
+ }
199
+ callback("onHeadersEnd");
200
+ state = 7 /* PART_DATA_START */;
201
+ break;
202
+ case 7 /* PART_DATA_START */:
203
+ state = 8 /* PART_DATA */;
204
+ mark("onPartData");
205
+ case 8 /* PART_DATA */:
206
+ previousIndex = index;
207
+ if (index === 0) {
208
+ i += boundaryEnd;
209
+ while (i < bufferLength && !(data[i] in boundaryChars)) {
210
+ i += boundaryLength;
211
+ }
212
+ i -= boundaryEnd;
213
+ c = data[i];
214
+ }
215
+ if (index < boundary.length) {
216
+ if (boundary[index] === c) {
217
+ if (index === 0) {
218
+ dataCallback("onPartData", true);
219
+ }
220
+ index++;
221
+ } else {
222
+ index = 0;
223
+ }
224
+ } else if (index === boundary.length) {
225
+ index++;
226
+ if (c === CR) {
227
+ flags |= F.PART_BOUNDARY;
228
+ } else if (c === HYPHEN) {
229
+ flags |= F.LAST_BOUNDARY;
230
+ } else {
231
+ index = 0;
232
+ }
233
+ } else if (index - 1 === boundary.length) {
234
+ if (flags & F.PART_BOUNDARY) {
235
+ index = 0;
236
+ if (c === LF) {
237
+ flags &= ~F.PART_BOUNDARY;
238
+ callback("onPartEnd");
239
+ callback("onPartBegin");
240
+ state = 1 /* HEADER_FIELD_START */;
241
+ break;
242
+ }
243
+ } else if (flags & F.LAST_BOUNDARY) {
244
+ if (c === HYPHEN) {
245
+ callback("onPartEnd");
246
+ state = 9 /* END */;
247
+ flags = 0;
248
+ } else {
249
+ index = 0;
250
+ }
251
+ } else {
252
+ index = 0;
253
+ }
254
+ }
255
+ if (index > 0) {
256
+ lookbehind[index - 1] = c;
257
+ } else if (previousIndex > 0) {
258
+ const _lookbehind = new Uint8Array(lookbehind.buffer, lookbehind.byteOffset, lookbehind.byteLength);
259
+ callback("onPartData", 0, previousIndex, _lookbehind);
260
+ previousIndex = 0;
261
+ mark("onPartData");
262
+ i--;
263
+ }
264
+ break;
265
+ case 9 /* END */:
266
+ break;
267
+ default:
268
+ throw new Error(`Unexpected state entered: ${state}`);
269
+ }
270
+ }
271
+ dataCallback("onHeaderField");
272
+ dataCallback("onHeaderValue");
273
+ dataCallback("onPartData");
274
+ this.index = index;
275
+ this.state = state;
276
+ this.flags = flags;
277
+ }
278
+ end() {
279
+ if (this.state === 1 /* HEADER_FIELD_START */ && this.index === 0 || this.state === 8 /* PART_DATA */ && this.index === this.boundary.length) {
280
+ this.onPartEnd();
281
+ } else if (this.state !== 9 /* END */) {
282
+ throw new Error("MultipartParser.end(): stream ended unexpectedly");
283
+ }
284
+ }
285
+ }
286
+ function _fileName(headerValue) {
287
+ const m = headerValue.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);
288
+ if (!m) {
289
+ return;
290
+ }
291
+ const match = m[2] || m[3] || "";
292
+ let filename = match.slice(match.lastIndexOf("\\") + 1);
293
+ filename = filename.replace(/%22/g, '"');
294
+ filename = filename.replace(/&#(\d{4});/g, (m2, code) => {
295
+ return String.fromCharCode(code);
296
+ });
297
+ return filename;
298
+ }
299
+ async function toFormData(Body, ct) {
300
+ if (!/multipart/i.test(ct)) {
301
+ throw new TypeError("Failed to fetch");
302
+ }
303
+ const m = ct.match(/boundary=(?:"([^"]+)"|([^;]+))/i);
304
+ if (!m) {
305
+ throw new TypeError("no or bad content-type header, no multipart boundary");
306
+ }
307
+ const parser = new MultipartParser(m[1] || m[2]);
308
+ let headerField;
309
+ let headerValue;
310
+ let entryValue;
311
+ let entryName;
312
+ let contentType;
313
+ let filename;
314
+ const entryChunks = [];
315
+ const formData = new import_esm_min.FormData();
316
+ const onPartData = (ui8a) => {
317
+ entryValue += decoder.decode(ui8a, { stream: true });
318
+ };
319
+ const appendToFile = (ui8a) => {
320
+ entryChunks.push(ui8a);
321
+ };
322
+ const appendFileToFormData = () => {
323
+ const file = new import_blob_from.File(entryChunks, filename, { type: contentType });
324
+ formData.append(entryName, file);
325
+ };
326
+ const appendEntryToFormData = () => {
327
+ formData.append(entryName, entryValue);
328
+ };
329
+ const decoder = new TextDecoder("utf-8");
330
+ decoder.decode();
331
+ parser.onPartBegin = function() {
332
+ parser.onPartData = onPartData;
333
+ parser.onPartEnd = appendEntryToFormData;
334
+ headerField = "";
335
+ headerValue = "";
336
+ entryValue = "";
337
+ entryName = "";
338
+ contentType = "";
339
+ filename = null;
340
+ entryChunks.length = 0;
341
+ };
342
+ parser.onHeaderField = function(ui8a) {
343
+ headerField += decoder.decode(ui8a, { stream: true });
344
+ };
345
+ parser.onHeaderValue = function(ui8a) {
346
+ headerValue += decoder.decode(ui8a, { stream: true });
347
+ };
348
+ parser.onHeaderEnd = function() {
349
+ headerValue += decoder.decode();
350
+ headerField = headerField.toLowerCase();
351
+ if (headerField === "content-disposition") {
352
+ const m2 = headerValue.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);
353
+ if (m2) {
354
+ entryName = m2[2] || m2[3] || "";
355
+ }
356
+ filename = _fileName(headerValue);
357
+ if (filename) {
358
+ parser.onPartData = appendToFile;
359
+ parser.onPartEnd = appendFileToFormData;
360
+ }
361
+ } else if (headerField === "content-type") {
362
+ contentType = headerValue;
363
+ }
364
+ headerValue = "";
365
+ headerField = "";
366
+ };
367
+ for await (const chunk of Body) {
368
+ parser.write(chunk);
369
+ }
370
+ parser.end();
371
+ return formData;
372
+ }
@@ -0,0 +1,172 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var referrer_exports = {};
19
+ __export(referrer_exports, {
20
+ DEFAULT_REFERRER_POLICY: () => DEFAULT_REFERRER_POLICY,
21
+ ReferrerPolicy: () => ReferrerPolicy,
22
+ determineRequestsReferrer: () => determineRequestsReferrer,
23
+ isOriginPotentiallyTrustworthy: () => isOriginPotentiallyTrustworthy,
24
+ isUrlPotentiallyTrustworthy: () => isUrlPotentiallyTrustworthy,
25
+ parseReferrerPolicyFromHeader: () => parseReferrerPolicyFromHeader,
26
+ stripURLForUseAsAReferrer: () => stripURLForUseAsAReferrer,
27
+ validateReferrerPolicy: () => validateReferrerPolicy
28
+ });
29
+ module.exports = __toCommonJS(referrer_exports);
30
+ var import_url = require("@gjsify/deno-runtime/ext/url/00_url");
31
+ var import_net = require("net");
32
+ function stripURLForUseAsAReferrer(url, originOnly = false) {
33
+ if (url == null || url === "no-referrer") {
34
+ return "no-referrer";
35
+ }
36
+ url = new import_url.URL(url);
37
+ if (/^(about|blob|data):$/.test(url.protocol)) {
38
+ return "no-referrer";
39
+ }
40
+ url.username = "";
41
+ url.password = "";
42
+ url.hash = "";
43
+ if (originOnly) {
44
+ url.pathname = "";
45
+ url.search = "";
46
+ }
47
+ return url;
48
+ }
49
+ const ReferrerPolicy = /* @__PURE__ */ new Set([
50
+ "",
51
+ "no-referrer",
52
+ "no-referrer-when-downgrade",
53
+ "same-origin",
54
+ "origin",
55
+ "strict-origin",
56
+ "origin-when-cross-origin",
57
+ "strict-origin-when-cross-origin",
58
+ "unsafe-url"
59
+ ]);
60
+ const DEFAULT_REFERRER_POLICY = "strict-origin-when-cross-origin";
61
+ function validateReferrerPolicy(referrerPolicy) {
62
+ if (!ReferrerPolicy.has(referrerPolicy)) {
63
+ throw new TypeError(`Invalid referrerPolicy: ${referrerPolicy}`);
64
+ }
65
+ return referrerPolicy;
66
+ }
67
+ function isOriginPotentiallyTrustworthy(url) {
68
+ if (/^(http|ws)s:$/.test(url.protocol)) {
69
+ return true;
70
+ }
71
+ const hostIp = url.host.replace(/(^\[)|(]$)/g, "");
72
+ const hostIPVersion = (0, import_net.isIP)(hostIp);
73
+ if (hostIPVersion === 4 && /^127\./.test(hostIp)) {
74
+ return true;
75
+ }
76
+ if (hostIPVersion === 6 && /^(((0+:){7})|(::(0+:){0,6}))0*1$/.test(hostIp)) {
77
+ return true;
78
+ }
79
+ if (url.host === "localhost" || url.host.endsWith(".localhost")) {
80
+ return false;
81
+ }
82
+ if (url.protocol === "file:") {
83
+ return true;
84
+ }
85
+ return false;
86
+ }
87
+ function isUrlPotentiallyTrustworthy(url) {
88
+ if (/^about:(blank|srcdoc)$/.test(url.toString())) {
89
+ return true;
90
+ }
91
+ if (typeof url === "string") {
92
+ url = new import_url.URL(url);
93
+ }
94
+ if (url.protocol === "data:") {
95
+ return true;
96
+ }
97
+ if (/^(blob|filesystem):$/.test(url.protocol)) {
98
+ return true;
99
+ }
100
+ return isOriginPotentiallyTrustworthy(url);
101
+ }
102
+ function determineRequestsReferrer(request, obj = {}) {
103
+ const { referrerURLCallback, referrerOriginCallback } = obj;
104
+ if (request.referrer === "no-referrer" || request.referrerPolicy === "") {
105
+ return null;
106
+ }
107
+ const policy = request.referrerPolicy;
108
+ if (request.referrer === "about:client") {
109
+ return "no-referrer";
110
+ }
111
+ const referrerSource = new import_url.URL(request.referrer);
112
+ let referrerURL = stripURLForUseAsAReferrer(referrerSource);
113
+ let referrerOrigin = stripURLForUseAsAReferrer(referrerSource, true);
114
+ if (referrerURL.toString().length > 4096) {
115
+ referrerURL = referrerOrigin;
116
+ }
117
+ if (referrerURLCallback) {
118
+ referrerURL = referrerURLCallback(referrerURL);
119
+ }
120
+ if (referrerOriginCallback) {
121
+ referrerOrigin = referrerOriginCallback(referrerOrigin);
122
+ }
123
+ const currentURL = new import_url.URL(request.url);
124
+ switch (policy) {
125
+ case "no-referrer":
126
+ return "no-referrer";
127
+ case "origin":
128
+ return referrerOrigin;
129
+ case "unsafe-url":
130
+ return referrerURL;
131
+ case "strict-origin":
132
+ if (isUrlPotentiallyTrustworthy(referrerURL) && !isUrlPotentiallyTrustworthy(currentURL)) {
133
+ return "no-referrer";
134
+ }
135
+ return referrerOrigin.toString();
136
+ case "strict-origin-when-cross-origin":
137
+ if (referrerURL.origin === currentURL.origin) {
138
+ return referrerURL;
139
+ }
140
+ if (isUrlPotentiallyTrustworthy(referrerURL) && !isUrlPotentiallyTrustworthy(currentURL)) {
141
+ return "no-referrer";
142
+ }
143
+ return referrerOrigin;
144
+ case "same-origin":
145
+ if (referrerURL.origin === currentURL.origin) {
146
+ return referrerURL;
147
+ }
148
+ return "no-referrer";
149
+ case "origin-when-cross-origin":
150
+ if (referrerURL.origin === currentURL.origin) {
151
+ return referrerURL;
152
+ }
153
+ return referrerOrigin;
154
+ case "no-referrer-when-downgrade":
155
+ if (isUrlPotentiallyTrustworthy(referrerURL) && !isUrlPotentiallyTrustworthy(currentURL)) {
156
+ return "no-referrer";
157
+ }
158
+ return referrerURL;
159
+ default:
160
+ throw new TypeError(`Invalid referrerPolicy: ${policy}`);
161
+ }
162
+ }
163
+ function parseReferrerPolicyFromHeader(headers) {
164
+ const policyTokens = (headers.get("referrer-policy") || "").split(/[,\s]+/);
165
+ let policy = "";
166
+ for (const token of policyTokens) {
167
+ if (token && ReferrerPolicy.has(token)) {
168
+ policy = token;
169
+ }
170
+ }
171
+ return policy;
172
+ }