Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Don't require esModuleInterop downstream #55

Closed

Conversation

jeffomatic
Copy link

This change updates Node stdlib imports to use the following ES6 import style:

import * as x from 'x';

This allows downstream TypeScript projects (direct or transitive) to use node-agent-base without enabling the esModuleInterop option. Ideally, it would be nice not to force users to enable this option if they don't want it.

To prevent regression, this PR switches off the esModuleInterop option in this package's own tsconfig.json. This means that the test suite also needed to be updated. In particular, the case that modifies https.globalAgent requires a slight tweak, because ES6 module imports mark modules as read-only for the importing scope.

This change updates Node stdlib imports to use the following
ES6 import style:

```
import * as x from 'x';
```

This allows downstream TypeScript projects (direct or transitive)
to use node-agent-base without enabling the esModuleInterop
option. Ideally, it would be nice not to force users to enable
this option if they don't want it.
@@ -13,7 +13,7 @@ import { Agent, RequestOptions } from '../src';
// versions we have to patch the internal `_http_agent` module instead
// (see: https://github.com/nodejs/node/pull/25170).
// @ts-ignore
import httpAgent from '_http_agent';
import * as httpAgent from '_http_agent';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the // @ts-ignore means we don't have to do the require() hack that was necessary for https below.

@Nytelife26
Copy link

Looks good, but the package hasn't been updated in months. Seems unlikely you'll get a merge.

@TooTallNate
Copy link
Owner

TooTallNate commented Mar 8, 2021

I'm potentially open to this change. However, I wonder how are you consuming this module? Are you using the source TypeScript files directly or the compiled version from the dist dir (which would be the default npm way).

@jeffomatic
Copy link
Author

I wonder how are you consuming this module? Are you using the source TypeScript files directly or the compiled version from the dist dir (which would be the default npm way).

Sure. We ran into issue when adding proxy-agent to a TypeScript project; folks on the team are hard against enabling esModuleInterop for reasons I won't dive into here. proxy-agent uses node-agent-base as a dependency. The TypeScript compiler dies when reading node-agent-base's index.d.ts, which, as you mention, does appear in dist.

Our current workaround is to use require() to import proxy-agent, which more or less short-circuits the typechecker. The downside is that proxy-agent's exports are no longer typechecked, since require forces an any type.

@Uzlopak
Copy link

Uzlopak commented Apr 22, 2021

I ran also today into this issue :/

Would be great if you would fix it.

@Uzlopak
Copy link

Uzlopak commented Apr 23, 2021

@TooTallNate

The issue is happening when you have a "normal" typescript project without esModuleInterop set to true. When you import ProxyAgent, it will also use your package. So when the typings are imported, your esModule import will break your project, as it can not determine what this means.

This is your current tarball in npm:
https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz

So it uses the typings in the dist folder, which have esModuleInterop imports.
Bildschirmfoto von 2021-04-23 02-30-40

@Trainmaster
Copy link

Without enabling esModuleInterop, this is blocking an upgrade from proxy-agent@3.1.1 to proxy-agent@4.0.2. And this is necessary because agent-base@4.2.1 is in the dependency tree which has the following bug: #25.

"proxy-agent": {
  "version": "3.1.1",
  "resolved": "xxx/proxy-agent-3.1.1.tgz",
  "integrity": "sha1-fgTga/Nq+mJKFUC+JHtHyXC9MBQ=",
  "requires": {
    "agent-base": "^4.2.0",
    "debug": "4",
    "http-proxy-agent": "^2.1.0",
    "https-proxy-agent": "^3.0.0",
    "lru-cache": "^5.1.1",
    "pac-proxy-agent": "^3.0.1",
    "proxy-from-env": "^1.0.0",
    "socks-proxy-agent": "^4.0.1"
  }
},
"socks-proxy-agent": {
  "version": "4.0.2",
  "resolved": "xxx/socks-proxy-agent-4.0.2.tgz",
  "integrity": "sha1-PImR8xRbJ5nnDhG9X7yLGWMRY4Y=",
  "requires": {
    "agent-base": "~4.2.1",
    "socks": "~2.3.2"
  },

@TooTallNate TooTallNate changed the title don't require esModuleInterop downstream Don't require esModuleInterop downstream Nov 22, 2021
@orgads
Copy link

orgads commented Dec 8, 2021

@TooTallNate Do you plan to merge this?

@heanzyzabala
Copy link

@TooTallNate Any updates on this?

@TooTallNate
Copy link
Owner

This code in this repository has been moved to the proxy-agents monorepo, so I am closing this pull request. If you feel that this change is still necessary as of the latest release, feel free to open a new pull request over there.

@TooTallNate TooTallNate closed this May 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants