diff --git a/browser.js b/browser.js new file mode 100644 index 0000000..5c9ddad --- /dev/null +++ b/browser.js @@ -0,0 +1,5 @@ +var WritableStream = require('stream-browserify').Writable; +var createBlobStream = require('./create-blob-stream'); + +module.exports = createBlobStream(WritableStream); + diff --git a/create-blob-stream.js b/create-blob-stream.js new file mode 100644 index 0000000..24a859b --- /dev/null +++ b/create-blob-stream.js @@ -0,0 +1,50 @@ +var inherits = require("inherits"); +var Blob = require("blob"); +var URL = global.URL || global.webkitURL || global.mozURL; + +function createBlobStream(WritableStream) { + function BlobStream() { + if (!(this instanceof BlobStream)) return new BlobStream(); + + WritableStream.call(this); + this._chunks = []; + this._blob = null; + this.length = 0; + } + + inherits(BlobStream, WritableStream); + + BlobStream.prototype._write = function (chunk, encoding, callback) { + // convert chunks to Uint8Arrays (e.g. Buffer when array fallback is being used) + if (!(chunk instanceof Uint8Array)) chunk = new Uint8Array(chunk); + + this.length += chunk.length; + this._chunks.push(chunk); + callback(); + }; + + BlobStream.prototype.toBlob = function (type) { + type = type || "application/octet-stream"; + + // cache the blob if needed + if (!this._blob) { + this._blob = new Blob(this._chunks, { + type: type, + }); + + this._chunks = []; // free memory + } + + // if the cached blob's type doesn't match the requested type, make a new blob + if (this._blob.type !== type) this._blob = new Blob([this._blob], { type: type }); + + return this._blob; + }; + + BlobStream.prototype.toBlobURL = function (type) { + return URL.createObjectURL(this.toBlob(type)); + }; + return BlobStream; +} + +module.exports = createBlobStream; diff --git a/index.js b/index.js index e82b492..e8a679d 100644 --- a/index.js +++ b/index.js @@ -1,51 +1,4 @@ var WritableStream = require('stream').Writable; -var util = require('util'); -var Blob = require('blob'); -var URL = global.URL || global.webkitURL || global.mozURL; +var createBlobStream = require('./create-blob-stream'); -function BlobStream() { - if (!(this instanceof BlobStream)) - return new BlobStream; - - WritableStream.call(this); - this._chunks = []; - this._blob = null; - this.length = 0; -} - -util.inherits(BlobStream, WritableStream); - -BlobStream.prototype._write = function(chunk, encoding, callback) { - // convert chunks to Uint8Arrays (e.g. Buffer when array fallback is being used) - if (!(chunk instanceof Uint8Array)) - chunk = new Uint8Array(chunk); - - this.length += chunk.length; - this._chunks.push(chunk); - callback(); -}; - -BlobStream.prototype.toBlob = function(type) { - type = type || 'application/octet-stream'; - - // cache the blob if needed - if (!this._blob) { - this._blob = new Blob(this._chunks, { - type: type - }); - - this._chunks = []; // free memory - } - - // if the cached blob's type doesn't match the requested type, make a new blob - if (this._blob.type !== type) - this._blob = new Blob([this._blob], { type: type }); - - return this._blob; -}; - -BlobStream.prototype.toBlobURL = function(type) { - return URL.createObjectURL(this.toBlob(type)); -}; - -module.exports = BlobStream; +module.exports = createBlobStream(WritableStream); diff --git a/package.json b/package.json index 20424f0..a8c55cf 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.3", "description": "A Node-style writable stream for HTML5 Blobs", "main": "index.js", + "browser": "browser.js", "directories": { "test": "test" }, @@ -21,7 +22,9 @@ }, "homepage": "https://github.com/devongovett/blob-stream", "dependencies": { - "blob": "0.0.4" + "blob": "0.0.4", + "inherits": "^2.0.4", + "stream-browserify": "^3.0.0" }, "devDependencies": { "tape": "^2.12.3"