nats-micro
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -29,4 +29,4 @@ "use strict"; | ||
const nats = __importStar(require("nats")); | ||
const debug_1 = require("./debug"); | ||
const localConfig_1 = require("./localConfig"); | ||
const log_1 = require("./log"); | ||
const utils_1 = require("./utils"); | ||
@@ -42,3 +42,3 @@ class Broker { | ||
async connect() { | ||
log_1.log.info(`loc[[BROKER]] Connecting to NATS at loc[[${localConfig_1.localConfig.nats.serverUrl}]]`); | ||
debug_1.debug.broker.info(`Connecting to server at ${localConfig_1.localConfig.nats.serverUrl}`); | ||
try { | ||
@@ -50,7 +50,7 @@ this.connection = await nats.connect({ | ||
this.connectionClosedWaiter = this.connection.closed(); | ||
log_1.log.info('loc[[BROKER]] Connected to NATS'); | ||
debug_1.debug.broker.info('Connected to server'); | ||
return this; | ||
} | ||
catch (err) { | ||
log_1.log.error(`loc[[BROKER]] Error connecting to NATS: ${err.toString()}`); | ||
debug_1.debug.broker.error(`Error connecting to server: ${err.toString()}`); | ||
throw err; | ||
@@ -60,3 +60,3 @@ } | ||
async disconnect() { | ||
log_1.log.info('loc[[BROKER]] Disconnecting from NATS'); | ||
debug_1.debug.broker.info('Disconnecting from server'); | ||
try { | ||
@@ -67,6 +67,6 @@ await this.connection.close(); | ||
throw err; | ||
log_1.log.info('loc[[BROKER]] Disconnected from NATS'); | ||
debug_1.debug.broker.info('Disconnected from server'); | ||
} | ||
catch (err) { | ||
log_1.log.error(`loc[[BROKER]] Error disconnecting from NATS: ${err.toString()}`); | ||
debug_1.debug.broker.error(`Error disconnecting from server: ${err.toString()}`); | ||
throw err; | ||
@@ -83,5 +83,6 @@ } | ||
if (err) { | ||
log_1.log.error(`loc[[BROKER]] Error in message on loc[[${msg.subject}]]: data[[${err}]]`); | ||
debug_1.debug.broker.error(`Incoming error in message on "${msg.subject}": ${JSON.stringify(err)}`); | ||
} | ||
else { | ||
debug_1.debug.broker.debug(`Incoming message on "${msg.subject}": ${JSON.stringify(msg.string())}`); | ||
try { | ||
@@ -102,3 +103,3 @@ this.ee.emit(msg.subject, { | ||
} | ||
log_1.log.error(`loc[[BROKER]] Error decoding message on loc[[${msg.subject}]] data[["${content}"]]`); | ||
debug_1.debug.broker.error(`Error decoding JSON from "${content}"`); | ||
} | ||
@@ -108,3 +109,3 @@ } | ||
async subscribe(subject) { | ||
log_1.log.debug(`loc[[BROKER]] Subscribing to data[[${subject}]]`); | ||
debug_1.debug.broker.debug(`Subscribing to "${subject}"`); | ||
this.connection.subscribe(subject, { | ||
@@ -111,0 +112,0 @@ callback: this.handleMessageFromSubscription.bind(this), |
@@ -29,6 +29,6 @@ "use strict"; | ||
async start() { | ||
const handleSchema = (0, utils_1.wrapMethod)(this.broker, this.handleSchema.bind(this)); | ||
const handleInfo = (0, utils_1.wrapMethod)(this.broker, this.handleInfo.bind(this)); | ||
const handlePing = (0, utils_1.wrapMethod)(this.broker, this.handlePing.bind(this)); | ||
const handleStats = (0, utils_1.wrapMethod)(this.broker, this.handleStats.bind(this)); | ||
const handleSchema = (0, utils_1.wrapMethod)(this.broker, this.id, 'handleSchema', this.handleSchema.bind(this)); | ||
const handleInfo = (0, utils_1.wrapMethod)(this.broker, this.id, 'handleInfo', this.handleInfo.bind(this)); | ||
const handlePing = (0, utils_1.wrapMethod)(this.broker, this.id, 'handlePing', this.handlePing.bind(this)); | ||
const handleStats = (0, utils_1.wrapMethod)(this.broker, this.id, 'handleStats', this.handleStats.bind(this)); | ||
this.broker.on('$SRV.SCHEMA', handleSchema); | ||
@@ -35,0 +35,0 @@ this.broker.on(`$SRV.SCHEMA.${this.config.name}`, handleSchema); |
@@ -5,3 +5,3 @@ "use strict"; | ||
exports.localConfig = { | ||
version: '0.6.0', | ||
version: '0.7.0', | ||
nats: { | ||
@@ -8,0 +8,0 @@ serverUrl: process.env.NATS_URI || 'nats://localhost:4222', |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Microservice = void 0; | ||
const debug_threads_ns_1 = require("debug-threads-ns"); | ||
const debug_1 = require("./debug"); | ||
const storage_1 = require("./decorators/storage"); | ||
@@ -29,4 +31,7 @@ const discovery_1 = require("./discovery"); | ||
async start() { | ||
debug_threads_ns_1.threadContext.init(this.discovery.id); | ||
const cfg = this.discovery.config; | ||
debug_1.debug.ms.thread.info(`Registering microservice ${cfg.name}(${Object.keys(cfg.methods).join(',')})`); | ||
await this.discovery.start(); | ||
for (const [name, method] of Object.entries(this.discovery.config.methods)) | ||
for (const [name, method] of Object.entries(cfg.methods)) | ||
this.startMethod(name, method); | ||
@@ -33,0 +38,0 @@ return this; |
import { MaybePromise, MessageMaybeReplyTo, MicroserviceMethodConfig, Sender } from './types'; | ||
export declare function randomId(): string; | ||
export declare function camelCase(s: string): string; | ||
export declare function wrapMethod<T, R>(broker: Sender, callback: (args: T) => MaybePromise<R>): (msg: MessageMaybeReplyTo<T>) => void; | ||
export declare function wrapMethod<T, R>(broker: Sender, id: string, methodName: string, callback: (args: T) => MaybePromise<R>): (msg: MessageMaybeReplyTo<T>) => void; | ||
export declare function wrapMethodSafe<T, R>(broker: Sender, callback: (args: T) => MaybePromise<R>, method: MicroserviceMethodConfig<T, R>): (msg: MessageMaybeReplyTo<T>) => void; |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.wrapMethodSafe = exports.wrapMethod = exports.camelCase = exports.randomId = void 0; | ||
const debug_threads_ns_1 = require("debug-threads-ns"); | ||
const errio_1 = __importDefault(require("errio")); | ||
@@ -12,3 +13,3 @@ const nanoid_esm_1 = __importDefault(require("nanoid-esm")); | ||
const zod_1 = require("zod"); | ||
const log_1 = require("./log"); | ||
const debug_1 = require("./debug"); | ||
function randomId() { | ||
@@ -22,4 +23,6 @@ return (0, nanoid_esm_1.default)(16); | ||
exports.camelCase = camelCase; | ||
function wrapMethod(broker, callback) { | ||
function wrapMethod(broker, id, methodName, callback) { | ||
return async (msg) => { | ||
debug_threads_ns_1.threadContext.init(id); | ||
debug_1.debug.ms.thread.debug(`Executing ${methodName}(${JSON.stringify(msg.data)})`); | ||
const output = await callback(msg.data); | ||
@@ -65,3 +68,3 @@ if (!(0, util_1.isUndefined)(output) && 'replyTo' in msg && msg.replyTo) { | ||
const error = err.message ?? errio_1.default.stringify(err); | ||
log_1.log.error(error); | ||
debug_1.debug.error(error); | ||
if ('replyTo' in msg && msg.replyTo) { | ||
@@ -68,0 +71,0 @@ broker.send(msg.replyTo, { error }); |
{ | ||
"name": "nats-micro", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "NATS micro compatible extra-lightweight microservice library", | ||
@@ -31,2 +31,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"debug-threads-ns": "^0.2.2", | ||
"errio": "^1.2.2", | ||
@@ -33,0 +34,0 @@ "fast-safe-stringify": "^2.1.1", |
import { EventEmitter } from 'events'; | ||
import * as nats from 'nats'; | ||
import { debug } from './debug'; | ||
import { localConfig } from './localConfig'; | ||
import { log } from './log'; | ||
import { | ||
@@ -25,5 +25,5 @@ ExecOptions, MessageMaybeReplyTo, MethodSubject, SendOptions, | ||
public async connect(): Promise<this> { | ||
log.info(`loc[[BROKER]] Connecting to NATS at loc[[${localConfig.nats.serverUrl}]]`); | ||
debug.broker.info(`Connecting to server at ${localConfig.nats.serverUrl}`); | ||
try { | ||
this.connection = await nats.connect({ | ||
this.connection = await nats.connect({ | ||
name: this.name, | ||
@@ -33,7 +33,7 @@ servers: localConfig.nats.serverUrl, | ||
this.connectionClosedWaiter = this.connection.closed(); | ||
log.info('loc[[BROKER]] Connected to NATS'); | ||
debug.broker.info('Connected to server'); | ||
return this; | ||
} | ||
catch (err) { | ||
log.error(`loc[[BROKER]] Error connecting to NATS: ${err.toString()}`); | ||
debug.broker.error(`Error connecting to server: ${err.toString()}`); | ||
throw err; | ||
@@ -44,3 +44,3 @@ } | ||
public async disconnect(): Promise<void> { | ||
log.info('loc[[BROKER]] Disconnecting from NATS'); | ||
debug.broker.info('Disconnecting from server'); | ||
try { | ||
@@ -51,6 +51,6 @@ await this.connection.close(); | ||
throw err; | ||
log.info('loc[[BROKER]] Disconnected from NATS'); | ||
debug.broker.info('Disconnected from server'); | ||
} | ||
catch (err) { | ||
log.error(`loc[[BROKER]] Error disconnecting from NATS: ${err.toString()}`); | ||
debug.broker.error(`Error disconnecting from server: ${err.toString()}`); | ||
throw err; | ||
@@ -73,5 +73,6 @@ } | ||
if (err) { | ||
log.error(`loc[[BROKER]] Error in message on loc[[${msg.subject}]]: data[[${err}]]`); | ||
debug.broker.error(`Incoming error in message on "${msg.subject}": ${JSON.stringify(err)}`); | ||
} | ||
else { | ||
debug.broker.debug(`Incoming message on "${msg.subject}": ${JSON.stringify(msg.string())}`); | ||
try { | ||
@@ -95,3 +96,3 @@ this.ee.emit( | ||
} | ||
log.error(`loc[[BROKER]] Error decoding message on loc[[${msg.subject}]] data[["${content}"]]`); | ||
debug.broker.error(`Error decoding JSON from "${content}"`); | ||
} | ||
@@ -102,3 +103,3 @@ } | ||
private async subscribe(subject: string): Promise<void> { | ||
log.debug(`loc[[BROKER]] Subscribing to data[[${subject}]]`); | ||
debug.broker.debug(`Subscribing to "${subject}"`); | ||
this.connection.subscribe( | ||
@@ -105,0 +106,0 @@ subject, |
@@ -39,6 +39,6 @@ import moment from 'moment'; | ||
const handleSchema = wrapMethod(this.broker, this.handleSchema.bind(this)); | ||
const handleInfo = wrapMethod(this.broker, this.handleInfo.bind(this)); | ||
const handlePing = wrapMethod(this.broker, this.handlePing.bind(this)); | ||
const handleStats = wrapMethod(this.broker, this.handleStats.bind(this)); | ||
const handleSchema = wrapMethod(this.broker, this.id, 'handleSchema', this.handleSchema.bind(this)); | ||
const handleInfo = wrapMethod(this.broker, this.id, 'handleInfo', this.handleInfo.bind(this)); | ||
const handlePing = wrapMethod(this.broker, this.id, 'handlePing', this.handlePing.bind(this)); | ||
const handleStats = wrapMethod(this.broker, this.id, 'handleStats', this.handleStats.bind(this)); | ||
@@ -45,0 +45,0 @@ this.broker.on('$SRV.SCHEMA', handleSchema); |
export const localConfig = { | ||
version: '0.6.0', | ||
version: '0.7.0', | ||
nats: { | ||
@@ -4,0 +4,0 @@ serverUrl: process.env.NATS_URI || 'nats://localhost:4222', |
@@ -0,2 +1,4 @@ | ||
import { threadContext } from 'debug-threads-ns'; | ||
import { Broker } from './broker'; | ||
import { debug } from './debug'; | ||
import { storage } from './decorators/storage'; | ||
@@ -49,5 +51,11 @@ import { Discovery } from './discovery'; | ||
threadContext.init(this.discovery.id); | ||
const cfg = this.discovery.config; | ||
debug.ms.thread.info(`Registering microservice ${cfg.name}(${Object.keys(cfg.methods).join(',')})`); | ||
await this.discovery.start(); | ||
for (const [name, method] of Object.entries(this.discovery.config.methods)) | ||
for (const [name, method] of Object.entries(cfg.methods)) | ||
this.startMethod(name, method); | ||
@@ -54,0 +62,0 @@ |
@@ -0,1 +1,2 @@ | ||
import { threadContext } from 'debug-threads-ns'; | ||
import errio from 'errio'; | ||
@@ -6,3 +7,3 @@ import nanoid from 'nanoid-esm'; | ||
import { log } from './log'; | ||
import { debug } from './debug'; | ||
import { | ||
@@ -22,2 +23,4 @@ MaybePromise, MessageMaybeReplyTo, MicroserviceMethodConfig, Sender, | ||
broker: Sender, | ||
id: string, | ||
methodName: string, | ||
callback: (args: T) => MaybePromise<R>, | ||
@@ -27,2 +30,6 @@ ): (msg: MessageMaybeReplyTo<T>) => void { | ||
return async (msg) => { | ||
threadContext.init(id); | ||
debug.ms.thread.debug(`Executing ${methodName}(${JSON.stringify(msg.data)})`); | ||
const output: R = await callback(msg.data); | ||
@@ -84,3 +91,3 @@ if (!isUndefined(output) && 'replyTo' in msg && msg.replyTo) { | ||
log.error(error); | ||
debug.error(error); | ||
@@ -87,0 +94,0 @@ if ('replyTo' in msg && msg.replyTo) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
81371
1747
8
+ Addeddebug-threads-ns@^0.2.2
+ Addedansi-styles@4.3.0(transitive)
+ Addedasync-hook-jl@1.7.6(transitive)
+ Addedasyncc@2.0.7(transitive)
+ Addedatomic-sleep@1.0.0(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcls-hooked@4.2.2(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddebug-level@2.1.2(transitive)
+ Addeddebug-threads-ns@0.2.3(transitive)
+ Addedemitter-listener@1.1.2(transitive)
+ Addedflatstr@1.0.12(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedmap-lru@2.1.0(transitive)
+ Addedms@2.1.3(transitive)
+ Addednanoid@3.3.7(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedshimmer@1.2.1(transitive)
+ Addedsonic-boom@2.8.0(transitive)
+ Addedstack-chain@1.3.7(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedthread-context@0.1.1(transitive)