Skip to content

Commit 7acf9f0

Browse files
committed
feat: add crypto polyfill for health check and Node.js compatibility
1 parent 2d80dfe commit 7acf9f0

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

local-tests/build.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,33 @@ try {
7878
}
7979
};
8080

81+
/**
82+
* Adds crypto polyfill to health check build
83+
*/
84+
export const postBuildHealthPolyfill = () => {
85+
try {
86+
const file = fs.readFileSync(`./${TEST_DIR}/build/health/index.mjs`, 'utf8');
87+
const content = `// Additional crypto polyfill check
88+
try {
89+
if (!globalThis.crypto && typeof webcrypto !== 'undefined') {
90+
globalThis.crypto = webcrypto;
91+
}
92+
} catch (error) {
93+
console.error('❌ Error in crypto polyfill', error);
94+
}
95+
`;
96+
const newFile = content + file;
97+
fs.writeFileSync(`./${TEST_DIR}/build/health/index.mjs`, newFile);
98+
} catch (e) {
99+
throw new Error(`Error in postBuildHealthPolyfill: ${e}`);
100+
}
101+
};
102+
81103
// Go!
82104
(async () => {
83105
const start = Date.now();
84106
await build();
85107
postBuildPolyfill();
108+
postBuildHealthPolyfill();
86109
console.log(`[build.mjs] 🚀 Build time: ${Date.now() - start}ms`);
87110
})();

local-tests/health/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { createLitStatusClient } from '@lit-protocol/lit-status-sdk';
22
import { DatilHealthManager } from './DatilHealthManager';
33

4-
// Fix for Node.js crypto in ESM
5-
import { webcrypto } from 'node:crypto';
6-
if (!globalThis.crypto) {
7-
globalThis.crypto = webcrypto as Crypto;
8-
}
9-
104
// Configuration
115
const NETWORK = process.env.NETWORK!;
126
const PRODUCT = 'js-sdk/datil';

local-tests/shim.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
import { createRequire } from 'module';
2+
import { webcrypto } from 'node:crypto';
3+
24
const require = createRequire(import.meta.url);
35
global.require = require;
6+
7+
// Add crypto polyfill for Node.js
8+
if (!globalThis.crypto) {
9+
globalThis.crypto = webcrypto;
10+
}

0 commit comments

Comments
 (0)