node-fetch icon indicating copy to clipboard operation
node-fetch copied to clipboard

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

Open 1c7 opened this issue 6 years ago • 18 comments

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

1c7 avatar Apr 23 '20 12:04 1c7

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

1c7 avatar Apr 23 '20 12:04 1c7

I hope this problem can be fixed, Because I want to turn on code minify for my electron.js main process code. For now, I have to turn it off (File size 898kb -> 2.3mb)

1c7 avatar Apr 23 '20 12:04 1c7

Using Symbol.toStringTag was supposed to prevent this specific problem.

Richienb avatar Apr 23 '20 12:04 Richienb

@Richienb Thank you for helping, so should I send a PR or? What's the next step?

1c7 avatar Apr 23 '20 12:04 1c7

@1c7 PRs are welcome to fix this issue.

Richienb avatar Apr 23 '20 12:04 Richienb

@Richienb Even though just changing this one line, I have to run all the test and make sure not to break other things, and understand how other pieces works it's still quite complicated, Would you mind helping me fix this? Or let other core members who are familiar with these codes to fix it?

Really appreciate that, Thanks!

Thanks for building node-fetch, helpful lib!

1c7 avatar Apr 23 '20 12:04 1c7

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

1c7 avatar Apr 23 '20 17:04 1c7

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

RuurdBijlsma avatar Dec 20 '20 01:12 RuurdBijlsma

RuurdBijlsma sugarcode looks nice, but, can not be use in a typescript project: The Typescript transpiler refused to overwrite the name getter with the Error:

Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'AbortSignal'.ts(2699)

Setting keep_classnames: true in the TerserPlugin works, but this change is applied to all classnames, I do not see any option in the TerserPlugin to provide a set of classname to keep untouched.

https://github.com/node-fetch/node-fetch/pull/1001 fix the issue, but only on NodeJS v15+

UrielCh avatar Jan 04 '21 10:01 UrielCh

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

turansky avatar Feb 25 '21 23:02 turansky

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

domgenv avatar Jan 17 '22 17:01 domgenv

I came up to this problem (using Esbuild) and fixed that by making the package that uses node-fetch (in my case @sanity/client) as external resource or in other words, excluding the package from the bundling itself:

// build.js

// ...
const baseConfig = {
  external: ['@sanity/client', /* ... other external dependencies ... */]
}

I tried the suggestion from @domgenv to set keepNames to true but unfortunately it didn't work.

raulfdm avatar Jan 22 '22 14:01 raulfdm

https://github.com/node-fetch/node-fetch/issues/784#issuecomment-618527886 How could I achieve this with nuxtjs?

Gbr22 avatar Jan 29 '22 19:01 Gbr22

A bit more information, in case anyone is curious: As mentioned in https://github.com/node-fetch/node-fetch/issues/784#issuecomment-618372321, current versions of node-fetch use Symbol.toStringTag, so they should work just fine, even after minification. node-fetch 2.x (the most recent pre-ESM version, and the version used by, e.g., @azure/storage-blob 12.12.0) still checks against name, so it's affected by this (and can be fixed by adding one of the keep-names options).

joshkel avatar Oct 20 '22 20:10 joshkel

Node.js 16.x internal AbortController can use in node-fetch 2.x.

yantze avatar Oct 24 '22 06:10 yantze

For reference, when using terser with @swc/core this is the minimal config to fix this issue in your webpack config. It does not appear to support regexes for keep_classnames nor keep_fnames

    optimization: {
      minimizer: [
        new TerserPlugin({
          minify: TerserPlugin.swcMinify,
          terserOptions: {
            mangle: {
              // older versions of node-fetch requires AbortSignal to be untouched.
              // https://github.com/node-fetch/node-fetch/issues/784
              reserved: ['AbortSignal'],
            },
          },
        }),
      ]
    },

vlinder avatar Dec 21 '23 12:12 vlinder

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

dimaboychev avatar Feb 22 '24 18:02 dimaboychev

If anyone is using https://github.com/actions/toolkit/tree/main/packages/artifact v2 with esbuild and facing this issue, using --keep-names (https://esbuild.github.io/api/#keep-names) flag seems to be fixing the issue. Thanks to the above linked issue.

shinebayar-g avatar Apr 16 '24 06:04 shinebayar-g