-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathid.js
More file actions
280 lines (233 loc) · 5.91 KB
/
Copy pathid.js
File metadata and controls
280 lines (233 loc) · 5.91 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
'use strict'
const { randomFillSync } = require('crypto')
const { channel } = require('dc-polyfill')
const UINT_MAX = 4_294_967_296
const data = new Uint8Array(8 * 8192)
const zeroId = new Uint8Array(8)
let batch = 0
channel('datadog:identity:update').subscribe(reseed)
// Internal representation of a trace or span ID.
class Identifier {
/** @type {number[] | Uint8Array} */
#buffer
/** @type {bigint | undefined} */
#bigInt
/** @type {string | undefined} */
#stringHex
/** @type {string | undefined} */
#stringDecimal
/**
* @param {string} value
* @param {number} [radix]
*/
constructor (value, radix = 16) {
this.#buffer = radix === 16
? createBuffer(value)
: fromString(value, radix)
}
/**
* @param {number} [radix]
* @returns {string}
*/
toString (radix = 16) {
if (radix === 16) {
this.#stringHex ??= Buffer.from(this.#buffer).toString('hex')
return this.#stringHex
}
if (radix === 10) {
this.#stringDecimal ??= toNumberString(this.#buffer, 10)
return this.#stringDecimal
}
return toNumberString(this.#buffer, radix)
}
/**
* @returns {bigint}
*/
toBigInt () {
this.#bigInt ??= Buffer.from(this.#buffer).readBigUInt64BE(0)
return this.#bigInt
}
/**
* @returns {number[] | Uint8Array}
*/
toBuffer () {
return this.#buffer
}
/**
* @returns {number[] | Uint8Array}
*/
toArray () {
if (this.#buffer.length === 8) {
return this.#buffer
}
return this.#buffer.slice(-8)
}
/**
* @returns {string}
*/
toJSON () {
return this.toString()
}
/**
* Returns the full hex trace ID. When this is a 64-bit identifier and `traceIdHigh`
* is provided, prepends it to form the 128-bit trace ID. Otherwise returns
* only this identifier's hex representation.
*
* @param {string | undefined} traceIdHigh - 16-char hex of the upper 64 bits, or undefined
* @returns {string}
*/
toTraceIdHex (traceIdHigh) {
if (traceIdHigh && this.#buffer.length <= 8) {
return traceIdHigh + this.toString(16)
}
return this.toString(16)
}
/**
* @param {Identifier} other
* @returns {boolean}
*/
equals (other) {
// Big-endian suffix compare: when buffers differ in length, only the
// rightmost `min(this.length, other.length)` bytes are checked.
for (let i = this.#buffer.length - 1, j = other.#buffer.length - 1; i >= 0 && j >= 0; i--, j--) {
if (this.#buffer[i] !== other.#buffer[j]) return false
}
return true
}
}
// Create a buffer, using an optional hexadecimal value if provided.
/**
* @param {string} value
* @returns {number[] | Uint8Array}
*/
function createBuffer (value) {
if (value === '0') return zeroId
if (!value) return pseudoRandom()
const size = Math.ceil(value.length / 16) * 16
const bytes = size / 2
const buffer = []
value = value.padStart(size, '0')
for (let i = 0; i < bytes; i++) {
buffer[i] = Number.parseInt(value.slice(i * 2, i * 2 + 2), 16)
}
return buffer
}
// Convert a numerical string to a buffer using the specified radix.
/**
* @param {string} str
* @param {number} raddix
* @returns {number[]}
*/
function fromString (str, raddix) {
const buffer = new Array(8)
const len = str.length
let pos = 0
let high = 0
let low = 0
if (str[0] === '-') pos++
const sign = pos
while (pos < len) {
const chr = Number.parseInt(str[pos++], raddix)
if (!(chr >= 0)) break // NaN
low = low * raddix + chr
high = high * raddix + Math.floor(low / UINT_MAX)
low %= UINT_MAX
}
if (sign) {
high = ~high
if (low) {
low = UINT_MAX - low
} else {
high++
}
}
writeUInt32BE(buffer, high, 0)
writeUInt32BE(buffer, low, 4)
return buffer
}
// Convert a buffer to a numerical string.
/**
* @param {number[] | Uint8Array} buffer
* @param {number} [radix]
* @returns {string}
*/
function toNumberString (buffer, radix) {
let high = readInt32(buffer, buffer.length - 8)
let low = readInt32(buffer, buffer.length - 4)
let str = ''
radix ||= 10
while (1) {
const mod = (high % radix) * UINT_MAX + low
high = Math.floor(high / radix)
low = Math.floor(mod / radix)
str = (mod % radix).toString(radix) + str
if (!high && !low) break
}
return str
}
// Simple pseudo-random 64-bit ID generator.
/**
* @returns {number[] | Uint8Array}
*/
function pseudoRandom () {
if (batch === 0) {
randomFillSync(data)
}
batch = (batch + 1) % 8192
const offset = batch * 8
return [
data[offset] & 0x7F, // only positive int64,
data[offset + 1],
data[offset + 2],
data[offset + 3],
data[offset + 4],
data[offset + 5],
data[offset + 6],
data[offset + 7],
]
}
// Read a buffer to unsigned integer bytes.
/**
* @param {number[] | Uint8Array} buffer
* @param {number} offset
* @returns {number}
*/
function readInt32 (buffer, offset) {
return (buffer[offset + 0] * 16_777_216) +
(buffer[offset + 1] << 16) +
(buffer[offset + 2] << 8) +
buffer[offset + 3]
}
// Write unsigned integer bytes to a buffer.
/**
* @param {number[] | Uint8Array} buffer
* @param {number} value
* @param {number} offset
*/
function writeUInt32BE (buffer, value, offset) {
buffer[3 + offset] = value & 255
value >>= 8
buffer[2 + offset] = value & 255
value >>= 8
buffer[1 + offset] = value & 255
value >>= 8
buffer[0 + offset] = value & 255
}
/**
* Resets the batch cursor, forcing the next ID batch to draw a fresh
* randomFillSync() call on MicroVM clone resume. Node's crypto RNG is
* re-seeded from the kernel CSPRNG on snapshot resume, so re-invoking it
* is sufficient — no need to read /dev/urandom directly.
*/
function reseed () {
batch = 0
}
/**
* @param {string} [value]
* @param {number} [radix]
* @returns {Identifier}
*/
module.exports = function createIdentifier (value, radix) {
return new Identifier(value ?? '', radix)
}
module.exports.Identifier = Identifier