-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpRequest.js
414 lines (370 loc) · 12.1 KB
/
HttpRequest.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
import { Readable } from 'node:stream';
import { URLSearchParams } from 'node:url';
/** @typedef {import('../types').RequestMethod} RequestMethod */
/** @typedef {import('http').IncomingHttpHeaders} IncomingHttpHeaders */
/** @typedef {import('../types/index.js').MediaType} MediaType */
/** @typedef {Partial<MediaType> & {parse:(this:HttpRequest)=>any|PromiseLike<any>, test?:(this:HttpRequest, mediaType: MediaType)=>boolean}} ContentReaderRegistration */
/** @type {import('stream/consumers')} */
let streamConsumers;
try {
streamConsumers = await import(new URL('node:stream/consumers').toString());
} catch {}
let BlobClass = (typeof Blob === 'undefined' ? undefined : Blob);
/**
* @typedef HttpRequestOptions
* @prop {IncomingHttpHeaders} [headers]
* @prop {RequestMethod} method
* @prop {Readable} stream
* @prop {string} href URL.href
* @prop {URL['origin']} origin URL.origin
* @prop {URL['protocol']} protocol URL.protocol
* @prop {URL['username']} username URL.username
* @prop {URL['password']} password URL.password
* @prop {URL['host']} host URL.host
* @prop {URL['hostname']} hostname URL.hostname
* @prop {URL['port']} port URL.port
* @prop {URL['pathname']} pathname URL.pathname
* @prop {URL['search']} search URL.search
* @prop {URL['hash']} hash URL.hash
* @prop {string} scheme
* @prop {string} authority
* @prop {string} path unparsed path
* @prop {string} url unparsed url
* @prop {string} query
* @prop {string} fragment
* @prop {any} [body]
*/
/**
* @implements URL
*/
export default class HttpRequest {
/** @type {ReadableStream} */
#readable;
#bodyUsed = false;
/** @type {MediaType} */
#mediaType;
/** @param {HttpRequestOptions} options */
constructor(options) {
/** @type {IncomingHttpHeaders} */
this.headers = options.headers ?? {};
this.method = options.method;
this.stream = options.stream;
this.href = options.href;
this.origin = options.origin;
this.protocol = options.protocol;
this.username = options.username;
this.password = options.password;
this.host = options.host;
this.hostname = options.hostname;
this.port = options.port;
this.pathname = options.pathname;
this.search = options.search;
this.hash = options.hash;
this.scheme = options.scheme;
this.authority = options.authority;
this.path = options.path;
this.query = options.query;
this.fragment = options.fragment;
this.url = options.url;
/** @type {any} */
this.body = options.body ?? null;
}
// Per request buffer allocation limits
/** @type {number} Minimum initial buffer size when working with chunks */
MIN_INITIAL_BUFFER_SIZE = 16 * 1024; // 64KB
/** @type {number} Maximium initial buffer size when working with chunks */
MAX_INITIAL_BUFFER_SIZE = 4 * 1024 * 1024; // 4MB
/** @type {number} Absolute maximum buffer size to be allocated per request. */
MAX_BUFFER_SIZE = 64 * 1024 * 1024; // 64MB
/** @type {ContentReaderRegistration[]} */
contentReaders = [
{ type: 'text', parse: this.text },
{ type: 'application', subtype: 'json', parse: this.json },
{ suffix: 'json', parse: this.json },
];
/**
* @param {import('stream').Duplex} downstream
* @param {Object} [options]
* @param {boolean} [options.forwardErrors=true] Forward errors back toward source
* @param {boolean} [options.autoPipe=true]
* @param {boolean} [options.autoDestroy=true] Ignored if auto-piping
* @param {boolean} [options.autoPause=false]
* @return {Readable} previous tailend stream
*/
addDownstream(downstream, options = {}) {
const inputStream = this.stream;
this.stream = downstream;
if (!inputStream.readable) throw new Error('STREAM_NOT_READABLE');
if (options.forwardErrors !== false) {
downstream.on('error', (error) => inputStream.emit('error', error));
}
if (options.autoPipe !== false) {
inputStream.pipe(downstream);
} else if (options.autoDestroy !== false) {
inputStream.on('close', () => downstream.destroy());
inputStream.on('end', () => downstream.destroy());
}
if (options.autoPause) {
inputStream.pause();
}
return inputStream;
}
get bodyUsed() { return this.#bodyUsed; }
/**
* @throws {Error<'NOT_SUPPORTED'>}
* @return {?ReadableStream}
*/
get readable() {
if (this.#readable === undefined) {
if (this.method === 'GET' || this.method === 'HEAD') {
this.#readable = null;
} else if (Readable.toWeb) {
this.#readable = Readable.toWeb(this.stream);
this.#bodyUsed = true;
} else {
// Should use await .blob().stream() instead.
throw new Error('NOT_SUPPORTED');
}
}
return this.#readable;
}
async read() {
if (this.method === 'GET' || this.method === 'HEAD') {
return this.searchParams;
}
const {
type, tree, subtype, suffix,
} = this.mediaType;
for (const entry of this.contentReaders) {
if (entry.type && entry.type !== type) continue;
if (entry.subtype && entry.subtype !== subtype) continue;
if (entry.tree && entry.tree !== tree) continue;
if (entry.suffix && entry.suffix !== suffix) continue;
if (entry.test && !entry.test.call(this, this.mediaType)) continue;
// eslint-disable-next-line @typescript-eslint/return-await, no-await-in-loop
return await entry.parse.call(this);
}
const chunks = [];
for await (const chunk of this.stream) {
chunks.push(chunk);
}
this.#bodyUsed = true;
if (!chunks.length) return null;
const [firstChunk] = chunks;
if (chunks.length === 1) return firstChunk;
if (Buffer.isBuffer(firstChunk)) {
return Buffer.concat(chunks);
}
if (typeof firstChunk === 'string') {
return chunks.join('');
}
return chunks;
}
/**
* @throws {Error<'MAX_BUFFER_SIZE_REACHED'>}
* @return {Promise<Buffer>}
*/
async buffer() {
try {
if (streamConsumers) {
return await streamConsumers.buffer(this.stream);
}
let buffer;
let offset = 0;
const sourceEncoding = this.stream.readableEncoding;
for await (const c of this.stream) {
const chunk = sourceEncoding ? Buffer.from(c, sourceEncoding) : c;
if (!buffer) {
// Initial buffer allocation. Use content-length as reference, but not hard requirement.
// Size up to content-length, capped by MAX_INITIAL_BUFFER_SIZE
// Size down to chunk-length, capped by MIN_INITIAL_BUFFER_SIZE
const contentLength = Number.parseInt(this.headers['content-length'], 10);
const initialSize = (contentLength > 0 && chunk.length < contentLength)
? Math.min(this.MAX_INITIAL_BUFFER_SIZE, contentLength)
: Math.max(chunk.length, this.MIN_INITIAL_BUFFER_SIZE);
buffer = Buffer.allocUnsafe(initialSize);
} else if (buffer.length < offset + chunk.length) {
if (buffer.length === this.MAX_BUFFER_SIZE) {
throw new Error('MAX_BUFFER_SIZE_REACHED');
}
const temporaryBuffer = buffer;
buffer = Buffer.allocUnsafe(Math.min(this.MAX_BUFFER_SIZE, temporaryBuffer.length * 2));
temporaryBuffer.copy(buffer, 0, 0, temporaryBuffer.length);
}
chunk.copy(buffer, offset, 0, chunk.length);
offset += chunk.length;
}
// Must partition to clear unsafe allocation
return buffer.slice(0, offset);
} finally {
this.#bodyUsed = true;
}
}
/**
* @return {Promise<ArrayBuffer>}
*/
async arrayBuffer() {
try {
if (streamConsumers) {
return await streamConsumers.arrayBuffer(this.stream);
}
const buffer = await this.buffer();
return buffer.buffer;
} finally {
this.#bodyUsed = true;
}
}
async blob() {
if (streamConsumers) {
const contentType = this.headers['content-type'];
// @ts-expect-error Bad typings
const result = await streamConsumers.blob(this.stream, {
type: contentType,
});
this.#bodyUsed = true;
if (!contentType || result.type === contentType) {
return result;
}
// Proxy needed to set type.
return new Proxy(result, {
get: (target, p, receiver) => {
if (p === 'type') return this.headers['content-type'];
return Reflect.get(target, p, receiver);
},
});
}
if (BlobClass === undefined) {
try {
const module = await import('node:buffer');
if ('Blob' in module === false) throw new Error('NOT_SUPPORTED');
BlobClass = module.Blob;
} catch {
BlobClass = null;
}
}
if (BlobClass === null) {
throw new Error('NOT_SUPPORTED');
}
try {
const chunks = [];
for await (const chunk of this.stream) { chunks.push(chunk); }
return new BlobClass(chunks, {
type: this.headers['content-type'],
});
} finally {
this.#bodyUsed = true;
}
}
/** @return {Promise<any>} */
async json() {
if (streamConsumers) {
this.#bodyUsed = true;
return await streamConsumers.json(this.stream);
}
const text = await this.text();
return JSON.parse(text);
}
async text() {
try {
const encoding = this.bufferEncoding;
if (encoding === 'utf-8' && streamConsumers) {
return await streamConsumers.text(this.stream);
}
// https://github.com/nodejs/node/blob/094b2a/lib/stream/consumers.js#L57
const dec = new TextDecoder(encoding === 'utf16le' ? 'utf-16le' : encoding);
let text = '';
for await (const chunk of this.stream) {
text += typeof chunk === 'string'
? chunk
: dec.decode(chunk, { stream: true });
}
// Flush the streaming TextDecoder so that any pending
// incomplete multibyte characters are handled.
text += dec.decode(undefined, { stream: false });
return text;
} finally {
this.#bodyUsed = true;
}
}
get mediaType() {
if (!this.#mediaType) {
const contentType = this.headers['content-type'];
let type;
let tree;
let subtype;
let suffix;
/** @type {Record<string,string>} */
const parameters = {};
if (contentType) {
for (const directive of contentType.split(';')) {
let [key, value] = directive.split('=');
key = key.trim().toLowerCase();
if (value === undefined) {
let rest;
[type, rest] = key.split('/');
const treeEntries = rest.split('.');
const subtypeSuffix = treeEntries.pop();
tree = treeEntries.join('.');
[subtype, suffix] = subtypeSuffix.split('+');
continue;
}
value = value.trim();
const firstQuote = value.indexOf('"');
const lastQuote = value.lastIndexOf('"');
if (firstQuote !== -1 && lastQuote !== -1) {
if (firstQuote === lastQuote) {
throw new Error('ERR_CONTENT_TYPE');
}
value = value.slice(firstQuote + 1, lastQuote);
}
parameters[key] = value;
}
}
this.#mediaType = {
type, subtype, suffix, tree, parameters,
};
}
return this.#mediaType;
}
/** @return {null|undefined|string} */
get charset() {
return this.mediaType.parameters.charset;
}
get bufferEncoding() {
const { charset } = this;
switch (charset?.toLowerCase()) {
case 'ucs-2':
case 'ucs2':
case 'utf-16le':
case 'utf16le':
return 'utf16le';
case 'utf-8':
case 'utf8':
return 'utf-8';
case 'base64':
case 'hex':
return /** @type {BufferEncoding} */ (charset);
case 'ascii': // Default
case 'binary':
case 'iso-8859-1':
case 'latin1':
default:
return 'latin1';
}
}
async formData() {
throw new Error('UNSUPPORTED_MEDIA_TYPE');
}
/** @type {URLSearchParams} */
#searchParams;
get searchParams() {
// eslint-disable-next-line no-return-assign
return this.#searchParams ??= new URLSearchParams(this.query);
}
toJSON() {
return this.href;
}
toString() {
return this.href;
}
}