@defra/lis-hubs-infra-core 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-hubs-infra-core",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Shared hub mechanics for livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -25,6 +25,12 @@ function onRequest(request, h) {
25
25
  const correlationId =
26
26
  request.headers['x-cdp-request-id'] || crypto.randomUUID()
27
27
  initialStore.set(`correlation_id`, correlationId)
28
+ // A single Map, shared by reference across both cycles below - not a
29
+ // fresh Map per cycle, or anything set into logger.context during
30
+ // _lifecycle (e.g. hydrateAuthorization's user_email_hash, set from
31
+ // onPreAuth) would be invisible to _reply, which is where hapi-pino's
32
+ // response log line actually fires.
33
+ const logContextStore = new Map(initialStore)
28
34
 
29
35
  // _lifecycle covers routing through the handler; _reply covers
30
36
  // everything after (including _postCycle, _abort and _finalize,
@@ -36,7 +42,7 @@ function onRequest(request, h) {
36
42
  // reached that log line - context does not survive back across an
37
43
  // awaited storage.run() call into an unwrapped caller.
38
44
  for (const cycle of ['_lifecycle', '_reply']) {
39
- wrapLifecycleHook(request, cycle, initialStore)
45
+ wrapLifecycleHook(request, cycle, initialStore, logContextStore)
40
46
  }
41
47
  return h.continue
42
48
  }
@@ -49,15 +55,14 @@ function onRequest(request, h) {
49
55
  * @param {object} request
50
56
  * @param {string} cycle - '_lifecycle' or '_reply'
51
57
  * @param {Map} initialStore
58
+ * @param {Map} logContextStore
52
59
  * @returns {void}
53
60
  */
54
- function wrapLifecycleHook(request, cycle, initialStore) {
61
+ function wrapLifecycleHook(request, cycle, initialStore, logContextStore) {
55
62
  const original = request[cycle].bind(request)
56
63
  request[cycle] = (...args) =>
57
64
  contextStore.run(initialStore, () =>
58
- logger.context.run(new Map(initialStore.entries()), () =>
59
- original(...args)
60
- )
65
+ logger.context.run(logContextStore, () => original(...args))
61
66
  )
62
67
  }
63
68
 
@@ -10,6 +10,7 @@ import {
10
10
  plugin,
11
11
  set
12
12
  } from './request-context.js'
13
+ import { logger } from './logging/index.js'
13
14
 
14
15
  const mocks = {
15
16
  run: vi.spyOn(AsyncLocalStorage.prototype, 'run'),
@@ -75,12 +76,43 @@ describe('requestContext', () => {
75
76
  request._reply()
76
77
  expect(mocks.run).toHaveBeenCalledTimes(4)
77
78
  const [firstStore] = mocks.run.mock.calls[0]
79
+ const [secondStore] = mocks.run.mock.calls[1]
78
80
  const [thirdStore] = mocks.run.mock.calls[2]
81
+ const [fourthStore] = mocks.run.mock.calls[3]
79
82
  expect(firstStore).toBe(thirdStore)
80
83
  expect(firstStore).toBeInstanceOf(Map)
84
+ expect(secondStore).toBe(fourthStore)
85
+ expect(secondStore).toBeInstanceOf(Map)
81
86
  expect(result).toBe(h.continue)
82
87
  })
83
88
 
89
+ test('a value set on logger.context during _lifecycle is still visible during _reply', () => {
90
+ // Arrange
91
+ const ext = vi.fn()
92
+ const server = { ext }
93
+ plugin.register(server)
94
+ const [[, handler]] = ext.mock.calls
95
+ const h = { continue: Symbol('continue') }
96
+ let valueSeenDuringReply
97
+ const request = {
98
+ headers: {},
99
+ _lifecycle: vi.fn(() => {
100
+ logger.context.set('user_email_hash', 'hashed-value')
101
+ }),
102
+ _reply: vi.fn(() => {
103
+ valueSeenDuringReply = logger.context.get('user_email_hash')
104
+ })
105
+ }
106
+
107
+ // Act
108
+ handler(request, h)
109
+ request._lifecycle()
110
+ request._reply()
111
+
112
+ // Assert
113
+ expect(valueSeenDuringReply).toBe('hashed-value')
114
+ })
115
+
84
116
  test('onRequest forwards arguments through _reply to the original method', () => {
85
117
  // Arrange
86
118
  const ext = vi.fn()