-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
71 lines (59 loc) · 1.76 KB
/
utils.ts
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
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
import {
ContentRange,
filterKeys,
isRepresentationHeader,
not,
parseAcceptRanges,
RangeHeader,
Status,
stringifyContentRange,
type Token,
} from "./deps.ts";
export const enum RangeUnit {
Bytes = "bytes",
None = "none",
}
/** Shallow merge two headers. */
export function shallowMergeHeaders(left: Headers, right: Headers): Headers {
const lHeader = new Headers(left);
right.forEach((value, key) => lHeader.set(key, value));
return lHeader;
}
export class RequestedRangeNotSatisfiableResponse extends Response {
constructor(
contentRange: ContentRange,
init?: Omit<ResponseInit, "status"> | undefined,
) {
const { statusText } = init ?? {};
const headers = filterKeys(
new Headers(init?.headers),
not(isRepresentationHeader),
);
if (!headers.has(RangeHeader.ContentRange)) {
const contentRangeStr = stringifyContentRange(contentRange);
headers.set(RangeHeader.ContentRange, contentRangeStr);
}
super(null, {
status: Status.RequestedRangeNotSatisfiable,
statusText,
headers,
});
}
}
/** Whether the inputs are equal to case sensitivity. */
export function equalsCaseInsensitive(left: string, right: string): boolean {
if (left === right) return true;
return !left.localeCompare(right, undefined, { sensitivity: "accent" });
}
/** Whether the input has {@link Token} or not.
* If the input is invalid [`Accept-Ranges`](https://www.rfc-editor.org/rfc/rfc9110.html#section-14.3-2) then `false`.
*/
export function hasToken(input: string, token: Token): boolean {
try {
return parseAcceptRanges(input).includes(token);
} catch {
return false;
}
}