-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathwrite-core.js
69 lines (57 loc) · 1.66 KB
/
write-core.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
// write-core.js
var ExtBuffer = require("./ext-buffer").ExtBuffer;
var ExtPacker = require("./ext-packer");
var WriteType = require("./write-type");
var CodecBase = require("./codec-base");
CodecBase.install({
addExtPacker: addExtPacker,
getExtPacker: getExtPacker,
init: init
});
exports.preset = init.call(CodecBase.preset);
function getEncoder(options) {
var writeType = WriteType.getWriteType(options);
return encode;
function encode(encoder, value) {
var func = writeType[typeof value];
if (!func) throw new Error("Unsupported type \"" + (typeof value) + "\": " + value);
func(encoder, value);
}
}
function init() {
var options = this.options;
this.encode = getEncoder(options);
if (options && options.preset) {
ExtPacker.setExtPackers(this);
}
return this;
}
function addExtPacker(etype, Class, packer) {
packer = CodecBase.filter(packer);
var name = Class.name;
if (name && name !== "Object") {
var packers = this.extPackers || (this.extPackers = {});
packers[name] = extPacker;
} else {
// fallback for IE
var list = this.extEncoderList || (this.extEncoderList = []);
list.unshift([Class, extPacker]);
}
function extPacker(value) {
if (packer) value = packer(value);
return new ExtBuffer(value, etype);
}
}
function getExtPacker(value) {
var packers = this.extPackers || (this.extPackers = {});
var c = value.constructor;
var e = c && c.name && packers[c.name];
if (e) return e;
// fallback for IE
var list = this.extEncoderList || (this.extEncoderList = []);
var len = list.length;
for (var i = 0; i < len; i++) {
var pair = list[i];
if (c === pair[0]) return pair[1];
}
}