Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fallback to abort-controller polyfill #162

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cheerio from "cheerio";
import { fetch } from "cross-fetch";
import AbortController from "abort-controller";
import AbortControllerPolyfill from "abort-controller";
import urlObj from "url";
import { CONSTANTS } from "./constants";

Expand Down Expand Up @@ -404,7 +404,7 @@ export async function getLinkPreview(
}

const timeout = options?.timeout ?? 3000; // 3 second timeout default
const controller = new AbortController();
const controller = createAbortController();
const timeoutCounter = setTimeout(() => controller.abort(), timeout);

const fetchOptions = {
Expand Down Expand Up @@ -488,3 +488,10 @@ export async function getPreviewFromContent(

return parseResponse(response, options);
}


function createAbortController(): AbortController | AbortControllerPolyfill {
return typeof AbortController == 'undefined'
? new AbortControllerPolyfill()
: new AbortController();
}