10000 feat: export asyncLocalStorage instance to global by fengmk2 · Pull Request #267 · eggjs/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: export asyncLocalStorage instance to global #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 () => {
< 8000 /td> 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