-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
refactor: replace uuid with native Crypto API #11769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Important Review skippedReview was skipped due to path filters ⛔ Files ignored due to path filters (1)
CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including You can disable this status message by setting the WalkthroughThe PR removes the Changes
Sequence Diagram(s)sequenceDiagram
participant CallSite as Call Site
participant PT as PlatformTools
participant RG as RandomGenerator
participant Crypto as Native Crypto APIs
CallSite->>PT: generateUuid()
PT->>RG: uuidv4()
alt Node.js crypto.randomUUID available
RG->>Crypto: crypto.randomUUID()
Crypto-->>RG: uuid
else globalThis.crypto.randomUUID available
RG->>Crypto: globalThis.crypto.randomUUID()
Crypto-->>RG: uuid
else Fallback: getRandomValues
RG->>Crypto: crypto.getRandomValues(16)
Crypto-->>RG: bytes
RG->>RG: apply RFC4122 version/variant mask & format
end
RG-->>PT: uuid string
PT-->>CallSite: uuid string
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/util/StringUtils.ts (1)
1-1: Consider using native crypto for SHA-1 instead of RandomGenerator.The PR objectives specify replacing sha.js with the native crypto API, but
RandomGenerator.sha1()is a pure JavaScript implementation (ported from PHP's locutus library), not a native crypto call. The comment inRandomGenerator.tsitself recommends "using Node's native crypto modules directly in a streaming fashion for faster and more efficient hashing."Consider using the native crypto API for SHA-1:
import crypto from "crypto"Then update the
hashfunction:export function hash(input: string, options: IHashOptions = {}): string { - const hashedInput = RandomGenerator.sha1(input) + const hashedInput = crypto.createHash('sha1').update(input).digest('hex') if (options.length && options.length > 0) { return hashedInput.slice(0, options.length) } return hashedInput }For browser environments, this would need platform-specific handling similar to
PlatformTools.generateUuid(). Do you want me to propose a complete cross-platform solution?Also applies to: 120-120
src/platform/PlatformTools.ts (1)
281-289: Verify type safety; error handling is optional depending on supported browser versions.Node.js version 16.13.0 (minimum in package.json) satisfies the 14.17.0+ requirement for
crypto.randomUUID(). The code is compatible with the project's minimum Node.js version.For the browser path, the type safety concern is valid:
(globalThis as any).crypto.randomUUID()bypasses TypeScript checking. Consider improving type safety by replacing the cast with proper optional chaining:- return (globalThis as any).crypto.randomUUID() + return globalThis.crypto.randomUUID()Error handling for older browsers (lacking crypto.randomUUID support) is optional and depends on your minimum supported browser versions. If you need to support browsers older than Chrome 92, Firefox 95, or Safari 15.4, add defensive checks before the call.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (6)
package.json(0 hunks)src/cache/DbQueryResultCache.ts(2 hunks)src/platform/PlatformTools.ts(2 hunks)src/query-builder/InsertQueryBuilder.ts(2 hunks)src/util/StringUtils.ts(2 hunks)test/github-issues/10569/issue-10569.ts(2 hunks)
💤 Files with no reviewable changes (1)
- package.json
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-07-27T20:50:10.364Z
Learnt from: alumni
Repo: typeorm/typeorm PR: 11581
File: docs/docs/drivers/postgres.md:23-23
Timestamp: 2025-07-27T20:50:10.364Z
Learning: The correct data source type for Aurora PostgreSQL in TypeORM is `aurora-postgres`, not `aurora-data-api-pg`. The `aurora-data-api-pg` driver was renamed to `aurora-postgres` according to the CHANGELOG.md. This is defined in the DatabaseType union type and AuroraPostgresConnectionOptions interface.
Applied to files:
src/query-builder/InsertQueryBuilder.ts
🧬 Code graph analysis (4)
src/query-builder/InsertQueryBuilder.ts (1)
src/platform/PlatformTools.ts (1)
PlatformTools(18-290)
src/util/StringUtils.ts (1)
src/util/RandomGenerator.ts (1)
RandomGenerator(1-165)
src/cache/DbQueryResultCache.ts (1)
src/platform/PlatformTools.ts (1)
PlatformTools(18-290)
test/github-issues/10569/issue-10569.ts (1)
src/platform/PlatformTools.ts (1)
PlatformTools(18-290)
🔇 Additional comments (4)
src/cache/DbQueryResultCache.ts (1)
8-8: LGTM! Clean migration to native UUID generation.The replacement of
uuidv4()withPlatformTools.generateUuid()is correctly implemented for the Spanner-specific case where auto-generated columns are not supported.Also applies to: 271-271
src/query-builder/InsertQueryBuilder.ts (1)
14-14: LGTM! Proper UUID generation fallback.The replacement correctly uses
PlatformTools.generateUuid()for databases that don't natively support UUID generation. The integration withlocallyGeneratedtracking is maintained.Also applies to: 1410-1410
test/github-issues/10569/issue-10569.ts (1)
6-6: LGTM! Test updated to match new UUID generation approach.The test correctly uses
PlatformTools.generateUuid()consistent with the rest of the codebase.Also applies to: 36-36
src/platform/PlatformTools.ts (1)
2-2: LGTM! Proper crypto import.The crypto import is correctly added to support native UUID generation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can break platforms that don't support crypto. Read the discussion in #11693
Also: the internal sha1 implementation has a flaw (when whitespace chars are in the input) - so this is a breaking change that cannot be merged into master (we use master for the 0.3 version).
|
@alumni |
74bddfd to
1f469bc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/platform/BrowserPlatformTools.template (1)
143-203: Consider eliminating code duplication.The
sha1()andgenerateUuid()implementations are duplicated betweenPlatformTools.tsandBrowserPlatformTools.template. While some duplication is expected given the platform-specific nature of this file, the UUID generation fallback logic (lines 171-202) is identical.If feasible within your build/bundling constraints, consider extracting the common fallback logic to a shared utility that both platform implementations can reference. This would reduce maintenance burden and ensure consistency. However, if the template replacement mechanism makes this impractical, the current duplication is acceptable.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (9)
package.json(0 hunks)src/cache/DbQueryResultCache.ts(2 hunks)src/naming-strategy/DefaultNamingStrategy.ts(9 hunks)src/naming-strategy/LegacyOracleNamingStrategy.ts(2 hunks)src/platform/BrowserPlatformTools.template(1 hunks)src/platform/PlatformTools.ts(3 hunks)src/query-builder/InsertQueryBuilder.ts(2 hunks)src/util/StringUtils.ts(2 hunks)test/github-issues/10569/issue-10569.ts(2 hunks)
💤 Files with no reviewable changes (1)
- package.json
🚧 Files skipped from review as they are similar to previous changes (2)
- src/query-builder/InsertQueryBuilder.ts
- src/util/StringUtils.ts
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-07-26T12:22:52.215Z
Learnt from: alumni
Repo: typeorm/typeorm PR: 11580
File: src/util/ImportUtils.ts:58-62
Timestamp: 2025-07-26T12:22:52.215Z
Learning: In TypeORM's codebase, `importOrRequireFile` is always called with resolved file paths. For example, `DirectoryExportedClassesLoader.ts` calls `PlatformTools.pathResolve(file)` before passing the path to `importOrRequireFile`, and `PlatformTools.pathResolve()` is a wrapper around Node.js `path.resolve()`. This means the cache keys in `getNearestPackageJson` will always be absolute paths, eliminating concerns about cache consistency between relative and absolute path formats.
Applied to files:
src/platform/PlatformTools.ts
🧬 Code graph analysis (5)
src/naming-strategy/LegacyOracleNamingStrategy.ts (1)
src/platform/PlatformTools.ts (1)
PlatformTools(18-348)
src/cache/DbQueryResultCache.ts (1)
src/platform/PlatformTools.ts (1)
PlatformTools(18-348)
src/platform/PlatformTools.ts (1)
src/util/RandomGenerator.ts (1)
RandomGenerator(1-165)
src/naming-strategy/DefaultNamingStrategy.ts (1)
src/platform/PlatformTools.ts (1)
PlatformTools(18-348)
test/github-issues/10569/issue-10569.ts (1)
src/platform/PlatformTools.ts (1)
PlatformTools(18-348)
🔇 Additional comments (8)
src/cache/DbQueryResultCache.ts (1)
8-8: LGTM!The migration from the
uuidpackage toPlatformTools.generateUuid()is clean and maintains the same functionality for Spanner cache ID generation.Also applies to: 271-271
src/naming-strategy/LegacyOracleNamingStrategy.ts (1)
2-2: LGTM!The migration to
PlatformTools.sha1()maintains backward compatibility since it delegates to the sameRandomGenerator.sha1()implementation, ensuring database constraint names remain unchanged.Also applies to: 57-57
src/naming-strategy/DefaultNamingStrategy.ts (1)
2-2: LGTM!All constraint name generation methods have been consistently updated to use
PlatformTools.sha1(). Since this delegates to the same underlyingRandomGenerator.sha1()implementation, existing database constraint names will remain unchanged.Also applies to: 63-63, 76-76, 92-92, 102-102, 117-117, 133-133, 144-144, 155-155
test/github-issues/10569/issue-10569.ts (1)
6-6: LGTM!The test has been updated to use
PlatformTools.generateUuid()instead of the externaluuidpackage, maintaining the same test behavior.Also applies to: 36-36
src/platform/PlatformTools.ts (3)
2-2: LGTM!The crypto import addition and formatting cleanup are appropriate for the new functionality.
Also applies to: 134-136
281-290: LGTM!The
sha1()method correctly delegates toRandomGenerator.sha1(), maintaining backward compatibility for constraint names. The lazy import avoids circular dependencies.
292-347: Security concern acknowledged but implementation is already defensive; fallback only reached in unsupported environments.The review correctly identifies that Math.random() is not cryptographically secure (line 324-327). However, verification shows the implementation has three secure fallbacks before reaching it:
- TypeORM requires Node.js >=16.13.0, which includes
crypto.randomUUID()(available since Node 15.7.0)- Browser/modern environments try
globalThis.crypto.randomUUID()- Fallback to
crypto.getRandomValues()with RFC 4122 manual implementation- Only then Math.random() as last resort
The code already acknowledges this in the comment at line 324: "Last resort: Math.random (not cryptographically secure)."
For the cache key usage, UUIDs are used in DbQueryResultCache for cache IDs, where unpredictability is less critical than for security tokens. The Math.random() fallback would only execute in truly unsupported/legacy environments—modern React Native includes crypto polyfills, modern browsers support the crypto API.
Rather than throwing an error (which could break legitimate edge cases), consider adding a warning log when the Math.random() fallback is used, with clear documentation about supported environments.
src/platform/BrowserPlatformTools.template (1)
143-150: LGTM!The
sha1()method correctly delegates toRandomGenerator.sha1()for consistent hashing across platforms.
| /** | ||
| * Generates UUID v4 with fallback for environments without crypto support. | ||
| */ | ||
| static generateUuid(): string { | ||
| try { | ||
| // Try modern browser crypto API | ||
| if ( | ||
| typeof globalThis !== "undefined" && | ||
| globalThis.crypto && | ||
| typeof globalThis.crypto.randomUUID === "function" | ||
| ) { | ||
| return globalThis.crypto.randomUUID() | ||
| } | ||
| } catch (e) { | ||
| // Fall through to polyfill | ||
| } | ||
|
|
||
| // Fallback implementation for older browsers and React Native | ||
| // Based on RFC 4122 version 4 UUID | ||
| const randomBytes = new Uint8Array(16) | ||
|
|
||
| if ( | ||
| typeof globalThis !== "undefined" && | ||
| globalThis.crypto && | ||
| typeof globalThis.crypto.getRandomValues === "function" | ||
| ) { | ||
| globalThis.crypto.getRandomValues(randomBytes) | ||
| } else { | ||
| // Last resort: Math.random (not cryptographically secure) | ||
| for (let i = 0; i < 16; i++) { | ||
| randomBytes[i] = Math.floor(Math.random() * 256) | ||
| } | ||
| } | ||
|
|
||
| // Set version (4) and variant bits according to RFC 4122 | ||
| randomBytes[6] = (randomBytes[6] & 0x0f) | 0x40 | ||
| randomBytes[8] = (randomBytes[8] & 0x3f) | 0x80 | ||
|
|
||
| // Convert to UUID string format | ||
| const hexValues: string[] = [] | ||
| randomBytes.forEach((byte) => { | ||
| hexValues.push(byte.toString(16).padStart(2, "0")) | ||
| }) | ||
|
|
||
| return [ | ||
| hexValues.slice(0, 4).join(""), | ||
| hexValues.slice(4, 6).join(""), | ||
| hexValues.slice(6, 8).join(""), | ||
| hexValues.slice(8, 10).join(""), | ||
| hexValues.slice(10, 16).join(""), | ||
| ].join("-") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same security concern: Math.random() fallback is not cryptographically secure.
This implementation has the same security issue as PlatformTools.ts: the Math.random() fallback (lines 181-183) produces weak, predictable UUIDs when crypto APIs are unavailable.
Apply the same fix recommended for PlatformTools.ts:
} else {
- // Last resort: Math.random (not cryptographically secure)
- for (let i = 0; i < 16; i++) {
- randomBytes[i] = Math.floor(Math.random() * 256)
- }
+ throw new Error(
+ "Cryptographically secure random number generation is not available. " +
+ "Please ensure your browser supports crypto.getRandomValues()."
+ )
}Modern browsers have supported crypto.getRandomValues() since 2013-2014, so this fallback should rarely (if ever) be needed in practice.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| /** | |
| * Generates UUID v4 with fallback for environments without crypto support. | |
| */ | |
| static generateUuid(): string { | |
| try { | |
| // Try modern browser crypto API | |
| if ( | |
| typeof globalThis !== "undefined" && | |
| globalThis.crypto && | |
| typeof globalThis.crypto.randomUUID === "function" | |
| ) { | |
| return globalThis.crypto.randomUUID() | |
| } | |
| } catch (e) { | |
| // Fall through to polyfill | |
| } | |
| // Fallback implementation for older browsers and React Native | |
| // Based on RFC 4122 version 4 UUID | |
| const randomBytes = new Uint8Array(16) | |
| if ( | |
| typeof globalThis !== "undefined" && | |
| globalThis.crypto && | |
| typeof globalThis.crypto.getRandomValues === "function" | |
| ) { | |
| globalThis.crypto.getRandomValues(randomBytes) | |
| } else { | |
| // Last resort: Math.random (not cryptographically secure) | |
| for (let i = 0; i < 16; i++) { | |
| randomBytes[i] = Math.floor(Math.random() * 256) | |
| } | |
| } | |
| // Set version (4) and variant bits according to RFC 4122 | |
| randomBytes[6] = (randomBytes[6] & 0x0f) | 0x40 | |
| randomBytes[8] = (randomBytes[8] & 0x3f) | 0x80 | |
| // Convert to UUID string format | |
| const hexValues: string[] = [] | |
| randomBytes.forEach((byte) => { | |
| hexValues.push(byte.toString(16).padStart(2, "0")) | |
| }) | |
| return [ | |
| hexValues.slice(0, 4).join(""), | |
| hexValues.slice(4, 6).join(""), | |
| hexValues.slice(6, 8).join(""), | |
| hexValues.slice(8, 10).join(""), | |
| hexValues.slice(10, 16).join(""), | |
| ].join("-") | |
| } | |
| /** | |
| * Generates UUID v4 with fallback for environments without crypto support. | |
| */ | |
| static generateUuid(): string { | |
| try { | |
| // Try modern browser crypto API | |
| if ( | |
| typeof globalThis !== "undefined" && | |
| globalThis.crypto && | |
| typeof globalThis.crypto.randomUUID === "function" | |
| ) { | |
| return globalThis.crypto.randomUUID() | |
| } | |
| } catch (e) { | |
| // Fall through to polyfill | |
| } | |
| // Fallback implementation for older browsers and React Native | |
| // Based on RFC 4122 version 4 UUID | |
| const randomBytes = new Uint8Array(16) | |
| if ( | |
| typeof globalThis !== "undefined" && | |
| globalThis.crypto && | |
| typeof globalThis.crypto.getRandomValues === "function" | |
| ) { | |
| globalThis.crypto.getRandomValues(randomBytes) | |
| } else { | |
| throw new Error( | |
| "Cryptographically secure random number generation is not available. " + | |
| "Please ensure your browser supports crypto.getRandomValues()." | |
| ) | |
| } | |
| // Set version (4) and variant bits according to RFC 4122 | |
| randomBytes[6] = (randomBytes[6] & 0x0f) | 0x40 | |
| randomBytes[8] = (randomBytes[8] & 0x3f) | 0x80 | |
| // Convert to UUID string format | |
| const hexValues: string[] = [] | |
| randomBytes.forEach((byte) => { | |
| hexValues.push(byte.toString(16).padStart(2, "0")) | |
| }) | |
| return [ | |
| hexValues.slice(0, 4).join(""), | |
| hexValues.slice(4, 6).join(""), | |
| hexValues.slice(6, 8).join(""), | |
| hexValues.slice(8, 10).join(""), | |
| hexValues.slice(10, 16).join(""), | |
| ].join("-") | |
| } |
|
@alumni Changes MadePlatform Compatibility
Updated both Node and Browser PlatformTools implementations SHA-1 Breaking Change
Please let me know if this direction is acceptable or if you'd prefer a different approach. |
|
So first of all, if there's any breaking change, we would like to do this in the next major version of TypeORM, which is developed in the The Crypto WebAPI ( We could add a For SHA1, we can't use native crypto since it's async, so we can use our own implementation. Our implementation has a bug with certain characters (missing I think the UUIDv4 change is not breaking, but the SHA1 will be. |
d525cb4 to
76df701
Compare
|
@alumni Changes MadeUUID Implementation
SHA1 Scope Reduction
Should I update the PR title to reflect the reduced scope (e.g., "refactor: replace uuid with native Crypto API")? Or keep the current title and clarify in comments? And, May I proceed with creating a PR targeting the Looking forward to your feedback! |
|
@mag123c a few more things: the lockfile needs updating after Looking at the source code of |
c355083 to
c0ad3aa
Compare
|
@alumni Thank you for the detailed feedback on React Native/Hermes support! Updates Made
Before implementing, I conducted research to ensure this is the optimal approach 1. Hermes crypto support
2. RFC 4122 compliance
3. Others
|
|
Fixed missing The entry was accidentally removed during a previous lock update when npm pruned the optional package. Manually restored from master's lock file. |
|
@mag123c this should be done against |
|
@pkuczynski I think here it's a problem of how we created the tasks, we should probably create 3 of them (shajs, uuid, and buffer), not just one :P However, if |
|
I've resolved the conflicts after the base branch was changed to |
db7620b to
1870e55
Compare
|
seems the lockfile needs an update so the checks run correctly :) @mag123c |
7307b7e to
34e048a
Compare
PR Code Suggestions ✨Latest suggestions up to dd2cb3c
Previous suggestionsSuggestions up to commit f823ff8
Suggestions up to commit 26feed2
Suggestions up to commit ce231ba
Suggestions up to commit bb5b5b5
Suggestions up to commit af425f4
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
af8c357 to
240c3ca
Compare
240c3ca to
5461092
Compare
src/util/RandomGenerator.ts
Outdated
| */ | ||
| static uuidv4(): string { | ||
| // Try Node.js native crypto | ||
| if (typeof process !== "undefined" && process.versions?.node) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be removed. globalThis.crypto should exist in all node versions that we support: Crypto: randomUUID() method - Web APIs | MDN
src/util/RandomGenerator.ts
Outdated
| if ( | ||
| typeof globalThis !== "undefined" && | ||
| globalThis.crypto && | ||
| typeof globalThis.crypto.randomUUID === "function" | ||
| ) { | ||
| try { | ||
| return globalThis.crypto.randomUUID() | ||
| } catch (e) { | ||
| // Fall through to custom implementation | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use more modern JS, if TS allows it. This applies in other places in this PR, but I'll only write it here.
| if ( | |
| typeof globalThis !== "undefined" && | |
| globalThis.crypto && | |
| typeof globalThis.crypto.randomUUID === "function" | |
| ) { | |
| try { | |
| return globalThis.crypto.randomUUID() | |
| } catch (e) { | |
| // Fall through to custom implementation | |
| } | |
| } | |
| const uuid = globalThis.crypto?.randomUUID?.() | |
| if (uuid) { | |
| return uuid | |
| } |
src/platform/PlatformTools.ts
Outdated
| * Generates UUID v4 using native crypto API with fallback for environments | ||
| * that don't support it (e.g., React Native, Hermes). | ||
| */ | ||
| static generateUuid(): string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would make more sense for SHA-1 which we already merged CC: @G0maa
But for UUIDv4, since the implementation is generic, we probably don't need to do it, we can use the implementation directly, without having to go through PlatformTools.
| /** | ||
| * Logging functions needed by AdvancedConsoleLogger | ||
| * @param prefix | ||
| * @param info |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the TSDoc for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution @mag123c.
Few nitpicks, PR is ok.
| /** | ||
| * Reads the version string from package.json of the given package. | ||
| * This operation is only supported in node. | ||
| * @param name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is what I mentioned in an earlier comment. I would revert the changes to this file.
| const uuid = globalThis.crypto?.randomUUID?.() | ||
| if (uuid) { | ||
| return uuid | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick(needn't to fix): it seems that this type of logic is going to repeat a lot, should we abstract it somewhere (e.g. PlatformTools?) @alumni
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your comment references this above, so just to clarify: It seems in different places we have to do the same logic: If node do x, if browser do y, if RN do z. so I was suggesting a "standarized" way of doing this somewhere. i.e. such that every PR uses the same method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep it as it is - we don't need to do things based on which platform we're running, but based on what features the platform provides. So if the platform provides the randomUUID function, we should use it.
Over time we should get rid of PlatformTools if possible.
| if (globalThis.crypto?.getRandomValues) { | ||
| globalThis.crypto.getRandomValues(randomBytes) | ||
| } else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which environment is going to use this branch? (Browser, Node, RN, ...?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anything that does not support the Crypto WebAPI (or doesn't support it properly).
There are problems in Hermes (RN/Expo) as I understand.
bb5b5b5 to
ce231ba
Compare
| // This approach is also used by expo-crypto and other RN libraries | ||
| // Note: For TypeORM's use case (DB IDs, cache IDs), uniqueness is sufficient | ||
| for (let i = 0; i < 16; i++) { | ||
| randomBytes[i] = Math.floor(Math.random() * 256) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this enough entropy for uniqueness? I imagine its generally okay but thought I'd ask out of interest 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for uniqueness it's sufficient. This fallback is only for RN/Hermes environments, and was already discussed with @alumni above
|
I think this PR is ready @alumni |
Our comments in PlatformTools were not solved entirely :P The PR is still removing the TSDoc that you recently added :P |
gioboa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mag123c 👏


Description of change
This change by removing unnecessary external cryptographic dependencies (sha.js and uuid) and replacing them with native Crypto API functionality available in modern Node.js and browser environments.
Pull-Request Checklist
masterbranchSummary by CodeRabbit
Chores
Refactor
✏️ Tip: You can customize this high-level summary in your review settings.