question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fetch doesn't allow for AbortSignal custom impl with constructor name other then exactly AbortSignal

See original GitHub issue

May I ask you to elaborate on the purpose of this validation?

https://github.com/node-fetch/node-fetch/blob/7c66f9ce822a3a06fe01eb583a30b4c087c596c2/src/utils/is.js#L53-L58

For my use case, it is redundant. I want to pass a custom adapter from vscode.CancellationToken to AbortSignal:

import { AbortSignal } from "node_fetch/externals";

class CancellationSignal implements AbortSignal {
    subscriptions = new WeakMap<Listener, vscode.Disposable>();

    constructor(private readonly ct: vscode.CancellationToken) {}

    get aborted() {
        return this.ct.isCancellationRequested;
    }

    addEventListener(_type: "abort", listener: Listener, _opts?: unknown) {
        this.subscriptions.set(listener, this.ct.onCancellationRequested(listener, this));
    }

    removeEventListener(_type: "abort", listener: Listener) {
        this.subscriptions.get(listener)?.dispose();
    }

    // Some excess APIs that are not used by `node_fetch` impl
    onabort = null
    dispatchEvent() { return false; }
}

This is very annoying that I cannot name my class how I want. Besides that why can’t I pass a simple object literal that conforms to AbortSignal? There is no such explicit contract that an instance of abort signal passed to fetch must be a custom class instance, since AbortSignlas is nothing but an interface…

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dzek69commented, Mar 18, 2020

I agree. This is also very annoying when you bundle and minify your code. Minifiers will change the name of your class and node-fetch will fail on that.

If somebody finds that useful - that’s fine. But having some global configuration to disable this behavior would be nice.

0reactions
jimmywartingcommented, Jan 23, 2022

Closing…

  • Mangling class name is fine.
  • Using a AbortSignal that lacks a missing symbol.toStringTag is not…

Also I took a extra peek on our isAbortSignal function and we also check if the name match EventTarget… this is weird. should remove that (but maybe not right now as it might break something, on v4 we will drop older node support when v12 goes EOL and we can start using built in AbortController provided by NodeJS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AbortSignal - Web APIs - MDN Web Docs - Mozilla
The AbortSignal interface represents a signal object that allows you to communicate with a DOM request (such as a fetch request) and abort ......
Read more >
Errors | Node.js v19.3.0 Documentation
Most asynchronous methods that accept a callback function will accept an Error object passed as the first argument to that function. If that...
Read more >
Abortable fetch - Chrome Developers
Aborting fetches using a new web primitive – The abort ... It's a new AbortSignal that mimics the signal passed to the constructor....
Read more >
AbortController is your friend - Whistlr
It's to use AbortController to provide a fetch() you can abort early: ... but has a big caveat: note that AbortSignal doesn't fire...
Read more >
Using fetch(), AbortSignal, And setTimeout() To Apply Retry ...
Essentially, we're going to separate the parts of the code that change at different rates. What I ended-up with was an API client...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found