nats-micro
Advanced tools
Comparing version 0.20.0 to 0.21.0
@@ -5,4 +5,4 @@ "use strict"; | ||
exports.localConfig = { | ||
version: '0.20.0', | ||
version: '0.21.0', | ||
}; | ||
//# sourceMappingURL=localConfig.js.map |
@@ -31,6 +31,8 @@ "use strict"; | ||
class Discovery { | ||
constructor(broker, configOrGetter) { | ||
constructor(broker, configOrGetter, options = {}) { | ||
this.broker = broker; | ||
this.configOrGetter = configOrGetter; | ||
this.options = options; | ||
this.methodStats = {}; | ||
this.lateAddedMethods = {}; | ||
this.startedAt = new Date(); | ||
@@ -43,3 +45,3 @@ this.id = (0, utils_js_1.randomId)(); | ||
} | ||
get config() { | ||
get originalConfig() { | ||
if (typeof (this.configOrGetter) === 'function') | ||
@@ -49,2 +51,9 @@ return this.configOrGetter(); | ||
} | ||
get config() { | ||
let config = this.originalConfig; | ||
if (this.options.transformConfig) | ||
config = this.options.transformConfig(config); | ||
config.methods = Object.assign(Object.assign({}, config.methods), this.lateAddedMethods); | ||
return config; | ||
} | ||
start() { | ||
@@ -95,3 +104,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
addMethod(name, method) { | ||
this.config.methods[name] = method; | ||
this.lateAddedMethods[name] = method; | ||
} | ||
@@ -98,0 +107,0 @@ profileMethod(name, error, time) { |
@@ -22,3 +22,5 @@ "use strict"; | ||
this.startedMethods = {}; | ||
this.discovery = new discovery_js_1.Discovery(broker, config); | ||
this.discovery = new discovery_js_1.Discovery(broker, config, { | ||
transformConfig: this.transformConfig.bind(this), | ||
}); | ||
} | ||
@@ -51,2 +53,13 @@ static create(broker, config) { | ||
} | ||
transformConfig(config) { | ||
return Object.assign(Object.assign({}, config), { methods: Object.assign(Object.assign({}, config.methods), { microservice_stop: { | ||
handler: this.handleStop.bind(this), | ||
metadata: { | ||
'nats.micro.ext.v1.feature': 'microservice_stop', | ||
'nats.micro.ext.v1.feature.params': `{"name":"${config.name}","id":"${this.id}"}`, | ||
}, | ||
unbalanced: true, | ||
local: true, | ||
} }) }); | ||
} | ||
startMethod(name, method) { | ||
@@ -76,2 +89,7 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
handleStop() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.stop(); | ||
}); | ||
} | ||
stop() { | ||
@@ -78,0 +96,0 @@ return __awaiter(this, void 0, void 0, function* () { |
export const localConfig = { | ||
version: '0.20.0', | ||
version: '0.21.0', | ||
}; | ||
//# sourceMappingURL=localConfig.js.map |
@@ -25,6 +25,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
export class Discovery { | ||
constructor(broker, configOrGetter) { | ||
constructor(broker, configOrGetter, options = {}) { | ||
this.broker = broker; | ||
this.configOrGetter = configOrGetter; | ||
this.options = options; | ||
this.methodStats = {}; | ||
this.lateAddedMethods = {}; | ||
this.startedAt = new Date(); | ||
@@ -37,3 +39,3 @@ this.id = randomId(); | ||
} | ||
get config() { | ||
get originalConfig() { | ||
if (typeof (this.configOrGetter) === 'function') | ||
@@ -43,2 +45,9 @@ return this.configOrGetter(); | ||
} | ||
get config() { | ||
let config = this.originalConfig; | ||
if (this.options.transformConfig) | ||
config = this.options.transformConfig(config); | ||
config.methods = Object.assign(Object.assign({}, config.methods), this.lateAddedMethods); | ||
return config; | ||
} | ||
start() { | ||
@@ -89,3 +98,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
addMethod(name, method) { | ||
this.config.methods[name] = method; | ||
this.lateAddedMethods[name] = method; | ||
} | ||
@@ -92,0 +101,0 @@ profileMethod(name, error, time) { |
@@ -19,3 +19,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
this.startedMethods = {}; | ||
this.discovery = new Discovery(broker, config); | ||
this.discovery = new Discovery(broker, config, { | ||
transformConfig: this.transformConfig.bind(this), | ||
}); | ||
} | ||
@@ -48,2 +50,13 @@ static create(broker, config) { | ||
} | ||
transformConfig(config) { | ||
return Object.assign(Object.assign({}, config), { methods: Object.assign(Object.assign({}, config.methods), { microservice_stop: { | ||
handler: this.handleStop.bind(this), | ||
metadata: { | ||
'nats.micro.ext.v1.feature': 'microservice_stop', | ||
'nats.micro.ext.v1.feature.params': `{"name":"${config.name}","id":"${this.id}"}`, | ||
}, | ||
unbalanced: true, | ||
local: true, | ||
} }) }); | ||
} | ||
startMethod(name, method) { | ||
@@ -73,2 +86,7 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
handleStop() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.stop(); | ||
}); | ||
} | ||
stop() { | ||
@@ -75,0 +93,0 @@ return __awaiter(this, void 0, void 0, function* () { |
import { Broker } from '../broker.js'; | ||
import { MethodProfile, MicroserviceConfig, MicroserviceMethodConfig } from '../types/index.js'; | ||
export type DiscoveryOptions = { | ||
transformConfig?: (config: MicroserviceConfig) => MicroserviceConfig; | ||
}; | ||
export declare class Discovery { | ||
private readonly broker; | ||
readonly configOrGetter: MicroserviceConfig | (() => MicroserviceConfig); | ||
private readonly options; | ||
readonly id: string; | ||
@@ -13,3 +17,5 @@ readonly startedAt: Date; | ||
private readonly handleStatsWrap; | ||
constructor(broker: Broker, configOrGetter: MicroserviceConfig | (() => MicroserviceConfig)); | ||
private readonly lateAddedMethods; | ||
constructor(broker: Broker, configOrGetter: MicroserviceConfig | (() => MicroserviceConfig), options?: DiscoveryOptions); | ||
get originalConfig(): MicroserviceConfig; | ||
get config(): MicroserviceConfig; | ||
@@ -16,0 +22,0 @@ start(): Promise<this>; |
@@ -13,5 +13,7 @@ import { Discovery } from './discovery.js'; | ||
get config(): Readonly<MicroserviceConfig>; | ||
private transformConfig; | ||
private startMethod; | ||
private stopMethod; | ||
start(): Promise<this>; | ||
private handleStop; | ||
stop(): Promise<this>; | ||
@@ -18,0 +20,0 @@ addMethod<R, T>(name: string, method: MicroserviceMethodConfig<R, T>): this; |
{ | ||
"name": "nats-micro", | ||
"version": "0.20.0", | ||
"version": "0.21.0", | ||
"description": "NATS micro compatible extra-lightweight microservice library", | ||
@@ -5,0 +5,0 @@ "main": "lib/esm/index.js", |
export const localConfig = { | ||
version: '0.20.0', | ||
version: '0.21.0', | ||
}; |
@@ -25,2 +25,6 @@ import moment from 'moment'; | ||
export type DiscoveryOptions = { | ||
transformConfig?: (config: MicroserviceConfig) => MicroserviceConfig; | ||
}; | ||
export class Discovery { | ||
@@ -36,5 +40,8 @@ | ||
private readonly lateAddedMethods: MicroserviceConfig['methods'] = {}; | ||
constructor( | ||
private readonly broker: Broker, | ||
public readonly configOrGetter: MicroserviceConfig | (() => MicroserviceConfig), | ||
private readonly options: DiscoveryOptions = {}, | ||
) { | ||
@@ -50,3 +57,3 @@ this.startedAt = new Date(); | ||
public get config(): MicroserviceConfig { | ||
public get originalConfig(): MicroserviceConfig { | ||
if (typeof (this.configOrGetter) === 'function') | ||
@@ -58,2 +65,15 @@ return this.configOrGetter(); | ||
public get config(): MicroserviceConfig { | ||
let config = this.originalConfig; | ||
if (this.options.transformConfig) | ||
config = this.options.transformConfig(config); | ||
config.methods = { | ||
...config.methods, | ||
...this.lateAddedMethods, | ||
}; | ||
return config; | ||
} | ||
public async start(): Promise<this> { | ||
@@ -119,3 +139,3 @@ | ||
): void { | ||
this.config.methods[name] = method; | ||
this.lateAddedMethods[name] = method; | ||
} | ||
@@ -122,0 +142,0 @@ |
@@ -24,3 +24,9 @@ import { threadContext } from 'debug-threads-ns'; | ||
) { | ||
this.discovery = new Discovery(broker, config); | ||
this.discovery = new Discovery( | ||
broker, | ||
config, | ||
{ | ||
transformConfig: this.transformConfig.bind(this), | ||
}, | ||
); | ||
} | ||
@@ -64,2 +70,21 @@ | ||
private transformConfig(config: MicroserviceConfig): MicroserviceConfig { | ||
return { | ||
...config, | ||
methods: { | ||
...config.methods, | ||
microservice_stop: { | ||
handler: this.handleStop.bind(this), | ||
metadata: { | ||
'nats.micro.ext.v1.feature': 'microservice_stop', | ||
'nats.micro.ext.v1.feature.params': `{"name":"${config.name}","id":"${this.id}"}`, | ||
}, | ||
unbalanced: true, | ||
local: true, | ||
}, | ||
}, | ||
}; | ||
} | ||
private async startMethod<R, T>( | ||
@@ -114,2 +139,6 @@ name: string, | ||
private async handleStop(): Promise<void> { | ||
await this.stop(); | ||
} | ||
public async stop(): Promise<this> { | ||
@@ -116,0 +145,0 @@ |
@@ -57,3 +57,4 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
expect(spyOn.callCount).to.eq(12); | ||
expect(spyOn.callCount).to.greaterThanOrEqual(12); | ||
for (const schema of ['SCHEMA', 'INFO', 'PING', 'STATS']) { | ||
@@ -82,8 +83,3 @@ expect(spyOn.calledWith(`$SRV.${schema}`)).to.be.true; | ||
expect(spyOn.callCount).to.eq(12); | ||
for (const schema of ['SCHEMA', 'INFO', 'PING', 'STATS']) { | ||
expect(spyOn.calledWith(`$SRV.${schema}`)).to.be.true; | ||
expect(spyOn.calledWith(`$SRV.${schema}.${service.config.name}`)).to.be.true; | ||
expect(spyOn.calledWith(`$SRV.${schema}.${service.config.name}.${service.id}`)).to.be.true; | ||
} | ||
expect(service).to.exist; | ||
}); | ||
@@ -135,2 +131,26 @@ | ||
it('stop method', async function () { | ||
const service = await createService(); | ||
expect(spyOn.calledWith(`hello.${service.id}.microservice_stop`)).to.be.true; | ||
const info: MicroserviceInfo | undefined = await broker.request('$SRV.INFO', ''); | ||
expect(info).to.exist; | ||
expect(info).to.containSubset({ | ||
endpoints: [ | ||
{ | ||
name: 'microservice_stop', | ||
metadata: { | ||
'nats.micro.ext.v1.feature': 'microservice_stop', | ||
'nats.micro.ext.v1.feature.params': JSON.stringify({ name: 'hello', id: service.id }), | ||
'nats.micro.ext.v1.method.local': 'true', | ||
'nats.micro.ext.v1.method.unbalanced': 'true', | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
it('automatic global method subject', async function () { | ||
@@ -143,3 +163,3 @@ | ||
expect(info).to.exist; | ||
expect(info!.endpoints).to.have.deep.members([{ | ||
expect(info!.endpoints).to.include.deep.members([{ | ||
name: 'method1', | ||
@@ -160,3 +180,3 @@ subject: 'hello.method1', | ||
expect(info).to.exist; | ||
expect(info!.endpoints).to.have.deep.members([{ | ||
expect(info!.endpoints).to.include.deep.members([{ | ||
name: 'method1', | ||
@@ -179,3 +199,3 @@ subject: `hello.${service.id}.method1`, | ||
expect(info).to.exist; | ||
expect(info!.endpoints).to.have.deep.members([{ | ||
expect(info!.endpoints).to.include.deep.members([{ | ||
name: 'method1', | ||
@@ -196,3 +216,3 @@ subject: 'testSubject', | ||
expect(info).to.exist; | ||
expect(info!.endpoints).to.have.deep.members([{ | ||
expect(info!.endpoints).to.include.deep.members([{ | ||
name: 'method1', | ||
@@ -199,0 +219,0 @@ subject: 'hello.method1', |
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
284997
5015
37
0
0
0
13