Skip to content

Commit

Permalink
fix(AttachmentExtractor): set probe timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Oct 14, 2023
1 parent b30960f commit 671afaa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/discord-player/src/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class Player extends PlayerEventsEmitter<PlayerEvents> {
queryCache: options.queryCache === null ? null : options.queryCache || new QueryCache(this),
useLegacyFFmpeg: false,
skipFFmpeg: false,
probeTimeout: 5000,
...options,
ytdlOptions: {
highWaterMark: 1 << 25,
Expand Down
4 changes: 4 additions & 0 deletions packages/discord-player/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,8 @@ export interface PlayerInitOptions {
* Skip ffmpeg process when possible
*/
skipFFmpeg?: boolean;
/**
* The probe timeout in milliseconds. Defaults to 5000.
*/
probeTimeout?: number;
}
26 changes: 20 additions & 6 deletions packages/extractor/src/extractors/AttachmentExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ export class AttachmentExtractor extends BaseExtractor {
try {
// eslint-disable-next-line
const mediaplex = require('mediaplex') as typeof import('mediaplex');
const { result, stream } = await mediaplex.probeStream(data);
const timeout = this.context.player.options.probeTimeout ?? 5000;

const { result, stream } = (await Promise.race([
mediaplex.probeStream(data),
new Promise((_, r) => {
setTimeout(() => r(new Error('Timeout')), timeout);
})
])) as Awaited<ReturnType<typeof mediaplex.probeStream>>;

if (result) {
trackInfo.duration = result.duration * 1000;
Expand Down Expand Up @@ -121,12 +128,19 @@ export class AttachmentExtractor extends BaseExtractor {
// eslint-disable-next-line
const mediaplex = require('mediaplex') as typeof import('mediaplex');

const { result, stream } = await mediaplex.probeStream(
createReadStream(query, {
start: 0,
end: 1024 * 1024 * 10
const timeout = this.context.player.options.probeTimeout ?? 5000;

const { result, stream } = (await Promise.race([
mediaplex.probeStream(
createReadStream(query, {
start: 0,
end: 1024 * 1024 * 10
})
),
new Promise((_, r) => {
setTimeout(() => r(new Error('Timeout')), timeout);
})
);
])) as Awaited<ReturnType<typeof mediaplex.probeStream>>;

if (result) {
trackInfo.duration = result.duration * 1000;
Expand Down

0 comments on commit 671afaa

Please sign in to comment.