forked from KSDaemon/wampy.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMsgpackSerializer.js
More file actions
34 lines (25 loc) · 783 Bytes
/
Copy pathMsgpackSerializer.js
File metadata and controls
34 lines (25 loc) · 783 Bytes
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
import msgpack5 from 'msgpack5';
const msgpack = msgpack5();
export class MsgpackSerializer {
constructor () {
this.protocol = 'msgpack';
this.isBinary = true;
}
encode (data) {
return msgpack.encode(data);
}
decode (data) {
return new Promise(resolve => {
const type = data.constructor.name;
if (type === 'ArrayBuffer' || type === 'Buffer') {
resolve(msgpack.decode(new Uint8Array(data)));
} else {
const reader = new FileReader();
reader.onload = function () {
resolve(msgpack.decode(new Uint8Array(this.result)));
};
reader.readAsArrayBuffer(data);
}
});
}
}