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.

minification is messing up node-fetch's detection of abort signals

See original GitHub issue

I’m using nodemailer-mailgun-transport. When attempting to call transporter.sendMail, I’m getting:

TypeError: Expected signal to be an instanceof AbortSignal

The stack trace is kinda useless because it’s minified. Also, because mailgun-js bundles all its deps with webpack, it’s pretty difficult to find, but I’m pretty sure here’s where the mailgun-js code gets involved: https://github.com/mailgun/mailgun-js/blob/28c71ded8b15dfa84d5933c9d458b834aa952d7a/lib/request.ts#L76

After investigating, I realized that node-fetch attempts to validate whether signal is an AbortSignal based on the name of the constructor.

In the latest stable version of node-fetch:

https://github.com/node-fetch/node-fetch/blob/v2.6.1/src/request.js#L42

And in the v3 beta it’s still based on the name, but is implemented slightly differently:

https://github.com/node-fetch/node-fetch/blob/v3.0.0-beta.9/src/utils/is.js

In any case, the minification of mailgun-js is causing this issue.

Suggested solution

At the very least, don’t minify. This would solve the problem. Also, you may have a reason to bundle, but that’s pretty unusual so I’m curious why you’re doing it. Can you avoid it? In my experience people handle bundling themselves for stuff like this.

Issue Analytics

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

github_iconTop GitHub Comments

12reactions
kentcdoddscommented, May 29, 2021

For anyone else who only really needs to send simple emails (like me), here’s what I wrote to do that with the API directly:

type MailgunMessage = {
  to: string
  from: string
  subject: string
  text: string
  html: string
}

const auth = `${Buffer.from(`api:${mailgunSendingKey}`).toString('base64')}`

function sendEmail(message: MailgunMessage) {
  return fetch(`https://api.mailgun.net/v3/${mailgunDomain}/messages`, {
    method: 'post',
    body: new URLSearchParams(message),
    headers: {
      Authorization: `Basic ${auth}`,
    },
  })
}

Docs:

Look mah, no dependencies!

5reactions
kentcdoddscommented, May 17, 2021

Just realized why this was working a few weeks ago and suddenly stopped working. 8 days ago, nodemailer-mailgun-transport switched from the old mailgun-js to this mailgun.js: https://github.com/orliesaurus/nodemailer-mailgun-transport/commit/9c6596ac48f3371291a63e2cf700a87ba450067a

So yeah, anyway, I’m not sure why nobody else has reported this issue because from what I can tell, this shouldn’t work for anyone using mailgun.js on the server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to remove "TypeError: Expected signal to be ...
object[NAME] === 'AbortSignal' is not true anymore so this error would pop up, The only solution is to turn off code minification
Read more >
AbortSignal: abort event - Web APIs - MDN Web Docs - Mozilla
You can detect the abort event using an addEventListener method: const controller = new AbortController(); const signal = controller.signal; ...
Read more >
The complete guide to AbortController in Node.js
An instance of the AbortController class exposes the abort method and the signal property. Invoking the abort method emits the abort event to ......
Read more >
Abortable fetch - Chrome Developers
This means every Request has a signal, whether one is given to its constructor or not. # Reacting to an aborted fetch. When...
Read more >
Fetch: Abort
let controller = new AbortController(); fetch(url, { signal: controller.signal });. The fetch method knows how to work with AbortController ...
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