Skip to content

Commit 752f1af

Browse files
committed
feat: add urllib4 fetch
1 parent 10b65ed commit 752f1af

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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';
2630
import {
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
*

lib/core/fetch_factory.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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;

lib/egg.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Messenger = require('./core/messenger');
1414
const DNSCacheHttpClient = require('./core/dnscache_httpclient');
1515
const HttpClient = require('./core/httpclient');
1616
const HttpClientNext = require('./core/httpclient_next');
17+
const FetchFactory = require('./core/fetch_factory');
1718
const createLoggers = require('./core/logger');
1819
const Singleton = require('./core/singleton');
1920
const 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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
});

0 commit comments

Comments
 (0)