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 = <T>( res: http.IncomingMessage, mode: "text" | "json" | "buffer" | "arraybuffer" -): Promise<string | any | Buffer | ArrayBuffer> => +): Promise<T> => 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<string>(res, "text"), + json: () => readData<JSON>(res, "json"), + buffer: () => readData<Buffer>(res, "buffer"), + arrayBuffer: () => readData<ArrayBuffer>(res, "arraybuffer") }); });