Skip to content

Commit 310abca

Browse files
committed
fix: polyfill support and fixes
1 parent bb000c2 commit 310abca

File tree

6 files changed

+135
-3
lines changed

6 files changed

+135
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ const client = new NilaiOpenAIClient({
150150
- **🛡️ Secure Delegation**: Server-side token management with configurable expiration and usage limits.
151151
- **🌐 Network Flexibility**: Support for sandbox and production `nilauth` environments.
152152
- **🔒 Type Safety**: Strongly typed with Zod schema validation for robust development.
153+
- **🔧 Universal Compatibility**: Built-in polyfills for Node.js environments ensure seamless operation across different platforms without manual configuration.
153154

154155
## 🏗️ Architecture
155156

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
"types": "./dist/index.d.ts",
1111
"import": "./dist/index.js"
1212
},
13+
"./polyfills": {
14+
"types": "./dist/polyfills.d.ts",
15+
"import": "./dist/polyfills.js"
16+
},
1317
"./package.json": "./package.json"
1418
},
1519
"scripts": {
@@ -51,19 +55,22 @@
5155
"@typescript-eslint/eslint-plugin": "^8.34.0",
5256
"@typescript-eslint/parser": "^8.34.0",
5357
"@vitest/coverage-v8": "^3.2.4",
54-
"dotenv": "^16.5.0",
58+
"dotenv": "17.2.0",
5559
"eslint": "^9.29.0",
5660
"tsup": "^8.5.0",
5761
"tsx": "^4.20.3",
5862
"typescript": "^5.8.3",
5963
"vite-tsconfig-paths": "^5.1.4",
60-
"vitest": "^2.1.9"
64+
"vitest": "3.2.4"
6165
},
6266
"dependencies": {
6367
"@nillion/nuc": "0.1.0-rc.8",
6468
"@noble/curves": "^1.9.2",
6569
"@noble/secp256k1": "^2.3.0",
6670
"openai": "^5.3.0",
6771
"zod": "^3.25.64"
72+
},
73+
"optionalDependencies": {
74+
"buffer": "^6.0.3"
6875
}
6976
}

pnpm-lock.yaml

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

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Import polyfills first to ensure they're available when other modules load
2+
import './polyfills';
3+
14
export { NilaiOpenAIClient } from "./client";
25
export { DelegationTokenServer } from './server';
36
export {

src/polyfills.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* Polyfills for Node.js environment compatibility
3+
* This module ensures that browser APIs like atob, btoa, Buffer, and crypto
4+
* are available in Node.js environments where they might be missing.
5+
*/
6+
7+
function ensureGlobals() {
8+
// Ensure globalThis is available (for older Node.js versions)
9+
if (typeof globalThis === 'undefined') {
10+
// @ts-ignore
11+
global.globalThis = global;
12+
}
13+
14+
// Node.js environment polyfills
15+
if (typeof globalThis !== 'undefined') {
16+
// atob polyfill for Node.js (base64 decode)
17+
if (typeof globalThis.atob === 'undefined') {
18+
globalThis.atob = function(str: string): string {
19+
try {
20+
// Use Node.js Buffer if available
21+
if (typeof Buffer !== 'undefined') {
22+
return Buffer.from(str, 'base64').toString('binary');
23+
}
24+
// Fallback for environments without Buffer
25+
throw new Error('atob not supported in this environment');
26+
} catch (error) {
27+
throw new Error(`Invalid base64 string: ${error}`);
28+
}
29+
};
30+
}
31+
32+
// btoa polyfill for Node.js (base64 encode)
33+
if (typeof globalThis.btoa === 'undefined') {
34+
globalThis.btoa = function(str: string): string {
35+
try {
36+
// Use Node.js Buffer if available
37+
if (typeof Buffer !== 'undefined') {
38+
return Buffer.from(str, 'binary').toString('base64');
39+
}
40+
// Fallback for environments without Buffer
41+
throw new Error('btoa not supported in this environment');
42+
} catch (error) {
43+
throw new Error(`Invalid string for base64 encoding: ${error}`);
44+
}
45+
};
46+
}
47+
48+
// Buffer polyfill for environments that don't have it globally
49+
if (typeof globalThis.Buffer === 'undefined') {
50+
try {
51+
// Try to import Buffer from Node.js
52+
if (typeof require !== 'undefined') {
53+
const { Buffer: NodeBuffer } = require('buffer');
54+
globalThis.Buffer = NodeBuffer;
55+
} else {
56+
// Try to use a buffer polyfill if available via dynamic import
57+
// Note: This is a best-effort approach for ESM environments
58+
import('buffer').then(({ Buffer: PolyfillBuffer }) => {
59+
globalThis.Buffer = PolyfillBuffer;
60+
}).catch(() => {
61+
// Buffer not available, some functionality may be limited
62+
console.warn('@nillion/nilai-ts: Buffer not available, some functionality may be limited');
63+
});
64+
}
65+
} catch {
66+
// Buffer not available
67+
}
68+
}
69+
70+
// Crypto polyfill for Node.js
71+
if (typeof globalThis.crypto === 'undefined') {
72+
try {
73+
if (typeof require !== 'undefined') {
74+
const crypto = require('crypto');
75+
// Use webcrypto if available (Node.js 16+), otherwise use crypto
76+
globalThis.crypto = crypto.webcrypto || crypto;
77+
}
78+
} catch {
79+
// crypto not available
80+
console.warn('@nillion/nilai-ts: crypto not available, some functionality may be limited');
81+
}
82+
}
83+
84+
// TextEncoder/TextDecoder polyfills for older Node.js versions
85+
if (typeof globalThis.TextEncoder === 'undefined') {
86+
try {
87+
if (typeof require !== 'undefined') {
88+
const util = require('util');
89+
globalThis.TextEncoder = util.TextEncoder;
90+
globalThis.TextDecoder = util.TextDecoder;
91+
}
92+
} catch {
93+
// TextEncoder/TextDecoder not available
94+
}
95+
}
96+
}
97+
}
98+
99+
// Auto-initialize when module is imported
100+
ensureGlobals();
101+
102+
export { ensureGlobals };

tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from "tsup";
22

33
export default defineConfig({
4-
entry: ["src/index.ts"],
4+
entry: ["src/index.ts", "src/polyfills.ts"],
55
format: ["esm"],
66
dts: true,
77
splitting: false,

0 commit comments

Comments
 (0)