Add initial NATS code
This commit is contained in:
31
package-lock.json
generated
31
package-lock.json
generated
@@ -13,6 +13,7 @@
|
|||||||
"@aws-sdk/lib-storage": "^3.964.0",
|
"@aws-sdk/lib-storage": "^3.964.0",
|
||||||
"@owlboard/backend-data-contracts": "^0.1.0",
|
"@owlboard/backend-data-contracts": "^0.1.0",
|
||||||
"mongodb": "^7.0.0",
|
"mongodb": "^7.0.0",
|
||||||
|
"nats": "^2.29.3",
|
||||||
"readline": "^1.3.0",
|
"readline": "^1.3.0",
|
||||||
"xxhashjs": "^0.2.2"
|
"xxhashjs": "^0.2.2"
|
||||||
},
|
},
|
||||||
@@ -2405,6 +2406,30 @@
|
|||||||
"node": ">=20.19.0"
|
"node": ">=20.19.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/nats": {
|
||||||
|
"version": "2.29.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/nats/-/nats-2.29.3.tgz",
|
||||||
|
"integrity": "sha512-tOQCRCwC74DgBTk4pWZ9V45sk4d7peoE2njVprMRCBXrhJ5q5cYM7i6W+Uvw2qUrcfOSnuisrX7bEx3b3Wx4QA==",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"dependencies": {
|
||||||
|
"nkeys.js": "1.1.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 14.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/nkeys.js": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/nkeys.js/-/nkeys.js-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-tB/a0shZL5UZWSwsoeyqfTszONTt4k2YS0tuQioMOD180+MbombYVgzDUYHlx+gejYK6rgf08n/2Df99WY0Sxg==",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"dependencies": {
|
||||||
|
"tweetnacl": "1.0.3"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/punycode": {
|
"node_modules/punycode": {
|
||||||
"version": "2.3.1",
|
"version": "2.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
|
||||||
@@ -2542,6 +2567,12 @@
|
|||||||
"fsevents": "~2.3.3"
|
"fsevents": "~2.3.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/tweetnacl": {
|
||||||
|
"version": "1.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz",
|
||||||
|
"integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==",
|
||||||
|
"license": "Unlicense"
|
||||||
|
},
|
||||||
"node_modules/typescript": {
|
"node_modules/typescript": {
|
||||||
"version": "5.9.3",
|
"version": "5.9.3",
|
||||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
"@aws-sdk/lib-storage": "^3.964.0",
|
"@aws-sdk/lib-storage": "^3.964.0",
|
||||||
"@owlboard/backend-data-contracts": "^0.1.0",
|
"@owlboard/backend-data-contracts": "^0.1.0",
|
||||||
"mongodb": "^7.0.0",
|
"mongodb": "^7.0.0",
|
||||||
|
"nats": "^2.29.3",
|
||||||
"readline": "^1.3.0",
|
"readline": "^1.3.0",
|
||||||
"xxhashjs": "^0.2.2"
|
"xxhashjs": "^0.2.2"
|
||||||
},
|
},
|
||||||
|
|||||||
26
src/nats.ts
Normal file
26
src/nats.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { connect, JSONCodec } from "nats";
|
||||||
|
import type { ConnectionOptions, NatsConnection, Payload } from "nats";
|
||||||
|
import { log } from "./logger";
|
||||||
|
|
||||||
|
const jc = JSONCodec();
|
||||||
|
|
||||||
|
async function getNatsConnection(): Promise<NatsConnection> {
|
||||||
|
const serverUrl = process.env.MQ_URL || "nats://localhost:4222";
|
||||||
|
|
||||||
|
const options: ConnectionOptions = {
|
||||||
|
servers: serverUrl,
|
||||||
|
name: `${process.env.HOSTNAME}` || 'local',
|
||||||
|
reconnect: true,
|
||||||
|
maxReconnectAttempts: -1,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (process.env.MQ_USER && process.env.MQ_PASS) {
|
||||||
|
options.user = process.env.MQ_USER;
|
||||||
|
options.pass = process.env.MQ_PASS;
|
||||||
|
log("INFO", "NATS: Using username/password authentication");
|
||||||
|
} else {
|
||||||
|
log("INFO", "NATS: Connecting without authentication");
|
||||||
|
}
|
||||||
|
|
||||||
|
return await connect(options)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user