-
Notifications
You must be signed in to change notification settings - Fork 1
/
id.js
246 lines (208 loc) · 4.88 KB
/
id.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const varint = require('varint')
/**
* The number of bytes needed for a `ID` instance.
* @const
* @public
*/
const BYTES = 4
const zeroes = Buffer.alloc(BYTES)
function set(target, bytes) {
return Buffer.prototype.set.call(target, Buffer.from(bytes))
}
/**
* The `ID` class represents a container for 32 bits of characters, the
* concatenation of four printable ASCII character in the range ' ' (SP, 0x20)
* through '~' (0x7E). Spaces (0x20) cannot precede printing characters;
* trailing spaces are allowed. Control characters are forbidden.
* @class ID
* @extends Buffer (Uint8Array)
*/
class ID extends Uint8Array {
/**
* Overloads `Buffer.alloc()` for `ID` instances.
* @static
* @return {ID}
*/
static alloc() {
return new this()
}
/**
* Overloads `Buffer.from()` for `ID` instances.
* @static
* @return {ID}
*/
static from(id) {
return new this(id)
}
/**
* Returns an empty `ID` instance.
* @static
* @accessor
* @type {ID}
*/
static get EMPTY() {
return this.from(zeroes)
}
/**
* Normalizes bytes based on type and final encoding. Extending classes
* can overload this method for controlled normalization.
* @static
* @param {Buffer|String|Array|ID} bytes
* @param {?(String)} encoding
* @return {Buffer}
*/
static normalize(bytes, encoding) {
if ('number' === typeof bytes) {
bytes = Buffer.from(bytes.toString(16), 'hex')
}
bytes = Buffer.from(bytes, encoding)
if (bytes.length > BYTES) {
bytes = Buffer.from(varint.decode(bytes).toString(16), 'hex')
}
return bytes
}
/**
* Validate bytes for a `ID` instances. Extending classes
* can overload this method for controlled validation.
* @static
* @param {Buffer|String|Array|ID} bytes
* @param {?(String)} encoding
* @return {Boolean}
*/
static validate(bytes, encoding) {
const { normalize } = this
if (!bytes) {
return false
}
if ('object' === typeof bytes) {
if (null === Object.getPrototypeOf(bytes)) {
return false
}
if (Object.prototype === Object.getPrototypeOf(bytes)) {
return false
}
}
if ('function' === typeof bytes) {
return false
}
bytes = normalize.call(this, bytes, encoding)
// must be 4 bytes
if (BYTES !== bytes.length) {
return false
}
// spaces may not proceed ASCII characters in bytes
if (0x20 === bytes[0]) {
return false
}
// in the range of 0x20 - 0x7e, or null byte
for (const byte of bytes) {
if (0 !== byte && (byte < 0x20 || byte > 0x7e)) {
return false
}
}
return true
}
/**
* The number of bytes needed for a `ID` allocation.
* @static
* @accessor
* @type {Number}
*/
static get BYTES() {
return BYTES
}
/**
* `ID` class constructor.
* @param {?(Buffer|String|ID)} id
*/
constructor(id) {
super(BYTES)
this.set(id)
}
/**
* `true` if the state of the ID
* @accessor
* @type {Boolean
*/
get isValid() {
return 0 !== Buffer.compare(this.toBuffer(), ID.EMPTY)
}
/**
* Set id value on `ID` instances.
* @param {?(String|Buffer|ID|Uint8Array|Number)} id
* @return {Boolean}
*/
set(id) {
const { constructor } = this
const { validate, normalize } = constructor
if (validate.call(constructor, id)) {
id = normalize.call(constructor, id)
const tmp = Buffer.alloc(BYTES)
id.copy(tmp)
set(this, tmp)
return true
}
return false
}
/**
* Compare a buffer, string, ID, or array of bytes with the
* `ID` instances bytes.
* @param {String|ID|Buffer|Array} target
*/
compare(target, ...args) {
if ('string' === typeof target) {
target = Buffer.from(target)
} else if (Array.isArray(target)) {
target = Buffer.from(target)
}
return super.compare(target, ...args)
}
/**
* Convert `ID` instance directly to `Buffer`, using the same internal
* `ArrayBuffer` for this instance.
* @return {Buffer}
*/
toBuffer() {
return Buffer.from(this.buffer) // `this.buffer` is the `ArrayBuffer`
}
/**
* Convert `ID` instance to an `Array`.
* @return {Array}
*/
toArray() {
return Array.from(this.toBuffer())
}
}
// inherit `Buffer`, working around 'DEP0005'
Object.setPrototypeOf(ID.prototype, Buffer.prototype)
/**
* Encode a given `id` string or buffer.
* @param {Buffer|String|Array|ID} bytes
* @return {ID}
*/
function encode(id) {
return ID.from(id)
}
/**
* Decode a given `ID` instance into a id string.
* @param {ID|Buffer} id
* @return {String}
*/
function decode(id) {
return id.toString('utf8')
}
/**
* Returns the encoding length for a `ID` instance.
* @return {Number}
*/
function encodingLength(id) {
void id
return BYTES
}
/**
* Module exports.
*/
module.exports = {
BYTES, ID,
encode, decode, encodingLength
}