Skip to content

Commit 2bb02dc

Browse files
committed
fix(#78): move instance from module to global scope
1 parent 169f7d3 commit 2bb02dc

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
'use strict';
22

33
const { AsyncLocalStorage } = require('async_hooks');
4-
const asyncLocalStorage = new AsyncLocalStorage();
4+
5+
const STORAGE_KEY = Symbol.for(
6+
'skonves/express-http-context/asyncLocalStorage',
7+
);
8+
9+
globalThis[STORAGE_KEY] = globalThis[STORAGE_KEY] ?? new AsyncLocalStorage();
510

611
/** Express.js middleware that is responsible for initializing the context for each request. */
712
function middleware(req, res, next) {
13+
const asyncLocalStorage = globalThis[STORAGE_KEY];
814
if (!asyncLocalStorage.getStore()) {
915
asyncLocalStorage.run(new Map(), () => next());
1016
} else {
@@ -17,7 +23,7 @@ function middleware(req, res, next) {
1723
* @param {string} key
1824
*/
1925
function get(key) {
20-
return asyncLocalStorage.getStore()?.get(key);
26+
return globalThis[STORAGE_KEY].getStore()?.get(key);
2127
}
2228

2329
/**
@@ -26,6 +32,7 @@ function get(key) {
2632
* @param {*} value
2733
*/
2834
function set(key, value) {
35+
const asyncLocalStorage = globalThis[STORAGE_KEY];
2936
if (asyncLocalStorage.getStore()) {
3037
asyncLocalStorage.getStore()?.set(key, value);
3138
return value;
@@ -37,5 +44,5 @@ module.exports = {
3744
middleware,
3845
get: get,
3946
set: set,
40-
asyncLocalStorage
47+
asyncLocalStorage: globalThis[STORAGE_KEY],
4148
};

tests/express-test-harness.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,9 @@ describe('express-http-context', function () {
260260
expect(response1.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]?.length).toBe(21);
261261
expect(response2.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]?.length).toBe(21);
262262

263-
// This is the specific example I had flagged in the Github issue (#26, #78) - where
264-
// setting something into the httpContext in a common library, but it's
265-
// unusable from within the application code.
266263
expect(response1.body.requestId).toBe(response1.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]);
267264
expect(response2.body.requestId).toBe(response2.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]);
268265

269-
// These operations also fail, I suspect, because neither of the set/get
270-
// functions are usable, because the directly imported AsyncLocalStorage has
271-
// not been initialised by a call to `app.use(middleware)` within our code
272-
// here. Effectively this is another manifestation of the same bug -
273-
// showing that although the middleware *has* already been initialised in
274-
// Express request handler chain, it is not usable because the
275-
// AsyncLocalStorage context is not identical for all usages of the
276-
// `express-http-context` library code.
277266
expect(response1.body.valueFromContext).toBe('value1');
278267
expect(response2.body.valueFromContext).toBe('value2');
279268
});

0 commit comments

Comments
 (0)