This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
blob.js
101 lines (93 loc) · 2.45 KB
/
blob.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Simple Blob and FileReader shim that does enough of an impression of the W3C
* File API that our unit tests can pass.
*
* Key differences:
* - Blob does not snapshot mutable TypedArray or ArrayBuffer instances passed
* in.
* - Blob surfaces helper logic/properties with underscore-prefixed names.
* - FileReader is not fully implemented and its events are bare bones.
**/
function Blob(parts, properties) {
this._parts = parts;
var size = 0;
parts.forEach(function(part) {
if (part instanceof Blob)
size += part.size;
else if (part instanceof ArrayBuffer)
size += part.byteLength;
else
size += part.length;
});
this.size = size;
this.type = properties ? properties.type : null;
};
exports.Blob = Blob;
Blob.prototype = {
_asArrayBuffer: function() {
var buffer = new ArrayBuffer(this.size);
var u8 = new Uint8Array(buffer);
var offset = 0;
this._parts.forEach(function(part) {
var sub8;
if (part instanceof Blob) {
var subarr = part._asArrayBuffer();
sub8 = new Uint8Array(subarr);
u8.set(sub8, offset);
offset += sub8.length;
}
else if (typeof(part) === 'string') {
var encoder = new TextEncoder('utf-8');
sub8 = encoder.encode(part);
u8.set(sub8, offset);
offset += sub8.length;
}
else if (part instanceof ArrayBuffer) {
sub8 = new Uint8Array(part);
u8.set(sub8, offset);
offset += sub8.length;
}
else {
u8.set(part, offset);
offset += part.length;
}
});
return buffer;
}
};
function FileReader() {
this.error = null;
this.readyState = this.EMPTY;
this.result = null;
this.onabort = null;
this.onerror = null;
this.onload = null;
this.onloadend = null;
this.onloadstart = null;
this.onprogress = null;
}
exports.FileReader = FileReader;
FileReader.prototype = {
EMPTY: 0,
LOADING: 1,
DONE: 2,
readAsArrayBuffer: function(blob) {
process.nextTick(function() {
var event = { target: this };
if (this.onload)
this.onload(event);
if (this.onloadend)
this.onloadend(event);
}.bind(this));
this.result = blob._asArrayBuffer();
},
readAsBinaryString: function(blob) {
throw new Error('not implemented');
},
readAsDataURL: function(blob) {
throw new Error('not implemented');
},
readAsText: function(blob, encoding) {
throw new Error('not implemented');
},
};