Skip to content

Commit

Permalink
fix: handle process not defined in browser bundle (#1536)
Browse files Browse the repository at this point in the history
  • Loading branch information
cajames authored Feb 27, 2024
1 parent 5cfd597 commit d4633d7
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 10 deletions.
11 changes: 10 additions & 1 deletion packages/internal/metrics/src/utils/checkEnv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export function isTestEnvironment() {
import { isBrowser } from './browser';
import { errorBoundary } from './errorBoundary';

function isTestEnvironmentFn() {
if (isBrowser()) {
return false;
}

if (typeof process === 'undefined') {
return false;
}
Expand All @@ -7,3 +14,5 @@ export function isTestEnvironment() {
// Just use process.env.CI for now.
return process.env.JEST_WORKER_ID !== undefined;
}

export const isTestEnvironment = errorBoundary(isTestEnvironmentFn, false);
12 changes: 12 additions & 0 deletions packages/internal/metrics/src/utils/errorBoundary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ describe('errorBoundary', () => {
};
expect(errorBoundary(testFn)).not.toThrowError();
});
it('should return the fallback result for a function that throws', () => {
const testFn = (): number => {
throw new Error('test');
};
expect(errorBoundary(testFn, 3)()).toEqual(3);
});
it('should return the fallback result for an async function that throws', () => {
const testFn = async (): Promise<number> => {
throw new Error('test');
};
expect(errorBoundary(testFn, Promise.resolve(3))()).resolves.toEqual(3);
});
});
10 changes: 7 additions & 3 deletions packages/internal/metrics/src/utils/errorBoundary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export function errorBoundary<T extends (...args: any[]) => any>(fn: T): T {
export function errorBoundary<T extends (
...args: any[]) => any>(
fn: T,
fallbackResult?: ReturnType<T>,
): T {
const wrappedFunction = ((...args: Parameters<T>): ReturnType<T> => {
try {
// Execute the original function
Expand All @@ -7,13 +11,13 @@ export function errorBoundary<T extends (...args: any[]) => any>(fn: T): T {
if (result instanceof Promise) {
// Silent fail for now, in future
// we can send errors to a logging service
return result.catch(() => undefined) as ReturnType<T>;
return result.catch(() => fallbackResult) as ReturnType<T>;
}

return result;
} catch (error) {
// As above, fail silently for now
return undefined as ReturnType<T>;
return fallbackResult as ReturnType<T>;
}
}) as T;

Expand Down
2 changes: 1 addition & 1 deletion packages/internal/metrics/src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';

const IMTBL_API = 'https://api.x.immutable.com';
const IMTBL_API = 'https://api.immutable.com';

export async function post<T = any>(path: string, data: any) {
const client = axios.create({
Expand Down
1 change: 0 additions & 1 deletion packages/passport/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@types/axios": "^0.14.0",
"@types/jest": "^29.4.3",
"@types/jwt-encode": "^1.0.1",
"@types/node": "^18.14.2",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.57.1",
Expand Down
7 changes: 5 additions & 2 deletions packages/passport/sdk/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const shouldLog: boolean = process?.env?.JEST_WORKER_ID === undefined;

const warn = (...args: any[]) => {
if (typeof process === 'undefined') {
return;
}

const shouldLog: boolean = process?.env?.JEST_WORKER_ID === undefined;
if (shouldLog) {
// eslint-disable-next-line no-console
console.warn(...args);
Expand Down
3 changes: 2 additions & 1 deletion sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export default [
__SDK_VERSION__: pkg.version,

// This breaks in a dex dependency, so manually replacing it.
'process.env.NODE_ENV': '"production"'
'process.env.NODE_ENV': '"production"',
'process': 'undefined'
}),
terser(),
],
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3719,7 +3719,6 @@ __metadata:
"@types/axios": ^0.14.0
"@types/jest": ^29.4.3
"@types/jwt-encode": ^1.0.1
"@types/node": ^18.14.2
"@types/react": ^18.0.28
"@types/react-dom": ^18.0.11
"@typescript-eslint/eslint-plugin": ^5.57.1
Expand Down

0 comments on commit d4633d7

Please sign in to comment.