Skip to content

Commit 35ca51a

Browse files
authored
refactor: replace uuid with native Crypto API (#11769)
1 parent e5a8afb commit 35ca51a

6 files changed

Lines changed: 56 additions & 10 deletions

File tree

‎package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
"sql-highlight": "^6.1.0",
106106
"tinyglobby": "^0.2.15",
107107
"tslib": "^2.8.1",
108-
"uuid": "^11.1.0",
109108
"yargs": "^18.0.0"
110109
},
111110
"devDependencies": {

‎pnpm-lock.yaml‎

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/cache/DbQueryResultCache.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { DataSource } from "../data-source/DataSource"
33
import { MssqlParameter } from "../driver/sqlserver/MssqlParameter"
44
import { QueryRunner } from "../query-runner/QueryRunner"
55
import { Table } from "../schema-builder/table/Table"
6+
import { RandomGenerator } from "../util/RandomGenerator"
67
import { QueryResultCache } from "./QueryResultCache"
78
import { QueryResultCacheOptions } from "./QueryResultCacheOptions"
8-
import { v4 as uuidv4 } from "uuid"
99

1010
/**
1111
* Caches query result into current database, into separate table called "query-result-cache".
@@ -268,7 +268,7 @@ export class DbQueryResultCache implements QueryResultCache {
268268
this.connection.driver.options.type === "spanner" &&
269269
!insertedValues.id
270270
) {
271-
insertedValues.id = uuidv4()
271+
insertedValues.id = RandomGenerator.uuidv4()
272272
}
273273

274274
// otherwise insert

‎src/query-builder/InsertQueryBuilder.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { v4 as uuidv4 } from "uuid"
21
import { EntityTarget } from "../common/EntityTarget"
32
import { ObjectLiteral } from "../common/ObjectLiteral"
43
import { AuroraMysqlDriver } from "../driver/aurora-mysql/AuroraMysqlDriver"
@@ -12,6 +11,7 @@ import { ColumnMetadata } from "../metadata/ColumnMetadata"
1211
import { BroadcasterResult } from "../subscriber/BroadcasterResult"
1312
import { InstanceChecker } from "../util/InstanceChecker"
1413
import { ObjectUtils } from "../util/ObjectUtils"
14+
import { RandomGenerator } from "../util/RandomGenerator"
1515
import { InsertOrUpdateOptions } from "./InsertOrUpdateOptions"
1616
import { QueryBuilder } from "./QueryBuilder"
1717
import { QueryDeepPartialEntity } from "./QueryPartialEntity"
@@ -1407,7 +1407,7 @@ export class InsertQueryBuilder<
14071407
!this.connection.driver.isUUIDGenerationSupported() &&
14081408
value === undefined
14091409
) {
1410-
value = uuidv4()
1410+
value = RandomGenerator.uuidv4()
14111411
expression += this.createParameter(value)
14121412

14131413
if (!(valueSetIndex in this.expressionMap.locallyGenerated)) {

‎src/util/RandomGenerator.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,54 @@
11
export class RandomGenerator {
2+
/**
3+
* Generates a RFC 4122 version 4 UUID.
4+
* Uses native crypto.randomUUID() if available, otherwise falls back to
5+
* a custom implementation using crypto.getRandomValues().
6+
*
7+
* @returns A version 4 UUID string
8+
*/
9+
static uuidv4(): string {
10+
// Try native crypto.randomUUID() (available in Node.js 19+ and modern browsers)
11+
const uuid = globalThis.crypto?.randomUUID?.()
12+
if (uuid) {
13+
return uuid
14+
}
15+
16+
// Custom implementation using crypto.getRandomValues()
17+
// Based on RFC 4122 version 4 UUID specification
18+
const randomBytes = new Uint8Array(16)
19+
20+
if (globalThis.crypto?.getRandomValues) {
21+
globalThis.crypto.getRandomValues(randomBytes)
22+
} else {
23+
// Fallback for React Native/Hermes and environments without crypto support
24+
// Hermes (React Native's JavaScript engine) does not provide crypto APIs
25+
// Math.random() is permitted by RFC 4122 for UUID v4 ("pseudo-randomly")
26+
// This approach is also used by expo-crypto and other RN libraries
27+
// Note: For TypeORM's use case (DB IDs, cache IDs), uniqueness is sufficient
28+
for (let i = 0; i < 16; i++) {
29+
randomBytes[i] = Math.floor(Math.random() * 256)
30+
}
31+
}
32+
33+
// Set version (4) and variant bits according to RFC 4122
34+
randomBytes[6] = (randomBytes[6] & 0x0f) | 0x40 // Version 4
35+
randomBytes[8] = (randomBytes[8] & 0x3f) | 0x80 // Variant 10
36+
37+
// Convert to UUID string format (xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx)
38+
const hexValues: string[] = []
39+
randomBytes.forEach((byte) => {
40+
hexValues.push(byte.toString(16).padStart(2, "0"))
41+
})
42+
43+
return [
44+
hexValues.slice(0, 4).join(""),
45+
hexValues.slice(4, 6).join(""),
46+
hexValues.slice(6, 8).join(""),
47+
hexValues.slice(8, 10).join(""),
48+
hexValues.slice(10, 16).join(""),
49+
].join("-")
50+
}
51+
252
/**
353
* discuss at: http://locutus.io/php/sha1/
454
* original by: Webtoolkit.info (http://www.webtoolkit.info/)

‎test/github-issues/10569/issue-10569.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import "reflect-metadata"
22

33
import { expect } from "chai"
4-
import { v4 } from "uuid"
54

65
import { DataSource } from "../../../src/data-source/DataSource"
6+
import { RandomGenerator } from "../../../src/util/RandomGenerator"
77
import {
88
closeTestingConnections,
99
createTestingConnections,
@@ -33,7 +33,7 @@ describe("github issues > #10569 Fix type inferencing of EntityManager#create",
3333

3434
const user = dataSource.manager.create(User, createUserContract)
3535

36-
user.id = v4()
36+
user.id = RandomGenerator.uuidv4()
3737

3838
expect(user.id).to.exist
3939
})

0 commit comments

Comments
 (0)