Tuple Schmuple

This commit is contained in:
Flummi 2025-03-18 13:11:10 +01:00
parent cf64ee2e01
commit c395eef497
2 changed files with 24 additions and 19 deletions

11
dist/index.js vendored
View File

@ -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) => {

View File

@ -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")
});
});