54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
const stream = require('stream');
|
|
const Blob = require('./blob.js');
|
|
const Headers = require('./headers.js');
|
|
|
|
class Response {
|
|
constructor(raw, ejectSelfFromCache, fromCache) {
|
|
Object.assign(this, raw);
|
|
this.ejectSelfFromCache = ejectSelfFromCache;
|
|
this.headers = new Headers(raw.headers);
|
|
this.fromCache = fromCache;
|
|
this.bodyUsed = false;
|
|
|
|
if (this.bodyBuffer.type === 'Buffer') {
|
|
this.bodyBuffer = Buffer.from(this.bodyBuffer);
|
|
}
|
|
}
|
|
|
|
get body() {
|
|
return stream.Readable.from(this.bodyBuffer);
|
|
}
|
|
|
|
consumeBody() {
|
|
if (this.bodyUsed) {
|
|
throw new Error('Error: body used already');
|
|
}
|
|
|
|
this.bodyUsed = true;
|
|
return this.bodyBuffer;
|
|
}
|
|
|
|
async text() {
|
|
return this.consumeBody().toString();
|
|
}
|
|
|
|
async json() {
|
|
return JSON.parse(this.consumeBody().toString());
|
|
}
|
|
|
|
async buffer() {
|
|
return this.consumeBody();
|
|
}
|
|
|
|
async blob() {
|
|
const type = this.headers.get('content-type');
|
|
return new Blob([this.consumeBody()], { type });
|
|
}
|
|
|
|
ejectFromCache() {
|
|
return this.ejectSelfFromCache();
|
|
}
|
|
}
|
|
|
|
module.exports = Response;
|