node-fetch: Is there a way to remove "TypeError: Expected signal to be an instanceof AbortSignal"?

https://github.com/node-fetch/node-fetch/blob/master/src/request.js?ts=2#L120

This line causing problem

image

Let’s check out ths isAbortSignal function

image

When would it happen?

After Webpack bundle my code, code minification would change the name,
object[NAME] === 'AbortSignal' is not true anymore so this error would pop up, The only solution is to turn off code minification There should be a better way than this

Propose (What to do next)

  1. Delete this part altogether, don’t check the name
  2. Don’t compare with string ‘AbortSignal’ like this object[NAME] === 'AbortSignal', in Ruby we have class.name to get the class name, I am not sure what’s the equivalent in JS

Maybe this StackOverflow question helpful: How to get a JavaScript object’s class?

Thanks

About this issue

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

Commits related to this issue

Most upvoted comments

For those using esbuild, an equivalent solution to 1c7’s solution is to set the “keepNames” option to true.

https://esbuild.github.io/api/#keep-names

Done, fixed it

Enviroment

  • Electron.js 8.0.0
  • “vue-cli-plugin-electron-builder”: “^1.4.5”
  • @azure/storage-blob”: “12.1.1”

Solution:

This can still minify code, but, without mangle with the class name and function name

vue.config.js

const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
 pluginOptions: {
  electronBuilder: {
   chainWebpackMainProcess: config => {
    config.plugins.delete('uglify')
    config.plugin('uglify').use(TerserPlugin, [{
     terserOptions: {
      mangle: false,
      sourceMap: true,
      compress: false,
      keep_classnames: true,
      keep_fnames: true,
      output: {
       comments: false,
      },
     }
    }])
    config.optimization.minimize(false);
    console.log(config.toString())
   },
  }
 },
}

Result (This worked)

This is src/background.js file after running npm run electron:build

image

For comparison (This doesn’t work)

node-fetch would raise error image

FYI if anyone stumbles on this. I get this error in nextjs 14 application with “@azure/storage-blob”: “^12.17.0”: “Blob Upload Error TypeError: Expected signal to be an instanceof AbortSignal”.

To bypass the error you can disable server minification via next.config.js file like this:

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    serverMinification: false
  }
}

module.exports = nextConfig

WA, which save single name AbortSignal

// keep_classnames is required to workaround node-fetch Expected signal to be an instanceof AbortSignal
config.optimization = {
  minimizer: [
    new TerserPlugin({
      cache: true,
      parallel: true,
      sourceMap: true, // Must be set to true if using source-maps in production
      terserOptions: {
        // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
        mangle: false,
        sourceMap: true,
        // compress: false,
        keep_classnames: /AbortSignal/,
        keep_fnames: /AbortSignal/,
        output: {
          beautify: true,
          indent_level: 1
        }
      }
    }),
  ],
};

Source

I couldn’t get the TerserPlugin to work, so I fixed it another way, maybe someone else has a use for this as well. I wrapped the abort signal in the following class: https://gist.github.com/RuurdBijlsma/66e31f47c1e2f2c14b4652b43f65a8fc

So instead of passing abortController.signal to node-fetch or a googleapis request, I would pass new AbortSignal(abortController.signal), the main thing here is static get name() which returns the proper name even when minimization is fully enabled

@1c7 PRs are welcome to fix this issue.

I can not provide reproducible code for now. sorry.

How I encounter this problem?

I am building a Electron.js app that can upload file to Azure Storage. Using "@azure/storage-blob": "12.1.1" Everything works fine in development, but after I bundle the code for production,

image

This TypeError: Expected signal to be an instanceof AbortSignal would popup. After tracking it, I found out it’s node-fetch rasing this error