Add https request error handling
This commit is contained in:
parent
342c977606
commit
b29a9121f2
@ -1,6 +1,5 @@
|
|||||||
import { video_info } from "."
|
import { video_info } from "."
|
||||||
import { LiveStreaming, Stream } from "./classes/LiveStream"
|
import { LiveStreaming, Stream } from "./classes/LiveStream"
|
||||||
import { request } from "./utils/request"
|
|
||||||
|
|
||||||
export enum StreamType{
|
export enum StreamType{
|
||||||
Arbitrary = 'arbitrary',
|
Arbitrary = 'arbitrary',
|
||||||
|
|||||||
@ -10,17 +10,18 @@ interface RequestOpts extends RequestOptions{
|
|||||||
async function https_getter(req_url : string, options : RequestOpts = {}): Promise<IncomingMessage>{
|
async function https_getter(req_url : string, options : RequestOpts = {}): Promise<IncomingMessage>{
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let s = new URL(req_url)
|
let s = new URL(req_url)
|
||||||
if(!options.method) options.method = "GET"
|
options.method ??= "GET"
|
||||||
let req_options : RequestOptions = {
|
let req_options : RequestOptions = {
|
||||||
host : s.hostname,
|
host : s.hostname,
|
||||||
path : s.pathname + s.search,
|
path : s.pathname + s.search,
|
||||||
headers : (options.headers) ? options.headers : {},
|
headers : options.headers ?? {},
|
||||||
method : options.method
|
method : options.method
|
||||||
}
|
}
|
||||||
|
|
||||||
let req = https.request(req_options, (response) => {
|
let req = https.request(req_options, (response) => {
|
||||||
resolve(response)
|
resolve(response)
|
||||||
})
|
})
|
||||||
|
req.on('error', reject)
|
||||||
if(options.method === "POST") req.write(options.body)
|
if(options.method === "POST") req.write(options.body)
|
||||||
req.end()
|
req.end()
|
||||||
})
|
})
|
||||||
@ -28,6 +29,7 @@ async function https_getter(req_url : string, options : RequestOpts = {}): Promi
|
|||||||
|
|
||||||
export async function request(url : string, options? : RequestOpts): Promise<string>{
|
export async function request(url : string, options? : RequestOpts): Promise<string>{
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try{
|
||||||
let data = ''
|
let data = ''
|
||||||
let res = await https_getter(url, options)
|
let res = await https_getter(url, options)
|
||||||
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
|
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
|
||||||
@ -39,16 +41,24 @@ export async function request(url : string, options? : RequestOpts): Promise<str
|
|||||||
res.setEncoding('utf-8')
|
res.setEncoding('utf-8')
|
||||||
res.on('data', (c) => data+=c)
|
res.on('data', (c) => data+=c)
|
||||||
res.on('end', () => resolve(data))
|
res.on('end', () => resolve(data))
|
||||||
|
}
|
||||||
|
catch(err) {
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function request_stream(url : string, options? : RequestOpts): Promise<IncomingMessage>{
|
export async function request_stream(url : string, options? : RequestOpts): Promise<IncomingMessage>{
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let data = ''
|
try{
|
||||||
let res = await https_getter(url, options)
|
let res = await https_getter(url, options)
|
||||||
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
|
if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){
|
||||||
res = await https_getter(res.headers.location as string, options)
|
res = await https_getter(res.headers.location as string, options)
|
||||||
}
|
}
|
||||||
resolve(res)
|
resolve(res)
|
||||||
|
}
|
||||||
|
catch(err){
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user