From 72fafc8abd4cec322c0a210aa797f28c4bfdae32 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 20 Dec 2023 01:00:06 +0800 Subject: [PATCH] fix: use gals lib (#268) https://github.com/node-modules/gals --- lib/egg.js | 8 ++------ package.json | 1 + test/asyncLocalStorage.test.js | 9 ++++++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/egg.js b/lib/egg.js index bcd8ad2d..9db5022d 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -1,5 +1,4 @@ const assert = require('assert'); -const { AsyncLocalStorage } = require('async_hooks'); const fs = require('fs'); const KoaApplication = require('koa'); const EggConsoleLogger = require('egg-logger').EggConsoleLogger; @@ -7,6 +6,7 @@ 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'); @@ -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 { @@ -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(); diff --git a/package.json b/package.json index aa3780d4..fd158a26 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/asyncLocalStorage.test.js b/test/asyncLocalStorage.test.js index 9a4854c2..0f71c19a 100644 --- a/test/asyncLocalStorage.test.js +++ b/test/asyncLocalStorage.test.js @@ -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', () => { @@ -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()); }); });