From 80eb70a3822db6a257cb47dfa0e939e80e63a702 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 4 Jul 2022 09:20:24 +0200 Subject: [PATCH] Adjust some usages to not require @types/node (#1576) --- CHANGELOG.md | 7 +++++++ packages/util/src/has.ts | 9 +++++++++ packages/util/src/logger.ts | 2 ++ packages/x-global/src/index.ts | 10 ++++++++-- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4a6d11f2..4d4b1b4e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## master + +Changes: + +- Allow for usage without `@types/node` installed + + ## 9.7.2 Jul 4, 2022 Changes: diff --git a/packages/util/src/has.ts b/packages/util/src/has.ts index fea24b17b..ae6e780bd 100644 --- a/packages/util/src/has.ts +++ b/packages/util/src/has.ts @@ -3,6 +3,15 @@ import { BigInt } from '@polkadot/x-bigint'; +// 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'; diff --git a/packages/util/src/logger.ts b/packages/util/src/logger.ts index f02ef083f..53a4d60fe 100644 --- a/packages/util/src/logger.ts +++ b/packages/util/src/logger.ts @@ -13,6 +13,8 @@ import { u8aToHex } from './u8a/toHex'; import { u8aToU8a } from './u8a/toU8a'; import { hasProcess } from './has'; +declare const process: { env: Record }; + type ConsoleType = 'error' | 'log' | 'warn'; type LogType = ConsoleType | 'debug'; diff --git a/packages/x-global/src/index.ts b/packages/x-global/src/index.ts index 9068e8c54..1df08c5d3 100644 --- a/packages/x-global/src/index.ts +++ b/packages/x-global/src/index.ts @@ -3,11 +3,17 @@ export { packageInfo } from './packageInfo'; +// Ensure that we are able to run this without any @types/node definitions +// and without having lib: ['dom'] in our TypeScript configuration +// (may not be available in all environments, e.g. Deno springs to mind) +declare const global: typeof globalThis; +declare const self: typeof globalThis; + type GlobalThis = typeof globalThis; -type GlobalNames = keyof typeof window; +type GlobalNames = keyof GlobalThis; -type GlobalType = typeof window[T]; +type GlobalType = typeof globalThis[T]; function evaluateThis (fn: (code: string) => unknown): GlobalThis { return fn('return this') as GlobalThis;