Use array + join, instead of append for request body strings

This commit is contained in:
absidue 2021-10-28 18:57:44 +02:00
parent 533aebc2ff
commit a13e588246
2 changed files with 6 additions and 4 deletions

View File

@ -69,6 +69,7 @@ export class Proxy {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.socket.setEncoding('utf-8'); this.socket.setEncoding('utf-8');
this.socket.once('error', (err) => reject(err)); this.socket.once('error', (err) => reject(err));
const parts: string[] = [];
this.socket.on('data', (chunk: string) => { this.socket.on('data', (chunk: string) => {
if (this.rawHeaders.length === 0) { if (this.rawHeaders.length === 0) {
this.rawHeaders = chunk; this.rawHeaders = chunk;
@ -76,10 +77,11 @@ export class Proxy {
} else { } else {
const arr = chunk.split('\r\n'); const arr = chunk.split('\r\n');
if (arr.length > 1 && arr[0].length < 5) arr.shift(); if (arr.length > 1 && arr[0].length < 5) arr.shift();
this.body += arr.join(''); parts.push(...arr);
} }
}); });
this.socket.on('end', () => { this.socket.on('end', () => {
this.body = parts.join('');
resolve(this); resolve(this);
}); });
}); });

View File

@ -49,7 +49,6 @@ export function request_stream(req_url: string, options: RequestOpts = { method:
export function request(req_url: string, options: RequestOpts = { method: 'GET' }): Promise<string> { export function request(req_url: string, options: RequestOpts = { method: 'GET' }): Promise<string> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
if (!options?.proxies || options.proxies.length === 0) { if (!options?.proxies || options.proxies.length === 0) {
let data = '';
let cookies_added = false; let cookies_added = false;
if (options.cookies) { if (options.cookies) {
let cook = getCookies(); let cook = getCookies();
@ -80,9 +79,10 @@ export function request(req_url: string, options: RequestOpts = { method: 'GET'
} else if (Number(res.statusCode) > 400) { } else if (Number(res.statusCode) > 400) {
reject(new Error(`Got ${res.statusCode} from the request`)); reject(new Error(`Got ${res.statusCode} from the request`));
} }
const data: string[] = [];
res.setEncoding('utf-8'); res.setEncoding('utf-8');
res.on('data', (c) => (data += c)); res.on('data', (c) => data.push(c));
res.on('end', () => resolve(data)); res.on('end', () => resolve(data.join('')));
} else { } else {
let cookies_added = false; let cookies_added = false;
if (options.cookies) { if (options.cookies) {