From a13e588246e6fdb221833ecec9b5e45b40fe3626 Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Thu, 28 Oct 2021 18:57:44 +0200 Subject: [PATCH] Use array + join, instead of append for request body strings --- play-dl/Request/classes.ts | 4 +++- play-dl/Request/index.ts | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/play-dl/Request/classes.ts b/play-dl/Request/classes.ts index 423e434..74621c6 100644 --- a/play-dl/Request/classes.ts +++ b/play-dl/Request/classes.ts @@ -69,6 +69,7 @@ export class Proxy { return new Promise((resolve, reject) => { this.socket.setEncoding('utf-8'); this.socket.once('error', (err) => reject(err)); + const parts: string[] = []; this.socket.on('data', (chunk: string) => { if (this.rawHeaders.length === 0) { this.rawHeaders = chunk; @@ -76,10 +77,11 @@ export class Proxy { } else { const arr = chunk.split('\r\n'); if (arr.length > 1 && arr[0].length < 5) arr.shift(); - this.body += arr.join(''); + parts.push(...arr); } }); this.socket.on('end', () => { + this.body = parts.join(''); resolve(this); }); }); diff --git a/play-dl/Request/index.ts b/play-dl/Request/index.ts index 381bc81..6145884 100644 --- a/play-dl/Request/index.ts +++ b/play-dl/Request/index.ts @@ -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 { return new Promise(async (resolve, reject) => { if (!options?.proxies || options.proxies.length === 0) { - let data = ''; let cookies_added = false; if (options.cookies) { let cook = getCookies(); @@ -80,9 +79,10 @@ export function request(req_url: string, options: RequestOpts = { method: 'GET' } else if (Number(res.statusCode) > 400) { reject(new Error(`Got ${res.statusCode} from the request`)); } + const data: string[] = []; res.setEncoding('utf-8'); - res.on('data', (c) => (data += c)); - res.on('end', () => resolve(data)); + res.on('data', (c) => data.push(c)); + res.on('end', () => resolve(data.join(''))); } else { let cookies_added = false; if (options.cookies) {