Tuple Schmuple
This commit is contained in:
parent
cf64ee2e01
commit
c395eef497
11
dist/index.js
vendored
11
dist/index.js
vendored
@ -3,11 +3,12 @@ import https from "https";
|
|||||||
import { URL } from "url";
|
import { URL } from "url";
|
||||||
import querystring from "querystring";
|
import querystring from "querystring";
|
||||||
import zlib from "zlib";
|
import zlib from "zlib";
|
||||||
const decompress = (data, header) => {
|
const decompress = (data, encoding) => {
|
||||||
if (header === "gzip")
|
switch (encoding) {
|
||||||
return zlib.gunzipSync(data);
|
case "br": return zlib.brotliDecompressSync(data);
|
||||||
if (header === "deflate")
|
case "gzip": return zlib.gunzipSync(data);
|
||||||
return zlib.inflateSync(data);
|
case "deflate": return zlib.inflateSync(data);
|
||||||
|
}
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
const readData = (res, mode) => new Promise((resolve, reject) => {
|
const readData = (res, mode) => new Promise((resolve, reject) => {
|
||||||
|
32
src/index.ts
32
src/index.ts
@ -18,18 +18,22 @@ interface ExtendedRequestOptions extends http.RequestOptions {
|
|||||||
timeout?: number;
|
timeout?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const decompress = (data: Buffer, header: string | undefined): Buffer => {
|
const decompress = (
|
||||||
if(header === "gzip")
|
data: Buffer,
|
||||||
return zlib.gunzipSync(data);
|
encoding: string | undefined
|
||||||
if(header === "deflate")
|
): Buffer => {
|
||||||
return zlib.inflateSync(data);
|
switch(encoding) {
|
||||||
|
case "br": return zlib.brotliDecompressSync(data);
|
||||||
|
case "gzip": return zlib.gunzipSync(data);
|
||||||
|
case "deflate": return zlib.inflateSync(data);
|
||||||
|
}
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
const readData = (
|
const readData = <T>(
|
||||||
res: http.IncomingMessage,
|
res: http.IncomingMessage,
|
||||||
mode: "text" | "json" | "buffer" | "arraybuffer"
|
mode: "text" | "json" | "buffer" | "arraybuffer"
|
||||||
): Promise<string | any | Buffer | ArrayBuffer> =>
|
): Promise<T> =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
const chunks: Buffer[] = [];
|
const chunks: Buffer[] = [];
|
||||||
res
|
res
|
||||||
@ -40,11 +44,11 @@ const readData = (
|
|||||||
if(mode === "json")
|
if(mode === "json")
|
||||||
resolve(JSON.parse(data.toString("utf8")));
|
resolve(JSON.parse(data.toString("utf8")));
|
||||||
else if(mode === "buffer")
|
else if(mode === "buffer")
|
||||||
resolve(data);
|
resolve(data as T);
|
||||||
else if(mode === "arraybuffer")
|
else if(mode === "arraybuffer")
|
||||||
resolve(new Uint8Array(data).buffer);
|
resolve(new Uint8Array(data).buffer as T);
|
||||||
else
|
else
|
||||||
resolve(data.toString("utf8"));
|
resolve(data.toString("utf8") as T);
|
||||||
}
|
}
|
||||||
catch(err: any) {
|
catch(err: any) {
|
||||||
reject(err);
|
reject(err);
|
||||||
@ -86,10 +90,10 @@ export default async (
|
|||||||
resolve({
|
resolve({
|
||||||
body: res,
|
body: res,
|
||||||
headers: res.headers,
|
headers: res.headers,
|
||||||
text: () => readData(res, "text"),
|
text: () => readData<string>(res, "text"),
|
||||||
json: () => readData(res, "json"),
|
json: () => readData<JSON>(res, "json"),
|
||||||
buffer: () => readData(res, "buffer"),
|
buffer: () => readData<Buffer>(res, "buffer"),
|
||||||
arrayBuffer: () => readData(res, "arraybuffer")
|
arrayBuffer: () => readData<ArrayBuffer>(res, "arraybuffer")
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user