diff --git a/CHANGELOG.md b/CHANGELOG.md index b392b7c82..73e605d08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Changes: - Expose `assertUnreachable` for codepaths that should not be followed - Swap to upstream `@noble/hashes` - Preserve all-caps word splits in `stringCamelCase` +- Adjust `x-*` fallbacks via `x-global/extractGlobal` - Sync with upstream Substrate ss58 registry - Maintainability cleanups diff --git a/packages/x-bigint/src/shim.ts b/packages/x-bigint/src/shim.ts index 30cea0080..262947c3b 100644 --- a/packages/x-bigint/src/shim.ts +++ b/packages/x-bigint/src/shim.ts @@ -2,8 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 import { BigInt } from '@polkadot/x-bigint'; -import { xglobal } from '@polkadot/x-global'; +import { exposeGlobal } from '@polkadot/x-global'; -if (typeof xglobal.BigInt !== 'function') { - (xglobal as Record).BigInt = BigInt; -} +exposeGlobal('BigInt', BigInt); diff --git a/packages/x-fetch/src/node.ts b/packages/x-fetch/src/node.ts index b2c9fe3f0..eab52947a 100644 --- a/packages/x-fetch/src/node.ts +++ b/packages/x-fetch/src/node.ts @@ -3,12 +3,8 @@ import nodeFetch from 'node-fetch'; -import { xglobal } from '@polkadot/x-global'; +import { extractGlobal } from '@polkadot/x-global'; export { packageInfo } from './packageInfo'; -export const fetch = ( - typeof xglobal.fetch === 'undefined' - ? nodeFetch as unknown as typeof global.fetch - : xglobal.fetch -); +export const fetch = extractGlobal('fetch', nodeFetch); diff --git a/packages/x-fetch/src/shim.ts b/packages/x-fetch/src/shim.ts index 940628d66..f286afbf8 100644 --- a/packages/x-fetch/src/shim.ts +++ b/packages/x-fetch/src/shim.ts @@ -2,8 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 import { fetch } from '@polkadot/x-fetch'; -import { xglobal } from '@polkadot/x-global'; +import { exposeGlobal } from '@polkadot/x-global'; -if (typeof xglobal.fetch !== 'function') { - (xglobal as Record).fetch = fetch; -} +exposeGlobal('fetch', fetch); diff --git a/packages/x-global/src/index.ts b/packages/x-global/src/index.ts index ec9943243..fed6ca841 100644 --- a/packages/x-global/src/index.ts +++ b/packages/x-global/src/index.ts @@ -5,6 +5,10 @@ export { packageInfo } from './packageInfo'; type GlobalThis = typeof globalThis; +type GlobalNames = keyof typeof window; + +type GlobalType = typeof window[T]; + function evaluateThis (fn: (code: string) => unknown): GlobalThis { return fn('return this') as GlobalThis; } @@ -20,3 +24,15 @@ export const xglobal = ( ? window : evaluateThis(Function) ); + +export function extractGlobal > (name: N, fallback: unknown): T { + return typeof xglobal[name as 'undefined'] === 'undefined' + ? fallback as T + : xglobal[name as 'undefined'] as unknown as T; +} + +export function exposeGlobal (name: N, fallback: unknown): void { + if (typeof xglobal[name as 'undefined'] === 'undefined') { + xglobal[name as 'undefined'] = fallback as undefined; + } +} diff --git a/packages/x-textdecoder/src/browser.ts b/packages/x-textdecoder/src/browser.ts index fa4913594..1cc8fa070 100644 --- a/packages/x-textdecoder/src/browser.ts +++ b/packages/x-textdecoder/src/browser.ts @@ -1,14 +1,10 @@ // Copyright 2017-2021 @polkadot/x-textencoder authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { xglobal } from '@polkadot/x-global'; +import { extractGlobal } from '@polkadot/x-global'; import { TextDecoder as Fallback } from './fallback'; export { packageInfo } from './packageInfo'; -export const TextDecoder = ( - typeof xglobal.TextDecoder === 'undefined' - ? Fallback as unknown as typeof xglobal.TextDecoder - : xglobal.TextDecoder -); +export const TextDecoder = extractGlobal('TextDecoder', Fallback); diff --git a/packages/x-textdecoder/src/node.ts b/packages/x-textdecoder/src/node.ts index 9d0719668..712d78b39 100644 --- a/packages/x-textdecoder/src/node.ts +++ b/packages/x-textdecoder/src/node.ts @@ -3,12 +3,8 @@ import util from 'util'; -import { xglobal } from '@polkadot/x-global'; +import { extractGlobal } from '@polkadot/x-global'; export { packageInfo } from './packageInfo'; -export const TextDecoder = ( - typeof xglobal.TextDecoder === 'undefined' - ? util.TextDecoder as unknown as typeof xglobal.TextDecoder - : xglobal.TextDecoder -); +export const TextDecoder = extractGlobal('TextDecoder', util.TextDecoder); diff --git a/packages/x-textdecoder/src/shim.ts b/packages/x-textdecoder/src/shim.ts index 7b4b02165..1a2fd2a08 100644 --- a/packages/x-textdecoder/src/shim.ts +++ b/packages/x-textdecoder/src/shim.ts @@ -1,9 +1,7 @@ // Copyright 2017-2021 @polkadot/x-textdecoder authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { xglobal } from '@polkadot/x-global'; +import { exposeGlobal } from '@polkadot/x-global'; import { TextDecoder } from '@polkadot/x-textdecoder'; -if (typeof xglobal.TextDecoder !== 'function') { - (xglobal as Record).TextDecoder = TextDecoder; -} +exposeGlobal('TextDecoder', TextDecoder); diff --git a/packages/x-textencoder/src/browser.ts b/packages/x-textencoder/src/browser.ts index 981692c96..8724690f0 100644 --- a/packages/x-textencoder/src/browser.ts +++ b/packages/x-textencoder/src/browser.ts @@ -1,14 +1,10 @@ // Copyright 2017-2021 @polkadot/x-textencoder authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { xglobal } from '@polkadot/x-global'; +import { extractGlobal } from '@polkadot/x-global'; import { TextEncoder as Fallback } from './fallback'; export { packageInfo } from './packageInfo'; -export const TextEncoder = ( - typeof xglobal.TextEncoder === 'undefined' - ? Fallback as unknown as typeof xglobal.TextEncoder - : xglobal.TextEncoder -); +export const TextEncoder = extractGlobal('TextEncoder', Fallback); diff --git a/packages/x-textencoder/src/node.ts b/packages/x-textencoder/src/node.ts index 286eb3aa3..6ad59a854 100644 --- a/packages/x-textencoder/src/node.ts +++ b/packages/x-textencoder/src/node.ts @@ -3,11 +3,11 @@ import util from 'util'; -import { xglobal } from '@polkadot/x-global'; +import { extractGlobal } from '@polkadot/x-global'; export { packageInfo } from './packageInfo'; -class NodeFallback { +class Fallback { #encoder: util.TextEncoder; constructor () { @@ -20,8 +20,4 @@ class NodeFallback { } } -export const TextEncoder = ( - typeof xglobal.TextEncoder === 'undefined' - ? NodeFallback as unknown as typeof xglobal.TextEncoder - : xglobal.TextEncoder -); +export const TextEncoder = extractGlobal('TextEncoder', Fallback); diff --git a/packages/x-textencoder/src/shim.ts b/packages/x-textencoder/src/shim.ts index 8b18983fe..1507cec56 100644 --- a/packages/x-textencoder/src/shim.ts +++ b/packages/x-textencoder/src/shim.ts @@ -1,9 +1,7 @@ // Copyright 2017-2021 @polkadot/x-textencoder authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { xglobal } from '@polkadot/x-global'; +import { exposeGlobal } from '@polkadot/x-global'; import { TextEncoder } from '@polkadot/x-textencoder'; -if (typeof xglobal.TextEncoder !== 'function') { - (xglobal as Record).TextEncoder = TextEncoder; -} +exposeGlobal('TextEncoder', TextEncoder); diff --git a/packages/x-ws/src/node.ts b/packages/x-ws/src/node.ts index 0604c3ce6..b10dd3996 100644 --- a/packages/x-ws/src/node.ts +++ b/packages/x-ws/src/node.ts @@ -3,12 +3,8 @@ import ws from 'websocket'; -import { xglobal } from '@polkadot/x-global'; +import { extractGlobal } from '@polkadot/x-global'; export { packageInfo } from './packageInfo'; -export const WebSocket = ( - typeof xglobal.WebSocket === 'undefined' - ? ws.w3cwebsocket as unknown as typeof xglobal.WebSocket - : xglobal.WebSocket -); +export const WebSocket = extractGlobal('WebSocket', ws.w3cwebsocket); diff --git a/packages/x-ws/src/shim.ts b/packages/x-ws/src/shim.ts index 526430ec5..9bc655a33 100644 --- a/packages/x-ws/src/shim.ts +++ b/packages/x-ws/src/shim.ts @@ -1,9 +1,7 @@ // Copyright 2017-2021 @polkadot/x-ws authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { xglobal } from '@polkadot/x-global'; +import { exposeGlobal } from '@polkadot/x-global'; import { WebSocket } from '@polkadot/x-ws'; -if (typeof xglobal.WebSocket !== 'function') { - (xglobal as Record).WebSocket = WebSocket; -} +exposeGlobal('WebSocket', WebSocket);