npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fetch-blob

v4.0.0

Published

Blob & File implementation in Node.js, originally from node-fetch.

Downloads

22,173,290

Readme

fetch-blob

npm version build status coverage status install size

A Blob implementation in Node.js, originally from node-fetch.

Use the built-in Blob in Node.js 18 and later.

Installation

npm install fetch-blob

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 calling blob.stream() was replaced with whatwg streams - (Read "Differences from other blobs" for more info.)

  • 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 itself it 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 in from.js that wraps a file path into a blob-like item and read lazily (nodejs plans to implement this as well)
  • 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'
  const stream = Readable.from(blob.stream())

Usage

// Ways to import
import { Blob } from 'fetch-blob'
import { File } from 'fetch-blob/file.js'

const { Blob } = await import('fetch-blob')


// Ways to read the blob:
const blob = new Blob(['hello, world'])

await blob.text()
await blob.arrayBuffer()
for await (let chunk of  blob.stream()) { ... }
blob.stream().getReader().read()
blob.stream().getReader({mode: 'byob'}).read(view)

Blob part backed up by filesystem

fetch-blob/from.js comes packed with tools to convert any filepath into either a Blob or a File It 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 blob
import {File, Blob, blobFrom, blobFromSync, fileFrom, fileFromSync} from 'fetch-blob/from.js'

const fsFile = fileFromSync('./2-GiB-file.bin', 'application/octet-stream')
const fsBlob = await blobFrom('./2-GiB-file.mp4')

// Not a 4 GiB memory snapshot, just holds references
// points to where data is located on the disk
const blob = new Blob([fsFile, fsBlob, 'memory', new Uint8Array(10)])
console.log(blob.size) // ~4 GiB

blobFrom|blobFromSync|fileFrom|fileFromSync(path, [mimetype])

Creating a temporary file on the disk

(requires FinalizationRegistry - node v14.6)

When using both createTemporaryBlob and createTemporaryFile then you will write data to the temporary folder in their respective OS. The arguments can be anything that fsPromises.writeFile supports. NodeJS v14.17.0+ also supports writing (async)Iterable streams and passing in a AbortSignal, so both NodeJS stream and whatwg streams are supported. When the file have been written it will return a Blob/File handle with a references to this temporary location on the disk. When you no longer have a references to this 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'

const req = new Request('https://httpbin.org/image/png')
const res = await fetch(req)
const type = res.headers.get('content-type')
const signal = req.signal
let blob = await createTemporaryBlob(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 })

Creating Blobs backed up by other async sources

Our Blob & File class are more generic then any other polyfills in the way that it can accept any blob look-a-like item An example of this is that our blob implementation can be constructed with parts coming from BlobDataItem (aka a filepath) or from buffer.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 has Symbol.toStringTag, size, slice(), stream() methods (the stream method can be as simple as being a sync or async iterator that yields Uint8Arrays. If you then wrap it in our Blob or File new 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 the MDN documentation and tests for more details of how to use the Blob.