-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.js
91 lines (73 loc) · 2.31 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict'
const Buffer = require('safe-buffer').Buffer
const assert = require('assert')
const bl = require('bl')
const streams = require('./lib/streams')
const buildDecode = require('./lib/decoder')
const buildEncode = require('./lib/encoder')
const IncompleteBufferError = require('./lib/helpers.js').IncompleteBufferError
const DateCodec = require('./lib/codecs/DateCodec')
function msgpack (options) {
const encodingTypes = []
const decodingTypes = new Map()
options = options || {
forceFloat64: false,
compatibilityMode: false,
// if true, skips encoding Dates using the msgpack
// timestamp ext format (-1)
disableTimestampEncoding: false,
preferMap: false,
// options.protoAction: 'error' (default) / 'remove' / 'ignore'
protoAction: 'error'
}
decodingTypes.set(DateCodec.type, DateCodec.decode)
if (!options.disableTimestampEncoding) {
encodingTypes.push(DateCodec)
}
function registerEncoder (check, encode) {
assert(check, 'must have an encode function')
assert(encode, 'must have an encode function')
encodingTypes.push({ check, encode })
return this
}
function registerDecoder (type, decode) {
assert(type >= 0, 'must have a non-negative type')
assert(decode, 'must have a decode function')
decodingTypes.set(type, decode)
return this
}
function register (type, constructor, encode, decode) {
assert(constructor, 'must have a constructor')
assert(encode, 'must have an encode function')
assert(type >= 0, 'must have a non-negative type')
assert(decode, 'must have a decode function')
function check (obj) {
return (obj instanceof constructor)
}
function reEncode (obj) {
const buf = bl()
const header = Buffer.allocUnsafe(1)
header.writeInt8(type, 0)
buf.append(header)
buf.append(encode(obj))
return buf
}
this.registerEncoder(check, reEncode)
this.registerDecoder(type, decode)
return this
}
return {
encode: buildEncode(encodingTypes, options),
decode: buildDecode(decodingTypes, options),
register,
registerEncoder,
registerDecoder,
encoder: streams.encoder,
decoder: streams.decoder,
// needed for levelup support
buffer: true,
type: 'msgpack5',
IncompleteBufferError
}
}
module.exports = msgpack