Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

TypeError: Expected signal to be an instanceof AbortSignal #83

Closed
1 task done
netcoding87 opened this issue Jan 28, 2020 · 8 comments
Closed
1 task done

TypeError: Expected signal to be an instanceof AbortSignal #83

netcoding87 opened this issue Jan 28, 2020 · 8 comments

Comments

@netcoding87
Copy link

Package Version: "^3.0.3"

  • nodejs
    • nodejs version: 12.13.1
    • os name/version: Win 10 (1809)

Describe the bug
I am deploying a backend node app (using typescript) inside a docker container (based on node:12.13.1-alpine). Inside this app I am trying to authenticate to azure via loginWithServicePrincipalSecret. But right now I am getting the following error: TypeError: Expected signal to be an instanceof AbortSignal

To Reproduce
Steps to reproduce the behavior:

import { ComputeManagementClient } from '@azure/arm-compute'
import { VirtualMachine } from '@azure/arm-compute/esm/models'
import * as msRestNodeAuth from '@azure/ms-rest-nodeauth'

const login = async () => {
  try {
    return await msRestNodeAuth.loginWithServicePrincipalSecret(
      cliendId,
      secret,
      domain
    )
  } catch (err) {
    console.error(err)
  }

  return null
}

export const listVMs = async (): Promise<VirtualMachine[]> => {
  try {
    const resourceGroupName = 'myResourceGroupName'
    const creds = await login()
    if (creds) {
      const client = new ComputeManagementClient(creds, subscriptionId)
      const result = await client.virtualMachines.list(resourceGroupName )
      return result.map((machine) => machine)
    }
  } catch (err) {
    console.error(err)
  }

  return []
}

Expected behavior
Expect to be able to receive the auth response.

Additional context
I've already seen the issue #82 which is similar but adding dom and/or dom.iterable to the tsconfig.json did not help. Here is what my tsconfig.json looks now:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": false,
    "lib": ["esnext", "esnext.asynciterable", "dom", "dom.iterable"],
    "module": "esnext",
    "moduleResolution": "node",
    "noImplicitAny": false,
    "outDir": "./build",
    "resolveJsonModule": true,
    "sourceMap": true,
    "strict": true,
    "target": "ES2019"
  },
  "include": ["src"]
}
@netcoding87
Copy link
Author

After further investigation on this topic I managed to find out that this problem comes from the production mode build. On local dev I build and run the app in development mode and everythings works fine. But of course for deployment I switch to production and then the type error is thrown. When build and running the app locally in production mode the same error is also thrown...

@kepikoi
Copy link

kepikoi commented Mar 30, 2020

Having the same issue with @azure/identity@1.0.2 running a production webpack build. No issue evident when building to development

@netcoding87 were you able to work around this?

@netcoding87
Copy link
Author

Hey @kepikoi, unfortunately not with webpack... We switched to use typescript compiler only and there we do not have this issue anymore.

@kepikoi
Copy link

kepikoi commented Mar 31, 2020

I think I found the cause of this issue. Tldr is webpack does rewrite the constructor names in production mode and node-fetch 2.6 contains logic where it compares whether some constructor name is exactly AbortSignal which is just not the case with the rewritten artifact.

This issue is documented in node-fetch/node-fetch#667 and was fixed in the 3.x branch which is currently in the pre-release stage.

So, unless the maintainers of https://github.com/Azure/ms-rest-js are willing to upgrade node-fetch to an unstable release, it seems like we're stuck with this error for some time

@kepikoi
Copy link

kepikoi commented Mar 31, 2020

It is possible to disable the minimization of constructor names in webpack in production which will work around node-fetch/node-fetch#667

module.exports = {
  //...
  optimization: {
    minimize: false
  }
};

With this I was able to get a working production build again

@daviwil
Copy link
Contributor

daviwil commented Jun 17, 2020

Hi all, thanks for documenting the fixes you made to work around the issue! I don't think we'll be able to update to a pre-release version of node-fetch in the short term since it would introduce extra maintenance overhead. For now I'd recommend that anyone who encounters this issue to follow @kepikoi's example to disable this option in Webpack to get around the issue.

@daviwil daviwil closed this as completed Jun 17, 2020
@rohinz
Copy link

rohinz commented Sep 11, 2020

If you don't want to disable minimization at all, just use a regular expression to prevent minificaiton of the AbortSignal type:

const TerserPlugin = require('terser-webpack-plugin')

const config = {
  mode: 'development',
 // your default config
}

module.exports = (env, argv) => {

  if (argv.mode === 'production') {
    config.mode = 'production'
    config.devtool = 'source-map'
    config.optimization = {
      ...(config.optimization || {}),
      minimize: true,
      minimizer: [
        new TerserPlugin({
          terserOptions: {
            keep_fnames: /AbortSignal/,
          },
        }),
      ],
    }
  }

  return config
}

@CdTgr
Copy link

CdTgr commented Oct 17, 2023

For anyone working with esbuild and encounter the same error, use the --keep-names flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants