Skip to content

Commit

Permalink
feat: export asyncLocalStorage instance to global (#267)
Browse files Browse the repository at this point in the history
closes eggjs/egg#5271

pick from #266
  • Loading branch information
fengmk2 authored Dec 19, 2023
1 parent 2920f6e commit 910fe85
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
6 changes: 1 addition & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 9 additions & 3 deletions lib/egg.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -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();

Expand Down
7 changes: 7 additions & 0 deletions test/asyncLocalStorage.test.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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')]);
});
});
4 changes: 3 additions & 1 deletion test/loader/mixin/load_middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 910fe85

Please sign in to comment.