-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathutils.js
57 lines (47 loc) · 1.5 KB
/
utils.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
/** @param {Record<string, any>} obj */
export function lowercase_keys(obj) {
/** @type {Record<string, any>} */
const clone = {};
for (const key in obj) {
clone[key.toLowerCase()] = obj[key];
}
return clone;
}
/** @param {Record<string, string>} params */
export function decode_params(params) {
for (const key in params) {
// input has already been decoded by decodeURI
// now handle the rest that decodeURIComponent would do
params[key] = params[key]
.replace(/%23/g, '#')
.replace(/%3[Bb]/g, ';')
.replace(/%2[Cc]/g, ',')
.replace(/%2[Ff]/g, '/')
.replace(/%3[Ff]/g, '?')
.replace(/%3[Aa]/g, ':')
.replace(/%40/g, '@')
.replace(/%26/g, '&')
.replace(/%3[Dd]/g, '=')
.replace(/%2[Bb]/g, '+')
.replace(/%24/g, '$');
}
return params;
}
/** @param {any} body */
export function is_pojo(body) {
if (typeof body !== 'object') return false;
if (body) {
if (body instanceof Uint8Array) return false;
// body could be a node Readable, but we don't want to import
// node built-ins, so we use duck typing
if (body._readableState && typeof body.pipe === 'function') return false;
// similarly, it could be a web ReadableStream
if (typeof ReadableStream !== 'undefined' && body instanceof ReadableStream) return false;
}
return true;
}
/** @param {import('types').RequestEvent} event */
export function normalize_request_method(event) {
const method = event.request.method.toLowerCase();
return method === 'delete' ? 'del' : method; // 'delete' is a reserved word
}