Adjust Buffer check (#1701)

* Adjust Buffer check

* Adjust
This commit is contained in:
Jaco
2023-01-07 12:12:43 +02:00
committed by GitHub
parent 0b11d5b9c1
commit ceff8cc9ff
3 changed files with 22 additions and 2 deletions
+7
View File
@@ -1,5 +1,12 @@
# CHANGELOG
## master
Changes:
- Adjust `isBuffer` check on `xglobal` (no auto-polyfill detection)
## 10.2.2 Jan 6, 2023
Contributed:
+7 -1
View File
@@ -11,6 +11,12 @@ declare const __dirname: unknown;
declare const module: unknown;
declare const require: unknown;
// We define a scappy low-level interface to mock Buffer
// (this removes the need for the node typings)
type BufferClass = ((value: unknown) => unknown) & {
isBuffer: (value: unknown) => boolean;
}
/** true if the environment has proper BigInt support */
export const hasBigInt = typeof BigInt === 'function' && typeof BigInt.asIntN === 'function';
@@ -30,7 +36,7 @@ export const hasWasm = typeof WebAssembly !== 'undefined';
// 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';
export const hasBuffer = typeof xglobal.Buffer === 'function' && typeof (xglobal.Buffer as BufferClass).isBuffer === 'function';
/** true if the environment has process available (typically Node.js) */
export const hasProcess = typeof xglobal.process === 'object';
+8 -1
View File
@@ -1,9 +1,16 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { xglobal } from '@polkadot/x-global';
import { hasBuffer } from '../has';
import { isFunction } from './function';
// We define these interfaces to not have the Buffer type
// (doing this removes the need for node types being available)
interface BufTyp { isBuffer: (value: unknown) => boolean }
interface BufObj { readDoubleLE: (...args: unknown[]) => unknown }
/**
* @name isBuffer
* @summary Tests for a `Buffer` object instance.
@@ -20,5 +27,5 @@ import { isFunction } from './function';
*/
export function isBuffer (value: unknown): value is Buffer {
// we do check a function first, since it is slightly faster than isBuffer itself
return hasBuffer && isFunction(value && (value as Buffer).readDoubleLE) && Buffer.isBuffer(value);
return hasBuffer && isFunction(value && (value as BufObj).readDoubleLE) && (xglobal.Buffer as BufTyp).isBuffer(value);
}