Skip to content

Commit

Permalink
fix: use gals lib (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 19, 2023
1 parent 910fe85 commit 72fafc8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
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 @@ -62,6 +62,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());
});
});

0 comments on commit 72fafc8

Please sign in to comment.