mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
Add option to throw on non-empty RCON responses
Add option to FactorioServer.sendRcon to throw on non-empty responses from Factorio. This is useful for detecting errors returned by commands that should not have a response.
This commit is contained in:
1 parent
f88e1f1d3a
commit
221dc855f4
3 files changed
+17
-3
No files matched your search
@@ -625,15 +625,22 @@ class FactorioServer extends events.EventEmitter {
|
|||||||
* wait until it is establied and then send the message.
|
* wait until it is establied and then send the message.
|
||||||
*
|
*
|
||||||
* @param {string} message - message to send to server over RCON.
|
* @param {string} message - message to send to server over RCON.
|
||||||
|
* @param {boolean} expectEmpty -
|
||||||
|
* if true throw if the response is not empty. Useful for detecting
|
||||||
|
* errors that might have been sent in response.
|
||||||
* @returns {string} response from server.
|
* @returns {string} response from server.
|
||||||
*/
|
*/
|
||||||
async sendRcon(message) {
|
async sendRcon(message, expectEmpty) {
|
||||||
this._check(["running"])
|
this._check(["running"])
|
||||||
if (!this._rconReady) {
|
if (!this._rconReady) {
|
||||||
await events.once(this, 'rcon-ready');
|
await events.once(this, 'rcon-ready');
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this._rconClient.send(message);
|
let response = await this._rconClient.send(message);
|
||||||
|
if (expectEmpty && response !== '') {
|
||||||
|
throw new Error(`Expected empty response but got "${response}"`);
|
||||||
|
}
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class InstancePlugin extends plugin.BaseInstancePlugin {
|
|||||||
async chatEventHandler(message) {
|
async chatEventHandler(message) {
|
||||||
// TODO check if cross server chat is enabled
|
// TODO check if cross server chat is enabled
|
||||||
let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`;
|
let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`;
|
||||||
await this.instance.server.sendRcon(`/sc game.print('${escapeLuaString(content)}')`);
|
await this.instance.server.sendRcon(`/sc game.print('${escapeLuaString(content)}')`, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onOutput(output) {
|
async onOutput(output) {
|
||||||
|
|||||||
@@ -129,6 +129,13 @@ describe("Integration of lib/factorio/server", function() {
|
|||||||
let result = await server.sendRcon("/sc rcon.print('success')");
|
let result = await server.sendRcon("/sc rcon.print('success')");
|
||||||
assert.equal(result, 'success\n');
|
assert.equal(result, 'success\n');
|
||||||
});
|
});
|
||||||
|
it("throws on non-empty response when enabled", async function() {
|
||||||
|
slowTest(this);
|
||||||
|
await assert.rejects(
|
||||||
|
server.sendRcon("/sc rcon.print('fail')", true),
|
||||||
|
new Error('Expected empty response but got "fail\n"')
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(".stop()", function() {
|
describe(".stop()", function() {
|
||||||
|
|||||||
Reference in new issue
Block a user