diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 38ee4915..0e621ec6 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -13,12 +13,10 @@ name: "CodeQL" on: push: - branches: [ "master", 3.x ] + branches: [ "master" ] pull_request: # The branches below must be a subset of the branches above branches: [ "master" ] - schedule: - - cron: '33 15 * * 2' jobs: analyze: diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index adbf2fdd..a2673ed7 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -7,11 +7,9 @@ on: pull_request: branches: [ master ] - workflow_dispatch: {} - jobs: Job: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 + uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: - version: '14.19.0, 14, 16, 18' + version: '14.19.0, 14, 16, 18, 20' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 16125872..a2bf04a7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,14 +4,10 @@ on: push: branches: [ master ] - workflow_dispatch: {} - jobs: release: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-release.yml@v1 + uses: eggjs/github-actions/.github/workflows/node-release.yml@master secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GIT_TOKEN: ${{ secrets.GIT_TOKEN }} - with: - checkTest: false diff --git a/lib/egg.js b/lib/egg.js index 4911773b..bcd8ad2d 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -1,4 +1,5 @@ const assert = require('assert'); +const { AsyncLocalStorage } = require('async_hooks'); const fs = require('fs'); const KoaApplication = require('koa'); const EggConsoleLogger = require('egg-logger').EggConsoleLogger; @@ -15,6 +16,7 @@ const DEPRECATE = Symbol('EggCore#deprecate'); const ROUTER = Symbol('EggCore#router'); const EGG_LOADER = Symbol.for('egg#loader'); const CLOSE_PROMISE = Symbol('EggCore#closePromise'); +const EGG_CTX_STORAGE = Symbol.for('egg#ctxStorage'); class EggCore extends KoaApplication { @@ -35,9 +37,13 @@ class EggCore extends KoaApplication { assert(fs.statSync(options.baseDir).isDirectory(), `Directory ${options.baseDir} is not a directory`); assert(options.type === 'application' || options.type === 'agent', 'options.type should be application or agent'); - // enable asyncLocalStorage by default - // https://github.com/koajs/koa/pull/1721 - super({ asyncLocalStorage: true }); + // disable koa als and use egg logic + super({ asyncLocalStorage: false }); + // can access the AsyncLocalStorage instance in global + if (!global[EGG_CTX_STORAGE]) { + global[EGG_CTX_STORAGE] = new AsyncLocalStorage(); + } + this.ctxStorage = global[EGG_CTX_STORAGE]; this.timing = new Timing(); diff --git a/test/asyncLocalStorage.test.js b/test/asyncLocalStorage.test.js index 23d85c65..9a4854c2 100644 --- a/test/asyncLocalStorage.test.js +++ b/test/asyncLocalStorage.test.js @@ -1,5 +1,6 @@ const assert = require('assert'); const path = require('path'); +const { AsyncLocalStorage } = require('async_hooks'); const request = require('supertest'); const EggApplication = require('./fixtures/egg').Application; @@ -23,4 +24,10 @@ describe('test/asyncLocalStorage.test.js', () => { assert(res.body.traceId); assert(app.currentContext === undefined); }); + + it('should access als on global', async () => { + assert(global[Symbol.for('egg#ctxStorage')]); + assert(global[Symbol.for('egg#ctxStorage')] instanceof AsyncLocalStorage); + assert.equal(app.ctxStorage, global[Symbol.for('egg#ctxStorage')]); + }); }); diff --git a/test/loader/mixin/load_middleware.test.js b/test/loader/mixin/load_middleware.test.js index 2c5bd1b6..077a1ff7 100644 --- a/test/loader/mixin/load_middleware.test.js +++ b/test/loader/mixin/load_middleware.test.js @@ -32,10 +32,12 @@ describe('test/loader/mixin/load_middleware.test.js', () => { assert(!('a' in app.middleware)); assert(app.middleware.static === app.middlewares.static); + const names = []; for (const mw of app.middleware) { assert(typeof mw === 'function'); + names.push(mw._name); } - assert(Object.keys(app.middleware).length === 4); + assert.deepEqual(names, [ 'status', 'static', 'custom' ]); }); it('should override middlewares of plugin by framework', async () => {