From e41ec366cf5cac4e87146033940f76cc39a19e7f Mon Sep 17 00:00:00 2001 From: homura Date: Mon, 11 Dec 2023 12:42:46 +0800 Subject: [PATCH] fix(rpc): abort-controller is not a constructor in browsers --- .changeset/tough-jobs-decide.md | 5 +++++ packages/rpc/src/index.ts | 8 ++++---- packages/rpc/src/method.ts | 6 +++--- 3 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 .changeset/tough-jobs-decide.md diff --git a/.changeset/tough-jobs-decide.md b/.changeset/tough-jobs-decide.md new file mode 100644 index 000000000..281ea1912 --- /dev/null +++ b/.changeset/tough-jobs-decide.md @@ -0,0 +1,5 @@ +--- +"@ckb-lumos/rpc": patch +--- + +fix: `_abortController.AbortController` is not a constructor in browser diff --git a/packages/rpc/src/index.ts b/packages/rpc/src/index.ts index 9405c55a9..7e12a78dd 100644 --- a/packages/rpc/src/index.ts +++ b/packages/rpc/src/index.ts @@ -10,7 +10,7 @@ import { } from "./exceptions"; import { RPCConfig } from "./types/common"; import fetch_ from "cross-fetch"; -import { AbortController as CrossAbortController } from "abort-controller"; +import AbortController from "abort-controller"; export const ParamsFormatter = paramsFormatter; export const ResultFormatter = resultFormatter; @@ -134,8 +134,8 @@ export class CKBRPC extends Base { } }); - const controller = new CrossAbortController() as AbortController; - const signal = controller.signal; + const controller = new AbortController(); + const signal = controller.signal as AbortSignal; const timeout = setTimeout( () => controller.abort(), @@ -147,7 +147,7 @@ export class CKBRPC extends Base { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payload), - signal: signal, + signal, }) .then((res) => res.json()); diff --git a/packages/rpc/src/method.ts b/packages/rpc/src/method.ts index e671eff62..247045466 100644 --- a/packages/rpc/src/method.ts +++ b/packages/rpc/src/method.ts @@ -1,7 +1,7 @@ import { IdNotMatchException, ResponseException } from "./exceptions"; import { CKBComponents } from "./types/api"; import { RPCConfig } from "./types/common"; -import { AbortController as CrossAbortController } from "abort-controller"; +import AbortController from "abort-controller"; import fetch_ from "cross-fetch"; export class Method { @@ -42,8 +42,8 @@ export class Method { /* eslint-disable @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types */ public call = async (...params: (string | number | object)[]) => { const payload = this.getPayload(...params); - const controller = new CrossAbortController() as AbortController; - const signal = controller.signal; + const controller = new AbortController(); + const signal = controller.signal as AbortSignal; const timeout = setTimeout(() => controller.abort(), this.#config.timeout);