add StreamUtils

This commit is contained in:
Snowflake107 2021-06-23 15:10:39 +05:45
parent 2b82fe2b70
commit ed92e332ab
2 changed files with 40 additions and 0 deletions

View file

@ -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";

37
src/utils/StreamUtils.ts Normal file
View file

@ -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<Buffer>}
*/
static toBuffer(stream: Readable | Duplex): Promise<Buffer> {
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 };