From 2b7e1a8267b169aa018350f0debb2838184db7db Mon Sep 17 00:00:00 2001 From: James Li Date: Thu, 16 May 2024 17:19:58 -0700 Subject: [PATCH] Make fs dynamically imported --- package-lock.json | 10 ++++++++-- package.json | 3 ++- src/dimo.ts | 41 ++++++++++++++++++++++++++++++++--------- tsconfig.json | 5 +++-- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9e7d6d3..5660bdc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,16 @@ { "name": "@dimo-network/dimo-node-sdk", - "version": "1.0.2", + "version": "1.1.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dimo-network/dimo-node-sdk", - "version": "1.0.0", + "version": "1.1.4", "license": "Apache-2.0", "dependencies": { "axios": "^1.6.7", + "fs": "^0.0.1-security", "web3": "^4.6.0" }, "devDependencies": { @@ -2863,6 +2864,11 @@ "node": ">= 6" } }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index 53dec52..37d8cb3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dimo-network/dimo-node-sdk", - "version": "1.1.3", + "version": "1.1.4", "description": "DIMO SDK for JavaScript", "main": "dist/index.js", "author": "James Li", @@ -24,6 +24,7 @@ }, "dependencies": { "axios": "^1.6.7", + "fs": "^0.0.1-security", "web3": "^4.6.0" }, "devDependencies": { diff --git a/src/dimo.ts b/src/dimo.ts index 2940cd1..7600189 100644 --- a/src/dimo.ts +++ b/src/dimo.ts @@ -1,5 +1,4 @@ import { DimoEnvironment } from './environments'; -import fs from 'fs'; import { Identity, @@ -54,13 +53,37 @@ export class DIMO { // Helper Function async authenticate() { - const credentials = JSON.parse(fs.readFileSync('.credentials.json', 'utf8')); - // Call getToken with credentials - const authHeader = await this.auth.getToken({ - client_id: credentials.client_id, - domain: credentials.redirect_uri, - private_key: credentials.private_key, - }); - return authHeader; + let fs; + try { + // Dynamically import fs + if (typeof process !== 'undefined' && process.versions && process.versions.node) { + fs = await import('fs'); + } else { + // Optionally handle the case where 'fs' is not available, returns null + console.log('Not in Node.js environment; `fs` module is not available.'); + return null; + } + + if (!fs.existsSync('.credentials.json')) { + throw new Error('Credentials file does not exist'); + } + + const data = fs.readFileSync('.credentials.json', 'utf8'); + const credentials = JSON.parse(data); + + const authHeader = await this.auth.getToken({ + client_id: credentials.client_id, + domain: credentials.redirect_uri, + private_key: credentials.private_key, + }); + + return authHeader; + + } catch (error: any) { + // Handle file not existing and other errors + console.error('Failed to authenticate:', error.message); + // Decide whether to throw the error or handle it differently + throw new Error('Authentication failed'); + } } } diff --git a/tsconfig.json b/tsconfig.json index 2fde5c0..cec5dc2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,10 +11,11 @@ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ "strict": true, /* Enable all strict type-checking options. */ "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ + "skipLibCheck": true, /* Skip type checking all .d.ts files. */ + "types": ["node"] }, "include": [ - "src" + "src", "node" ], "exclude": [], "ts-node": {