Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80547 views
1
'use strict';
2
3
4
var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
5
(typeof Uint16Array !== 'undefined') &&
6
(typeof Int32Array !== 'undefined');
7
8
9
exports.assign = function (obj /*from1, from2, from3, ...*/) {
10
var sources = Array.prototype.slice.call(arguments, 1);
11
while (sources.length) {
12
var source = sources.shift();
13
if (!source) { continue; }
14
15
if (typeof(source) !== 'object') {
16
throw new TypeError(source + 'must be non-object');
17
}
18
19
for (var p in source) {
20
if (source.hasOwnProperty(p)) {
21
obj[p] = source[p];
22
}
23
}
24
}
25
26
return obj;
27
};
28
29
30
// reduce buffer size, avoiding mem copy
31
exports.shrinkBuf = function (buf, size) {
32
if (buf.length === size) { return buf; }
33
if (buf.subarray) { return buf.subarray(0, size); }
34
buf.length = size;
35
return buf;
36
};
37
38
39
var fnTyped = {
40
arraySet: function (dest, src, src_offs, len, dest_offs) {
41
if (src.subarray && dest.subarray) {
42
dest.set(src.subarray(src_offs, src_offs+len), dest_offs);
43
return;
44
}
45
// Fallback to ordinary array
46
for(var i=0; i<len; i++) {
47
dest[dest_offs + i] = src[src_offs + i];
48
}
49
},
50
// Join array of chunks to single array.
51
flattenChunks: function(chunks) {
52
var i, l, len, pos, chunk, result;
53
54
// calculate data length
55
len = 0;
56
for (i=0, l=chunks.length; i<l; i++) {
57
len += chunks[i].length;
58
}
59
60
// join chunks
61
result = new Uint8Array(len);
62
pos = 0;
63
for (i=0, l=chunks.length; i<l; i++) {
64
chunk = chunks[i];
65
result.set(chunk, pos);
66
pos += chunk.length;
67
}
68
69
return result;
70
}
71
};
72
73
var fnUntyped = {
74
arraySet: function (dest, src, src_offs, len, dest_offs) {
75
for(var i=0; i<len; i++) {
76
dest[dest_offs + i] = src[src_offs + i];
77
}
78
},
79
// Join array of chunks to single array.
80
flattenChunks: function(chunks) {
81
return [].concat.apply([], chunks);
82
}
83
};
84
85
86
// Enable/Disable typed arrays use, for testing
87
//
88
exports.setTyped = function (on) {
89
if (on) {
90
exports.Buf8 = Uint8Array;
91
exports.Buf16 = Uint16Array;
92
exports.Buf32 = Int32Array;
93
exports.assign(exports, fnTyped);
94
} else {
95
exports.Buf8 = Array;
96
exports.Buf16 = Array;
97
exports.Buf32 = Array;
98
exports.assign(exports, fnUntyped);
99
}
100
};
101
102
exports.setTyped(TYPED_OK);
103