forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.extbuffer.js
More file actions
executable file
·79 lines (69 loc) · 2.34 KB
/
23.extbuffer.js
File metadata and controls
executable file
·79 lines (69 loc) · 2.34 KB
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
#!/usr/bin/env mocha -R spec
/*jshint -W053 */
var assert = require("assert");
var msgpackJS = "../index";
var isBrowser = ("undefined" !== typeof window);
var msgpack = isBrowser && window.msgpack || require(msgpackJS);
var TITLE = __filename.replace(/^.*\//, "");
var HAS_UINT8ARRAY = ("undefined" !== typeof Uint8Array);
describe(TITLE, function() {
it("ExtBuffer (0x00)", function() {
testExtBuffer(0);
});
it("ExtBuffer (0x20-0xFF)", function() {
for (var i = 32; i < 256; i++) {
testExtBuffer(i);
}
});
it("ExtBuffer Array (0x20-0xFF)", function() {
for (var i = 32; i < 256; i++) {
testExtBufferArray(i);
}
});
function testExtBuffer(type) {
// fixext 8 -- 0xd7
var header = new Buffer([0xd7, type]);
var content = new Buffer(8);
for (var i = 0; i < 8; i++) {
content[i] = (type + i) & 0x7F;
}
var source = Buffer.concat([header, content]);
var decoded = msgpack.decode(source);
assert.equal(decoded.type, type);
assert.equal(decoded.buffer.length, content.length);
assert.deepEqual(toArray(decoded.buffer), toArray(content));
var encoded = msgpack.encode(decoded);
assert.deepEqual(toArray(encoded), toArray(source));
}
// Unpack and re-pack an array of extension types.
// Tests, among other things, that the right number of bytes are
// consumed with each ext type read.
function testExtBufferArray(type) {
function content(j) {
var x = j * type;
return Buffer([x & 0x7F, (x + 1) & 0x7F]);
}
// fixarray len 10
var arrayHeader = new Buffer([0x9a]);
var fullBuffer = arrayHeader;
for (var j = 0; j < 10; j++) {
// fixext 2 -- 0xd5
var header = new Buffer([0xd5, type]);
fullBuffer = Buffer.concat([fullBuffer, header, content(j)]);
}
var decoded = msgpack.decode(fullBuffer);
assert.equal(true, decoded instanceof Array);
assert.equal(decoded.length, 10);
for (j = 0; j < 10; j++) {
assert.equal(decoded[j].type, type);
assert.equal(decoded[j].buffer.length, 2);
assert.deepEqual(decoded[j].buffer, content(j));
}
var encoded = msgpack.encode(decoded);
assert.deepEqual(encoded, fullBuffer);
}
});
function toArray(array) {
if (HAS_UINT8ARRAY && array instanceof ArrayBuffer) array = new Uint8Array(array);
return Array.prototype.slice.call(array);
}