Skip to content

convert anything to Buffer with promise #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
Say you're using the ['buffer'](https://github.com/feross/buffer) module on npm, or
[browserify](http://browserify.org/) and you're working with lots of binary data.

Unfortunately, sometimes the browser or someone else's API gives you a `Blob`. Silly
browser. How do you convert it to a `Buffer`?
How do you convert it to a `Buffer`?

Something with a goofy `FileReader` thingy... Time to Google for it yet again... There must be a better way!

***There is! Simply use this module!***
***Simply use this module!***

Works in the browser. This module is used by [WebTorrent](http://webtorrent.io)!

Expand All @@ -34,14 +31,12 @@ npm install blob-to-buffer
### usage

```js
var toBuffer = require('blob-to-buffer')
const toBuffer = require('blob-to-buffer')

// Get a Blob somehow...
var blob = new Blob([ new Uint8Array([1, 2, 3]) ], { type: 'application/octet-binary' })

toBuffer(blob, function (err, buffer) {
if (err) throw err
const blob = new Blob([ new Uint8Array([1, 2, 3]) ])

toBuffer(blob).then(buffer => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you switch this example to use await?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, i only used .then() as it was more cross platform compatible and many ppl just copy/paste code without using babel

buffer[0] // => 1
buffer.readUInt8(1) // => 2
})
Expand Down
22 changes: 2 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/* global Blob, FileReader */
/* global Response, Buffer */

module.exports = function blobToBuffer (blob, cb) {
if (typeof Blob === 'undefined' || !(blob instanceof Blob)) {
throw new Error('first argument must be a Blob')
}
if (typeof cb !== 'function') {
throw new Error('second argument must be a function')
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep this check in, but reverse the condition? It would be nice to help folks who are upgrading from 1.x and might leave the callback in by accident? It's similar to how we left the check in stream-to-blob when switching to promises: https://github.com/feross/stream-to-blob/blob/34d257b46d0f8c5d710194336d87f0623aa60fd4/index.js#L7

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm, don't follow... 😕

maybe close this PR just and let you do as you want?


const reader = new FileReader()

function onLoadEnd (e) {
reader.removeEventListener('loadend', onLoadEnd, false)
if (e.error) cb(e.error)
else cb(null, Buffer.from(reader.result))
}

reader.addEventListener('loadend', onLoadEnd, false)
reader.readAsArrayBuffer(blob)
}
module.exports = anything => new Response(anything).arrayBuffer().then(Buffer.from)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this function to be named so it shows up with a name in stack traces. Also, I know that Response can handle many types, but the point of this package is to convert a blob, so the argument should be called blob.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer.from won't work in a browser?

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "blob-to-buffer",
"description": "Convert a Blob to a Buffer",
"version": "1.2.8",
"version": "2.0.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't bump the version in a PR. This should happen when the new version is actually published.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

"author": {
"name": "Feross Aboukhadijeh",
"email": "[email protected]",
Expand Down
15 changes: 2 additions & 13 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@
const toBuffer = require('../')
const test = require('tape')

const blob = new Blob([new Uint8Array([1, 2, 3])], { type: 'application/octet-binary' })
const blob = new Blob([new Uint8Array([1, 2, 3])])

test('Basic tests', function (t) {
toBuffer(blob, function (err, buffer) {
if (err) throw err
toBuffer(blob).then(buffer => {
t.deepEqual(buffer, Buffer.from([1, 2, 3]))
t.end()
})
})

test('Callback error on invalid arguments', function (t) {
t.throws(function () {
toBuffer({ blah: 1 }, function () {})
})
t.throws(function () {
toBuffer(blob)
})
t.end()
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this test removed? What happens when an invalid argument like an object is passed in?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response is able to accept many things, Typed arrays, ArrayBuffer, strings, Blob/files, FormData, URLSearchParams, ReadableStream, and even DataView and convert it to a ArrayBuffer using new Response(x).arrayBuffer()

It would be silly to just restrict it to only Blobs when you can accept many more types of stuff and let it be more loose and be used for more than just Blobs.

anything not understood by the Response constructor will be casted to a string using the toString function or secondly String(x)

so what happens when you do

  • await new Response({}).text() is that you get "[object Object]"
  • await new Response(new Date()).text() yields "Sat Aug 24 2019 00:33:59 GMT+0200 (centraleuropeisk sommartid)" (since it has a toSting function)
  • await new Response({toString() { return 'abc'}}).text() yields "abc" even doe it's a plain object

So it's a bit hard to get a error when Response can accept literally anything

So it's no longer just blob-to-buffer, it's now anything to buffer