blob-to-buffer
Advanced tools
Comparing version 1.2.1 to 1.2.2
33
index.js
@@ -0,17 +1,34 @@ | ||
/* global Blob, FileReader */ | ||
// arraybuffer -> buffer without copy | ||
var toBuffer = require('typedarray-to-buffer') | ||
module.exports = function blobToBuffer (blob, cb) { | ||
if (typeof Blob === 'undefined' || !(blob instanceof Blob)) | ||
if (typeof Blob === 'undefined' || !(blob instanceof Blob)) { | ||
throw new Error('first argument must be a Blob') | ||
if (typeof cb !== 'function') | ||
} | ||
if (typeof cb !== 'function') { | ||
throw new Error('second argument must be a function') | ||
} | ||
var reader = new FileReader() | ||
reader.addEventListener('load', function (e) { | ||
// uint8array -> buffer without copy | ||
var buffer = toBuffer(new Uint8Array(e.target.result)) | ||
cb(null, buffer) | ||
}) | ||
reader.addEventListener('error', cb) | ||
function onLoad (e) { | ||
unregisterEvents() | ||
cb(null, toBuffer(e.target.result)) | ||
} | ||
function onError (err) { | ||
unregisterEvents() | ||
cb(err) | ||
} | ||
function unregisterEvents () { | ||
reader.removeEventListener('load', onLoad) | ||
reader.removeEventListener('error', onError) | ||
} | ||
reader.addEventListener('load', onLoad) | ||
reader.addEventListener('error', onError) | ||
reader.readAsArrayBuffer(blob) | ||
} |
{ | ||
"name": "blob-to-buffer", | ||
"description": "Convert a Blob to a Buffer", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"author": "Feross Aboukhadijeh <feross@feross.org> (http://feross.org/)", | ||
@@ -13,2 +13,3 @@ "bugs": { | ||
"devDependencies": { | ||
"standard": "^3.8.0", | ||
"tape": "^4.0.0", | ||
@@ -32,3 +33,3 @@ "zuul": "^3.0.0" | ||
"scripts": { | ||
"test": "zuul -- test/*.js" | ||
"test": "standard && zuul -- test/*.js" | ||
}, | ||
@@ -35,0 +36,0 @@ "testling": { |
@@ -0,1 +1,3 @@ | ||
/* global Blob */ | ||
var toBuffer = require('../') | ||
@@ -14,3 +16,2 @@ var test = require('tape') | ||
test('Callback error on invalid arguments', function (t) { | ||
@@ -17,0 +18,0 @@ t.throws(function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5723
47
3