feat: add gzip and deflate decompression support and request timeout option
This commit is contained in:
parent
08b74ec61b
commit
cf64ee2e01
15
dist/index.js
vendored
15
dist/index.js
vendored
@ -2,13 +2,21 @@ import http from "http";
|
|||||||
import https from "https";
|
import https from "https";
|
||||||
import { URL } from "url";
|
import { URL } from "url";
|
||||||
import querystring from "querystring";
|
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);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
const readData = (res, mode) => new Promise((resolve, reject) => {
|
const readData = (res, mode) => new Promise((resolve, reject) => {
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
res
|
res
|
||||||
.on("data", chunk => chunks.push(chunk))
|
.on("data", chunk => chunks.push(chunk))
|
||||||
.on("end", () => {
|
.on("end", () => {
|
||||||
const data = Buffer.concat(chunks);
|
|
||||||
try {
|
try {
|
||||||
|
const data = decompress(Buffer.concat(chunks), res.headers["content-encoding"]);
|
||||||
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")
|
||||||
@ -39,6 +47,7 @@ export default async (urlString, options = {}) => {
|
|||||||
headers: {
|
headers: {
|
||||||
...options.headers,
|
...options.headers,
|
||||||
...(body ? { "Content-Length": Buffer.byteLength(body) } : {}),
|
...(body ? { "Content-Length": Buffer.byteLength(body) } : {}),
|
||||||
|
"Accept-Encoding": "gzip, deflate"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -55,6 +64,10 @@ export default async (urlString, options = {}) => {
|
|||||||
arrayBuffer: () => readData(res, "arraybuffer")
|
arrayBuffer: () => readData(res, "arraybuffer")
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
req.setTimeout(options.timeout || 5e3, () => {
|
||||||
|
req.destroy();
|
||||||
|
reject(new Error("Request timed out"));
|
||||||
|
});
|
||||||
req.on("error", reject);
|
req.on("error", reject);
|
||||||
if (body)
|
if (body)
|
||||||
req.write(body);
|
req.write(body);
|
||||||
|
18
src/index.ts
18
src/index.ts
@ -2,6 +2,7 @@ import http from "http";
|
|||||||
import https from "https";
|
import https from "https";
|
||||||
import { URL } from "url";
|
import { URL } from "url";
|
||||||
import querystring from "querystring";
|
import querystring from "querystring";
|
||||||
|
import zlib from "zlib";
|
||||||
|
|
||||||
type ResponseData = {
|
type ResponseData = {
|
||||||
body: http.IncomingMessage;
|
body: http.IncomingMessage;
|
||||||
@ -14,8 +15,17 @@ type ResponseData = {
|
|||||||
|
|
||||||
interface ExtendedRequestOptions extends http.RequestOptions {
|
interface ExtendedRequestOptions extends http.RequestOptions {
|
||||||
body?: any;
|
body?: any;
|
||||||
|
timeout?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const decompress = (data: Buffer, header: string | undefined): Buffer => {
|
||||||
|
if(header === "gzip")
|
||||||
|
return zlib.gunzipSync(data);
|
||||||
|
if(header === "deflate")
|
||||||
|
return zlib.inflateSync(data);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
const readData = (
|
const readData = (
|
||||||
res: http.IncomingMessage,
|
res: http.IncomingMessage,
|
||||||
mode: "text" | "json" | "buffer" | "arraybuffer"
|
mode: "text" | "json" | "buffer" | "arraybuffer"
|
||||||
@ -25,8 +35,8 @@ const readData = (
|
|||||||
res
|
res
|
||||||
.on("data", chunk => chunks.push(chunk))
|
.on("data", chunk => chunks.push(chunk))
|
||||||
.on("end", () => {
|
.on("end", () => {
|
||||||
const data = Buffer.concat(chunks);
|
|
||||||
try {
|
try {
|
||||||
|
const data = decompress(Buffer.concat(chunks), res.headers["content-encoding"]);
|
||||||
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")
|
||||||
@ -64,6 +74,7 @@ export default async (
|
|||||||
headers: {
|
headers: {
|
||||||
...options.headers,
|
...options.headers,
|
||||||
...(body ? { "Content-Length": Buffer.byteLength(body) } : {}),
|
...(body ? { "Content-Length": Buffer.byteLength(body) } : {}),
|
||||||
|
"Accept-Encoding": "gzip, deflate"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -82,6 +93,11 @@ export default async (
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
req.setTimeout(options.timeout || 5e3, () => {
|
||||||
|
req.destroy();
|
||||||
|
reject(new Error("Request timed out"));
|
||||||
|
});
|
||||||
|
|
||||||
req.on("error", reject);
|
req.on("error", reject);
|
||||||
if(body)
|
if(body)
|
||||||
req.write(body);
|
req.write(body);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user