-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
51 lines (39 loc) · 1.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var WritableStream = require('stream').Writable;
var util = require('util');
var Blob = require('blob');
var URL = global.URL || global.webkitURL || global.mozURL;
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;