Skip to content

Commit d86b8c0

Browse files
authored
feat: http proxy util (#106)
1 parent 586fde5 commit d86b8c0

10 files changed

+577
-22
lines changed

LICENSE

+93
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,96 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22+
23+
---
24+
25+
https://github.com/node-fetch/node-fetch
26+
27+
The MIT License (MIT)
28+
29+
Copyright (c) 2016 - 2020 Node Fetch Team
30+
31+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
32+
33+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
34+
35+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36+
37+
---
38+
39+
https://github.com/mysticatea/abort-controller
40+
41+
MIT License
42+
43+
Copyright (c) 2017 Toru Nagashima
44+
45+
Permission is hereby granted, free of charge, to any person obtaining a copy
46+
of this software and associated documentation files (the "Software"), to deal
47+
in the Software without restriction, including without limitation the rights
48+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
49+
copies of the Software, and to permit persons to whom the Software is
50+
furnished to do so, subject to the following conditions:
51+
52+
The above copyright notice and this permission notice shall be included in all
53+
copies or substantial portions of the Software.
54+
55+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
61+
SOFTWARE.
62+
63+
---
64+
65+
https://github.com/TooTallNate/proxy-agents
66+
67+
(The MIT License)
68+
69+
Copyright (c) 2013 Nathan Rajlich <[email protected]>
70+
71+
Permission is hereby granted, free of charge, to any person obtaining
72+
a copy of this software and associated documentation files (the
73+
'Software'), to deal in the Software without restriction, including
74+
without limitation the rights to use, copy, modify, merge, publish,
75+
distribute, sublicense, and/or sell copies of the Software, and to
76+
permit persons to whom the Software is furnished to do so, subject to
77+
the following conditions:
78+
79+
The above copyright notice and this permission notice shall be
80+
included in all copies or substantial portions of the Software.
81+
82+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
83+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
84+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
85+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
86+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
87+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
88+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
89+
90+
---
91+
92+
https://github.com/nodejs/undici
93+
94+
MIT License
95+
96+
Copyright (c) Matteo Collina and Undici contributors
97+
98+
Permission is hereby granted, free of charge, to any person obtaining a copy
99+
of this software and associated documentation files (the "Software"), to deal
100+
in the Software without restriction, including without limitation the rights
101+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
102+
copies of the Software, and to permit persons to whom the Software is
103+
furnished to do so, subject to the following conditions:
104+
105+
The above copyright notice and this permission notice shall be included in all
106+
copies or substantial portions of the Software.
107+
108+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
109+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
110+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
111+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
112+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
113+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
114+
SOFTWARE.

README.md

+55-12
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
[![][npm-version-src]][npm-version-href]
44
[![][github-actions-src]][github-actions-href]
55
[![][packagephobia-src]][packagephobia-href]
6+
67
<!-- [![npm downloads][npm-downloads-src]][npm-downloads-href] -->
78
<!-- [![Codecov][codecov-src]][codecov-href] -->
89

9-
A redistribution of [node-fetch v3](https://github.com/node-fetch/node-fetch) for better backward and forward compatibility.
10+
A redistribution of [node-fetch v3](https://github.com/node-fetch/node-fetch) (+ more!) for better backward and forward compatibility.
1011

1112
**Why this package?**
1213

1314
- We can no longer `require('node-fetch')` with latest version. This stopped popular libraries from upgrading and dependency conflicts between `node-fetch@2` and `node-fetch@3`.
1415
- With upcoming versions of Node.js, native `fetch` is being supported. We are prepared for native fetch support using this package yet keep supporting older Node versions.
16+
- With introduction of native fetch to Node.js via [undici](https://github.com/nodejs/undici) there is no easy way to support http proxies!
1517

1618
**Features:**
1719

@@ -25,6 +27,8 @@ A redistribution of [node-fetch v3](https://github.com/node-fetch/node-fetch) fo
2527

2628
✅ Polyfill support for Node.js
2729

30+
✅ Compact and simple proxy supporting both Node.js versions without native fetch using [HTTP Agent](https://github.com/TooTallNate/proxy-agents/tree/main/packages/proxy-agent) and versions with native fetch using [Undici Proxy Agent](https://undici.nodejs.org/#/docs/api/ProxyAgent)
31+
2832
## Usage
2933

3034
Install `node-fetch-native` dependency:
@@ -44,22 +48,65 @@ You can now either import or require the dependency:
4448

4549
```js
4650
// ESM
47-
import fetch from 'node-fetch-native'
51+
import fetch from "node-fetch-native";
4852

4953
// CommonJS
50-
const fetch = require('node-fetch-native')
54+
const fetch = require("node-fetch-native");
5155
```
5256

5357
More named exports:
5458

5559
```js
5660
// ESM
57-
import { fetch, Blob, FormData, Headers, Request, Response, AbortController } from 'node-fetch-native'
61+
import {
62+
fetch,
63+
Blob,
64+
FormData,
65+
Headers,
66+
Request,
67+
Response,
68+
AbortController,
69+
} from "node-fetch-native";
5870

5971
// CommonJS
60-
const { fetch, Blob, FormData, Headers, Request, Response, AbortController } = require('node-fetch-native')
72+
const {
73+
fetch,
74+
Blob,
75+
FormData,
76+
Headers,
77+
Request,
78+
Response,
79+
AbortController,
80+
} = require("node-fetch-native");
81+
```
82+
83+
## Proxy support
84+
85+
Node.js has no built-in support for HTTP Proxies for fetch ([see nodejs/undici#1650](https://github.com/nodejs/undici/issues/1650) and [nodejs/node#8381](https://github.com/nodejs/node/issues/8381))
86+
87+
This package bundles a compact and simple proxy supported for both Node.js versions without native fetch using [HTTP Agent](https://github.com/TooTallNate/proxy-agents/tree/main/packages/proxy-agent) and versions with native fetch using [Undici Proxy Agent](https://undici.nodejs.org/#/docs/api/ProxyAgent).
88+
89+
**Usage:**
90+
91+
```ts
92+
import { fetch } from "node-fetch-native"; // or use global fetch
93+
import { createProxy } from "node-fetch-native/proxy";
94+
95+
const proxy = createProxy(); // Uses HTTPS_PROXY or HTTP_PROXY by default
96+
97+
const proxy = createProxy({ url: "http://localhost:8080" });
98+
99+
await fetch("https://google.com", {
100+
...proxy,
101+
});
61102
```
62103

104+
`createProxy` returns an object with `agent` for older Node.js version and `dispatcher` keys for newer Node.js versions with undici and native fetch.
105+
106+
If no `url` option is provided, `HTTPS_PROXY` or `HTTP_PROXY` value will be used and if they also are not set, both `agent` and `dispatcher` values will be undefined.
107+
108+
**Note:** Using export conditions, this utility works in Node.js and for other runtimes, it will simply return an stubbed version as most of other runtimes now support http proxy out of the box!
109+
63110
## Force using non-native version
64111

65112
Sometimes you want to explicitly use none native (`node-fetch`) implementation of `fetch` in case of issues with native/polyfill version of `globalThis.fetch` with Node.js or runtime environment.
@@ -77,10 +124,10 @@ Using the polyfill method, we can once ensure global fetch is available in the e
77124

78125
```js
79126
// ESM
80-
import 'node-fetch-native/polyfill'
127+
import "node-fetch-native/polyfill";
81128

82129
// CJS
83-
require('node-fetch-native/polyfill')
130+
require("node-fetch-native/polyfill");
84131

85132
// You can now use fetch() without any import!
86133
```
@@ -137,20 +184,16 @@ Made with 💛
137184
[node-fetch is published under the MIT license](https://github.com/node-fetch/node-fetch/blob/main/LICENSE.md)
138185

139186
<!-- Badges -->
187+
140188
[npm-version-src]: https://flat.badgen.net/npm/v/node-fetch-native
141189
[npm-version-href]: https://npmjs.com/package/node-fetch-native
142-
143190
[npm-downloads-src]: https://flat.badgen.net/npm/dm/node-fetch-native
144191
[npm-downloads-href]: https://npmjs.com/package/node-fetch-native
145-
146192
[github-actions-src]: https://flat.badgen.net/github/checks/unjs/node-fetch-native
147193
[github-actions-href]: https://github.com/unjs/node-fetch-native/actions?query=workflow%3Aci
148-
149194
[packagephobia-src]: https://flat.badgen.net/packagephobia/install/node-fetch-native
150195
[packagephobia-href]: https://packagephobia.com/result?p=node-fetch-native
151-
152196
[packagephobia-s-src]: https://flat.badgen.net/packagephobia/install/node-fetch-native?label=node-fetch-native&scale=.9
153197
[packagephobia-s-href]: https://packagephobia.com/result?p=node-fetch-native
154-
155198
[packagephobia-s-alt-src]: https://flat.badgen.net/packagephobia/install/node-fetch?label=node-fetch&scale=.9
156199
[packagephobia-s-alt-href]: https://packagephobia.com/result?p=node-fetch

build.config.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { rm } from "node:fs/promises";
2+
import { resolve } from "node:path";
13
import { defineBuildConfig } from "unbuild";
24

35
export default defineBuildConfig({
@@ -13,5 +15,18 @@ export default defineBuildConfig({
1315
keepNames: true,
1416
},
1517
},
16-
entries: ["src/index", "src/native", "src/polyfill", "src/node"],
18+
entries: [
19+
"src/index",
20+
"src/native",
21+
"src/polyfill",
22+
"src/node",
23+
"src/proxy",
24+
"src/proxy-stub",
25+
],
26+
hooks: {
27+
async "build:done"(ctx) {
28+
// Save few bytes from dist...
29+
await rm(resolve(ctx.options.outDir, "proxy.mjs"));
30+
},
31+
},
1732
});

lib/proxy.d.cts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type * as http from "node:http";
2+
import type * as https from "node:https";
3+
import type * as undici from "undici";
4+
5+
export declare const createProxy: (opts?: { url?: string }) => {
6+
agent: http.Agent | https.Agent | undefined;
7+
dispatcher: undici.Dispatcher | undefined;
8+
};

lib/proxy.d.mts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type * as http from "node:http";
2+
import type * as https from "node:https";
3+
import type * as undici from "undici";
4+
5+
export declare const createProxy: (opts?: { url?: string }) => {
6+
agent: http.Agent | https.Agent | undefined;
7+
dispatcher: undici.Dispatcher | undefined;
8+
};

package.json

+29-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@
7878
"types": "./lib/index.d.cts",
7979
"default": "./lib/index.cjs"
8080
}
81+
},
82+
"./proxy": {
83+
"node": {
84+
"import": {
85+
"types": "./lib/proxy.d.mts",
86+
"default": "./dist/proxy.cjs"
87+
},
88+
"require": {
89+
"types": "./lib/proxy.d.cts",
90+
"default": "./dist/proxy.cjs"
91+
}
92+
},
93+
"default": {
94+
"import": {
95+
"types": "./lib/proxy.d.mts",
96+
"default": "./dist/proxy-stub.mjs"
97+
},
98+
"require": {
99+
"types": "./lib/proxy.d.cts",
100+
"default": "./dist/proxy-stub.cjs"
101+
}
102+
}
81103
}
82104
},
83105
"main": "./lib/index.cjs",
@@ -100,16 +122,22 @@
100122
"test": "pnpm lint && pnpm build && vitest run --coverage"
101123
},
102124
"devDependencies": {
125+
"@types/node": "^20.10.5",
103126
"@vitest/coverage-v8": "^1.1.0",
104127
"abort-controller": "^3.0.0",
128+
"agent-base": "^7.1.0",
105129
"changelogen": "^0.5.5",
106130
"eslint": "^8.56.0",
107131
"eslint-config-unjs": "^0.2.1",
132+
"http-proxy-agent": "^7.0.0",
133+
"https-proxy-agent": "^7.0.2",
108134
"node-fetch": "^3.3.2",
109135
"prettier": "^3.1.1",
136+
"proxy-agent": "^6.3.1",
110137
"typescript": "^5.3.3",
111138
"unbuild": "^2.0.0",
139+
"undici": "^6.2.1",
112140
"vitest": "^1.1.0"
113141
},
114142
"packageManager": "[email protected]"
115-
}
143+
}

0 commit comments

Comments
 (0)