File tree Expand file tree Collapse file tree 4 files changed +52
-0
lines changed
Expand file tree Collapse file tree 4 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ import {
2323 RequestOptions ,
2424 HttpClientResponse as HttpClientResponseNext ,
2525} from 'urllib-next' ;
26+ import {
27+ FetchFactory ,
28+ fetch ,
29+ } from 'urllib4' ;
2630import {
2731 EggCoreBase ,
2832 FileLoaderOption ,
@@ -589,6 +593,12 @@ declare module 'egg' {
589593 */
590594 httpclient : EggHttpClient ;
591595
596+ /**
597+ * node fetch
598+ */
599+ FetchFactory : FetchFactory ;
600+ fetch : typeof fetch ,
601+
592602 /**
593603 * Logger for Application, wrapping app.coreLogger with context infomation
594604 *
Original file line number Diff line number Diff line change 1+ const debug = require ( 'util' ) . debuglog ( 'egg:lib:core:httpclient_next' ) ;
2+
3+ const mainNodejsVersion = parseInt ( process . versions . node . split ( '.' ) [ 0 ] ) ;
4+ let FetchFactory ;
5+ if ( mainNodejsVersion >= 18 ) {
6+ // urllib@4 only works on Node.js >= 18
7+ try {
8+ const urllib4 = require ( 'urllib4' ) ;
9+ FetchFactory = urllib4 . FetchFactory ;
10+ debug ( 'urllib4 enable' ) ;
11+ } catch ( err ) {
12+ debug ( 'require urllib4 error: %s' , err ) ;
13+ }
14+ }
15+
16+ module . exports = FetchFactory ;
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ const Messenger = require('./core/messenger');
1414const DNSCacheHttpClient = require ( './core/dnscache_httpclient' ) ;
1515const HttpClient = require ( './core/httpclient' ) ;
1616const HttpClientNext = require ( './core/httpclient_next' ) ;
17+ const FetchFactory = require ( './core/fetch_factory' ) ;
1718const createLoggers = require ( './core/logger' ) ;
1819const Singleton = require ( './core/singleton' ) ;
1920const utils = require ( './core/utils' ) ;
@@ -51,6 +52,11 @@ class EggApplication extends EggCore {
5152 this . ContextHttpClient = ContextHttpClient ;
5253 this . HttpClient = HttpClient ;
5354 this . HttpClientNext = HttpClientNext ;
55+ this . FetchFactory = FetchFactory ;
56+ if ( FetchFactory ) {
57+ this . FetchFactory . setClientOptions ( ) ;
58+ this . fetch = FetchFactory . fetch ;
59+ }
5460
5561 this . loader . loadConfig ( ) ;
5662
Original file line number Diff line number Diff line change 1+ const assert = require ( 'node:assert' ) ;
2+ const mm = require ( 'egg-mock' ) ;
3+ const FetchFactory = require ( '../../../lib/core/fetch_factory' ) ;
4+ const utils = require ( '../../utils' ) ;
5+
6+ describe ( 'test/lib/core/fetch_factory.test.js' , ( ) => {
7+ if ( ! FetchFactory ) return ;
8+ let url ;
9+
10+ before ( async ( ) => {
11+ url = await utils . startLocalServer ( ) ;
12+ } ) ;
13+
14+ afterEach ( mm . restore ) ;
15+
16+ it ( 'should fetch ok' , async ( ) => {
17+ const { status } = await FetchFactory . fetch ( url ) ;
18+ assert ( status === 200 ) ;
19+ } ) ;
20+ } ) ;
You can’t perform that action at this time.
0 commit comments