11import { useCallback , useMemo , useRef , useState } from 'react' ;
2- import { DownloadFunction , IResolverProps , IUseDownloader , IWindowDownloaderEmbedded , TError } from './types' ;
2+ import {
3+ DownloadFunction ,
4+ IResolverProps ,
5+ IUseDownloader ,
6+ IWindowDownloaderEmbedded ,
7+ TError ,
8+ UseDownloaderOptions ,
9+ } from './types' ;
310
411export const resolver =
512 ( {
@@ -8,66 +15,66 @@ export const resolver =
815 setPercentageCallback,
916 setErrorCallback,
1017 } : IResolverProps ) =>
11- ( response : Response ) : Response => {
12- if ( ! response . ok ) {
13- throw Error ( `${ response . status } ${ response . type } ${ response . statusText } ` ) ;
14- }
18+ ( response : Response ) : Response => {
19+ if ( ! response . ok ) {
20+ throw Error ( `${ response . status } ${ response . type } ${ response . statusText } ` ) ;
21+ }
1522
16- if ( ! response . body ) {
17- throw Error ( 'ReadableStream not yet supported in this browser.' ) ;
18- }
23+ if ( ! response . body ) {
24+ throw Error ( 'ReadableStream not yet supported in this browser.' ) ;
25+ }
1926
20- const responseBody = response . body ;
27+ const responseBody = response . body ;
2128
22- const contentEncoding = response . headers . get ( 'content-encoding' ) ;
23- const contentLength = response . headers . get (
24- contentEncoding ? 'x-file-size' : 'content-length'
25- ) ;
29+ const contentEncoding = response . headers . get ( 'content-encoding' ) ;
30+ const contentLength = response . headers . get (
31+ contentEncoding ? 'x-file-size' : 'content-length'
32+ ) ;
2633
27- const total = parseInt ( contentLength || '0' , 10 ) ;
34+ const total = parseInt ( contentLength || '0' , 10 ) ;
2835
29- setSize ( ( ) => total ) ;
36+ setSize ( ( ) => total ) ;
3037
31- let loaded = 0 ;
38+ let loaded = 0 ;
3239
33- const stream = new ReadableStream < Uint8Array > ( {
34- start ( controller ) {
35- setControllerCallback ( controller ) ;
40+ const stream = new ReadableStream < Uint8Array > ( {
41+ start ( controller ) {
42+ setControllerCallback ( controller ) ;
3643
37- const reader = responseBody . getReader ( ) ;
44+ const reader = responseBody . getReader ( ) ;
3845
39- async function read ( ) : Promise < void > {
40- return reader
41- . read ( )
42- . then ( ( { done, value } ) => {
43- if ( done ) {
44- return controller . close ( ) ;
45- }
46+ async function read ( ) : Promise < void > {
47+ return reader
48+ . read ( )
49+ . then ( ( { done, value } ) => {
50+ if ( done ) {
51+ return controller . close ( ) ;
52+ }
4653
47- loaded += value ?. byteLength || 0 ;
54+ loaded += value ?. byteLength || 0 ;
4855
49- if ( value ) {
50- controller . enqueue ( value ) ;
51- }
56+ if ( value ) {
57+ controller . enqueue ( value ) ;
58+ }
5259
53- setPercentageCallback ( { loaded, total } ) ;
60+ setPercentageCallback ( { loaded, total } ) ;
5461
55- return read ( ) ;
56- } )
57- . catch ( ( error : Error ) => {
58- setErrorCallback ( error ) ;
59- reader . cancel ( 'Cancelled' ) ;
62+ return read ( ) ;
63+ } )
64+ . catch ( ( error : Error ) => {
65+ setErrorCallback ( error ) ;
66+ reader . cancel ( 'Cancelled' ) ;
6067
61- return controller . error ( error ) ;
62- } ) ;
63- }
68+ return controller . error ( error ) ;
69+ } ) ;
70+ }
6471
65- return read ( ) ;
66- } ,
67- } ) ;
72+ return read ( ) ;
73+ } ,
74+ } ) ;
6875
69- return new Response ( stream ) ;
70- } ;
76+ return new Response ( stream ) ;
77+ } ;
7178
7279export const jsDownload = (
7380 data : Blob ,
@@ -79,8 +86,13 @@ export const jsDownload = (
7986 type : mime || 'application/octet-stream' ,
8087 } ) ;
8188
82- if ( typeof ( window as unknown as IWindowDownloaderEmbedded ) . navigator . msSaveBlob !== 'undefined' ) {
83- return ( window as unknown as IWindowDownloaderEmbedded ) . navigator . msSaveBlob ( blob , filename ) ;
89+ if (
90+ typeof ( window as unknown as IWindowDownloaderEmbedded ) . navigator
91+ . msSaveBlob !== 'undefined'
92+ ) {
93+ return (
94+ window as unknown as IWindowDownloaderEmbedded
95+ ) . navigator . msSaveBlob ( blob , filename ) ;
8496 }
8597
8698 const blobURL =
@@ -105,7 +117,9 @@ export const jsDownload = (
105117 } , 200 ) ;
106118} ;
107119
108- export default function useDownloader ( ) : IUseDownloader {
120+ export default function useDownloader (
121+ options : UseDownloaderOptions = { }
122+ ) : IUseDownloader {
109123 const debugMode = process . env . REACT_APP_DEBUG_MODE ;
110124
111125 const [ elapsed , setElapsed ] = useState ( 0 ) ;
@@ -139,9 +153,12 @@ export default function useDownloader(): IUseDownloader {
139153 } ) ;
140154 } , [ ] ) ;
141155
142- const setControllerCallback = useCallback ( ( controller : ReadableStreamController < Uint8Array > | null ) => {
143- controllerRef . current = controller ;
144- } , [ ] ) ;
156+ const setControllerCallback = useCallback (
157+ ( controller : ReadableStreamController < Uint8Array > | null ) => {
158+ controllerRef . current = controller ;
159+ } ,
160+ [ ]
161+ ) ;
145162
146163 const closeControllerCallback = useCallback ( ( ) => {
147164 if ( controllerRef . current ) {
@@ -183,8 +200,9 @@ export default function useDownloader(): IUseDownloader {
183200 } , timeout ) ;
184201
185202 return fetch ( downloadUrl , {
203+ ...options ,
186204 method : 'GET' ,
187- signal : fetchController . signal
205+ signal : fetchController . signal ,
188206 } )
189207 . then ( resolverWithProgress )
190208 . then ( ( data ) => {
@@ -221,6 +239,7 @@ export default function useDownloader(): IUseDownloader {
221239 setControllerCallback ,
222240 setPercentageCallback ,
223241 setErrorCallback ,
242+ options ,
224243 ]
225244 ) ;
226245
0 commit comments