Skip to content

[suggestion] send/respond with blob #4807

@jimmywarting

Description

@jimmywarting

NodeJS now has support for Blob's globally,
...earlier you had to load it from require('buffer').Blob

it would be cool / awesome if it where possible to respond with a Blob by doing something like

const str = `<h1>Hello World</h1>`
const blob = new Blob([str], { type: 'text/html' })
const file = new File([blob], 'index.html', { type: 'text/html' })

app.get('/', (req, res) => {
  res.send(blob) // or:
  res.download(blob, 'name.html')
  res.download(file) // name taken from file instead
})

Doing this would take care of

  1. Setting the response header content-type to the blob's type (only if content-type haven't been set manually)
  2. Setting the response header content-length to the blob's size
  3. and pipe the data from blob.stream() to the response
  4. if you used res.download(blob) then it would also add content-disposition attachment header

it's also looking like if node will at some point add a way of getting blobs from the filesystem also, but i don't know when.
ref: nodejs/node#39015

edit: NodeJS just shipped fs.openAsBlob(path, { type }) in v20+

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions