Skip to content

Commit

Permalink
chore: Remove AbortController polyfill (#1726)
Browse files Browse the repository at this point in the history
## Explanation

The `AbortController` polyfill has not been required since updating to a
minimum Node.js version of v16 in #1262. This API has been [built into
Node.js since
v15.4.0](https://nodejs.org/docs/latest-v16.x/api/globals.html#class-abortcontroller),
and is included in all versions of v16. It is also supported by [all
browsers that our extension
supports](https://developer.mozilla.org/en-US/docs/Web/API/AbortController#browser_compatibility),
and has been [supported by React Native since
v0.60.0](https://github.com/react-native-community/releases/blob/master/CHANGELOG.md#v0600).

This helps unblock the TypeScript update to v4.8.4. The update resulted
in some type errors from this polyfill package.

## References
None

## Changelog

### `@metamask/assets-controllers`

### Removed
- **BREAKING:** Remove AbortController polyfill
  - This package now assumes that the AbortController global exists

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
Gudahtt authored and MajorLift committed Oct 11, 2023
1 parent f85cc4d commit 34fba60
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 41 deletions.
1 change: 0 additions & 1 deletion packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@metamask/rpc-errors": "^5.1.1",
"@metamask/utils": "^6.2.0",
"@types/uuid": "^8.3.0",
"abort-controller": "^3.0.0",
"async-mutex": "^0.2.6",
"ethereumjs-util": "^7.0.10",
"immer": "^9.0.6",
Expand Down
7 changes: 3 additions & 4 deletions packages/assets-controllers/src/TokenListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
NetworkState,
} from '@metamask/network-controller';
import type { Hex } from '@metamask/utils';
import { AbortController as WhatwgAbortController } from 'abort-controller';
import { Mutex } from 'async-mutex';
import type { Patch } from 'immer';

Expand Down Expand Up @@ -96,7 +95,7 @@ export class TokenListController extends BaseControllerV2<

private chainId: Hex;

private abortController: WhatwgAbortController;
private abortController: AbortController;

/**
* Creates a TokenListController instance.
Expand Down Expand Up @@ -139,7 +138,7 @@ export class TokenListController extends BaseControllerV2<
this.cacheRefreshThreshold = cacheRefreshThreshold;
this.chainId = chainId;
this.updatePreventPollingOnNetworkRestart(preventPollingOnNetworkRestart);
this.abortController = new WhatwgAbortController();
this.abortController = new AbortController();
if (onNetworkStateChange) {
onNetworkStateChange(async (networkControllerState) => {
await this.#onNetworkControllerStateChange(networkControllerState);
Expand All @@ -163,7 +162,7 @@ export class TokenListController extends BaseControllerV2<
async #onNetworkControllerStateChange(networkControllerState: NetworkState) {
if (this.chainId !== networkControllerState.providerConfig.chainId) {
this.abortController.abort();
this.abortController = new WhatwgAbortController();
this.abortController = new AbortController();
this.chainId = networkControllerState.providerConfig.chainId;
if (this.state.preventPollingOnNetworkRestart) {
this.clearingTokenListData();
Expand Down
7 changes: 3 additions & 4 deletions packages/assets-controllers/src/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import type {
} from '@metamask/network-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import type { Hex } from '@metamask/utils';
import { AbortController as WhatwgAbortController } from 'abort-controller';
import { Mutex } from 'async-mutex';
import { EventEmitter } from 'events';
import { v1 as random } from 'uuid';
Expand Down Expand Up @@ -125,7 +124,7 @@ export class TokensController extends BaseController<
> {
private readonly mutex = new Mutex();

private abortController: WhatwgAbortController;
private abortController: AbortController;

private readonly messagingSystem: TokensControllerMessenger;

Expand Down Expand Up @@ -231,7 +230,7 @@ export class TokensController extends BaseController<
};

this.initialize();
this.abortController = new WhatwgAbortController();
this.abortController = new AbortController();
this.getERC20TokenName = getERC20TokenName;
this.getNetworkClientById = getNetworkClientById;

Expand All @@ -253,7 +252,7 @@ export class TokensController extends BaseController<
const { selectedAddress } = this.config;
const { chainId } = providerConfig;
this.abortController.abort();
this.abortController = new WhatwgAbortController();
this.abortController = new AbortController();
this.configure({ chainId });
this.update({
tokens: allTokens[chainId]?.[selectedAddress] || [],
Expand Down
25 changes: 12 additions & 13 deletions packages/assets-controllers/src/token-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { toHex } from '@metamask/controller-utils';
import { AbortController as WhatwgAbortController } from 'abort-controller';
import nock from 'nock';

import {
Expand Down Expand Up @@ -140,7 +139,7 @@ const sampleChainId = toHex(sampleDecimalChainId);
describe('Token service', () => {
describe('fetchTokenList', () => {
it('should call the tokens api and return the list of tokens', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
.reply(200, sampleTokenList)
Expand All @@ -152,7 +151,7 @@ describe('Token service', () => {
});

it('should return undefined if the fetch is aborted', async () => {
const abortController = new WhatwgAbortController();
const abortController = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
// well beyond time it will take to abort
Expand All @@ -170,7 +169,7 @@ describe('Token service', () => {
});

it('should return undefined if the fetch fails with a network error', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
.replyWithError('Example network error')
Expand All @@ -182,7 +181,7 @@ describe('Token service', () => {
});

it('should return undefined if the fetch fails with an unsuccessful status code', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
.reply(500)
Expand All @@ -194,7 +193,7 @@ describe('Token service', () => {
});

it('should return undefined if the fetch fails with a timeout', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
// well beyond timeout
Expand All @@ -212,7 +211,7 @@ describe('Token service', () => {

describe('fetchTokenMetadata', () => {
it('should call the api to return the token metadata for eth address provided', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
nock(TOKEN_END_POINT_API)
.get(
`/token/${sampleDecimalChainId}?address=0x514910771af9ca656af840dff83e8264ecf986ca`,
Expand All @@ -230,7 +229,7 @@ describe('Token service', () => {
});

it('should return undefined if the fetch is aborted', async () => {
const abortController = new WhatwgAbortController();
const abortController = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
// well beyond time it will take to abort
Expand All @@ -249,7 +248,7 @@ describe('Token service', () => {
});

it('should return undefined if the fetch fails with a network error', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
.replyWithError('Example network error')
Expand All @@ -265,7 +264,7 @@ describe('Token service', () => {
});

it('should return undefined if the fetch fails with an unsuccessful status code', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
.reply(500)
Expand All @@ -281,7 +280,7 @@ describe('Token service', () => {
});

it('should return undefined if the fetch fails with a timeout', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
// well beyond timeout
Expand All @@ -300,7 +299,7 @@ describe('Token service', () => {
});

it('should throw error if fetching from non supported network', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
await expect(
fetchTokenMetadata(
toHex(5),
Expand All @@ -312,7 +311,7 @@ describe('Token service', () => {
});

it('should call the tokens api and return undefined', async () => {
const { signal } = new WhatwgAbortController();
const { signal } = new AbortController();
nock(TOKEN_END_POINT_API)
.get(`/tokens/${sampleDecimalChainId}`)
.reply(404, undefined)
Expand Down
1 change: 0 additions & 1 deletion packages/controller-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"devDependencies": {
"@metamask/auto-changelog": "^3.1.0",
"@types/jest": "^27.4.1",
"abort-controller": "^3.0.0",
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"nock": "^13.3.1",
Expand Down
18 changes: 0 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,6 @@ __metadata:
"@types/jest": ^27.4.1
"@types/node": ^16.18.24
"@types/uuid": ^8.3.0
abort-controller: ^3.0.0
async-mutex: ^0.2.6
deepmerge: ^4.2.2
ethereumjs-util: ^7.0.10
Expand Down Expand Up @@ -1505,7 +1504,6 @@ __metadata:
"@metamask/utils": ^6.2.0
"@spruceid/siwe-parser": 1.1.3
"@types/jest": ^27.4.1
abort-controller: ^3.0.0
deepmerge: ^4.2.2
eth-ens-namehash: ^2.0.8
eth-rpc-errors: ^4.0.2
Expand Down Expand Up @@ -3449,15 +3447,6 @@ __metadata:
languageName: node
linkType: hard

"abort-controller@npm:^3.0.0":
version: 3.0.0
resolution: "abort-controller@npm:3.0.0"
dependencies:
event-target-shim: ^5.0.0
checksum: 170bdba9b47b7e65906a28c8ce4f38a7a369d78e2271706f020849c1bfe0ee2067d4261df8bbb66eb84f79208fd5b710df759d64191db58cfba7ce8ef9c54b75
languageName: node
linkType: hard

"acorn-globals@npm:^6.0.0":
version: 6.0.0
resolution: "acorn-globals@npm:6.0.0"
Expand Down Expand Up @@ -5596,13 +5585,6 @@ __metadata:
languageName: node
linkType: hard

"event-target-shim@npm:^5.0.0":
version: 5.0.1
resolution: "event-target-shim@npm:5.0.1"
checksum: 1ffe3bb22a6d51bdeb6bf6f7cf97d2ff4a74b017ad12284cc9e6a279e727dc30a5de6bb613e5596ff4dc3e517841339ad09a7eec44266eccb1aa201a30448166
languageName: node
linkType: hard

"evp_bytestokey@npm:^1.0.3":
version: 1.0.3
resolution: "evp_bytestokey@npm:1.0.3"
Expand Down

0 comments on commit 34fba60

Please sign in to comment.