Skip to content

fix: move hashing function to PlatformTools - #12648

Merged
alumni merged 1 commit into
masterfrom
fix/12559-require-crypto
Jul 6, 2026
Merged

alumni merged 1 commit into
masterfrom
fix/12559-require-crypto

Conversation

@alumni

@alumni alumni commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Description of change

Partly fixes #12559.

Under browser platforms, TypeORM was trying to import node:crypto and falling back to the built-in sha1 function if it couldn't.

However, the metro bundler used by React Native is not happy with this since it's not aware of node:crypto.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links a relevant issue using a closing keyword:
    Fixes #NNNN, Closes #NNNN, or Resolves #NNNN
  • There are new or updated tests validating the change (tests/**.test.ts) n/a
  • Documentation has been updated to reflect this change (docs/docs/**.md) n/a

@github-actions github-actions Bot added linked-issue PR references an issue possible-duplicate PR may duplicate an existing open PR labels Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Other open PRs also reference #12559: #12647. Maintainers may want to coordinate.

Comment thread src/platform/PlatformTools.ts Dismissed
Comment thread src/platform/PlatformTools.ts Dismissed
@alumni
alumni force-pushed the fix/12559-require-crypto branch from f6beccb to c3d943f Compare July 2, 2026 15:24
@pkg-pr-new

pkg-pr-new Bot commented Jul 2, 2026

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12648
    

commit: c3d943f

@alumni
alumni requested a review from G0maa July 2, 2026 15:25
@alumni
alumni marked this pull request as ready for review July 2, 2026 15:26
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Heavy StringUtils dependency 🐞 Bug ➹ Performance
Description
src/util/StringUtils.ts now imports PlatformTools at module load, so any consumer importing
common helpers (e.g., camelCase, shorten) will also load PlatformTools and its top-level
dependencies (@sqltools/formatter, sql-highlight, crypto, etc.) in Node. This increases
startup/module-load overhead and couples lightweight string helpers to Node-only tooling.
Code

src/util/StringUtils.ts[1]

+import { PlatformTools } from "../platform/PlatformTools"
Evidence
StringUtils now depends on PlatformTools at import time, and PlatformTools has several heavy
top-level imports. Core modules like DefaultNamingStrategy and DriverUtils import StringUtils,
so this dependency expansion affects common Node code paths.

src/util/StringUtils.ts[1-1]
src/util/StringUtils.ts[115-123]
src/platform/PlatformTools.ts[1-8]
src/naming-strategy/DefaultNamingStrategy.ts[1-3]
src/driver/DriverUtils.ts[1-4]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`StringUtils` has a new top-level import of `PlatformTools`, which forces loading `PlatformTools` (and its heavy imports) even when callers only use non-hash string helpers.

## Issue Context
The PR’s goal is to avoid Metro failing on `node:crypto`; that can still be achieved without a module-level dependency from `StringUtils` to `PlatformTools`.

## Fix Focus Areas
- src/util/StringUtils.ts[1-1]
- src/util/StringUtils.ts[115-123]

Suggested approach: remove the top-level `import { PlatformTools } ...` and require/import `PlatformTools` *inside* `hash()` (or extract SHA-1 into a tiny module with browser mapping), so non-hash users don’t pay the `PlatformTools` load cost.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Removed utility exports 🐞 Bug ⚙ Maintainability
Description
This PR removes PlatformTools.readPackageVersion and StringUtils.abbreviate, which will break
downstream code that imports them (including via deep-import paths that the package export patterns
allow). The PR title/description only describes hashing changes, so this compatibility break is
likely unintended.
Code

src/platform/PlatformTools.ts[L29-43]

-    /**
-     * Reads the version string from package.json of the given package.
-     * This operation is only supported in node.
-     *
-     * @param name
-     */
-    static readPackageVersion(name: string): string {
-        try {
-            return require(`${name}/package.json`).version
-        } catch (err) {
-            throw new TypeError(
-                `Failed to read package.json for "${name}": ${err.message}`,
-                { cause: err },
-            )
+        if (typeof globalThis !== "undefined") {
+            return globalThis
        }
+        return global
Evidence
The current PlatformTools and StringUtils files no longer contain these exports, and
package.json export patterns indicate consumers can deep-import internal modules where these
helpers previously lived.

src/platform/PlatformTools.ts[17-40]
src/util/StringUtils.ts[43-55]
package.json[19-45]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Two previously exported helpers were removed (`PlatformTools.readPackageVersion`, `StringUtils.abbreviate`). If any downstream code imports these, it will fail to compile or run.

## Issue Context
`package.json` exports includes a wildcard pattern, so deep imports are possible; even if these helpers are “internal”, they can be in real-world use.

## Fix Focus Areas
- src/platform/PlatformTools.ts[23-43]
- src/util/StringUtils.ts[47-66]
- package.json[19-45]

Suggested approach: re-add these functions (optionally marked deprecated) or provide a backwards-compatible shim that forwards/throws with a clear message, and document the change if removal is intentional.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Qodo Logo

@sonarqubecloud

sonarqubecloud Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

@alumni
alumni requested review from Cprakhar, gioboa and smith-xyz July 6, 2026 08:43
@alumni
alumni merged commit c456cbd into master Jul 6, 2026
46 of 48 checks passed
@alumni
alumni deleted the fix/12559-require-crypto branch July 6, 2026 09:31
@github-actions github-actions Bot added this to the 1.0 milestone Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linked-issue PR references an issue possible-duplicate PR may duplicate an existing open PR

Development

Successfully merging this pull request may close these issues.

Build fails on version 1 due to failed resolution of expo-sqlite

3 participants