blob: b17b4813a2136a0c5d457ba373aba4301c823406 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3const Readable = require('stream').Readable;
4const lowercaseKeys = require('lowercase-keys');
5
6class Response extends Readable {
7 constructor(statusCode, headers, body, url) {
8 if (typeof statusCode !== 'number') {
9 throw new TypeError('Argument `statusCode` should be a number');
10 }
11 if (typeof headers !== 'object') {
12 throw new TypeError('Argument `headers` should be an object');
13 }
14 if (!(body instanceof Buffer)) {
15 throw new TypeError('Argument `body` should be a buffer');
16 }
17 if (typeof url !== 'string') {
18 throw new TypeError('Argument `url` should be a string');
19 }
20
21 super();
22 this.statusCode = statusCode;
23 this.headers = lowercaseKeys(headers);
24 this.body = body;
25 this.url = url;
26 }
27
28 _read() {
29 this.push(this.body);
30 this.push(null);
31 }
32}
33
34module.exports = Response;