Downldr
A simple downloader which prevents writing invalid files:
- Will throw an error in case of non 2xx status code
- Allows file type filtering
Install
npm install downldr
API
downldr(input, [options])
Returns a PassThrough stream
with some extra events.
complete
: When the target or returned stream is fully written.type
: Triggered when the file type is available &filter
function is not passed or returnedtrue
.abort
: When the request is aborted.
input
Type: string
/ object (request options)
You can pass either an URL
or an object that will be passed directly to request.
options (optional)
-
ignoreStatus
- Iftrue
it will ignore non2xx
status code, defaults tofalse
-
target
- Takes afunction
or aWritable Stream
. When passed the response will be piped into this stream. If it's afunction
the file will be passed extension if available & must return aWritable Stream
. The function will only be executed once the download is valid:2xx
status code & wasn't filtered by thefilter
function. -
filter
- which takes afunction
that should returnfalse
value if the download should be aborted.
This function will be triggered with the first chunk, and uses file-type to detect the file type.
{ // - type.mime may be null if `file-type` fails to detect the type // - type.contentType will always contain the response Conten-Type header // but the Content-Type header is not always // accurate; A video with png extension return typemime === 'image/jpeg'; // Only accepts jpeg}
If the file type is not in this list type.mime
will be undefined
, but you can do the checks either using type.contentType
or the first chunk
Note: type.mime
may also be undefined
for: docx
/pptx
/xlsx
The exception is detection of
docx
,pptx
, andxlsx
which potentially requires reading the whole file.
const downldr = ;
downldr().abort()
This method will abort the current download. It uses request.abort()
const downldr = ; const req = ; req;
.promise(input, options)
A convenient Promise
wrapper around downldr
.
options.target
is required, since .pipe
isn't available.
await downldr;
Will resolve
once target
is fully written, and reject
if an error occurs.
Examples
File type filter
const downldr = ;
Create stream on success
When saving the file to disk, a common practice is:
read
But if the Readable
stream fails, we end up with an empty file. To avoid that downldr
can take a target
option (function
) which will be executed only when it's time to actually write to the stream.
;
Set request Content-Type
const downldr = ; app;
License
ISC