This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
109 lines (91 loc) · 2.86 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict'
const encodings = require('./lib/encodings')
const rangeOptions = new Set(['lt', 'gt', 'lte', 'gte'])
module.exports = Codec
function Codec (opts) {
if (!(this instanceof Codec)) {
return new Codec(opts)
}
this.opts = opts || {}
this.encodings = encodings
}
Codec.prototype._encoding = function (encoding) {
if (typeof encoding === 'string') encoding = encodings[encoding]
if (!encoding) encoding = encodings.id
return encoding
}
Codec.prototype._keyEncoding = function (opts, batchOpts) {
return this._encoding((batchOpts && batchOpts.keyEncoding) ||
(opts && opts.keyEncoding) ||
this.opts.keyEncoding)
}
Codec.prototype._valueEncoding = function (opts, batchOpts) {
return this._encoding((batchOpts && (batchOpts.valueEncoding || batchOpts.encoding)) ||
(opts && (opts.valueEncoding || opts.encoding)) ||
(this.opts.valueEncoding || this.opts.encoding))
}
Codec.prototype.encodeKey = function (key, opts, batchOpts) {
return this._keyEncoding(opts, batchOpts).encode(key)
}
Codec.prototype.encodeValue = function (value, opts, batchOpts) {
return this._valueEncoding(opts, batchOpts).encode(value)
}
Codec.prototype.decodeKey = function (key, opts) {
return this._keyEncoding(opts).decode(key)
}
Codec.prototype.decodeValue = function (value, opts) {
return this._valueEncoding(opts).decode(value)
}
Codec.prototype.encodeBatch = function (ops, opts) {
return ops.map((_op) => {
const op = {
type: _op.type,
key: this.encodeKey(_op.key, opts, _op)
}
if (this.keyAsBuffer(opts, _op)) op.keyEncoding = 'binary'
if (_op.prefix) op.prefix = _op.prefix
if ('value' in _op) {
op.value = this.encodeValue(_op.value, opts, _op)
if (this.valueAsBuffer(opts, _op)) op.valueEncoding = 'binary'
}
return op
})
}
Codec.prototype.encodeLtgt = function (ltgt) {
const ret = {}
for (const key of Object.keys(ltgt)) {
if (key === 'start' || key === 'end') {
throw new Error('Legacy range options ("start" and "end") have been removed')
}
ret[key] = rangeOptions.has(key)
? this.encodeKey(ltgt[key], ltgt)
: ltgt[key]
}
return ret
}
Codec.prototype.createStreamDecoder = function (opts) {
if (opts.keys && opts.values) {
return (key, value) => {
return {
key: this.decodeKey(key, opts),
value: this.decodeValue(value, opts)
}
}
} else if (opts.keys) {
return (key) => {
return this.decodeKey(key, opts)
}
} else if (opts.values) {
return (_, value) => {
return this.decodeValue(value, opts)
}
} else {
return function () {}
}
}
Codec.prototype.keyAsBuffer = function (opts) {
return this._keyEncoding(opts).buffer
}
Codec.prototype.valueAsBuffer = function (opts) {
return this._valueEncoding(opts).buffer
}