-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDataUtils.js
159 lines (101 loc) · 3.57 KB
/
DataUtils.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const base64DataUrl = /^data:[^;]*;base64,([a-zA-Z0-9+/]+={0,2})$/;
export function areEqualBuffers(a, b) {
if (a.byteLength !== b.byteLength)
return false;
return areEqualViews(new Uint8View(a), new Uint8View(b));
}
export function areEqualViews(a, b) {
if (a.length !== b.length)
return false;
for (let t = 0, T = a.length; t < T; ++t)
if (a[t] !== b[t])
return false;
return true;
}
export function nodeToUint8(... buffers) {
let totalByteLength = buffers.reduce((sum, buffer) => sum + buffer.length, 0);
let array = new Uint8Array(totalByteLength);
let offset = 0;
for (let buffer of buffers) {
for (let t = 0, T = array.length; t < T; ++t) {
array[offset++] = buffer[t];
}
}
return array;
}
export function binaryStringToUint8(... strings) {
let totalByteLength = strings.reduce((sum, string) => sum + string.length, 0);
let array = new Uint8Array(totalByteLength);
let offset = 0;
for (let string of strings) {
for (let t = 0, T = string.length; t < T; ++t) {
array[offset++] = string.charCodeAt(t);
}
}
return array;
}
export function base64ToUint8(... strings) {
let isBrowser = typeof window !== `undefined`;
if (isBrowser) {
return binaryStringToUint8(... strings.map(string => atob(string)));
} else {
return nodeToUint8(... strings.map(string => new Buffer(string, `base64`)));
}
}
export function fetchArrayBuffer(path) {
return new Promise((resolve, reject) => {
let isBlob = typeof Blob !== `undefined` && path instanceof Blob;
let isDataURI = typeof path === `string` && path.match(base64DataUrl);
let isBrowser = typeof window !== `undefined`;
let isWeb = isBrowser || /^(https?:\/\/|blob:)/.test(path);
if (!isWeb && path.indexOf(`:`) !== -1)
throw new Error(`Invalid protocol`);
if (isBlob) {
let fileReader = new FileReader();
fileReader.addEventListener(`load`, e => {
resolve(e.target.result);
});
fileReader.addEventListener(`error`, e => {
reject();
});
fileReader.readAsArrayBuffer(path);
} else if (isDataURI) {
resolve(base64ToUint8(isDataURI[1]).buffer);
} else if (isBrowser) {
let xhr = new XMLHttpRequest();
xhr.open(`GET`, path, true);
xhr.responseType = `arraybuffer`;
xhr.onload = () => {
resolve(xhr.response);
};
xhr.onerror = () => {
reject(xhr.status);
};
xhr.send(null);
} else if (isWeb) {
let protocol = path.substr(0, path.indexOf(`:`));
let web = eval(`require(protocol /* http or https */)`);
let buffers = [ ];
web.get(path, res => {
res.on(`data`, chunk => {
buffers.push(chunk);
});
res.on(`error`, err => {
reject(err.message);
});
res.on(`end`, () => {
resolve(nodeToUint8(... buffers).buffer);
});
});
} else {
let fs = eval(`require('fs')`);
fs.readFile(path, (err, buffer) => {
if (err) {
reject(err);
} else {
resolve(nodeToUint8(buffer).buffer);
}
});
}
});
}