build(web,sign): drop vite-plugin-node-polyfills and its elliptic dependency (#70)

vite-plugin-node-polyfills depends on node-stdlib-browser, which always
installs crypto-browserify -> browserify-sign/create-ecdh -> elliptic
(GHSA-848j-6mx2-7j84, no patched release). No module in either bundle
ever imported the crypto polyfill; what the plugin actually provided was
three injected globals and the `events` alias.

sign: the only call sites were @pezkuwi/x-global (`global`, after a
globalThis check), shared/lib env helpers (`process`, typeof-guarded) and
bn.js's optional Buffer probe (try/catch; BN#toBuffer has no callers).
The plugin, the process shim alias and the `buffer` pre-bundle entry are
removed outright.

web: WalletConnect calls `Buffer.from` unguarded and imports `events`,
so the globals are still needed. src/lib/node-globals.ts now provides
Buffer/global/process, injected as scoped imports by @rollup/plugin-inject
in the build and by esbuild's inject in the dev pre-bundle - the same 18
modules the plugin injected into, and still nothing written onto
globalThis. `events` becomes a direct dependency. The unused shim files,
the vitest shim aliases and the `elliptic` override are removed.
This commit is contained in:
SatoshiQaziMuhammed authored and GitHub committed 2026-11-15 05:28:59 +04:00
1 parent d703331d85
commit 11fbe41a8a
16 files changed
+62 -3079

No files matched your search

+8 -1082
View File
File diff suppressed because it is too large. Load diff
+2 -2
View File
@@ -69,6 +69,7 @@
"date-fns": "^3.6.0",
"dompurify": "^3.4.17",
"embla-carousel-react": "^8.3.0",
"events": "^3.3.0",
"highlight.js": "^11.9.0",
"i18next": "^23.7.6",
"i18next-browser-languagedetector": "^7.2.0",
@@ -116,7 +117,6 @@
"@pezkuwi/x-textencoder": "^14.0.26",
"@pezkuwi/x-ws": "^14.0.26",
"@pezkuwi/networks": "^14.0.26",
"elliptic": "^6.6.1",
"@tailwindcss/typography": {
"postcss-selector-parser": "6.1.5"
},
@@ -126,6 +126,7 @@
},
"devDependencies": {
"@eslint/js": "^9.9.0",
"@rollup/plugin-inject": "^5.0.5",
"@tailwindcss/typography": "^0.5.16",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.0",
@@ -152,7 +153,6 @@
"typescript": "^5.5.3",
"typescript-eslint": "^8.0.1",
"vite": "^7.3.7",
"vite-plugin-node-polyfills": "^0.25.0",
"vite-plugin-subresource-integrity": "^0.0.12",
"vitest": "^4.1.12"
}
+17
View File
@@ -0,0 +1,17 @@
// Values for the three Node globals that some browser dependencies reference as free
// identifiers (WalletConnect calls `Buffer.from(...)` unguarded; a few libraries probe
// `global` / `process`). vite.config.ts injects these as scoped imports into exactly the
// modules that reference them - the build through @rollup/plugin-inject, the dev pre-bundle
// through esbuild's `inject` - so nothing is assigned onto `globalThis`.
//
// Nothing here may read the bare `process` / `global` / `Buffer` identifiers at runtime:
// the injector would rewrite them into a self-import. Use bracket access on globalThis.
import { Buffer } from 'buffer';
const g = globalThis as unknown as Record<string, unknown>;
const nodeGlobal = globalThis;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const nodeProcess = (g['process'] ?? { env: {} }) as any;
export { Buffer, nodeGlobal as global, nodeProcess as process };
-11
View File
@@ -1,11 +0,0 @@
// Production Rollup alias for vite-plugin-node-polyfills/shims/process.
// IMPORTANT: must not reference the `process` identifier at runtime —
// vite-plugin-node-polyfills rewrites it to `__process_polyfill`, creating
// a circular TDZ. Use bracket notation so the plugin leaves this file alone.
const g: Record<string, unknown> =
typeof globalThis !== 'undefined' ? (globalThis as Record<string, unknown>)
: typeof window !== 'undefined' ? (window as unknown as Record<string, unknown>)
: {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default (g['process'] ?? { env: {} }) as any;
-4
View File
@@ -1,4 +0,0 @@
// Buffer polyfill shim for vite-plugin-node-polyfills
import { Buffer } from 'buffer';
export { Buffer };
export default Buffer;
-2
View File
@@ -1,2 +0,0 @@
// Global polyfill shim for vite-plugin-node-polyfills
export default typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : {};
-9
View File
@@ -1,9 +0,0 @@
// Process polyfill shim for vite-plugin-node-polyfills
const processShim = {
env: {},
browser: true,
version: '',
versions: {},
};
export default processShim;
-2
View File
@@ -1,2 +0,0 @@
import { Buffer } from 'buffer';
export default Buffer;
-1
View File
@@ -1 +0,0 @@
export default globalThis;
-1
View File
@@ -1 +0,0 @@
export default { env: {} };
+25 -27
View File
@@ -2,9 +2,12 @@
import { defineConfig, configDefaults } from "vitest/config";
import react from "@vitejs/plugin-react-swc";
import path from "path";
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import inject from '@rollup/plugin-inject';
import subresourceIntegrity from 'vite-plugin-subresource-integrity';
// Node globals some browser dependencies reference as free identifiers; see the file header.
const nodeGlobals = path.resolve(__dirname, './src/lib/node-globals.ts');
// https://vitejs.dev/config/
export default defineConfig(({ command }) => ({
test: {
@@ -14,11 +17,6 @@ export default defineConfig(({ command }) => ({
// supabase/** holds Deno tests (Deno.test + pglite) run via `deno test`,
// not vitest — exclude them from the web (node/jsdom) test sweep.
exclude: [...configDefaults.exclude, 'supabase/**'],
alias: {
'vite-plugin-node-polyfills/shims/buffer': path.resolve(__dirname, './src/tests/mocks/buffer-shim.ts'),
'vite-plugin-node-polyfills/shims/global': path.resolve(__dirname, './src/tests/mocks/global-shim.ts'),
'vite-plugin-node-polyfills/shims/process': path.resolve(__dirname, './src/tests/mocks/process-shim.ts'),
},
},
server: {
host: "::",
@@ -34,14 +32,6 @@ export default defineConfig(({ command }) => ({
},
plugins: [
react(),
nodePolyfills({
globals: {
Buffer: true,
global: true,
process: true,
},
protocolImports: true,
}),
// SRI: production build sırasında <script>/<link> tag'lerine
// sha384 integrity hash ekle. CDN/proxy compromise olsa bile
// tampered asset browser tarafından load edilmez.
@@ -50,11 +40,6 @@ export default defineConfig(({ command }) => ({
resolve: {
mainFields: ['browser', 'module', 'main', 'exports'],
alias: {
// Rollup cannot resolve virtual shim modules in production — alias to real file.
// Dev mode: the plugin's own virtual module handles it; do NOT override it here.
...(command === 'build' ? {
'vite-plugin-node-polyfills/shims/process': path.resolve(__dirname, './src/lib/process-shim.ts'),
} : {}),
"@": path.resolve(__dirname, "./src"),
"@pezkuwi/i18n": path.resolve(__dirname, "../shared/i18n"),
"@pezkuwi/lib": path.resolve(__dirname, "../shared/lib"),
@@ -68,6 +53,19 @@ export default defineConfig(({ command }) => ({
},
optimizeDeps: {
include: ['@pezkuwi/util-crypto', '@pezkuwi/util', '@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring', 'buffer'],
esbuildOptions: {
inject: [nodeGlobals],
plugins: [
{
// Vite's dependency scanner marks every path outside node_modules as external, and
// esbuild refuses an external inject target; resolve it as a real file instead.
name: 'node-globals-resolver',
setup(build) {
build.onResolve({ filter: /[\\/]src[\\/]lib[\\/]node-globals\.ts$/ }, () => ({ path: nodeGlobals }));
},
},
],
},
},
// Strip all console.* and debugger statements from production bundles so the
// 600+ dev-time console calls never ship. Dev keeps full logging.
@@ -75,13 +73,13 @@ export default defineConfig(({ command }) => ({
build: {
rollupOptions: {
external: [],
onwarn(warning, warn) {
// Suppress the buffer shim warning - it's handled by vite-plugin-node-polyfills
if (warning.message?.includes('vite-plugin-node-polyfills/shims/buffer')) {
return;
}
warn(warning);
},
plugins: [
inject({
Buffer: [nodeGlobals, 'Buffer'],
global: [nodeGlobals, 'global'],
process: [nodeGlobals, 'process'],
}),
],
output: {
manualChunks: {
'pezkuwi': ['@pezkuwi/api', '@pezkuwi/extension-dapp', '@pezkuwi/keyring', '@pezkuwi/util', '@pezkuwi/util-crypto'],
@@ -94,5 +92,5 @@ export default defineConfig(({ command }) => ({
},
chunkSizeWarningLimit: 600
},
// assetsInclude: ['**/*.json'], // Disabled: interferes with node_modules JSON imports (crypto-browserify)
// assetsInclude: ['**/*.json'], // Disabled: interferes with node_modules JSON imports
}));