@nestjs/observe 0.3.20 → 0.3.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,33 @@
1
- import { randomBytes } from "crypto";
1
+ import { randomFillSync } from "crypto";
2
+ /** Random bytes in each id: 12 bits go into `rand_a`, 62 into `rand_b`. */
3
+ const RANDOM_BYTES_PER_ID = 10;
4
+ /**
5
+ * Random bytes for the ids below, refilled 1,024 ids' worth at a time.
6
+ *
7
+ * The agent mints an id for every span, so this runs once per provider call
8
+ * of every traced request. A `randomBytes(10)` per id - a native call and a
9
+ * buffer each time - was the agent's largest single cost on the request path:
10
+ * a fifth of the main thread's busy time on an endpoint with nine spans. The
11
+ * bytes still come from the CSPRNG; it is asked once per 1,024 ids, the way
12
+ * `crypto.randomUUID()` caches its own entropy.
13
+ */
14
+ const pool = Buffer.allocUnsafe(RANDOM_BYTES_PER_ID * 1024);
15
+ let poolOffset = pool.length;
16
+ const HEX_DIGITS = "0123456789abcdef";
17
+ /** Each byte value's two hex digits, as character codes. */
18
+ const HIGH_DIGIT = Uint8Array.from({ length: 256 }, (_, byte) => HEX_DIGITS.charCodeAt(byte >>> 4));
19
+ const LOW_DIGIT = Uint8Array.from({ length: 256 }, (_, byte) => HEX_DIGITS.charCodeAt(byte & 0x0f));
20
+ /**
21
+ * The id's 36 characters, written in place and read out as one string; the
22
+ * hyphens never move. Joined from pieces instead, an id was a concatenation
23
+ * V8 had to flatten before it could hash it, and the registry hashes every
24
+ * span id - as a map key - the moment it is minted.
25
+ */
26
+ const text = Buffer.alloc(36, "-");
27
+ function writeByte(at, byte) {
28
+ text[at] = HIGH_DIGIT[byte];
29
+ text[at + 1] = LOW_DIGIT[byte];
30
+ }
2
31
  /**
3
32
  * A UUID whose first 48 bits are the minting time, milliseconds since the
4
33
  * epoch (RFC 9562 version 7).
@@ -15,20 +44,29 @@ import { randomBytes } from "crypto";
15
44
  * decode the same way.
16
45
  */
17
46
  export function uuidv7(now = Date.now()) {
18
- // Ten random bytes: 12 bits go into `rand_a`, 62 into `rand_b`. The two
19
- // version and two variant bits overwrite what they need.
20
- const rand = randomBytes(10);
21
- const bytes = Buffer.allocUnsafe(16);
22
- // 48-bit big-endian timestamp, placed as a 32/16 split so no shift runs
23
- // past 2^32.
24
- bytes.writeUInt32BE(Math.floor(now / 0x10000), 0);
25
- bytes.writeUInt16BE(now % 0x10000, 4);
47
+ if (poolOffset + RANDOM_BYTES_PER_ID > pool.length) {
48
+ randomFillSync(pool);
49
+ poolOffset = 0;
50
+ }
51
+ const r = poolOffset;
52
+ poolOffset += RANDOM_BYTES_PER_ID;
53
+ // 48-bit big-endian timestamp, split 32/16 so no shift runs past 2^32.
54
+ const high = Math.floor(now / 0x10000);
55
+ const low = now % 0x10000;
56
+ writeByte(0, (high >>> 24) & 0xff);
57
+ writeByte(2, (high >>> 16) & 0xff);
58
+ writeByte(4, (high >>> 8) & 0xff);
59
+ writeByte(6, high & 0xff);
60
+ writeByte(9, (low >>> 8) & 0xff);
61
+ writeByte(11, low & 0xff);
26
62
  // Version 7 in the top nibble, 12 random bits below it.
27
- bytes[6] = 0x70 | (rand[0] & 0x0f);
28
- bytes[7] = rand[1];
63
+ writeByte(14, 0x70 | (pool[r] & 0x0f));
64
+ writeByte(16, pool[r + 1]);
29
65
  // Variant `10` in the top two bits, 62 random bits below.
30
- bytes[8] = 0x80 | (rand[2] & 0x3f);
31
- rand.copy(bytes, 9, 3, 10);
32
- const hex = bytes.toString("hex");
33
- return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
66
+ writeByte(19, 0x80 | (pool[r + 2] & 0x3f));
67
+ writeByte(21, pool[r + 3]);
68
+ for (let i = 0; i < 6; i++) {
69
+ writeByte(24 + i * 2, pool[r + 4 + i]);
70
+ }
71
+ return text.toString("latin1");
34
72
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/observe",
3
- "version": "0.3.20",
3
+ "version": "0.3.21",
4
4
  "description": "Observability and APM agent for NestJS - traces, runtime metrics and custom metrics",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "homepage": "https://observe.nestjs.com",
@@ -42,7 +42,8 @@
42
42
  },
43
43
  "files": [
44
44
  "dist",
45
- "!dist/**/*.tsbuildinfo"
45
+ "!dist/**/*.tsbuildinfo",
46
+ "NOTICE"
46
47
  ],
47
48
  "publishConfig": {
48
49
  "access": "public"
@@ -61,6 +62,7 @@
61
62
  "test:watch": "vitest",
62
63
  "test:cov": "vitest run --coverage",
63
64
  "test:int": "vitest run --config vitest.int.config.ts",
65
+ "bench": "npm run build && tsc -p bench/tsconfig.json && node bench/.build/run.js",
64
66
  "prepack": "npm run build",
65
67
  "prepublish:next": "npm run build",
66
68
  "publish:next": "npm publish --access public --tag next",
@@ -70,7 +72,7 @@
70
72
  "release": "release-it",
71
73
  "prepare": "husky"
72
74
  },
73
- "license": "MIT",
75
+ "license": "SEE LICENSE IN LICENSE",
74
76
  "dependencies": {
75
77
  "@jridgewell/trace-mapping": "0.3.31"
76
78
  },