Skip to content

Commit

Permalink
Merge branch '3.x' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jul 3, 2024
2 parents 2497f27 + c4bc7f4 commit 2a581e4
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name: CI
name: CI for 3.x

on:
push:
branches: [ master, 2.x, 1.x ]

branches: [ 3.x ]
pull_request:
branches: [ master, 2.x, 1.x ]
branches: [ 3.x ]

jobs:
Job:
Expand All @@ -15,3 +14,5 @@ jobs:
os: 'ubuntu-latest, macos-latest, windows-latest'
version: '14, 16, 18, 20, 22'
install: 'npm i -g npminstall && npminstall'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
14 changes: 0 additions & 14 deletions .github/workflows/release.yml

This file was deleted.

19 changes: 19 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

3.26.0 / 2024-07-01
==================

**features**
* [[`b0292a8b`](http://github.com/eggjs/egg/commit/b0292a8b7e76d5dbf7441b7164c39441dbae51ec)] - feat: allow to create httpClient from app (#5334) (fengmk2 <<suqian.yf@antgroup.com>>)

3.25.0 / 2024-06-27
==================

**features**
* [[`ceded0b1`](http://github.com/eggjs/egg/commit/ceded0b1c9217503c5ed9226f96c493d6bd00547)] - feat: allow to httpClient use HTTP2 first (#5332) (fengmk2 <<suqian.yf@antgroup.com>>)

**others**
* [[`8553c3f2`](http://github.com/eggjs/egg/commit/8553c3f23e423e9f60144b11a484b703fe7c9229)] - chore: remove auto release (fengmk2 <<suqian.yf@antgroup.com>>)
* [[`b4f01a1c`](http://github.com/eggjs/egg/commit/b4f01a1c6bf006c943c85fce334b81d61f55b7d0)] - chore: fix release branches name (fengmk2 <<suqian.yf@antgroup.com>>)
* [[`a8073b04`](http://github.com/eggjs/egg/commit/a8073b04fc3821bb23326c6c8b4fd0ccaeb5c200)] - chore: add release config (fengmk2 <<suqian.yf@antgroup.com>>)
* [[`8ce2ff90`](http://github.com/eggjs/egg/commit/8ce2ff90bfbb9e4580a23ea49a15fdb1c185fbb5)] - chore: add npm publish tag (fengmk2 <<suqian.yf@antgroup.com>>)
* [[`44950ed8`](http://github.com/eggjs/egg/commit/44950ed82a3ce4d5d4b9028aee98d6650298a552)] - chore: start 3.x LTS (fengmk2 <<suqian.yf@antgroup.com>>)
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ English | [简体中文](./README.zh-CN.md)
[![NPM download](https://img.shields.io/npm/dm/egg.svg?style=flat-square)](https://npmjs.org/package/egg)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Feggjs%2Fegg.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Feggjs%2Fegg?ref=badge_shield)

[![Continuous Integration](https://github.com/eggjs/egg/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg/actions?query=branch%3Amaster)
[![Continuous Integration](https://github.com/eggjs/egg/actions/workflows/nodejs-3.x.yml/badge.svg)](https://github.com/eggjs/egg/actions?query=branch%3A3.x)
[![Test coverage](https://img.shields.io/codecov/c/github/eggjs/egg.svg?style=flat-square)](https://codecov.io/gh/eggjs/egg)
[![Known Vulnerabilities](https://snyk.io/test/npm/egg/badge.svg?style=flat-square)](https://snyk.io/test/npm/egg)
[![Open Collective backers and sponsors](https://img.shields.io/opencollective/all/eggjs?style=flat-square)](https://opencollective.com/eggjs)


## Features

- Built-in Process Management
Expand Down Expand Up @@ -62,5 +61,4 @@ To become a contributor, please follow our [contributing guide](CONTRIBUTING.md)

[MIT](LICENSE)


[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Feggjs%2Fegg.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Feggjs%2Fegg?ref=badge_large)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Feggjs%2Fegg.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Feggjs%2Fegg?ref=badge_large)
2 changes: 2 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ module.exports = appInfo => {
* @property {Number} httpsAgent.maxSockets - https agent max socket number of one host, default is `Number.MAX_SAFE_INTEGER` @ses https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
* @property {Number} httpsAgent.maxFreeSockets - https agent max free socket number of one host, default is 256.
* @property {Boolean} useHttpClientNext - use urllib@3 HttpClient
* @property {Boolean} allowH2 - Allow to use HTTP2 first, only work on `useHttpClientNext = true`
*/
config.httpclient = {
enableDNSCache: false,
Expand All @@ -326,6 +327,7 @@ module.exports = appInfo => {
maxFreeSockets: 256,
},
useHttpClientNext: false,
// allowH2: false,
};

/**
Expand Down
8 changes: 5 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,18 @@ declare module 'egg' {
request?: HttpClientRequestOptions | RequestOptionsOld;
/** Whether enable dns cache */
enableDNSCache?: boolean;
/** Enable proxy request, default is false. */
/** Enable proxy request. Default is `false`. */
enableProxy?: boolean;
/** proxy agent uri or options, default is null. */
/** proxy agent uri or options. Default is `null`. */
proxy?: string | { [key: string]: any };
/** DNS cache lookup interval */
dnsCacheLookupInterval?: number;
/** DNS cache max age */
dnsCacheMaxLength?: number;
/** use urllib@3 HttpClient */
/** use urllib@3 HttpClient. Default is `false` */
useHttpClientNext?: boolean;
/** Allow to use HTTP2 first, only work on `useHttpClientNext = true`. Default is `false` */
allowH2?: boolean;
}

export interface EggAppConfig {
Expand Down
14 changes: 11 additions & 3 deletions lib/core/httpclient_next.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ const { HttpClient } = require('urllib-next');
const ms = require('humanize-ms');

class HttpClientNext extends HttpClient {
constructor(app) {
constructor(app, options) {
normalizeConfig(app);
const config = app.config.httpclient;
options = options || {};
options = {
...app.config.httpclient,
...options,
};
super({
app,
defaultArgs: config.request,
defaultArgs: options.request,
allowH2: options.allowH2,
// use on egg-security ssrf
// https://github.com/eggjs/egg-security/blob/master/lib/extend/safe_curl.js#L11
checkAddress: options.checkAddress,
});
this.app = app;
}
Expand Down
24 changes: 17 additions & 7 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,30 @@ class EggApplication extends EggCore {
return await this.httpclient.request(url, opts);
}

/**
* Create a new HttpClient instance with custom options
* @param {Object} [options] HttpClient init options
*/
createHttpClient(options) {
let httpClient;
if (this.config.httpclient.useHttpClientNext) {
httpClient = new this.HttpClientNext(this, options);
} else if (this.config.httpclient.enableDNSCache) {
httpClient = new DNSCacheHttpClient(this, options);
} else {
httpClient = new this.HttpClient(this, options);
}
return httpClient;
}

/**
* HttpClient instance
* @see https://github.com/node-modules/urllib
* @member {HttpClient}
*/
get httpclient() {
if (!this[HTTPCLIENT]) {
if (this.config.httpclient.useHttpClientNext) {
this[HTTPCLIENT] = new this.HttpClientNext(this);
} else if (this.config.httpclient.enableDNSCache) {
this[HTTPCLIENT] = new DNSCacheHttpClient(this);
} else {
this[HTTPCLIENT] = new this.HttpClient(this);
}
this[HTTPCLIENT] = this.createHttpClient();
}
return this[HTTPCLIENT];
}
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "egg",
"version": "3.24.1",
"version": "3.26.0",
"publishConfig": {
"tag": "latest"
"tag": "release-3.x",
"access": "public"
},
"description": "A web framework's framework for Node.js",
"keywords": [
Expand Down Expand Up @@ -54,7 +55,7 @@
"onelogger": "^1.0.0",
"sendmessage": "^2.0.0",
"urllib": "^2.33.0",
"urllib-next": "npm:urllib@^3.22.4",
"urllib-next": "npm:urllib@^3.26.0",
"utility": "^2.1.0",
"ylru": "^1.3.2"
},
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/apps/httpclient-http2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const assert = require('assert');

module.exports = app => {
class CustomHttpClient extends app.HttpClientNext {
request(url, opt) {
return new Promise(resolve => {
assert(/^http/.test(url), 'url should start with http, but got ' + url);
resolve();
}).then(() => {
return super.request(url, opt);
});
}

curl(url, opt) {
return this.request(url, opt);
}
}
app.HttpClientNext = CustomHttpClient;
};
4 changes: 4 additions & 0 deletions test/fixtures/apps/httpclient-http2/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.httpclient = {
useHttpClientNext: true,
allowH2: true,
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/httpclient-http2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "httpclient-overwrite"
}
37 changes: 37 additions & 0 deletions test/lib/core/httpclient.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const assert = require('node:assert');
const { sensitiveHeaders } = require('node:http2');
const mm = require('egg-mock');
const urllib = require('urllib');
const Httpclient = require('../../../lib/core/httpclient');
Expand Down Expand Up @@ -237,6 +238,12 @@ describe('test/lib/core/httpclient.test.js', () => {
});
after(() => app.close());

it('should work', async () => {
const res = await app.httpclient.request(url);
assert.equal(res.status, 200);
assert.equal(res.data.toString(), 'GET /');
});

it('should set request default global timeout to 99ms', () => {
return app.httpclient.curl(`${url}/timeout`)
.catch(err => {
Expand All @@ -255,6 +262,36 @@ describe('test/lib/core/httpclient.test.js', () => {
});
});

describe('overwrite httpclient support allowH2=true', () => {
let app;
before(() => {
app = utils.app('apps/httpclient-http2');
return app.ready();
});
after(() => app.close());

it('should work on http2', async () => {
const res = await app.httpclient.request(url);
assert.equal(res.status, 200);
assert.equal(res.data.toString(), 'GET /');
assert.equal(sensitiveHeaders in res.headers, false);
const res2 = await app.httpclient.request('https://registry.npmmirror.com/urllib/latest', {
dataType: 'json',
});
assert.equal(res2.status, 200);
assert.equal(res2.data.name, 'urllib');
assert.equal(sensitiveHeaders in res2.headers, true);
});

it('should assert url', () => {
return app.httpclient.curl('unknown url')
.catch(err => {
assert(err);
assert(err.message.includes('url should start with http, but got unknown url'));
});
});
});

describe('httpclient tracer', () => {
let app;
before(() => {
Expand Down

0 comments on commit 2a581e4

Please sign in to comment.