-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.js
544 lines (486 loc) · 15.1 KB
/
http.js
1
2
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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/**
* @copyright 2021-2024 Chris Zuber <admin@kernvalley.us>
*/
import { parse } from './dom.js';
import { signalAborted } from './abort.js';
import { setURLParams, setUTMParams, isObject, isNullish, callOnce } from './utility.js';
import { isTrustPolicy } from './trust.js';
import { HTTPException } from './HTTPException.js';
import * as TYPES from '@shgysk8zer0/consts/mimes.js';
function filename(src) {
if (typeof src === 'string') {
return new URL(src, location.origin).pathname.split('/').at(-1);
} else {
return '';
}
}
function getType({ headers }) {
if (headers instanceof Headers && headers.has('Content-Type')) {
return headers.get('Content-Type').split(';')[0];
} else {
return null;
}
}
function handleResponse(resp, { cause, message } = {}) {
if (! (resp instanceof Response)) {
throw new TypeError('Expected a Response object');
} else if (! resp.ok) {
throw new HTTPException(resp, { cause, message });
} else {
return resp;
}
}
function handleError(err) {
if (err instanceof HTTPException) {
throw err;
} else if (err instanceof Error) {
throw new HTTPException(Response.error(), { cause: err, message: err.message });
} else if (typeof err === 'string') {
throw new HTTPException(Response.error(), {
cause: new DOMException('Unknown network error'),
message: err,
});
} else {
throw new HTTPException(Response.error(), {
cause: new DOMException('Unknown network error'),
message: 'Unknown network error',
});
}
}
export async function fetch(url, opts = {}) {
if (opts.signal instanceof AbortSignal && opts.signal.aborted) {
throw new HTTPException(Response.error(), {
cause: opts.signal.reason,
message: opts.signal.reason instanceof Error ? opts.signal.reason.message : opts.signal.reason,
});
} else if (opts.signal instanceof AbortSignal && ! ('signal' in Request.prototype)) {
return await Promise.race([
globalThis.fetch(url, opts),
signalAborted(opts.signal),
]).then(resp => handleResponse(resp, { message: opts.errorMessage }), handleError);
} else {
return await globalThis.fetch(url, opts).then(resp => handleResponse(resp, { message: opts.errorMessage }), handleError);
}
}
export const getHTTPCSP = callOnce(async function getHTTPCSP() {
const { ok, headers } = await fetch(location.href, { method: 'HEAD' });
if (ok && headers.has('Content-Security-Policy')) {
const directives = headers.get('Content-Security-Policy').trim().split(';').filter(str => str.length !== 0);
return Object.fromEntries(directives.map(directive => {
const [key, ...rest] = directive.trim().split(' ').filter(part => part.length !== 0);
return [key, rest];
}));
}
});
export const getCSP = callOnce(async function getCSP() {
try {
const csp = await getHTTPCSP();
if (typeof csp === 'object' && ! Object.is(csp, null)) {
return csp;
} else {
return getMetaCSP();
}
} catch(err) {
console.error(err);
return getMetaCSP();
}
});
export function getMetaCSP() {
const meta = document.head.querySelector('meta[http-equiv="Content-Security-Policy"][content]');
if (meta instanceof HTMLMetaElement) {
const directives = meta.content.trim().split(';').filter(str => str.length !== 0);
return Object.fromEntries(directives.map(directive => {
const [key, ...rest] = directive.trim().split(' ').filter(part => part.length !== 0);
return [key, rest];
}));
} else {
return {};
}
}
export async function GET(url, {
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
priority ='auto',
referrerPolicy = 'no-referrer',
headers = new Headers(),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
errorMessage,
} = {}) {
if (typeof body !== 'undefined') {
url = setURLParams(url, body);
} else if (typeof url === 'string') {
url = new URL(url, document.baseURI);
}
if (typeof signal === 'undefined' && Number.isInteger(timeout)) {
signal = AbortSignal.timeout(timeout);
} else if (signal instanceof AbortController) {
signal = signal.signal;
}
return await fetch(url, { method: 'GET', mode, credentials, referrerPolicy,
headers, cache, redirect, priority, integrity, keepalive, signal, errorMessage });
}
export async function POST(url, {
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
priority = 'auto',
redirect = 'follow',
referrerPolicy = 'no-referrer',
headers = new Headers(),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
errorMessage,
} = {}) {
if (typeof body === 'object' && ! (body instanceof FormData)) {
if (body instanceof HTMLFormElement) {
body = new FormData(body);
} else if (body instanceof File) {
let file;
[body, file] = [new FormData(), body];
// @TODO figure out a default param name for the file
// @TODO handle multiple files and arrays containing files
body.set(file.name, file, file.name);
} else {
body = JSON.stringify(body);
if (headers instanceof Headers && ! headers.has('Content-Type')) {
headers.set('Content-Type', TYPES.JSON);
}
}
}
if (typeof signal === 'undefined' && Number.isInteger(timeout)) {
signal = AbortSignal.timeout(timeout);
} else if (signal instanceof AbortController) {
signal = signal.signal;
}
return await fetch(url, { method: 'POST', body, mode, credentials, referrerPolicy,
headers, cache, redirect, priority, integrity, keepalive, signal, errorMessage });
}
export async function DELETE(url, {
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
priority = 'auto',
referrerPolicy = 'no-referrer',
headers = new Headers(),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
errorMessage,
} = {}) {
if (typeof body !== 'undefined') {
url = setURLParams(url, body);
} else if (typeof url === 'string') {
url = new URL(url, document.baseURI);
}
if (typeof signal === 'undefined' && Number.isInteger(timeout)) {
signal = AbortSignal.timeout(timeout);
} else if (signal instanceof AbortController) {
signal = signal.signal;
}
return await fetch(url, { method: 'DELETE', mode, credentials, referrerPolicy,
headers, cache, redirect, priority, integrity, keepalive, signal, errorMessage });
}
export async function getHTML(url, {
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
priority = 'auto',
referrerPolicy = 'no-referrer',
headers = new Headers({ Accept: TYPES.HTML }),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
sanitizer: {
elements,
attributes,
comments,
} = {},
policy,
errorMessage,
} = {}) {
const html = await getText(url, { body, mode, credentials, referrerPolicy, headers,
cache, redirect, priority, integrity, keepalive, signal, timeout, errorMessage });
if (isTrustPolicy(policy)) {
const doc = new DOMParser().parseFromString(policy.createHTML(html), TYPES.HTML);
const frag = document.createDocumentFragment();
frag.append(...doc.head.children, ...doc.body.children);
return frag;
} else {
const frag = document.createDocumentFragment();
const doc = Document.parseHTML(html, { sanitizer: { elements, attributes, comments }});
frag.append(...doc.head.children, ...doc.body.children);
return frag;
}
}
export const createTemplateGetter = (...args) => callOnce(() => getHTML(...args));
export async function getText(url, {
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
priority = 'auto',
referrerPolicy = 'no-referrer',
headers = new Headers({ Accept: TYPES.TEXT }),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
errorMessage,
} = {}) {
const resp = await GET(url, { body, mode, credentials, referrerPolicy, headers,
cache, redirect, priority, integrity, keepalive, signal, timeout, errorMessage });
return await resp.text();
}
export async function getJSON(url, {
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
referrerPolicy = 'no-referrer',
priority = 'auto',
headers = new Headers({ Accept: TYPES.JSON }),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
errorMessage,
} = {}) {
const resp = await GET(url, { body, mode, credentials, referrerPolicy, headers,
cache, redirect, priority, integrity, keepalive, signal, timeout, errorMessage });
return await resp.json();
}
export async function getFile(url, {
name = null,
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
referrerPolicy = 'no-referrer',
priority = 'auto',
headers = new Headers(),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
errorMessage,
} = {}) {
if (typeof name !== 'string') {
name = filename(url);
}
const resp = await GET(url, { body, mode, credentials, referrerPolicy, headers,
cache, redirect, priority, integrity, keepalive, signal, timeout, errorMessage });
const type = getType(resp);
return new File([await resp.blob()], name, { type });
}
export async function submitForm(form) {
if (typeof form === 'string') {
return await submitForm(document.forms[form]);
} else if (form instanceof Event) {
form.preventDefault();
return await submitForm(form.target);
} else if (form instanceof HTMLFormElement) {
switch (form.method.toLowerCase()) {
case 'get':
return GET(form.action, { body: new FormData(form) });
case 'post':
return POST(form.action, { body: new FormData(form) });
case 'delete':
return DELETE(form.action, { body: new FormData(form) });
default:
throw new Error(`Unsupported method: ${form.method}`);
}
}
}
export const getManifest = callOnce(async function getManifest({ timeout, signal } = {}) {
const resp = await getLink('link[rel="manifest"][href]', { timeout, signal });
return await resp.json();
});
export async function getLink(link, { timeout, signal } = {}) {
if (typeof link === 'string') {
return await getLink(document.querySelector(link, { timeout, signal }));
} else if (! (link instanceof HTMLLinkElement)) {
throw new DOMException('Expected a <link>');
} else if (link.href.length === 0) {
throw new DOMException('Missing `href` on <link>');
} else {
const { href, integrity, type, referrerPolicy, crossOrigin } = link;
let credentials;
const mode = typeof crossOrigin === 'string' ? 'cors' : 'no-cors';
const headers = new Headers();
if (mode === 'cors') {
credentials = crossOrigin === 'use-credentials' ? 'include' : 'omit';
}
if (typeof type === 'string') {
headers.set('Accept', type);
}
return await GET(href, { mode, credentials, headers, integrity, referrerPolicy, timeout, signal });
}
}
export async function postHTML(url, {
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
referrerPolicy = 'no-referrer',
priority = 'auto',
headers = new Headers({ Accept: TYPES.HTML }),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
head = true,
asFrag = true,
sanitizer = undefined,
policy,
errorMessage,
} = {}) {
const html = await postText(url, { body, mode, credentials, referrerPolicy, headers,
cache, redirect, priority, integrity, keepalive, signal, timeout, errorMessage });
return parse(html, { head, asFrag, sanitizer, policy });
}
export async function postJSON(url, {
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
referrerPolicy = 'no-referrer',
priority = 'auto',
headers = new Headers({ Accept: TYPES.JSON }),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
errorMessage,
} = {}) {
const resp = await POST(url, { body, mode, credentials, referrerPolicy, headers,
cache, redirect, priority, integrity, keepalive, signal, timeout, errorMessage });
return await resp.json();
}
export async function postText(url, {
body = undefined,
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
referrerPolicy = 'no-referrer',
priority = 'auto',
headers = new Headers({ Accept: TYPES.TEXT }),
integrity = undefined,
keepalive = undefined,
signal = undefined,
timeout = null,
errorMessage,
} = {}) {
const resp = await POST(url, { body, mode, credentials, referrerPolicy, headers,
cache, redirect, priority, integrity, keepalive, signal, timeout, errorMessage });
return await resp.text();
}
export function navigateTo(to, {
params = {},
utm: {
source: utm_source,
medium: utm_medium = 'referral',
content: utm_content,
campaign: utm_campaign,
term: utm_term,
} = {},
allowedOrigins = [],
requirePath = false,
origin = location.origin,
} = {}) {
const url = new URL(to, origin);
if (! Array.isArray(allowedOrigins)) {
throw new TypeError('`allowedOrigins` must be an array');
} else if (! (url.origin === location.origin || allowedOrigins.includes(url.origin))) {
throw new TypeError(`${url.origin} is not an allowed origin`);
} else if (requirePath && url.pathname.length === 1) {
throw new TypeError('`pathname` is required');
} else if (! isObject(params)) {
throw new TypeError('Expected `params` to be an object');
} else {
if (typeof utm_source === 'string') {
Object.entries({...params, utm_source, utm_medium, utm_content, utm_campaign, utm_term })
.filter(([,v]) => ! isNullish(v))
.forEach(([k, v]) => url.searchParams.set(k, v));
} else {
Object.entries(params)
.filter(([,v]) => ! isNullish(v))
.forEach(([k, v]) => url.searchParams.set(k, v));
}
location.href = url.href;
}
}
export async function getCSSStyleSheet(url, {
mode = 'cors',
cache = 'default',
credentials = 'omit',
redirect = 'follow',
priority = 'auto',
referrerPolicy = 'no-referrer',
media,
disabled = false,
baseURL = document.baseURI,
signal,
} = {}) {
const resp = await fetch(url, {
headers: new Headers({ Accept: TYPES.CSS }),
mode, cache, credentials, redirect, priority, referrerPolicy, signal,
});
if (! resp.ok) {
throw new Error(`${resp.url} [${resp.status} ${resp.statusText}]`);
} else if (! resp.headers.get('Content-Type').startsWith(TYPES.CSS)) {
throw new TypeError(`Expected "Content-Type: ${TYPES.CSS}" but got ${resp.headers.get('Content-Type')}.`);
} else {
const css = await resp.text();
return new CSSStyleSheet({ media, disabled, baseURL }).replace(css);
}
}
export function postNav(url, data = {}, {
target = '_self' ,
enctype = TYPES.FORM_URL_ENCODED,
signal,
} = {}) {
if (! (signal instanceof AbortSignal && signal.aborted)) {
const form = document.createElement('form');
const inputs = Object.entries(data).map(([name, value]) => {
const input = document.createElement('input');
input.name = name;
input.type = 'hidden';
input.readOnly = true;
input.value = value;
return input;
});
form.action = url;
form.method = 'POST';
form.target = target;
form.enctype = enctype;
form.hidden = true;
form.append(...inputs);
form.addEventListener('submit', ({ target }) => setTimeout(() => target.remove(), 100), { signal });
requestAnimationFrame(() => {
document.body.append(form);
form.submit();
});
}
}
export { setURLParams, setUTMParams };