-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Adds custom header to pass the id of the original client request from API to all cascading requests to CouchDb. Changes request ID to being a 12 char long string (uuid slice) instead of the whole uuid. #8216
- Loading branch information
1 parent
6876239
commit d2bebe8
Showing
12 changed files
with
1,036 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const { AsyncLocalStorage } = require('node:async_hooks'); | ||
const asyncLocalStorage = new AsyncLocalStorage(); | ||
const { REQUEST_ID_HEADER } = require('../server-utils'); | ||
|
||
const request = require('@medic/couch-request'); | ||
|
||
module.exports = { | ||
set: (req, callback) => { | ||
asyncLocalStorage.run({ clientRequest: req }, callback); | ||
}, | ||
getRequestId: () => { | ||
const localStorage = asyncLocalStorage.getStore(); | ||
return localStorage?.clientRequest?.id; | ||
}, | ||
}; | ||
|
||
request.initialize(module.exports, REQUEST_ID_HEADER); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const sinon = require('sinon'); | ||
const rewire = require('rewire'); | ||
const { expect } = require('chai'); | ||
const asyncHooks = require('node:async_hooks'); | ||
const request = require('@medic/couch-request'); | ||
const serverUtils = require('../../../src/server-utils'); | ||
|
||
describe('async-storage', () => { | ||
let service; | ||
let asyncLocalStorage; | ||
|
||
beforeEach(() => { | ||
asyncLocalStorage = sinon.spy(asyncHooks, 'AsyncLocalStorage'); | ||
sinon.stub(request, 'initialize'); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should initialize async storage and initialize couch-request', async () => { | ||
service = rewire('../../../src/services/async-storage'); | ||
|
||
expect(asyncLocalStorage.callCount).to.equal(1); | ||
expect(request.initialize.args).to.deep.equal([[ | ||
service, | ||
serverUtils.REQUEST_ID_HEADER | ||
]]); | ||
}); | ||
|
||
it('set should set request uuid', () => { | ||
service = rewire('../../../src/services/async-storage'); | ||
const asyncLocalStorage = service.__get__('asyncLocalStorage'); | ||
sinon.stub(asyncLocalStorage, 'run'); | ||
|
||
const req = { this: 'is a req' }; | ||
const cb = sinon.stub(); | ||
Object.freeze(req); | ||
service.set(req, cb); | ||
expect(asyncLocalStorage.run.args).to.deep.equal([[ | ||
{ clientRequest: req }, | ||
cb | ||
]]); | ||
}); | ||
|
||
it('getRequestId should return request id when set', done => { | ||
service = rewire('../../../src/services/async-storage'); | ||
const req = { id: 'uuid' }; | ||
service.set(req, () => { | ||
expect(service.getRequestId()).to.equal('uuid'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('getRequestId should return nothing when there is no local storage', () => { | ||
service = rewire('../../../src/services/async-storage'); | ||
expect(service.getRequestId()).to.equal(undefined); | ||
}); | ||
|
||
it('getRequestId should return nothing when there is no client request', done => { | ||
service = rewire('../../../src/services/async-storage'); | ||
service.set(undefined, () => { | ||
expect(service.getRequestId()).to.equal(undefined); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.