rubensworks / fetch-sparql-endpoint.js

A simple, lightweight module to send queries to SPARQL endpoints and retrieve their results in a streaming fashion.
MIT License
22 stars 13 forks source link

AbortController problem for update queries when bundled #43

Closed smeckler closed 2 years ago

smeckler commented 2 years ago

Hi Ruben,

thank you for this library. It works well but I encountered a small problem with the AbortController when sending update queries. It's working when executed with Node, but the 'fetchUpdate' function has an error when it's bundled (with browserify or React).

Here is the problem in the browser:

const abort_controller_1 = require("abort-controller");

async fetchUpdate(endpoint, query) {
    const abortController = new abort_controller_1.AbortController(); // TypeError: abort_controller_1.AbortController is not a constructor
    const init = {
        method: 'POST',
        headers: {
            'content-type': 'application/sparql-update',
        },
        body: query,
        signal: abortController.signal,
    };
    await this.handleFetchCall(endpoint, init, { ignoreBody: true });
    abortController.abort();
}

A quick workaround was:

    let abortController;
    if (typeof window === 'undefined') { // --> nodeJS
        abortController = new abort_controller_1.AbortController();
    } else { // --> browser
        abortController = new AbortController();
    }
rubensworks commented 2 years ago

Hmm, this seems to be a bug in abort-controller: https://github.com/mysticatea/abort-controller/issues/32

Looks like it can be fixed by moving towards the default export of AbortController instead of its named export.

A PR is definitely welcome! :-)