diff --git a/.changeset/funny-jars-camp.md b/.changeset/funny-jars-camp.md new file mode 100644 index 0000000000..55be951fa8 --- /dev/null +++ b/.changeset/funny-jars-camp.md @@ -0,0 +1,8 @@ +--- +"@near-js/accounts": patch +"near-api-js": patch +"@near-js/providers": patch +"@near-js/utils": patch +--- + +add check for global 'process' object diff --git a/.changeset/wet-jars-attend.md b/.changeset/wet-jars-attend.md new file mode 100644 index 0000000000..96a008cc77 --- /dev/null +++ b/.changeset/wet-jars-attend.md @@ -0,0 +1,6 @@ +--- +"near-api-js": patch +"@near-js/providers": patch +--- + +fixes override of global fetch property diff --git a/packages/accounts/src/account.ts b/packages/accounts/src/account.ts index 2206212a74..3ae6c38abd 100644 --- a/packages/accounts/src/account.ts +++ b/packages/accounts/src/account.ts @@ -363,7 +363,7 @@ export class Account { * @param beneficiaryId The NEAR account that will receive the remaining Ⓝ balance from the account being deleted */ async deleteAccount(beneficiaryId: string) { - if (!process.env['NEAR_NO_LOGS']) { + if (!(typeof process === 'object' && process.env['NEAR_NO_LOGS'])) { console.log('Deleting an account does not automatically transfer NFTs and FTs to the beneficiary address. Ensure to transfer assets before deleting.'); } return this.signAndSendTransaction({ diff --git a/packages/near-api-js/src/connect.ts b/packages/near-api-js/src/connect.ts index cc4f36e3fc..f46971eee1 100644 --- a/packages/near-api-js/src/connect.ts +++ b/packages/near-api-js/src/connect.ts @@ -25,11 +25,8 @@ import { readKeyFile } from './key_stores/unencrypted_file_system_keystore'; import { InMemoryKeyStore, MergeKeyStore } from './key_stores'; import { Near, NearConfig } from './near'; -import fetch from './utils/setup-node-fetch'; import { logWarning } from './utils'; -global.fetch = fetch; - export interface ConnectConfig extends NearConfig { /** * Initialize an {@link key_stores/in_memory_key_store!InMemoryKeyStore} by reading the file at keyPath. @@ -57,7 +54,7 @@ export async function connect(config: ConnectConfig): Promise { keyPathStore, config.keyStore || config.deps?.keyStore ], { writeKeyStoreIndex: 1 }); - if (!process.env['NEAR_NO_LOGS']) { + if (!(typeof process === 'object' && process.env['NEAR_NO_LOGS'])) { console.log(`Loaded master account ${accountKeyFile[0]} key from ${config.keyPath} with public key = ${keyPair.getPublicKey()}`); } } diff --git a/packages/providers/src/fetch_json.ts b/packages/providers/src/fetch_json.ts index 386143c14c..bef1a029dc 100644 --- a/packages/providers/src/fetch_json.ts +++ b/packages/providers/src/fetch_json.ts @@ -16,7 +16,7 @@ export interface ConnectionInfo { headers?: { [key: string]: string | number }; } -const logWarning = (...args) => !process.env['NEAR_NO_LOGS'] && console.warn(...args); +const logWarning = (...args) => !(typeof process === 'object' && process.env['NEAR_NO_LOGS']) && console.warn(...args); export async function fetchJson(connectionInfoOrUrl: string | ConnectionInfo, json?: string): Promise { let connectionInfo: ConnectionInfo = { url: null }; @@ -28,11 +28,8 @@ export async function fetchJson(connectionInfoOrUrl: string | ConnectionInfo, js const response = await exponentialBackoff(START_WAIT_TIME_MS, RETRY_NUMBER, BACKOFF_MULTIPLIER, async () => { try { - if (!global.fetch) { - global.fetch = (await import('./fetch')).default; - } - const response = await global.fetch(connectionInfo.url, { + const response = await (global.fetch ?? (await import('./fetch')).default)(connectionInfo.url, { method: json ? 'POST' : 'GET', body: json ? json : undefined, headers: { ...connectionInfo.headers, 'Content-Type': 'application/json' } diff --git a/packages/providers/src/json-rpc-provider.ts b/packages/providers/src/json-rpc-provider.ts index ed379a5a9a..540e89c27b 100644 --- a/packages/providers/src/json-rpc-provider.ts +++ b/packages/providers/src/json-rpc-provider.ts @@ -372,7 +372,7 @@ export class JsonRpcProvider extends Provider { return response; } catch (error) { if (error.type === 'TimeoutError') { - if (!process.env['NEAR_NO_LOGS']) { + if (!(typeof process === 'object' && process.env['NEAR_NO_LOGS'])) { console.warn(`Retrying request to ${method} as it has timed out`, params); } return null; diff --git a/packages/utils/src/errors/errors.ts b/packages/utils/src/errors/errors.ts index d6c9c5bbc3..8173ea2e65 100644 --- a/packages/utils/src/errors/errors.ts +++ b/packages/utils/src/errors/errors.ts @@ -1,5 +1,5 @@ export function logWarning(...args: any[]): void { - if (!process.env['NEAR_NO_LOGS']){ + if (!(typeof process === 'object' && process.env['NEAR_NO_LOGS'])){ console.warn(...args); } } diff --git a/packages/utils/src/logging.ts b/packages/utils/src/logging.ts index 5024a7559f..4b134562b7 100644 --- a/packages/utils/src/logging.ts +++ b/packages/utils/src/logging.ts @@ -2,7 +2,7 @@ import { FinalExecutionOutcome } from '@near-js/types'; import { parseRpcError } from './errors'; -const SUPPRESS_LOGGING = !!process.env.NEAR_NO_LOGS; +const SUPPRESS_LOGGING = !!(typeof process === 'object' && process.env.NEAR_NO_LOGS); /** * Parse and print details from a query execution response