Skip to content
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
5 changes: 5 additions & 0 deletions browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var WritableStream = require('stream-browserify').Writable;
var createBlobStream = require('./create-blob-stream');

module.exports = createBlobStream(WritableStream);

50 changes: 50 additions & 0 deletions create-blob-stream.js
Original file line number Diff line number Diff line change
@@ -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;
51 changes: 2 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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"
Expand Down