utils.ts
4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { Buffer } from 'buffer';
import { getGlobal } from '../utils/global';
type RandomBytesFunction = (size: number) => Uint8Array;
/**
* Normalizes our expected stringified form of a function across versions of node
* @param fn - The function to stringify
*/
export function normalizedFunctionString(fn: Function): string {
return fn.toString().replace('function(', 'function (');
}
function isReactNative() {
const g = getGlobal<{ navigator?: { product?: string } }>();
return typeof g.navigator === 'object' && g.navigator.product === 'ReactNative';
}
const insecureRandomBytes: RandomBytesFunction = function insecureRandomBytes(size: number) {
const insecureWarning = isReactNative()
? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
: 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.';
console.warn(insecureWarning);
const result = Buffer.alloc(size);
for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256);
return result;
};
/* We do not want to have to include DOM types just for this check */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let window: any;
declare let require: Function;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let global: any;
declare const self: unknown;
const detectRandomBytes = (): RandomBytesFunction => {
if (typeof window !== 'undefined') {
// browser crypto implementation(s)
const target = window.crypto || window.msCrypto; // allow for IE11
if (target && target.getRandomValues) {
return size => target.getRandomValues(Buffer.alloc(size));
}
}
if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
// allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
return size => global.crypto.getRandomValues(Buffer.alloc(size));
}
let requiredRandomBytes: RandomBytesFunction | null | undefined;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
requiredRandomBytes = require('crypto').randomBytes;
} catch (e) {
// keep the fallback
}
// NOTE: in transpiled cases the above require might return null/undefined
return requiredRandomBytes || insecureRandomBytes;
};
export const randomBytes = detectRandomBytes();
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(
Object.prototype.toString.call(value)
);
}
export function isUint8Array(value: unknown): value is Uint8Array {
return Object.prototype.toString.call(value) === '[object Uint8Array]';
}
export function isBigInt64Array(value: unknown): value is BigInt64Array {
return Object.prototype.toString.call(value) === '[object BigInt64Array]';
}
export function isBigUInt64Array(value: unknown): value is BigUint64Array {
return Object.prototype.toString.call(value) === '[object BigUint64Array]';
}
export function isRegExp(d: unknown): d is RegExp {
return Object.prototype.toString.call(d) === '[object RegExp]';
}
export function isMap(d: unknown): d is Map<unknown, unknown> {
return Object.prototype.toString.call(d) === '[object Map]';
}
/** Call to check if your environment has `Buffer` */
export function haveBuffer(): boolean {
return typeof global !== 'undefined' && typeof global.Buffer !== 'undefined';
}
// To ensure that 0.4 of node works correctly
export function isDate(d: unknown): d is Date {
return isObjectLike(d) && Object.prototype.toString.call(d) === '[object Date]';
}
/**
* @internal
* this is to solve the `'someKey' in x` problem where x is unknown.
* https://github.com/typescript-eslint/typescript-eslint/issues/1071#issuecomment-541955753
*/
export function isObjectLike(candidate: unknown): candidate is Record<string, unknown> {
return typeof candidate === 'object' && candidate !== null;
}
declare let console: { warn(...message: unknown[]): void };
export function deprecate<T extends Function>(fn: T, message: string): T {
let warned = false;
function deprecated(this: unknown, ...args: unknown[]) {
if (!warned) {
console.warn(message);
warned = true;
}
return fn.apply(this, args);
}
return deprecated as unknown as T;
}