Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/api-svc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"js-yaml": "4.1.0",
"json-schema-ref-parser": "9.0.9",
"knex": "3.1.0",
"koa": "3.0.1",
"koa": "3.0.3",
"koa-body": "6.0.1",
"lodash": "4.17.21",
"module-alias": "2.2.3",
Expand Down
41 changes: 27 additions & 14 deletions modules/api-svc/src/ControlAgent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,31 +227,22 @@ class Client extends ws {

// Handle incoming message from the server.
_handle(data) {
// TODO: json-schema validation of received message- should be pretty straight-forward
// and will allow better documentation of the API
let msg;
try {
msg = deserialise(data);
} catch {
this._logger.warn('failed to parse received message: ', { data });
this.send(build.ERROR.NOTIFY.JSON_PARSE_ERROR());
}
this._logger.isDebugEnabled && this._logger.push({ msg }).debug('Handling received message');
const msg = this._deserialiseMessage(data);

// think how to simplify big switch
switch (msg.msg) {
case MESSAGE.CONFIGURATION:
switch (msg.verb) {
case VERB.NOTIFY: {
const dup = cloneJson(this._appConfig); // fast-json-patch explicitly mutates
_.merge(dup, msg.data);
this._logger.isDebugEnabled && this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug(`Emitting new agent configuration [${VERB.NOTIFY}]`);
this.emit(EVENT.RECONFIGURE, dup);
this._reconfigure(dup, VERB.NOTIFY);
break;
}
case VERB.PATCH: {
const dup = cloneJson(this._appConfig); // fast-json-patch explicitly mutates
jsonPatch.applyPatch(dup, msg.data);
this._logger.isDebugEnabled && this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug(`Emitting new agent configuration [${VERB.PATCH}]`);
this.emit(EVENT.RECONFIGURE, dup);
this._reconfigure(dup, VERB.PATCH);
break;
}
default:
Expand All @@ -275,6 +266,28 @@ class Client extends ws {
}

}

_deserialiseMessage (data) {
// TODO: json-schema validation of received message- should be pretty straight-forward
// and will allow better documentation of the API
let msg;
try {
msg = deserialise(data);
} catch {
this._logger.warn('failed to parse received message: ', { data });
this.send(build.ERROR.NOTIFY.JSON_PARSE_ERROR());
}
this._logger.isDebugEnabled && this._logger.push({ msg }).debug('Handling received message');

return msg;
}

_reconfigure(newConf, verb) {
this._logger.info(`Emitting new agent configuration [${verb}]`);
this._logger.debug('oldConf and newConf: ', { oldConf: this._appConfig, newConf });
this.emit(EVENT.RECONFIGURE, newConf);
this._appConfig = newConf; // !!!
}
}

const createConnectedControlAgentWs = async (conf, log) => {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2845,7 +2845,7 @@ __metadata:
js-yaml: "npm:4.1.0"
json-schema-ref-parser: "npm:9.0.9"
knex: "npm:3.1.0"
koa: "npm:3.0.1"
koa: "npm:3.0.3"
koa-body: "npm:6.0.1"
lodash: "npm:4.17.21"
module-alias: "npm:2.2.3"
Expand Down Expand Up @@ -10795,9 +10795,9 @@ __metadata:
languageName: node
linkType: hard

"koa@npm:3.0.1":
version: 3.0.1
resolution: "koa@npm:3.0.1"
"koa@npm:3.0.3":
version: 3.0.3
resolution: "koa@npm:3.0.3"
dependencies:
accepts: "npm:^1.3.8"
content-disposition: "npm:~0.5.4"
Expand All @@ -10817,7 +10817,7 @@ __metadata:
statuses: "npm:^2.0.1"
type-is: "npm:^2.0.1"
vary: "npm:^1.1.2"
checksum: 10/0e56f77f7192c10be6a3f5c4b248ec10b9b223e6894065a431df3c5c425db439fcc28ead29e7145087974550b51538790aafee6aeb5b0e26a32307d02a52bd41
checksum: 10/15df9a7777ad357851253deaba534403dcc97eb81efce263c8a8b30b5bc3e5db7d61e35b7a89ce359ec3cf043b50244996df0efe6f29785adefbc6f691a1eda6
languageName: node
linkType: hard

Expand Down