Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
106 lines (85 loc) · 2.98 KB
/
Copy pathindex.js
File metadata and controls
106 lines (85 loc) · 2.98 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict'
const Buffer = require('safe-buffer').Buffer
const assert = require('assert')
const { DynamicBuffer } = require('@platformatic/dynamic-buffer')
const appendBuffer = require('./lib/helpers.js').appendBuffer
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 = Object.assign({
forceFloat64: false,
compatibilityMode: false,
// if true, skips encoding Dates using the msgpack
// timestamp ext format (-1)
disableTimestampEncoding: false,
preferMap: false,
maxDepth: 100,
protoAction: 'error'
}, options || {})
if (options.protoAction === undefined) options.protoAction = 'error'
if (options.protoAction !== 'error' && options.protoAction !== 'remove' && options.protoAction !== 'ignore') {
throw new TypeError('protoAction must be "error", "remove", or "ignore"')
}
validateMaxLength(options.maxArrayLength, 'maxArrayLength')
validateMaxLength(options.maxMapLength, 'maxMapLength')
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 = new DynamicBuffer()
const header = Buffer.allocUnsafe(1)
header.writeInt8(type, 0)
appendBuffer(buf, header)
appendBuffer(buf, 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
}
}
function validateMaxLength (value, name) {
if (value !== undefined && (!Number.isSafeInteger(value) || value < 0)) {
throw new TypeError(name + ' must be a non-negative safe integer')
}
}
module.exports = msgpack