Skip to content

Commit

Permalink
Follow http/https redirects when playing song
Browse files Browse the repository at this point in the history
  • Loading branch information
macdja38 committed Jun 11, 2019
1 parent a46f8f5 commit bd1944a
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/voice/Piper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ try {
EventEmitter = require("events").EventEmitter;
}

function resolveHTTPType(link) {
return link.startsWith("http://") ? HTTP : HTTPS;
}

function webGetter(link, depth = 8) {
return new Promise((resolve, reject) => {
resolveHTTPType(link).get(link, (res) => {
const { statusCode, headers } = res.hasOwnProperty("statusCode") ? res : res.res;

if(statusCode === 301 || statusCode === 302 || statusCode === 307 || statusCode === 308) {
if(depth <= 0) {
reject(new Error("too many redirects"));
} else {
resolve(webGetter(headers.location, depth - 1));
}
} else {
resolve(res);
}
}).once('error', reject);
});
}

class Piper extends EventEmitter {
constructor(converterCommand, opus) {
super();
Expand Down Expand Up @@ -48,11 +70,7 @@ class Piper extends EventEmitter {
if(options.format === "dca" || options.format === "ogg" || options.format === "webm" || options.format === "pcm") {
if(source.startsWith("http://") || source.startsWith("https://")) {
const passThrough = new PassThroughStream();
if(source.startsWith("http://")) {
HTTP.get(source, (res) => res.pipe(passThrough)).once("error", (e) => this.stop(e));
} else {
HTTPS.get(source, (res) => res.pipe(passThrough)).once("error", (e) => this.stop(e));
}
webGetter(source).then(res => res.pipe(passThrough)).catch(e => this.stop(e));
source = passThrough;
} else {
try {
Expand Down

0 comments on commit bd1944a

Please sign in to comment.