Fixed stream ECONNRESET Errors

This commit is contained in:
killer069 2021-09-14 22:06:07 +05:30
parent 925d24c3a7
commit 9ed0048792

View File

@ -21,6 +21,9 @@ async function https_getter(req_url : string, options : RequestOpts = {}): Promi
let req = https.request(req_options, (response) => {
resolve(response)
})
req.on('error', (err) => {
reject(err)
})
if(options.method === "POST") req.write(options.body)
req.end()
})
@ -29,7 +32,8 @@ async function https_getter(req_url : string, options : RequestOpts = {}): Promi
export async function request(url : string, options? : RequestOpts): Promise<string>{
return new Promise(async (resolve, reject) => {
let data = ''
let res = await https_getter(url, options)
let res = await https_getter(url, options).catch((err) => err as string)
if(typeof res === 'string') throw new Error(res)
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
res = await https_getter(res.headers.location as string , options)
}
@ -44,7 +48,8 @@ export async function request(url : string, options? : RequestOpts): Promise<str
export async function request_stream(url : string, options? : RequestOpts): Promise<IncomingMessage>{
return new Promise(async (resolve, reject) => {
let res = await https_getter(url, options)
let res = await https_getter(url, options).catch((err) => err as string)
if(typeof res === 'string') throw new Error(res)
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
res = await https_getter(res.headers.location as string, options)
}