Skip to content

Commit d1cca96

Browse files
committed
lazy access body
Refs: nodejs/node#39520 (comment)
1 parent 3c9c0f7 commit d1cca96

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/node/response.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ function Response(req) {
3333
this.request = req;
3434
this.req = req.req;
3535
this.text = res.text;
36-
this.body = res.body === undefined ? {} : res.body;
3736
this.files = res.files || {};
3837
this.buffered = req._resBuffered;
3938
this.headers = res.headers;
@@ -47,6 +46,21 @@ function Response(req) {
4746
res.on('error', this.emit.bind(this, 'error'));
4847
}
4948

49+
// Lazy access res.body.
50+
// https://github.com/nodejs/node/pull/39520#issuecomment-889697136
51+
Object.defineProperty(Response.prototype, 'body', {
52+
get () {
53+
return this._body !== undefined
54+
? this._body
55+
: this.res.body !== undefined
56+
? this.res.body
57+
: {};
58+
},
59+
set (value) {
60+
this._body = value;
61+
}
62+
});
63+
5064
/**
5165
* Inherit from `Stream`.
5266
*/

0 commit comments

Comments
 (0)