Adjust & dedupe global extraction tests (#1301)

This commit is contained in:
Jaco
2021-12-08 20:23:46 +02:00
committed by GitHub
parent 306e503d23
commit 8a6c8b64df
13 changed files with 40 additions and 57 deletions
+1
View File
@@ -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
+2 -4
View File
@@ -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<string, unknown>).BigInt = BigInt;
}
exposeGlobal('BigInt', BigInt);
+2 -6
View File
@@ -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);
+2 -4
View File
@@ -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<string, unknown>).fetch = fetch;
}
exposeGlobal('fetch', fetch);
+16
View File
@@ -5,6 +5,10 @@ export { packageInfo } from './packageInfo';
type GlobalThis = typeof globalThis;
type GlobalNames = keyof typeof window;
type GlobalType<T extends keyof typeof window> = 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 <N extends GlobalNames, T extends GlobalType<N>> (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 <N extends GlobalNames> (name: N, fallback: unknown): void {
if (typeof xglobal[name as 'undefined'] === 'undefined') {
xglobal[name as 'undefined'] = fallback as undefined;
}
}
+2 -6
View File
@@ -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);
+2 -6
View File
@@ -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);
+2 -4
View File
@@ -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<string, unknown>).TextDecoder = TextDecoder;
}
exposeGlobal('TextDecoder', TextDecoder);
+2 -6
View File
@@ -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);
+3 -7
View File
@@ -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);
+2 -4
View File
@@ -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<string, unknown>).TextEncoder = TextEncoder;
}
exposeGlobal('TextEncoder', TextEncoder);
+2 -6
View File
@@ -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);
+2 -4
View File
@@ -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<string, unknown>).WebSocket = WebSocket;
}
exposeGlobal('WebSocket', WebSocket);