a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm

feat(tid): use Node.js' secure random for clockid

mary.my.id 5f6813a3 35d4a3db

verified
+5
.changeset/strict-melons-divide.md
··· 1 + --- 2 + '@atcute/tid': patch 3 + --- 4 + 5 + use secure random for clockid
+3 -1
packages/utilities/tid/lib/index.ts
··· 1 1 import { now as getNow } from '@atcute/time-ms'; 2 + 3 + import { random } from '#platform/random'; 2 4 import { s32decode, s32encode } from './s32.js'; 3 5 4 6 let lastTimestamp = 0; ··· 45 47 } 46 48 47 49 lastTimestamp = timestamp; 48 - return createRaw(timestamp, Math.floor(Math.random() * 1023)); 50 + return createRaw(timestamp, random(1024)); 49 51 }; 50 52 51 53 /**
+6
packages/utilities/tid/lib/random-node.ts
··· 1 + import { randomInt } from 'node:crypto'; 2 + 3 + /** returns a random integer in the range [0, max) */ 4 + export const random = (max: number): number => { 5 + return randomInt(max); 6 + };
+4
packages/utilities/tid/lib/random-web.ts
··· 1 + /** returns a random integer in the range [0, max) */ 2 + export const random = (max: number): number => { 3 + return Math.floor(Math.random() * max); 4 + };
+7
packages/utilities/tid/package.json
··· 24 24 "exports": { 25 25 ".": "./dist/index.js" 26 26 }, 27 + "imports": { 28 + "#platform/random": { 29 + "node": "./dist/random-node.js", 30 + "default": "./dist/random-web.js" 31 + } 32 + }, 27 33 "sideEffects": false, 28 34 "scripts": { 29 35 "build": "tsgo --project tsconfig.build.json", ··· 34 40 "@atcute/time-ms": "workspace:^" 35 41 }, 36 42 "devDependencies": { 43 + "@types/node": "^22.19.3", 37 44 "vitest": "^4.0.16" 38 45 } 39 46 }
+5 -2
packages/utilities/tid/tsconfig.json
··· 1 1 { 2 2 "compilerOptions": { 3 - "types": [], 3 + "types": ["node"], 4 4 "outDir": "dist/", 5 5 "esModuleInterop": true, 6 6 "skipLibCheck": true, ··· 18 18 "module": "NodeNext", 19 19 "sourceMap": true, 20 20 "declaration": true, 21 - "declarationMap": true 21 + "declarationMap": true, 22 + "paths": { 23 + "#platform/random": ["./lib/random-web.ts"] 24 + } 22 25 }, 23 26 "include": ["lib"] 24 27 }
+4 -1
pnpm-lock.yaml
··· 1127 1127 specifier: workspace:^ 1128 1128 version: link:../../misc/time-ms 1129 1129 devDependencies: 1130 + '@types/node': 1131 + specifier: ^22.19.3 1132 + version: 22.19.3 1130 1133 vitest: 1131 1134 specifier: ^4.0.16 1132 - version: 4.0.16(@types/node@25.0.3)(@vitest/browser-playwright@4.0.16)(jiti@2.6.1)(tsx@4.20.6)(yaml@2.8.0) 1135 + version: 4.0.16(@types/node@22.19.3)(@vitest/browser-playwright@4.0.16)(jiti@2.6.1)(tsx@4.20.6)(yaml@2.8.0) 1133 1136 1134 1137 packages/utilities/varint: 1135 1138 devDependencies: