@jnode/request 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +10 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jnode/request",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Simple HTTP(s) requesting package for Node.js.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -126,27 +126,24 @@ class RequestResponse {
126
126
  }
127
127
 
128
128
  buffer() {
129
+ if (this._body) return this._body;
129
130
  return new Promise((resolve, reject) => {
130
- if (!this._body) {
131
- const chunks = [];
132
- this.res.on('data', (d) => { chunks.push(d); });
133
- this.res.on('end', () => {
134
- this._body = Buffer.concat(chunks);
135
- resolve(Buffer.concat(chunks));
136
- });
137
- this.res.on('error', reject);
138
- } else {
139
- resolve(this._body);
140
- }
131
+ const chunks = [];
132
+ this.res.on('data', (d) => { chunks.push(d); });
133
+ this.res.on('end', () => {
134
+ this._body = Buffer.concat(chunks);
135
+ resolve(Buffer.concat(chunks));
136
+ });
137
+ this.res.on('error', reject);
141
138
  });
142
139
  }
143
140
 
144
141
  async text(encoding = 'utf8') {
145
- return (await this.buffer()).toString(encoding);
142
+ return this['_text_' + encoding] ?? (this['_text_' + encoding] = (await this.buffer()).toString(encoding));
146
143
  }
147
144
 
148
145
  async json(encoding = 'utf8') {
149
- return JSON.parse((await this.text(encoding)));
146
+ return this['_json_' + encoding] ?? (this['_json_' + encoding] = JSON.parse(await this.text(encoding)));
150
147
  }
151
148
 
152
149
  rl() {