fetch in chunks
v1.3.0
並列ダウンロード、進行状況の追跡、および中断の要求をサポートする、大きなファイルをチャンクでフェッチするためのユーティリティ。
npm を使用してパッケージをインストールします。
npm install フェッチインチャンク
「fetch-in-chunks」から fetchInChunks をインポートします。
非同期関数 fetchInChunks(url, options = {})
url
( string
): ダウンロードするファイルの URL。
options
( object
、Optional): 追加のオプションを含むオブジェクト。
options.chunkSize
( number
、デフォルト: 5 * 1024 * 1024
): ダウンロードする各チャンクのサイズ (バイト単位)。
options.maxParallelRequests
( number
、デフォルト: 1
): 並行してダウンロードするチャンクの数。
options.progressCallback
( function
、オプション): ダウンロードされたバイト数とファイルの合計サイズを使用して呼び出されるコールバック関数。
options.signal
( AbortSignal
、オプション): ダウンロードを中止するために使用できるAbortSignal
オブジェクト。
Promise<Blob>
: ダウンロードされたファイルを含むBlob
に解決される Promise。
import fetchInChunks from 'fetch-in-chunks';async function downloadFile() { try {const blob = await fetchInChunks('https://example.com/largefile.zip');return blob; catch (エラー) {console.error('ファイル取得エラー:', error); }}ダウンロードファイル();
import fetchInChunks from 'fetch-in-chunks';async function downloadFileWithProgress() { try {const blob = await fetchInChunks('https://example.com/largefile.zip', { progressCallback: (ダウンロード済み, 合計) => {console.log(`ダウンロード済み ${((ダウンロード済み / 合計) * 100) .toFixed(2)}%`); },});blob を返します。 catch (エラー) {console.error('ファイル取得エラー:', error); }}downloadFileWithProgress();
AbortController
を使用する場合import fetchInChunks from 'fetch-in-chunks';async function downloadFileWithAbort() { const コントローラ = new AbortController(); const シグナル = コントローラー.シグナル; try {const blob = await fetchInChunks('https://example.com/largefile.zip', { signal,});return blob; } catch (エラー) {if (error.name === 'AbortError') { console.log('ダウンロードが中止されました');} else { console.error('ファイル取得エラー:', error);} } // いつでもダウンロードを中止するには コントローラー.abort();}
このプロジェクトは、Apache 2.0 ライセンスに基づいてライセンスされています。詳細については、 LICENSE
ファイルを参照してください。