core-services-sdk 1.3.88 → 1.3.90
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
|
@@ -13,6 +13,18 @@ import { mask } from '../util/mask-sensitive.js'
|
|
|
13
13
|
|
|
14
14
|
const generateMsgId = () => `rbt_${ulid()}`
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Validates a RabbitMQ queue name.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} queue
|
|
20
|
+
* @throws {Error} If queue name is invalid
|
|
21
|
+
*/
|
|
22
|
+
const assertValidQueueName = (queue) => {
|
|
23
|
+
if (!queue || typeof queue !== 'string' || !queue.trim()) {
|
|
24
|
+
throw new Error(`Invalid queue name: "${queue}"`)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
16
28
|
/**
|
|
17
29
|
* Connects to a RabbitMQ server.
|
|
18
30
|
*
|
|
@@ -191,19 +203,15 @@ export const subscribeToQueue = async ({
|
|
|
191
203
|
}) => {
|
|
192
204
|
const logger = log.child({ op: 'subscribeToQueue', queue })
|
|
193
205
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
logger.error({ error: message })
|
|
197
|
-
throw new Error(message)
|
|
198
|
-
}
|
|
206
|
+
try {
|
|
207
|
+
assertValidQueueName(queue)
|
|
199
208
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
209
|
+
if (typeof onReceive !== 'function') {
|
|
210
|
+
const message = `Cannot subscribe to queue "${queue}" because onReceive is not a function`
|
|
211
|
+
logger.error({ error: message })
|
|
212
|
+
throw new Error(message)
|
|
213
|
+
}
|
|
205
214
|
|
|
206
|
-
try {
|
|
207
215
|
await channel.assertQueue(queue, { durable: true })
|
|
208
216
|
|
|
209
217
|
if (prefetch) {
|
|
@@ -282,6 +290,8 @@ export const initializeQueue = async ({ host, log }) => {
|
|
|
282
290
|
* @returns {Promise<boolean>} True if the message was sent successfully
|
|
283
291
|
*/
|
|
284
292
|
const publish = async (queue, data, correlationId) => {
|
|
293
|
+
assertValidQueueName(queue)
|
|
294
|
+
|
|
285
295
|
const msgId = generateMsgId()
|
|
286
296
|
const t0 = Date.now()
|
|
287
297
|
const logChild = logger.child({
|
|
@@ -296,6 +306,7 @@ export const initializeQueue = async ({ host, log }) => {
|
|
|
296
306
|
|
|
297
307
|
await channel.assertQueue(queue, { durable: true })
|
|
298
308
|
const payload = { msgId, data, correlationId }
|
|
309
|
+
|
|
299
310
|
const sent = channel.sendToQueue(
|
|
300
311
|
queue,
|
|
301
312
|
Buffer.from(JSON.stringify(payload)),
|
|
@@ -31,39 +31,24 @@ import { execSync } from 'node:child_process'
|
|
|
31
31
|
export function startRabbit({ containerName, ...rest }) {
|
|
32
32
|
console.log(`[RabbitTest] Starting RabbitMQ...`)
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
execSync(`docker rm -f ${containerName}`, { stdio: 'ignore' })
|
|
36
|
-
} catch {}
|
|
37
|
-
|
|
38
|
-
// Kill any containers that might still be holding the ports
|
|
39
|
-
for (const port of [rest.amqpPort, rest.uiPort]) {
|
|
40
|
-
try {
|
|
41
|
-
const id = execSync(`docker ps -q --filter "publish=${port}"`, {
|
|
42
|
-
encoding: 'utf8',
|
|
43
|
-
}).trim()
|
|
44
|
-
if (id) {
|
|
45
|
-
execSync(`docker rm -f ${id}`, { stdio: 'ignore' })
|
|
46
|
-
}
|
|
47
|
-
} catch {}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
execSync(
|
|
51
|
-
`docker run -d \
|
|
34
|
+
const dockerRunCmd = `docker run -d \
|
|
52
35
|
--name ${containerName} \
|
|
53
36
|
-e RABBITMQ_DEFAULT_USER=${rest.user} \
|
|
54
37
|
-e RABBITMQ_DEFAULT_PASS=${rest.pass} \
|
|
55
38
|
-p ${rest.amqpPort}:5672 \
|
|
56
39
|
-p ${rest.uiPort}:15672 \
|
|
57
|
-
--tmpfs /var/lib/rabbitmq \
|
|
58
40
|
--health-cmd="rabbitmq-diagnostics -q ping" \
|
|
59
41
|
--health-interval=5s \
|
|
60
42
|
--health-timeout=5s \
|
|
61
43
|
--health-retries=10 \
|
|
62
|
-
rabbitmq:3-management
|
|
63
|
-
{ stdio: 'inherit' },
|
|
64
|
-
)
|
|
44
|
+
rabbitmq:3-management`
|
|
65
45
|
|
|
66
|
-
|
|
46
|
+
cleanupContainer(containerName, [rest.amqpPort, rest.uiPort])
|
|
47
|
+
execSync(dockerRunCmd, { stdio: 'ignore' })
|
|
48
|
+
waitForRabbitHealthy(containerName, dockerRunCmd, [
|
|
49
|
+
rest.amqpPort,
|
|
50
|
+
rest.uiPort,
|
|
51
|
+
])
|
|
67
52
|
}
|
|
68
53
|
|
|
69
54
|
/**
|
|
@@ -79,54 +64,90 @@ export function startRabbit({ containerName, ...rest }) {
|
|
|
79
64
|
export function stopRabbit(containerName = 'rabbit-test') {
|
|
80
65
|
console.log(`[RabbitTest] Stopping RabbitMQ...`)
|
|
81
66
|
try {
|
|
82
|
-
execSync(`docker rm -
|
|
67
|
+
execSync(`docker rm -fv ${containerName}`, { stdio: 'ignore' })
|
|
83
68
|
} catch (error) {
|
|
84
69
|
console.error(`[RabbitTest] Failed to stop RabbitMQ: ${error}`)
|
|
85
70
|
}
|
|
86
71
|
}
|
|
87
72
|
|
|
73
|
+
function cleanupContainer(containerName, ports = []) {
|
|
74
|
+
try {
|
|
75
|
+
execSync(`docker rm -fv ${containerName}`, { stdio: 'ignore' })
|
|
76
|
+
} catch {}
|
|
77
|
+
|
|
78
|
+
for (const port of ports) {
|
|
79
|
+
try {
|
|
80
|
+
const id = execSync(`docker ps -q --filter "publish=${port}"`, {
|
|
81
|
+
encoding: 'utf8',
|
|
82
|
+
}).trim()
|
|
83
|
+
if (id) {
|
|
84
|
+
execSync(`docker rm -fv ${id}`, { stdio: 'ignore' })
|
|
85
|
+
}
|
|
86
|
+
} catch {}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
88
90
|
/**
|
|
89
91
|
* Waits until the RabbitMQ Docker container reports a healthy status.
|
|
90
92
|
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
+
* If the container crashes (e.g. due to Docker volume initialization race
|
|
94
|
+
* on macOS), it is automatically recreated and retried.
|
|
93
95
|
*
|
|
94
96
|
* @param {string} containerName
|
|
95
97
|
* Docker container name.
|
|
96
98
|
*
|
|
99
|
+
* @param {string} dockerRunCmd
|
|
100
|
+
* The docker run command to recreate the container if it crashes.
|
|
101
|
+
*
|
|
102
|
+
* @param {number[]} ports
|
|
103
|
+
* Host ports to clean up when recreating.
|
|
104
|
+
*
|
|
97
105
|
* @returns {void}
|
|
98
106
|
*
|
|
99
107
|
* @throws {Error}
|
|
100
108
|
* Throws if the container does not become healthy within the timeout.
|
|
101
109
|
*/
|
|
102
|
-
function waitForRabbitHealthy(containerName) {
|
|
110
|
+
function waitForRabbitHealthy(containerName, dockerRunCmd, ports) {
|
|
103
111
|
console.log(`[RabbitTest] Waiting for RabbitMQ to be healthy...`)
|
|
104
112
|
|
|
105
|
-
const maxRetries =
|
|
113
|
+
const maxRetries = 90
|
|
114
|
+
const maxRestarts = 3
|
|
106
115
|
let retries = 0
|
|
116
|
+
let restarts = 0
|
|
107
117
|
|
|
108
118
|
while (retries < maxRetries) {
|
|
119
|
+
// Check if the container has crashed
|
|
109
120
|
try {
|
|
110
|
-
const
|
|
111
|
-
`docker inspect --format='{{.State.
|
|
121
|
+
const status = execSync(
|
|
122
|
+
`docker inspect --format='{{.State.Status}}' ${containerName}`,
|
|
112
123
|
{ encoding: 'utf8' },
|
|
113
124
|
).trim()
|
|
114
125
|
|
|
115
|
-
if (
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (retries % 10 === 0 && retries > 0) {
|
|
126
|
+
if (status === 'exited' || status === 'dead') {
|
|
127
|
+
if (restarts >= maxRestarts) {
|
|
128
|
+
break
|
|
129
|
+
}
|
|
130
|
+
restarts++
|
|
121
131
|
console.log(
|
|
122
|
-
`[RabbitTest]
|
|
132
|
+
`[RabbitTest] Container crashed, restarting (${restarts}/${maxRestarts})...`,
|
|
123
133
|
)
|
|
134
|
+
cleanupContainer(containerName, ports)
|
|
135
|
+
execSync(dockerRunCmd, { stdio: 'ignore' })
|
|
136
|
+
execSync('sleep 2')
|
|
137
|
+
retries++
|
|
138
|
+
continue
|
|
124
139
|
}
|
|
140
|
+
} catch {}
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
execSync(`docker exec ${containerName} rabbitmq-diagnostics -q ping`, {
|
|
144
|
+
stdio: 'ignore',
|
|
145
|
+
})
|
|
146
|
+
console.log(`[RabbitTest] RabbitMQ is ready.`)
|
|
147
|
+
return
|
|
125
148
|
} catch {
|
|
126
149
|
if (retries % 10 === 0 && retries > 0) {
|
|
127
|
-
console.log(
|
|
128
|
-
`[RabbitTest] Container not ready yet (${retries}/${maxRetries})`,
|
|
129
|
-
)
|
|
150
|
+
console.log(`[RabbitTest] Still waiting... (${retries}/${maxRetries})`)
|
|
130
151
|
}
|
|
131
152
|
}
|
|
132
153
|
|
|
@@ -14,11 +14,13 @@ const AMQP_PORT = 5679
|
|
|
14
14
|
const UI_PORT = 15679
|
|
15
15
|
const USER = 'test'
|
|
16
16
|
const PASS = 'test'
|
|
17
|
-
const QUEUE = 'integration-test-queue'
|
|
18
17
|
|
|
19
18
|
const log = pino({
|
|
20
19
|
level: 'silent',
|
|
21
20
|
})
|
|
21
|
+
|
|
22
|
+
const uniqueQueue = (name) => `${name}-${Date.now()}-${Math.random()}`
|
|
23
|
+
|
|
22
24
|
// @ts-ignore
|
|
23
25
|
async function waitForRabbitConnection({ uri, log, timeoutMs = 30000 }) {
|
|
24
26
|
const start = Date.now()
|
|
@@ -36,12 +38,7 @@ async function waitForRabbitConnection({ uri, log, timeoutMs = 30000 }) {
|
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
describe('RabbitMQ integration', () => {
|
|
39
|
-
// @ts-ignore
|
|
40
41
|
let rabbit
|
|
41
|
-
// @ts-ignore
|
|
42
|
-
let unsubscribe
|
|
43
|
-
// @ts-ignore
|
|
44
|
-
let receivedMessages
|
|
45
42
|
|
|
46
43
|
beforeAll(async () => {
|
|
47
44
|
startRabbit({
|
|
@@ -66,11 +63,6 @@ describe('RabbitMQ integration', () => {
|
|
|
66
63
|
|
|
67
64
|
afterAll(async () => {
|
|
68
65
|
try {
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
if (unsubscribe) {
|
|
71
|
-
await unsubscribe()
|
|
72
|
-
}
|
|
73
|
-
// @ts-ignore
|
|
74
66
|
if (rabbit) {
|
|
75
67
|
await rabbit.close()
|
|
76
68
|
}
|
|
@@ -80,42 +72,81 @@ describe('RabbitMQ integration', () => {
|
|
|
80
72
|
})
|
|
81
73
|
|
|
82
74
|
it('should publish, consume, unsubscribe, and stop consuming', async () => {
|
|
83
|
-
|
|
75
|
+
const queue = uniqueQueue('integration')
|
|
76
|
+
const receivedMessages = []
|
|
84
77
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
queue: QUEUE,
|
|
88
|
-
// @ts-ignore
|
|
78
|
+
const unsubscribe = await rabbit.subscribe({
|
|
79
|
+
queue,
|
|
89
80
|
onReceive: async (data) => {
|
|
90
81
|
receivedMessages.push(data)
|
|
91
82
|
},
|
|
92
83
|
})
|
|
93
84
|
|
|
94
|
-
|
|
95
|
-
await rabbit.publish(QUEUE, { step: 1 })
|
|
85
|
+
await rabbit.publish(queue, { step: 1 })
|
|
96
86
|
await waitFor(() => receivedMessages.length === 1)
|
|
97
87
|
|
|
98
|
-
// @ts-ignore
|
|
99
88
|
expect(receivedMessages).toEqual([{ step: 1 }])
|
|
100
89
|
|
|
101
90
|
await unsubscribe()
|
|
102
91
|
|
|
103
|
-
|
|
104
|
-
await rabbit.publish(QUEUE, { step: 2 })
|
|
92
|
+
await rabbit.publish(queue, { step: 2 })
|
|
105
93
|
|
|
106
|
-
await sleep(
|
|
94
|
+
await sleep(500)
|
|
107
95
|
|
|
108
|
-
// @ts-ignore
|
|
109
96
|
expect(receivedMessages).toEqual([{ step: 1 }])
|
|
110
97
|
})
|
|
98
|
+
|
|
99
|
+
describe('RabbitMQ queue name validation', () => {
|
|
100
|
+
it('should throw when subscribing with invalid queues', async () => {
|
|
101
|
+
const cases = [undefined, '', ' ']
|
|
102
|
+
|
|
103
|
+
for (const queue of cases) {
|
|
104
|
+
await expect(
|
|
105
|
+
rabbit.subscribe({
|
|
106
|
+
queue,
|
|
107
|
+
onReceive: async () => {},
|
|
108
|
+
}),
|
|
109
|
+
).rejects.toThrow('Invalid queue name')
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('should throw when publishing with invalid queues', async () => {
|
|
114
|
+
const cases = [undefined, '', ' ']
|
|
115
|
+
|
|
116
|
+
for (const queue of cases) {
|
|
117
|
+
await expect(rabbit.publish(queue, { test: true })).rejects.toThrow(
|
|
118
|
+
'Invalid queue name',
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('should not affect valid queue when invalid publish is attempted', async () => {
|
|
124
|
+
const queue = uniqueQueue('validation')
|
|
125
|
+
const messages = []
|
|
126
|
+
|
|
127
|
+
const unsub = await rabbit.subscribe({
|
|
128
|
+
queue,
|
|
129
|
+
onReceive: async (data) => {
|
|
130
|
+
messages.push(data)
|
|
131
|
+
},
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
await rabbit.publish(queue, { ok: true })
|
|
135
|
+
await waitFor(() => messages.length === 1)
|
|
136
|
+
|
|
137
|
+
expect(messages).toEqual([{ ok: true }])
|
|
138
|
+
|
|
139
|
+
await expect(rabbit.publish(undefined, { bad: true })).rejects.toThrow()
|
|
140
|
+
|
|
141
|
+
await sleep(300)
|
|
142
|
+
|
|
143
|
+
expect(messages).toEqual([{ ok: true }])
|
|
144
|
+
|
|
145
|
+
await unsub()
|
|
146
|
+
})
|
|
147
|
+
})
|
|
111
148
|
})
|
|
112
149
|
|
|
113
|
-
/**
|
|
114
|
-
* Waits until a condition becomes true or times out.
|
|
115
|
-
*
|
|
116
|
-
* @param {() => boolean} predicate
|
|
117
|
-
* @param {number} timeoutMs
|
|
118
|
-
*/
|
|
119
150
|
async function waitFor(predicate, timeoutMs = 5000) {
|
|
120
151
|
const start = Date.now()
|
|
121
152
|
|
|
@@ -129,11 +160,6 @@ async function waitFor(predicate, timeoutMs = 5000) {
|
|
|
129
160
|
throw new Error('Condition not met within timeout')
|
|
130
161
|
}
|
|
131
162
|
|
|
132
|
-
/**
|
|
133
|
-
* Sleeps for the given number of milliseconds.
|
|
134
|
-
*
|
|
135
|
-
* @param {number} ms
|
|
136
|
-
*/
|
|
137
163
|
function sleep(ms) {
|
|
138
164
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
139
165
|
}
|
|
@@ -15,7 +15,7 @@ export function startMongo(port = 27027, containerName = 'mongo-test') {
|
|
|
15
15
|
stdio: 'inherit',
|
|
16
16
|
})
|
|
17
17
|
|
|
18
|
-
waitForMongo(port)
|
|
18
|
+
waitForMongo(port, containerName)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -40,7 +40,7 @@ export function startMongoReplicaSet(
|
|
|
40
40
|
{ stdio: 'inherit' },
|
|
41
41
|
)
|
|
42
42
|
|
|
43
|
-
waitForMongo(port)
|
|
43
|
+
waitForMongo(port, containerName)
|
|
44
44
|
|
|
45
45
|
// Initialize replica set
|
|
46
46
|
console.log(`[MongoTest] Initializing replica set "${replSet}"...`)
|
|
@@ -60,15 +60,16 @@ export function startMongoReplicaSet(
|
|
|
60
60
|
export function stopMongo(containerName = 'mongo-test') {
|
|
61
61
|
console.log(`[MongoTest] Stopping MongoDB...`)
|
|
62
62
|
try {
|
|
63
|
-
execSync(`docker rm -
|
|
63
|
+
execSync(`docker rm -fv ${containerName}`, { stdio: 'ignore' })
|
|
64
64
|
} catch {}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
function isConnected(port) {
|
|
67
|
+
function isConnected(port, containerName) {
|
|
68
68
|
try {
|
|
69
|
-
execSync(
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
execSync(
|
|
70
|
+
`docker exec ${containerName} mongosh --port 27017 --eval "db.runCommand({ ping: 1 })"`,
|
|
71
|
+
{ stdio: 'ignore' },
|
|
72
|
+
)
|
|
72
73
|
return true
|
|
73
74
|
} catch {
|
|
74
75
|
return false
|
|
@@ -78,14 +79,14 @@ function isConnected(port) {
|
|
|
78
79
|
* Wait until MongoDB is ready to accept connections
|
|
79
80
|
* @param {number} port
|
|
80
81
|
*/
|
|
81
|
-
function waitForMongo(port) {
|
|
82
|
+
function waitForMongo(port, containerName) {
|
|
82
83
|
console.log(`[MongoTest] Waiting for MongoDB to be ready...`)
|
|
83
84
|
const maxRetries = 60
|
|
84
85
|
let retries = 0
|
|
85
86
|
let connected = false
|
|
86
87
|
|
|
87
88
|
while (!connected && retries < maxRetries) {
|
|
88
|
-
connected = isConnected(port)
|
|
89
|
+
connected = isConnected(port, containerName)
|
|
89
90
|
if (!connected) {
|
|
90
91
|
retries++
|
|
91
92
|
execSync(`sleep 1`)
|