-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (144 loc) · 3.82 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const inspect = require('inspect-custom-symbol')
const nullStyle = Object.freeze({
stylize: Object.freeze(any => any)
})
function prepare (codec, type, name) {
if (!name) {
name = codec.name
}
return Object.freeze({
...codec,
name,
toString: inspectFn.bind(null, null, nullStyle),
[inspect]: inspectFn
})
function inspectFn (_, opts) {
return `[Codec ${opts.stylize(type, 'string')}|${opts.stylize(name, 'string')}]`
}
}
const b32Names = ['base32', 'base32c', 'base32h', 'base32hp', 'base32p']
let b32
function getB32 (name) {
let codec
return {
get () {
if (codec === undefined) {
if (b32 === undefined) {
b32 = require('base32-codecs')
}
codec = prepare(b32[name], 'string')
}
return codec
}
}
}
let base
const baseNames = ['ascii', 'base64', 'binary', 'hex', 'json', 'ndjson', 'ucs-2', 'ucs2', 'utf-8', 'utf16le', 'utf16-le', 'utf8']
function getBase (name) {
let codec
return {
get () {
if (codec === undefined) {
if (base === undefined) {
base = require('codecs')
}
const baseCodec = base(name)
codec = prepare(baseCodec, name === 'binary' ? 'Buffer' : name === 'json' || name === 'ndjson' ? 'any' : 'string')
}
return codec
}
}
}
const bigIntNames = ['bigIntLE', 'bigIntBE', 'bigIntQuick', 'bigUintLE', 'bigUintBE']
function getBigInt (name) {
let codec
return {
get () {
if (codec === undefined) {
codec = prepare(require('@consento/bigint-codec')[name], 'bigint')
}
return codec
}
}
}
const props = {}
for (const b32Name of b32Names) {
props[b32Name] = getB32(b32Name)
}
for (const baseName of baseNames) {
props[baseName] = getBase(baseName)
}
for (const bigIntName of bigIntNames) {
props[bigIntName] = getBigInt(bigIntName)
}
const byName = {}
let msgpack
props.msgpack = {
get () {
if (msgpack === undefined) {
msgpack = prepare(require('msgpack-codec'), 'any')
}
return msgpack
}
}
Object.defineProperties(byName, props)
function codecs (name, fallback) {
if (typeof name === 'object' && name !== null && typeof name.name === 'string' && typeof name.decode === 'function' && typeof name.encode === 'function') {
return name
}
const codec = byName[name]
if (codec !== undefined) {
return codec
}
if (fallback !== undefined && fallback !== null) {
return fallback
}
return byName.binary
}
const available = b32Names.concat(baseNames).concat(bigIntNames).concat(['msgpack']).sort()
codecs.available = available
codecs.has = codec => available.includes(codec)
codecs.bindMsgpack = (base, name) => {
if (msgpack === undefined) {
msgpack = prepare(require('msgpack-codec'), 'any')
}
return prepare({
encode (input, options) {
if (options) {
return msgpack.encode(input, { ...base.encode, ...options })
}
return msgpack.encode(input, base.encode)
},
decode (input, options) {
if (options) {
return msgpack.decode(input, { ...base.decode, ...options })
}
return msgpack.decode(input, base.decode)
}
}, 'any', name || 'msgpack-ext')
}
codecs[Symbol.iterator] = () => {
const nextName = available[Symbol.iterator]()
return {
next () {
while (true) {
const name = nextName.next()
if (name.done) {
return name
}
const value = byName[name.value]
if (value.name !== name.value) {
continue
}
return {
done: false,
value
}
}
}
}
}
codecs[inspect] = (_, { stylize }) => `function ${stylize('@consento/codecs', 'module')} (${available.map(name => stylize(name, 'special')).join('|')}): { name, encode, decode }`
codecs.prepare = prepare
Object.defineProperties(codecs, props)
module.exports = Object.freeze(codecs)