-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnats.ts
More file actions
36 lines (33 loc) · 889 Bytes
/
Copy pathnats.ts
File metadata and controls
36 lines (33 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as core from '@actions/core'
import { NatsConnection, StringCodec, connect, jwtAuthenticator } from 'nats'
export async function connectToMQ({
urls: natsUrls,
jwt,
nKeySeed
}: {
urls: string
jwt: string
nKeySeed: string
}): Promise<NatsConnection> {
const urls = natsUrls?.split(',') ?? []
const servers = urls.map(s => s.trim())
core.debug(`connecting to:\n${servers.join('\n')}`)
const natsConn = await connect({
servers,
authenticator: jwtAuthenticator(jwt, new TextEncoder().encode(nKeySeed))
})
core.debug(`NATS connected to ${natsConn.info?.server_name}`)
return natsConn
}
/**
* @param {import("nats").NatsConnection} nc
* @param {string} subject
* @param {string} message
*/
export function publishMessage(
nc: NatsConnection,
subject: string,
message: string
): void {
nc.publish(subject, StringCodec().encode(message))
}