From c395eef4972966003c4c2252e6aa9e9caacbd92c Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 18 Mar 2025 13:11:10 +0100 Subject: [PATCH] Tuple Schmuple --- dist/index.js | 11 ++++++----- src/index.ts | 32 ++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/dist/index.js b/dist/index.js index 4027acd..7dbf21d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3,11 +3,12 @@ import https from "https"; import { URL } from "url"; import querystring from "querystring"; import zlib from "zlib"; -const decompress = (data, header) => { - if (header === "gzip") - return zlib.gunzipSync(data); - if (header === "deflate") - return zlib.inflateSync(data); +const decompress = (data, encoding) => { + switch (encoding) { + case "br": return zlib.brotliDecompressSync(data); + case "gzip": return zlib.gunzipSync(data); + case "deflate": return zlib.inflateSync(data); + } return data; }; const readData = (res, mode) => new Promise((resolve, reject) => { diff --git a/src/index.ts b/src/index.ts index 16a0e45..d9421f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,18 +18,22 @@ interface ExtendedRequestOptions extends http.RequestOptions { timeout?: number; } -const decompress = (data: Buffer, header: string | undefined): Buffer => { - if(header === "gzip") - return zlib.gunzipSync(data); - if(header === "deflate") - return zlib.inflateSync(data); +const decompress = ( + data: Buffer, + encoding: string | undefined +): Buffer => { + switch(encoding) { + case "br": return zlib.brotliDecompressSync(data); + case "gzip": return zlib.gunzipSync(data); + case "deflate": return zlib.inflateSync(data); + } return data; }; -const readData = ( +const readData = ( res: http.IncomingMessage, mode: "text" | "json" | "buffer" | "arraybuffer" -): Promise => +): Promise => new Promise((resolve, reject) => { const chunks: Buffer[] = []; res @@ -40,11 +44,11 @@ const readData = ( if(mode === "json") resolve(JSON.parse(data.toString("utf8"))); else if(mode === "buffer") - resolve(data); + resolve(data as T); else if(mode === "arraybuffer") - resolve(new Uint8Array(data).buffer); + resolve(new Uint8Array(data).buffer as T); else - resolve(data.toString("utf8")); + resolve(data.toString("utf8") as T); } catch(err: any) { reject(err); @@ -86,10 +90,10 @@ export default async ( resolve({ body: res, headers: res.headers, - text: () => readData(res, "text"), - json: () => readData(res, "json"), - buffer: () => readData(res, "buffer"), - arrayBuffer: () => readData(res, "arraybuffer") + text: () => readData(res, "text"), + json: () => readData(res, "json"), + buffer: () => readData(res, "buffer"), + arrayBuffer: () => readData(res, "arraybuffer") }); });