From b29a9121f2b0c050af553ef67df3eb3bac2cd43f Mon Sep 17 00:00:00 2001 From: cjh980402 <9804cjh@naver.com> Date: Mon, 13 Sep 2021 14:13:36 +0900 Subject: [PATCH 1/3] Add https request error handling --- play-dl/YouTube/stream.ts | 9 +++---- play-dl/YouTube/utils/request.ts | 42 ++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/play-dl/YouTube/stream.ts b/play-dl/YouTube/stream.ts index 3c29288..8f50b5c 100644 --- a/play-dl/YouTube/stream.ts +++ b/play-dl/YouTube/stream.ts @@ -1,13 +1,12 @@ import { video_info } from "." import { LiveStreaming, Stream } from "./classes/LiveStream" -import { request } from "./utils/request" export enum StreamType{ Arbitrary = 'arbitrary', - Raw = 'raw', - OggOpus = 'ogg/opus', - WebmOpus = 'webm/opus', - Opus = 'opus', + Raw = 'raw', + OggOpus = 'ogg/opus', + WebmOpus = 'webm/opus', + Opus = 'opus', } interface InfoData{ diff --git a/play-dl/YouTube/utils/request.ts b/play-dl/YouTube/utils/request.ts index 39693a4..7010f35 100644 --- a/play-dl/YouTube/utils/request.ts +++ b/play-dl/YouTube/utils/request.ts @@ -10,17 +10,18 @@ interface RequestOpts extends RequestOptions{ async function https_getter(req_url : string, options : RequestOpts = {}): Promise{ return new Promise((resolve, reject) => { let s = new URL(req_url) - if(!options.method) options.method = "GET" + options.method ??= "GET" let req_options : RequestOptions = { host : s.hostname, path : s.pathname + s.search, - headers : (options.headers) ? options.headers : {}, + headers : options.headers ?? {}, method : options.method } let req = https.request(req_options, (response) => { resolve(response) }) + req.on('error', reject) if(options.method === "POST") req.write(options.body) req.end() }) @@ -28,27 +29,36 @@ async function https_getter(req_url : string, options : RequestOpts = {}): Promi export async function request(url : string, options? : RequestOpts): Promise{ return new Promise(async (resolve, reject) => { - let data = '' - let res = await https_getter(url, options) - if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){ - res = await https_getter(res.headers.location as string , options) + try{ + let data = '' + let res = await https_getter(url, options) + if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){ + res = await https_getter(res.headers.location as string , options) + } + else if(Number(res.statusCode) > 400){ + reject(`Got ${res.statusCode} from the request`) + } + res.setEncoding('utf-8') + res.on('data', (c) => data+=c) + res.on('end', () => resolve(data)) } - else if(Number(res.statusCode) > 400){ - reject(`Got ${res.statusCode} from the request`) + catch(err) { + reject(err) } - res.setEncoding('utf-8') - res.on('data', (c) => data+=c) - res.on('end', () => resolve(data)) }) } export async function request_stream(url : string, options? : RequestOpts): Promise{ return new Promise(async (resolve, reject) => { - let data = '' - let res = await https_getter(url, options) - if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){ - res = await https_getter(res.headers.location as string, options) + try{ + let res = await https_getter(url, options) + if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){ + res = await https_getter(res.headers.location as string, options) + } + resolve(res) + } + catch(err){ + reject(err) } - resolve(res) }) } \ No newline at end of file From c427ff9326c582722d73c9411da32baadf576b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8A=88=EB=A6=AC=ED=8A=AC?= <9804cjh@naver.com> Date: Mon, 13 Sep 2021 14:30:59 +0900 Subject: [PATCH 2/3] Remove useless error handling --- play-dl/YouTube/utils/request.ts | 41 ++++++++++++-------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/play-dl/YouTube/utils/request.ts b/play-dl/YouTube/utils/request.ts index 7010f35..976c487 100644 --- a/play-dl/YouTube/utils/request.ts +++ b/play-dl/YouTube/utils/request.ts @@ -1,5 +1,5 @@ import https, { RequestOptions } from 'https' -import {IncomingMessage } from 'http' +import { IncomingMessage } from 'http' import { URL } from 'url' interface RequestOpts extends RequestOptions{ @@ -21,7 +21,6 @@ async function https_getter(req_url : string, options : RequestOpts = {}): Promi let req = https.request(req_options, (response) => { resolve(response) }) - req.on('error', reject) if(options.method === "POST") req.write(options.body) req.end() }) @@ -29,36 +28,26 @@ async function https_getter(req_url : string, options : RequestOpts = {}): Promi export async function request(url : string, options? : RequestOpts): Promise{ return new Promise(async (resolve, reject) => { - try{ - let data = '' - let res = await https_getter(url, options) - if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){ - res = await https_getter(res.headers.location as string , options) - } - else if(Number(res.statusCode) > 400){ - reject(`Got ${res.statusCode} from the request`) - } - res.setEncoding('utf-8') - res.on('data', (c) => data+=c) - res.on('end', () => resolve(data)) + let data = '' + let res = await https_getter(url, options) + if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){ + res = await https_getter(res.headers.location as string , options) } - catch(err) { - reject(err) + else if(Number(res.statusCode) > 400){ + reject(`Got ${res.statusCode} from the request`) } + res.setEncoding('utf-8') + res.on('data', (c) => data+=c) + res.on('end', () => resolve(data)) }) } export async function request_stream(url : string, options? : RequestOpts): Promise{ return new Promise(async (resolve, reject) => { - try{ - let res = await https_getter(url, options) - if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){ - res = await https_getter(res.headers.location as string, options) - } - resolve(res) - } - catch(err){ - reject(err) + let res = await https_getter(url, options) + if(Number(res.statusCode) >= 300 && Number(res.statusCode) < 400){ + res = await https_getter(res.headers.location as string, options) } + resolve(res) }) -} \ No newline at end of file +} From 047d5a666aa376387fbf0b8540c9d6dcfc74eacd Mon Sep 17 00:00:00 2001 From: Killer069 <65385476+killer069@users.noreply.github.com> Date: Mon, 13 Sep 2021 11:14:30 +0530 Subject: [PATCH 3/3] only first stream would retry now --- play-dl/YouTube/classes/LiveStream.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/play-dl/YouTube/classes/LiveStream.ts b/play-dl/YouTube/classes/LiveStream.ts index 597a3f8..8674562 100644 --- a/play-dl/YouTube/classes/LiveStream.ts +++ b/play-dl/YouTube/classes/LiveStream.ts @@ -180,7 +180,7 @@ export class Stream { "range" : `bytes=${this.bytes_count}-${end >= this.content_length ? '' : end}` } }) - if(Number(stream.statusCode) >= 400){ + if(Number(stream.statusCode) >= 400 && this.bytes_count === 0){ this.cleanup() await this.retry() this.loop()