Skip to content

Commit 0db3862

Browse files
committed
add abortcontroller override
1 parent d2079aa commit 0db3862

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ipfs-race",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Resolve an IPFS path using multiple gateways and methods to get data the fastest.",
55
"main": "dist/index.js",
66
"scripts": {

src/resolve.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as isIPFS from 'is-ipfs'
22
import {defaultIpfsGateways, defaultIpnsGateways} from "./defaultGateways";
33
import {isValidHttpUrl} from "./isValidHttpUrl";
44
import any from "promise.any";
5-
import {AbortController} from "node-abort-controller";
65

76
export type FetchType = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
87

@@ -11,10 +10,11 @@ interface ResolveOptions {
1110
ipnsGateways?: string[];
1211
defaultProtocolIfUnspecified?: "ipfs" | "ipns";
1312
fetchOverride?: FetchType;
13+
abortControllerOverride?: AbortController;
1414
logErrors?: boolean;
1515
}
1616

17-
const defaultResolveOptions: Required<Omit<ResolveOptions, "fetchOverride">> = {
17+
const defaultResolveOptions: Required<Omit<ResolveOptions, "fetchOverride" | "abortControllerOverride">> = {
1818
ipfsGateways: defaultIpfsGateways,
1919
ipnsGateways: defaultIpnsGateways,
2020
defaultProtocolIfUnspecified: "ipfs",
@@ -47,13 +47,13 @@ async function resolve(uri: string, options?: ResolveOptions): Promise<ResolveOu
4747
// The library will check many common spots for a fetch object. Depending on the ecosystem, lots of these variables don't
4848
// exist and throw errors, so I put them in try catch block.
4949
let fetchOverride: FetchType | undefined = options?.fetchOverride;
50+
let abortControllerOverride: AbortController | undefined = options?.abortControllerOverride;
5051
if (!options?.fetchOverride) {
5152
try {
5253
fetchOverride = globalThis.fetch;
5354
} catch (err) {
5455
}
5556

56-
5757
if (!fetchOverride) {
5858
try {
5959
fetchOverride = window.fetch.bind(window);
@@ -75,10 +75,32 @@ async function resolve(uri: string, options?: ResolveOptions): Promise<ResolveOu
7575
throw new Error("Fetch library not found, please pass in 'fetchOverride'. If you are running in the browser, this will likely be window.fetch, if in a node version >16.x.x it is global. If importing before 16.x.x then you will likely need to install 'node-fetch'.");
7676
}
7777

78+
// if we are using a version of node that has abort controllers, use them, otherwise import from node-abort-controller
79+
if (!options?.abortControllerOverride) {
80+
try {
81+
// @ts-ignore
82+
abortControllerOverride = AbortController;
83+
} catch (err) {
84+
}
85+
86+
if (!abortControllerOverride) {
87+
try {
88+
abortControllerOverride = await import("node-abort-controller") as unknown as AbortController;
89+
} catch (err) {
90+
console.error(err)
91+
}
92+
}
93+
}
94+
95+
if (!abortControllerOverride) {
96+
throw new Error("AbortController library not found, please pass in 'abortControllerOverride'. If you are running in the browser, this will likely be AbortController, if in a node version >16.x.x it is global. If importing before 16.x.x then you will likely need to install 'node-abort-controller'.");
97+
}
98+
7899
// merge the options with the default options
79100
const _options: Required<ResolveOptions> = {
80101
...defaultResolveOptions,
81102
fetchOverride,
103+
abortControllerOverride,
82104
...(options || {})
83105
};
84106

0 commit comments

Comments
 (0)