- Notifications
You must be signed in to change notification settings - Fork32
A Blob implementation in Node.js, originally from node-fetch.
License
node-fetch/fetch-blob
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A Blob implementation in Node.js, originally fromnode-fetch.
Use the built-inBlob
in Node.js 18 and later.
npm install fetch-blob
Upgrading from 2x to 3x
Updating from 2 to 3 should be a breeze since there is not many changes to the blob specification.The major cause of a major release is coding standards.- internal WeakMaps was replaced with private fields- internal Buffer.from was replaced with TextEncoder/Decoder- internal buffers was replaced with Uint8Arrays- CommonJS was replaced with ESM- The node stream returned by callingblob.stream()
was replaced with whatwg streams- (Read "Differences from other blobs" for more info.)
Differences from other Blobs
- Unlike NodeJS
buffer.Blob
(Added in: v15.7.0) and browser native Blob this polyfilled version can't be sent via PostMessage - This blob version is more arbitrary, it can be constructed with blob parts that isn't a instance of itselfit has to look and behave as a blob to be accepted as a blob part.
- The benefit of this is that you can create other types of blobs that don't contain any internal data that has to be read in other ways, such as the
BlobDataItem
created infrom.js
that wraps a file path into a blob-like item and read lazily (nodejs plans toimplement this as well)
- The benefit of this is that you can create other types of blobs that don't contain any internal data that has to be read in other ways, such as the
- The
blob.stream()
is the most noticeable differences. It returns a WHATWG stream now. to keep it as a node stream you would have to do:
import{Readable}from'stream'conststream=Readable.from(blob.stream())
// Ways to importimport{Blob}from'fetch-blob'import{File}from'fetch-blob/file.js'const{ Blob}=awaitimport('fetch-blob')// Ways to read the blob:constblob=newBlob(['hello, world'])awaitblob.text()awaitblob.arrayBuffer()forawait(letchunkofblob.stream()){ ...}blob.stream().getReader().read()blob.stream().getReader({mode:'byob'}).read(view)
fetch-blob/from.js
comes packed with tools to convert any filepath into either a Blob or a FileIt will not read the content into memory. It will only stat the file for last modified date and file size.
// The default export is sync and use fs.stat to retrieve size & last modified as a blobimport{File,Blob,blobFrom,blobFromSync,fileFrom,fileFromSync}from'fetch-blob/from.js'constfsFile=fileFromSync('./2-GiB-file.bin','application/octet-stream')constfsBlob=awaitblobFrom('./2-GiB-file.mp4')// Not a 4 GiB memory snapshot, just holds references// points to where data is located on the diskconstblob=newBlob([fsFile,fsBlob,'memory',newUint8Array(10)])console.log(blob.size)// ~4 GiB
blobFrom|blobFromSync|fileFrom|fileFromSync(path, [mimetype])
(requiresFinalizationRegistry - node v14.6)
When using bothcreateTemporaryBlob
andcreateTemporaryFile
then you will write data to the temporary folder in their respective OS.The arguments can be anything thatfsPromises.writeFile supports. NodeJSv14.17.0+ also supports writing (async)Iterable streams and passing in aAbortSignal, so both NodeJS stream and whatwg streams are supported. When thefile have been written it will return a Blob/File handle with a references tothis temporary location on the disk. When you no longer have a references tothis Blob/File anymore and it have been GC then it will automatically be deleted.
This files are also unlinked upon exiting the process.
import{createTemporaryBlob,createTemporaryFile}from'fetch-blob/from.js'constreq=newRequest('https://httpbin.org/image/png')constres=awaitfetch(req)consttype=res.headers.get('content-type')constsignal=req.signalletblob=awaitcreateTemporaryBlob(res.body,{ type, signal})// const file = createTemporaryBlob(res.body, 'img.png', { type, signal })blob=undefined// loosing references will delete the file from disk
createTemporaryBlob(data, { type, signal })
createTemporaryFile(data, FileName, { type, signal, lastModified })
Our Blob & File class are more generic then any other polyfills in the way that it can accept any blob look-a-like itemAn example of this is that our blob implementation can be constructed with parts coming fromBlobDataItem (aka a filepath) or frombuffer.Blob, It dose not have to implement all the methods - just enough that it can be read/understood by our Blob implementation. The minium requirements is that it hasSymbol.toStringTag
,size
,slice()
,stream()
methods (the stream methodcan be as simple as being a sync or async iterator that yields Uint8Arrays. If you then wrap it in our Blob or Filenew Blob([blobDataItem])
then you get all of the other methods that should be implemented in a blob or file (aka: text(), arrayBuffer() and type and a ReadableStream)
An example of this could be to create a file or blob like item coming from a remote HTTP request. Or from a DataBase
See theMDN documentation andtests for more details of how to use the Blob.
About
A Blob implementation in Node.js, originally from node-fetch.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors12
Uh oh!
There was an error while loading.Please reload this page.