-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathext-packer.js
78 lines (64 loc) · 2.46 KB
/
ext-packer.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
// ext-packer.js
exports.setExtPackers = setExtPackers;
var Bufferish = require("./bufferish");
var Buffer = Bufferish.global;
var packTypedArray = Bufferish.Uint8Array.from;
var _encode;
var ERROR_COLUMNS = {name: 1, message: 1, stack: 1, columnNumber: 1, fileName: 1, lineNumber: 1};
function setExtPackers(codec) {
codec.addExtPacker(0x0E, Error, [packError, encode]);
codec.addExtPacker(0x01, EvalError, [packError, encode]);
codec.addExtPacker(0x02, RangeError, [packError, encode]);
codec.addExtPacker(0x03, ReferenceError, [packError, encode]);
codec.addExtPacker(0x04, SyntaxError, [packError, encode]);
codec.addExtPacker(0x05, TypeError, [packError, encode]);
codec.addExtPacker(0x06, URIError, [packError, encode]);
codec.addExtPacker(0x0A, RegExp, [packRegExp, encode]);
codec.addExtPacker(0x0B, Boolean, [packValueOf, encode]);
codec.addExtPacker(0x0C, String, [packValueOf, encode]);
codec.addExtPacker(0x0D, Date, [Number, encode]);
codec.addExtPacker(0x0F, Number, [packValueOf, encode]);
if ("undefined" !== typeof Uint8Array) {
codec.addExtPacker(0x11, Int8Array, packTypedArray);
codec.addExtPacker(0x12, Uint8Array, packTypedArray);
codec.addExtPacker(0x13, Int16Array, packTypedArray);
codec.addExtPacker(0x14, Uint16Array, packTypedArray);
codec.addExtPacker(0x15, Int32Array, packTypedArray);
codec.addExtPacker(0x16, Uint32Array, packTypedArray);
codec.addExtPacker(0x17, Float32Array, packTypedArray);
// PhantomJS/1.9.7 doesn't have Float64Array
if ("undefined" !== typeof Float64Array) {
codec.addExtPacker(0x18, Float64Array, packTypedArray);
}
// IE10 doesn't have Uint8ClampedArray
if ("undefined" !== typeof Uint8ClampedArray) {
codec.addExtPacker(0x19, Uint8ClampedArray, packTypedArray);
}
codec.addExtPacker(0x1A, ArrayBuffer, packTypedArray);
codec.addExtPacker(0x1D, DataView, packTypedArray);
}
if (Bufferish.hasBuffer) {
codec.addExtPacker(0x1B, Buffer, Bufferish.from);
}
}
function encode(input) {
if (!_encode) _encode = require("./encode").encode; // lazy load
return _encode(input);
}
function packValueOf(value) {
return (value).valueOf();
}
function packRegExp(value) {
value = RegExp.prototype.toString.call(value).split("/");
value.shift();
var out = [value.pop()];
out.unshift(value.join("/"));
return out;
}
function packError(value) {
var out = {};
for (var key in ERROR_COLUMNS) {
out[key] = value[key];
}
return out;
}