Skip to content

Commit

Permalink
Adjust Buffer* types
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Aug 17, 2023
1 parent 00784ab commit fa667ec
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/util/src/has.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { BufferObjClass } from './types.js';
import type { BufferClass } from './types.js';

import { BigInt } from '@polkadot/x-bigint';
import { xglobal } from '@polkadot/x-global';
Expand Down Expand Up @@ -32,7 +32,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 === 'function' && typeof (xglobal.Buffer as unknown as BufferObjClass).isBuffer === 'function';
export const hasBuffer = typeof xglobal.Buffer === 'function' && typeof (xglobal.Buffer as unknown as BufferClass).isBuffer === 'function';

/** true if the environment has process available (typically Node.js) */
export const hasProcess = typeof xglobal.process === 'object';
6 changes: 3 additions & 3 deletions packages/util/src/is/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { BufferObj, BufferObjClass } from '../types.js';
import type { BufferClass, BufferObject } from '../types.js';

import { xglobal } from '@polkadot/x-global';

Expand All @@ -22,7 +22,7 @@ import { isFunction } from './function.js';
* console.log('isBuffer', isBuffer(Buffer.from([]))); // => true
* ```
*/
export function isBuffer <T = BufferObj> (value: unknown): value is T {
export function isBuffer <T = BufferObject> (value: unknown): value is T {
// we do check a function first, since it is slightly faster than isBuffer itself
return hasBuffer && !!value && isFunction((value as unknown as BufferObj).readDoubleLE) && (xglobal.Buffer as unknown as BufferObjClass).isBuffer(value);
return hasBuffer && !!value && isFunction((value as unknown as BufferObject).readDoubleLE) && (xglobal.Buffer as unknown as BufferClass).isBuffer(value);
}
8 changes: 4 additions & 4 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export type HexDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
// One day when template strings support regex, we can improve this
export type HexString = `0x${string}`;

// BufferObj interface compatible with Buffer since we don't want to require
// BufferObject interface compatible with Buffer since we don't want to require
// references to the Buffer types from the node typings
//
// Caveat: the references still do sneak in in the d.ts files, specifically
// inside u8a/toBuffer & is/buffer (but not in compiled outputs)
export interface BufferObj extends Uint8Array {
export interface BufferObject extends Uint8Array {
// Possibly used externally via type imports
equals: (otherBuffer: Uint8Array) => boolean;
// As used in is/buffer
Expand All @@ -91,9 +91,9 @@ export interface BufferObj extends Uint8Array {

// We define a scappy low-level interface to mock Buffer
// (this removes the need for the node typings in built bundles)
export interface BufferObjClass extends Class<BufferObj> {
export interface BufferClass extends Class<BufferObject> {
// As used in u8a/toBuffer
from: <T = BufferObj>(value: unknown) => T;
from: <T = BufferObject>(value: unknown) => T;
// As used in is/buffer
isBuffer: (value: unknown) => boolean;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/util/src/u8a/toBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { BufferObj, BufferObjClass } from '../types.js';
import type { BufferClass, BufferObject } from '../types.js';

import { xglobal } from '@polkadot/x-global';

Expand All @@ -19,6 +19,6 @@ import { xglobal } from '@polkadot/x-global';
* console.log('Buffer', u8aToBuffer(new Uint8Array([1, 2, 3])));
* ```
*/
export function u8aToBuffer <T = BufferObj> (value?: Uint8Array | null): T {
return (xglobal.Buffer as unknown as BufferObjClass).from(value || []);
export function u8aToBuffer <T = BufferObject> (value?: Uint8Array | null): T {
return (xglobal.Buffer as unknown as BufferClass).from(value || []);
}

0 comments on commit fa667ec

Please sign in to comment.