From ed92e332ab51ad3850018edcddeaa6c6d5cbd420 Mon Sep 17 00:00:00 2001 From: Snowflake107 Date: Wed, 23 Jun 2021 15:10:39 +0545 Subject: [PATCH] add StreamUtils --- src/index.ts | 3 +++ src/utils/StreamUtils.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/utils/StreamUtils.ts diff --git a/src/index.ts b/src/index.ts index 8acfe26..5739670 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,11 @@ export { AudioFilters } from "./utils/AudioFilters"; export { ExtractorModel } from "./Structures/ExtractorModel"; export { Playlist } from "./Structures/Playlist"; export { Player } from "./Player"; +export { QueryResolver } from "./utils/QueryResolver"; export { Queue } from "./Structures/Queue"; +export { StreamUtils } from "./utils/StreamUtils"; export { Track } from "./Structures/Track"; +export { Util } from "./utils/Util"; export { VoiceUtils } from "./VoiceInterface/VoiceUtils"; export { VoiceEvents, StreamDispatcher } from "./VoiceInterface/BasicStreamDispatcher"; export { VoiceAdapterCreator } from "./VoiceInterface/AdapterCreator"; diff --git a/src/utils/StreamUtils.ts b/src/utils/StreamUtils.ts new file mode 100644 index 0000000..88b6bd2 --- /dev/null +++ b/src/utils/StreamUtils.ts @@ -0,0 +1,37 @@ +import { Duplex, Readable, PassThrough } from "stream"; + +class StreamUtils { + /** + * Stream utils + */ + constructor() { + throw new Error("Cannot instantiate static class"); + } + + /** + * Can be used to re-create used streams + * @param {Readable|Duplex} stream The used stream + * @returns {Readable} + */ + static clone(stream: Readable | Duplex): Readable { + const passed = stream.pipe(new PassThrough()); + return passed; + } + + /** + * Converts stream to buffer + * @param {Readable|Duplex} stream The stream + * @returns {Promise} + */ + static toBuffer(stream: Readable | Duplex): Promise { + return new Promise((resolve, reject) => { + const chunks: Buffer[] = []; + + stream.on("data", (chunk) => chunks.push(chunk)); + stream.on("end", () => resolve(Buffer.concat(chunks))); + stream.on("error", reject); + }); + } +} + +export { StreamUtils };