Skip to content
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

fix: use gals lib #268

Merged
merged 2 commits 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
8 changes: 2 additions & 6 deletions lib/egg.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');
const fs = require('fs');
const KoaApplication = require('koa');
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
const debug = require('debug')('egg-core');
const is = require('is-type-of');
const co = require('co');
const Router = require('@eggjs/router').EggRouter;
const { getAsyncLocalStorage } = require('gals');
const BaseContextClass = require('./utils/base_context_class');
const utils = require('./utils');
const Timing = require('./utils/timing');
Expand All @@ -16,7 +16,6 @@ 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 @@ -40,10 +39,7 @@ class EggCore extends KoaApplication {
// 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.ctxStorage = getAsyncLocalStorage();

this.timing = new Timing();

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"egg-logger": "^3.1.0",
"egg-path-matching": "^1.0.1",
"extend2": "^1.0.0",
"gals": "^1.0.1",
"get-ready": "^2.0.1",
"globby": "^11.0.2",
"is-type-of": "^1.2.1",
Expand Down
9 changes: 6 additions & 3 deletions test/asyncLocalStorage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const path = require('path');
const { AsyncLocalStorage } = require('async_hooks');
const request = require('supertest');
const { getAsyncLocalStorage, kGALS } = require('gals');
const EggApplication = require('./fixtures/egg').Application;

describe('test/asyncLocalStorage.test.js', () => {
Expand All @@ -26,8 +27,10 @@ describe('test/asyncLocalStorage.test.js', () => {
});

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')]);
assert(global[Symbol.for('gals#asyncLocalStorage')]);
assert(global[kGALS]);
assert(global[Symbol.for('gals#asyncLocalStorage')] instanceof AsyncLocalStorage);
assert.equal(app.ctxStorage, global[Symbol.for('gals#asyncLocalStorage')]);
assert.equal(app.ctxStorage, getAsyncLocalStorage());
});
});
Loading