From 7de3255a45e0dedcd74dff0a67f3d06a31025349 Mon Sep 17 00:00:00 2001 From: David Wheatley Date: Fri, 22 Dec 2023 03:47:38 +0000 Subject: [PATCH] fix: batch network requests in `fetchAudio` --- src/crunker.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/crunker.ts b/src/crunker.ts index 64c3725..8f990ce 100644 --- a/src/crunker.ts +++ b/src/crunker.ts @@ -58,9 +58,25 @@ export default class Crunker { } /** - * Asynchronously fetches multiple audio files and returns an array of AudioBuffers. + * Asynchronously fetches multiple audio files and returns an array of AudioBuffers, + * splitting into groups of 200 files to avoid resource exhaustion. */ async fetchAudio(...filepaths: CrunkerInputTypes[]): Promise { + const buffers: AudioBuffer[] = []; + const groups = Math.ceil(filepaths.length / 200); + + for (let i = 0; i < groups; i++) { + const group = filepaths.slice(i * 200, (i + 1) * 200); + buffers.push(...(await this._fetchAudio(...group))); + } + + return buffers; + } + + /** + * Asynchronously fetches multiple audio files and returns an array of AudioBuffers. + */ + private async _fetchAudio(...filepaths: CrunkerInputTypes[]): Promise { return await Promise.all( filepaths.map(async (filepath) => { let buffer: ArrayBuffer;