Skip to content
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: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ jspm_packages
.node_repl_history

*.tgz

# This is a local copy of the express-http-context library
# It is used to avoid a circular dependency on the express-http-context library
packages/express-http-context-intermediate-library/express-http-context.js
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
'use strict';

const { AsyncLocalStorage } = require('async_hooks');
const asyncLocalStorage = new AsyncLocalStorage();

const STORAGE_KEY = Symbol.for(
'skonves/express-http-context/asyncLocalStorage',
);

globalThis[STORAGE_KEY] = globalThis[STORAGE_KEY] ?? new AsyncLocalStorage();

/** Express.js middleware that is responsible for initializing the context for each request. */
function middleware(req, res, next) {
const asyncLocalStorage = globalThis[STORAGE_KEY];
if (!asyncLocalStorage.getStore()) {
asyncLocalStorage.run(new Map(), () => next());
} else {
Expand All @@ -17,7 +23,7 @@ function middleware(req, res, next) {
* @param {string} key
*/
function get(key) {
return asyncLocalStorage.getStore()?.get(key);
return globalThis[STORAGE_KEY].getStore()?.get(key);
}

/**
Expand All @@ -26,6 +32,7 @@ function get(key) {
* @param {*} value
*/
function set(key, value) {
const asyncLocalStorage = globalThis[STORAGE_KEY];
if (asyncLocalStorage.getStore()) {
asyncLocalStorage.getStore()?.set(key, value);
return value;
Expand All @@ -37,5 +44,5 @@ module.exports = {
middleware,
get: get,
set: set,
asyncLocalStorage
asyncLocalStorage: globalThis[STORAGE_KEY],
};
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
'!**/*.config.js',
'!**/coverage/**',
'!**/node_modules/**',
'!**/packages/**',
'!**/tests/**',
],
coverageDirectory: 'coverage',
Expand Down
54 changes: 52 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"description": "Get and set request-scoped context anywhere",
"main": "index.js",
"browser": "browser.js",
"sideEffects": true,
"engines": {
"node": ">=14.8.0"
},
"scripts": {
"pretest": "cp index.js packages/express-http-context-intermediate-library/express-http-context.js",
"test": "jest"
},
"repository": {
Expand All @@ -32,6 +34,7 @@
"homepage": "https://github.com/skonves/express-http-context#readme",
"funding": "https://github.com/skonves/express-http-context?sponsor=1",
"devDependencies": {
"@oliverlockwood/express-http-context-intermediate-library": "file:packages/express-http-context-intermediate-library",
"express": "^4.21.2",
"jest": "^29.7.0",
"supertest": "^7.0.0"
Expand Down
8 changes: 8 additions & 0 deletions packages/express-http-context-intermediate-library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# express-http-context-intermediate-library

An intermediate library, which makes use of express-http-context, to demonstrate existence (or otherwise!) of a bug


### Publishing to NPM

Run `./npmPublish.sh a.b.c`, where `a.b.c` is the intended semantic version (e.g. `1.2.3`).
29 changes: 29 additions & 0 deletions packages/express-http-context-intermediate-library/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

const httpContext = require("./express-http-context");
const { nanoid } = require("nanoid");

const REQUEST_ID_HTTP_HEADER_NAME = "my-request-id";
const REQUEST_ID_CONTEXT_KEY = "myRequestId";

// less realistic, but useful for testing!
const REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME = "my-request-id-in-response";

function init(app) {
app.use(httpContext.middleware);

app.use((req, res, next) => {
const requestId = req.header(REQUEST_ID_HTTP_HEADER_NAME) || nanoid();

httpContext.set(REQUEST_ID_CONTEXT_KEY, requestId);
res.header(REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME, requestId);

next();
});
}

module.exports = {
init,
REQUEST_ID_CONTEXT_KEY,
REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME,
};
36 changes: 36 additions & 0 deletions packages/express-http-context-intermediate-library/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@oliverlockwood/express-http-context-intermediate-library",
"version": "0.0.5-original-library",
"description": "An intermediate library, which makes use of express-http-context, to demonstrate existence (or otherwise!) of a bug",
"main": "index.js",
"types": "index.d.ts",
"engines": {
"node": ">=16.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/oliverlockwood/express-http-context-intermediate-library.git"
},
"keywords": [
"express",
"http",
"request",
"context"
],
"author": "Oliver Lockwood",
"contributors": [
"Oliver Lockwood (@oliverlockwood)"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/oliverlockwood/express-http-context-intermediate-library/issues"
},
"homepage": "https://github.com/oliverlockwood/express-http-context-intermediate-library#readme",
"directories": {
"test": "test"
},
"dependencies": {
"express": "4.21.2",
"nanoid": "3.3.11"
}
}
44 changes: 44 additions & 0 deletions tests/express-test-harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const express = require('express');
const supertest = require('supertest');

const httpContext = require('../index');
const { init, REQUEST_ID_CONTEXT_KEY, REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME } = require('@oliverlockwood/express-http-context-intermediate-library');

describe('express-http-context', function () {
it('does not store or return context outside of request', function () {
Expand Down Expand Up @@ -222,4 +223,47 @@ describe('express-http-context', function () {
done();
});
});

it('maintains unique value when the library is depended upon both directly and transitively', async () => {
// ARRANGE
const app = express();

// this function in the test library makes the following two calls:
// 1. app.use(middleware) and
// 2. httpContext.set(REQUEST_ID_CONTEXT_KEY, <a unique id>)
// as can be seen in https://github.com/oliverlockwood/express-http-context-intermediate-library/blob/original-express-http-context/src/index.ts#L13-L19
init(app);

app.get('/', ((req, res) => {
httpContext.set('value', req.query['value']);

res.status(200).json({
fred: '123',
value: req.query['value'],
valueFromContext: httpContext.get('value'),
requestId: httpContext.get(REQUEST_ID_CONTEXT_KEY)
});
}));

const request = supertest(app);

// ACT
const [response1, response2] = await Promise.all([
request.get('/').query({ value: 'value1' }),
request.get('/').query({ value: 'value2' }),
]);

// ASSERT
expect(response1.body.value).toBe('value1');
expect(response2.body.value).toBe('value2');

expect(response1.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]?.length).toBe(21);
expect(response2.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]?.length).toBe(21);

expect(response1.body.requestId).toBe(response1.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]);
expect(response2.body.requestId).toBe(response2.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]);

expect(response1.body.valueFromContext).toBe('value1');
expect(response2.body.valueFromContext).toBe('value2');
});
});