Skip to content

Commit c97380d

Browse files
committed
Be selective about instanceof
1 parent 7fdd84e commit c97380d

File tree

4 files changed

+46
-41
lines changed

4 files changed

+46
-41
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
// The GraphQL.js version info.
3030
export { version, versionInfo } from './version.js';
31+
export { setEnv } from './utilities/env.js';
32+
export type { Env } from './utilities/env.js';
3133

3234
// The primary entry point into fulfilling a GraphQL request.
3335
export type { GraphQLArgs } from './graphql.js';

src/jsutils/__tests__/instanceOf-test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { expect } from 'chai';
2-
import { describe, it } from 'mocha';
2+
import { beforeEach, describe, it } from 'mocha';
3+
4+
import { setEnv } from '../../utilities/env.js';
35

46
import { instanceOf } from '../instanceOf.js';
57

68
describe('instanceOf', () => {
9+
beforeEach(() => {
10+
setEnv('development');
11+
});
12+
713
it('do not throw on values without prototype', () => {
814
class Foo {
915
get [Symbol.toStringTag]() {

src/jsutils/instanceOf.ts

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
1+
import { isProduction } from '../utilities/env.js';
2+
13
import { inspect } from './inspect.js';
24

3-
/* c8 ignore next 3 */
4-
const isProduction =
5-
globalThis.process != null &&
6-
// eslint-disable-next-line no-undef
7-
process.env.NODE_ENV === 'production';
8-
9-
/**
10-
* A replacement for instanceof which includes an error warning when multi-realm
11-
* constructors are detected.
12-
* See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
13-
* See: https://webpack.js.org/guides/production/
14-
*/
15-
export const instanceOf: (value: unknown, constructor: Constructor) => boolean =
16-
/* c8 ignore next 6 */
17-
// FIXME: https://github.com/graphql/graphql-js/issues/2317
18-
isProduction
19-
? function instanceOf(value: unknown, constructor: Constructor): boolean {
20-
return value instanceof constructor;
21-
}
22-
: function instanceOf(value: unknown, constructor: Constructor): boolean {
23-
if (value instanceof constructor) {
24-
return true;
25-
}
26-
if (typeof value === 'object' && value !== null) {
27-
// Prefer Symbol.toStringTag since it is immune to minification.
28-
const className = constructor.prototype[Symbol.toStringTag];
29-
const valueClassName =
30-
// We still need to support constructor's name to detect conflicts with older versions of this library.
31-
Symbol.toStringTag in value
32-
? value[Symbol.toStringTag]
33-
: value.constructor?.name;
34-
if (className === valueClassName) {
35-
const stringifiedValue = inspect(value);
36-
throw new Error(
37-
`Cannot use ${className} "${stringifiedValue}" from another module or realm.
5+
export function instanceOf(value: unknown, constructor: Constructor): boolean {
6+
if (isProduction()) {
7+
return value instanceof constructor;
8+
}
9+
10+
if (value instanceof constructor) {
11+
return true;
12+
}
13+
14+
if (typeof value === 'object' && value !== null) {
15+
// Prefer Symbol.toStringTag since it is immune to minification.
16+
const className = constructor.prototype[Symbol.toStringTag];
17+
const valueClassName =
18+
// We still need to support constructor's name to detect conflicts with older versions of this library.
19+
Symbol.toStringTag in value
20+
? value[Symbol.toStringTag]
21+
: value.constructor?.name;
22+
if (className === valueClassName) {
23+
const stringifiedValue = inspect(value);
24+
throw new Error(
25+
`Cannot use ${className} "${stringifiedValue}" from another module or realm.
3826
3927
Ensure that there is only one instance of "graphql" in the node_modules
4028
directory. If different versions of "graphql" are the dependencies of other
@@ -46,11 +34,11 @@ Duplicate "graphql" modules cannot be used at the same time since different
4634
versions may have different capabilities and behavior. The data from one
4735
version used in the function from another could produce confusing and
4836
spurious results.`,
49-
);
50-
}
51-
}
52-
return false;
53-
};
37+
);
38+
}
39+
}
40+
return false;
41+
}
5442

5543
interface Constructor {
5644
prototype: {

src/utilities/env.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type Env = 'production' | 'development';
2+
3+
let env: Env = 'production';
4+
5+
export const setEnv = (newEnv: Env): void => {
6+
env = newEnv;
7+
};
8+
9+
export const isProduction = (): boolean => env === 'production';

0 commit comments

Comments
 (0)