Adjust has{Buffer, Process} checks w/ globalThis (#1683)

This commit is contained in:
Jaco
2022-12-02 09:42:41 +02:00
committed by GitHub
parent 1ad77eb58c
commit 984a4a1ae8
4 changed files with 34 additions and 11 deletions
+11
View File
@@ -1,5 +1,16 @@
# CHANGELOG
## master
Contributed:
- Ledger support for Ajuna (Thanks to https://github.com/carlosala)
Changes:
- `has{Buffer, Process}` now checks on `globalThis` (helps bundlers with auto-injection)
## 10.1.14 Nov 27, 2022
Changes:
+10
View File
@@ -0,0 +1,10 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { hasBuffer } from './has';
describe('hasBuffer', (): void => {
it('has Buffer (Jest + Node.js)', (): void => {
expect(hasBuffer).toEqual(typeof Buffer !== 'undefined');
});
});
+10 -8
View File
@@ -2,22 +2,18 @@
// SPDX-License-Identifier: Apache-2.0
import { BigInt } from '@polkadot/x-bigint';
import { xglobal } from '@polkadot/x-global';
// Since we run in very different environments, we have to ensure we have all
// the types used here for detection (some of these may require Node definitions,
// which are not available in Deno/browser)
declare const __dirname: unknown;
declare const Buffer: unknown;
declare const module: unknown;
declare const process: unknown;
declare const require: unknown;
/** true if the environment has proper BigInt support */
export const hasBigInt = typeof BigInt === 'function' && typeof BigInt.asIntN === 'function';
/** true if the environment has support for Buffer */
export const hasBuffer = typeof Buffer !== 'undefined';
/** true if the environment is CJS */
export const hasCjs = typeof require === 'function' && typeof module !== 'undefined';
@@ -27,8 +23,14 @@ export const hasDirname = typeof __dirname !== 'undefined';
/** true if the environment is ESM */
export const hasEsm = !hasCjs;
/** true if the environment has process available (typically Node.js) */
export const hasProcess = typeof process === 'object';
/** true if the environment has WebAssembly available */
export const hasWasm = typeof WebAssembly !== 'undefined';
// NOTE We check the following on globalThis, avoiding specific polyfill detection
// that some bundlers such as parcel would add (this is a check, not a use)
/** true if the environment has support for Buffer (typically Node.js) */
export const hasBuffer = typeof xglobal.Buffer !== 'undefined';
/** true if the environment has process available (typically Node.js) */
export const hasProcess = typeof xglobal.process === 'object';
+3 -3
View File
@@ -3,6 +3,8 @@
import type { Logger, Logger$Data } from './types';
import { xglobal } from '@polkadot/x-global';
import { formatDate } from './format/formatDate';
import { isBn } from './is/bn';
import { isBuffer } from './is/buffer';
@@ -13,8 +15,6 @@ import { u8aToHex } from './u8a/toHex';
import { u8aToU8a } from './u8a/toU8a';
import { hasProcess } from './has';
declare const process: { env: Record<string, string> };
type ConsoleType = 'error' | 'log' | 'warn';
type LogType = ConsoleType | 'debug';
@@ -124,7 +124,7 @@ function getDebugFlag (env: readonly string[], type: string): boolean {
}
function parseEnv (type: string): [boolean, number] {
const env = (hasProcess ? process : {}).env || {};
const env = (hasProcess ? xglobal.process as { env: Record<string, string> } : {}).env || {};
const maxSize = parseInt(env.DEBUG_MAX || '-1', 10);
return [