Skip to content

Commit

Permalink
lazy access body (#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morita0711 committed Jan 18, 2022
1 parent 7a3e932 commit 9c2cab6
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/node/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function Response(request) {
this.request = request;
this.req = request.req;
this.text = res.text;
this.body = res.body === undefined ? {} : res.body;
this.files = res.files || {};
this.buffered = request._resBuffered;
this.headers = res.headers;
Expand All @@ -48,6 +47,21 @@ function Response(request) {
res.on('error', this.emit.bind(this, 'error'));
}

// Lazy access res.body.
// https://github.com/nodejs/node/pull/39520#issuecomment-889697136
Object.defineProperty(Response.prototype, 'body', {
get () {
return this._body !== undefined
? this._body
: this.res.body !== undefined
? this.res.body
: {};
},
set (value) {
this._body = value;
}
});

/**
* Inherit from `Stream`.
*/
Expand Down

0 comments on commit 9c2cab6

Please sign in to comment.