-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
260 lines (260 loc) · 8.84 KB
/
mod.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
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
import { isStringSingleLine } from "https://raw.githubusercontent.com/hugoalh/is-string-singleline-es/v1.0.4/mod.ts";
const parametersNeedLowerCase: string[] = [
"rel",
"type"
];
const regexpLinkWhitespace = /[\n\r\s\t]/;
/**
* HTTP header `Link` entry.
*/
export type HTTPHeaderLinkEntry = [
uri: string,
parameters: { [key: string]: string; }
];
function validateURI(uri: string): void {
if (!(isStringSingleLine(uri) && !regexpLinkWhitespace.test(uri))) {
throw new SyntaxError(`\`${uri}\` is not a valid URI!`);
}
}
function* parseLinkFromString(input: string): Generator<HTTPHeaderLinkEntry> {
// Remove Unicode characters of BOM (Byte Order Mark) and no-break space.
const inputFmt: string = input.replaceAll("\u00A0", "").replaceAll("\uFEFF", "");
for (let cursor: number = 0; cursor < inputFmt.length; cursor += 1) {
while (regexpLinkWhitespace.test(inputFmt.charAt(cursor))) {
cursor += 1;
}
if (inputFmt.charAt(cursor) !== "<") {
throw new SyntaxError(`Unexpected character \`${inputFmt.charAt(cursor)}\` at position ${cursor}; Expect character \`<\`!`);
}
cursor += 1;
const cursorEndURI: number = inputFmt.indexOf(">", cursor);
if (cursorEndURI === -1) {
throw new SyntaxError(`Missing end of URI delimiter character \`>\` after position ${cursor}!`);
}
if (cursorEndURI === cursor) {
throw new SyntaxError(`Missing URI at position ${cursor}!`);
}
const uriSlice: string = inputFmt.slice(cursor, cursorEndURI);
validateURI(uriSlice);
const uri: HTTPHeaderLinkEntry[0] = decodeURI(uriSlice);
const parameters: HTTPHeaderLinkEntry[1] = {};
cursor = cursorEndURI + 1;
while (regexpLinkWhitespace.test(inputFmt.charAt(cursor))) {
cursor += 1;
}
if (
cursor === inputFmt.length ||
inputFmt.charAt(cursor) === ","
) {
yield [uri, parameters];
continue;
}
if (inputFmt.charAt(cursor) !== ";") {
throw new SyntaxError(`Unexpected character \`${inputFmt.charAt(cursor)}\` at position ${cursor}; Expect character \`;\`!`);
}
cursor += 1;
while (cursor < inputFmt.length) {
while (regexpLinkWhitespace.test(inputFmt.charAt(cursor))) {
cursor += 1;
}
const parameterKey: string | undefined = inputFmt.slice(cursor).match(/^[\w-]+\*?/)?.[0].toLowerCase();
if (typeof parameterKey === "undefined") {
throw new SyntaxError(`Unexpected character \`${inputFmt.charAt(cursor)}\` at position ${cursor}; Expect a valid parameter key!`);
}
cursor += parameterKey.length;
while (regexpLinkWhitespace.test(inputFmt.charAt(cursor))) {
cursor += 1;
}
if (
cursor === inputFmt.length ||
inputFmt.charAt(cursor) === ","
) {
parameters[parameterKey] = "";
break;
}
if (inputFmt.charAt(cursor) === ";") {
parameters[parameterKey] = "";
cursor += 1;
continue;
}
if (inputFmt.charAt(cursor) !== "=") {
throw new SyntaxError(`Unexpected character \`${inputFmt.charAt(cursor)}\` at position ${cursor}; Expect character \`=\`!`);
}
cursor += 1;
while (regexpLinkWhitespace.test(inputFmt.charAt(cursor))) {
cursor += 1;
}
let parameterValue: string = "";
if (inputFmt.charAt(cursor) === "\"") {
cursor += 1;
while (cursor < inputFmt.length) {
if (inputFmt.charAt(cursor) === "\"") {
cursor += 1;
break;
}
if (inputFmt.charAt(cursor) === "\\") {
cursor += 1;
}
parameterValue += inputFmt.charAt(cursor);
cursor += 1;
}
} else {
const cursorDiffParameterValue: number = inputFmt.slice(cursor).search(/[\s;,]/);
if (cursorDiffParameterValue === -1) {
parameterValue += inputFmt.slice(cursor);
cursor += parameterValue.length;
} else {
parameterValue += inputFmt.slice(cursor, cursorDiffParameterValue);
cursor += cursorDiffParameterValue;
}
}
parameters[parameterKey] = parametersNeedLowerCase.includes(parameterKey) ? parameterValue.toLowerCase() : parameterValue;
while (regexpLinkWhitespace.test(inputFmt.charAt(cursor))) {
cursor += 1;
}
if (
cursor === inputFmt.length ||
inputFmt.charAt(cursor) === ","
) {
break;
}
if (inputFmt.charAt(cursor) === ";") {
cursor += 1;
continue;
}
throw new SyntaxError(`Unexpected character \`${inputFmt.charAt(cursor)}\` at position ${cursor}; Expect character \`,\`, character \`;\`, or end of the string!`);
}
yield [uri, parameters];
}
}
/**
* Handle the HTTP header `Link` according to the specification RFC 8288.
*/
export class HTTPHeaderLink {
get [Symbol.toStringTag](): string {
return "HTTPHeaderLink";
}
#entries: HTTPHeaderLinkEntry[] = [];
/**
* Handle the HTTP header `Link` according to the specification RFC 8288.
* @param {...(string | Headers | HTTPHeaderLink | HTTPHeaderLinkEntry[] | Response)} inputs Input.
*/
constructor(...inputs: (string | Headers | HTTPHeaderLink | HTTPHeaderLinkEntry[] | Response)[]) {
if (inputs.length > 0) {
this.add(...inputs);
}
}
/**
* Add entries.
* @param {...(string | Headers | HTTPHeaderLink | HTTPHeaderLinkEntry[] | Response)} inputs Input.
* @returns {this}
*/
add(...inputs: (string | Headers | HTTPHeaderLink | HTTPHeaderLinkEntry[] | Response)[]): this {
for (const input of inputs) {
if (input instanceof HTTPHeaderLink) {
this.#entries.push(...structuredClone(input.#entries));
} else if (Array.isArray(input)) {
this.#entries.push(...input.map(([uri, parameters]: HTTPHeaderLinkEntry): HTTPHeaderLinkEntry => {
validateURI(uri);
Object.entries(parameters).forEach(([key, value]: [string, string]): void => {
if (
key !== key.toLowerCase() ||
!(/^[\w-]+\*?$/.test(key))
) {
throw new SyntaxError(`\`${key}\` is not a valid parameter key!`);
}
if (parametersNeedLowerCase.includes(key) && value !== value.toLowerCase()) {
throw new SyntaxError(`\`${value}\` is not a valid parameter value!`);
}
});
return [uri, structuredClone(parameters)];
}));
} else {
for (const entry of parseLinkFromString(((
input instanceof Headers ||
input instanceof Response
) ? ((input instanceof Headers) ? input : input.headers).get("Link") : input) ?? "")) {
this.#entries.push(entry);
}
}
}
return this;
}
/**
* Return all of the entries.
* @returns {HTTPHeaderLinkEntry[]} Entries.
*/
entries(): HTTPHeaderLinkEntry[] {
return structuredClone(this.#entries);
}
/**
* Get entries by parameter.
* @param {string} key Key of the parameter.
* @param {string} value Value of the parameter.
* @returns {HTTPHeaderLinkEntry[]} Entries which match the parameter.
*/
getByParameter(key: string, value: string): HTTPHeaderLinkEntry[] {
if (key !== key.toLowerCase()) {
throw new SyntaxError(`\`${key}\` is not a valid parameter key!`);
}
if (key === "rel") {
return this.getByRel(value);
}
return structuredClone(this.#entries.filter((entry: HTTPHeaderLinkEntry): boolean => {
return (entry[1][key] === value);
}));
}
/**
* Get entries by parameter `rel`.
* @param {string} value Value of the parameter `rel`.
* @returns {HTTPHeaderLinkEntry[]} Entries which match the parameter.
*/
getByRel(value: string): HTTPHeaderLinkEntry[] {
if (value !== value.toLowerCase()) {
throw new SyntaxError(`\`${value}\` is not a valid parameter \`rel\` value!`);
}
return structuredClone(this.#entries.filter((entity: HTTPHeaderLinkEntry): boolean => {
return (entity[1].rel?.toLowerCase() === value);
}));
}
/**
* Whether have entries that match parameter.
* @param {string} key Key of the parameter.
* @param {string} value Value of the parameter.
* @returns {boolean} Determine result.
*/
hasParameter(key: string, value: string): boolean {
return (this.getByParameter(key, value).length > 0);
}
/**
* Stringify entries.
* @returns {string} Stringified entries.
*/
toString(): string {
return this.#entries.map(([uri, parameters]: HTTPHeaderLinkEntry): string => {
return [
`<${encodeURI(uri)}>`,
...Object.entries(parameters).map(([key, value]: [string, string]): string => {
return ((value.length > 0) ? `${key}="${value.replaceAll("\"", "\\\"")}"` : key);
})
].join("; ");
}).join(", ");
}
/**
* Parse the HTTP header `Link` according to the specification RFC 8288.
* @param {...(string | Headers | HTTPHeaderLink | HTTPHeaderLinkEntry[] | Response)} inputs Input.
* @returns {HTTPHeaderLink}
*/
static parse(...inputs: (string | Headers | HTTPHeaderLink | HTTPHeaderLinkEntry[] | Response)[]): HTTPHeaderLink {
return new this(...inputs);
}
/**
* Stringify as the HTTP header `Link` according to the specification RFC 8288.
* @param {...(string | Headers | HTTPHeaderLink | HTTPHeaderLinkEntry[] | Response)} inputs Input.
* @returns {string}
*/
static stringify(...inputs: (string | Headers | HTTPHeaderLink | HTTPHeaderLinkEntry[] | Response)[]): string {
return new this(...inputs).toString();
}
}
export default HTTPHeaderLink;