From 44950ed82a3ce4d5d4b9028aee98d6650298a552 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 18 Jun 2024 09:17:47 +0800 Subject: [PATCH 1/9] chore: start 3.x LTS --- .github/workflows/{nodejs.yml => nodejs-3.x.yml} | 9 +++++---- .github/workflows/{release.yml => release-3.x.yml} | 5 +++-- README.md | 6 ++---- package.json | 4 +--- 4 files changed, 11 insertions(+), 13 deletions(-) rename .github/workflows/{nodejs.yml => nodejs-3.x.yml} (70%) rename .github/workflows/{release.yml => release-3.x.yml} (86%) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs-3.x.yml similarity index 70% rename from .github/workflows/nodejs.yml rename to .github/workflows/nodejs-3.x.yml index 68ad9c1ac4..e23430a1a5 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs-3.x.yml @@ -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: @@ -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 }} diff --git a/.github/workflows/release.yml b/.github/workflows/release-3.x.yml similarity index 86% rename from .github/workflows/release.yml rename to .github/workflows/release-3.x.yml index d8fbb48f9f..89793f68df 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release-3.x.yml @@ -1,7 +1,8 @@ -name: Release +name: Release for 3.x + on: push: - branches: [ master, 2.x, 1.x ] + branches: [ 3.x ] jobs: release: diff --git a/README.md b/README.md index 602a358413..c094395ff3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) \ No newline at end of file +[![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) diff --git a/package.json b/package.json index bbf1bf0cf3..968ff262d1 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,7 @@ { "name": "egg", "version": "3.24.1", - "publishConfig": { - "tag": "latest" - }, + "publishConfig": {}, "description": "A web framework's framework for Node.js", "keywords": [ "web", From ceded0b1c9217503c5ed9226f96c493d6bd00547 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 27 Jun 2024 10:17:46 +0800 Subject: [PATCH 2/9] feat: allow to httpClient use HTTP2 first (#5332) base on https://github.com/node-modules/urllib/pull/516 ## Summary by CodeRabbit - **New Features** - Introduced the ability to enable HTTP/2 support through the `allowH2` property when using the next-generation HTTP client. - **Bug Fixes** - Enhanced clarity and accuracy of comments for HTTP client configuration properties. - **Improvements** - Updated `urllib-next` dependency to version `^3.26.0` for better performance and stability. - Added tests to verify HTTP/2 functionality and custom HTTP client behavior. --- config/config.default.js | 2 + index.d.ts | 8 ++-- lib/core/httpclient_next.js | 1 + package.json | 2 +- test/fixtures/apps/httpclient-http2/app.js | 21 +++++++++++ .../httpclient-http2/config/config.default.js | 4 ++ .../apps/httpclient-http2/package.json | 3 ++ test/lib/core/httpclient.test.js | 37 +++++++++++++++++++ 8 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/apps/httpclient-http2/app.js create mode 100644 test/fixtures/apps/httpclient-http2/config/config.default.js create mode 100644 test/fixtures/apps/httpclient-http2/package.json diff --git a/config/config.default.js b/config/config.default.js index 5c0259ea06..53a913a20a 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -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, @@ -326,6 +327,7 @@ module.exports = appInfo => { maxFreeSockets: 256, }, useHttpClientNext: false, + // allowH2: false, }; /** diff --git a/index.d.ts b/index.d.ts index 1dbf24df55..7cff3ed7ca 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 { diff --git a/lib/core/httpclient_next.js b/lib/core/httpclient_next.js index 266338d522..8b127cebbf 100644 --- a/lib/core/httpclient_next.js +++ b/lib/core/httpclient_next.js @@ -8,6 +8,7 @@ class HttpClientNext extends HttpClient { super({ app, defaultArgs: config.request, + allowH2: config.allowH2, }); this.app = app; } diff --git a/package.json b/package.json index 968ff262d1..7de8d5c60c 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,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" }, diff --git a/test/fixtures/apps/httpclient-http2/app.js b/test/fixtures/apps/httpclient-http2/app.js new file mode 100644 index 0000000000..29c5a62b35 --- /dev/null +++ b/test/fixtures/apps/httpclient-http2/app.js @@ -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; +}; diff --git a/test/fixtures/apps/httpclient-http2/config/config.default.js b/test/fixtures/apps/httpclient-http2/config/config.default.js new file mode 100644 index 0000000000..77834b89ad --- /dev/null +++ b/test/fixtures/apps/httpclient-http2/config/config.default.js @@ -0,0 +1,4 @@ +exports.httpclient = { + useHttpClientNext: true, + allowH2: true, +}; diff --git a/test/fixtures/apps/httpclient-http2/package.json b/test/fixtures/apps/httpclient-http2/package.json new file mode 100644 index 0000000000..56b5d28da8 --- /dev/null +++ b/test/fixtures/apps/httpclient-http2/package.json @@ -0,0 +1,3 @@ +{ + "name": "httpclient-overwrite" +} diff --git a/test/lib/core/httpclient.test.js b/test/lib/core/httpclient.test.js index 2b5fbb73e5..a98daf7390 100644 --- a/test/lib/core/httpclient.test.js +++ b/test/lib/core/httpclient.test.js @@ -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'); @@ -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 => { @@ -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(() => { From 8ce2ff90bfbb9e4580a23ea49a15fdb1c185fbb5 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 27 Jun 2024 10:22:38 +0800 Subject: [PATCH 3/9] chore: add npm publish tag --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 7de8d5c60c..836b0214db 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,10 @@ { "name": "egg", "version": "3.24.1", - "publishConfig": {}, + "publishConfig": { + "tag": "release-3.x", + "access": "public" + }, "description": "A web framework's framework for Node.js", "keywords": [ "web", From a8073b04fc3821bb23326c6c8b4fd0ccaeb5c200 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 27 Jun 2024 11:06:25 +0800 Subject: [PATCH 4/9] chore: add release config --- package.json | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/package.json b/package.json index 836b0214db..749e6f7196 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,15 @@ "tag": "release-3.x", "access": "public" }, + "release": { + "branches": [ + { + "name": "3.x", + "range": ">=3.0.0 < 4.0.0", + "channel": "release-3.x" + } + ] + }, "description": "A web framework's framework for Node.js", "keywords": [ "web", From b4f01a1c6bf006c943c85fce334b81d61f55b7d0 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 27 Jun 2024 11:18:42 +0800 Subject: [PATCH 5/9] chore: fix release branches name --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 749e6f7196..733d210319 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "release": { "branches": [ { - "name": "3.x", + "name": "([0-9]).x", "range": ">=3.0.0 < 4.0.0", "channel": "release-3.x" } From 8553c3f23e423e9f60144b11a484b703fe7c9229 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 27 Jun 2024 11:27:27 +0800 Subject: [PATCH 6/9] chore: remove auto release --- .github/workflows/release-3.x.yml | 15 --------------- package.json | 9 --------- 2 files changed, 24 deletions(-) delete mode 100644 .github/workflows/release-3.x.yml diff --git a/.github/workflows/release-3.x.yml b/.github/workflows/release-3.x.yml deleted file mode 100644 index 89793f68df..0000000000 --- a/.github/workflows/release-3.x.yml +++ /dev/null @@ -1,15 +0,0 @@ -name: Release for 3.x - -on: - push: - branches: [ 3.x ] - -jobs: - release: - name: Node.js - uses: eggjs/github-actions/.github/workflows/node-release.yml@master - secrets: - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - GIT_TOKEN: ${{ secrets.GIT_TOKEN }} - with: - install: 'npm install --legacy-peer-deps --no-package-lock --no-fund' diff --git a/package.json b/package.json index 733d210319..836b0214db 100644 --- a/package.json +++ b/package.json @@ -5,15 +5,6 @@ "tag": "release-3.x", "access": "public" }, - "release": { - "branches": [ - { - "name": "([0-9]).x", - "range": ">=3.0.0 < 4.0.0", - "channel": "release-3.x" - } - ] - }, "description": "A web framework's framework for Node.js", "keywords": [ "web", From 62285ed4e24763bc9d0b6f1f9c27d972f49ff8cf Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 27 Jun 2024 11:27:58 +0800 Subject: [PATCH 7/9] Release 3.25.0 --- History.md | 13 +++++++++++++ package.json | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 History.md diff --git a/History.md b/History.md new file mode 100644 index 0000000000..86a0e52514 --- /dev/null +++ b/History.md @@ -0,0 +1,13 @@ + +3.25.0 / 2024-06-27 +================== + +**features** + * [[`ceded0b1`](http://github.com/eggjs/egg/commit/ceded0b1c9217503c5ed9226f96c493d6bd00547)] - feat: allow to httpClient use HTTP2 first (#5332) (fengmk2 <>) + +**others** + * [[`8553c3f2`](http://github.com/eggjs/egg/commit/8553c3f23e423e9f60144b11a484b703fe7c9229)] - chore: remove auto release (fengmk2 <>) + * [[`b4f01a1c`](http://github.com/eggjs/egg/commit/b4f01a1c6bf006c943c85fce334b81d61f55b7d0)] - chore: fix release branches name (fengmk2 <>) + * [[`a8073b04`](http://github.com/eggjs/egg/commit/a8073b04fc3821bb23326c6c8b4fd0ccaeb5c200)] - chore: add release config (fengmk2 <>) + * [[`8ce2ff90`](http://github.com/eggjs/egg/commit/8ce2ff90bfbb9e4580a23ea49a15fdb1c185fbb5)] - chore: add npm publish tag (fengmk2 <>) + * [[`44950ed8`](http://github.com/eggjs/egg/commit/44950ed82a3ce4d5d4b9028aee98d6650298a552)] - chore: start 3.x LTS (fengmk2 <>) diff --git a/package.json b/package.json index 836b0214db..cfe5a48298 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg", - "version": "3.24.1", + "version": "3.25.0", "publishConfig": { "tag": "release-3.x", "access": "public" From b0292a8b7e76d5dbf7441b7164c39441dbae51ec Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 1 Jul 2024 23:00:53 +0800 Subject: [PATCH 8/9] feat: allow to create httpClient from app (#5334) --- lib/core/httpclient_next.js | 15 +++++++++++---- lib/egg.js | 24 +++++++++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/core/httpclient_next.js b/lib/core/httpclient_next.js index 8b127cebbf..1a18e6ce23 100644 --- a/lib/core/httpclient_next.js +++ b/lib/core/httpclient_next.js @@ -2,13 +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, - allowH2: config.allowH2, + 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; } diff --git a/lib/egg.js b/lib/egg.js index aabba225b4..a70c69fae8 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -286,6 +286,22 @@ 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 @@ -293,13 +309,7 @@ class EggApplication extends EggCore { */ 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]; } From c4bc7f42401f3325a4862a12423db3b15a02555a Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 1 Jul 2024 23:02:48 +0800 Subject: [PATCH 9/9] Release 3.26.0 --- History.md | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index 86a0e52514..d0634a5fe1 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,10 @@ +3.26.0 / 2024-07-01 +================== + +**features** + * [[`b0292a8b`](http://github.com/eggjs/egg/commit/b0292a8b7e76d5dbf7441b7164c39441dbae51ec)] - feat: allow to create httpClient from app (#5334) (fengmk2 <>) + 3.25.0 / 2024-06-27 ================== diff --git a/package.json b/package.json index cfe5a48298..2d8cf0ad98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg", - "version": "3.25.0", + "version": "3.26.0", "publishConfig": { "tag": "release-3.x", "access": "public"