mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Reorganise packages/lib
Move the majority of the script files into a src directory to be consistent with the organisation of the other packages.
This commit is contained in:
1 parent
bfe119eaa3
commit
bc574ee1cb
48 files changed
+41
-41
No files matched your search
@@ -0,0 +1,85 @@
|
||||
"use strict";
|
||||
|
||||
|
||||
/**
|
||||
* Helper class for implementing the exponential backoff algorithm
|
||||
*
|
||||
* Provides a convenient interface for implementing an exponential backoff
|
||||
* when reconnecting or resending in a distributed network.
|
||||
*
|
||||
* Once instantiated the delay method will return the next interval to wait
|
||||
* for in milliseconds before making another attempt. For each attempt the
|
||||
* delay chosen is a random value between 0 and the current maximum delay
|
||||
* wwhich starts at a value of 2 * `base` and doubles for ever attempt up to
|
||||
* the passed value of `max`. The value for the delay will reset back to
|
||||
* `base` if more time than the value for `reset` has passed between two
|
||||
* invocations of delay.
|
||||
*
|
||||
* @alias module:lib.ExponentialBackoff
|
||||
*/
|
||||
class ExponentialBackoff {
|
||||
/**
|
||||
* Construct ExponentialBackoff helper
|
||||
*
|
||||
* The options passed may also be modified on the instance at runtime.
|
||||
*
|
||||
* @param {object=} options -
|
||||
* Options for this exponential backoff helper
|
||||
* @param {number=} [options.base=1] -
|
||||
* Base delay in seconds. The first delay will on average be this
|
||||
* long, but can be anywhere between 0 and 2 times this value.
|
||||
* @param {number=} [options.max=60] -
|
||||
* Maximum delay in seconds. A delay will not take more than this
|
||||
* value.
|
||||
* @param {number=} [options.reset=2*max] -
|
||||
* Time in seconds between two invocations of delay which will cause
|
||||
* it to start over with `base` instead of doubling.
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
/**
|
||||
* Base delay in seconds. The first delay will on average be this
|
||||
* long, but can be anywhere between 0 and 2 times this value.
|
||||
* @type {number}
|
||||
*/
|
||||
this.base = options.base || 1;
|
||||
/**
|
||||
* Maximum delay in seconds. A delay will not take more than this
|
||||
* value.
|
||||
* @type {number}
|
||||
*/
|
||||
this.max = options.max || 60;
|
||||
/**
|
||||
* Time in seconds between two invocations of delay which will cause
|
||||
* it to start over with `base` instead of doubling.
|
||||
* @type {number}
|
||||
*/
|
||||
this.reset = options.reset || 2 * this.max;
|
||||
|
||||
this._exp = 0;
|
||||
this._lastInvocationTime = Date.now();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the next delay to wait
|
||||
*
|
||||
* Returns the next delay to wait according to the exponential backoff
|
||||
* algorithm. This is a value between 0 and min(2 * options.base *
|
||||
* 2<sup>retries</sup>, optinos.max).
|
||||
*
|
||||
* @returns {number} time in milliseconds to wait.
|
||||
*/
|
||||
delay() {
|
||||
let invocationTime = Date.now();
|
||||
let interval = (invocationTime - this._lastInvocationTime) / 1000;
|
||||
this._lastInvocationTime = invocationTime;
|
||||
|
||||
if (interval > this.reset) {
|
||||
this._exp = 0;
|
||||
}
|
||||
|
||||
this._exp = Math.min(this._exp + 1, Math.log2(this.max));
|
||||
return Math.random() * this.base * 2 ** this._exp * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ExponentialBackoff;
|
||||
@@ -0,0 +1,88 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Stastics collected about a player.
|
||||
* @alias module:lib.PlayerStats
|
||||
*/
|
||||
class PlayerStats {
|
||||
/**
|
||||
* Count of the number of times this player has been seen joining this server.
|
||||
* @type {number}
|
||||
*/
|
||||
joinCount = 0;
|
||||
|
||||
/**
|
||||
* Time in ms this player has been seen online on this instance.
|
||||
* @type {number}
|
||||
*/
|
||||
onlineTimeMs = 0;
|
||||
|
||||
/**
|
||||
* Timestamp the player was last seen joining.
|
||||
* @type {Date=}
|
||||
*/
|
||||
lastJoinAt;
|
||||
|
||||
/**
|
||||
* Timestamp the player was last seen leaving.
|
||||
* @type {Date=}
|
||||
*/
|
||||
lastLeaveAt;
|
||||
|
||||
/**
|
||||
* Reason the player was last seen leaving with.
|
||||
* @type {string=}
|
||||
*/
|
||||
lastLeaveReason;
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
"join_count": { type: "integer" },
|
||||
"online_time_ms": { type: "number" },
|
||||
"last_join_at": { type: "number" },
|
||||
"last_leave_at": { type: "number" },
|
||||
"last_leave_reason": { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
constructor(json = {}) {
|
||||
if (json["join_count"]) {
|
||||
this.joinCount = json["join_count"];
|
||||
}
|
||||
if (json["online_time_ms"]) {
|
||||
this.onlineTimeMs = json["online_time_ms"];
|
||||
}
|
||||
if (json["last_join_at"]) {
|
||||
this.lastJoinAt = new Date(json["last_join_at"]);
|
||||
}
|
||||
if (json["last_leave_at"]) {
|
||||
this.lastLeaveAt = new Date(json["last_leave_at"]);
|
||||
this.lastLeaveReason = json["last_leave_reason"] || "quit";
|
||||
}
|
||||
}
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json);
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
let json = {};
|
||||
if (this.joinCount) {
|
||||
json["join_count"] = this.joinCount;
|
||||
}
|
||||
if (this.onlineTimeMs) {
|
||||
json["online_time_ms"] = this.onlineTimeMs;
|
||||
}
|
||||
if (this.lastJoinAt) {
|
||||
json["last_join_at"] = this.lastJoinAt.getTime();
|
||||
}
|
||||
if (this.lastLeaveAt) {
|
||||
json["last_leave_at"] = this.lastLeaveAt.getTime();
|
||||
json["last_leave_reason"] = this.lastLeaveReason;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PlayerStats;
|
||||
@@ -0,0 +1,104 @@
|
||||
"use strict";
|
||||
|
||||
|
||||
/**
|
||||
* Helper class for rate limiting actions
|
||||
*
|
||||
* Provides a simple interface for implementing actions that are rate
|
||||
* limited on invocations.
|
||||
*
|
||||
* @example
|
||||
* const rateLimiter = RateLimiter({
|
||||
* action: () => { console.log("action"); },
|
||||
* maxRate: 1,
|
||||
* });
|
||||
*
|
||||
* for (let i = 0; i < 100; i++) {
|
||||
* rateLimiter.activate();
|
||||
* // Prints action twice to the console, once for the first activation
|
||||
* // and one more time 1 second later for 99 other activations that were
|
||||
* // exceeding the rate limit.
|
||||
* }
|
||||
* @alias module:lib.RateLimiter
|
||||
*/
|
||||
class RateLimiter {
|
||||
/**
|
||||
* Construct RateLimiter helper
|
||||
*
|
||||
* The options passed may also be modified on the instance at runtime.
|
||||
*
|
||||
* @param {object=} options - Options for this rate limiter
|
||||
* @param {number=} options.maxRate -
|
||||
* Maximum rate of activations per second.
|
||||
* @param {function()=} options.action -
|
||||
* Callback invoked at maste `maxRate` per second on activation.
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
/**
|
||||
* Maximum rate of activations per second.
|
||||
* @type {number}
|
||||
*/
|
||||
this.maxRate = options.maxRate || 1;
|
||||
|
||||
/**
|
||||
* Callback invoked at most `maxRate` per second on activation.
|
||||
* @type {function()}
|
||||
*/
|
||||
this.action = options.action || null;
|
||||
|
||||
this._lastRun = 0;
|
||||
this._runTimeout = null;
|
||||
this.lastProgressBroadcast = Date.now();
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate the rate limited action
|
||||
*
|
||||
* Checks if the invocation of the rate limited action now would fall
|
||||
* within the rate limit. If it is within the limit then the action is
|
||||
* invoked immediatly, otherwise a timeout is registered to invoke the
|
||||
* action at next point in time where it will be within the rate limit.
|
||||
*
|
||||
* @returns {boolean} true if activation was within the rate limit.
|
||||
*/
|
||||
activate() {
|
||||
let now = Date.now();
|
||||
if (now < this._lastRun + 1000 / this.maxRate) {
|
||||
if (!this._runTimeout && this.action !== null) {
|
||||
this._runTimeout = setTimeout(() => {
|
||||
this._runTimeout = null;
|
||||
this._lastRun = Date.now();
|
||||
this.action();
|
||||
}, this._lastRun + 1000 / this.maxRate - now);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// We may end up activating before the timeout does.
|
||||
if (this._runTimeout) {
|
||||
clearTimeout(this._runTimeout);
|
||||
this._runTimeout = null;
|
||||
}
|
||||
|
||||
this._lastRun = now;
|
||||
if (this.action !== null) {
|
||||
this.action();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel pending action
|
||||
*
|
||||
* Cancel pending action invocation. This is necessary when shutting
|
||||
* down the use of the rate limit as a timeout is registered to invoke
|
||||
* the next action when activated faster than the rate limit allows.
|
||||
*/
|
||||
cancel() {
|
||||
if (this._runTimeout) {
|
||||
clearTimeout(this._runTimeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RateLimiter;
|
||||
@@ -0,0 +1,283 @@
|
||||
/**
|
||||
* Command library
|
||||
*
|
||||
* Data types and utilites for commands to clusterioctl's CLI.
|
||||
*
|
||||
* @author Hornwitser
|
||||
* @module lib/command
|
||||
*/
|
||||
"use strict";
|
||||
const libData = require("./data");
|
||||
const libLink = require("./link");
|
||||
const libErrors = require("./errors");
|
||||
|
||||
/**
|
||||
* Represents a command that can be runned by clusterioctl
|
||||
* @static
|
||||
*/
|
||||
class Command {
|
||||
/**
|
||||
* Define an executable command.
|
||||
*
|
||||
* @param {Object} cmd - Command definiton.
|
||||
* @param {Array} cmd.definiton -
|
||||
* Arguments to pass to yargs .command method to define this
|
||||
* command.
|
||||
* @param {function(args, control)} handler -
|
||||
* Async function invoked when the command is executed. Is given
|
||||
* the parsed args and a reference to the {@link
|
||||
* module:ctl/ctl.Control} instance.
|
||||
*/
|
||||
constructor({ definition, handler }) {
|
||||
if (definition[0] instanceof Array) {
|
||||
this.name = definition[0][0].split(" ")[0];
|
||||
this.alias = definition[0].slice(1);
|
||||
} else {
|
||||
this.name = definition[0].split(" ")[0];
|
||||
this.alias = [];
|
||||
}
|
||||
this._definition = definition;
|
||||
this._handler = handler;
|
||||
}
|
||||
|
||||
register(yargs) {
|
||||
yargs.command(...this._definition);
|
||||
}
|
||||
|
||||
async run(args, control) {
|
||||
await this._handler(args, control);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A node in the command tree that can hold commands and sub trees
|
||||
*
|
||||
* Container which can contain {@link module:lib.Command}s and other
|
||||
* CommandTrees. This is used by clusterioctl to hold the full tree of
|
||||
* available commands, you may extend this tree by using a control plugin,
|
||||
* see {@link module:lib.BaseControlPlugin#addCommands}
|
||||
*
|
||||
* @static
|
||||
*/
|
||||
class CommandTree {
|
||||
/**
|
||||
* Define a command containing sub commands.
|
||||
*
|
||||
* @param {Object} cmd - Command definition
|
||||
* @param {string} cmd.name - Name of the command.
|
||||
* @param {Array<string>} cmd.alias - Aliases for this tree.
|
||||
* @param {description} cmd.description -
|
||||
* Descripton to provide for this command tree node.
|
||||
*/
|
||||
constructor({ name, alias, description }) {
|
||||
if (typeof name !== "string") {
|
||||
throw new Error("name must be a string");
|
||||
}
|
||||
|
||||
this.name = name;
|
||||
this.alias = alias || [];
|
||||
this.description = description;
|
||||
this.subCommands = new Map();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a command or command tree to this tree
|
||||
*
|
||||
* @param {module:lib.Command|module:lib.CommandTree} command -
|
||||
* The command to add to this command tree.
|
||||
*/
|
||||
add(command) {
|
||||
if (this.subCommands.has(command.name)) {
|
||||
throw new Error(`Command ${command.name} already exists`);
|
||||
}
|
||||
this.subCommands.set(command.name, command);
|
||||
for (let alias of command.alias) {
|
||||
if (this.subCommands.has(alias)) {
|
||||
throw new Error(`Alias ${alias} for command ${command.name} already exists`);
|
||||
}
|
||||
this.subCommands.set(alias, command);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a command or command tree from this tree
|
||||
*
|
||||
* @param {string} name -
|
||||
* The name of the command or command tree to retrieve.
|
||||
* @return {?module:lib.Command|module:lib.CommandTree}
|
||||
* The command to add to this command tree.
|
||||
*/
|
||||
get(name) {
|
||||
return this.subCommands.get(name) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the registered commands into the given yargs parser
|
||||
*
|
||||
* Traverses the the tree of sub-commands recursively and adds all of
|
||||
* them to the given yargs parser.
|
||||
*
|
||||
* @param {Object} yargs - yargs parser to register with.
|
||||
*/
|
||||
register(yargs) {
|
||||
yargs.command([this.name].concat(this.alias), this.description, (yargs) => {
|
||||
for (let [name, command] of this.subCommands) {
|
||||
// Check if the entry is not an alias.
|
||||
if (name === command.name) {
|
||||
command.register(yargs);
|
||||
}
|
||||
}
|
||||
}, () => {
|
||||
yargs.showHelp();
|
||||
yargs.exit();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Resolve a string into a host ID
|
||||
*
|
||||
* Resolves a string with either an host name or an id into an integer with
|
||||
* the host ID.
|
||||
*
|
||||
* @param {module:lib.Link} client - link to controller to query host on.
|
||||
* @param {string} hostName - string with name or id of host.
|
||||
* @returns {Promise<number>} host ID.
|
||||
* @static
|
||||
*/
|
||||
async function resolveHost(client, hostName) {
|
||||
let hostId;
|
||||
if (/^-?\d+$/.test(hostName)) {
|
||||
hostId = parseInt(hostName, 10);
|
||||
} else {
|
||||
let hosts = await client.sendTo("controller", new libData.HostListRequest());
|
||||
for (let host of hosts) {
|
||||
if (host.name === hostName) {
|
||||
hostId = host.id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hostId === undefined) {
|
||||
throw new libErrors.CommandError(`No host named ${hostName}`);
|
||||
}
|
||||
}
|
||||
|
||||
return hostId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a string to an instance ID
|
||||
*
|
||||
* Resolves a string with either an instance name or an id into an integer
|
||||
* with the instance ID.
|
||||
*
|
||||
* @param {module:lib.Link} client - link to controller to query instance on.
|
||||
* @param {string} instanceName - string with name or id of instance.
|
||||
* @returns {Promise<number>} instance ID.
|
||||
* @static
|
||||
*/
|
||||
async function resolveInstance(client, instanceName) {
|
||||
let instanceId;
|
||||
if (/^-?\d+$/.test(instanceName)) {
|
||||
instanceId = parseInt(instanceName, 10);
|
||||
} else {
|
||||
let instances = await client.sendTo("controller", new libData.InstanceDetailsListRequest());
|
||||
for (let instance of instances) {
|
||||
if (instance.name === instanceName) {
|
||||
instanceId = instance.id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (instanceId === undefined) {
|
||||
throw new libErrors.CommandError(`No instance named ${instanceName}`);
|
||||
}
|
||||
}
|
||||
|
||||
return instanceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a string to a mod pack ID
|
||||
*
|
||||
* Resolevs a string with either a mod pack name or an id into an integer
|
||||
* with the mod pack ID.
|
||||
*
|
||||
* @param {module:lib.Link} client -
|
||||
* link to controller to query mod pack on.
|
||||
* @param {string} modPackName - string with name or id of mod pack.
|
||||
* @returns {Promise<number>} mod pack ID.
|
||||
* @static
|
||||
*/
|
||||
async function resolveModPack(client, modPackName) {
|
||||
let modPackId;
|
||||
if (/^-?\d+/.test(modPackName)) {
|
||||
modPackId = parseInt(modPackName, 10);
|
||||
} else {
|
||||
let modPacks = await client.sendTo("controller", new libData.ModPackListRequest());
|
||||
for (let modPack of modPacks) {
|
||||
if (modPack.name === modPackName) {
|
||||
modPackId = modPack.id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (modPackId === undefined) {
|
||||
throw new libErrors.CommandError(`No mod pack named ${modPackName}`);
|
||||
}
|
||||
}
|
||||
|
||||
return modPackId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve role object from string
|
||||
*
|
||||
* Resolves a string with either a role name or an id into an object
|
||||
* representing the role.
|
||||
*
|
||||
* @param {module:lib.Link} client - link to controller to query role on.
|
||||
* @param {string} roleName - string with name or id of role.
|
||||
* @returns {Promise<object>} Role info.
|
||||
* @static
|
||||
*/
|
||||
async function retrieveRole(client, roleName) {
|
||||
let roles = await client.sendTo("controller", new libData.RoleListRequest());
|
||||
|
||||
let resolvedRole;
|
||||
if (/^-?\d+$/.test(roleName)) {
|
||||
let roleId = parseInt(roleName, 10);
|
||||
for (let role of roles) {
|
||||
if (role.id === roleId) {
|
||||
resolvedRole = role;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
for (let role of roles) {
|
||||
if (role.name === roleName) {
|
||||
resolvedRole = role;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!resolvedRole) {
|
||||
throw new libErrors.CommandError(`No role named ${roleName}`);
|
||||
}
|
||||
|
||||
return resolvedRole;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Command,
|
||||
CommandTree,
|
||||
|
||||
resolveHost,
|
||||
resolveInstance,
|
||||
resolveModPack,
|
||||
retrieveRole,
|
||||
};
|
||||
@@ -0,0 +1,859 @@
|
||||
// Configuration classes
|
||||
"use strict";
|
||||
|
||||
const events = require("events");
|
||||
|
||||
const isDeepStrictEqual = require("../is_deep_strict_equal");
|
||||
const { basicType } = require("../helpers");
|
||||
|
||||
|
||||
/**
|
||||
* Invalid Access exception
|
||||
*
|
||||
* Exception class for when an access attempt is made to a field that
|
||||
* does not permitt this access is done.
|
||||
*
|
||||
* @extends Error
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class InvalidAccess extends Error { };
|
||||
|
||||
/**
|
||||
* Invalid Value exception
|
||||
*
|
||||
* Exception class for when invalid values are attempted to be set on a
|
||||
* field.
|
||||
*
|
||||
* @extends Error
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class InvalidValue extends Error { };
|
||||
|
||||
/**
|
||||
* Invalid Field exception
|
||||
*
|
||||
* Exception class for when a group or field that does not exist is
|
||||
* attempted to be accessed.
|
||||
*
|
||||
* @extends Error
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class InvalidField extends Error { };
|
||||
|
||||
/**
|
||||
* Split the given string on the first instance of separator
|
||||
*
|
||||
* Splits `string` on the first instance of `separator` and returns an
|
||||
* array consisting of the string up to the separator and the string
|
||||
* after the separator. Returns an array with the string and an empty
|
||||
* string if the separator is not present.
|
||||
*
|
||||
* @param {string} separator - Separator to split string by.
|
||||
* @param {string} string - String to split
|
||||
* @returns {Array} string split on separator.
|
||||
* @memberof module:lib
|
||||
* @private
|
||||
* @inner
|
||||
*/
|
||||
function splitOn(separator, string) {
|
||||
let index = string.indexOf(separator);
|
||||
if (index === -1) {
|
||||
return [string, ""];
|
||||
}
|
||||
return [string.slice(0, index), string.slice(index + separator.length)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Collection of config groups
|
||||
*
|
||||
* Represents a collection of ConfigGroup instances that can be
|
||||
* managed as one.
|
||||
*
|
||||
* This is an EventEmitter with the following events:
|
||||
* - fieldChanged -
|
||||
* invoked after a field has been updated in one of the config groups
|
||||
* belonging to this config. Takes three parameters: group, field,
|
||||
* prev.
|
||||
*
|
||||
* @extends events.EventEmitter
|
||||
*
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class Config extends events.EventEmitter {
|
||||
/**
|
||||
* Create a new instance of the given config
|
||||
*
|
||||
* @param {string} location -
|
||||
* Location to evaluate access for this instance from
|
||||
*/
|
||||
constructor(location) {
|
||||
super();
|
||||
|
||||
if (!this.constructor._finalized) {
|
||||
throw new Error(`Cannot instantiate incomplete Config class ${this.constructor.name}`);
|
||||
}
|
||||
|
||||
if (typeof location !== "string") {
|
||||
throw new Error("location must be a string");
|
||||
}
|
||||
|
||||
this.location = location;
|
||||
this._groups = new Map();
|
||||
this._unknownGroups = new Map();
|
||||
}
|
||||
|
||||
async init() {
|
||||
for (let [name, GroupClass] of this.constructor._groups) {
|
||||
if (!this._groups.has(name)) {
|
||||
let group = new GroupClass(this);
|
||||
await group.init();
|
||||
this._groups.set(name, group);
|
||||
}
|
||||
}
|
||||
this._initialized = true;
|
||||
}
|
||||
|
||||
_validate(serializedConfig) {
|
||||
if (basicType(serializedConfig) !== "object") {
|
||||
throw new Error(`Expected object, not ${basicType(serializedConfig)} for config`);
|
||||
}
|
||||
|
||||
if (basicType(serializedConfig.groups) !== "array") {
|
||||
throw new Error(`Expected groups to be an array, not ${basicType(serializedConfig.groups)}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load config from a serialized object
|
||||
*
|
||||
* @param {object} serializedConfig - Serialized config to load.
|
||||
* @param {string=} location - Location used for access control.
|
||||
*/
|
||||
async load(serializedConfig, location = this.location) {
|
||||
this._validate(serializedConfig);
|
||||
for (let serializedGroup of serializedConfig.groups) {
|
||||
let GroupClass = this.constructor._groups.get(serializedGroup.name);
|
||||
if (!GroupClass) {
|
||||
this._unknownGroups.set(serializedGroup.name, serializedGroup);
|
||||
continue;
|
||||
}
|
||||
|
||||
let group = new GroupClass(this);
|
||||
await group.load(serializedGroup, location);
|
||||
this._groups.set(group.name, group);
|
||||
}
|
||||
|
||||
await this.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the config to a plain JavaScript object
|
||||
*
|
||||
* @param {string=} location - Location used for access control.
|
||||
* @returns {Object} JSON serializable representation of the config.
|
||||
*/
|
||||
serialize(location = this.location) {
|
||||
let groups = [...this._unknownGroups.values()];
|
||||
for (let group of this._groups.values()) {
|
||||
groups.push(group.serialize(location));
|
||||
}
|
||||
|
||||
return { groups };
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the configuration based on a serialized config
|
||||
*
|
||||
* Updates all groups in the config with groups in the serialized
|
||||
* config passed.
|
||||
*
|
||||
* @param {Object} serializedConfig - Output from .serialize().
|
||||
* @param {boolean} notify - Invoke fieldChanged events if true.
|
||||
* @param {string=} location - Location used for access control.
|
||||
*/
|
||||
update(serializedConfig, notify, location = this.location) {
|
||||
this._validate(serializedConfig);
|
||||
for (let serializedGroup of serializedConfig.groups) {
|
||||
let group = this._groups.get(serializedGroup.name);
|
||||
if (group) {
|
||||
group.update(serializedGroup, notify, location);
|
||||
} else {
|
||||
this._unknownGroups.set(serializedGroup.name, serializedGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if field can be accessed from the given location
|
||||
*
|
||||
* @param {string} name - Name of the field to check.
|
||||
* @param {string=} location - Location used for access control.
|
||||
* @returns {boolean} true if field is accessible
|
||||
*/
|
||||
canAccess(name, location = this.location) {
|
||||
let [group, field] = splitOn(".", name);
|
||||
return this.group(group).canAccess(field, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value for a config field
|
||||
*
|
||||
* @param {string} name - Name of field to get.
|
||||
* @param {string=} location - Location used for access control.
|
||||
* @returns {*} the value for the field.
|
||||
*/
|
||||
get(name, location = this.location) {
|
||||
let [group, field] = splitOn(".", name);
|
||||
return this.group(group).get(field, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value for a config field
|
||||
*
|
||||
* @param {string} name - Name of field to set.
|
||||
* @param {*} value - the value to set on field
|
||||
* @param {string=} location - Location used for access control.
|
||||
*/
|
||||
set(name, value, location = this.location) {
|
||||
let [group, field] = splitOn(".", name);
|
||||
this.group(group).set(field, value, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set property of an object value for a config field
|
||||
*
|
||||
* @param {string} name - Name of field to set property on.
|
||||
* @param {string} prop - Name of property to set on field.
|
||||
* @param {*} value - the value to set the property to.
|
||||
* @param {string=} location - Location used for access control.
|
||||
*/
|
||||
setProp(name, prop, value, location = this.location) {
|
||||
let [group, field] = splitOn(".", name);
|
||||
this.group(group).setProp(field, prop, value, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the config group instance with the given name
|
||||
*
|
||||
* @param {string} name - Name of group to get.
|
||||
* @returns {module:lib.ConfigGroup} config group.
|
||||
*/
|
||||
group(name) {
|
||||
if (!this._initialized) {
|
||||
throw new Error(`${this.constructor.name} instance is uninitialized`);
|
||||
}
|
||||
let group = this._groups.get(name);
|
||||
if (!group) {
|
||||
throw new InvalidField(`No config group named '${name}'`);
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
static _initSubclass() {
|
||||
// These properties are initialized here in order to ensure that
|
||||
// they end up on the sub-class.
|
||||
if (!this._initialized) {
|
||||
this._initialized = true;
|
||||
this._finalized = false;
|
||||
this._groups = new Map();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapping of group name to group class
|
||||
*
|
||||
* Returns the internal mapping of group names to group classes that
|
||||
* mave been registered with this config. Note that mutating this
|
||||
* object will lead to unexpected behaviour.
|
||||
*
|
||||
* @returns {Map<string, GroupClass>} config groups
|
||||
*/
|
||||
static get groups() {
|
||||
this._initSubclass();
|
||||
return this._groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks this Config class from adding more groups.
|
||||
*/
|
||||
static finalize() {
|
||||
this._initSubclass();
|
||||
this._finalized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a ConfigGroup with this Config
|
||||
*
|
||||
* Adds the ConfigGroup to this config as a group that will get
|
||||
* initialized, loaded and serialized with this config class.
|
||||
*
|
||||
* @param {module:lib.ConfigGroup} group - class to register.
|
||||
*/
|
||||
static registerGroup(group) {
|
||||
this._initSubclass();
|
||||
|
||||
if (this._finalized) {
|
||||
throw new Error("Cannot register group on finalized config");
|
||||
}
|
||||
|
||||
if (!group._finalized) {
|
||||
throw new Error("Group must be finalized before it can be registered");
|
||||
}
|
||||
|
||||
if (this._groups.has(group.groupName)) {
|
||||
throw new Error(`${group.groupName} has already been registered`);
|
||||
}
|
||||
|
||||
this._groups.set(group.groupName, group);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Collection of related config entries
|
||||
*
|
||||
* Type checks and stores a collection of related config entries. Attempt
|
||||
* to set invalid values for fields will result in an error thrown while
|
||||
* loading a config with invalid values will result in those being replaced
|
||||
* by the innitial value for those fields.
|
||||
*
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class ConfigGroup {
|
||||
/**
|
||||
* Creates a new config group.
|
||||
*
|
||||
* After the creation of the new config group you have to call
|
||||
* either .init() or .load() on it in order to fully initialize it.
|
||||
*
|
||||
* @param {module:lib.Config} config -
|
||||
* Parent config for this group instance.
|
||||
*/
|
||||
constructor(config) {
|
||||
if (!this.constructor._finalized) {
|
||||
throw new Error(`Cannot instantiate incomplete ConfigGroup class ${this.constructor.name}`);
|
||||
}
|
||||
|
||||
this._fields = new Map();
|
||||
this._unknownFields = new Map();
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check access for a field
|
||||
*
|
||||
* @param {object} def - definition for for the field to check.
|
||||
* @param {string} remote - location of remote access to check for.
|
||||
* @param {boolean} error - throw on failure to pass check.
|
||||
* @throws {module:lib.InvalidAccess}
|
||||
* if access is not granted and error is true.
|
||||
* @returns {boolean} true if access is granted
|
||||
* @private
|
||||
*/
|
||||
_checkAccess(def, remote, error) {
|
||||
for (let loc of [remote, this.config.location]) {
|
||||
if (!def.access.includes(loc)) {
|
||||
if (error) {
|
||||
throw new InvalidAccess(`Field '${def.name}' is not accessible from ${loc}`);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize new config group
|
||||
*
|
||||
* Computes and assigns the initial values for all fields of the
|
||||
* config group.
|
||||
*/
|
||||
async init() {
|
||||
for (let [name, def] of this.constructor._definitions) {
|
||||
if (this._fields.has(name)) {
|
||||
continue;
|
||||
}
|
||||
if (!def.access.includes(this.config.location)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let value = null;
|
||||
if (typeof def.initial_value === "function") {
|
||||
value = await def.initial_value();
|
||||
|
||||
} else if (def.initial_value !== undefined) {
|
||||
value = def.initial_value;
|
||||
}
|
||||
|
||||
this._fields.set(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load from serialized group
|
||||
*
|
||||
* Loads all the values from the passed serialized version of the
|
||||
* group and then initializes any possible missing fields from it to
|
||||
* their default values.
|
||||
*
|
||||
* @param {Object} serializedGroup -
|
||||
* Result from a previous call to .serialize().
|
||||
* @param {string=} location - Location used for access control.
|
||||
*/
|
||||
async load(serializedGroup, location = this.config.location) {
|
||||
this.update(serializedGroup, false, location);
|
||||
await this.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Name of the group
|
||||
*
|
||||
* Shortcut for `group.constructor.groupName`
|
||||
*/
|
||||
get name() {
|
||||
return this.constructor.groupName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if field can be accessed from the given location
|
||||
*
|
||||
* @param {string} name - Name of field to check.
|
||||
* @param {string=} location - Location used for access control.
|
||||
* @returns {boolean} true if field is accessible
|
||||
*/
|
||||
canAccess(name, location = this.config.location) {
|
||||
let def = this.constructor._definitions.get(name);
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
}
|
||||
return this._checkAccess(def, location, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value for field
|
||||
*
|
||||
* @param {string} name - Name of field to get.
|
||||
* @param {string=} location - Location used for access control.
|
||||
* @returns {*} Value stored for the field.
|
||||
*/
|
||||
get(name, location = this.config.location) {
|
||||
let def = this.constructor._definitions.get(name);
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
}
|
||||
this._checkAccess(def, location, true);
|
||||
|
||||
return this._fields.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set value of field
|
||||
*
|
||||
* The field must be defined for the config group and the value is
|
||||
* type checked against the field definition.
|
||||
*
|
||||
* @param {string} name - Name of field to set.
|
||||
* @param {*} value - Value to set for field.
|
||||
* @param {string=} location - Location used for access control.
|
||||
* @throws {module:lib.InvalidField} if field is not defined.
|
||||
* @throws {module:lib.InvalidValue} if value is not allowed for the field.
|
||||
*/
|
||||
set(name, value, location = this.config.location) {
|
||||
let def = this.constructor._definitions.get(name);
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
}
|
||||
this._checkAccess(def, location, true);
|
||||
|
||||
// Empty strings are treates as null
|
||||
if (value === "") {
|
||||
value = null;
|
||||
}
|
||||
|
||||
if (def.enum && !def.enum.includes(value)) {
|
||||
throw new InvalidValue(`Expected one of [${def.enum.join(", ")}], not ${value}`);
|
||||
}
|
||||
|
||||
if (basicType(value) === "string") {
|
||||
if (def.type === "boolean") {
|
||||
if (value === "true") {
|
||||
value = true;
|
||||
} else if (value === "false") {
|
||||
value = false;
|
||||
}
|
||||
|
||||
} else if (def.type === "number") {
|
||||
let numberRegExp = /^[+-]?(Infinity|\d+\.(\d+)?([eE][+-]?\d+)?|\.?\d+([eE][+-]?\d+)?)$/;
|
||||
if (numberRegExp.test(value.trim())) {
|
||||
value = Number.parseFloat(value);
|
||||
}
|
||||
|
||||
} else if (def.type === "object") {
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch (err) {
|
||||
throw new InvalidValue(`Error parsing value for ${name}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
if (!def.optional) {
|
||||
throw new InvalidValue(`Field ${name} cannot be null`);
|
||||
}
|
||||
|
||||
} else if (basicType(value) !== def.type) {
|
||||
throw new InvalidValue(`Expected type of ${name} to be ${def.type}, not ${basicType(value)}`);
|
||||
}
|
||||
|
||||
let prev = this._fields.get(name);
|
||||
this._fields.set(name, value);
|
||||
if (this.config && !isDeepStrictEqual(value, prev)) {
|
||||
this.config.emit("fieldChanged", this, name, prev);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set property of object field
|
||||
*
|
||||
* Update value of stored object field by setting the specified property
|
||||
* on it. The field must be defined as an object for the config group.
|
||||
*
|
||||
* @param {string} name - Name of field to set property on.
|
||||
* @param {string} prop - Name of property to set on field.
|
||||
* @param {*} value - the value to set the property to.
|
||||
* @param {string=} location - Location used for access control.
|
||||
* @throws {module:lib.InvalidField} if field is not defined.
|
||||
* @throws {module:lib.InvalidValue} if field is not an object.
|
||||
*/
|
||||
setProp(name, prop, value, location = this.config.location) {
|
||||
let def = this.constructor._definitions.get(name);
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
}
|
||||
this._checkAccess(def, location, true);
|
||||
|
||||
if (def.type !== "object") {
|
||||
throw new InvalidValue(`Cannot set property on non-object field '${name}'`);
|
||||
}
|
||||
|
||||
let prev = this._fields.get(name);
|
||||
let updated = {...prev || {}};
|
||||
|
||||
if (value !== undefined) {
|
||||
updated[prop] = value;
|
||||
} else {
|
||||
delete updated[prop];
|
||||
}
|
||||
this._fields.set(name, updated);
|
||||
if (this.config && !isDeepStrictEqual(updated, prev)) {
|
||||
this.config.emit("fieldChanged", this, name, prev);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a config group from a serialized group
|
||||
*
|
||||
* Overwrites the values of all the fields in this config group that
|
||||
* has a valid value in the passed serialized group with that value.
|
||||
*
|
||||
* @param {Object} serializedGroup -
|
||||
* Result from a previous call to .serialize().
|
||||
* @param {boolean} notify - Invoke fieldChanged events if true.
|
||||
* @param {string=} location - Location used for access control.
|
||||
*/
|
||||
update(serializedGroup, notify, location = this.config.location) {
|
||||
if (basicType(serializedGroup) !== "object") {
|
||||
throw new Error(`Expected object, not ${basicType(serializedGroup)} for ConfigGroup`);
|
||||
}
|
||||
|
||||
if (serializedGroup.name !== this.name) {
|
||||
throw new Error(`Expected group name ${this.name}, not ${serializedGroup.name}`);
|
||||
}
|
||||
|
||||
if (basicType(serializedGroup.fields) !== "object") {
|
||||
throw new Error(`Expected fields to be an object, not ${basicType(serializedGroup.fields)}`);
|
||||
}
|
||||
|
||||
for (let [name, value] of Object.entries(serializedGroup.fields)) {
|
||||
let def = this.constructor._definitions.get(name);
|
||||
if (!def) {
|
||||
this._unknownFields.set(name, value);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!this._checkAccess(def, location, false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (def.optional) {
|
||||
if (value !== null && basicType(value) !== def.type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
} else if (basicType(value) !== def.type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (def.enum && !def.enum.includes(value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let prev = this._fields.get(name);
|
||||
this._fields.set(name, value);
|
||||
if (notify && this.config && !isDeepStrictEqual(value, prev)) {
|
||||
this.config.emit("fieldChanged", this, name, prev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the config to a plain JavaScript object
|
||||
*
|
||||
* @param {string=} location - Location used for access control.
|
||||
* @returns {Object} JSON serializable representation of the group.
|
||||
*/
|
||||
serialize(location = this.config.location) {
|
||||
let fields = {};
|
||||
for (let [name, value] of this._fields) {
|
||||
let def = this.constructor._definitions.get(name);
|
||||
if (!this._checkAccess(def, location, false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
fields[name] = value;
|
||||
}
|
||||
|
||||
for (let [name, value] of this._unknownFields) {
|
||||
fields[name] = value;
|
||||
}
|
||||
|
||||
return {
|
||||
name: this.name,
|
||||
fields,
|
||||
};
|
||||
}
|
||||
|
||||
static _initSubclass() {
|
||||
// These properties are initialized here in order to ensure that
|
||||
// they end up on the sub-class.
|
||||
if (!this._initialized) {
|
||||
this._initialized = true;
|
||||
this._definitions = new Map();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapping of config name to field meta data
|
||||
*
|
||||
* Returns the internal meta data for the fields of this class.
|
||||
* Note that mutating this object will lead to unexpected behaviour.
|
||||
*
|
||||
* @returns {Map<string, Object>} field meta data
|
||||
*/
|
||||
static get definitions() {
|
||||
this._initSubclass();
|
||||
return this._definitions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a new configuration field
|
||||
*
|
||||
* @param {Object} item - Field definition.
|
||||
* @param {Array<string>=} item.access -
|
||||
* Array of strings declaring where this config entry can be read
|
||||
* and modified from.
|
||||
* @param {string} item.type -
|
||||
* string declaring the type of the config value, supports
|
||||
* boolean, string, number and object.
|
||||
* @param {string} item.name -
|
||||
* Name of the configuration field. Should follow the
|
||||
* lower_case_underscore style.
|
||||
* @param {string} item.title -
|
||||
* Text used to identify the config field in user interfaces.
|
||||
* @param {string} item.description -
|
||||
* Text used to describe the config field in user interfaces.
|
||||
* @param {Array=} item.enum -
|
||||
* Array of all values that are acceptible. Useful for making
|
||||
* enumerated values.
|
||||
* @param {boolean=} item.restartRequired -
|
||||
* True if a restart of the entity this config is attached to is
|
||||
* required for changes to take effect. Informative only, defaults
|
||||
* to false.
|
||||
* @param {Array=} item.restartRequiredProps -
|
||||
* Properties to invert the value of restartRequired for if present.
|
||||
* @param {boolean=} item.optional -
|
||||
* True if this field can be set to null. Defaults to false.
|
||||
* @param {*=} item.initial_value -
|
||||
* Value this config field should take in a newly initialized
|
||||
* config. This can also be an async function returning the
|
||||
* value to use.
|
||||
*/
|
||||
static define(item) {
|
||||
if (this._finalized) {
|
||||
throw new Error(`Cannot define field for ConfigGroup class ${this.name} after it has been finalized`);
|
||||
}
|
||||
|
||||
if (basicType(this.groupName) !== "string") {
|
||||
throw new Error(`Expected ConfigGroup class ${this.name} to have the groupName property set to a string`);
|
||||
}
|
||||
|
||||
this._initSubclass();
|
||||
|
||||
let itemKeys = Object.keys(item);
|
||||
|
||||
// Check for required properties
|
||||
["name", "type"].forEach(prop => {
|
||||
if (!itemKeys.includes(prop)) {
|
||||
throw new Error(`${prop} is required when defining an field`);
|
||||
}
|
||||
});
|
||||
|
||||
// Validate properties
|
||||
let validTypes = ["boolean", "string", "number", "object"];
|
||||
// eslint-disable-next-line complexity
|
||||
Object.keys(item).forEach(key => {
|
||||
let value = item[key];
|
||||
let valueType = basicType(value);
|
||||
switch (key) {
|
||||
case "type":
|
||||
if (!validTypes.includes(value)) {
|
||||
throw new Error(`${value} is not a valid type`);
|
||||
}
|
||||
break;
|
||||
|
||||
case "access":
|
||||
if (valueType !== "array") {
|
||||
throw new Error("access must be an array");
|
||||
}
|
||||
break;
|
||||
|
||||
case "enum":
|
||||
if (valueType !== "array") {
|
||||
throw new Error("enum must be an array");
|
||||
}
|
||||
break;
|
||||
|
||||
case "name":
|
||||
if (valueType !== "string") {
|
||||
throw new Error("name must be a string");
|
||||
}
|
||||
break;
|
||||
|
||||
case "title":
|
||||
if (valueType !== "string") {
|
||||
throw new Error("title must be a string");
|
||||
}
|
||||
break;
|
||||
|
||||
case "description":
|
||||
if (valueType !== "string") {
|
||||
throw new Error("description must be a string");
|
||||
}
|
||||
break;
|
||||
|
||||
case "restartRequired":
|
||||
if (valueType !== "boolean") {
|
||||
throw new Error("restartRequired must be a boolean");
|
||||
}
|
||||
break;
|
||||
|
||||
case "restartRequiredProps":
|
||||
if (valueType !== "array") {
|
||||
throw new Error("restartRequiredProps must be a array");
|
||||
}
|
||||
break;
|
||||
|
||||
case "optional":
|
||||
if (valueType !== "boolean") {
|
||||
throw new Error("optional must be a boolean");
|
||||
}
|
||||
break;
|
||||
|
||||
case "initial_value":
|
||||
if (valueType !== item.type && valueType !== "function") {
|
||||
throw new Error("initial_value must match the type or be a function");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown property ${key}`);
|
||||
}
|
||||
});
|
||||
|
||||
if (this._definitions.has(item.name)) {
|
||||
throw new Error(`Config field ${item.name} has already been defined`);
|
||||
}
|
||||
|
||||
let computed = {
|
||||
access: this.defaultAccess,
|
||||
optional: false,
|
||||
fullName: `${this.groupName}.${item.name}`,
|
||||
restartRequired: false,
|
||||
...item,
|
||||
};
|
||||
|
||||
if (item.restartRequiredProps && item.type !== "object") {
|
||||
throw new Error("restartRequiredProps is only allowed if type is object");
|
||||
}
|
||||
|
||||
if (!computed.access) {
|
||||
throw new Error(`access not set and no defaultAccess defined for ${this.name}`);
|
||||
}
|
||||
|
||||
if (!computed.optional && computed.initial_value === undefined) {
|
||||
throw new Error(`Non-optional field ${item.name} needs an initial_value`);
|
||||
}
|
||||
|
||||
this._definitions.set(item.name, computed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks this ConfigGroup class from adding more definitons.
|
||||
*/
|
||||
static finalize() {
|
||||
if (basicType(this.groupName) !== "string") {
|
||||
throw new Error(`Expected ConfigGroup class ${this.name} to have the groupName property set to a string`);
|
||||
}
|
||||
|
||||
this._initSubclass();
|
||||
this._finalized = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Config group for plugins
|
||||
*
|
||||
* Grouping that includes per plugin fields clusterio uses to manage
|
||||
* plugins. This is currently only the enabled field. It is otherwise
|
||||
* identical to the ConfigGroup class.
|
||||
*
|
||||
* @extends module:lib.ConfigGroup
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class PluginConfigGroup extends ConfigGroup {
|
||||
static _initSubclass() {
|
||||
if (!this._initialized) {
|
||||
super._initSubclass();
|
||||
this.define({
|
||||
name: "load_plugin",
|
||||
title: "Load Plugin",
|
||||
restartRequired: true,
|
||||
type: "boolean",
|
||||
initial_value: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
InvalidAccess,
|
||||
InvalidField,
|
||||
InvalidValue,
|
||||
|
||||
Config,
|
||||
ConfigGroup,
|
||||
PluginConfigGroup,
|
||||
};
|
||||
@@ -0,0 +1,529 @@
|
||||
// Core definitions for the configuration system
|
||||
"use strict";
|
||||
|
||||
const classes = require("./classes");
|
||||
|
||||
|
||||
/**
|
||||
* Controller config group for {@link module:lib.ControllerConfig}
|
||||
* @extends module:lib.ConfigGroup
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class ControllerGroup extends classes.ConfigGroup { }
|
||||
ControllerGroup.defaultAccess = ["controller", "host", "control"];
|
||||
ControllerGroup.groupName = "controller";
|
||||
ControllerGroup.define({
|
||||
name: "name",
|
||||
title: "Name",
|
||||
description: "Name of the cluster.",
|
||||
type: "string",
|
||||
initial_value: "Your Cluster",
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "mods_directory",
|
||||
title: "Mods Directory",
|
||||
description: "Path to directory where mods shared with the cluster are stored.",
|
||||
type: "string",
|
||||
initial_value: "mods",
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "database_directory",
|
||||
title: "Database directory",
|
||||
description: "Directory where item and configuration data is stored.",
|
||||
type: "string",
|
||||
initial_value: "database",
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "http_port",
|
||||
title: "HTTP Port",
|
||||
description: "Port to listen for HTTP connections on, set to null to not listen for HTTP connections.",
|
||||
restartRequired: true,
|
||||
type: "number",
|
||||
optional: true,
|
||||
initial_value: 8080,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "https_port",
|
||||
title: "HTTPS Port",
|
||||
description: "Port to listen for HTTPS connection on, set to null to not listen for HTTPS connections.",
|
||||
restartRequired: true,
|
||||
type: "number",
|
||||
optional: true,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "bind_address",
|
||||
title: "Bind Address",
|
||||
description: "IP address to bind the HTTP and HTTPS ports on.",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "external_address",
|
||||
title: "External Address",
|
||||
description: "Public facing address the controller is hosted on.",
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "tls_certificate",
|
||||
title: "TLS Certificate",
|
||||
description: "Path to the certificate to use for HTTPS.",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "tls_private_key",
|
||||
title: "TLS Private Key",
|
||||
description: "Path to the private key to use for HTTPS.",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
access: ["controller"],
|
||||
name: "auth_secret",
|
||||
title: "Controller Authentication Secret",
|
||||
description:
|
||||
"Secret used to generate and verify authentication tokens. " +
|
||||
"Should be a long string of random letters and numbers. " +
|
||||
"Do not share this.",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "heartbeat_interval",
|
||||
title: "Heartbeat Interval",
|
||||
description: "Interval heartbeats are sent out on WebSocket connections.",
|
||||
type: "number",
|
||||
initial_value: 15,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "session_timeout",
|
||||
title: "Session Timeout",
|
||||
description: "Time in seconds before giving up resuming a dropped WebSocket session.",
|
||||
type: "number",
|
||||
initial_value: 60,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "metrics_timeout",
|
||||
title: "Metrics Timeout",
|
||||
description: "Timeout in seconds for metrics gathering from hosts.",
|
||||
type: "number",
|
||||
initial_value: 8,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "proxy_stream_timeout",
|
||||
title: "Proxy Stream Timeout",
|
||||
description: "Timeout in seconds for proxy streams to start flowing.",
|
||||
type: "number",
|
||||
initial_value: 15,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "default_mod_pack_id",
|
||||
title: "Default Mod Pack",
|
||||
description: "Mod pack used by default for instances.",
|
||||
type: "number",
|
||||
optional: true,
|
||||
});
|
||||
ControllerGroup.define({
|
||||
name: "default_role_id",
|
||||
title: "Default role",
|
||||
description: "ID of role assigned by default to new users.",
|
||||
type: "number",
|
||||
optional: true,
|
||||
initial_value: 1,
|
||||
});
|
||||
ControllerGroup.finalize();
|
||||
|
||||
/**
|
||||
* Controller Config class
|
||||
* @extends module:lib.Config
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class ControllerConfig extends classes.Config { }
|
||||
ControllerConfig.registerGroup(ControllerGroup);
|
||||
|
||||
|
||||
/**
|
||||
* Host config group for {@link module:lib.HostConfig}
|
||||
* @extends module:lib.ConfigGroup
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class HostGroup extends classes.ConfigGroup {}
|
||||
HostGroup.defaultAccess = ["controller", "host", "control"];
|
||||
HostGroup.groupName = "host";
|
||||
HostGroup.define({
|
||||
name: "name",
|
||||
description: "Name of the host",
|
||||
type: "string",
|
||||
initial_value: "New Host",
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "id",
|
||||
description: "ID of the host",
|
||||
type: "number",
|
||||
initial_value: () => Math.random() * 2**31 | 0,
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "factorio_directory",
|
||||
description: "Path to directory to look for factorio installs",
|
||||
type: "string",
|
||||
initial_value: "factorio",
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "mods_directory",
|
||||
title: "Mods Directory",
|
||||
description: "Path to directory where mods for instances are cached.",
|
||||
type: "string",
|
||||
initial_value: "mods",
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "mods_directory_is_shared",
|
||||
title: "Mods Directory is Shared",
|
||||
description: "True if the mods directory is shared with the controller.",
|
||||
type: "boolean",
|
||||
initial_value: true,
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "instances_directory",
|
||||
description: "Path to directory to store instances in.",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
initial_value: "instances",
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "controller_url",
|
||||
description: "URL to connect to the controller at",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
initial_value: "http://localhost:8080/",
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "controller_token",
|
||||
description: "Token to authenticate to controller with.",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
initial_value: "enter token here",
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "tls_ca",
|
||||
description: "Path to Certificate Authority to validate TLS connection to controller against.",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "public_address",
|
||||
description: "Public facing address players should connect to in order to join instances on this host",
|
||||
type: "string",
|
||||
initial_value: "localhost",
|
||||
});
|
||||
HostGroup.define({
|
||||
name: "max_reconnect_delay",
|
||||
title: "Max Reconnect Delay",
|
||||
description: "Maximum delay to wait before attempting to reconnect WebSocket",
|
||||
type: "number",
|
||||
initial_value: 60,
|
||||
});
|
||||
HostGroup.finalize();
|
||||
|
||||
/**
|
||||
* Host Config class
|
||||
* @extends module:lib.Config
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class HostConfig extends classes.Config { }
|
||||
HostConfig.registerGroup(HostGroup);
|
||||
|
||||
|
||||
/**
|
||||
* Instance config group for {@link module:lib.InstanceConfig}
|
||||
* @extends module:lib.ConfigGroup
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class InstanceGroup extends classes.ConfigGroup { }
|
||||
InstanceGroup.defaultAccess = ["controller", "host", "control"];
|
||||
InstanceGroup.groupName = "instance";
|
||||
InstanceGroup.define({
|
||||
name: "name",
|
||||
type: "string",
|
||||
initial_value: "New Instance",
|
||||
});
|
||||
InstanceGroup.define({
|
||||
name: "id",
|
||||
description: "ID of the instance",
|
||||
type: "number",
|
||||
initial_value: () => Math.random() * 2**31 | 0,
|
||||
});
|
||||
InstanceGroup.define({
|
||||
name: "assigned_host",
|
||||
type: "number",
|
||||
optional: true,
|
||||
});
|
||||
InstanceGroup.define({
|
||||
name: "auto_start",
|
||||
description: "Automatically start this instance when the host hosting it is started up",
|
||||
type: "boolean",
|
||||
initial_value: false,
|
||||
});
|
||||
InstanceGroup.finalize();
|
||||
|
||||
/**
|
||||
* Factorio config group for {@link module:lib.InstanceConfig}
|
||||
* @extends module:lib.ConfigGroup
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class FactorioGroup extends classes.ConfigGroup { }
|
||||
FactorioGroup.defaultAccess = ["controller", "host", "control"];
|
||||
FactorioGroup.groupName = "factorio";
|
||||
FactorioGroup.define({
|
||||
name: "version",
|
||||
description: "Version of the game to run, use latest to run the latest installed version",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
initial_value: "latest",
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "game_port",
|
||||
description: "UDP port to run game on, uses a random port if null",
|
||||
restartRequired: true,
|
||||
type: "number",
|
||||
optional: true,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "rcon_port",
|
||||
description: "TCP port to run RCON on, uses a random port if null",
|
||||
restartRequired: true,
|
||||
type: "number",
|
||||
optional: true,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "rcon_password",
|
||||
description: "Password for RCON, randomly generated if null",
|
||||
restartRequired: true,
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "player_online_autosave_slots",
|
||||
description:
|
||||
"Rename autosaves where players have been online since the previous autosave into a separate autosave " +
|
||||
"pool with this many slots. Requires autosaves to be enabled to work. Set to 0 to disable.",
|
||||
type: "number",
|
||||
initial_value: 5,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "mod_pack",
|
||||
description: "Mod pack to use on this server",
|
||||
restartRequired: true,
|
||||
type: "number",
|
||||
optional: true,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "enable_save_patching",
|
||||
description: "Patch saves with Lua code. Required for Clusterio integrations, lua modules, and most plugins.",
|
||||
restartRequired: true,
|
||||
type: "boolean",
|
||||
initial_value: true,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "enable_whitelist",
|
||||
description: "Turn on whitelist for joining the server.",
|
||||
type: "boolean",
|
||||
initial_value: false,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "enable_authserver_bans",
|
||||
description: "Turn on Factorio.com based multiplayer bans.",
|
||||
restartRequired: true,
|
||||
type: "boolean",
|
||||
initial_value: false,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "settings",
|
||||
description: "Settings overridden in server-settings.json",
|
||||
restartRequired: true,
|
||||
restartRequiredProps: [
|
||||
"afk_autokick_interval", "allow_commands", "autosave_interval", "autosave_only_on_server", "description",
|
||||
"ignore_player_limit_for_returning_players", "max_players", "max_upload_slots",
|
||||
"max_upload_in_kilobytes_per_second", "name", "only_admins_can_pause_the_game", "game_password",
|
||||
"require_user_verification", "tags", "visibility",
|
||||
],
|
||||
type: "object",
|
||||
initial_value: {}, // See create instance handler in controller.
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "verbose_logging",
|
||||
description: "Enable verbose logging on the Factorio server",
|
||||
restartRequired: true,
|
||||
type: "boolean",
|
||||
initial_value: false,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "strip_paths",
|
||||
description: "Strip down instance paths in the log",
|
||||
restartRequired: true,
|
||||
type: "boolean",
|
||||
initial_value: true,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "sync_adminlist",
|
||||
description: "Synchronize adminlist with controller",
|
||||
type: "boolean",
|
||||
initial_value: true,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "sync_whitelist",
|
||||
description: "Synchronize whitelist with controller",
|
||||
type: "boolean",
|
||||
initial_value: true,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "sync_banlist",
|
||||
description: "Synchronize banlist with controller",
|
||||
type: "boolean",
|
||||
initial_value: true,
|
||||
});
|
||||
FactorioGroup.define({
|
||||
name: "max_concurrent_commands",
|
||||
description: "Maximum number of RCON commands trasmitted in parallel",
|
||||
restartRequired: true,
|
||||
type: "number",
|
||||
initial_value: 5,
|
||||
});
|
||||
FactorioGroup.finalize();
|
||||
|
||||
/**
|
||||
* Instance config class
|
||||
* @extends module:lib.Config
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class InstanceConfig extends classes.Config { }
|
||||
InstanceConfig.registerGroup(InstanceGroup);
|
||||
InstanceConfig.registerGroup(FactorioGroup);
|
||||
|
||||
|
||||
/**
|
||||
* Control config group for {@link module:lib.ControlConfig}
|
||||
* @extends module:lib.ConfigGroup
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class ControlGroup extends classes.ConfigGroup {}
|
||||
ControlGroup.defaultAccess = ["control"];
|
||||
ControlGroup.groupName = "control";
|
||||
ControlGroup.define({
|
||||
name: "controller_url",
|
||||
description: "URL to connect to the controller at",
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
ControlGroup.define({
|
||||
name: "controller_token",
|
||||
description: "Token to authenticate to controller with.",
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
ControlGroup.define({
|
||||
name: "tls_ca",
|
||||
description: "Path to Certificate Authority to validate TLS connection to controller against.",
|
||||
type: "string",
|
||||
optional: true,
|
||||
});
|
||||
ControlGroup.define({
|
||||
name: "max_reconnect_delay",
|
||||
title: "Max Reconnect Delay",
|
||||
description: "Maximum delay to wait before attempting to reconnect WebSocket",
|
||||
type: "number",
|
||||
initial_value: 60,
|
||||
});
|
||||
ControlGroup.finalize();
|
||||
|
||||
/**
|
||||
* Control config class
|
||||
* @extends module:lib.Config
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class ControlConfig extends classes.Config { }
|
||||
ControlConfig.registerGroup(ControlGroup);
|
||||
|
||||
|
||||
function validateGroup(pluginInfo, groupName) {
|
||||
if (!(pluginInfo[groupName].prototype instanceof classes.PluginConfigGroup)) {
|
||||
throw new Error(
|
||||
`Expected ${groupName} for ${pluginInfo.name} to be a subclass of PluginConfigGroup`
|
||||
);
|
||||
}
|
||||
|
||||
if (pluginInfo[groupName].groupName !== pluginInfo.name) {
|
||||
throw new Error(
|
||||
`Expected ${groupName} for ${pluginInfo.name} to be named after the plugin`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the config groups for the provided plugin infos
|
||||
*
|
||||
* @param {Array<Object>} pluginInfos - Array of plugin info objects.
|
||||
* @memberof module:lib
|
||||
*/
|
||||
function registerPluginConfigGroups(pluginInfos) {
|
||||
for (let pluginInfo of pluginInfos) {
|
||||
if (pluginInfo.ControllerConfigGroup) {
|
||||
validateGroup(pluginInfo, "ControllerConfigGroup");
|
||||
ControllerConfig.registerGroup(pluginInfo.ControllerConfigGroup);
|
||||
|
||||
} else {
|
||||
class ControllerConfigGroup extends classes.PluginConfigGroup { }
|
||||
ControllerConfigGroup.defaultAccess = ["controller", "host", "control"];
|
||||
ControllerConfigGroup.groupName = pluginInfo.name;
|
||||
ControllerConfigGroup.finalize();
|
||||
ControllerConfig.registerGroup(ControllerConfigGroup);
|
||||
}
|
||||
|
||||
if (pluginInfo.instanceEntrypoint) {
|
||||
if (pluginInfo.InstanceConfigGroup) {
|
||||
validateGroup(pluginInfo, "InstanceConfigGroup");
|
||||
InstanceConfig.registerGroup(pluginInfo.InstanceConfigGroup);
|
||||
|
||||
} else {
|
||||
class InstanceConfigGroup extends classes.PluginConfigGroup { }
|
||||
InstanceConfigGroup.defaultAccess = ["controller", "host", "control"];
|
||||
InstanceConfigGroup.groupName = pluginInfo.name;
|
||||
InstanceConfigGroup.finalize();
|
||||
InstanceConfig.registerGroup(InstanceConfigGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock configs from adding more groups and make them usable
|
||||
*
|
||||
* @memberof module:lib
|
||||
*/
|
||||
function finalizeConfigs() {
|
||||
ControllerConfig.finalize();
|
||||
HostConfig.finalize();
|
||||
InstanceConfig.finalize();
|
||||
ControlConfig.finalize();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ControllerGroup,
|
||||
HostGroup,
|
||||
InstanceGroup,
|
||||
FactorioGroup,
|
||||
ControlGroup,
|
||||
|
||||
ControllerConfig,
|
||||
HostConfig,
|
||||
InstanceConfig,
|
||||
ControlConfig,
|
||||
|
||||
registerPluginConfigGroups,
|
||||
finalizeConfigs,
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Configuration System
|
||||
* @author Hornwitser
|
||||
* @module lib/config
|
||||
*/
|
||||
"use strict";
|
||||
const classes = require("./classes");
|
||||
const definitions = require("./definitions");
|
||||
|
||||
module.exports = {
|
||||
...classes,
|
||||
...definitions,
|
||||
};
|
||||
@@ -0,0 +1,46 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Export manifest for a mod pack
|
||||
*
|
||||
* Tracks files stored on disk for data that extracted from Factorio using a
|
||||
* given mod pack.
|
||||
* @alias module:lib.ExportManifest
|
||||
*/
|
||||
class ExportManifest {
|
||||
|
||||
/**
|
||||
* Mapping between known assets and their file names.
|
||||
*
|
||||
* Currently exported assets are
|
||||
* - settings: JSON of mod settings prototypes.
|
||||
* - prototypes: JSON of all game prototypes except settings.
|
||||
* - item-spritesheet: PNG containing icons from the game.
|
||||
* - item-metadata: JSON of Map entries describing icons in item-spritesheet
|
||||
* - locale: JSON of flattened Map entries of en locale strings from the game.
|
||||
* @type {Object<string, string>}
|
||||
*/
|
||||
assets = {};
|
||||
|
||||
constructor(assets) {
|
||||
if (assets) { this.assets = assets; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["assets"],
|
||||
properties: {
|
||||
"assets": {
|
||||
type: "object",
|
||||
additionalProperties: { type: "string" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.assets);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ExportManifest;
|
||||
@@ -0,0 +1,235 @@
|
||||
"use strict";
|
||||
const fs = require("fs-extra");
|
||||
const JSZip = require("jszip");
|
||||
const path = require("path");
|
||||
|
||||
const libHash = require("../hash");
|
||||
const libSchema = require("../schema");
|
||||
const { findRoot } = require("../zip_ops");
|
||||
|
||||
const { integerModVersion, integerFactorioVersion, modVersionRegExp } = require("./version");
|
||||
|
||||
|
||||
/**
|
||||
* Info about a mod available on the controller.
|
||||
*
|
||||
* See https://wiki.factorio.com/Tutorial:Mod_structure#info.json for
|
||||
* details of the invidiual fields sourced from info.json.
|
||||
* @alias module:lib.ModInfo
|
||||
*/
|
||||
class ModInfo {
|
||||
/**
|
||||
* Internal name of this mod.
|
||||
* This is the name of the zip file as well as the name that appears in
|
||||
* info.json.
|
||||
* @type {string}
|
||||
*/
|
||||
name = "";
|
||||
|
||||
/**
|
||||
* Version of the mod.
|
||||
* Sourced from info.json.
|
||||
* @type {string}
|
||||
*/
|
||||
version = "";
|
||||
|
||||
/**
|
||||
* Integer representation of the version
|
||||
* @type {number}
|
||||
*/
|
||||
get integerVersion() {
|
||||
return integerModVersion(this.version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display name of the mod.
|
||||
* Sourced from info.json.
|
||||
* @type {string}
|
||||
*/
|
||||
title = "";
|
||||
|
||||
/**
|
||||
* Author of the mod.
|
||||
* Sourced from info.json.
|
||||
* @type {string}
|
||||
*/
|
||||
author = "";
|
||||
|
||||
/**
|
||||
* Contact field for mod.
|
||||
* Sourced from info.json.
|
||||
* @type {string}
|
||||
*/
|
||||
contact = "";
|
||||
|
||||
/**
|
||||
* Homepage of the mod.
|
||||
* Sourced from info.json.
|
||||
* @type {string}
|
||||
*/
|
||||
homepage = "";
|
||||
|
||||
/**
|
||||
* Description of the mod.
|
||||
* Sourced from info.json.
|
||||
* @type {string}
|
||||
*/
|
||||
description = "";
|
||||
|
||||
/**
|
||||
* Major version of Factorio this mod supports.
|
||||
* Sourced from info.json.
|
||||
* @type {string}
|
||||
*/
|
||||
factorioVersion = "0.12";
|
||||
|
||||
/**
|
||||
* Integer representation of the factorioVersion
|
||||
* @type {number}
|
||||
*/
|
||||
get integerFactorioVersion() {
|
||||
return integerFactorioVersion(this.factorioVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dependiences for this mod.
|
||||
* Sourced from info.json.
|
||||
* @type {Array<string>}
|
||||
*/
|
||||
dependencies = ["base"];
|
||||
|
||||
/**
|
||||
* Expected name of zip file containing this mod.
|
||||
* @type {number}
|
||||
*/
|
||||
get filename() {
|
||||
return `${this.name}_${this.version}.zip`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Size of the mod in bytes
|
||||
* @type {number}
|
||||
*/
|
||||
size = 0;
|
||||
|
||||
/**
|
||||
* SHA1 hash of this mod
|
||||
* @type {string=}
|
||||
*/
|
||||
sha1;
|
||||
|
||||
/**
|
||||
* True if this mod has been deleted
|
||||
* @type {boolean}
|
||||
*/
|
||||
isDeleted = false;
|
||||
|
||||
// Content of info.json found in mod files
|
||||
static infoJsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "version", "title", "author"],
|
||||
properties: {
|
||||
"name": { type: "string" },
|
||||
"version": { type: "string" },
|
||||
"title": { type: "string" },
|
||||
"author": { type: "string" },
|
||||
"contact": { type: "string" },
|
||||
"homepage": { type: "string" },
|
||||
"description": { type: "string" },
|
||||
"factorio_version": { type: "string" },
|
||||
"dependencies": { type: "array", items: { type: "string" } },
|
||||
},
|
||||
};
|
||||
|
||||
static validateInfo = libSchema.compile(this.infoJsonSchema);
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
...this.infoJsonSchema.properties,
|
||||
"size": { type: "integer" },
|
||||
"sha1": { type: "string" },
|
||||
"is_deleted": { type: "boolean" },
|
||||
},
|
||||
};
|
||||
|
||||
static validate = libSchema.compile(this.jsonSchema);
|
||||
|
||||
static fromJSON(json) {
|
||||
const modInfo = new this();
|
||||
|
||||
// info.json fields
|
||||
if (json.name) { modInfo.name = json.name; }
|
||||
if (json.version) { modInfo.version = json.version; }
|
||||
if (json.title) { modInfo.title = json.title; }
|
||||
if (json.author) { modInfo.author = json.author; }
|
||||
if (json.contact) { modInfo.contact = json.contact; }
|
||||
if (json.homepage) { modInfo.homepage = json.homepage; }
|
||||
if (json.description) { modInfo.description = json.description; }
|
||||
if (json.factorio_version) { modInfo.factorioVersion = json.factorio_version; }
|
||||
if (json.dependencies) { modInfo.dependencies = json.dependencies; }
|
||||
|
||||
// Additional data
|
||||
if (json.size) { modInfo.size = json.size; }
|
||||
if (json.sha1) { modInfo.sha1 = json.sha1; }
|
||||
if (json.is_deleted) { modInfo.isDeleted = json.is_deleted; }
|
||||
|
||||
return modInfo;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
let json = {
|
||||
name: this.name,
|
||||
version: this.version,
|
||||
title: this.title,
|
||||
author: this.author,
|
||||
};
|
||||
if (this.contact) { json.contact = this.contact; }
|
||||
if (this.homepage) { json.homepage = this.homepage; }
|
||||
if (this.description) { json.description = this.description; }
|
||||
if (this.factorio_version !== "0.12") { json.factorio_version = this.factorioVersion; }
|
||||
if (this.dependencies.length !== 1 || this.dependencies[0] !== "base") {
|
||||
json.dependencies = this.dependencies;
|
||||
}
|
||||
if (this.size) { json.size = this.size; }
|
||||
if (this.sha1) { json.sha1 = this.sha1; }
|
||||
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
|
||||
return json;
|
||||
}
|
||||
|
||||
static async fromModFile(modPath) {
|
||||
let modInfo;
|
||||
{
|
||||
// XXX: JSZip needs the whole archive loaded in memory to work.
|
||||
// This is clearly untenable and will be replaced later.
|
||||
let zip = await JSZip.loadAsync(await fs.readFile(modPath));
|
||||
let root = zip.folder(findRoot(zip));
|
||||
|
||||
let infoFile = root.file("info.json");
|
||||
if (!infoFile) {
|
||||
throw new Error("Mod contains no info.json file");
|
||||
}
|
||||
|
||||
modInfo = JSON.parse(await infoFile.async("string"));
|
||||
}
|
||||
|
||||
let valid = this.validateInfo(modInfo);
|
||||
if (!valid) {
|
||||
throw new Error("Mod's info.json is not valid");
|
||||
}
|
||||
|
||||
if (!modVersionRegExp.test(modInfo.version)) {
|
||||
throw new Error(`Mod's version (${modInfo.version}) is invalid`);
|
||||
}
|
||||
|
||||
return this.fromJSON({
|
||||
...modInfo,
|
||||
|
||||
size: (await fs.stat(modPath)).size,
|
||||
sha1: await libHash.hashFile(modPath),
|
||||
is_deleted: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ModInfo;
|
||||
@@ -0,0 +1,372 @@
|
||||
"use strict";
|
||||
const zlib = require("zlib");
|
||||
|
||||
const libSchema = require("../schema");
|
||||
|
||||
const ExportManifest = require("./ExportManifest");
|
||||
const { integerFactorioVersion } = require("./version");
|
||||
|
||||
|
||||
/**
|
||||
* A setting for a mod.
|
||||
* @typedef {object} module:lib.ModPack~ModSetting
|
||||
* @property {boolean|number|string} value - Value of the given mod setting.
|
||||
*/
|
||||
const ModSettingJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["value"],
|
||||
properties: {
|
||||
"value": { type: ["boolean", "number", "string"] },
|
||||
},
|
||||
};
|
||||
|
||||
const ModSettingsJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: ModSettingJsonSchema,
|
||||
};
|
||||
|
||||
/**
|
||||
* A mod in a mod pack
|
||||
* @typedef {object} module:lib.ModPack~ModRecord
|
||||
* @property {string} name - name of the mod.
|
||||
* @property {boolean} enabled - if mod is to be loaded.
|
||||
* @property {string} version - version of the mod.
|
||||
* @property {string=} sha1 - SHA1 hash of the zip file.
|
||||
*/
|
||||
const ModRecordJsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "enabled", "version"],
|
||||
properties: {
|
||||
"name": { type: "string" },
|
||||
"enabled": { type: "boolean" },
|
||||
"version": { type: "string" },
|
||||
"sha1": { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Factorio Mod Pack
|
||||
*
|
||||
* Tracks mods and settings for a collection of Factorio mods.
|
||||
* @alias module:lib.ModPack
|
||||
*/
|
||||
class ModPack {
|
||||
/**
|
||||
* Id of this mod pack
|
||||
* @type {number}
|
||||
*/
|
||||
id;
|
||||
|
||||
/**
|
||||
* Name of this mod pack.
|
||||
* @type {string}
|
||||
*/
|
||||
name = "";
|
||||
|
||||
/**
|
||||
* Description for this modpcak.
|
||||
* @type {string}
|
||||
*/
|
||||
description = "";
|
||||
|
||||
/**
|
||||
* Version of Factorio this mod pack is for
|
||||
* @type {string}
|
||||
*/
|
||||
factorioVersion = "1.1.0";
|
||||
|
||||
/**
|
||||
* Integer representation of the factorioVersion
|
||||
* @type {number}
|
||||
*/
|
||||
get integerFactorioVersion() {
|
||||
return integerFactorioVersion(this.factorioVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mods included in this mod pack
|
||||
* @type {Map<string, module:lib.ModPack~ModRecord>}
|
||||
*/
|
||||
mods = new Map();
|
||||
|
||||
/**
|
||||
* Mod settings for this mod pack
|
||||
* @type {Object<string, Map<string, module:lib.ModPack~ModSetting>>=}
|
||||
*/
|
||||
settings = {
|
||||
"startup": new Map(),
|
||||
"runtime-global": new Map(),
|
||||
"runtime-per-user": new Map(),
|
||||
};
|
||||
|
||||
/**
|
||||
* Mapping to files containing exported data for this modpack
|
||||
* @type {module:lib.ExportManifest|undefined}
|
||||
*/
|
||||
exportManifest;
|
||||
|
||||
/**
|
||||
* True if this mod pack has been deleted from the list of mod packs.
|
||||
* @type {boolean}
|
||||
*/
|
||||
isDeleted = false;
|
||||
|
||||
shallowClone() {
|
||||
const clone = new this.constructor();
|
||||
clone.id = this.id;
|
||||
clone.name = this.name;
|
||||
clone.description = this.description;
|
||||
clone.factorioVersion = this.factorioVersion;
|
||||
clone.mods = this.mods;
|
||||
clone.settings = this.settings;
|
||||
clone.exportManifest = this.exportManifest;
|
||||
clone.isDeleted = this.isDeleted;
|
||||
return clone;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["name", "description", "factorio_version", "mods", "settings"],
|
||||
properties: {
|
||||
"id": { type: "integer" },
|
||||
"name": { type: "string" },
|
||||
"description": { type: "string" },
|
||||
"factorio_version": { type: "string" },
|
||||
"mods": {
|
||||
type: "array",
|
||||
items: ModRecordJsonSchema,
|
||||
},
|
||||
"settings": {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["startup", "runtime-global", "runtime-per-user"],
|
||||
properties: {
|
||||
"startup": ModSettingsJsonSchema,
|
||||
"runtime-global": ModSettingsJsonSchema,
|
||||
"runtime-per-user": ModSettingsJsonSchema,
|
||||
},
|
||||
},
|
||||
"export_manifest": ExportManifest.jsonSchema,
|
||||
"is_deleted": { type: "boolean" },
|
||||
},
|
||||
};
|
||||
|
||||
static validate = libSchema.compile(this.jsonSchema);
|
||||
|
||||
static fromJSON(json) {
|
||||
const modPack = new this();
|
||||
|
||||
if (json.id) {
|
||||
modPack.id = json.id;
|
||||
} else {
|
||||
modPack.id = Math.random() * 2**31 | 0;
|
||||
}
|
||||
if (json.name) { modPack.name = json.name; }
|
||||
if (json.description) { modPack.description = json.description; }
|
||||
if (json.factorio_version) { modPack.factorioVersion = json.factorio_version; }
|
||||
if (json.mods) { modPack.mods = new Map(json.mods.map(m => [m.name, m])); }
|
||||
if (json.settings) {
|
||||
modPack.settings = {
|
||||
"startup": new Map(Object.entries(json.settings["startup"])),
|
||||
"runtime-global": new Map(Object.entries(json.settings["runtime-global"])),
|
||||
"runtime-per-user": new Map(Object.entries(json.settings["runtime-per-user"])),
|
||||
};
|
||||
}
|
||||
if (json.export_manifest) { modPack.exportManifest = ExportManifest.fromJSON(json.export_manifest); }
|
||||
if (json.is_deleted) { modPack.isDeleted = json.is_deleted; }
|
||||
|
||||
if (!modPack.mods.has("base")) {
|
||||
modPack.mods.set("base", { name: "base", enabled: true, version: modPack.factorioVersion });
|
||||
}
|
||||
|
||||
return modPack;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
let json = {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
description: this.description,
|
||||
factorio_version: this.factorioVersion,
|
||||
mods: [...this.mods.values()],
|
||||
settings: {
|
||||
"startup": Object.fromEntries(this.settings["startup"]),
|
||||
"runtime-global": Object.fromEntries(this.settings["runtime-global"]),
|
||||
"runtime-per-user": Object.fromEntries(this.settings["runtime-per-user"]),
|
||||
},
|
||||
};
|
||||
if (this.exportManifest) { json.export_manifest = this.exportManifest; }
|
||||
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
|
||||
return json;
|
||||
}
|
||||
|
||||
toModPackString() {
|
||||
const json = this.toJSON();
|
||||
delete json.id;
|
||||
delete json.export_manifest;
|
||||
|
||||
// eslint-disable-next-line node/no-sync
|
||||
let buf = zlib.deflateSync(JSON.stringify(json));
|
||||
return buf.toString("base64");
|
||||
}
|
||||
|
||||
static fromModPackString(modPackString) {
|
||||
let buf = Buffer.from(modPackString, "base64");
|
||||
try {
|
||||
// eslint-disable-next-line node/no-sync
|
||||
buf = zlib.inflateSync(buf);
|
||||
} catch (err) {
|
||||
if (err.code.startsWith("Z_")) {
|
||||
throw new Error("Malformed mod pack string: zlib inflate failed");
|
||||
}
|
||||
}
|
||||
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(buf);
|
||||
} catch (err) {
|
||||
throw new Error(`Malformed mod pack string: ${err.message}`);
|
||||
}
|
||||
|
||||
if (!this.validate(json)) {
|
||||
throw new Error("Malformed mod pack string: Schema validation failed");
|
||||
}
|
||||
|
||||
return this.fromJSON(json);
|
||||
}
|
||||
|
||||
toModSettingsDat() {
|
||||
function uint8Byte(int) {
|
||||
return Buffer.from(Uint8Array.from([int]).buffer);
|
||||
}
|
||||
function int16Bytes(int) {
|
||||
return Buffer.from(Int16Array.from([int]).buffer);
|
||||
}
|
||||
function uint32Bytes(int) {
|
||||
return Buffer.from(Uint32Array.from([int]).buffer);
|
||||
}
|
||||
function uint32SpaceOptimizedBytes(int) {
|
||||
if (0 <= int && int < 0xff) {
|
||||
return uint8Byte(int);
|
||||
}
|
||||
return Buffer.concat([
|
||||
uint8Byte(0xff),
|
||||
uint32Bytes(int),
|
||||
]);
|
||||
}
|
||||
function doubleBytes(double) {
|
||||
return Buffer.from(Float64Array.from([double]).buffer);
|
||||
}
|
||||
function stringBytes(str) {
|
||||
const buf = Buffer.from(str, "utf8");
|
||||
return Buffer.concat([
|
||||
uint32SpaceOptimizedBytes(buf.length),
|
||||
buf,
|
||||
]);
|
||||
}
|
||||
function immutableStringBytes(str) {
|
||||
if (str === undefined) {
|
||||
return uint8Byte(1); // empty
|
||||
}
|
||||
return Buffer.concat([
|
||||
uint8Byte(0), // empty
|
||||
stringBytes(str),
|
||||
]);
|
||||
}
|
||||
function versionBytes(version) {
|
||||
const [main, major, minor] = version.split(".").map(n => Number.parseInt(n, 10));
|
||||
return Buffer.concat([
|
||||
int16Bytes(main),
|
||||
int16Bytes(major),
|
||||
int16Bytes(minor),
|
||||
Buffer.alloc(2), // developer
|
||||
Buffer.alloc(1), // reserved byte
|
||||
]);
|
||||
}
|
||||
function propertyTreeListBytes(entries) {
|
||||
const sizeBytes = uint32Bytes(entries.length);
|
||||
const itemBytes = entries.flatMap(([key, item]) => [
|
||||
immutableStringBytes(key),
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
propertyTreeBytes(item),
|
||||
]);
|
||||
return Buffer.concat([
|
||||
sizeBytes,
|
||||
...itemBytes,
|
||||
]);
|
||||
}
|
||||
function propertyTreeBytes(element) {
|
||||
let type;
|
||||
let dataBytes;
|
||||
if (typeof element === null) {
|
||||
type = 1;
|
||||
dataBytes = Buffer.alloc(0);
|
||||
} else if (typeof element === "boolean") {
|
||||
type = 1;
|
||||
dataBytes = uint8Byte(element);
|
||||
} else if (typeof element === "number") {
|
||||
type = 2;
|
||||
dataBytes = doubleBytes(element);
|
||||
} else if (typeof element === "string") {
|
||||
type = 3;
|
||||
dataBytes = immutableStringBytes(element);
|
||||
} else if (element instanceof Array) {
|
||||
type = 4;
|
||||
dataBytes = propertyTreeListBytes(element.map(item => [undefined, item]));
|
||||
} else if (typeof element === "object") {
|
||||
type = 5;
|
||||
dataBytes = propertyTreeListBytes(Object.entries(element));
|
||||
} else {
|
||||
throw new Error("Bad element passed to propertyTreeBytes");
|
||||
}
|
||||
return Buffer.concat([
|
||||
uint8Byte(type),
|
||||
Buffer.alloc(1), // anyTypeFlag
|
||||
dataBytes,
|
||||
]);
|
||||
}
|
||||
|
||||
return Buffer.concat([
|
||||
versionBytes(this.factorioVersion),
|
||||
propertyTreeBytes(this.toJSON().settings),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill in missing settings with their default values
|
||||
*
|
||||
* Uses the provided setting prototypes to add any missing mod settings
|
||||
* in the mod pack with the default value from the prototype.
|
||||
*
|
||||
* @param {Object<string, object>} settingPrototypes -
|
||||
* Setting prototypes exported from the game.
|
||||
* @param {Logger} logger - Logger used to report warnings on.
|
||||
*/
|
||||
fillDefaultSettings(settingPrototypes, logger) {
|
||||
const knownTypes = ["bool-setting", "int-setting", "double-setting", "string-setting"];
|
||||
let prototypes = Object.entries(settingPrototypes)
|
||||
.filter(([type, _]) => knownTypes.includes(type))
|
||||
.flatMap(([_, settings]) => Object.values(settings))
|
||||
;
|
||||
|
||||
for (let prototype of prototypes) {
|
||||
if (!["startup", "runtime-global", "runtime-per-user"].includes(prototype.setting_type)) {
|
||||
logger.warn(`Ignoring ${prototype.name} with unknown setting_type ${prototype.setting_type}`);
|
||||
continue;
|
||||
}
|
||||
if (prototype.default_value === undefined) {
|
||||
logger.warn(`Ignoring ${prototype.name} with missing default_value`);
|
||||
continue;
|
||||
}
|
||||
if (this.settings[prototype.setting_type].get(prototype.name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.settings[prototype.setting_type].set(prototype.name, { value: prototype.default_value });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ModPack;
|
||||
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
|
||||
function jsonArray(ItemClass) {
|
||||
return {
|
||||
jsonSchema: {
|
||||
type: "array",
|
||||
items: ItemClass.jsonSchema,
|
||||
},
|
||||
|
||||
fromJSON(json) {
|
||||
return json.map(i => ItemClass.fromJSON(i));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function jsonPrimitive(type) {
|
||||
return {
|
||||
jsonSchema: { type },
|
||||
fromJSON(json) {
|
||||
return json;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
JsonBoolean: jsonPrimitive("boolean"),
|
||||
JsonNumber: jsonPrimitive("number"),
|
||||
JsonString: jsonPrimitive("string"),
|
||||
|
||||
jsonArray,
|
||||
jsonPrimitive,
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Shared data types used in Clusterio
|
||||
* @module lib/data
|
||||
* @author Hornwitser
|
||||
*/
|
||||
"use strict";
|
||||
const ExportManifest = require("./ExportManifest");
|
||||
const ModInfo = require("./ModInfo");
|
||||
const ModPack = require("./ModPack");
|
||||
const composites = require("./composites");
|
||||
const messagesCore = require("./messages_core");
|
||||
const messagesController = require("./messages_controller");
|
||||
const messagesHost = require("./messages_host");
|
||||
const messagesInstance = require("./messages_instance");
|
||||
const messagesMod = require("./messages_mod");
|
||||
const messagesUser = require("./messages_user");
|
||||
const version = require("./version");
|
||||
|
||||
module.exports = {
|
||||
ExportManifest,
|
||||
ModInfo,
|
||||
ModPack,
|
||||
...composites,
|
||||
...messagesCore,
|
||||
...messagesController,
|
||||
...messagesHost,
|
||||
...messagesInstance,
|
||||
...messagesMod,
|
||||
...messagesUser,
|
||||
...version,
|
||||
};
|
||||
@@ -0,0 +1,384 @@
|
||||
"use strict";
|
||||
const { JsonString } = require("./composites");
|
||||
|
||||
// TODO: remove after config refactor
|
||||
/** @memberof module:lib */
|
||||
class RawConfig {
|
||||
/** @type {object} */
|
||||
serializedConfig;
|
||||
|
||||
constructor(serializedConfig) {
|
||||
this.serializedConfig = serializedConfig;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["serializedConfig"],
|
||||
properties: {
|
||||
"serializedConfig": { type: "object" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.serializedConfig);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ControllerConfigGetRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.controller.get_config";
|
||||
static Response = RawConfig;
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class ControllerConfigSetFieldRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.controller.update_config";
|
||||
|
||||
/** @type {string} */
|
||||
field;
|
||||
/** @type {string} */
|
||||
value;
|
||||
|
||||
constructor(field, value) {
|
||||
this.field = field;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["field", "value"],
|
||||
properties: {
|
||||
"field": { type: "string" },
|
||||
"value": { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.field, json.value);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ControllerConfigSetPropRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.controller.update_config";
|
||||
|
||||
/** @type {string} */
|
||||
field;
|
||||
/** @type {string} */
|
||||
prop;
|
||||
/** @type {undefined} */
|
||||
value;
|
||||
|
||||
constructor(field, prop, value) {
|
||||
this.field = field;
|
||||
this.prop = prop;
|
||||
if (value) { this.value = value; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["field", "prop"],
|
||||
properties: {
|
||||
field: { type: "string" },
|
||||
prop: { type: "string" },
|
||||
value: {},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.field, json.prop, json.value);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class HostGenerateTokenRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.host.generate_token";
|
||||
|
||||
/** @type {?number} */
|
||||
hostId;
|
||||
|
||||
constructor(hostId) {
|
||||
this.hostId = hostId;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["hostId"],
|
||||
properties: {
|
||||
hostId: { type: ["integer", "null"] },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.hostId);
|
||||
}
|
||||
|
||||
static Response = JsonString;
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class HostConfigCreateRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.host.create_config";
|
||||
|
||||
/** @type {?number} */
|
||||
id;
|
||||
/** @type {?string} */
|
||||
name;
|
||||
/** @type {boolean} */
|
||||
generateToken;
|
||||
|
||||
constructor(id, name, generateToken) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.generateToken = generateToken;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["id", "name", "generateToken"],
|
||||
properties: {
|
||||
"id": { type: ["integer", "null"] },
|
||||
"name": { type: ["string", "null"] },
|
||||
"generateToken": { type: "boolean" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.id, json.name, json.generateToken);
|
||||
}
|
||||
|
||||
static Response = RawConfig;
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class LogSetSubscriptionsRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.log.follow";
|
||||
|
||||
/** @type {boolean} */
|
||||
all;
|
||||
/** @type {boolean} */
|
||||
controller;
|
||||
/** @type {array} */
|
||||
hostIds;
|
||||
/** @type {array} */
|
||||
instanceIds;
|
||||
/** @type {?string} */
|
||||
maxLevel;
|
||||
|
||||
constructor(all, controller, hostIds, instanceIds, maxLevel) {
|
||||
this.all = all;
|
||||
this.controller = controller;
|
||||
this.hostIds = hostIds;
|
||||
this.instanceIds = instanceIds;
|
||||
this.maxLevel = maxLevel;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["all", "controller", "hostIds", "instanceIds", "maxLevel"],
|
||||
properties: {
|
||||
all: { type: "boolean" },
|
||||
controller: { type: "boolean" },
|
||||
hostIds: {
|
||||
type: "array",
|
||||
items: { type: "integer" },
|
||||
},
|
||||
instanceIds: {
|
||||
type: "array",
|
||||
items: { type: "integer" },
|
||||
},
|
||||
maxLevel: { type: ["string", "null"] },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.all, json.controller, json.hostIds, json.instanceIds, json.maxLevel);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class LogQueryRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.log.query";
|
||||
|
||||
/** @type {boolean} */
|
||||
all;
|
||||
/** @type {boolean} */
|
||||
controller;
|
||||
/** @type {Array<number>} */
|
||||
hostIds;
|
||||
/** @type {Array<number>} */
|
||||
instanceIds;
|
||||
/** @type {?string} */
|
||||
maxLevel;
|
||||
/** @type {integer} */
|
||||
limit;
|
||||
/** @type {undefined} */
|
||||
order;
|
||||
|
||||
constructor(all, controller, hostIds, instanceIds, maxLevel, limit, order) {
|
||||
this.all = all;
|
||||
this.controller = controller;
|
||||
this.hostIds = hostIds;
|
||||
this.instanceIds = instanceIds;
|
||||
this.maxLevel = maxLevel;
|
||||
this.limit = limit;
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["all", "controller", "hostIds", "instanceIds", "maxLevel", "limit", "order"],
|
||||
properties: {
|
||||
all: { type: "boolean" },
|
||||
controller: { type: "boolean" },
|
||||
hostIds: {
|
||||
type: "array",
|
||||
items: { type: "integer" },
|
||||
},
|
||||
instanceIds: {
|
||||
type: "array",
|
||||
items: { type: "integer" },
|
||||
},
|
||||
maxLevel: { type: ["string", "null"] },
|
||||
limit: { type: "integer" },
|
||||
order: {
|
||||
enum: ["asc", "desc"],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(
|
||||
json.all, json.controller, json.hostIds, json.instanceIds, json.maxLevel, json.limit, json.order
|
||||
);
|
||||
}
|
||||
}
|
||||
LogQueryRequest.Response = class Response {
|
||||
/** @type {Array<object>} */
|
||||
log;
|
||||
|
||||
constructor(log) {
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["log"],
|
||||
properties: {
|
||||
log: {
|
||||
type: "array",
|
||||
items: { type: "object" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.log);
|
||||
}
|
||||
};
|
||||
|
||||
/** @memberof module:lib */
|
||||
class LogMessageEvent {
|
||||
static type = "event";
|
||||
static src = ["host", "controller"];
|
||||
static dst = ["controller", "control"];
|
||||
|
||||
/** @type {object} */
|
||||
info;
|
||||
|
||||
constructor(info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["info"],
|
||||
properties: {
|
||||
"info": {
|
||||
type: "object",
|
||||
required: ["level", "message"],
|
||||
properties: {
|
||||
"level": { type: "string" },
|
||||
"message": { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.info);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class DebugDumpWsRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.debug.dump_ws";
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class DebugWsMessageEvent {
|
||||
static type = "event";
|
||||
static src = "controller";
|
||||
static dst = "control";
|
||||
|
||||
/** @type {string} */
|
||||
direction;
|
||||
/** @type {string} */
|
||||
content;
|
||||
|
||||
constructor(direction, content) {
|
||||
this.direction = direction;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["direction", "content"],
|
||||
properties: {
|
||||
direction: { type: "string" },
|
||||
content: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.direction, json.content);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
RawConfig,
|
||||
ControllerConfigGetRequest,
|
||||
ControllerConfigSetFieldRequest,
|
||||
ControllerConfigSetPropRequest,
|
||||
HostGenerateTokenRequest,
|
||||
HostConfigCreateRequest,
|
||||
LogSetSubscriptionsRequest,
|
||||
LogQueryRequest,
|
||||
LogMessageEvent,
|
||||
DebugDumpWsRequest,
|
||||
DebugWsMessageEvent,
|
||||
};
|
||||
@@ -0,0 +1,889 @@
|
||||
"use strict";
|
||||
const libSchema = require("../schema");
|
||||
|
||||
|
||||
/** @memberof module:lib */
|
||||
class Address {
|
||||
/** Controller address */
|
||||
static controller = 0;
|
||||
/** Instance address */
|
||||
static instance = 1;
|
||||
/** Host address */
|
||||
static host = 2;
|
||||
/** Control address */
|
||||
static control = 3;
|
||||
/** Broadcast */
|
||||
static broadcast = 4;
|
||||
|
||||
/** @type {number} */
|
||||
type;
|
||||
|
||||
/** @type {string} */
|
||||
id;
|
||||
|
||||
/** @type {string|undefined} */
|
||||
requestId;
|
||||
|
||||
constructor(type, id, requestId) {
|
||||
if (typeof type !== "number") {
|
||||
throw Error(`Invalid type ${type}`);
|
||||
}
|
||||
this.type = type;
|
||||
this.id = id;
|
||||
this.requestId = requestId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from convenient shorthand notation to address.
|
||||
*
|
||||
* Supported notations and their corresponding address:
|
||||
* - `"controller"`: new Address(Address.controller, 0)
|
||||
* - `{ hostId: id }`: new Address(Address.host, id)
|
||||
* - `{ instanceId: id }`: new Address(Address.instance, id)
|
||||
* - `{ controlId: id }`: new Address(Address.control, id)
|
||||
* `"allHosts"`: new Address(Address.broadcast, Address.host)
|
||||
* `"allInstances"`: new Address(Address.broadcast, Address.instance)
|
||||
* `"allControls"`: new Address(Address.broadcast, Address.control)
|
||||
* @param {module:lib.Address|string|object} shorthand -
|
||||
* Shorthand to translate with.
|
||||
* @returns {module:lib.Address} Translated address.
|
||||
*/
|
||||
static fromShorthand(shorthand) {
|
||||
if (shorthand instanceof Address) { return shorthand; }
|
||||
if (shorthand === "controller") { return new Address(Address.controller, 0); }
|
||||
if (shorthand === "allHosts") { return new Address(Address.broadcast, Address.host); }
|
||||
if (shorthand === "allInstances") { return new Address(Address.broadcast, Address.instance); }
|
||||
if (shorthand === "allControls") { return new Address(Address.broadcast, Address.control); }
|
||||
let entries = Object.entries(shorthand);
|
||||
if (entries.length === 1) {
|
||||
let [name, value] = entries[0];
|
||||
if (name === "hostId") { return new Address(Address.host, value); }
|
||||
if (name === "instanceId") { return new Address(Address.instance, value); }
|
||||
if (name === "controlId") { return new Address(Address.control, value); }
|
||||
}
|
||||
throw Error("Unrecognized shorthand");
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "array",
|
||||
minItems: 2,
|
||||
items: [
|
||||
{ type: "integer", minimum: 0, maximum: 4 },
|
||||
{ type: "integer" },
|
||||
{ type: "integer" },
|
||||
],
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json[0], json[1], json[2]);
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
if (this.requestId !== undefined) {
|
||||
return [this.type, this.id, this.requestId];
|
||||
}
|
||||
return [this.type, this.id];
|
||||
}
|
||||
|
||||
toString() {
|
||||
const typeMap = ["controller", "instance", "host", "control"];
|
||||
const type = typeMap[this.type] || this.type;
|
||||
if (this.type === Address.broadcast) {
|
||||
return `[Address ${type}:${typeMap[this.id] || this.id}]`;
|
||||
}
|
||||
if (this.requestId === undefined) {
|
||||
return `[Address ${type}:${this.id}]`;
|
||||
}
|
||||
return `[Address ${type}:${this.id}:${this.requestId}]`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string suitable for indexing the address in a Map
|
||||
* @returns {string} unique string for this address
|
||||
*/
|
||||
index() {
|
||||
return `${this.type}:${this.id}:${this.requestId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this address targets the given destination
|
||||
* @param {module:lib.Address} dst - Destination to check
|
||||
* @returns {boolean} true if this address is addressed to the given destination
|
||||
*/
|
||||
addressedTo(dst) {
|
||||
if (this.type !== Address.broadcast) {
|
||||
return (
|
||||
this.type === dst.type
|
||||
&& this.id === dst.id
|
||||
);
|
||||
}
|
||||
return dst.type !== Address.broadcast && this.id === dst.type;
|
||||
}
|
||||
|
||||
equals(other) {
|
||||
return (
|
||||
this.type === other.type
|
||||
&& this.id === other.id
|
||||
&& this.requestId === other.requestId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class Message {
|
||||
/** @type {string} */
|
||||
type;
|
||||
|
||||
constructor(type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
static jsonSchema;
|
||||
static validate;
|
||||
|
||||
static fromJSON(json) {
|
||||
/* eslint-disable no-use-before-define */
|
||||
if (json.type === "hello") { return MessageHello.fromJSON(json); }
|
||||
if (json.type === "registerHost") { return MessageRegisterHost.fromJSON(json); }
|
||||
if (json.type === "registerControl") { return MessageRegisterControl.fromJSON(json); }
|
||||
if (json.type === "ready") { return MessageReady.fromJSON(json); }
|
||||
if (json.type === "resume") { return MessageResume.fromJSON(json); }
|
||||
if (json.type === "continue") { return MessageContinue.fromJSON(json); }
|
||||
if (json.type === "invalidate") { return MessageInvalidate.fromJSON(json); }
|
||||
if (json.type === "heartbeat") { return MessageHeartbeat.fromJSON(json); }
|
||||
if (json.type === "request") { return MessageRequest.fromJSON(json); }
|
||||
if (json.type === "response") { return MessageResponse.fromJSON(json); }
|
||||
if (json.type === "responseError") { return MessageResponseError.fromJSON(json); }
|
||||
if (json.type === "event") { return MessageEvent.fromJSON(json); }
|
||||
if (json.type === "disconnect") { return MessageDisconnect.fromJSON(json); }
|
||||
/* eslint-enable no-use-before-define */
|
||||
throw new Error(`Unrecognized message type ${json.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class HelloData {
|
||||
/** @type {string} */
|
||||
version;
|
||||
/** @type {object} */
|
||||
plugins;
|
||||
|
||||
constructor(version, plugins) {
|
||||
this.version = version;
|
||||
this.plugins = plugins;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["version", "plugins"],
|
||||
properties: {
|
||||
"version": { type: "string" },
|
||||
"plugins": {
|
||||
type: "object",
|
||||
additionalProperties: { type: "string" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.version, json.plugins);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageHello extends Message {
|
||||
/** @type {module:lib.HelloData} */
|
||||
data;
|
||||
|
||||
constructor(data) {
|
||||
super("hello");
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["type", "data"],
|
||||
properties: {
|
||||
"type": { const: "hello" },
|
||||
"data": HelloData.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(HelloData.fromJSON(json.data));
|
||||
}
|
||||
};
|
||||
|
||||
/** @memberof module:lib */
|
||||
class RegisterHostData {
|
||||
/** @type {string} */
|
||||
token;
|
||||
/** @type {string} */
|
||||
agent;
|
||||
/** @type {string} */
|
||||
version;
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {string} */
|
||||
id;
|
||||
/** @type {string|undefined} */
|
||||
publicAddress;
|
||||
/** @type {Object<string, string>} */
|
||||
plugins;
|
||||
|
||||
constructor(token, agent, version, name, id, publicAddress, plugins) {
|
||||
this.token = token;
|
||||
this.agent = agent;
|
||||
this.version = version;
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
this.publicAddress = publicAddress;
|
||||
this.plugins = plugins;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["token", "agent", "version", "name", "id", "plugins"],
|
||||
properties: {
|
||||
"token": { type: "string" },
|
||||
"agent": { type: "string" },
|
||||
"version": { type: "string" },
|
||||
"name": { type: "string" },
|
||||
"id": { type: "integer" },
|
||||
"publicAddress": { type: "string" },
|
||||
"plugins": {
|
||||
type: "object",
|
||||
additionalProperties: { type: "string" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.token, json.agent, json.version, json.name, json.id, json.publicAddress, json.plugins);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageRegisterHost extends Message {
|
||||
/** @type {module:lib.RegisterHostData} */
|
||||
data;
|
||||
|
||||
constructor(data) {
|
||||
super("registerHost");
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["type", "data"],
|
||||
properties: {
|
||||
"type": { const: "registerHost" },
|
||||
"data": RegisterHostData.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(RegisterHostData.fromJSON(json.data));
|
||||
}
|
||||
};
|
||||
|
||||
/** @memberof module:lib */
|
||||
class RegisterControlData {
|
||||
/** @type {string} */
|
||||
token;
|
||||
/** @type {string} */
|
||||
agent;
|
||||
/** @type {string} */
|
||||
version;
|
||||
|
||||
constructor(token, agent, version) {
|
||||
this.token = token;
|
||||
this.agent = agent;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["token", "agent", "version"],
|
||||
properties: {
|
||||
"token": { type: "string" },
|
||||
"agent": { type: "string" },
|
||||
"version": { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.token, json.agent, json.version);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageRegisterControl extends Message {
|
||||
/** @type {module:lib.RegisterControlData} */
|
||||
data;
|
||||
|
||||
constructor(data) {
|
||||
super("registerControl");
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["data"],
|
||||
properties: {
|
||||
"type": { const: "registerControl" },
|
||||
"data": RegisterControlData.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(RegisterControlData.fromJSON(json.data));
|
||||
}
|
||||
};
|
||||
|
||||
/** @memberof module:lib */
|
||||
class AccountDetails {
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {Array} */
|
||||
roles;
|
||||
|
||||
constructor(name, roles) {
|
||||
this.name = name;
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "roles"],
|
||||
properties: {
|
||||
"name": { type: "string" },
|
||||
"roles": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
required: ["name", "id", "permissions"],
|
||||
properties: {
|
||||
"name": { type: "string" },
|
||||
"id": { type: "integer" },
|
||||
"permissions": {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.roles);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class ReadyData {
|
||||
/** @type {module:lib.Address} */
|
||||
src;
|
||||
/** @type {string} */
|
||||
sessionToken;
|
||||
/** @type {number} */
|
||||
sessionTimeout;
|
||||
/** @type {number} */
|
||||
heartbeatInterval;
|
||||
/** @type {AccountDetails|undefined} */
|
||||
account;
|
||||
|
||||
constructor(src, sessionToken, sessionTimeout, heartbeatInterval, account) {
|
||||
this.src = src;
|
||||
this.sessionToken = sessionToken;
|
||||
this.sessionTimeout = sessionTimeout;
|
||||
this.heartbeatInterval = heartbeatInterval;
|
||||
if (account) { this.account = account; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["src", "sessionToken", "sessionTimeout", "heartbeatInterval"],
|
||||
properties: {
|
||||
"src": Address.jsonSchema,
|
||||
"sessionToken": { type: "string" },
|
||||
"sessionTimeout": { type: "number" },
|
||||
"heartbeatInterval": { type: "number" },
|
||||
"account": AccountDetails.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
let account;
|
||||
if (json.account) { account = AccountDetails.fromJSON(json.account); }
|
||||
return new this(
|
||||
Address.fromJSON(json.src),
|
||||
json.sessionToken,
|
||||
json.sessionTimeout,
|
||||
json.heartbeatInterval,
|
||||
account
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageReady extends Message {
|
||||
/** @type {module:lib.ReadyData} */
|
||||
data;
|
||||
|
||||
constructor(data) {
|
||||
super("ready");
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["data"],
|
||||
properties: {
|
||||
"type": { const: "ready" },
|
||||
"data": ReadyData.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(ReadyData.fromJSON(json.data));
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class ResumeData {
|
||||
/** @type {string} */
|
||||
sessionToken;
|
||||
/** @type {integer|undefined} */
|
||||
lastSeq;
|
||||
|
||||
constructor(sessionToken, lastSeq) {
|
||||
this.sessionToken = sessionToken;
|
||||
if (lastSeq !== undefined) { this.lastSeq = lastSeq; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["sessionToken"],
|
||||
properties: {
|
||||
"sessionToken": { type: "string" },
|
||||
"lastSeq": { type: "integer" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.sessionToken, json.lastSeq);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageResume extends Message {
|
||||
/** @type {module:lib.ResumeData} */
|
||||
data;
|
||||
|
||||
constructor(data) {
|
||||
super("resume");
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["data"],
|
||||
properties: {
|
||||
"type": { const: "resume" },
|
||||
"data": ResumeData.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(ResumeData.fromJSON(json.data));
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class ContinueData {
|
||||
/** @type {number} */
|
||||
sessionTimeout;
|
||||
/** @type {number} */
|
||||
heartbeatInterval;
|
||||
/** @type {integer|undefined} */
|
||||
lastSeq;
|
||||
|
||||
constructor(sessionTimeout, heartbeatInterval, lastSeq) {
|
||||
this.sessionTimeout = sessionTimeout;
|
||||
this.heartbeatInterval = heartbeatInterval;
|
||||
if (lastSeq !== undefined) { this.lastSeq = lastSeq; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["sessionTimeout", "heartbeatInterval"],
|
||||
properties: {
|
||||
"sessionTimeout": { type: "number" },
|
||||
"heartbeatInterval": { type: "number" },
|
||||
"lastSeq": { type: "integer" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.sessionTimeout, json.heartbeatInterval, json.lastSeq);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageContinue extends Message {
|
||||
/** @type {module:lib.ContinueData} */
|
||||
data;
|
||||
|
||||
constructor(data) {
|
||||
super("continue");
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["data"],
|
||||
properties: {
|
||||
"type": { const: "continue" },
|
||||
"data": ContinueData.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(ContinueData.fromJSON(json.data));
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageInvalidate extends Message {
|
||||
constructor() {
|
||||
super("invalidate");
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
"type": { const: "invalidate" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this();
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageHeartbeat extends Message {
|
||||
/** @type {integer|undefined} */
|
||||
seq;
|
||||
|
||||
constructor(seq) {
|
||||
super("heartbeat");
|
||||
this.seq = seq;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["type"],
|
||||
properties: {
|
||||
"type": { const: "heartbeat" },
|
||||
"seq": { type: "integer" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.seq);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageRequest extends Message {
|
||||
/** @type {number} */
|
||||
seq;
|
||||
/** @type {Address} */
|
||||
src;
|
||||
/** @type {Address} */
|
||||
dst;
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {*} */
|
||||
data;
|
||||
|
||||
constructor(seq, src, dst, name, data) {
|
||||
super("request");
|
||||
this.seq = seq;
|
||||
this.src = src;
|
||||
this.dst = dst;
|
||||
this.name = name;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["type", "seq", "src", "dst", "name"],
|
||||
properties: {
|
||||
"type": { const: "request" },
|
||||
"seq": { type: "integer" },
|
||||
"src": Address.jsonSchema,
|
||||
"dst": Address.jsonSchema,
|
||||
"name": { type: "string" },
|
||||
"data": {},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(
|
||||
json.seq,
|
||||
Address.fromJSON(json.src),
|
||||
Address.fromJSON(json.dst),
|
||||
json.name,
|
||||
json.data,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageResponse extends Message {
|
||||
/** @type {number} */
|
||||
seq;
|
||||
/** @type {Address} */
|
||||
src;
|
||||
/** @type {Address} */
|
||||
dst;
|
||||
/** @type {*} */
|
||||
data;
|
||||
|
||||
constructor(seq, src, dst, data) {
|
||||
super("response");
|
||||
this.seq = seq;
|
||||
this.src = src;
|
||||
this.dst = dst;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["type", "seq", "src", "dst"],
|
||||
properties: {
|
||||
"type": { const: "response" },
|
||||
"seq": { type: "integer" },
|
||||
"src": Address.jsonSchema,
|
||||
"dst": Address.jsonSchema,
|
||||
"data": {},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(
|
||||
json.seq,
|
||||
Address.fromJSON(json.src),
|
||||
Address.fromJSON(json.dst),
|
||||
json.data,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/** @memberof module:lib */
|
||||
class ResponseError {
|
||||
/** @type {string} */
|
||||
message;
|
||||
/** @type {string|undefined} */
|
||||
code;
|
||||
/** @type {string|undefined} */
|
||||
stack;
|
||||
|
||||
constructor(message, code, stack) {
|
||||
this.message = message;
|
||||
if (code) { this.code = code; }
|
||||
if (stack) { this.stack = stack; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["message"],
|
||||
properties: {
|
||||
"message": { type: "string" },
|
||||
"code": { type: "string" },
|
||||
"stack": { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.message, json.code, json.stack);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageResponseError extends Message {
|
||||
/** @type {number} */
|
||||
seq;
|
||||
/** @type {Address} */
|
||||
src;
|
||||
/** @type {Address} */
|
||||
dst;
|
||||
/** @type {module:lib.ResponseError} */
|
||||
data;
|
||||
|
||||
constructor(seq, src, dst, data) {
|
||||
super("responseError");
|
||||
this.seq = seq;
|
||||
this.src = src;
|
||||
this.dst = dst;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["type", "seq", "src", "dst", "data"],
|
||||
properties: {
|
||||
"type": { const: "responseError" },
|
||||
"seq": { type: "integer" },
|
||||
"src": Address.jsonSchema,
|
||||
"dst": Address.jsonSchema,
|
||||
"data": ResponseError.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(
|
||||
json.seq,
|
||||
Address.fromJSON(json.src),
|
||||
Address.fromJSON(json.dst),
|
||||
ResponseError.fromJSON(json.data),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageEvent extends Message {
|
||||
/** @type {number} */
|
||||
seq;
|
||||
/** @type {Address} */
|
||||
src;
|
||||
/** @type {Address} */
|
||||
dst;
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {*} */
|
||||
data;
|
||||
|
||||
constructor(seq, src, dst, name, data) {
|
||||
super("event");
|
||||
this.seq = seq;
|
||||
this.src = src;
|
||||
this.dst = dst;
|
||||
this.name = name;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["type", "seq", "src", "dst", "name"],
|
||||
properties: {
|
||||
"type": { const: "event" },
|
||||
"seq": { type: "integer" },
|
||||
"src": Address.jsonSchema,
|
||||
"dst": Address.jsonSchema,
|
||||
"name": { type: "string" },
|
||||
"data": {},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(
|
||||
json.seq,
|
||||
Address.fromJSON(json.src),
|
||||
Address.fromJSON(json.dst),
|
||||
json.name,
|
||||
json.data,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/** @memberof module:lib */
|
||||
class MessageDisconnect extends Message {
|
||||
/** @type {string} */
|
||||
data;
|
||||
|
||||
constructor(stage) {
|
||||
super("disconnect");
|
||||
this.data = stage;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["type"],
|
||||
properties: {
|
||||
"type": { const: "disconnect" },
|
||||
"data": { enum: ["prepare", "ready"] },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.data);
|
||||
}
|
||||
};
|
||||
|
||||
Message.jsonSchema = {
|
||||
type: "object",
|
||||
required: ["type"],
|
||||
properties: {
|
||||
"type": { enum: [
|
||||
"hello", "registerHost", "registerControl", "ready", "resume", "continue", "invalidate",
|
||||
"heartbeat", "request", "response", "responseError", "event", "disconnect",
|
||||
] },
|
||||
},
|
||||
oneOf: [
|
||||
MessageHello.jsonSchema,
|
||||
MessageRegisterHost.jsonSchema,
|
||||
MessageRegisterControl.jsonSchema,
|
||||
MessageReady.jsonSchema,
|
||||
MessageResume.jsonSchema,
|
||||
MessageContinue.jsonSchema,
|
||||
MessageInvalidate.jsonSchema,
|
||||
MessageHeartbeat.jsonSchema,
|
||||
MessageRequest.jsonSchema,
|
||||
MessageResponse.jsonSchema,
|
||||
MessageResponseError.jsonSchema,
|
||||
MessageEvent.jsonSchema,
|
||||
MessageDisconnect.jsonSchema,
|
||||
],
|
||||
};
|
||||
Message.validate = libSchema.compile(Message.jsonSchema);
|
||||
|
||||
/** @memberof module:lib */
|
||||
class PingRequest {
|
||||
static type = "request";
|
||||
static src = ["controller", "host", "control"];
|
||||
static dst = ["controller", "host", "control"];
|
||||
static permission = null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Message,
|
||||
HelloData,
|
||||
MessageHello,
|
||||
RegisterHostData,
|
||||
MessageRegisterHost,
|
||||
RegisterControlData,
|
||||
MessageRegisterControl,
|
||||
AccountDetails,
|
||||
ReadyData,
|
||||
MessageReady,
|
||||
ResumeData,
|
||||
MessageResume,
|
||||
ContinueData,
|
||||
MessageContinue,
|
||||
MessageInvalidate,
|
||||
MessageHeartbeat,
|
||||
Address,
|
||||
MessageRequest,
|
||||
MessageResponse,
|
||||
ResponseError,
|
||||
MessageResponseError,
|
||||
MessageEvent,
|
||||
MessageDisconnect,
|
||||
PingRequest,
|
||||
};
|
||||
@@ -0,0 +1,243 @@
|
||||
"use strict";
|
||||
const { jsonArray } = require("./composites");
|
||||
|
||||
|
||||
/** @memberof module:lib */
|
||||
class HostDetails {
|
||||
/** @type {string} */
|
||||
agent;
|
||||
/** @type {string} */
|
||||
version;
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {number} */
|
||||
id;
|
||||
/** @type {boolean} */
|
||||
connected;
|
||||
/** @type {string|undefined} */
|
||||
publicAddress;
|
||||
|
||||
constructor(agent, version, name, id, connected, publicAddress) {
|
||||
this.agent = agent;
|
||||
this.version = version;
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
this.connected = connected;
|
||||
if (publicAddress !== undefined) { this.publicAddress = publicAddress; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["agent", "version", "name", "id", "connected"],
|
||||
properties: {
|
||||
"agent": { type: "string" },
|
||||
"version": { type: "string" },
|
||||
"name": { type: "string" },
|
||||
"id": { type: "integer" },
|
||||
"connected": { type: "boolean" },
|
||||
"publicAddress": { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.agent, json.version, json.name, json.id, json.connected, json.publicAddress);
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class HostListRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.host.list";
|
||||
static Response = jsonArray(HostDetails);
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class HostSetSubscriptionsRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.host.subscribe";
|
||||
|
||||
/** @type {boolean} */
|
||||
all;
|
||||
/** @type {Array<number>} */
|
||||
hostIds;
|
||||
|
||||
constructor(all, hostIds) {
|
||||
this.all = all;
|
||||
this.hostIds = hostIds;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["all", "hostIds"],
|
||||
properties: {
|
||||
all: { type: "boolean" },
|
||||
hostIds: {
|
||||
type: "array",
|
||||
items: { type: "integer" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.all, json.hostIds);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* @memberof module:lib */
|
||||
class HostUpdateEvent {
|
||||
static type = "event";
|
||||
static src = "controller";
|
||||
static dst = "control";
|
||||
|
||||
/** @type {module:lib.HostDetails} */
|
||||
update;
|
||||
|
||||
constructor(update) {
|
||||
this.update = update;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["update"],
|
||||
properties: {
|
||||
update: HostDetails.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(HostDetails.fromJSON(json.update));
|
||||
}
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class HostMetricsRequest {
|
||||
static type = "request";
|
||||
static src = "controller";
|
||||
static dst = "host";
|
||||
}
|
||||
HostMetricsRequest.Response = class Response { // TODO: Use JSON class pattern in Prometheus
|
||||
/** @type {array} */
|
||||
results;
|
||||
|
||||
constructor(results) {
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["results"],
|
||||
properties: {
|
||||
"results": {
|
||||
type: "array",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.results);
|
||||
}
|
||||
};
|
||||
|
||||
/** @memberof module:lib */
|
||||
class ControllerConnectionEvent {
|
||||
static type = "event";
|
||||
static src = "host";
|
||||
static dst = "instance";
|
||||
|
||||
/** @type {string} */
|
||||
event;
|
||||
|
||||
constructor(event) {
|
||||
this.event = event;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["event"],
|
||||
properties: {
|
||||
"event": { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.event);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class PrepareControllerDisconnectRequest {
|
||||
static type = "request";
|
||||
static src = "host";
|
||||
static dst = "instance";
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class SyncUserListsEvent {
|
||||
static type = "event";
|
||||
static src = "controller";
|
||||
static dst = "host";
|
||||
|
||||
/** @type {Set<string>} */
|
||||
adminlist;
|
||||
/** @type {Map<string, string>} */
|
||||
banlist;
|
||||
/** @type {Set<string>} */
|
||||
whitelist;
|
||||
|
||||
constructor(adminlist, banlist, whitelist) {
|
||||
this.adminlist = adminlist;
|
||||
this.banlist = banlist;
|
||||
this.whitelist = whitelist;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["adminlist", "banlist", "whitelist"],
|
||||
properties: {
|
||||
"adminlist": {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
"banlist": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "array",
|
||||
minItems: 2,
|
||||
items: [{ type: "string" }, { type: "string" }],
|
||||
},
|
||||
},
|
||||
"whitelist": {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(new Set(json.adminlist), new Set(json.banlist), new Set(json.whitelist));
|
||||
}
|
||||
|
||||
static toJSON() {
|
||||
return {
|
||||
adminlist: [...this.adminlist],
|
||||
banlist: [...this.banlist],
|
||||
whitelist: [...this.whitelist],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
HostDetails,
|
||||
HostListRequest,
|
||||
HostSetSubscriptionsRequest,
|
||||
HostUpdateEvent,
|
||||
HostMetricsRequest,
|
||||
ControllerConnectionEvent,
|
||||
PrepareControllerDisconnectRequest,
|
||||
SyncUserListsEvent,
|
||||
};
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,474 @@
|
||||
"use strict";
|
||||
const ModPack = require("./ModPack");
|
||||
const ModInfo = require("./ModInfo");
|
||||
const { jsonArray, JsonString } = require("./composites");
|
||||
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModPackGetRequest {
|
||||
static type = "request";
|
||||
static src = ["instance", "control"];
|
||||
static dst = "controller";
|
||||
static permission = "core.mod_pack.get";
|
||||
|
||||
/** @type {number} */
|
||||
id;
|
||||
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "integer" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.id);
|
||||
}
|
||||
|
||||
static Response = ModPack;
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModPackGetDefaultRequest {
|
||||
static type = "request";
|
||||
static src = ["instance", "control"];
|
||||
static dst = "controller";
|
||||
static permission = "core.mod_pack.get";
|
||||
static Response = ModPack;
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModPackListRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod_pack.list";
|
||||
static Response = jsonArray(ModPack);
|
||||
}
|
||||
|
||||
/** @memberof module:lib */
|
||||
class ModPackCreateRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod_pack.create";
|
||||
|
||||
/** @type {module:lib.ModPack} */
|
||||
modPack;
|
||||
|
||||
constructor(modPack) {
|
||||
this.modPack = modPack;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["modPack"],
|
||||
properties: {
|
||||
"modPack": ModPack.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(ModPack.fromJSON(json.modPack));
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModPackUpdateRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod_pack.update";
|
||||
|
||||
/** @type {module:lib.ModPack} */
|
||||
modPack;
|
||||
|
||||
constructor(modPack) {
|
||||
this.modPack = modPack;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["modPack"],
|
||||
properties: {
|
||||
"modPack": ModPack.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(ModPack.fromJSON(json.modPack));
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModPackDeleteRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod_pack.delete";
|
||||
|
||||
/** @type {number} */
|
||||
id;
|
||||
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "integer" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.id);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModPackSetSubscriptionsRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod_pack.subscribe";
|
||||
|
||||
/** @type {boolean} */
|
||||
all;
|
||||
/** @type {Array<number>} */
|
||||
modPackIds;
|
||||
|
||||
constructor(all, modPackIds) {
|
||||
this.all = all;
|
||||
this.modPackIds = modPackIds;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["all", "modPackIds"],
|
||||
properties: {
|
||||
all: { type: "boolean" },
|
||||
modPackIds: {
|
||||
type: "array",
|
||||
items: { type: "integer" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.all, json.modPackIds);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModGetRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod.get";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {string} */
|
||||
version;
|
||||
|
||||
constructor(name, version) {
|
||||
this.name = name;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "version"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
version: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.version);
|
||||
}
|
||||
|
||||
static Response = ModInfo;
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModListRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod.list";
|
||||
static Response = jsonArray(ModInfo);
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModSearchRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod.search";
|
||||
|
||||
/** @type {string} */
|
||||
query;
|
||||
/** @type {string} */
|
||||
factorioVersion;
|
||||
/** @type {number} */
|
||||
page;
|
||||
/** @type {number|undefined} */
|
||||
pageSize;
|
||||
/** @type {string|undefined} */
|
||||
sort;
|
||||
/** @type {string|undefined} */
|
||||
sortOrder;
|
||||
|
||||
constructor(query, factorioVersion, page, pageSize, sort, sortOrder) {
|
||||
this.query = query;
|
||||
this.factorioVersion = factorioVersion;
|
||||
this.page = page;
|
||||
if (pageSize) { this.pageSize = pageSize; }
|
||||
if (sort) { this.sort = sort; }
|
||||
if (sortOrder) { this.sortOrder = sortOrder; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["query", "factorioVersion", "page"],
|
||||
properties: {
|
||||
query: { type: "string" },
|
||||
factorioVersion: { type: "string" },
|
||||
pageSize: { type: "integer" },
|
||||
page: { type: "integer" },
|
||||
sort: { type: "string" },
|
||||
sortOrder: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.query, json.factorioVersion, json.page, json.pageSize, json.sort, json.sortOrder);
|
||||
}
|
||||
}
|
||||
ModSearchRequest.Response = class Response {
|
||||
/** @type {Array<string>} */
|
||||
queryIssues;
|
||||
/** @type {number} */
|
||||
pageCount;
|
||||
/** @type {number} */
|
||||
resultCount;
|
||||
/** @type {Array<object>} */
|
||||
results;
|
||||
|
||||
constructor(queryIssues, pageCount, resultCount, results) {
|
||||
this.queryIssues = queryIssues;
|
||||
this.pageCount = pageCount;
|
||||
this.resultCount = resultCount;
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["queryIssues", "pageCount", "resultCount", "results"],
|
||||
properties: {
|
||||
queryIssues: {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
pageCount: { type: "integer" },
|
||||
resultCount: { type: "integer" },
|
||||
results: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
versions: {
|
||||
type: "array",
|
||||
items: ModInfo.jsonSchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
let results = json.results.map(
|
||||
({ name, versions }) => ({ name, versions: versions.map(mod => ModInfo.fromJSON(mod)) })
|
||||
);
|
||||
return new this(json.queryIssues, json.pageCount, json.resultCount, results);
|
||||
}
|
||||
};
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModSetSubscriptionsRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod.subscribe";
|
||||
|
||||
/** @type {boolean} */
|
||||
all;
|
||||
/** @type {Array<string>} */
|
||||
modNames;
|
||||
|
||||
constructor(all, modNames) {
|
||||
this.all = all;
|
||||
this.modNames = modNames;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["all", "modNames"],
|
||||
properties: {
|
||||
all: { type: "boolean" },
|
||||
modNames: {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.all, json.modNames);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModDownloadRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod.download";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {string} */
|
||||
version;
|
||||
|
||||
constructor(name, version) {
|
||||
this.name = name;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "version"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
version: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.version);
|
||||
}
|
||||
|
||||
static Response = JsonString;
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModDeleteRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.mod.delete";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {string} */
|
||||
version;
|
||||
|
||||
constructor(name, version) {
|
||||
this.name = name;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "version"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
version: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.version);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModPackUpdateEvent {
|
||||
static type = "event";
|
||||
static src = "controller";
|
||||
static dst = "control";
|
||||
|
||||
/** @type {module:lib.ModPack} */
|
||||
modPack;
|
||||
|
||||
constructor(modPack) {
|
||||
this.modPack = modPack;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["modPack"],
|
||||
properties: {
|
||||
modPack: ModPack.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(ModPack.fromJSON(json.modPack));
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class ModUpdateEvent {
|
||||
static type = "event";
|
||||
static src = "controller";
|
||||
static dst = "control";
|
||||
|
||||
/** @type {module:lib.ModInfo} */
|
||||
mod;
|
||||
|
||||
constructor(mod) {
|
||||
this.mod = mod;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["mod"],
|
||||
properties: {
|
||||
mod: ModInfo.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(ModInfo.fromJSON(json.mod));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ModPackGetRequest,
|
||||
ModPackGetDefaultRequest,
|
||||
ModPackListRequest,
|
||||
ModPackCreateRequest,
|
||||
ModPackUpdateRequest,
|
||||
ModPackDeleteRequest,
|
||||
ModPackSetSubscriptionsRequest,
|
||||
ModGetRequest,
|
||||
ModListRequest,
|
||||
ModSearchRequest,
|
||||
ModSetSubscriptionsRequest,
|
||||
ModDownloadRequest,
|
||||
ModDeleteRequest,
|
||||
ModPackUpdateEvent,
|
||||
ModUpdateEvent,
|
||||
};
|
||||
@@ -0,0 +1,708 @@
|
||||
"use strict";
|
||||
const PlayerStats = require("../PlayerStats");
|
||||
const { jsonArray, JsonNumber } = require("./composites");
|
||||
|
||||
/* @memberof module:lib */
|
||||
class RawPermission { // TODO refactor into lib/user.Permission
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {string} */
|
||||
title;
|
||||
/** @type {string} */
|
||||
description;
|
||||
|
||||
constructor(name, title, description) {
|
||||
this.name = name;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "title", "description"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
title: { type: "string" },
|
||||
description: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.title, json.description);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class PermissionListRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.permission.list";
|
||||
static Response = jsonArray(RawPermission);
|
||||
}
|
||||
|
||||
class RawRole { // TODO refactor into lib/user.Role
|
||||
/** @type {number} */
|
||||
id;
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {string} */
|
||||
description;
|
||||
/** @type {Array<string>} */
|
||||
permissions;
|
||||
|
||||
constructor(id, name, description, permissions) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.permissions = permissions;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["id", "name", "description", "permissions"],
|
||||
properties: {
|
||||
id: { type: "integer" },
|
||||
name: { type: "string" },
|
||||
description: { type: "string" },
|
||||
permissions: {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.id, json.name, json.description, json.permissions);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class RoleListRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.role.list";
|
||||
static Response = jsonArray(RawRole);
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class RoleCreateRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.role.create";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {string} */
|
||||
description;
|
||||
/** @type {Array<string>} */
|
||||
permissions;
|
||||
|
||||
constructor(name, description, permissions) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.permissions = permissions;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "description", "permissions"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
description: { type: "string" },
|
||||
permissions: {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.description, json.permissions);
|
||||
}
|
||||
|
||||
static Response = JsonNumber;
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class RoleUpdateRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.role.update";
|
||||
|
||||
/** @type {number} */
|
||||
id;
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {string} */
|
||||
description;
|
||||
/** @type {Array<string>} */
|
||||
permissions;
|
||||
|
||||
constructor(id, name, description, permissions) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.permissions = permissions;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["id", "name", "description", "permissions"],
|
||||
properties: {
|
||||
id: { type: "integer" },
|
||||
name: { type: "string" },
|
||||
description: { type: "string" },
|
||||
permissions: {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.id, json.name, json.description, json.permissions);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class RoleGrantDefaultPermissionsRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.role.update";
|
||||
|
||||
/** @type {number} */
|
||||
id;
|
||||
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "integer" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.id);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class RoleDeleteRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.role.delete";
|
||||
|
||||
/** @type {number} */
|
||||
id;
|
||||
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "integer" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.id);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class RawUser { // TODO refactor into lib/user.User
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {Array<number>} */
|
||||
roles;
|
||||
/** @type {Array<number>} */
|
||||
instances;
|
||||
/** @type {boolean|undefined} */
|
||||
isAdmin;
|
||||
/** @type {boolean|undefined} */
|
||||
isBanned;
|
||||
/** @type {boolean|undefined} */
|
||||
isWhitelisted;
|
||||
/** @type {string|undefined} */
|
||||
banReason;
|
||||
/** @type {boolean|undefined} */
|
||||
isDeleted;
|
||||
/** @type {module:lib.PlayerStats} */
|
||||
playerStats;
|
||||
/** @type {Map<number, module:lib.PlayerStats>|undefined} */
|
||||
instanceStats;
|
||||
|
||||
constructor(
|
||||
name, roles, instances, isAdmin, isBanned, isWhitelisted, banReason,
|
||||
isDeleted, playerStats, instanceStats
|
||||
) {
|
||||
this.name = name;
|
||||
this.roles = roles;
|
||||
this.instances = instances;
|
||||
if (isAdmin !== undefined) { this.isAdmin = isAdmin; }
|
||||
if (isBanned !== undefined) { this.isBanned = isBanned; }
|
||||
if (isWhitelisted !== undefined) { this.isWhitelisted = isWhitelisted; }
|
||||
if (banReason !== undefined) { this.banReason = banReason; }
|
||||
if (isDeleted !== undefined) { this.isDeleted = isDeleted; }
|
||||
if (playerStats !== undefined) { this.playerStats = playerStats; }
|
||||
if (instanceStats !== undefined) { this.instanceStats = instanceStats; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "roles", "instances"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
roles: { type: "array", items: { type: "integer" } },
|
||||
instances: { type: "array", items: { type: "integer" } },
|
||||
isAdmin: { type: "boolean" },
|
||||
isBanned: { type: "boolean" },
|
||||
isWhitelisted: { type: "boolean" },
|
||||
banReason: { type: "string" },
|
||||
isDeleted: { type: "boolean" },
|
||||
playerStats: PlayerStats.jsonSchema,
|
||||
instanceStats: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "array",
|
||||
minItems: 2,
|
||||
items: [{ type: "integer" }, PlayerStats.jsonSchema],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
let playerStats;
|
||||
if (json.playerStats) {
|
||||
playerStats = PlayerStats.fromJSON(json.playerStats);
|
||||
}
|
||||
let instanceStats;
|
||||
if (json.instanceStats) {
|
||||
instanceStats = new Map(
|
||||
json.instanceStats.map(([id, stats]) => [id, PlayerStats.fromJSON(stats)])
|
||||
);
|
||||
}
|
||||
return new this(
|
||||
json.name, json.roles, json.instances, json.isAdmin, json.isBanned, json.isWhitelisted, json.banReason,
|
||||
json.isDeleted, playerStats, instanceStats
|
||||
);
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
const json = {
|
||||
name: this.name,
|
||||
roles: this.roles,
|
||||
instances: this.instances,
|
||||
isAdmin: this.isAdmin,
|
||||
isBanned: this.isBanned,
|
||||
isWhitelisted: this.isWhitelisted,
|
||||
banReason: this.banReason,
|
||||
isDeleted: this.isDeleted,
|
||||
playerStats: this.playerStats,
|
||||
};
|
||||
if (this.instanceStats) {
|
||||
json.instanceStats = [...this.instanceStats];
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserGetRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.get";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name);
|
||||
}
|
||||
|
||||
static Response = RawUser;
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserListRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.list";
|
||||
static Response = jsonArray(RawUser);
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserSetSubscriptionsRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.subscribe";
|
||||
|
||||
/** @type {boolean} */
|
||||
all;
|
||||
/** @type {Array<string>} */
|
||||
names;
|
||||
|
||||
constructor(all, names) {
|
||||
this.all = all;
|
||||
this.names = names;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["all", "names"],
|
||||
properties: {
|
||||
all: { type: "boolean" },
|
||||
names: {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.all, json.names);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserCreateRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.create";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserRevokeTokenRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.revoke_token";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserUpdateRolesRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.update_roles";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {Array<number>} */
|
||||
roles;
|
||||
|
||||
constructor(name, roles) {
|
||||
this.name = name;
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "roles"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
roles: {
|
||||
type: "array",
|
||||
items: { type: "integer" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.roles);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserSetAdminRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.set_admin";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {boolean} */
|
||||
create;
|
||||
/** @type {boolean} */
|
||||
admin;
|
||||
|
||||
constructor(name, create, admin) {
|
||||
this.name = name;
|
||||
this.create = create;
|
||||
this.admin = admin;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "create", "admin"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
create: { type: "boolean" },
|
||||
admin: { type: "boolean" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.create, json.admin);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserSetWhitelistedRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.set_whitelisted";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {boolean} */
|
||||
create;
|
||||
/** @type {boolean} */
|
||||
whitelisted;
|
||||
|
||||
constructor(name, create, whitelisted) {
|
||||
this.name = name;
|
||||
this.create = create;
|
||||
this.whitelisted = whitelisted;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "create", "whitelisted"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
create: { type: "boolean" },
|
||||
whitelisted: { type: "boolean" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.create, json.whitelisted);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserSetBannedRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.set_banned";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {boolean} */
|
||||
create;
|
||||
/** @type {boolean} */
|
||||
banned;
|
||||
/** @type {string} */
|
||||
reason;
|
||||
|
||||
constructor(name, create, banned, reason) {
|
||||
this.name = name;
|
||||
this.create = create;
|
||||
this.banned = banned;
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name", "create", "banned", "reason"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
create: { type: "boolean" },
|
||||
banned: { type: "boolean" },
|
||||
reason: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name, json.create, json.banned, json.reason);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserDeleteRequest {
|
||||
static type = "request";
|
||||
static src = "control";
|
||||
static dst = "controller";
|
||||
static permission = "core.user.delete";
|
||||
|
||||
/** @type {string} */
|
||||
name;
|
||||
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["name"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* @memberof module:lib */
|
||||
class AccountUpdateEvent {
|
||||
static type = "event";
|
||||
static src = "controller";
|
||||
static dst = "control";
|
||||
|
||||
/** @type {Array<object>|undefined} */
|
||||
roles;
|
||||
|
||||
constructor(roles) {
|
||||
if (roles) { this.roles = roles; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
roles: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
required: ["name", "id", "permissions"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
id: { type: "integer" },
|
||||
permissions: {
|
||||
type: "array",
|
||||
items: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(json.roles);
|
||||
}
|
||||
}
|
||||
|
||||
/* @memberof module:lib */
|
||||
class UserUpdateEvent {
|
||||
static type = "event";
|
||||
static src = "controller";
|
||||
static dst = "control";
|
||||
|
||||
/** @type {module:lib.RawUser} */
|
||||
user;
|
||||
|
||||
constructor(user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
required: ["user"],
|
||||
properties: {
|
||||
user: RawUser.jsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
static fromJSON(json) {
|
||||
return new this(RawUser.fromJSON(json.user));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
RawPermission,
|
||||
PermissionListRequest,
|
||||
RawRole,
|
||||
RoleListRequest,
|
||||
RoleCreateRequest,
|
||||
RoleUpdateRequest,
|
||||
RoleGrantDefaultPermissionsRequest,
|
||||
RoleDeleteRequest,
|
||||
RawUser,
|
||||
UserGetRequest,
|
||||
UserListRequest,
|
||||
UserSetSubscriptionsRequest,
|
||||
UserCreateRequest,
|
||||
UserRevokeTokenRequest,
|
||||
UserUpdateRolesRequest,
|
||||
UserSetAdminRequest,
|
||||
UserSetWhitelistedRequest,
|
||||
UserSetBannedRequest,
|
||||
UserDeleteRequest,
|
||||
AccountUpdateEvent,
|
||||
UserUpdateEvent,
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
// Helper functions for dealing with factorio version numbers
|
||||
"use strict";
|
||||
|
||||
|
||||
/**
|
||||
* Integer representation of a mod version
|
||||
*
|
||||
* Suitable for sorting and comparing version numbers which may have different
|
||||
* number of leading zeros in them.
|
||||
* @param {string} version - string representation of version.
|
||||
* @returns {number} numeric representation.
|
||||
* @alias module:lib.integerModVersion
|
||||
*/
|
||||
function integerModVersion(version) {
|
||||
const [major, minor, sub] = version.split(".").map(n => Number.parseInt(n, 10));
|
||||
return major * 0x100000000 + minor * 0x10000 + sub; // Can't use bitwise here because this is 48-bits.
|
||||
}
|
||||
|
||||
/**
|
||||
* Integer representation of a factorio version
|
||||
*
|
||||
* Suitable for sorting and comparing version numbers which may have different
|
||||
* number of leading zeros in them.
|
||||
* @param {string} version - string representation of version.
|
||||
* @returns {number} numeric representation.
|
||||
* @alias module:lib.integerFactorioVersion
|
||||
*/
|
||||
function integerFactorioVersion(version) {
|
||||
const [main, major, minor] = version.split(".").map(n => Number.parseInt(n, 10));
|
||||
return main * 0x100000000 + major * 0x10000 + (minor || 0); // Can't use bitwise here because this is 48-bits.
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches valid mod versions.
|
||||
* @type {RegExp}
|
||||
* @alias module:lib.modVersionRegExp
|
||||
*/
|
||||
const modVersionRegExp = /^\d+\.\d+\.\d+$/;
|
||||
|
||||
module.exports = {
|
||||
integerModVersion,
|
||||
integerFactorioVersion,
|
||||
modVersionRegExp,
|
||||
};
|
||||
@@ -0,0 +1,288 @@
|
||||
/**
|
||||
* Implementation of controller databases
|
||||
*
|
||||
* @module lib/database
|
||||
* @author Hornwitser
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
const fs = require("fs-extra");
|
||||
|
||||
const libFileOps = require("./file_ops");
|
||||
const { basicType } = require("./helpers");
|
||||
|
||||
|
||||
/**
|
||||
* Converts a Map with only string keys to a JavaScript object.
|
||||
*
|
||||
* @param {Map} map - the Map to convert to an object.
|
||||
* @return {object} - Object with the mapping's key-values as properties.
|
||||
* @throws {Error} if there are non-string keys in map.
|
||||
*/
|
||||
function mapToObject(map) {
|
||||
let obj = {};
|
||||
for (let [key, value] of map) {
|
||||
if (typeof key !== "string") {
|
||||
throw new Error(
|
||||
`Expected all keys to be string but got ${typeof key}`
|
||||
);
|
||||
}
|
||||
|
||||
obj[key] = value;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load JSON object file as a Map
|
||||
*
|
||||
* Loads the JSON file specified by path containing an object with
|
||||
* properties as key value pairs and converts it into a JavaScript Map.
|
||||
* Returns an empty Map if the file does not exist.
|
||||
*
|
||||
* @param {string} filePath - The path to the JSON file to load.
|
||||
* @throws {Error} If JSON file did not contain an object.
|
||||
* @throws {Error} If an error occured reading the file.
|
||||
*/
|
||||
async function loadJsonAsMap(filePath) {
|
||||
try {
|
||||
let content = await fs.readFile(filePath);
|
||||
let parsed = JSON.parse(content);
|
||||
|
||||
if (basicType(parsed) !== "object") {
|
||||
throw new Error(`Expected object but got ${basicType(parsed)}`);
|
||||
}
|
||||
|
||||
return new Map(Object.entries(parsed));
|
||||
|
||||
} catch (err) {
|
||||
if (err.code === "ENOENT") {
|
||||
// File does not exist, return empty map
|
||||
return new Map();
|
||||
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a Map as a JSON object file
|
||||
*
|
||||
* Save the content of a key-value Map with only string keys as a JSON file.
|
||||
* If the directory the file is to be saved into doesn't exist it will be
|
||||
* created.
|
||||
*
|
||||
* @param {string} filePath - The path to the JSON file that will be saved.
|
||||
* @param {Map} map - Map with only string keys to save.
|
||||
* @throws {Error} if there are non-string keys in map.
|
||||
* @throws {Error} if an error occured writing to the file.
|
||||
*/
|
||||
async function saveMapAsJson(filePath, map) {
|
||||
let obj = mapToObject(map);
|
||||
await libFileOps.safeOutputFile(filePath, JSON.stringify(obj, null, 4));
|
||||
}
|
||||
|
||||
/**
|
||||
* Load JSON array file as a Map
|
||||
*
|
||||
* Loads the JSON file specified by path containing an array of objects that
|
||||
* each have an id property into a Map from the id to the object.
|
||||
*
|
||||
* @param {string} filePath - The path to the JSON file to load.
|
||||
* @throws {Error} If JSON file did not contain an array.
|
||||
* @throws {Error} If there are objects in the array without an id property.
|
||||
* @throws {Error} If an error occured reading the file.
|
||||
*/
|
||||
async function loadJsonArrayAsMap(filePath) {
|
||||
try {
|
||||
let content = await fs.readFile(filePath);
|
||||
let parsed = JSON.parse(content);
|
||||
|
||||
if (basicType(parsed) !== "array") {
|
||||
throw new Error(`Expected array but got ${basicType(parsed)}`);
|
||||
}
|
||||
|
||||
let map = new Map();
|
||||
for (let element of parsed) {
|
||||
if (basicType(element) !== "object") {
|
||||
throw new Error("Expected all elements to be objects");
|
||||
}
|
||||
|
||||
if (element.id === undefined) {
|
||||
throw new Error("Expected all elements to have an id property");
|
||||
}
|
||||
|
||||
map.set(element.id, element);
|
||||
}
|
||||
return map;
|
||||
|
||||
} catch (err) {
|
||||
if (err.code === "ENOENT") {
|
||||
// File does not exist, return empty map
|
||||
return new Map();
|
||||
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a Map as a JSON array file
|
||||
*
|
||||
* Save the values of a Map with objects containing an id property that is
|
||||
* equal to the key the objects are stored at into a JSON file.
|
||||
*
|
||||
* @param {string} filePath - The path to the JSON file that will be saved.
|
||||
* @param {Map} map -
|
||||
* Map with objects containing an id property equal to the key they are
|
||||
* stored under.
|
||||
* @throws {Error} if an error occured writing to the file.
|
||||
*/
|
||||
async function saveMapAsJsonArray(filePath, map) {
|
||||
await libFileOps.safeOutputFile(filePath, JSON.stringify([...map.values()], null, 4));
|
||||
}
|
||||
|
||||
|
||||
function checkName(name) {
|
||||
if (typeof name !== "string") {
|
||||
throw new Error("name must be a string");
|
||||
}
|
||||
}
|
||||
|
||||
function checkCount(count) {
|
||||
if (typeof count !== "number" || isNaN(count)) {
|
||||
throw new Error("count must be a number");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Item database
|
||||
*
|
||||
* Stores counts of items for the controller. Items that have not been
|
||||
* stored in the database are treated as having a stored count of zero. When
|
||||
* serialized the database discards entries with a zero counts, and when
|
||||
* deserialized the content is verified.
|
||||
*/
|
||||
class ItemDatabase {
|
||||
|
||||
/**
|
||||
* Create a new item database
|
||||
*
|
||||
* @param {object|undefined} serialized - An object from a previous call to
|
||||
* {@link module:libbase~ItemDatabase#serialize} to restore the
|
||||
* database from. An empty database will be created if this parameter
|
||||
* is left undefined.
|
||||
*/
|
||||
constructor(serialized) {
|
||||
this._items = new Map();
|
||||
|
||||
// Verify the content of the serialized database
|
||||
if (serialized !== undefined) {
|
||||
for (let [name, count] of Object.entries(serialized)) {
|
||||
checkName(name);
|
||||
checkCount(count);
|
||||
|
||||
this._items.set(name, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize item database
|
||||
*
|
||||
* Serialize the item database into a plain JavaScript object that can be
|
||||
* turned inta a string with JSON.stringify().
|
||||
*
|
||||
* @returns {object} Serialized representation of the database
|
||||
*/
|
||||
serialize() {
|
||||
let obj = {};
|
||||
for (let [name, count] of this._items) {
|
||||
if (count !== 0) {
|
||||
obj[name] = count;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Approximate size of the items database.
|
||||
*
|
||||
* Does not account for entries being zero in the item database.
|
||||
*/
|
||||
get size() {
|
||||
return this._items.size;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the count of an item stored in the database
|
||||
*
|
||||
* Returns the count of a given item that has been stored in to the
|
||||
* database. If the item has not been previously stored into the database
|
||||
* then 0 is returned.
|
||||
*
|
||||
* @param {string} name - The name of the item to get the count of.
|
||||
* @returns {number} The count of the item stored.
|
||||
*/
|
||||
getItemCount(name) {
|
||||
checkName(name);
|
||||
|
||||
if (!this._items.has(name)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return this._items.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds count of an item to the database
|
||||
*
|
||||
* Add count copies of the item specified by name to the item database.
|
||||
*
|
||||
* @param {string} name - The name of the item to add.
|
||||
* @param {number} count - The count of item to remove.
|
||||
*/
|
||||
addItem(name, count) {
|
||||
checkName(name);
|
||||
checkCount(count);
|
||||
|
||||
if (this._items.has(name)) {
|
||||
count += this._items.get(name);
|
||||
}
|
||||
|
||||
this._items.set(name, count);
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes count of an item from the database.
|
||||
*
|
||||
* Remove count copies of the item specified by name from the item database
|
||||
* If count is greater than the currently stored count the stored item
|
||||
* count will become negative.
|
||||
*
|
||||
* @param {string} name - The name of the item to remove.
|
||||
* @param {number} count - The count of items to remove.
|
||||
*/
|
||||
removeItem(name, count) {
|
||||
checkName(name);
|
||||
checkCount(count);
|
||||
|
||||
count = -count;
|
||||
if (this._items.has(name)) {
|
||||
count += this._items.get(name);
|
||||
}
|
||||
|
||||
this._items.set(name, count);
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mapToObject,
|
||||
loadJsonAsMap,
|
||||
saveMapAsJson,
|
||||
loadJsonArrayAsMap,
|
||||
saveMapAsJsonArray,
|
||||
ItemDatabase,
|
||||
};
|
||||
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* Errors thrown in Clusterio
|
||||
* @module lib/errors
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Thrown by commands when they fail
|
||||
* @static
|
||||
*/
|
||||
class CommandError extends Error { }
|
||||
|
||||
/**
|
||||
* Thrown from requests sent when an error occured handling it
|
||||
* @static
|
||||
*/
|
||||
class RequestError extends Error {
|
||||
constructor(message, code, stack) {
|
||||
super(message);
|
||||
if (code) { this.code = code; }
|
||||
if (stack) { this.stack = stack; }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown when a permission check fails.
|
||||
*
|
||||
* Is a subclass of RequestError to prevent logging stack traces when
|
||||
* requests fail due to permission denied.
|
||||
*
|
||||
* @static
|
||||
*/
|
||||
class PermissionError extends RequestError { }
|
||||
|
||||
/**
|
||||
* Signal for messages that fail validation
|
||||
* @static
|
||||
*/
|
||||
class InvalidMessage extends Error {
|
||||
constructor(msg, errors) {
|
||||
super(msg);
|
||||
this.errors = errors || null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown from requests when the session it was sent on was lost
|
||||
* @static
|
||||
*/
|
||||
class SessionLost extends Error { }
|
||||
|
||||
/**
|
||||
* Thrown when WebSocket authentication failed
|
||||
* @static
|
||||
*/
|
||||
class AuthenticationFailed extends Error { }
|
||||
|
||||
/**
|
||||
* Errror class for known errors occuring during startup
|
||||
* @static
|
||||
*/
|
||||
class StartupError extends Error { }
|
||||
|
||||
/**
|
||||
* Errors outside of our control
|
||||
* @static
|
||||
*/
|
||||
class EnvironmentError extends Error { }
|
||||
|
||||
/**
|
||||
* Errors caused by plugins
|
||||
* @static
|
||||
*/
|
||||
class PluginError extends Error {
|
||||
constructor(pluginName, original) {
|
||||
super(`PluginError: ${original.message}`);
|
||||
this.pluginName = pluginName;
|
||||
this.original = original;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
CommandError,
|
||||
RequestError,
|
||||
PermissionError,
|
||||
InvalidMessage,
|
||||
SessionLost,
|
||||
AuthenticationFailed,
|
||||
StartupError,
|
||||
EnvironmentError,
|
||||
PluginError,
|
||||
};
|
||||
@@ -0,0 +1,393 @@
|
||||
"use strict";
|
||||
const zlib = require("zlib");
|
||||
|
||||
|
||||
class MapReaderState {
|
||||
constructor(buf) {
|
||||
this.pos = 0;
|
||||
this.buf = buf;
|
||||
this.last_position = { x: 0, y: 0 };
|
||||
}
|
||||
}
|
||||
|
||||
function readUInt8(state) {
|
||||
let value = state.buf.readUInt8(state.pos);
|
||||
state.pos += 1;
|
||||
return value;
|
||||
}
|
||||
|
||||
function readBool(state) {
|
||||
let value = readUInt8(state) !== 0;
|
||||
return value;
|
||||
}
|
||||
|
||||
function readInt16(state) {
|
||||
let value = state.buf.readInt16LE(state.pos);
|
||||
state.pos += 2;
|
||||
return value;
|
||||
}
|
||||
|
||||
function readUInt16(state) {
|
||||
let value = state.buf.readUInt16LE(state.pos);
|
||||
state.pos += 2;
|
||||
return value;
|
||||
}
|
||||
|
||||
function readInt32(state) {
|
||||
let value = state.buf.readInt32LE(state.pos);
|
||||
state.pos += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
function readUInt32(state) {
|
||||
let value = state.buf.readUInt32LE(state.pos);
|
||||
state.pos += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
function readUInt32so(state) {
|
||||
let value = readUInt8(state);
|
||||
if (value === 0xff) {
|
||||
return readUInt32(state);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function readFloat(state) {
|
||||
let value = state.buf.readFloatLE(state.pos);
|
||||
state.pos += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
function readDouble(state) {
|
||||
let value = state.buf.readDoubleLE(state.pos);
|
||||
state.pos += 8;
|
||||
return value;
|
||||
}
|
||||
|
||||
function readString(state) {
|
||||
let size = readUInt32so(state);
|
||||
let data = state.buf.slice(state.pos, state.pos + size).toString("utf-8");
|
||||
state.pos += size;
|
||||
return data;
|
||||
}
|
||||
|
||||
function readOptional(state, readValue) {
|
||||
let load = readUInt8(state) !== 0;
|
||||
if (!load) {
|
||||
return null;
|
||||
}
|
||||
return readValue(state);
|
||||
}
|
||||
|
||||
function readArray(state, readItem) {
|
||||
let size = readUInt32so(state);
|
||||
|
||||
let array = [];
|
||||
for (let i = 0; i < size; i++) {
|
||||
let item = readItem(state);
|
||||
array.push(item);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
function readDict(state, readKey, readValue) {
|
||||
let size = readUInt32so(state);
|
||||
|
||||
let mapping = new Map();
|
||||
for (let i = 0; i < size; i++) {
|
||||
let key = readKey(state);
|
||||
let value = readValue(state);
|
||||
mapping.set(key, value);
|
||||
}
|
||||
|
||||
return mapping;
|
||||
}
|
||||
|
||||
function readVersion(state) {
|
||||
let major = readUInt16(state);
|
||||
let minor = readUInt16(state);
|
||||
let patch = readUInt16(state);
|
||||
let developer = readUInt16(state);
|
||||
return [major, minor, patch, developer];
|
||||
}
|
||||
|
||||
function readFrequencySizeRichness(state) {
|
||||
return {
|
||||
frequency: readFloat(state),
|
||||
size: readFloat(state),
|
||||
richness: readFloat(state),
|
||||
};
|
||||
}
|
||||
|
||||
function readAutoplaceSetting(state) {
|
||||
return {
|
||||
treat_missing_as_default: readBool(state),
|
||||
settings: Object.fromEntries(readDict(state, readString, readFrequencySizeRichness)),
|
||||
};
|
||||
}
|
||||
|
||||
function readMapPosition(state) {
|
||||
let x, y;
|
||||
let x_diff = readInt16(state) / 256;
|
||||
if (x_diff === 0x7fff / 256) {
|
||||
x = readInt32(state) / 256;
|
||||
y = readInt32(state) / 256;
|
||||
} else {
|
||||
let y_diff = readInt16(state) / 256;
|
||||
x = state.last_position.x + x_diff;
|
||||
y = state.last_position.y + y_diff;
|
||||
}
|
||||
state.last_position.x = x;
|
||||
state.last_position.x = y;
|
||||
return { x, y };
|
||||
}
|
||||
|
||||
function readBoundingBox(state) {
|
||||
return {
|
||||
left_top: readMapPosition(state),
|
||||
right_bottom: readMapPosition(state),
|
||||
orientation: {
|
||||
x: readInt16(state),
|
||||
y: readInt16(state),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function readCliffSettings(state) {
|
||||
return {
|
||||
name: readString(state),
|
||||
elevation_0: readFloat(state),
|
||||
elevation_interval: readFloat(state),
|
||||
richness: readFloat(state),
|
||||
};
|
||||
}
|
||||
|
||||
function readMapGenSettings(state) {
|
||||
return {
|
||||
terrain_segmentation: readFloat(state),
|
||||
water: readFloat(state),
|
||||
autoplace_controls: Object.fromEntries(readDict(state, readString, readFrequencySizeRichness)),
|
||||
autoplace_settings: Object.fromEntries(readDict(state, readString, readAutoplaceSetting)),
|
||||
default_enable_all_autoplace_controls: readBool(state),
|
||||
seed: readUInt32(state),
|
||||
width: readUInt32(state),
|
||||
height: readUInt32(state),
|
||||
area_to_generate_at_start: readBoundingBox(state),
|
||||
starting_area: readFloat(state),
|
||||
peaceful_mode: readBool(state),
|
||||
starting_points: readArray(state, readMapPosition),
|
||||
property_expression_names: Object.fromEntries(readDict(state, readString, readString)),
|
||||
cliff_settings: readCliffSettings(state),
|
||||
};
|
||||
}
|
||||
|
||||
function readPollution(state) {
|
||||
return {
|
||||
enabled: readOptional(state, readBool),
|
||||
diffusion_ratio: readOptional(state, readDouble),
|
||||
min_to_diffuse: readOptional(state, readDouble),
|
||||
ageing: readOptional(state, readDouble),
|
||||
expected_max_per_chunk: readOptional(state, readDouble),
|
||||
min_to_show_per_chunk: readOptional(state, readDouble),
|
||||
min_pollution_to_damage_trees: readOptional(state, readDouble),
|
||||
pollution_with_max_forest_damage: readOptional(state, readDouble),
|
||||
pollution_per_tree_damage: readOptional(state, readDouble),
|
||||
pollution_restored_per_tree_damage: readOptional(state, readDouble),
|
||||
max_pollution_to_restore_trees: readOptional(state, readDouble),
|
||||
enemy_attack_pollution_consumption_modifier: readOptional(state, readDouble),
|
||||
};
|
||||
}
|
||||
|
||||
function readSteeringValue(state) {
|
||||
return {
|
||||
radius: readOptional(state, readDouble),
|
||||
separation_factor: readOptional(state, readDouble),
|
||||
separation_force: readOptional(state, readDouble),
|
||||
force_unit_fuzzy_goto_behavior: readOptional(state, readBool),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
function readSteering(state) {
|
||||
return {
|
||||
default: readSteeringValue(state),
|
||||
moving: readSteeringValue(state),
|
||||
};
|
||||
}
|
||||
|
||||
function readEnemyEvolution(state) {
|
||||
return {
|
||||
enabled: readOptional(state, readBool),
|
||||
time_factor: readOptional(state, readDouble),
|
||||
destroy_factor: readOptional(state, readDouble),
|
||||
pollution_factor: readOptional(state, readDouble),
|
||||
};
|
||||
}
|
||||
|
||||
function readEnemyExpansion(state) {
|
||||
return {
|
||||
enabled: readOptional(state, readBool),
|
||||
max_expansion_distance: readOptional(state, readUInt32),
|
||||
friendly_base_influence_radius: readOptional(state, readUInt32),
|
||||
enemy_building_influence_radius: readOptional(state, readUInt32),
|
||||
building_coefficient: readOptional(state, readDouble),
|
||||
other_base_coefficient: readOptional(state, readDouble),
|
||||
neighbouring_chunk_coefficient: readOptional(state, readDouble),
|
||||
neighbouring_base_chunk_coefficient: readOptional(state, readDouble),
|
||||
max_colliding_tiles_coefficient: readOptional(state, readDouble),
|
||||
settler_group_min_size: readOptional(state, readUInt32),
|
||||
settler_group_max_size: readOptional(state, readUInt32),
|
||||
min_expansion_cooldown: readOptional(state, readUInt32),
|
||||
max_expansion_cooldown: readOptional(state, readUInt32),
|
||||
};
|
||||
}
|
||||
|
||||
function readUnitGroup(state) {
|
||||
return {
|
||||
min_group_gathering_time: readOptional(state, readUInt32),
|
||||
max_group_gathering_time: readOptional(state, readUInt32),
|
||||
max_wait_time_for_late_members: readOptional(state, readUInt32),
|
||||
max_group_radius: readOptional(state, readDouble),
|
||||
min_group_radius: readOptional(state, readDouble),
|
||||
max_member_speedup_when_behind: readOptional(state, readDouble),
|
||||
max_member_slowdown_when_ahead: readOptional(state, readDouble),
|
||||
max_group_slowdown_factor: readOptional(state, readDouble),
|
||||
max_group_member_fallback_factor: readOptional(state, readDouble),
|
||||
member_disown_distance: readOptional(state, readDouble),
|
||||
tick_tolerance_when_member_arrives: readOptional(state, readUInt32),
|
||||
max_gathering_unit_groups: readOptional(state, readUInt32),
|
||||
max_unit_group_size: readOptional(state, readUInt32),
|
||||
};
|
||||
}
|
||||
|
||||
function readPathFinder(state) {
|
||||
return {
|
||||
fwd2bwd_ratio: readOptional(state, readInt32),
|
||||
goal_pressure_ratio: readOptional(state, readDouble),
|
||||
use_path_cache: readOptional(state, readBool),
|
||||
max_steps_worked_per_tick: readOptional(state, readDouble),
|
||||
max_work_done_per_tick: readOptional(state, readUInt32),
|
||||
short_cache_size: readOptional(state, readUInt32),
|
||||
long_cache_size: readOptional(state, readUInt32),
|
||||
short_cache_min_cacheable_distance: readOptional(state, readDouble),
|
||||
short_cache_min_algo_steps_to_cache: readOptional(state, readUInt32),
|
||||
long_cache_min_cacheable_distance: readOptional(state, readDouble),
|
||||
cache_max_connect_to_cache_steps_multiplier: readOptional(state, readUInt32),
|
||||
cache_accept_path_start_distance_ratio: readOptional(state, readDouble),
|
||||
cache_accept_path_end_distance_ratio: readOptional(state, readDouble),
|
||||
negative_cache_accept_path_start_distance_ratio: readOptional(state, readDouble),
|
||||
negative_cache_accept_path_end_distance_ratio: readOptional(state, readDouble),
|
||||
cache_path_start_distance_rating_multiplier: readOptional(state, readDouble),
|
||||
cache_path_end_distance_rating_multiplier: readOptional(state, readDouble),
|
||||
stale_enemy_with_same_destination_collision_penalty: readOptional(state, readDouble),
|
||||
ignore_moving_enemy_collision_distance: readOptional(state, readDouble),
|
||||
enemy_with_different_destination_collision_penalty: readOptional(state, readDouble),
|
||||
general_entity_collision_penalty: readOptional(state, readDouble),
|
||||
general_entity_subsequent_collision_penalty: readOptional(state, readDouble),
|
||||
extended_collision_penalty: readOptional(state, readDouble),
|
||||
max_clients_to_accept_any_new_request: readOptional(state, readUInt32),
|
||||
max_clients_to_accept_short_new_request: readOptional(state, readUInt32),
|
||||
direct_distance_to_consider_short_request: readOptional(state, readUInt32),
|
||||
short_request_max_steps: readOptional(state, readUInt32),
|
||||
short_request_ratio: readOptional(state, readDouble),
|
||||
min_steps_to_check_path_find_termination: readOptional(state, readUInt32),
|
||||
start_to_goal_cost_multiplier_to_terminate_path_find: readOptional(state, readDouble),
|
||||
overload_levels: readOptional(state, (p) => readArray(p, readUInt32)),
|
||||
overload_multipliers: readOptional(state, (p) => readArray(p, readDouble)),
|
||||
negative_path_cache_delay_interval: readOptional(state, readUInt32),
|
||||
};
|
||||
}
|
||||
|
||||
function readDifficultySettings(state) {
|
||||
return {
|
||||
recipe_difficulty: readUInt8(state),
|
||||
technology_difficulty: readUInt8(state),
|
||||
technology_price_multiplier: readDouble(state),
|
||||
research_queue_setting: ["always", "after-victory", "never"][readUInt8(state)],
|
||||
};
|
||||
}
|
||||
|
||||
function readMapSettings(state) {
|
||||
return {
|
||||
pollution: readPollution(state),
|
||||
steering: readSteering(state),
|
||||
enemy_evolution: readEnemyEvolution(state),
|
||||
enemy_expansion: readEnemyExpansion(state),
|
||||
unit_group: readUnitGroup(state),
|
||||
path_finder: readPathFinder(state),
|
||||
max_failed_behavior_count: readUInt32(state),
|
||||
difficulty_settings: readDifficultySettings(state),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {object} module:lib.MapExchangeResult
|
||||
* @property {array} version -
|
||||
* Version of Factorio the string was created with.
|
||||
* @property {object} map_gen_settings -
|
||||
* Decoded map generator settings in the format the --map-gen-settings
|
||||
* command line option to Factorio expect.
|
||||
* @property {object} map_settings -
|
||||
* Decoded map settings in the format the --map-settings command line option
|
||||
* to Factorio expects.
|
||||
* @property {number} checksum - checksum for the exchange string.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parse a Map Exchange String
|
||||
*
|
||||
* Reads and decodes the data in the given map exchange string and returns
|
||||
* data structures that can be fed into the Factorio server when creating a
|
||||
* save to set the map gen settings and the map settings for the save.
|
||||
*
|
||||
* @param {string} exchangeString - Max Exchange String to parse.
|
||||
* @returns {module:lib.MapExchangeResult} Parsed result.
|
||||
* @memberof module:lib
|
||||
*/
|
||||
function readMapExchangeString(exchangeString) {
|
||||
exchangeString = exchangeString.replace(/[ \t\n\r]+/g, "");
|
||||
if (!/>>>[0-9a-zA-Z\/+]+={0,3}<<</.test(exchangeString)) {
|
||||
throw new Error("Not a map exchange string");
|
||||
}
|
||||
|
||||
let buf = Buffer.from(exchangeString.slice(3, -3), "base64");
|
||||
try {
|
||||
// eslint-disable-next-line node/no-sync
|
||||
buf = zlib.inflateSync(buf);
|
||||
} catch (err) {
|
||||
if (err.code.startsWith("Z_")) {
|
||||
throw new Error("Malformed map exchange string: zlib inflate failed");
|
||||
}
|
||||
}
|
||||
|
||||
let state = new MapReaderState(buf);
|
||||
let data;
|
||||
|
||||
try {
|
||||
data = {
|
||||
version: readVersion(state),
|
||||
unknown: readUInt8(state),
|
||||
map_gen_settings: readMapGenSettings(state),
|
||||
map_settings: readMapSettings(state),
|
||||
checksum: readUInt32(state),
|
||||
};
|
||||
} catch (err) {
|
||||
if (err.code === "ERR_OUT_OF_RANGE") {
|
||||
throw new Error("Malformed map exchange string: reached end before finishing parsing");
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (state.pos !== buf.length) {
|
||||
throw new Error("Malformed map exchange string: data after end");
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
readMapExchangeString,
|
||||
};
|
||||
@@ -0,0 +1,475 @@
|
||||
// Export of item icons and locale
|
||||
"use strict";
|
||||
const fs = require("fs-extra");
|
||||
const path = require("path");
|
||||
const Jimp = require("jimp");
|
||||
const JSZip = require("jszip");
|
||||
|
||||
const libIni = require("../ini");
|
||||
const libBuildMod = require("../../build_mod");
|
||||
const libZipOps = require("../zip_ops");
|
||||
|
||||
/**
|
||||
* Generate the export mod needed for exportData
|
||||
*
|
||||
* Packs the lua export mod located in lua/export into the mods folder for
|
||||
* the server given, with dependencies generated for all the mods present in
|
||||
* the server's mods folder.
|
||||
*
|
||||
* @param {module:lib.FactorioServer} server -
|
||||
* The server to generate the export mod for.
|
||||
* @memberof module:lib
|
||||
*/
|
||||
async function generateExportMod(server) {
|
||||
let dependencies = [];
|
||||
let splitter = /^(.*)_(\d+\.\d+\.\d+)(\.zip)?$/;
|
||||
for (let entry of await fs.readdir(server.writePath("mods"))) {
|
||||
let match = splitter.exec(entry);
|
||||
if (match && match[1] !== "export") {
|
||||
dependencies.push(`? ${match[1]}`);
|
||||
}
|
||||
}
|
||||
|
||||
await libBuildMod.build({
|
||||
clean: false,
|
||||
build: true,
|
||||
pack: true,
|
||||
// XXX fix path when moving lib/factorio to host
|
||||
sourceDir: path.join(__dirname, "..", "..", "..", "host", "lua", "export"),
|
||||
outputDir: server.writePath("mods"),
|
||||
bumpPatch: false,
|
||||
factorioVersion: server.version.replace(/\.\d+$/, ""),
|
||||
dependencies,
|
||||
});
|
||||
}
|
||||
|
||||
let zipCache = new Map();
|
||||
async function loadZip(server, modVersions, mod) {
|
||||
let modVersion = modVersions.get(mod);
|
||||
if (!modVersion) {
|
||||
throw new Error(`Got path for unknown mod ${mod}`);
|
||||
}
|
||||
|
||||
let zipPath = server.writePath("mods", `${mod}_${modVersion}.zip`);
|
||||
let zip = zipCache.get(zipPath);
|
||||
if (!zip) {
|
||||
zip = await JSZip.loadAsync(await fs.readFile(zipPath));
|
||||
zipCache.set(zipPath, zip);
|
||||
}
|
||||
|
||||
return zip.folder(libZipOps.findRoot(zip));
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the given Factorio file path into a Buffer
|
||||
*
|
||||
* @param {module:lib.FactorioServer} server -
|
||||
* The server to load the file from.
|
||||
* @param {Map<string, string>} modVersions - Mapping of mod to version used.
|
||||
* @param {string} modPath - Factorio style path to the file to load.
|
||||
* @returns {Promise<?Buffer>} The content of the file or null if not found.
|
||||
* @memberof module:lib
|
||||
* @private
|
||||
* @inner
|
||||
*/
|
||||
async function loadFile(server, modVersions, modPath) {
|
||||
let match = /^__([^\/]+)__\/(.*)$/.exec(modPath);
|
||||
if (!match) {
|
||||
throw new Error(`Bad mod path ${match}`);
|
||||
}
|
||||
|
||||
let [, mod, filePath] = match;
|
||||
|
||||
if (["core", "base"].includes(mod)) {
|
||||
try {
|
||||
return await fs.readFile(server.dataPath(mod, filePath));
|
||||
} catch (err) {
|
||||
if (err.code === "ENOENT") {
|
||||
return null;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
let zip;
|
||||
try {
|
||||
zip = await loadZip(server, modVersions, mod);
|
||||
} catch (err) {
|
||||
if (err.code === "ENOENT") {
|
||||
return null;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
let file = zip.file(filePath);
|
||||
if (!file) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return await file.async("nodebuffer");
|
||||
}
|
||||
|
||||
async function loadIcon(server, modVersions, iconPath, iconSize, iconMipmaps, iconCache) {
|
||||
let icon = iconCache.get(iconPath);
|
||||
if (icon === undefined) {
|
||||
let fileContent = await loadFile(server, modVersions, iconPath);
|
||||
if (fileContent) {
|
||||
icon = await Jimp.read(fileContent);
|
||||
icon.crop(0, 0, iconSize, iconSize);
|
||||
iconCache.set(iconPath, icon);
|
||||
} else {
|
||||
icon = null;
|
||||
server._logger.warn(`${iconPath} not found`);
|
||||
}
|
||||
iconCache.set(iconPath, icon);
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
async function loadSimpleIcon(server, modVersions, item, size, iconCache) {
|
||||
let icon = await loadIcon(server, modVersions, item.icon, item.icon_size, 0, iconCache);
|
||||
if (icon) {
|
||||
let iconScale = size / item.icon_size;
|
||||
if (iconScale !== 1) {
|
||||
icon = icon.clone();
|
||||
icon.scale(iconScale);
|
||||
}
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
async function loadLayeredIcon(server, modVersions, item, size, iconCache) {
|
||||
let baseLayerSize = item.icons[0].icon_size || item.icon_size;
|
||||
let icon = await Jimp.create(size, size);
|
||||
|
||||
// The scaling factor of the base layer
|
||||
let baseLayerScale = item.icons[0].scale || 32 / baseLayerSize;
|
||||
|
||||
// The size in pixels of one unit
|
||||
let baseUnit = size / (baseLayerSize * baseLayerScale);
|
||||
|
||||
for (let layer of item.icons) {
|
||||
let layerSize = layer.icon_size || item.icon_size;
|
||||
let iconLayer = await loadIcon(server, modVersions, layer.icon, layerSize, layer.icon_mipmaps || 0, iconCache);
|
||||
|
||||
if (!iconLayer) {
|
||||
continue;
|
||||
}
|
||||
|
||||
iconLayer = iconLayer.clone();
|
||||
|
||||
let tint;
|
||||
if (layer.tint) {
|
||||
let divisor = (layer.tint.r > 1 || layer.tint.g > 1 || layer.tint.b > 1) ? 255 : 1;
|
||||
tint = {
|
||||
r: (layer.tint.r || 0) / divisor,
|
||||
g: (layer.tint.g || 0) / divisor,
|
||||
b: (layer.tint.b || 0) / divisor,
|
||||
a: layer.tint.a || 1,
|
||||
};
|
||||
} else {
|
||||
tint = { r: 1, b: 1, g: 1, a: 1 };
|
||||
}
|
||||
|
||||
let layerScale = layer.scale || 32 / layerSize;
|
||||
let [xs, ys] = layer.shift || [0, 0];
|
||||
let realScale = layerScale * baseUnit;
|
||||
let sizeShift = (size - layerSize * realScale) / 2;
|
||||
|
||||
xs = xs * baseUnit + sizeShift;
|
||||
ys = ys * baseUnit + sizeShift;
|
||||
|
||||
if (realScale !== 1) {
|
||||
iconLayer.scale(realScale);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-loop-func
|
||||
iconLayer.scan(0, 0, iconLayer.bitmap.width, iconLayer.bitmap.height, (x, y, sidx) => {
|
||||
x += xs;
|
||||
y += ys;
|
||||
if (x < 0 || x >= size || y < 0 || y >= size) {
|
||||
return;
|
||||
}
|
||||
let sdata = iconLayer.bitmap.data;
|
||||
let ddata = icon.bitmap.data;
|
||||
let didx = icon.getPixelIndex(x, y);
|
||||
|
||||
let sa = sdata[sidx + 3] / 255;
|
||||
let da = ddata[didx + 3] / 255;
|
||||
let db = da * (1 - sa * tint.a);
|
||||
let cb = sa + da * (1 - sa);
|
||||
ddata[didx + 0] = Math.min(255, (sdata[sidx + 0] * sa * tint.r + ddata[didx + 0] * db) / cb);
|
||||
ddata[didx + 1] = Math.min(255, (sdata[sidx + 1] * sa * tint.g + ddata[didx + 1] * db) / cb);
|
||||
ddata[didx + 2] = Math.min(255, (sdata[sidx + 2] * sa * tint.b + ddata[didx + 2] * db) / cb);
|
||||
ddata[didx + 3] = Math.min(255, sdata[sidx + 3] + ddata[didx + 3] * (1 - sa));
|
||||
});
|
||||
}
|
||||
|
||||
return icon;
|
||||
}
|
||||
|
||||
const itemTypes = new Set([
|
||||
"item",
|
||||
"ammo",
|
||||
"capsule",
|
||||
"gun",
|
||||
"item-with-entity-data",
|
||||
"item-with-label",
|
||||
"item-with-inventory",
|
||||
"blueprint-book",
|
||||
"item-with-tags",
|
||||
"selection-tool",
|
||||
"blueprint",
|
||||
"copy-paste-tool",
|
||||
"deconstruction-item",
|
||||
"upgrade-item",
|
||||
"module",
|
||||
"rail-planner",
|
||||
"spidertron-remote",
|
||||
"tool",
|
||||
"armor",
|
||||
"mining-tool",
|
||||
"repair-tool",
|
||||
|
||||
// XXX Bad hack to get icons for fluids. These should be treated as a
|
||||
// separate namespace from items as they may be named the same as items.
|
||||
"fluid",
|
||||
// TODO consider adding virtual signals and recipes
|
||||
]);
|
||||
|
||||
function filterItems(prototypes) {
|
||||
return Object.entries(prototypes)
|
||||
.filter(([type, _]) => itemTypes.has(type))
|
||||
.flatMap(([_, typePrototypes]) => Object.values(typePrototypes))
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export item icons and data
|
||||
*
|
||||
* Assembles and packs the icons for the item prototypes given into a single
|
||||
* spritesheet and json file with meta data.
|
||||
*
|
||||
* @param {module:lib.FactorioServer} server -
|
||||
* The server to generate the export mod for.
|
||||
* @param {Map<string, string>} modVersions -
|
||||
* Mapping of mod name to versions to get icons from.
|
||||
* @param {Array<Object>} items - Array of item prototypes.
|
||||
* @return {Promise<{itemSheet: Jimp, itemData: Map<string,Object>}>}
|
||||
* Item spritesheet and metadata.
|
||||
* @memberof module:lib
|
||||
* @private
|
||||
* @inner
|
||||
*/
|
||||
async function exportItems(server, modVersions, items) {
|
||||
|
||||
// Size to render icons at
|
||||
let size = 32;
|
||||
|
||||
// Width of spritesheet
|
||||
let width = 1024;
|
||||
|
||||
let rows = Math.ceil(items.length / (width / size));
|
||||
let iconSheet = await Jimp.create(width, rows * size);
|
||||
let itemData = new Map();
|
||||
let pos = 0;
|
||||
|
||||
let iconCache = new Map();
|
||||
let simpleIcons = new Map();
|
||||
for (let item of items) {
|
||||
let icon;
|
||||
let iconPos;
|
||||
if (item.icons) {
|
||||
icon = await loadLayeredIcon(server, modVersions, item, size, iconCache);
|
||||
iconPos = pos;
|
||||
|
||||
} else {
|
||||
iconPos = simpleIcons.get(item.icon);
|
||||
if (iconPos === undefined) {
|
||||
icon = await loadSimpleIcon(server, modVersions, item, size, iconCache);
|
||||
if (icon) {
|
||||
iconPos = pos;
|
||||
simpleIcons.set(item.icon, pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (iconPos !== undefined) {
|
||||
itemData.set(item.name, {
|
||||
x: iconPos * size % width,
|
||||
y: Math.floor(iconPos / (width / size)) * size,
|
||||
size,
|
||||
localised_name: item.localised_name,
|
||||
localised_description: item.localised_description,
|
||||
});
|
||||
}
|
||||
|
||||
if (icon) {
|
||||
iconSheet.composite(icon, pos * size % width, Math.floor(pos / (width / size)) * size);
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
|
||||
iconSheet.crop(0, 0, width, Math.ceil(pos / (width / size)) * size);
|
||||
|
||||
return { iconSheet, itemData };
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the locale files for the base game and the given mods
|
||||
*
|
||||
* Parses and merges all the locales for the all the mods given through
|
||||
* `modVersions` and `modOrder`.
|
||||
*
|
||||
* @param {module:lib.FactorioServer} server -
|
||||
* The server to export the locale from.
|
||||
* @param {Map<string, string>} modVersions -
|
||||
* Mapping of mod name to version to export locale from.
|
||||
* @param {Array<string>} modOrder - Load order of the mods.
|
||||
* @param {string} languageCode - Language to export locale for.
|
||||
* @returns {Promise<Map<string, string>>} merged locale information
|
||||
* @memberof module:lib
|
||||
* @private
|
||||
* @inner
|
||||
*/
|
||||
async function exportLocale(server, modVersions, modOrder, languageCode) {
|
||||
let mergedLocales = new Map();
|
||||
|
||||
function mergeLocale(locale) {
|
||||
for (let [category, entries] of Object.entries(locale)) {
|
||||
if (typeof entries === "string") {
|
||||
mergedLocales.set(category, entries);
|
||||
|
||||
} else {
|
||||
for (let [key, value] of Object.entries(entries)) {
|
||||
mergedLocales.set(`${category}.${key}`, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let baseLocaleFilePath = server.dataPath("base", "locale", languageCode, "base.cfg");
|
||||
mergeLocale(libIni.parse(await fs.readFile(baseLocaleFilePath, "utf8")));
|
||||
|
||||
for (let mod of modOrder) {
|
||||
if (["core", "base", "export"].includes(mod)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let zip;
|
||||
try {
|
||||
zip = await loadZip(server, modVersions, mod);
|
||||
} catch (err) {
|
||||
if (err.code === "ENOENT") {
|
||||
continue;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
for (let file of zip.file(new RegExp(`locale\\/${languageCode}\\/.*\\.cfg`))) {
|
||||
let content = await file.async("nodebuffer");
|
||||
mergeLocale(libIni.parse(content.toString("utf8")));
|
||||
}
|
||||
}
|
||||
|
||||
return mergedLocales;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the locale and item icons for the given factorio server
|
||||
*
|
||||
* @param {module:lib.FactorioServer} server -
|
||||
* The server to export the data from.
|
||||
* @returns {Promise<JSZip>} zip file with exported data.
|
||||
* @memberof module:lib
|
||||
*/
|
||||
async function exportData(server) {
|
||||
await generateExportMod(server);
|
||||
|
||||
let settings = {};
|
||||
let prototypes = {};
|
||||
let modVersions = new Map();
|
||||
let modOrder = [];
|
||||
|
||||
function add(obj, prototype) {
|
||||
if (!Object.prototype.hasOwnProperty.call(obj, prototype.type)) {
|
||||
obj[prototype.type] = {};
|
||||
}
|
||||
obj[prototype.type][prototype.name] = prototype;
|
||||
}
|
||||
|
||||
server.on("ipc-prototype_export", data => add(prototypes, data));
|
||||
server.on("ipc-settings_export", data => add(settings, data));
|
||||
server.on("ipc-mod_setting_mod", ({ name, mod }) => {
|
||||
for (let type of Object.values(settings)) {
|
||||
if (Object.prototype.hasOwnProperty.call(type, name)) {
|
||||
type[name].mod = mod;
|
||||
return;
|
||||
}
|
||||
}
|
||||
server._logger.error(`Unable to find ${name} in settings prototypes`);
|
||||
});
|
||||
server.on("ipc-mod_list", data => { modVersions = new Map(Object.entries(data)); });
|
||||
server.on("output", parsed => {
|
||||
if (parsed.format === "seconds" && parsed.type === "generic") {
|
||||
let match = /^Checksum of (.*): \d+$/.exec(parsed.message);
|
||||
if (match) {
|
||||
modOrder.push(match[1]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
await server.startScenario("base/freeplay");
|
||||
await server.kill();
|
||||
} finally {
|
||||
await fs.unlink(server.writePath("mods", "export_0.0.0.zip"));
|
||||
}
|
||||
|
||||
if (!Object.keys(prototypes).length) {
|
||||
throw new Error("No prototypes got exported");
|
||||
}
|
||||
|
||||
// Some mod authors put leading zeros into the versions of their zip files.
|
||||
let splitter = /^(.*)_(\d+)\.(\d+)\.(\d+)\.zip?$/;
|
||||
for (let entry of await fs.readdir(server.writePath("mods"))) {
|
||||
let match = splitter.exec(entry);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let modVersion = `${match[2]}.${match[3]}.${match[4]}`;
|
||||
let normalizedVersion =
|
||||
`${Number.parseInt(match[2], 10)}.${Number.parseInt(match[3], 10)}.${Number.parseInt(match[4], 10)}`
|
||||
;
|
||||
|
||||
if (modVersion === normalizedVersion) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (modVersions.get(match[1]) === normalizedVersion) {
|
||||
modVersions.set(match[1], modVersion);
|
||||
}
|
||||
}
|
||||
|
||||
let { iconSheet, itemData } = await exportItems(server, modVersions, filterItems(prototypes));
|
||||
let locale = await exportLocale(server, modVersions, modOrder, "en");
|
||||
|
||||
// Free up the memory used by zip files loaded during the export.
|
||||
zipCache.clear();
|
||||
|
||||
let zip = new JSZip();
|
||||
zip.file("export/settings.json", JSON.stringify(settings));
|
||||
zip.file("export/prototypes.json", JSON.stringify(prototypes));
|
||||
zip.file("export/item-spritesheet.png", await iconSheet.getBufferAsync(Jimp.MIME_PNG));
|
||||
zip.file("export/item-metadata.json", JSON.stringify([...itemData.entries()]));
|
||||
zip.file("export/locale.json", JSON.stringify([...locale.entries()]));
|
||||
return zip;
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
exportData,
|
||||
|
||||
// For testing only
|
||||
_exportLocale: exportLocale,
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Library for interfacing with Factorio Servers and saves.
|
||||
* @module lib/factorio
|
||||
* @author Hornwitser
|
||||
*/
|
||||
"use strict";
|
||||
const exchange_string = require("./exchange_string");
|
||||
const export_ = require("./export");
|
||||
const patch = require("./patch");
|
||||
const server = require("./server");
|
||||
|
||||
module.exports = {
|
||||
...exchange_string,
|
||||
...export_,
|
||||
...patch,
|
||||
...server,
|
||||
};
|
||||
@@ -0,0 +1,271 @@
|
||||
// Library for patching Factorio saves with scenario code.
|
||||
|
||||
"use strict";
|
||||
const events = require("events");
|
||||
const fs = require("fs-extra");
|
||||
const JSZip = require("jszip");
|
||||
const path = require("path");
|
||||
const semver = require("semver");
|
||||
|
||||
const libHash = require("../hash");
|
||||
const { findRoot } = require("../zip_ops");
|
||||
|
||||
|
||||
const knownScenarios = {
|
||||
// First seen in 0.17.63
|
||||
"4e866186ebe297f1038fd325b09df1a1f5e2fdd1": {
|
||||
"name": "freeplay",
|
||||
"modules": [
|
||||
"freeplay",
|
||||
"silo-script",
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates control.lua code for loading the Clusterio modules
|
||||
*
|
||||
* @param {Object} patchInfo - The patch info files's json content
|
||||
* @returns {string} Generated control.lua code.
|
||||
* @memberof module:lib
|
||||
* @private
|
||||
* @inner
|
||||
*/
|
||||
function generateLoader(patchInfo) {
|
||||
let lines = [
|
||||
"-- Auto generated scenario module loader created by Clusterio",
|
||||
"-- Modifications to this file will be lost when loaded in Clusterio",
|
||||
`clusterio_patch_number = ${patchInfo.patch_number}`,
|
||||
"",
|
||||
'local event_handler = require("event_handler")',
|
||||
"",
|
||||
"-- Scenario modules",
|
||||
];
|
||||
|
||||
for (let moduleName of patchInfo["scenario"]["modules"]) {
|
||||
lines.push(`event_handler.add_lib(require("${moduleName}"))`);
|
||||
}
|
||||
|
||||
lines.push(...[
|
||||
"",
|
||||
"-- Clusterio modules",
|
||||
]);
|
||||
|
||||
for (let module of patchInfo["modules"]) {
|
||||
for (let file of module["files"]) {
|
||||
let requirePath = `modules/${module.name}/${file["path"].slice(0, -4)}`;
|
||||
if (file["load"]) {
|
||||
lines.push(`event_handler.add_lib(require("${requirePath}"))`);
|
||||
}
|
||||
if (file["require"]) {
|
||||
lines.push(`require("${requirePath}")`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// End last line with a newline
|
||||
lines.push("");
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorders modules to satisfy their dependencies
|
||||
*
|
||||
* Looks through and reorders the array of module definitions in order to
|
||||
* satisfy the property that dependencies are earlier in the array than
|
||||
* their dependents. Throws an error if this is not possible.
|
||||
*
|
||||
* @param {Array<Object>} modules - Array of modules to reorder
|
||||
* @memberof module:lib
|
||||
* @private
|
||||
* @inner
|
||||
*/
|
||||
function reorderDependencies(modules) {
|
||||
let index = 0;
|
||||
let present = new Map();
|
||||
let hold = new Map();
|
||||
reorder: while (index < modules.length) {
|
||||
let module = modules[index];
|
||||
if (semver.valid(module.version) === null) {
|
||||
throw new Error(`Invalid version '${module.version}' for module ${module.name}`);
|
||||
}
|
||||
|
||||
for (let [dependency, requirement] of Object.entries(module.dependencies)) {
|
||||
if (semver.validRange(requirement) === null) {
|
||||
throw new Error(
|
||||
`Invalid version range '${requirement}' for dependency ${dependency} on module ${module.name}`
|
||||
);
|
||||
}
|
||||
|
||||
if (present.has(dependency)) {
|
||||
if (!semver.satisfies(present.get(dependency), requirement)) {
|
||||
throw new Error(`Module ${module.name} requires ${dependency} ${requirement}`);
|
||||
}
|
||||
|
||||
// We have an unmet dependency, take it out and continue
|
||||
} else {
|
||||
if (hold.has(dependency)) {
|
||||
hold.get(dependency).push(module);
|
||||
} else {
|
||||
hold.set(dependency, [module]);
|
||||
}
|
||||
modules.splice(index, 1);
|
||||
continue reorder;
|
||||
}
|
||||
}
|
||||
|
||||
// No unmet dependencies, record and continue
|
||||
present.set(module.name, module.version);
|
||||
index += 1;
|
||||
|
||||
if (hold.has(module.name)) {
|
||||
modules.splice(index, 0, ...hold.get(module.name));
|
||||
hold.delete(module.name);
|
||||
}
|
||||
}
|
||||
|
||||
if (!hold.size) {
|
||||
return;
|
||||
}
|
||||
|
||||
// There are three reasons for a module to end up being held: The module depends
|
||||
// on a module that is missing, the module is part of a dependency loop, or the
|
||||
// the module depends on a module that satisfy any of these conditions.
|
||||
|
||||
let remaining = new Map();
|
||||
for (let heldModules of hold.values()) {
|
||||
for (let module of heldModules) {
|
||||
remaining.set(module.name, module);
|
||||
}
|
||||
}
|
||||
|
||||
// Start with a random module from the remaining modules
|
||||
for (let module of remaining.values()) {
|
||||
let cycle = [];
|
||||
while (true) {
|
||||
// Find an unmet dependency
|
||||
let dependency = Object.keys(module.dependencies).find(name => !present.has(name));
|
||||
|
||||
if (!remaining.has(dependency)) {
|
||||
// There's no module being held up by this dependency, the
|
||||
// dependency is missing.
|
||||
throw new Error(`Missing dependency ${dependency} for module ${module.name}`);
|
||||
}
|
||||
|
||||
if (cycle.includes(module.name)) {
|
||||
cycle.push(module.name);
|
||||
cycle.splice(0, cycle.indexOf(module.name));
|
||||
throw new Error(`Module dependency loop detected: ${cycle.join(" -> ")}`);
|
||||
}
|
||||
|
||||
cycle.push(module.name);
|
||||
module = remaining.get(dependency);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch a save with the given modules
|
||||
*
|
||||
* Adds the modules given by the modules parameter to the save located
|
||||
* at savePath and rewrites the control.lua in the save to load the
|
||||
* modules that were added. Will also remove any previous module
|
||||
* located in the save.
|
||||
*
|
||||
* @param {string} savePath - Path to the Factorio save to patch.
|
||||
* @param {Array<Object>} modules - Description of the modules to patch.
|
||||
* @memberof module:lib
|
||||
*/
|
||||
async function patch(savePath, modules) {
|
||||
let zip = await JSZip.loadAsync(await fs.readFile(savePath));
|
||||
let root = zip.folder(findRoot(zip));
|
||||
|
||||
let patchInfoFile = root.file("clusterio.json");
|
||||
let patchInfo;
|
||||
if (patchInfoFile !== null) {
|
||||
let content = await patchInfoFile.async("string");
|
||||
patchInfo = JSON.parse(content);
|
||||
|
||||
// No info file present, try to detect if it's a known compatible scenario.
|
||||
} else {
|
||||
let controlStream = root.file("control.lua").nodeStream("nodebuffer");
|
||||
let controlHash = await libHash.hashStream(controlStream);
|
||||
|
||||
if (controlHash in knownScenarios) {
|
||||
patchInfo = {
|
||||
"scenario": knownScenarios[controlHash],
|
||||
};
|
||||
} else {
|
||||
throw new Error(`Unable to patch save, unknown scenario (${controlHash})`);
|
||||
}
|
||||
}
|
||||
|
||||
// Increment patch number
|
||||
patchInfo["patch_number"] = (patchInfo["patch_number"] || 0) + 1;
|
||||
|
||||
// Remove any existing modules from the save
|
||||
patchInfo["modules"] = [];
|
||||
for (let file of root.file(/^modules\//)) {
|
||||
zip.remove(file.name);
|
||||
}
|
||||
|
||||
reorderDependencies(modules);
|
||||
|
||||
// Add the modules to the save.
|
||||
for (let module of modules) {
|
||||
let moduleDir = root.folder(`modules/${module.name}`);
|
||||
let moduleInfo = {
|
||||
"name": module.name,
|
||||
"files": [],
|
||||
};
|
||||
|
||||
let dirs = [[module.path, ""]];
|
||||
while (dirs.length) {
|
||||
let [dir, relativeDir] = dirs.pop();
|
||||
for (let entry of await fs.readdir(dir, { withFileTypes: true })) {
|
||||
let fsPath = path.join(dir, entry.name);
|
||||
let relativePath = path.posix.join(relativeDir, entry.name);
|
||||
|
||||
if (entry.isFile()) {
|
||||
moduleDir.file(relativePath, await fs.readFile(fsPath));
|
||||
moduleInfo["files"].push({
|
||||
"path": relativePath,
|
||||
"load": module.load.includes(relativePath),
|
||||
"require": module.require.includes(relativePath),
|
||||
});
|
||||
|
||||
} else if (entry.isDirectory()) {
|
||||
dirs.push([fsPath, relativePath]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
patchInfo["modules"].push(moduleInfo);
|
||||
}
|
||||
|
||||
// Add loading code and patch info
|
||||
root.file("control.lua", generateLoader(patchInfo));
|
||||
root.file("clusterio.json", JSON.stringify(patchInfo, null, 4));
|
||||
|
||||
// Write back the save
|
||||
let tempSavePath = `${savePath}.tmp`;
|
||||
let stream = zip.generateNodeStream({ compression: "DEFLATE" });
|
||||
let fd = await fs.open(tempSavePath, "w");
|
||||
try {
|
||||
let pipe = stream.pipe(fs.createWriteStream("", { fd, autoClose: false }));
|
||||
await events.once(pipe, "finish");
|
||||
await fs.fsync(fd);
|
||||
} finally {
|
||||
await fs.close(fd);
|
||||
}
|
||||
await fs.rename(tempSavePath, savePath);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
patch,
|
||||
|
||||
// For testing only
|
||||
_generateLoader: generateLoader,
|
||||
_reorderDependencies: reorderDependencies,
|
||||
};
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,246 @@
|
||||
/**
|
||||
* @module lib/file_ops
|
||||
*/
|
||||
"use strict";
|
||||
const fs = require("fs-extra");
|
||||
const path = require("path");
|
||||
const crypto = require("crypto"); // needed for getTempFile
|
||||
|
||||
/**
|
||||
* Returns the newest file in a directory
|
||||
*
|
||||
* @param {string} directory - The directory to look for the newest file in.
|
||||
* @param {function(string): boolean} filter -
|
||||
* Optional function to filter out files to ignore. Should return true
|
||||
* if the file is to be considered.
|
||||
* @returns {Promise<string|undefined>}
|
||||
* Name of the file with the newest timestamp or null if the
|
||||
* directory contains no files.
|
||||
*/
|
||||
async function getNewestFile(directory, filter = (name) => true) {
|
||||
let newestTime = new Date(0);
|
||||
let newestFile;
|
||||
for (let entry of await fs.readdir(directory, { withFileTypes: true })) {
|
||||
if (entry.isFile()) {
|
||||
let stat = await fs.stat(path.join(directory, entry.name));
|
||||
if (filter(entry.name) && stat.mtime > newestTime) {
|
||||
newestTime = stat.mtime;
|
||||
newestFile = entry.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newestFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the total size of all files in a directory
|
||||
*
|
||||
* Sums up the file size of all files in the given directory if it exists.
|
||||
* Error reading the size of files are ignored, and error reading the
|
||||
* directory will result in 0 being returned.
|
||||
*
|
||||
* @param {string} directory - The directory to sum files in.
|
||||
* @returns {Promise<number>} The size in bytes of all files in the directory.
|
||||
*/
|
||||
async function directorySize(directory) {
|
||||
let dirEntries;
|
||||
try {
|
||||
dirEntries = await fs.readdir(directory, { withFileTypes: true });
|
||||
} catch (err) {
|
||||
if (err.code === "ENOENT") {
|
||||
return 0;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
let statTasks = [];
|
||||
for (let entry of dirEntries) {
|
||||
if (entry.isFile()) {
|
||||
statTasks.push(fs
|
||||
.stat(path.join(directory, entry.name))
|
||||
.then(stat => stat.size, _ => 0)
|
||||
);
|
||||
}
|
||||
}
|
||||
return (await Promise.all(statTasks)).reduce((a, v) => a + v, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies name in case it already exisist at the given directory
|
||||
*
|
||||
* Checks the directory passed if it already contains a file or folder with
|
||||
* the given name, if it does not returns name unmodified, otherwise it
|
||||
* returns a modified name that does not exist in the directory.
|
||||
*
|
||||
* Warning: this function should not be relied upon to be accurate for
|
||||
* security sensitive applications. The selection process is inherently
|
||||
* racy and a file or folder may have been created in the folder by the time
|
||||
* this function returns.
|
||||
*
|
||||
* @param {string} directory - directory to check in.
|
||||
* @param {string} name - file name to check for, may have extension.
|
||||
* @param {string} extension - dot extension used for the file name.
|
||||
* @returns {Promise<string>} modified name with extension that likely does
|
||||
* not exist in the folder
|
||||
*/
|
||||
async function findUnusedName(directory, name, extension = "") {
|
||||
if (extension && name.endsWith(extension)) {
|
||||
name = name.slice(0, -extension.length);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
if (!await fs.pathExists(path.join(directory, `${name}${extension}`))) {
|
||||
return `${name}${extension}`;
|
||||
}
|
||||
|
||||
let match = /^(.*?)(-(\d+))?$/.exec(name);
|
||||
if (!match[2]) {
|
||||
name = `${match[1]}-2`;
|
||||
} else {
|
||||
name = `${match[1]}-${Number.parseInt(match[3], 10) + 1}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Warning: this function should not be relied upon to be accurate for
|
||||
* security sensitive applications. The selection process is inherently
|
||||
* racy and a file or folder may have been created in the folder by the time
|
||||
* this function returns.
|
||||
*
|
||||
* @param {string} prefix - Prefix for file
|
||||
* @param {string} suffix - Suffix for file
|
||||
* @param {string} tmpdir - Directory for temp file
|
||||
*/
|
||||
async function getTempFile(prefix, suffix, tmpdir) {
|
||||
prefix = (typeof prefix !== "undefined") ? prefix : "tmp.";
|
||||
suffix = (typeof suffix !== "undefined") ? suffix : "";
|
||||
tmpdir = (typeof tmpdir !== "undefined") ? tmpdir : "./";
|
||||
let fileName = path.join(prefix + crypto.randomBytes(16).toString("hex") + suffix);
|
||||
let freeFile = await findUnusedName(tmpdir, fileName);
|
||||
let fullPath = path.join(tmpdir, freeFile);
|
||||
return fullPath;
|
||||
}
|
||||
/**
|
||||
* Safely write data to a file
|
||||
*
|
||||
* Same as fs-extra.outputFile except the data is written to a temporary
|
||||
* file that's renamed over the target file. The name of the temporary file
|
||||
* is the same as the target file with the suffix `.tmp` added.
|
||||
*
|
||||
* If the operation fails it may leave behind the temporary file. This
|
||||
* should not be too much of an issue as the next time the same file is
|
||||
* written the temporary will be overwritten and renamed to the target file.
|
||||
*
|
||||
* @param {string} file - Path to file to write.
|
||||
* @param {string|Buffer} data - Content to write.
|
||||
* @param {object|string} options - see fs.writeFile, `flag` must not be set.
|
||||
*/
|
||||
async function safeOutputFile(file, data, options={}) {
|
||||
let directory = path.dirname(file);
|
||||
if (!await fs.pathExists(directory)) {
|
||||
await fs.mkdirs(directory);
|
||||
}
|
||||
let temporary = `${file}.tmp`;
|
||||
let fd = await fs.open(temporary, "w");
|
||||
try {
|
||||
await fs.writeFile(fd, data, options);
|
||||
await fs.fsync(fd);
|
||||
} finally {
|
||||
await fs.close(fd);
|
||||
}
|
||||
|
||||
await fs.rename(temporary, file);
|
||||
}
|
||||
|
||||
|
||||
// Reserved names by allmost all filesystems
|
||||
const badNames = [".", ".."];
|
||||
|
||||
// Reserved namespaces in Windows
|
||||
const oneToNine = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
const badWinNamespaces = [
|
||||
"CON", "PRN", "AUX", "NUL",
|
||||
...oneToNine.map(n => `COM${n}`),
|
||||
...oneToNine.map(n => `LPT${n}`),
|
||||
];
|
||||
|
||||
/**
|
||||
* Check if a string is a valid file name
|
||||
*
|
||||
* @param {string} name - Name to check
|
||||
* @throws Error if the name is unsuitable.
|
||||
*/
|
||||
function checkFilename(name) {
|
||||
// All of these are bad in Windows only, except for /, . and ..
|
||||
// See: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
|
||||
const badChars = /[<>:"\/\\|?*\x00-\x1f]/g;
|
||||
const badEnd = /[. ]$/;
|
||||
|
||||
if (typeof name !== "string") {
|
||||
throw new Error("must be a string");
|
||||
}
|
||||
|
||||
if (name === "") {
|
||||
throw new Error("cannot be empty");
|
||||
}
|
||||
|
||||
if (badChars.test(name)) {
|
||||
throw new Error('cannot contain <>:"\\/|=* or control characters');
|
||||
}
|
||||
|
||||
if (badNames.includes(name)) {
|
||||
throw new Error(
|
||||
`cannot be named ${name}`
|
||||
);
|
||||
}
|
||||
|
||||
if (badWinNamespaces.includes(name.toUpperCase().split(".")[0])) {
|
||||
throw new Error(
|
||||
"cannot be named any of CON PRN AUX NUL COM1-9 and LPT1-9"
|
||||
);
|
||||
}
|
||||
|
||||
if (badEnd.test(name)) {
|
||||
throw new Error("cannot end with . or space");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up string to be suitable for use as filename
|
||||
*
|
||||
* @param {string} name - Arbitrary name string
|
||||
* @returns {string} Filename suitable to use in the filesystem
|
||||
*/
|
||||
function cleanFilename(name) {
|
||||
// copied from checkFilename due to RegExp with global flag containing state.
|
||||
const badChars = /[<>:"\/\\|?*\x00-\x1f]/g;
|
||||
const badEnd = /[. ]$/;
|
||||
|
||||
if (typeof name !== "string") {
|
||||
throw new Error("name must be a string");
|
||||
}
|
||||
|
||||
if (name === "" || badNames.includes(name.toUpperCase())) {
|
||||
name += "_";
|
||||
}
|
||||
|
||||
if (badWinNamespaces.includes(name.toUpperCase().split(".")[0])) {
|
||||
name = [`${name.split(".")[0]}_`, ...name.split(".").slice(1)].join(".");
|
||||
}
|
||||
|
||||
name = name.replace(badChars, "_");
|
||||
name = name.replace(badEnd, "_");
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
getNewestFile,
|
||||
directorySize,
|
||||
findUnusedName,
|
||||
getTempFile,
|
||||
safeOutputFile,
|
||||
checkFilename,
|
||||
cleanFilename,
|
||||
};
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Hashing functions
|
||||
* @module lib/hash
|
||||
*/
|
||||
"use strict";
|
||||
const crypto = require("crypto");
|
||||
const fs = require("fs-extra");
|
||||
|
||||
|
||||
/**
|
||||
* Returns a promise that resolves to the SHA1 hash of the stream given
|
||||
*
|
||||
* @param {Stream} stream - Node stream of the content to hash.
|
||||
* @returns {Promise<string>} hash of the stream.
|
||||
*/
|
||||
function hashStream(stream) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let hasher = crypto.createHash("sha1");
|
||||
hasher.setEncoding("hex");
|
||||
hasher.on("finish", () => {
|
||||
resolve(hasher.read());
|
||||
});
|
||||
|
||||
stream.on("error", (error) => {
|
||||
// the docs doesn't say anything about what's passed on error
|
||||
reject(error);
|
||||
});
|
||||
|
||||
stream.pipe(hasher);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a promise that resolves to the SHA1 hash of the file given by path
|
||||
*
|
||||
* @param {string} path - Path to the file to hash.
|
||||
* @returns {Promise<string>} hash of the file given.
|
||||
*/
|
||||
function hashFile(path) {
|
||||
return hashStream(fs.createReadStream(path));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hashStream,
|
||||
hashFile,
|
||||
};
|
||||
@@ -0,0 +1,290 @@
|
||||
/**
|
||||
* Collection of small utilites that are useful in multiple places.
|
||||
* @module lib/helpers
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Return a string describing the type of the value passed
|
||||
*
|
||||
* Works the same as typeof, excpet that null and array types get their
|
||||
* own string.
|
||||
*
|
||||
* @param {*} value - value to return the type of.
|
||||
* @returns {string} basic type of the value passed.
|
||||
* @static
|
||||
*/
|
||||
function basicType(value) {
|
||||
if (value === null) { return "null"; }
|
||||
if (value instanceof Array) { return "array"; }
|
||||
return typeof value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Asynchronously wait for the given duration
|
||||
*
|
||||
* @param {number} duration - Time to wait for in milliseconds.
|
||||
* @static
|
||||
*/
|
||||
async function wait(duration) {
|
||||
await new Promise(resolve => { setTimeout(resolve, duration); });
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Resolve a promise with a timeout.
|
||||
*
|
||||
* @param {Promise} promise - Promise to wait for.
|
||||
* @param {number} time - Maximum time im milliseconds to wait for.
|
||||
* @param {*=} timeoutResult - Value to return if the operation timed out.
|
||||
* @static
|
||||
*/
|
||||
async function timeout(promise, time, timeoutResult) {
|
||||
let timer;
|
||||
try {
|
||||
return await Promise.race([
|
||||
promise,
|
||||
new Promise(resolve => {
|
||||
timer = setTimeout(() => resolve(timeoutResult), time);
|
||||
}),
|
||||
]);
|
||||
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read stream to the end and return its content
|
||||
*
|
||||
* Reads the stream given asynchronously until the end is reached and
|
||||
* returns all the data which was read from the stream.
|
||||
*
|
||||
* @param {Readable} stream - byte stream to read to the end.
|
||||
* @returns {Promise<Buffer>} content of the stream.
|
||||
* @static
|
||||
*/
|
||||
async function readStream(stream) {
|
||||
let chunks = [];
|
||||
for await (let chunk of stream) {
|
||||
// Support using ^Z to end input on Windows
|
||||
if (process.platform === "win32" && stream.isTTY && chunk.toString() === "\x1a\r\n") {
|
||||
break;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return Buffer.concat(chunks);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Escapes text for inclusion in a RegExp
|
||||
*
|
||||
* Adds \ character in front of special meta characters in the passsed in
|
||||
* text so that it can be embedded into a RegExp and only match the text.
|
||||
*
|
||||
* See https://stackoverflow.com/a/9310752
|
||||
*
|
||||
* @param {string} text - Text to escape RegExp meta chars in.
|
||||
* @returns {string} escaped text.
|
||||
* @static
|
||||
*/
|
||||
function escapeRegExp(text) {
|
||||
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
||||
}
|
||||
|
||||
/**
|
||||
* Format byte count for human readable display
|
||||
*
|
||||
* Shortens a large number of bytes using the kB/MB/GB/TB prefixes.
|
||||
* @param {number} bytes - Count of bytes to format.
|
||||
* @returns {string} formatted text.
|
||||
* @static
|
||||
*/
|
||||
function formatBytes(bytes) {
|
||||
if (bytes === 0) {
|
||||
return "0\u{A0}Bytes"; // No-break space
|
||||
}
|
||||
|
||||
let units = ["\u{A0}Bytes", "\u{A0}kB", "\u{A0}MB", "\u{A0}GB", "\u{A0}TB"];
|
||||
let factor = 1000;
|
||||
let power = Math.min(Math.floor(Math.log(bytes) / Math.log(factor)), units.length);
|
||||
return (power > 0 ? (bytes / factor ** power).toFixed(2) : bytes) + units[power];
|
||||
}
|
||||
|
||||
function parseSearchIdentifier(pos, input) {
|
||||
// character = ? all characters excluding space : " ?
|
||||
// identifier = 1*character
|
||||
let startPos = pos;
|
||||
while (pos < input.length && ![" ", '"', ":"].includes(input.charAt(pos))) {
|
||||
pos += 1;
|
||||
}
|
||||
return [pos, input.slice(startPos, pos)];
|
||||
}
|
||||
|
||||
function parseSearchWord(pos, input) {
|
||||
// non quote character = ? all characters excluding " ?
|
||||
// word op = "-"
|
||||
// word = [ word op ], ( identifier | '"', *non quote character, '"' )
|
||||
let word = {
|
||||
type: "word",
|
||||
};
|
||||
if (input.charAt(pos) === "-") {
|
||||
word.exclude = true;
|
||||
pos += 1;
|
||||
}
|
||||
if (input.charAt(pos) === '"') {
|
||||
pos += 1;
|
||||
let startPos = pos;
|
||||
let endPos = pos;
|
||||
while (pos < input.length && input.charAt(pos) !== '"') {
|
||||
pos += 1;
|
||||
endPos += 1;
|
||||
}
|
||||
if (input.charAt(pos) === '"') {
|
||||
pos += 1;
|
||||
}
|
||||
word.value = input.slice(startPos, endPos);
|
||||
} else {
|
||||
([pos, word.value] = parseSearchIdentifier(pos, input));
|
||||
}
|
||||
return [pos, word];
|
||||
}
|
||||
|
||||
function parseSearchTerm(pos, input, attributes, issues) {
|
||||
// attribute = identifier, ':', word
|
||||
// term = word | attribute
|
||||
let term;
|
||||
if (["-", '"'].includes(input.charAt(pos))) {
|
||||
([pos, term] = parseSearchWord(pos, input));
|
||||
} else {
|
||||
let identifier;
|
||||
([pos, identifier] = parseSearchIdentifier(pos, input));
|
||||
if (input.charAt(pos) === ":") {
|
||||
pos += 1;
|
||||
identifier = identifier.toLowerCase();
|
||||
if (!Object.prototype.hasOwnProperty.call(attributes, identifier)) {
|
||||
issues.push(`Unregonized attribute "${identifier}", use quotes to escape colons`);
|
||||
} else if (attributes[identifier] === "word") {
|
||||
let value;
|
||||
([pos, value] = parseSearchWord(pos, input));
|
||||
term = { type: "attribute", name: identifier, value };
|
||||
} else {
|
||||
throw new Error(`Bad attribute format ${attributes[identifier]} for ${identifier}`);
|
||||
}
|
||||
} else {
|
||||
term = { type: "word", value: identifier };
|
||||
}
|
||||
}
|
||||
return [pos, term];
|
||||
}
|
||||
|
||||
/**
|
||||
* Match a parsed word term with the given texts.
|
||||
*
|
||||
* Returns true if the passed word term in the search matches at least one
|
||||
* of the passed text snippets. If the word mode is exclude then returns
|
||||
* true if none of the passed text snippets match.
|
||||
*
|
||||
* @param {lib/helpers.ParsedTerm} word - Word to match.
|
||||
* @param {...string} texts - Text to match word in.
|
||||
* @returns {boolean} true if the word matches.
|
||||
*/
|
||||
function wordMatches(word, ...texts) {
|
||||
if (word.type !== "word") {
|
||||
throw Error("wordMatches: parameter is not a word");
|
||||
}
|
||||
let matches = false;
|
||||
for (let text of texts) {
|
||||
if (text.includes(word.value)) {
|
||||
matches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (word.exclude) {
|
||||
matches = !matches;
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {object} module:lib~ParsedTerm
|
||||
* @property {string} type - Type of term, either attribute or word.
|
||||
* @property {string=} name - attribute only: Name of attribute.
|
||||
* @property {boolean=} exclude - word only: exclude results with this word.
|
||||
* @property {string | module:lib.ParsedTerm} value -
|
||||
* Parsed value of this term. Is a string if type is word, and a
|
||||
* ParsedTerm if type is attribute.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} module:lib~ParsedSearch
|
||||
* @property {Array<module:lib~ParsedTerm>} terms -
|
||||
* Parsed result of search terms.
|
||||
* @property {Array<string>} issues -
|
||||
* Issues detected while parsing the seach string.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parse a search string with optional attributes
|
||||
*
|
||||
* Parses the given input as a search expression consisting of space
|
||||
* delimited words and attributes:word pairs. For aattributes to be
|
||||
* recognized they need to be passed as name: "word" in the attributes
|
||||
* parameter.
|
||||
*
|
||||
* Words in the search expression can optionally be prefixed by a - to
|
||||
* search for results that does not contain that word. E.g. -author contains
|
||||
* all results not matching author.
|
||||
*
|
||||
* If parsing fails an string describing the problem will be added to the
|
||||
* issues array returned and parsing will resume at some arbitrary point.
|
||||
* The issues should be shown to the end user so that they can correct their
|
||||
* search.
|
||||
*
|
||||
* @param {string} input - Search expression to parse.
|
||||
* @param {Object<string, string>} attributes -
|
||||
* Recognized attributes and their format. Currently only word is
|
||||
* supported.
|
||||
* @returns {module:lib~ParsedSearch} parsed terms of the search.
|
||||
* @static
|
||||
*/
|
||||
function parseSearchString(input, attributes = {}) {
|
||||
// whitespace = 1*" "
|
||||
// search = [ term, *( [ whitespace ], term ) ]
|
||||
input = input.trim();
|
||||
let parsed = {
|
||||
terms: [],
|
||||
issues: [],
|
||||
};
|
||||
let pos = 0;
|
||||
while (pos < input.length) {
|
||||
let term;
|
||||
([pos, term] = parseSearchTerm(pos, input, attributes, parsed.issues));
|
||||
if (term) {
|
||||
parsed.terms.push(term);
|
||||
}
|
||||
while (pos < input.length && input.charAt(pos) === " ") {
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
// istanbul ignore if (should not be possible)
|
||||
if (pos !== input.length) {
|
||||
throw new Error(`parse search ended at ${pos} which is not the end of the input (${input.length})`);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
basicType,
|
||||
wait,
|
||||
timeout,
|
||||
readStream,
|
||||
escapeRegExp,
|
||||
formatBytes,
|
||||
wordMatches,
|
||||
parseSearchString,
|
||||
};
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Factorio Compatible INI parser
|
||||
*
|
||||
* @module lib/ini
|
||||
* @author Hornwitser
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Parse INI file into object of sections
|
||||
* @param {string} input - content of INI file to parse.
|
||||
* @returns {object<string, object<string, string>>}
|
||||
* sections parsed from the ini file.
|
||||
*/
|
||||
function parse(input) {
|
||||
const sections = {};
|
||||
let currentSection = sections;
|
||||
const lines = input.split(/\r\n|\n/);
|
||||
for (let number = 1; number <= lines.length; number++) {
|
||||
let line = lines[number - 1].replace(/^[\t ]+/, "");
|
||||
if (!line || line[0] === "#" || line[0] === ";") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read section header
|
||||
if (line[0] === "[") {
|
||||
line = line.replace(/[\t ]+$/, "");
|
||||
if (line.slice(-1) !== "]") {
|
||||
throw new Error(`Unterminated section header on line ${number}`);
|
||||
}
|
||||
const name = line.slice(1, -1);
|
||||
if (Object.prototype.hasOwnProperty.call(sections, name)) {
|
||||
throw new Error(`Duplicated section [${name}] on line ${number}`);
|
||||
}
|
||||
currentSection = {};
|
||||
sections[name] = currentSection;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read key value
|
||||
const equalIndex = line.indexOf("=");
|
||||
if (equalIndex === -1) {
|
||||
throw new Error(`Missing value for key ${line} on line ${number}`);
|
||||
}
|
||||
const key = line.slice(0, equalIndex);
|
||||
if (Object.prototype.hasOwnProperty.call(currentSection, key)) {
|
||||
throw new Error(`Duplicated key ${key} on line ${number}`);
|
||||
}
|
||||
const value = line.slice(equalIndex + 1);
|
||||
currentSection[key] = value;
|
||||
}
|
||||
|
||||
return sections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize an object of section definitions into an INI file
|
||||
* @param {object<string, object<string, string>>} sections -
|
||||
* Sections to encode into ini format
|
||||
* @returns {string} sections serialized into INI format
|
||||
*/
|
||||
function stringify(sections) {
|
||||
let serialized = [];
|
||||
let first = true;
|
||||
|
||||
for (let [key, value] of Object.entries(sections)) {
|
||||
if (typeof value === "string") {
|
||||
serialized.push(`${key}=${value}`);
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
|
||||
for (let [section, content] of Object.entries(sections)) {
|
||||
if (typeof content === "object") {
|
||||
if (!first) {
|
||||
serialized.push("");
|
||||
}
|
||||
first = false;
|
||||
serialized.push(`[${section}]`);
|
||||
for (let [key, value] of Object.entries(content)) {
|
||||
serialized.push(`${key}=${value}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (serialized.length) { serialized.push(""); }
|
||||
return serialized.join("\n");
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parse,
|
||||
stringify,
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
/* eslint-disable node/global-require */
|
||||
"use strict";
|
||||
// eslint-disable-next-line node/no-process-env
|
||||
if (process.env.APP_ENV === "browser") {
|
||||
// eslint-disable-next-line node/no-extraneous-require
|
||||
module.exports = require("fast-deep-equal/es6");
|
||||
} else {
|
||||
module.exports = require("util").isDeepStrictEqual;
|
||||
}
|
||||
@@ -0,0 +1,692 @@
|
||||
// Connection adapters for Links
|
||||
"use strict";
|
||||
const assert = require("assert").strict;
|
||||
const events = require("events");
|
||||
// eslint-disable-next-line node/no-process-env
|
||||
const WebSocket = process.env.APP_ENV === "browser" ? window.WebSocket : require("ws");
|
||||
|
||||
const libData = require("../data");
|
||||
const libErrors = require("../errors");
|
||||
const { logger } = require("../logging");
|
||||
const ExponentialBackoff = require("../ExponentialBackoff");
|
||||
|
||||
|
||||
/**
|
||||
* Base connector for links
|
||||
*
|
||||
* @extends events.EventEmitter
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class BaseConnector extends events.EventEmitter {
|
||||
constructor(src, dst) {
|
||||
super();
|
||||
this._seq = 1;
|
||||
this.src = src;
|
||||
this.dst = dst;
|
||||
}
|
||||
|
||||
_reset() {
|
||||
this._seq = 1;
|
||||
}
|
||||
|
||||
_invalidate() {
|
||||
this._seq = 1;
|
||||
}
|
||||
|
||||
sendRequest(request, requestId, dst) {
|
||||
let seq = this._seq;
|
||||
this._seq += 1;
|
||||
let src = new libData.Address(this.src.type, this.src.id, requestId);
|
||||
let Request = request.constructor;
|
||||
let name = Request.plugin ? `${Request.plugin}:${Request.name}` : Request.name;
|
||||
let hasData = Boolean(Request.jsonSchema);
|
||||
const message = new libData.MessageRequest(seq, src, dst, name, hasData ? request : undefined);
|
||||
this.send(message);
|
||||
return seq;
|
||||
}
|
||||
|
||||
sendResponse(response, dst, src = this.src) {
|
||||
let seq = this._seq;
|
||||
this._seq += 1;
|
||||
const message = new libData.MessageResponse(seq, src, dst, response);
|
||||
this.send(message);
|
||||
return seq;
|
||||
}
|
||||
|
||||
sendResponseError(error, dst, src = this.src) {
|
||||
let seq = this._seq;
|
||||
this._seq += 1;
|
||||
const message = new libData.MessageResponseError(seq, src, dst, error);
|
||||
this.send(message);
|
||||
return seq;
|
||||
}
|
||||
|
||||
sendEvent(event, dst) {
|
||||
let seq = this._seq;
|
||||
this._seq += 1;
|
||||
let Event = event.constructor;
|
||||
let name = Event.plugin ? `${Event.plugin}:${Event.name}` : Event.name;
|
||||
let hasData = Boolean(Event.jsonSchema);
|
||||
const message = new libData.MessageEvent(seq, this.src, dst, name, hasData ? event : undefined);
|
||||
this.send(message);
|
||||
return seq;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base connector for links
|
||||
*
|
||||
* @extends module:lib.BaseConnector
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class WebSocketBaseConnector extends BaseConnector {
|
||||
constructor(src, dst) {
|
||||
super(src, dst);
|
||||
|
||||
// One of closed, connecting (client only), connected and resuming.
|
||||
this._state = "closed";
|
||||
this._closing = false;
|
||||
this._socket = null;
|
||||
this._lastHeartbeat = null;
|
||||
this._heartbeatId = null;
|
||||
this._heartbeatInterval = null;
|
||||
this._lastReceivedSeq = undefined;
|
||||
this._sendBuffer = [];
|
||||
}
|
||||
|
||||
_reset() {
|
||||
this._state = "closed";
|
||||
this._closing = false;
|
||||
assert(this._socket === null);
|
||||
this._lastHeartbeat = null;
|
||||
assert(this._heartbeatId === null);
|
||||
this._heartbeatInterval = null;
|
||||
this._lastReceivedSeq = undefined;
|
||||
this._sendBuffer.length = 0;
|
||||
super._reset();
|
||||
}
|
||||
|
||||
_invalidate() {
|
||||
this._state = "connecting";
|
||||
assert(this._closing === false);
|
||||
assert(this._socket);
|
||||
this._lastHeartbeat = null;
|
||||
assert(this._heartbeatId === null);
|
||||
this._heartbeatInterval = null;
|
||||
this._lastReceivedSeq = undefined;
|
||||
this._sendBuffer.length = 0;
|
||||
super._invalidate();
|
||||
}
|
||||
|
||||
_check(...expectedStates) {
|
||||
if (!expectedStates.includes(this._state)) {
|
||||
throw new Error(
|
||||
`Expected state ${expectedStates} but state is ${this._state}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
_dropSendBufferSeq(seq) {
|
||||
if (seq === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
let dropCount = 0;
|
||||
for (let index = 0; index < this._sendBuffer.length; index++) {
|
||||
if (this._sendBuffer[index].seq <= seq) {
|
||||
dropCount += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
this._sendBuffer.splice(0, dropCount);
|
||||
}
|
||||
|
||||
_parseMessage(text) {
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(text);
|
||||
} catch (err) {
|
||||
this.close(4000, "Malformed JSON");
|
||||
return undefined;
|
||||
}
|
||||
if (!libData.Message.validate(json)) {
|
||||
logger.error(`Received malformed message: ${text}`);
|
||||
// logger.error(JSON.stringify(libData.Message.validate.errors, null, 4));
|
||||
this.close(4000, "Malformed message");
|
||||
return undefined;
|
||||
}
|
||||
return libData.Message.fromJSON(json);
|
||||
}
|
||||
|
||||
_processMessage(message) {
|
||||
if (message.type === "heartbeat") {
|
||||
this._processHeartbeat(message);
|
||||
|
||||
} else if (message.type === "disconnect") {
|
||||
if (message.data === "ready") {
|
||||
this.emit("disconnectReady");
|
||||
|
||||
} else if (message.data === "prepare") {
|
||||
this.setClosing();
|
||||
this.emit("disconnectPrepare");
|
||||
}
|
||||
|
||||
} else {
|
||||
if (message.seq !== undefined) {
|
||||
this._lastReceivedSeq = message.seq;
|
||||
}
|
||||
|
||||
this.emit("message", message);
|
||||
}
|
||||
}
|
||||
|
||||
_doHeartbeat() {
|
||||
this._check("connected");
|
||||
if (Date.now() - this._lastHeartbeat > 2000 * this._heartbeatInterval) {
|
||||
logger.verbose("Connector | closing after heartbeat timed out");
|
||||
// eslint-disable-next-line node/no-process-env
|
||||
if (process.env.APP_ENV === "browser") {
|
||||
this._socket.close(4008, "Heartbeat timeout");
|
||||
} else {
|
||||
this._socket.terminate();
|
||||
}
|
||||
|
||||
} else {
|
||||
this._sendInternal(new libData.MessageHeartbeat(this._lastReceivedSeq));
|
||||
}
|
||||
}
|
||||
|
||||
_processHeartbeat(message) {
|
||||
this._lastHeartbeat = Date.now();
|
||||
this._dropSendBufferSeq(message.seq);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start sending heartbeats
|
||||
*/
|
||||
startHeartbeat() {
|
||||
if (this._heartbeatId) {
|
||||
throw new Error("heartbeat is already running");
|
||||
}
|
||||
this._lastHeartbeat = Date.now();
|
||||
this._heartbeatId = setInterval(() => {
|
||||
this._doHeartbeat();
|
||||
}, this._heartbeatInterval * 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop sending heartbeats
|
||||
*/
|
||||
stopHeartbeat() {
|
||||
if (this._heartbeatId) {
|
||||
clearInterval(this._heartbeatId);
|
||||
this._heartbeatId = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message over the socket
|
||||
*
|
||||
* This is a low level method that should only be used for implementing
|
||||
* links. See sendTo for sending requests and events.
|
||||
*
|
||||
* @param {module:lib.Message} message - Message to send.
|
||||
*/
|
||||
send(message) {
|
||||
if (!["connected", "resuming"].includes(this._state)) {
|
||||
throw new libErrors.SessionLost("No session");
|
||||
}
|
||||
|
||||
this._sendBuffer.push(message);
|
||||
if (this._state === "connected") {
|
||||
this._sendInternal(message);
|
||||
}
|
||||
}
|
||||
|
||||
_sendInternal(message) {
|
||||
this._socket.send(JSON.stringify(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set connection state to closing
|
||||
*
|
||||
* Signal the connection is about to close and that once the close
|
||||
* frame is received it should be considered finished.
|
||||
*/
|
||||
setClosing() {
|
||||
throw new Error("Abstract function");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gracefully disconnect
|
||||
*
|
||||
* Sets closing flag an initiates the disconnect sequence.
|
||||
*/
|
||||
async disconnect() {
|
||||
if (this._state !== "connected") {
|
||||
await this.close(1000, "Disconnect");
|
||||
return;
|
||||
}
|
||||
|
||||
this.setClosing();
|
||||
this.send(new libData.MessageDisconnect("prepare"));
|
||||
|
||||
let timeout;
|
||||
let waitTimeout = new Promise(resolve => { timeout = setTimeout(resolve, 10000); }); // Make Configurable?
|
||||
try {
|
||||
await Promise.race([
|
||||
waitTimeout,
|
||||
events.once(this, "close"),
|
||||
events.once(this, "disconnectReady"),
|
||||
]);
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
|
||||
await this.close(1000, "Disconnect");
|
||||
}
|
||||
|
||||
/**
|
||||
* True if the connection is established and active
|
||||
*/
|
||||
get connected() {
|
||||
return !this._closing && this._state === "connected";
|
||||
}
|
||||
|
||||
/**
|
||||
* True if the connection is closing down or closed
|
||||
*/
|
||||
get closing() {
|
||||
return this._closing || this._state === "closed";
|
||||
}
|
||||
|
||||
/**
|
||||
* True if the connector currently has a valid session that is either
|
||||
* connected or in the process of resuming.
|
||||
*/
|
||||
get hasSession() {
|
||||
return ["connected", "resuming"].includes(this._state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connector for controller clients
|
||||
*
|
||||
* @extends module:lib.WebSocketBaseConnector
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class WebSocketClientConnector extends WebSocketBaseConnector {
|
||||
constructor(url, maxReconnectDelay, tlsCa = null) {
|
||||
super(undefined, new libData.Address(libData.Address.controller, 0));
|
||||
|
||||
this._url = url;
|
||||
this._backoff = new ExponentialBackoff({ max: maxReconnectDelay });
|
||||
this._tlsCa = tlsCa;
|
||||
|
||||
// The following states are used in the client connector
|
||||
// closed: Not connected
|
||||
// connecting: Attempting to connect to server.
|
||||
// resuming: Attempting to resume an existing connection to the server.
|
||||
// connected: Connection is online
|
||||
this._reconnectId = null;
|
||||
this._sessionToken = null;
|
||||
this._sessionTimeout = null;
|
||||
this._startedResuming = null;
|
||||
}
|
||||
|
||||
_reset() {
|
||||
clearTimeout(this._reconnectId);
|
||||
this._reconnectId = null;
|
||||
this._sessionToken = null;
|
||||
this._sessionTimeout = null;
|
||||
this._startedResuming = null;
|
||||
super._reset();
|
||||
this.src = undefined;
|
||||
}
|
||||
|
||||
_invalidate() {
|
||||
assert(this._reconnectId === null);
|
||||
this._sessionToken = null;
|
||||
this._sessionTimeout = null;
|
||||
this._startedResuming = null;
|
||||
super._invalidate();
|
||||
this.src = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a handshake message over the socket
|
||||
*
|
||||
* Used in the register function in order to send the handshake messages
|
||||
* over the WebSocket.
|
||||
*
|
||||
* @param {module:lib.Message} message - Message to send.
|
||||
*/
|
||||
sendHandshake(message) {
|
||||
this._check("connecting", "resuming");
|
||||
this._sendInternal(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the connection with the given reason.
|
||||
*
|
||||
* Sends a close frame and disconnects the connector.
|
||||
*
|
||||
* @param {number} code - WebSocket close code.
|
||||
* @param {string} reason - WebSocket close reason.
|
||||
*/
|
||||
async close(code, reason) {
|
||||
if (this._state === "closed") {
|
||||
return;
|
||||
}
|
||||
|
||||
this._closing = true;
|
||||
if (this._state === "connected" || this._socket) {
|
||||
this._socket.close(code, reason);
|
||||
await events.once(this, "close");
|
||||
|
||||
} else {
|
||||
this._reset();
|
||||
this.emit("close");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the connection is expected to close
|
||||
*
|
||||
* Sets the internal flag to signal not to attempt to resume the
|
||||
* connection if the server closes it. This will close the connection
|
||||
* if it's currently attempting to connect or resume.
|
||||
*/
|
||||
setClosing() {
|
||||
if (this._state === "closed") {
|
||||
return;
|
||||
}
|
||||
|
||||
this._closing = true;
|
||||
if (this._state === "connected") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._socket) {
|
||||
this._socket.close(1000, "Connector closing");
|
||||
|
||||
} else {
|
||||
this._reset();
|
||||
this.emit("close");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the controller
|
||||
*/
|
||||
async connect() {
|
||||
this._check("closed");
|
||||
this._state = "connecting";
|
||||
|
||||
this._doConnect();
|
||||
|
||||
// Wait for the connection to be ready
|
||||
await events.once(this, "connect");
|
||||
}
|
||||
|
||||
_doConnect() {
|
||||
let url = new URL(this._url);
|
||||
url.pathname += "api/socket";
|
||||
url.protocol = url.protocol.replace("http", "ws");
|
||||
|
||||
// Open WebSocket to controller
|
||||
logger.verbose(`Connector | connecting to ${url}`);
|
||||
|
||||
// eslint-disable-next-line node/no-process-env
|
||||
if (process.env.APP_ENV === "browser") {
|
||||
this._socket = new WebSocket(url);
|
||||
|
||||
} else {
|
||||
let options = {};
|
||||
if (this._tlsCa) { options.ca = this._tlsCa; }
|
||||
this._socket = new WebSocket(url, options);
|
||||
}
|
||||
|
||||
this._attachSocketHandlers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt re-establishing the connecting again
|
||||
*/
|
||||
reconnect() {
|
||||
if (this._socket) {
|
||||
throw new Error("Cannot reconnect while socket is open");
|
||||
}
|
||||
|
||||
if (this._reconnectId !== null) {
|
||||
logger.error("Unexpected double call to reconnect");
|
||||
}
|
||||
|
||||
let delay = this._backoff.delay();
|
||||
if (
|
||||
this._state === "resuming"
|
||||
&& this._startedResuming + this._sessionTimeout * 1000 < Date.now() + delay
|
||||
) {
|
||||
logger.error("Connector | Session timed out trying to resume");
|
||||
this._reset();
|
||||
if (this._disconnecting) {
|
||||
this._reset();
|
||||
this.emit("close");
|
||||
} else {
|
||||
this._state = "connecting";
|
||||
this.emit("invalidate");
|
||||
}
|
||||
}
|
||||
logger.verbose(
|
||||
`Connector | waiting ${(Math.round(delay / 10) / 100)} seconds for reconnect`
|
||||
);
|
||||
this._reconnectId = setTimeout(() => {
|
||||
this._reconnectId = null;
|
||||
this._doConnect();
|
||||
}, delay);
|
||||
this._reconnectTime = Date.now() + delay;
|
||||
}
|
||||
|
||||
_attachSocketHandlers() {
|
||||
this._socket.onclose = event => {
|
||||
const previousState = this._state;
|
||||
|
||||
// Authentication failed
|
||||
if (event.code === 4003) {
|
||||
this.emit("error", new libErrors.AuthenticationFailed(event.reason));
|
||||
this._closing = true;
|
||||
}
|
||||
|
||||
this._socket = null;
|
||||
if (this._state === "connected") {
|
||||
this.stopHeartbeat();
|
||||
if (this._closing) {
|
||||
this._reset();
|
||||
this.emit("close");
|
||||
|
||||
} else {
|
||||
this._state = "resuming";
|
||||
this._startedResuming = Date.now();
|
||||
this.reconnect();
|
||||
this.emit("drop");
|
||||
}
|
||||
|
||||
} else {
|
||||
// eslint-disable-next-line no-lonely-if
|
||||
if (this._closing) {
|
||||
this._reset();
|
||||
this.emit("close");
|
||||
|
||||
} else {
|
||||
this.reconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// Log must be sent after state change is complete
|
||||
let message = `Connector | Close (code: ${event.code}, reason: ${event.reason})`;
|
||||
if (previousState === "connected" && event.code !== 1000) {
|
||||
logger.info(message);
|
||||
} else {
|
||||
logger.verbose(message);
|
||||
}
|
||||
};
|
||||
|
||||
this._socket.onerror = event => {
|
||||
// It's assumed that close is always called by ws
|
||||
let code = !event.error ? "" : `, code: ${event.error.code}`;
|
||||
let message = `Connector | Socket error: ${event.message || "unknown error"}${code}`;
|
||||
if (this._state === "connected") {
|
||||
logger.error(message);
|
||||
} else {
|
||||
// Don't log as error if not actually connected, prevents
|
||||
// log spam during reconnecting attempts.
|
||||
logger.verbose(message);
|
||||
}
|
||||
if (event.error) {
|
||||
// Abort connection if certificate validation failed
|
||||
if ([
|
||||
"CERT_HAS_EXPIRED",
|
||||
"DEPTH_ZERO_SELF_SIGNED_CERT",
|
||||
"ERR_TLS_CERT_ALTNAME_INVALID",
|
||||
].includes(event.error.code)) {
|
||||
this.emit("error", new libErrors.AuthenticationFailed(event.error.message));
|
||||
this._closing = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this._socket.onopen = () => {
|
||||
logger.verbose("Connector | Open");
|
||||
};
|
||||
|
||||
// Handle messages
|
||||
this._socket.onmessage = event => {
|
||||
let message = this._parseMessage(event.data);
|
||||
if (!message) {
|
||||
return;
|
||||
}
|
||||
if (["connecting", "resuming"].includes(this._state)) {
|
||||
this._processHandshake(message);
|
||||
|
||||
} else if (this._state === "connected") {
|
||||
this._processMessage(message);
|
||||
|
||||
} else {
|
||||
throw new Error(`Received message in unexpected state ${this._state}`);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
_processHandshake(message) {
|
||||
|
||||
let { type, data } = message;
|
||||
if (type === "hello") {
|
||||
logger.verbose(`Connector | received hello from controller version ${data.version}`);
|
||||
this.emit("hello", data);
|
||||
if (this._sessionToken) {
|
||||
logger.verbose("Connector | Attempting resume");
|
||||
this.sendHandshake(new libData.MessageResume(
|
||||
new libData.ResumeData(this._sessionToken, this._lastReceivedSeq)
|
||||
));
|
||||
} else {
|
||||
this.register();
|
||||
}
|
||||
|
||||
} else if (type === "ready") {
|
||||
logger.verbose("Connector | received ready from controller");
|
||||
this._state = "connected";
|
||||
this.src = data.src;
|
||||
this._sessionToken = data.sessionToken;
|
||||
this._sessionTimeout = data.sessionTimeout;
|
||||
this._heartbeatInterval = data.heartbeatInterval;
|
||||
this.startHeartbeat();
|
||||
for (let bufferedMessage of this._sendBuffer) {
|
||||
this._sendInternal(bufferedMessage);
|
||||
}
|
||||
this.emit("connect", data);
|
||||
|
||||
} else if (type === "continue") {
|
||||
logger.info("Connector | resuming existing session");
|
||||
this._state = "connected";
|
||||
this._heartbeatInterval = data.heartbeatInterval;
|
||||
this._sessionTimeout = data.sessionTimeout;
|
||||
this.startHeartbeat();
|
||||
this._dropSendBufferSeq(data.lastSeq);
|
||||
for (let bufferedMessage of this._sendBuffer) {
|
||||
this._sendInternal(bufferedMessage);
|
||||
}
|
||||
this._startedResuming = null;
|
||||
this.emit("resume");
|
||||
|
||||
} else if (type === "invalidate") {
|
||||
logger.warn("Connector | session invalidated by controller");
|
||||
if (this._disconnecting) {
|
||||
this._reset();
|
||||
this.emit("close");
|
||||
} else {
|
||||
this._invalidate();
|
||||
this.emit("invalidate");
|
||||
this.register();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the link with the server
|
||||
*
|
||||
* This function is expected to be overriden by sub-classes and send the
|
||||
* register message over the socket when invoked.
|
||||
*/
|
||||
register() {
|
||||
throw Error("Abstract function");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connector for in-memory local links
|
||||
*
|
||||
* @extends events.EventEmitter
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class VirtualConnector extends BaseConnector {
|
||||
constructor(src, dst) {
|
||||
super(src, dst);
|
||||
|
||||
this.other = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a pair of virtual connector hooked into each other
|
||||
*
|
||||
* Creates two virtual connectors that are liked to each othes such that
|
||||
* a message sent on one is received by the other.
|
||||
*
|
||||
* @param {module:lib.Address} src - Source for obverse connector
|
||||
* @param {module:lib.Address} dst - destination for obverse connector
|
||||
* @returns {Array} two virtuarl connectors.
|
||||
*/
|
||||
static makePair(src, dst) {
|
||||
let obverse = new this(src, dst);
|
||||
let reverse = new this(dst, src);
|
||||
obverse.other = reverse;
|
||||
reverse.other = obverse;
|
||||
return [obverse, reverse];
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the other end of the connector
|
||||
*
|
||||
* @param {module:lib.Message} message - Message type to send.
|
||||
*/
|
||||
send(message) {
|
||||
this.other.emit("message", message);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
BaseConnector,
|
||||
WebSocketBaseConnector,
|
||||
WebSocketClientConnector,
|
||||
VirtualConnector,
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Library for message based communication between nodes
|
||||
* @module lib/link
|
||||
* @author Hornwitser
|
||||
*/
|
||||
"use strict";
|
||||
const link = require("./link");
|
||||
const connectors = require("./connectors");
|
||||
|
||||
module.exports = {
|
||||
...link,
|
||||
...connectors,
|
||||
|
||||
// migrate: Allow info for plugins from before link refactor to load.
|
||||
Event: class Event {},
|
||||
Request: class Request {},
|
||||
};
|
||||
@@ -0,0 +1,686 @@
|
||||
// Implementation of Link class
|
||||
"use strict";
|
||||
|
||||
const libData = require("../data");
|
||||
const libErrors = require("../errors");
|
||||
const { logger } = require("../logging");
|
||||
const libSchema = require("../schema");
|
||||
const { dataClasses } = require("./messages");
|
||||
|
||||
// Some definitions for the terminology used here:
|
||||
// link: Either side of a controller - client connection
|
||||
// connector: Adapter between a link and a socket connection
|
||||
// connection: the controller side of a link
|
||||
// client: the side that is not the controller of a link
|
||||
// message: the complete object sent using the 'message' event
|
||||
|
||||
/**
|
||||
* Common interface for server and client connections
|
||||
*
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class Link {
|
||||
constructor(connector) {
|
||||
this.connector = connector;
|
||||
this.router = undefined;
|
||||
this.validateSent = true;
|
||||
|
||||
this._requestHandlers = new Map();
|
||||
this._requestFallbacks = new Map();
|
||||
this._eventHandlers = new Map();
|
||||
this._eventSnoopers = new Map();
|
||||
this._pendingRequests = new Map();
|
||||
this._forwardedRequests = new Map();
|
||||
this._nextRequestId = 1;
|
||||
|
||||
this.handle(libData.PingRequest, () => {});
|
||||
|
||||
// Process messages received by the connector
|
||||
connector.on("message", payload => {
|
||||
try {
|
||||
this._processMessage(payload);
|
||||
} catch (err) {
|
||||
if (err instanceof libErrors.InvalidMessage) {
|
||||
logger.error(err.message);
|
||||
if (err.errors) {
|
||||
logger.error(JSON.stringify(err.errors, null, 4));
|
||||
}
|
||||
|
||||
} else {
|
||||
// Unexpected error, bubble back to connector
|
||||
this.connector.emit("error", err);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
connector.on("disconnectPrepare", () => {
|
||||
this.prepareDisconnect().catch(
|
||||
err => {
|
||||
logger.error(`Unexpected error preparing disconnect:\n${err.stack}`);
|
||||
}
|
||||
).finally(
|
||||
() => {
|
||||
this.connector.send(new libData.MessageDisconnect("ready"));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
connector.on("invalidate", () => {
|
||||
this._clearPendingRequests(new libErrors.SessionLost("Session Lost"));
|
||||
});
|
||||
|
||||
connector.on("close", () => {
|
||||
this._clearPendingRequests(new libErrors.SessionLost("Session Closed"));
|
||||
});
|
||||
}
|
||||
|
||||
_clearPendingRequests(err) {
|
||||
for (let pending of this._pendingRequests.values()) {
|
||||
pending.reject(err);
|
||||
}
|
||||
for (let pending of this._forwardedRequests.values()) {
|
||||
if (pending.origin.connector.hasSession) {
|
||||
pending.origin.connector.sendResponseError(
|
||||
new libData.ResponseError(err.message, err.code), pending.src, pending.dst
|
||||
);
|
||||
}
|
||||
}
|
||||
this._pendingRequests.clear();
|
||||
this._forwardedRequests.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a received message on the link
|
||||
*
|
||||
* Validates and invokes the handler and/or waiters for a message that has
|
||||
* been received. An unhandled message is considered to be an error.
|
||||
*
|
||||
* @param {module:lib.Message} message - Message to process.
|
||||
* @throws {module:lib.InvalidMessage} if the message is invalid or not handled.
|
||||
*/
|
||||
_processMessage(message) {
|
||||
if (!["request", "response", "responseError", "event"].includes(message.type)) {
|
||||
throw new libErrors.InvalidMessage(`Unhandled message type ${message.type}`);
|
||||
}
|
||||
|
||||
let entry = this._validateMessage(message);
|
||||
if (entry && this.connector.dst.type === libData.Address.control) {
|
||||
try {
|
||||
this.validatePermission(message, entry); // Somewhat hacky, defined in ControlConnection
|
||||
} catch (err) {
|
||||
if (err instanceof libErrors.PermissionError) {
|
||||
return;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
if (message.type === "event" && this._eventSnoopers.has(entry.Event)) {
|
||||
let handler = this._eventSnoopers.get(entry.Event);
|
||||
handler(entry.eventFromJSON(message.data), message.src, message.dst).catch(err => {
|
||||
logger.error(`Unexpected error snooping ${message.name}:\n${err.stack}`);
|
||||
});
|
||||
}
|
||||
|
||||
if (!message.dst.addressedTo(this.connector.src)) {
|
||||
if (message.type === "response" || message.type === "responseError") {
|
||||
this._forwardedRequests.delete(message.dst.index());
|
||||
}
|
||||
this._routeMessage(message, entry);
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.type === "request") {
|
||||
this._processRequest(message, entry, this._requestHandlers.get(entry.Request));
|
||||
|
||||
} else if (message.type === "response") {
|
||||
this._processResponse(message);
|
||||
|
||||
} else if (message.type === "responseError") {
|
||||
this._processResponseError(message);
|
||||
|
||||
} else if (message.type === "event") {
|
||||
this._processEvent(message, entry);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ingress message validation
|
||||
*
|
||||
* Should be overridden by sub-classes to validate messages received.
|
||||
*
|
||||
* @param {module:lib.Message} message - Message to check.
|
||||
* @throws {module:lib.InvalidMessage} if the message is invalid.
|
||||
*/
|
||||
validateIngress(message) { }
|
||||
|
||||
_validateMessage(message) {
|
||||
try {
|
||||
if (message.src.type === libData.Address.broadcast) {
|
||||
throw new libErrors.InvalidMessage("Message src may not be broadcast");
|
||||
}
|
||||
this.validateIngress(message);
|
||||
} catch (err) {
|
||||
if (message.type === "request") {
|
||||
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (message.type === "request") {
|
||||
let entry = this.constructor._requestsByName.get(message.name);
|
||||
if (!entry) {
|
||||
let err = new libErrors.InvalidMessage(`Unrecognized request ${message.name}`);
|
||||
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||
throw err;
|
||||
}
|
||||
if (!entry.allowedSrcTypes.has(message.src.type)) {
|
||||
let err = new libErrors.InvalidMessage(`Source ${message.src} is not allowed for ${message.name}`);
|
||||
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||
throw err;
|
||||
}
|
||||
if (!entry.allowedDstTypes.has(message.dst.type)) {
|
||||
let err = new libErrors.InvalidMessage(`Destination ${message.dst} is not allowed for ${message.name}`);
|
||||
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||
throw err;
|
||||
}
|
||||
return entry;
|
||||
|
||||
} else if (message.type === "response") {
|
||||
return undefined;
|
||||
|
||||
} else if (message.type === "responseError") {
|
||||
return undefined;
|
||||
|
||||
} else if (message.type === "event") {
|
||||
let entry = this.constructor._eventsByName.get(message.name);
|
||||
if (!entry) {
|
||||
throw new libErrors.InvalidMessage(`Unrecognized event ${message.name}`);
|
||||
}
|
||||
if (!entry.allowedSrcTypes.has(message.src.type)) {
|
||||
throw new libErrors.InvalidMessage(`Source ${message.src} is not allowed for ${message.name}`);
|
||||
}
|
||||
if (message.dst.type === libData.Address.broadcast) {
|
||||
if (!entry.allowedDstTypes.has(message.dst.id)) {
|
||||
throw new libErrors.InvalidMessage(`Destination ${message.dst} is not allowed for ${message.name}`);
|
||||
}
|
||||
} else {
|
||||
// eslint-disable-next-line no-lonely-if
|
||||
if (!entry.allowedDstTypes.has(message.dst.type)) {
|
||||
throw new libErrors.InvalidMessage(`Destination ${message.dst} is not allowed for ${message.name}`);
|
||||
}
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
throw new Error("Should be unreachable");
|
||||
}
|
||||
|
||||
/**
|
||||
* Message permission validation
|
||||
*
|
||||
* Called when an request or event is received by a control connector.
|
||||
* Should be overridden by sub-classes to validate messages received.
|
||||
*
|
||||
* @param {module:lib.Message} message - Message to check.
|
||||
* @param {object} entry - Request or Event entry for this Message.
|
||||
* @throws {module:lib.PermissionError} if unauthorized.
|
||||
*/
|
||||
validatePermission(message, entry) { }
|
||||
|
||||
_routeMessage(message, entry) {
|
||||
if (!this.router) {
|
||||
let err = new libErrors.InvalidMessage(
|
||||
`Received message addressed to ${message.dst} but this link does not route messages`
|
||||
);
|
||||
if (message.type === "request") {
|
||||
this.connector.sendResponseError(
|
||||
new libData.ResponseError(err.message, err.code), message.src
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
let fallback = message.type === "request" ? this._requestFallbacks.get(entry.Request) : undefined;
|
||||
if (this.router.forwardMessage(this, message, Boolean(fallback))) {
|
||||
return;
|
||||
}
|
||||
if (!fallback) {
|
||||
throw new Error("Router requested fallback handling when fallback is unavailable");
|
||||
}
|
||||
if (message.type !== "request") {
|
||||
throw new Error(`Router requested fallback handling of unsupported message type ${message.type}`);
|
||||
}
|
||||
this._processRequest(message, entry, fallback, message.dst);
|
||||
}
|
||||
|
||||
_processRequest(message, entry, handler, spoofedSrc) {
|
||||
if (!handler) {
|
||||
this.connector.sendResponseError(
|
||||
new libData.ResponseError(`No handler for ${entry.Request.name}`), message.src
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = handler(entry.requestFromJSON(message.data), message.src, message.dst);
|
||||
} catch (err) {
|
||||
if (err.errors) {
|
||||
logger.error(JSON.stringify(err.errors, null, 4));
|
||||
}
|
||||
this.connector.sendResponseError(
|
||||
new libData.ResponseError(err.message, err.code, err.stack), message.src
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(response instanceof Promise)) {
|
||||
response = Promise.resolve(response);
|
||||
}
|
||||
|
||||
response.then(
|
||||
result => {
|
||||
if (this.validateSent) {
|
||||
if (entry.Response) {
|
||||
entry.responseFromJSON(JSON.parse(JSON.stringify(result)));
|
||||
} else if (result !== undefined) {
|
||||
throw new Error(`Expected empty response from ${entry.Request.name} handler`);
|
||||
}
|
||||
}
|
||||
this.connector.sendResponse(result, message.src, spoofedSrc);
|
||||
}
|
||||
).catch(
|
||||
err => {
|
||||
if (err instanceof libErrors.InvalidMessage) {
|
||||
logger.error(err.message);
|
||||
if (err.errors) {
|
||||
logger.error(JSON.stringify(err.errors, null, 4));
|
||||
}
|
||||
} else if (!(err instanceof libErrors.RequestError)) {
|
||||
logger.error(`Unexpected error responding to ${message.name}:\n${err.stack}`);
|
||||
}
|
||||
this.connector.sendResponseError(
|
||||
new libData.ResponseError(err.message, err.code, err.stack), message.src, spoofedSrc
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
_processResponse(message) {
|
||||
let pending = this._pendingRequests.get(message.dst.requestId);
|
||||
if (!pending) {
|
||||
throw new libErrors.InvalidMessage(
|
||||
`Received response ${message.dst.requestId} without a pending request`
|
||||
);
|
||||
}
|
||||
|
||||
if (!pending.dst.equals(message.src)) {
|
||||
throw new libErrors.InvalidMessage(`Received reply from ${message.src} for message sent to ${pending.dst}`);
|
||||
}
|
||||
|
||||
try {
|
||||
pending.resolve(pending.request.responseFromJSON(message.data));
|
||||
} catch (err) {
|
||||
// An invalid response object was likely received
|
||||
pending.reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
_processResponseError(message) {
|
||||
let pending = this._pendingRequests.get(message.dst.requestId);
|
||||
if (!pending) {
|
||||
throw new libErrors.InvalidMessage(
|
||||
`Received error response ${message.dst.requestId} without a pending request`
|
||||
);
|
||||
}
|
||||
|
||||
pending.reject(new libErrors.RequestError(message.data.message, message.data.code, message.data.stack));
|
||||
}
|
||||
|
||||
_processEvent(message, entry) {
|
||||
let handler = this._eventHandlers.get(entry.Event);
|
||||
if (!handler) {
|
||||
throw new libErrors.InvalidMessage(`Unhandled event ${message.name}`);
|
||||
}
|
||||
|
||||
handler(entry.eventFromJSON(message.data), message.src, message.dst).catch(err => {
|
||||
logger.error(`Unexpected error handling ${message.name}:\n${err.stack}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare connection for disconnect
|
||||
*
|
||||
* Waits for all pending requests on the connection to resolve.
|
||||
* Sub-classes should implement this handler with code that prevents
|
||||
* additional requests to be sent out before calling the super class
|
||||
* method.
|
||||
*/
|
||||
async prepareDisconnect() {
|
||||
let promises = [];
|
||||
for (let pending of this._pendingRequests.values()) {
|
||||
promises.push(pending.promise.then(() => {}, () => {}));
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a request or event to the other side of this link
|
||||
* @param {*} requestOrEvent - Request or event to send
|
||||
* @returns {Promise<*>|undefined}
|
||||
* Promise that resolves to the response if a request was sent or
|
||||
* undefined if it was an event.
|
||||
*/
|
||||
send(requestOrEvent) {
|
||||
return this.sendTo(this.connector.dst, requestOrEvent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a request or event to the given address
|
||||
*
|
||||
* @param {*|module:lib.Address} destination - Where to send it
|
||||
* @param {*} requestOrEvent - Request or event to send
|
||||
* @returns {Promise<*>|undefined}
|
||||
* Promise that resolves to the response if a request was sent or
|
||||
* undefined if it was an event.
|
||||
*/
|
||||
sendTo(destination, requestOrEvent) {
|
||||
let dst = libData.Address.fromShorthand(destination);
|
||||
|
||||
if (requestOrEvent.constructor.type === "request") {
|
||||
return this.sendRequest(requestOrEvent, dst);
|
||||
}
|
||||
if (requestOrEvent.constructor.type === "event") {
|
||||
return this.sendEvent(requestOrEvent, dst);
|
||||
}
|
||||
throw Error(`Expected request or event but got type ${requestOrEvent.constructor.type}`);
|
||||
}
|
||||
|
||||
sendRequest(request, dst) {
|
||||
let entry = this.constructor._requestsByClass.get(request.constructor);
|
||||
if (!entry) {
|
||||
throw new Error(`Attempt to send unregistered Request ${request.constructor.name}`);
|
||||
}
|
||||
if (this.validateSent) {
|
||||
entry.requestFromJSON(JSON.parse(JSON.stringify(request)));
|
||||
}
|
||||
|
||||
let pending = {
|
||||
request: entry,
|
||||
dst,
|
||||
};
|
||||
pending.promise = new Promise((resolve, reject) => {
|
||||
pending.resolve = resolve;
|
||||
pending.reject = reject;
|
||||
});
|
||||
let requestId = this._nextRequestId;
|
||||
this._nextRequestId += 1;
|
||||
this._pendingRequests.set(requestId, pending);
|
||||
this.connector.sendRequest(request, requestId, dst);
|
||||
return pending.promise;
|
||||
}
|
||||
|
||||
forwardRequest(message, origin) {
|
||||
if (this.validateSent) {
|
||||
let entry = this.constructor._requestsByName.get(message.name);
|
||||
if (!entry) {
|
||||
throw new Error(`Attempt to forward unregistered Request ${message.name}`);
|
||||
}
|
||||
entry.requestFromJSON(message.data);
|
||||
}
|
||||
|
||||
let pending = {
|
||||
origin,
|
||||
src: message.src,
|
||||
dst: message.dst,
|
||||
};
|
||||
this._forwardedRequests.set(message.src.index(), pending);
|
||||
this.connector.send(message);
|
||||
}
|
||||
|
||||
sendEvent(event, dst) {
|
||||
let entry = this.constructor._eventsByClass.get(event.constructor);
|
||||
if (!entry) {
|
||||
throw new Error(`Attempt to send unregistered Event ${event.constructor.name}`);
|
||||
}
|
||||
if (this.validateSent) {
|
||||
entry.eventFromJSON(JSON.parse(JSON.stringify(event)));
|
||||
}
|
||||
|
||||
this.connector.sendEvent(event, dst);
|
||||
}
|
||||
|
||||
handle(Class, handler) {
|
||||
if (Class.type === "request") {
|
||||
this.handleRequest(Class, handler);
|
||||
} else if (Class.type === "event") {
|
||||
this.handleEvent(Class, handler);
|
||||
} else {
|
||||
throw new Error(`Class ${Class.name} has unrecognized type ${Class.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
handleRequest(Request, handler) {
|
||||
let entry = this.constructor._requestsByClass.get(Request);
|
||||
if (!entry) {
|
||||
throw new Error(`Unregistered Request class ${Request.name}`);
|
||||
}
|
||||
if (this._requestHandlers.has(Request)) {
|
||||
throw new Error(`Request ${entry.name} is already registered`);
|
||||
}
|
||||
this._requestHandlers.set(Request, handler);
|
||||
}
|
||||
|
||||
fallbackRequest(Request, handler) {
|
||||
let entry = this.constructor._requestsByClass.get(Request);
|
||||
if (!entry) {
|
||||
throw new Error(`Unregistered Request class ${Request.name}`);
|
||||
}
|
||||
if (this._requestFallbacks.has(Request)) {
|
||||
throw new Error(`Request ${entry.name} is already fallbacked`);
|
||||
}
|
||||
this._requestFallbacks.set(Request, handler);
|
||||
}
|
||||
|
||||
handleEvent(Event, handler) {
|
||||
let entry = this.constructor._eventsByClass.get(Event);
|
||||
if (!entry) {
|
||||
throw new Error(`Unregistered Event class ${Event.name}`);
|
||||
}
|
||||
if (this._eventHandlers.has(Event)) {
|
||||
throw new Error(`Event ${entry.name} is already registered`);
|
||||
}
|
||||
this._eventHandlers.set(Event, handler);
|
||||
}
|
||||
|
||||
snoopEvent(Event, handler) {
|
||||
let entry = this.constructor._eventsByClass.get(Event);
|
||||
if (!entry) {
|
||||
throw new Error(`Unregistered Event class ${Event.name}`);
|
||||
}
|
||||
if (this._eventSnoopers.has(Event)) {
|
||||
throw new Error(`Event ${entry.name} is already snooped`);
|
||||
}
|
||||
this._eventSnoopers.set(Event, handler);
|
||||
}
|
||||
|
||||
static register(Class) {
|
||||
if (Class.type === "request") {
|
||||
this.registerRequest(Class);
|
||||
} else if (Class.type === "event") {
|
||||
this.registerEvent(Class);
|
||||
} else {
|
||||
throw new Error(`Data class ${Class.name} has unknown type ${Class.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
static requestFromJSON(Request, name) {
|
||||
if (Request.fromJSON) {
|
||||
if (!Request.jsonSchema) {
|
||||
throw new Error(`Request ${name} has static fromJSON but is missing static jsonSchema`);
|
||||
}
|
||||
let validate = libSchema.compile(Request.jsonSchema);
|
||||
return (json) => {
|
||||
if (!validate(json)) {
|
||||
throw new libErrors.InvalidMessage(`Request ${name} failed validation`, validate.errors);
|
||||
}
|
||||
return Request.fromJSON(json);
|
||||
};
|
||||
}
|
||||
if (Request.jsonSchema) {
|
||||
throw new Error(`Request ${name} has static jsonSchema but is missing static fromJSON`);
|
||||
}
|
||||
return () => new Request();
|
||||
}
|
||||
|
||||
static responseFromJSON(Response, name) {
|
||||
if (!Response.jsonSchema) {
|
||||
throw new Error(`Response for Request ${name} is missing static jsonSchema`);
|
||||
}
|
||||
if (!Response.fromJSON) {
|
||||
throw new Error(`Response for Request ${name} is missing static fromJSON`);
|
||||
}
|
||||
|
||||
let validate = libSchema.compile(Response.jsonSchema);
|
||||
return (json) => {
|
||||
if (!validate(json)) {
|
||||
throw new libErrors.InvalidMessage(
|
||||
`Response for request ${name} failed validation`, validate.errors
|
||||
);
|
||||
}
|
||||
return Response.fromJSON(json);
|
||||
};
|
||||
}
|
||||
|
||||
static allowedTypes(types, name, side) {
|
||||
if (types === undefined) {
|
||||
throw new Error(`Missing ${side} specification in ${name}`);
|
||||
}
|
||||
|
||||
if (!(types instanceof Array)) {
|
||||
types = [types];
|
||||
}
|
||||
|
||||
let allowed = new Set();
|
||||
for (let type of types) {
|
||||
let id = libData.Address[type];
|
||||
if (typeof id !== "number") {
|
||||
throw new Error(`Invalid type ${type} in ${side} specification of ${name}`);
|
||||
}
|
||||
if (id === libData.Address.broadcast) {
|
||||
throw new Error(`${side} specification may not use the broadcast type in ${name}`);
|
||||
}
|
||||
allowed.add(id);
|
||||
}
|
||||
return allowed;
|
||||
}
|
||||
|
||||
static _requestsByName = new Map();
|
||||
static _requestsByClass = new Map();
|
||||
|
||||
static registerRequest(Request) {
|
||||
const name = Request.plugin ? `${Request.plugin}:${Request.name}` : Request.name;
|
||||
if (this._requestsByName.has(name)) {
|
||||
throw new Error(`Request ${name} is already registered`);
|
||||
}
|
||||
|
||||
let entry = {
|
||||
Request,
|
||||
name,
|
||||
requestFromJSON: this.requestFromJSON(Request, name),
|
||||
allowedSrcTypes: this.allowedTypes(Request.src, name, "src"),
|
||||
allowedDstTypes: this.allowedTypes(Request.dst, name, "dst"),
|
||||
};
|
||||
|
||||
if (
|
||||
entry.allowedSrcTypes.has(libData.Address.control)
|
||||
&& !(Request.permission === null || ["function", "string"].includes(typeof Request.permission))
|
||||
) {
|
||||
throw new Error(`Invalid permission specification ${typeof Request.permission} on ${name}`);
|
||||
}
|
||||
|
||||
let Response = Request.Response;
|
||||
if (Response) {
|
||||
entry.Response = Response;
|
||||
entry.responseFromJSON = this.responseFromJSON(Response, name);
|
||||
} else {
|
||||
entry.responseFromJSON = () => undefined;
|
||||
}
|
||||
|
||||
this._requestsByName.set(name, entry);
|
||||
this._requestsByClass.set(Request, entry);
|
||||
}
|
||||
|
||||
static eventFromJSON(Event, name) {
|
||||
if (Event.fromJSON) {
|
||||
if (!Event.jsonSchema) {
|
||||
throw new Error(`Event ${name} has static fromJSON but is missing static jsonSchema`);
|
||||
}
|
||||
let validate = libSchema.compile(Event.jsonSchema);
|
||||
return (json) => {
|
||||
if (!validate(json)) {
|
||||
throw new libErrors.InvalidMessage(`Event ${name} failed validation`, validate.errors);
|
||||
}
|
||||
return Event.fromJSON(json);
|
||||
};
|
||||
}
|
||||
if (Event.jsonSchema) {
|
||||
throw new Error(`Event ${name} has static jsonSchema but is missing static fromJSON`);
|
||||
}
|
||||
return () => new Event();
|
||||
}
|
||||
|
||||
static _eventsByName = new Map();
|
||||
static _eventsByClass = new Map();
|
||||
|
||||
static registerEvent(Event) {
|
||||
const name = Event.plugin ? `${Event.plugin}:${Event.name}` : Event.name;
|
||||
if (this._eventsByName.has(name)) {
|
||||
throw new Error(`Event ${name} is already registered`);
|
||||
}
|
||||
|
||||
let entry = {
|
||||
Event,
|
||||
name,
|
||||
eventFromJSON: this.eventFromJSON(Event, name),
|
||||
allowedSrcTypes: this.allowedTypes(Event.src, name, "src"),
|
||||
allowedDstTypes: this.allowedTypes(Event.dst, name, "dst"),
|
||||
};
|
||||
|
||||
if (
|
||||
entry.allowedSrcTypes.has(libData.Address.control)
|
||||
&& !(Event.permission === null || ["function", "string"].includes(typeof Event.permission))
|
||||
) {
|
||||
throw new Error(`Invalid permission specification ${typeof Event.permission} on ${name}`);
|
||||
}
|
||||
|
||||
this._eventsByName.set(name, entry);
|
||||
this._eventsByClass.set(Event, entry);
|
||||
}
|
||||
}
|
||||
|
||||
for (let Class of dataClasses) {
|
||||
Link.register(Class);
|
||||
}
|
||||
|
||||
function registerPluginMessages(pluginInfos) {
|
||||
for (let pluginInfo of pluginInfos) {
|
||||
for (let Class of pluginInfo.messages || []) {
|
||||
if (Class.plugin !== pluginInfo.name) {
|
||||
throw new Error(
|
||||
`Expected message ${Class.name} from ${pluginInfo.name} to have a static ` +
|
||||
`plugin property set to "${pluginInfo.name}"`
|
||||
);
|
||||
}
|
||||
Link.register(Class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
Link,
|
||||
registerPluginMessages,
|
||||
};
|
||||
@@ -0,0 +1,111 @@
|
||||
"use strict";
|
||||
const core = require("../data/messages_core");
|
||||
const controller = require("../data/messages_controller");
|
||||
const host = require("../data/messages_host");
|
||||
const instance = require("../data/messages_instance");
|
||||
const mod = require("../data/messages_mod");
|
||||
const user = require("../data/messages_user");
|
||||
|
||||
// Event and Request payloads registered with the Link class. Used to decode
|
||||
// the data payload of incomming requsts and event messages into instances of
|
||||
// their proper classes.
|
||||
const dataClasses = [
|
||||
core.PingRequest,
|
||||
|
||||
controller.ControllerConfigGetRequest,
|
||||
controller.ControllerConfigSetFieldRequest,
|
||||
controller.ControllerConfigSetPropRequest,
|
||||
controller.HostGenerateTokenRequest,
|
||||
controller.HostConfigCreateRequest,
|
||||
controller.LogSetSubscriptionsRequest,
|
||||
controller.LogQueryRequest,
|
||||
controller.LogMessageEvent,
|
||||
controller.DebugDumpWsRequest,
|
||||
controller.DebugWsMessageEvent,
|
||||
|
||||
host.HostListRequest,
|
||||
host.HostSetSubscriptionsRequest,
|
||||
host.HostUpdateEvent,
|
||||
host.HostMetricsRequest,
|
||||
host.ControllerConnectionEvent,
|
||||
host.PrepareControllerDisconnectRequest,
|
||||
host.SyncUserListsEvent,
|
||||
|
||||
instance.InstanceDetailsGetRequest,
|
||||
instance.InstanceDetailsListRequest,
|
||||
instance.InstanceDetailsSetSubscriptionsRequest,
|
||||
instance.InstanceDetailsUpdateEvent,
|
||||
instance.InstanceCreateRequest,
|
||||
instance.InstanceConfigGetRequest,
|
||||
instance.InstanceConfigSetFieldRequest,
|
||||
instance.InstanceConfigSetPropRequest,
|
||||
instance.InstanceAssignRequest,
|
||||
instance.InstanceMetricsRequest,
|
||||
instance.InstanceStartRequest,
|
||||
instance.InstanceListSavesRequest,
|
||||
instance.InstanceSetSaveListSubscriptionsRequest,
|
||||
instance.InstanceSaveListUpdateEvent,
|
||||
instance.InstanceCreateSaveRequest,
|
||||
instance.InstanceRenameSaveRequest,
|
||||
instance.InstanceCopySaveRequest,
|
||||
instance.InstanceDeleteSaveRequest,
|
||||
instance.InstanceDownloadSaveRequest,
|
||||
instance.InstanceTransferSaveRequest,
|
||||
instance.InstancePullSaveRequest,
|
||||
instance.InstancePushSaveRequest,
|
||||
instance.InstanceLoadScenarioRequest,
|
||||
instance.InstanceExportDataRequest,
|
||||
instance.InstanceExtractPlayersRequest,
|
||||
instance.InstanceStopRequest,
|
||||
instance.InstanceKillRequest,
|
||||
instance.InstanceDeleteRequest,
|
||||
instance.InstanceDeleteInternalRequest,
|
||||
instance.InstanceSendRconRequest,
|
||||
instance.InstancesUpdateRequest,
|
||||
instance.InstanceAssignInternalRequest,
|
||||
instance.InstanceUnassignInternalRequest,
|
||||
instance.InstanceInitialisedEvent,
|
||||
instance.InstanceStatusChangedEvent,
|
||||
instance.InstanceDetailsChangedEvent,
|
||||
instance.InstanceBanlistUpdateEvent,
|
||||
instance.InstanceAdminlistUpdateEvent,
|
||||
instance.InstanceWhitelistUpdateEvent,
|
||||
instance.InstancePlayerUpdateEvent,
|
||||
|
||||
mod.ModPackGetRequest,
|
||||
mod.ModPackGetDefaultRequest,
|
||||
mod.ModPackListRequest,
|
||||
mod.ModPackCreateRequest,
|
||||
mod.ModPackUpdateRequest,
|
||||
mod.ModPackDeleteRequest,
|
||||
mod.ModPackSetSubscriptionsRequest,
|
||||
mod.ModGetRequest,
|
||||
mod.ModListRequest,
|
||||
mod.ModSearchRequest,
|
||||
mod.ModSetSubscriptionsRequest,
|
||||
mod.ModDownloadRequest,
|
||||
mod.ModDeleteRequest,
|
||||
mod.ModPackUpdateEvent,
|
||||
mod.ModUpdateEvent,
|
||||
|
||||
user.PermissionListRequest,
|
||||
user.RoleListRequest,
|
||||
user.RoleCreateRequest,
|
||||
user.RoleUpdateRequest,
|
||||
user.RoleGrantDefaultPermissionsRequest,
|
||||
user.RoleDeleteRequest,
|
||||
user.UserGetRequest,
|
||||
user.UserListRequest,
|
||||
user.UserSetSubscriptionsRequest,
|
||||
user.UserCreateRequest,
|
||||
user.UserRevokeTokenRequest,
|
||||
user.UserUpdateRolesRequest,
|
||||
user.UserSetAdminRequest,
|
||||
user.UserSetWhitelistedRequest,
|
||||
user.UserSetBannedRequest,
|
||||
user.UserDeleteRequest,
|
||||
user.AccountUpdateEvent,
|
||||
user.UserUpdateEvent,
|
||||
];
|
||||
|
||||
module.exports = { dataClasses };
|
||||
@@ -0,0 +1,151 @@
|
||||
"use strict";
|
||||
const winston = require("winston");
|
||||
const Transport = require("winston-transport");
|
||||
const { LEVEL, MESSAGE } = require("triple-beam");
|
||||
|
||||
const levels = Object.freeze({
|
||||
fatal: 0,
|
||||
error: 1,
|
||||
warn: 2,
|
||||
audit: 3,
|
||||
info: 4,
|
||||
server: 5,
|
||||
verbose: 6,
|
||||
});
|
||||
|
||||
const colors = {
|
||||
fatal: "inverse red",
|
||||
error: "bold brightRed",
|
||||
warn: "bold brightYellow",
|
||||
audit: "bold brightGreen",
|
||||
info: "bold brightBlue",
|
||||
server: "bold",
|
||||
verbose: "bold grey",
|
||||
};
|
||||
winston.addColors(colors);
|
||||
|
||||
// The Console Transport is replicated here because it's overly complicated
|
||||
// in winston, writes to stdout/err instead of console and doesn't work in
|
||||
// the browser.
|
||||
class ConsoleTransport extends Transport {
|
||||
constructor(options = {}) {
|
||||
super(options);
|
||||
this.errorLevels = new Set(options.errorLevels || ["fatal", "error"]);
|
||||
this.warnLevels = new Set(options.warnLevels || ["warn"]);
|
||||
this.filter = options.filter || (() => true);
|
||||
}
|
||||
|
||||
log(info, callback) {
|
||||
if (!this.filter(info)) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
/* eslint-disable no-console */
|
||||
if (this.errorLevels.has(info[LEVEL])) {
|
||||
console.error(info[MESSAGE]);
|
||||
} else if (this.warnLevels.has(info[LEVEL])) {
|
||||
console.warn(info[MESSAGE]);
|
||||
} else {
|
||||
console.log(info[MESSAGE]);
|
||||
}
|
||||
/* eslint-enable no-console */
|
||||
return callback();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats winston log messages for the web console
|
||||
*/
|
||||
class WebConsoleFormat {
|
||||
constructor(options = {}) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
transform(info, options) {
|
||||
let src = " ";
|
||||
if (info.host_id !== undefined) {
|
||||
src += `s:${info.host_name} - `;
|
||||
}
|
||||
if (info.instance_id !== undefined) {
|
||||
src += `i:${info.instance_name}`;
|
||||
}
|
||||
if (info.plugin) {
|
||||
src += `${info.plugin}: `;
|
||||
}
|
||||
|
||||
if (info.stack) {
|
||||
info[MESSAGE] = `[${info.level}]${src}${info.stack}`;
|
||||
} else {
|
||||
info[MESSAGE] = `[${info.level}]${src}${info.message}`;
|
||||
}
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter object for logs
|
||||
* @typedef {Object} module:lib~LogFilter
|
||||
* @property {string} [maxLevel] -
|
||||
* Maximum log level to include. Higher levels are more verbose.
|
||||
* @property {boolean} [all] -
|
||||
* Include log entries from controller, all hosts and all instances.
|
||||
* @property {boolean} [controller] -
|
||||
* Include log entries from the controller.
|
||||
* @property {Array<number>} [hostIds] -
|
||||
* Include log entries for the given hosts and instances of those
|
||||
* hosts by id.
|
||||
* @property {Array<number>} [instanceIds] -
|
||||
* Include log entries for the given instances by id.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create log filter by level and source.
|
||||
*
|
||||
* @param {module:lib~LogFilter} filter -
|
||||
* Filter to filter log entries by.
|
||||
* @returns {function(object): boolean}
|
||||
* filter returning true for log entries that match it.
|
||||
* @static
|
||||
*/
|
||||
function logFilter({ all, controller, hostIds, instanceIds, maxLevel }) {
|
||||
return info => {
|
||||
// Note: reversed to filter out undefined levels
|
||||
if (maxLevel && !(levels[info.level] <= levels[maxLevel])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (all) {
|
||||
return true;
|
||||
}
|
||||
if (controller && info.host_id === undefined) {
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
hostIds
|
||||
&& info.host_id !== undefined
|
||||
&& info.instance_id === undefined
|
||||
&& hostIds.includes(info.host_id)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
if (instanceIds && info.instance_id !== undefined && instanceIds.includes(info.instance_id)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
const logger = winston.createLogger({
|
||||
level: "verbose",
|
||||
levels,
|
||||
format: winston.format.timestamp(),
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
ConsoleTransport,
|
||||
WebConsoleFormat,
|
||||
colors,
|
||||
levels,
|
||||
logFilter,
|
||||
logger,
|
||||
};
|
||||
@@ -0,0 +1,429 @@
|
||||
/**
|
||||
* Utilities for logging on the Node.js side
|
||||
*
|
||||
* @author Hornwitser
|
||||
* @module lib/logging_utils
|
||||
*/
|
||||
"use strict";
|
||||
const events = require("events");
|
||||
const fs = require("fs-extra");
|
||||
const path = require("path");
|
||||
const stream = require("stream");
|
||||
const util = require("util");
|
||||
const winston = require("winston");
|
||||
const Transport = require("winston-transport");
|
||||
const { LEVEL, MESSAGE } = require("triple-beam");
|
||||
const chalk = require("chalk");
|
||||
|
||||
const libData = require("./data");
|
||||
const libErrors = require("./errors");
|
||||
const libFileOps = require("./file_ops");
|
||||
const { levels, logFilter, logger } = require("./logging");
|
||||
const libStream = require("./stream");
|
||||
|
||||
const finished = util.promisify(stream.finished);
|
||||
|
||||
|
||||
/**
|
||||
* Format a parsed Factorio message with colors
|
||||
*
|
||||
* Formats a parsed Factorio output from lib/factorio into a readable
|
||||
* colorized output using terminal escape codes that can be printed.
|
||||
*
|
||||
* @param {Object} parsed - Parsed Factorio server output.
|
||||
* @returns {string} terminal colorized message.
|
||||
* @private
|
||||
*/
|
||||
function formatServerOutput(parsed) {
|
||||
let time = "";
|
||||
if (parsed.format === "seconds") {
|
||||
time = `${chalk.yellow(parsed.time.padStart(8))} `;
|
||||
} else if (parsed.format === "date") {
|
||||
time = `${chalk.yellow(parsed.time)} `;
|
||||
}
|
||||
|
||||
let info = "";
|
||||
if (parsed.type === "log") {
|
||||
let level = parsed.level;
|
||||
if (level === "Script") {
|
||||
level = chalk.bold.greenBright(level);
|
||||
} else if (level === "Verbose") {
|
||||
level = chalk.bold.gray(level);
|
||||
} else if (level === "Info") {
|
||||
level = chalk.bold.blueBright(level);
|
||||
} else if (parsed.level === "Warning") {
|
||||
level = chalk.bold.yellowBright(level);
|
||||
} else if (parsed.level === "Error") {
|
||||
level = chalk.bold.redBright(level);
|
||||
}
|
||||
|
||||
info = `${level} ${chalk.gray(parsed.file)}: `;
|
||||
|
||||
} else if (parsed.type === "action") {
|
||||
info = `[${chalk.yellow(parsed.action)}] `;
|
||||
}
|
||||
|
||||
return time + info + parsed.message;
|
||||
}
|
||||
|
||||
// These are defined here to avoid circular dependencies and pulling them
|
||||
// into the web interface code.
|
||||
|
||||
/**
|
||||
* Formats winston log messages for a character terminal.
|
||||
* @static
|
||||
*/
|
||||
class TerminalFormat {
|
||||
constructor(options = {}) {
|
||||
this.options = options;
|
||||
this.colorize = winston.format.colorize(options);
|
||||
}
|
||||
|
||||
transform(info, options) {
|
||||
info = this.colorize.transform(info, this.colorize.options);
|
||||
let ts = "";
|
||||
if (options.showTimestamp && info.timestamp) {
|
||||
ts = `${info.timestamp.replace("T", " ")} `;
|
||||
}
|
||||
|
||||
let src = " ";
|
||||
if (info.instance_name) {
|
||||
src += `i:${info.instance_name} `;
|
||||
|
||||
} else if (info.host_name) {
|
||||
src += `s:${info.host_name} `;
|
||||
}
|
||||
if (info.plugin) {
|
||||
src += `${info.plugin}: `;
|
||||
}
|
||||
|
||||
if (info[LEVEL] === "server" && info.parsed) {
|
||||
info.message = formatServerOutput(info.parsed);
|
||||
}
|
||||
|
||||
if (info.stack) {
|
||||
info[MESSAGE] = `${ts}[${info.level}]${src}${info.stack}`;
|
||||
} else {
|
||||
info[MESSAGE] = `${ts}[${info.level}]${src}${info.message}`;
|
||||
}
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends logs over a lib/link connection.
|
||||
* @static
|
||||
*/
|
||||
class LinkTransport extends Transport {
|
||||
constructor(options) {
|
||||
super(options);
|
||||
|
||||
this.link = options.link;
|
||||
this.filter = options.filter || null;
|
||||
}
|
||||
|
||||
log(info, callback) {
|
||||
if (this.filter && !this.filter(info)) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
try {
|
||||
this.link.send(new libData.LogMessageEvent(info));
|
||||
} catch (err) {
|
||||
// Ignore session lost errors.
|
||||
if (!(err instanceof libErrors.SessionLost)) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
return callback();
|
||||
}
|
||||
}
|
||||
|
||||
const logFileGlob = /^[a-z]+-(\d{4}-\d{2}-\d{2})\.log$/;
|
||||
const logIndexVersion = 2;
|
||||
|
||||
/**
|
||||
* Keeps an index over a log directory to speed up queries to it
|
||||
* @static
|
||||
*/
|
||||
class LogIndex {
|
||||
constructor(logDirectory, serialized) {
|
||||
this.logDirectory = logDirectory;
|
||||
this.index = new Map();
|
||||
|
||||
if (serialized.version !== logIndexVersion) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let [file, serializedEntry] of Object.entries(serialized.files)) {
|
||||
this.index.set(file, {
|
||||
levels: new Set(serializedEntry.levels),
|
||||
controller: serializedEntry.controller,
|
||||
hostIds: new Set(serializedEntry.hostIds),
|
||||
instanceIds: new Set(serializedEntry.instanceIds),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
serialize() {
|
||||
let serialized = {
|
||||
version: logIndexVersion,
|
||||
files: {},
|
||||
};
|
||||
|
||||
for (let [file, entry] of this.index) {
|
||||
serialized.files[file] = {
|
||||
levels: [...entry.levels],
|
||||
controller: entry.controller,
|
||||
hostIds: [...entry.hostIds],
|
||||
instanceIds: [...entry.instanceIds],
|
||||
};
|
||||
}
|
||||
|
||||
return serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load or create an empty index for the given directory
|
||||
*
|
||||
* Loads index.json from the given log directy expecting it to contain
|
||||
* data about previously indexed files saved with {@link
|
||||
* module:lib.LogIndex#save}.
|
||||
*
|
||||
* @param {string} logDirectory - Path to directory to load from.
|
||||
* @returns {Promise<module:lib.LogIndex>} loaded or created log index.
|
||||
*/
|
||||
static async load(logDirectory) {
|
||||
try {
|
||||
let content = await fs.readFile(path.join(logDirectory, "index.json"));
|
||||
return new LogIndex(logDirectory, JSON.parse(content));
|
||||
} catch (err) {
|
||||
if (err.code !== "ENOENT") {
|
||||
logger.warn(`Failed to load ${path.join(logDirectory, "index.json")}: ${err}`);
|
||||
}
|
||||
return new LogIndex(logDirectory, {});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save index
|
||||
*
|
||||
* Saves corrently held index into index.json in the log directory it
|
||||
* was loaded/created from.
|
||||
*/
|
||||
async save() {
|
||||
await libFileOps.safeOutputFile(
|
||||
path.join(this.logDirectory, "index.json"),
|
||||
JSON.stringify(this.serialize(), null, 4)
|
||||
);
|
||||
}
|
||||
|
||||
async indexFile(file) {
|
||||
let filePath = path.join(this.logDirectory, file);
|
||||
let lineStream = new libStream.LineSplitter({ readableObjectMode: true });
|
||||
let fileStream = fs.createReadStream(filePath);
|
||||
fileStream.pipe(lineStream);
|
||||
let entry = {
|
||||
levels: new Set(),
|
||||
controller: false,
|
||||
hostIds: new Set(),
|
||||
instanceIds: new Set(),
|
||||
};
|
||||
for await (let line of lineStream) {
|
||||
let info;
|
||||
try {
|
||||
info = JSON.parse(line);
|
||||
} catch (err) {
|
||||
entry.levels.add("info");
|
||||
entry.controller = true;
|
||||
continue;
|
||||
}
|
||||
if (info.level) {
|
||||
entry.levels.add(info.level);
|
||||
}
|
||||
if (info.host_id === undefined) {
|
||||
entry.controller = true;
|
||||
} else if (info.instance_id === undefined) {
|
||||
entry.hostIds.add(info.host_id);
|
||||
}
|
||||
if (info.instance_id !== undefined) {
|
||||
entry.instanceIds.add(info.instance_id);
|
||||
}
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build index for files currently present in the log directory
|
||||
*/
|
||||
async buildIndex() {
|
||||
let today = new Date(Date.now() - 300e3).toISOString().slice(0, 10);
|
||||
|
||||
for (let file of await fs.readdir(this.logDirectory)) {
|
||||
let match = logFileGlob.exec(file);
|
||||
if (!match || match[1] >= today || this.index.has(file)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let entry = await this.indexFile(file);
|
||||
this.index.set(file, entry);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a file includes entries for the given filter
|
||||
*
|
||||
* @param {string} file -
|
||||
* Name of file in the log directory to check for.
|
||||
* @param {module:lib~LogFilter} filter -
|
||||
* Filter to check index if file contains entries for.
|
||||
* @returns {boolean}
|
||||
* true if the file may contain entries included by the filter.
|
||||
*/
|
||||
filterIncludesFile(file, { maxLevel, all, controller, instanceIds, hostIds }) {
|
||||
let entry = this.index.get(file);
|
||||
if (!entry) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
if (maxLevel && hasOwnProperty.call(levels, maxLevel)) {
|
||||
let fileLevels = [...entry.levels].map(
|
||||
fileLevel => (hasOwnProperty.call(levels, fileLevel) ? levels[fileLevel] : levels.verbose)
|
||||
);
|
||||
if (fileLevels.every(level => level > levels[maxLevel])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (all) {
|
||||
return true;
|
||||
}
|
||||
if (controller && entry.controller) {
|
||||
return true;
|
||||
}
|
||||
if (hostIds && hostIds.some(id => entry.hostIds.has(id))) {
|
||||
return true;
|
||||
}
|
||||
if (instanceIds && instanceIds.some(id => entry.instanceIds.has(id))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter object for querying logs
|
||||
* @typedef {Object} module:lib~QueryLogFilter
|
||||
* @property {number} [limit=100] - Maximum number of entries to return.
|
||||
* @property {string} [order="asc"] -
|
||||
* Return entries in ascending ("asc") date order or desceding ("desc")
|
||||
* date order.
|
||||
* @property {string} [maxLevel] -
|
||||
* Maximum log level to include. Higher levels are more verbose.
|
||||
* @property {boolean} [all] -
|
||||
* Include log entries from controller, all hosts and all instances.
|
||||
* @property {boolean} [controller] -
|
||||
* Include log entries from the controller.
|
||||
* @property {Array<number>} [hostIds] -
|
||||
* Include log entries for the given hosts and instances of those
|
||||
* hosts by id.
|
||||
* @property {Array<number>} [instanceIds] -
|
||||
* Include log entries for the given instances by id.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Query log directory
|
||||
*
|
||||
* @param {string} logDirectory - path to directory with logs.
|
||||
* @param {module:lib~QueryLogFilter} filter -
|
||||
* Filter to limit logs by.
|
||||
* @param {module:lib.LogIndex=} index -
|
||||
* Index to speed up query with.
|
||||
* @returns {Promise<Array<Object>>} log entries matching the filter
|
||||
*/
|
||||
async function queryLog(logDirectory, filter, index) {
|
||||
let files = (await fs.readdir(logDirectory)).filter(entry => logFileGlob.test(entry));
|
||||
|
||||
filter = {
|
||||
limit: 100,
|
||||
order: "asc",
|
||||
...filter,
|
||||
};
|
||||
|
||||
files.sort();
|
||||
if (filter.order === "desc") {
|
||||
files.reverse();
|
||||
}
|
||||
|
||||
const includeEntry = logFilter(filter);
|
||||
let log = [];
|
||||
for (let file of files) {
|
||||
if (index && !index.filterIncludesFile(file, filter)) {
|
||||
continue;
|
||||
}
|
||||
let filePath = path.join(logDirectory, file);
|
||||
let fileStream;
|
||||
let lineStream;
|
||||
if (filter.order === "asc") {
|
||||
lineStream = new libStream.LineSplitter({ readableObjectMode: true });
|
||||
fileStream = fs.createReadStream(filePath);
|
||||
} else {
|
||||
lineStream = new libStream.ReverseLineSplitter({ readableObjectMode: true });
|
||||
fileStream = await libStream.createReverseReadStream(filePath);
|
||||
}
|
||||
lineStream.on("data", line => {
|
||||
let info;
|
||||
try {
|
||||
info = JSON.parse(line);
|
||||
} catch (err) {
|
||||
info = { level: "info", message: line.toString("utf8") };
|
||||
}
|
||||
if (includeEntry(info)) {
|
||||
log.push(info);
|
||||
if (log.length >= filter.limit) {
|
||||
lineStream.destroy();
|
||||
fileStream.destroy();
|
||||
}
|
||||
}
|
||||
});
|
||||
fileStream.pipe(lineStream);
|
||||
try {
|
||||
await finished(lineStream);
|
||||
} catch (err) {
|
||||
if (log.length < filter.limit || err.code !== "ERR_STREAM_PREMATURE_CLOSE") {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
if (log.length >= filter.limit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return log;
|
||||
}
|
||||
|
||||
function handleUnhandledErrors() {
|
||||
/* eslint-disable node/no-process-exit */
|
||||
process.on("uncaughtException", err => {
|
||||
logger.fatal(`Uncaught exception:\n${err.stack}`);
|
||||
process.exit(1);
|
||||
});
|
||||
process.on("unhandledRejection", (reason, promise) => {
|
||||
logger.fatal(`Unhandled rejection:\n${reason.stack ? reason.stack : reason}`);
|
||||
process.exit(1);
|
||||
});
|
||||
/* eslint-enable node/no-process-exit */
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
TerminalFormat,
|
||||
LinkTransport,
|
||||
LogIndex,
|
||||
handleUnhandledErrors,
|
||||
queryLog,
|
||||
|
||||
// for testing only
|
||||
_formatServerOutput: formatServerOutput,
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Utilities for dealing with Lua
|
||||
* @module lib/lua_tools
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Escapes a string for inclusion into a lua string
|
||||
*
|
||||
* @param {string} content - String to escape.
|
||||
* @returns {string} Escaped string.
|
||||
*/
|
||||
function escapeString(content) {
|
||||
return content
|
||||
.replace(/\\/g, "\\\\")
|
||||
.replace(/"/g, '\\"')
|
||||
.replace(/'/g, "\\'")
|
||||
.replace(/\0/g, "\\0")
|
||||
.replace(/\n/g, "\\n")
|
||||
.replace(/\r/g, "\\r")
|
||||
;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
escapeString,
|
||||
};
|
||||
@@ -0,0 +1,818 @@
|
||||
/**
|
||||
* Plugin interfaces and utilities.
|
||||
* @module lib/plugin
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const helpers = require("./helpers");
|
||||
|
||||
/**
|
||||
* Conceptual base for controller and instance plugins.
|
||||
* @typedef {
|
||||
* module:lib.BaseControllerPlugin
|
||||
* |module:lib.BaseInstancePlugin
|
||||
* |module:lib.BaseControlPlugin
|
||||
* } module:lib.BasePlugin
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base class for instance plugins
|
||||
*
|
||||
* Instance plugins are subclasses of this class which get instantiated by
|
||||
* the host when it brings up an instance with the plugin enabled in the
|
||||
* config. To be discovered the class must be exported under the name
|
||||
* `InstancePlugin` in the module specified by the `instanceEntrypoint` in
|
||||
* the plugin's info.js file.
|
||||
*
|
||||
* Instances may be started and stopped many times, and many instances may
|
||||
* be running at the same time, each of which will have their own instance
|
||||
* of the InstancePlugin class.
|
||||
*
|
||||
* @static
|
||||
*/
|
||||
class BaseInstancePlugin {
|
||||
constructor(info, instance, host) {
|
||||
/**
|
||||
* The plugin's own info module
|
||||
*/
|
||||
this.info = info;
|
||||
|
||||
/**
|
||||
* Instance the plugin started for
|
||||
* @type {module:host/src/Instance}
|
||||
*/
|
||||
this.instance = instance;
|
||||
|
||||
/**
|
||||
* Host running the instance
|
||||
*
|
||||
* With the exepction of accessing the host's config you should
|
||||
* avoid ineracting with the host object directly.
|
||||
*
|
||||
* @type {module:host/src/Host}
|
||||
*/
|
||||
this.host = host;
|
||||
|
||||
/**
|
||||
* Logger for this plugin
|
||||
*
|
||||
* Instance of winston Logger for sending log messages from this
|
||||
* plugin. Supported methods and their corresponding log levels are
|
||||
* `error`, `warn`, `audit`, `info` and `verbose`.
|
||||
*/
|
||||
this.logger = instance.logger.child({ plugin: this.info.name });
|
||||
|
||||
this._pendingRconMessages = [];
|
||||
this._sendingRconMessages = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called immediately after the class is instantiated
|
||||
*/
|
||||
async init() { }
|
||||
|
||||
/**
|
||||
* Called when the value of a config field changed.
|
||||
*
|
||||
* Invoked after the value of the config field given by `field` has
|
||||
* changed.
|
||||
*
|
||||
* @param {module:lib.ConfigGroup} group -
|
||||
* The group who's field got changed.
|
||||
* @param {string} field - Name of the field that changed.
|
||||
* @param {*} prev - The previous value of the field.
|
||||
*/
|
||||
async onInstanceConfigFieldChanged(group, field, prev) { }
|
||||
|
||||
/**
|
||||
* Called before collecting Prometheus metrics
|
||||
*
|
||||
* Invoked before the default metrics of prometheus is collected. Note
|
||||
* that since this is done while replying to /metrics this should not
|
||||
* take a long time, otherwise the requst will time out.
|
||||
*
|
||||
* Although you can return the results of collecting your prometheus
|
||||
* collectors directly, all gauges in the default registry is already
|
||||
* automatically collected. It's therefore recommended to only update
|
||||
* the Gauges/Counters/etc in onMetric and let Clusterio deal will
|
||||
* collecting the values.
|
||||
*
|
||||
* @returns {*} an async iterator of prometheus metric results or undefined.
|
||||
*/
|
||||
async onMetrics() { }
|
||||
|
||||
/**
|
||||
* Called after the Factorio server is started
|
||||
*/
|
||||
async onStart() { }
|
||||
|
||||
/**
|
||||
* Called before the Factorio server is stopped
|
||||
*
|
||||
* This will not be called if for example the Factorio server crashes or
|
||||
* is killed.
|
||||
*/
|
||||
async onStop() { }
|
||||
|
||||
/**
|
||||
* Called when the instance exits
|
||||
*
|
||||
* Invoked when the instance is shut down. This may occur after init
|
||||
* has been called if an error occurs during startup. Note that if
|
||||
* the plugin's init() throws this method will still be invoked.
|
||||
*/
|
||||
onExit() { }
|
||||
|
||||
/**
|
||||
* Called when the Factorio outputs a line
|
||||
*
|
||||
* Called when the Factorio server outputs a line on either stdout or
|
||||
* stderr. The output is a parsed form of the line and contains the
|
||||
* following properties:
|
||||
*
|
||||
* @param {Object} parsed - parsed server output.
|
||||
* @param {string} parsed.source -
|
||||
* Where the message came from, one of "stdout" and "stderr".
|
||||
* @param {string} parsed.format -
|
||||
* Timestamp format, one of "date", "seconds" and "none".
|
||||
* @param {string=} parsed.time -
|
||||
* Timestamp of the message. Not present if format is "none".
|
||||
* @param {string} parsed.type -
|
||||
* Type of message, one of "log", "action" and "generic".
|
||||
* @param {string=} parsed.level -
|
||||
* Log level for "log" type. i.e "Info" normally.
|
||||
* @param {string=} parsed.file - File reported for "log" type.
|
||||
* @param {string=} parsed.action -
|
||||
* Kind of action for "action" type. i.e "CHAT" for chat.
|
||||
* @param {string} parsed.message - Main content of the line.
|
||||
* @param {string} line - raw line of server output.
|
||||
*/
|
||||
async onOutput(parsed, line) { }
|
||||
|
||||
/**
|
||||
* Called when an event on the controller connection happens
|
||||
*
|
||||
* The event param may be one of connect, drop, resume and close and has
|
||||
* the following meaning:
|
||||
*
|
||||
* ##### connect
|
||||
*
|
||||
* Invoked when a new connection to the controller has been established.
|
||||
*
|
||||
* ##### drop
|
||||
*
|
||||
* Invoked when a connection loss is detected between the host and the
|
||||
* controller. Plugins should respond to this event by throtteling messages
|
||||
* it is sending to the controller to an absolute minimum.
|
||||
*
|
||||
* Messages sent over a dropped controller connection will get queued up in
|
||||
* memory on the host and sent all in one go when the connection is
|
||||
* re-established again.
|
||||
*
|
||||
* ##### resume
|
||||
*
|
||||
* Invoked when the connection that had previously dropped is
|
||||
* re-established.
|
||||
*
|
||||
* ##### close
|
||||
*
|
||||
* Invoked when the connection to the controller has been closed. This
|
||||
* typically means the controller has shut down. Plugins should not
|
||||
* send any messages that goes to or via the controller after the
|
||||
* connection has been closed and before a new one is established.
|
||||
*
|
||||
* @param {string} event - one of connect, drop, resume and close
|
||||
*/
|
||||
onControllerConnectionEvent(event) { }
|
||||
|
||||
/**
|
||||
* Called when the controller is preparing to disconnect from the host
|
||||
*
|
||||
* Invoked when the controller has requested the disconnection from
|
||||
* the host. This typically happens when it is in the process of
|
||||
* shutting down.
|
||||
*
|
||||
* Plugins must stop sending messages to the controller, or are forwarded
|
||||
* via the controller after the prepare disconnect has been handled.
|
||||
*
|
||||
* @param {module:controller/src/HostConnection} connection -
|
||||
* The connection to the host preparing to disconnect.
|
||||
*/
|
||||
async onPrepareControllerDisconnect(connection) { }
|
||||
|
||||
/**
|
||||
* Called when a player joins or leaves the game
|
||||
*
|
||||
* Invoked when a player either joins or leaves the instance.
|
||||
*
|
||||
* @param {Object} event - Information about the event.
|
||||
* @param {string} event.type - Either "join", "leave" or "import".
|
||||
* @param {string} event.name - Name of the player that joined/left.
|
||||
* @param {string=} event.reason -
|
||||
* Only present for type "leave". Reason for player leaving the
|
||||
* game, one of the possible reasons in defines.disconnect_reason
|
||||
* or "server_quit" if the server exits while the player is online.
|
||||
*/
|
||||
async onPlayerEvent(event) { }
|
||||
|
||||
/**
|
||||
* Send RCON message to instance
|
||||
*
|
||||
* Send a message or command to the server which can potentially be
|
||||
* executed out of order in relation to other RCON commands.
|
||||
*
|
||||
* This should not be called before onStart or after onStop. A simple
|
||||
* way to achieve this is to check that the instance status is running
|
||||
* before sending commands. Note that the instance status is set to
|
||||
* running some time after `onStart` and some time before `onStop` is
|
||||
* invoked.
|
||||
*
|
||||
* @param {string} message - message to send to server over RCON.
|
||||
* @param {boolean=} [expectEmpty=false] -
|
||||
* if true throw if the response is not empty. Useful for detecting
|
||||
* errors that might have been sent in response.
|
||||
* @returns {Promise<string>} response from server.
|
||||
*/
|
||||
async sendRcon(message, expectEmpty=false) {
|
||||
return await this.instance.sendRcon(message, expectEmpty, this.info.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send serially ordered RCON message to instance
|
||||
*
|
||||
* Send a message or command to the server which will not be executed
|
||||
* out of order in relation to other RCON commands sent with this
|
||||
* method. The ordering applies per plugin, and two plugins sending
|
||||
* commands with this method may execute out of order in relation to
|
||||
* each other.
|
||||
*
|
||||
* This should not be called before onStart or after onStop. A simple
|
||||
* way to achieve this is to check that the instance status is running
|
||||
* before sending commands. Note that the instance status is set to
|
||||
* running some time after `onStart` and some time before `onStop` is
|
||||
* invoked.
|
||||
*
|
||||
* @param {string} message - message to send to server over RCON.
|
||||
* @param {boolean=} [expectEmpty=false] -
|
||||
* if true throw if the response is not empty. Useful for detecting
|
||||
* errors that might have been sent in response.
|
||||
* @returns {Promise<string>} response from server.
|
||||
*/
|
||||
async sendOrderedRcon(message, expectEmpty=false) {
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
this._pendingRconMessages.push({resolve, reject, message, expectEmpty});
|
||||
});
|
||||
if (!this._sendingRconMessages) {
|
||||
this._sendPendingRconMessages();
|
||||
}
|
||||
return await promise;
|
||||
}
|
||||
|
||||
async _sendPendingRconMessages() {
|
||||
this._sendingRconMessages = true;
|
||||
while (this._pendingRconMessages.length) {
|
||||
let task = this._pendingRconMessages.shift();
|
||||
try {
|
||||
let result = await this.sendRcon(task.message, task.expectEmpty);
|
||||
task.resolve(result);
|
||||
} catch (err) {
|
||||
task.reject(err);
|
||||
}
|
||||
}
|
||||
this._sendingRconMessages = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for controller plugins
|
||||
*
|
||||
* Controller plugins are subclasses of this class which get instantiated by
|
||||
* the controller on startup when the plugin is enabled in the config.
|
||||
* To be discovered the class must be exported under the name `ControllerPlugin`
|
||||
* in the module specified by the `controllerEntrypoint` in the plugin's info.js
|
||||
* file.
|
||||
*
|
||||
* @static
|
||||
*/
|
||||
class BaseControllerPlugin {
|
||||
constructor(info, controller, metrics, logger) {
|
||||
this.info = info;
|
||||
|
||||
/**
|
||||
* Controller
|
||||
* @type {module:controller/src/Controller}
|
||||
*/
|
||||
this.controller = controller;
|
||||
this.metrics = metrics;
|
||||
|
||||
/**
|
||||
* Logger for this plugin
|
||||
*
|
||||
* Instance of winston Logger for sending log messages from this
|
||||
* plugin. Supported methods and their corresponding log levels are
|
||||
* `error`, `warn`, `audit`, `info` and `verbose`.
|
||||
*/
|
||||
this.logger = logger.child({ plugin: this.info.name });
|
||||
}
|
||||
|
||||
/**
|
||||
* Called immediately after the class is instantiated
|
||||
*/
|
||||
async init() { }
|
||||
|
||||
/**
|
||||
* Called when the status of an instance changes
|
||||
*
|
||||
* Invoked when the controller has changed the status of an instance
|
||||
* or received notice from a host that the status of an instance has
|
||||
* changed. The possible statuses that can be notified about are:
|
||||
* - `unassigned:`: Instance is no longer asssigned to a host.
|
||||
* - `unknown`: Host assigned to instance is offline.
|
||||
* - `stopped`: Instance is no longer running or was just assigned to a
|
||||
* host.
|
||||
* - `starting`: Instance is in the process of starting up.
|
||||
* - `running`: Instance startup completed and is now running.
|
||||
* - `stopping`: Instance is in the processing stopping.
|
||||
* - `creating_save`: Instance is in the process of creating a save.
|
||||
* - `exporting_data`: Instance is in the process of exporting item
|
||||
* icons and locale data.
|
||||
* - `deleted`: Instance was deleted.
|
||||
*
|
||||
* On controller startup all known instances gets the status `unknown` if
|
||||
* they are assigned to a host, when the host then connects to the
|
||||
* controller and informs of the current status of its instances this hook
|
||||
* is invoked for all those instances. You can detect this situation by
|
||||
* checking if prev equals `unknown`. If the host disconnects the
|
||||
* instances will again get the `unknown` status.
|
||||
*
|
||||
* When instances are created on the controller they will notify of a status
|
||||
* change with prev set to null. While the status of new instances is
|
||||
* in most cases `unassigned` it's possible for the created instance to
|
||||
* start with any state in some host connection corner cases.
|
||||
*
|
||||
* Note that it's possible for status change notification to get lost in
|
||||
* the case of network outages if reconnect fails to re-establish the
|
||||
* session between the controller and the host.
|
||||
*
|
||||
* @param {module:controller/src/InstanceInfo} instance -
|
||||
* The instance that changed.
|
||||
* @param {?string} prev - the previous status of the instance.
|
||||
*/
|
||||
async onInstanceStatusChanged(instance, prev) { }
|
||||
|
||||
/**
|
||||
* Called when the value of a controller config field changed.
|
||||
*
|
||||
* Invoked after the value of the config field given by `field` has
|
||||
* changed on the controller.
|
||||
*
|
||||
* @param {module:lib.ConfigGroup} group -
|
||||
* The group who's field got changed.
|
||||
* @param {string} field - Name of the field that changed.
|
||||
* @param {*} prev - The previous value of the field.
|
||||
*/
|
||||
async onControllerConfigFieldChanged(group, field, prev) { }
|
||||
|
||||
/**
|
||||
* Called when the value of an instance config field changed.
|
||||
*
|
||||
* Invoked after the value of the config field given by `field` has
|
||||
* changed on an instance.
|
||||
*
|
||||
* @param {module:controller/src/InstanceInfo} instance -
|
||||
* The instance the config changed on.
|
||||
* @param {module:lib.ConfigGroup} group -
|
||||
* The group who's field got changed.
|
||||
* @param {string} field - Name of the field that changed.
|
||||
* @param {*} prev - The previous value of the field.
|
||||
*/
|
||||
async onInstanceConfigFieldChanged(instance, group, field, prev) { }
|
||||
|
||||
/**
|
||||
* Called before collecting Prometheus metrics
|
||||
*
|
||||
* Invoked before the default metrics of prometheus is collected. Note
|
||||
* that since this is done while replying to /metrics this should not
|
||||
* take a long time, otherwise the requst will time out.
|
||||
*
|
||||
* Although you can return the results of collecting your prometheus
|
||||
* collectors directly, all gauges in the default registry is already
|
||||
* automatically collected. It's therefore recommended to only update
|
||||
* the Gauges/Counters/etc in onMetric and let Clusterio deal will
|
||||
* collecting the values.
|
||||
*
|
||||
* @returns {*} an async iterator of prometheus metric results or undefined.
|
||||
*/
|
||||
async onMetrics() { }
|
||||
|
||||
/**
|
||||
* Called when the controller is shutting down
|
||||
*/
|
||||
async onShutdown() { }
|
||||
|
||||
/**
|
||||
* Called when an event on a host connection happens
|
||||
*
|
||||
* The event param may be one of connect, drop, resume and close and has
|
||||
* the following meaning:
|
||||
*
|
||||
* ##### connect
|
||||
*
|
||||
* Invoked when a new connection to the host has been established.
|
||||
*
|
||||
* ##### drop
|
||||
*
|
||||
* Invoked when a connection loss is detected between the controller and a
|
||||
* host. Plugins should respond to this event by throtteling messages
|
||||
* it is sending to the given host connection to an absolute minimum.
|
||||
*
|
||||
* Messages sent over a dropped host connection will get queued up in
|
||||
* memory on the controller and sent all in one go when the connection is
|
||||
* re-established again.
|
||||
*
|
||||
* ##### resume
|
||||
*
|
||||
* Invoked when a connection that had previously been dropped is
|
||||
* re-established
|
||||
*
|
||||
* ##### close
|
||||
*
|
||||
* Invoked when the connection to the host has been closed.
|
||||
*
|
||||
* @param {module:controller/src/HostConnection} connection -
|
||||
* The connection the event occured on.
|
||||
* @param {string} event - one of connect, drop, resume and close
|
||||
*/
|
||||
onHostConnectionEvent(connection, event) { }
|
||||
|
||||
/**
|
||||
* Called when an avent on a control connection happens
|
||||
*
|
||||
* The event param may be one of connect, drop, resume and close and has
|
||||
* the following meaning:
|
||||
*
|
||||
* ##### connect
|
||||
*
|
||||
* Invoked when a new connection to the control has been established.
|
||||
*
|
||||
* ##### drop
|
||||
*
|
||||
* Invoked when a connection loss is detected between the controller and a
|
||||
* control. Plugins should respond to this event by throtteling
|
||||
* messages it is sending to the given control connection to an absolute
|
||||
* minimum.
|
||||
*
|
||||
* Messages sent over a dropped control connection will get queued up in
|
||||
* memory on the controller and sent all in one go when the connection is
|
||||
* re-established again.
|
||||
*
|
||||
* ##### resume
|
||||
*
|
||||
* Invoked when a connection that had previously been dropped is
|
||||
* re-established.
|
||||
*
|
||||
* ##### close
|
||||
*
|
||||
* Invoked when the connection to the control has been closed.
|
||||
*
|
||||
* @param {module:controller/src/ControlConnection} connection -
|
||||
* The connection the event occured on.
|
||||
* @param {string} event - one of connect, drop, resume, and close.
|
||||
*/
|
||||
onControlConnectionEvent(connection, event) { }
|
||||
|
||||
/**
|
||||
* Called when a host is preparing to disconnect from the controller
|
||||
*
|
||||
* Invoked when a host has requested the disconnection from the controller.
|
||||
* This typically happens when it is in the process of shutting down.
|
||||
*
|
||||
* Plugins must stop sending messages to the host in question after the
|
||||
* prepare disconnect has been handled.
|
||||
*
|
||||
* @param {module:controller/src/HostConnection} connection -
|
||||
* The connection to the host preparing to disconnect.
|
||||
*/
|
||||
async onPrepareHostDisconnect(connection) { }
|
||||
|
||||
/**
|
||||
* Called when a mod pack is updated
|
||||
*
|
||||
* Invoked when a mod pack has been added, updated or deleted in the
|
||||
* cluster.
|
||||
*
|
||||
* If the mod pack has been deleted its `.isDeleted` property will be
|
||||
* true.
|
||||
*
|
||||
* @param {module:lib.ModPack} modPack - Mod pack that updated.
|
||||
*/
|
||||
async onModPackUpdated(modPack) { }
|
||||
|
||||
/**
|
||||
* Called when a mod stored on the controller is updated
|
||||
*
|
||||
* Invoked when a mod has been added, updated or deleted from the pool
|
||||
* of shared mods stored on the cluster.
|
||||
*
|
||||
* If a mod has been deleted its `.isDeleted` property will be true.
|
||||
*
|
||||
* @param {module:lib.ModInfo} mod - Mod that updated.
|
||||
*/
|
||||
async onModUpdated(mod) { }
|
||||
|
||||
/**
|
||||
* Called when a player joins or leaves an instance
|
||||
*
|
||||
* Invoked when a player either joins or leaves an instance in the
|
||||
* cluster.
|
||||
*
|
||||
* @param {module:controller/src/InstanceInfo} instance -
|
||||
* The instance it occured on.
|
||||
* @param {Object} event - Information about the event.
|
||||
* @param {string} event.type - Either "join", "leave" or "import".
|
||||
* @param {string} event.name - Name of the player that joined/left.
|
||||
* @param {string=} event.reason -
|
||||
* Only present for type "leave". Reason for player leaving the
|
||||
* game, one of the possible reasons in defines.disconnect_reason
|
||||
* or "server_quit" if the server exits while the player is online.
|
||||
*/
|
||||
async onPlayerEvent(instance, event) { }
|
||||
|
||||
/**
|
||||
* Broadcast event to all connected hosts
|
||||
*
|
||||
* Sends the given event to all hosts connected to the controller.
|
||||
* This does not include hosts that are in the process of closing the
|
||||
* connection, which typically happens when they are shutting down.
|
||||
*
|
||||
* @param {*} event - Event to send
|
||||
* @param {Object} data - Data ta pass with the event.
|
||||
*/
|
||||
broadcastEventToHosts(event, data={}) {
|
||||
if (event.type !== "event") {
|
||||
this.logger.warn(`Ignoring broadcast of ${event.type}`);
|
||||
return;
|
||||
}
|
||||
for (let hostConnection of this.controller.wsServer.hostConnections.values()) {
|
||||
if (
|
||||
!hostConnection.connector.closing
|
||||
&& (!event.plugin || hostConnection.plugins.has(event.plugin))
|
||||
) {
|
||||
event.send(hostConnection, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for clusterioctl plugins
|
||||
*
|
||||
* Control plugins are subclasses of this class which get instantiated by
|
||||
* clusterioctl in order to extend its functionallity. To be discovered the
|
||||
* class must be exported under the name `ControlPlugin` in the module
|
||||
* specified by the `controlEntrypoint` in the plugin's info.js file.
|
||||
*
|
||||
* @static
|
||||
*/
|
||||
class BaseControlPlugin {
|
||||
constructor(info, logger) {
|
||||
/**
|
||||
* The plugin's own info module
|
||||
*/
|
||||
this.info = info;
|
||||
|
||||
/**
|
||||
* Logger for this plugin
|
||||
*
|
||||
* Instance of winston Logger for sending log messages from this
|
||||
* plugin. Supported methods and their corresponding log levels are
|
||||
* `error`, `warn`, `audit`, `info` and `verbose`.
|
||||
*/
|
||||
this.logger = logger.child({ plugin: this.info.name });
|
||||
}
|
||||
|
||||
/**
|
||||
* Called immediately after the class is instantiated
|
||||
*/
|
||||
async init() { }
|
||||
|
||||
/**
|
||||
* Called to add commands to the command line interface.
|
||||
*
|
||||
* Invoked by clusterioctl to let plugins add commands. `rootCommand` is
|
||||
* the top level command node which the plugin should add its own {@link
|
||||
* module:lib.CommandTree} to.
|
||||
*
|
||||
* @param {module:lib.CommandTree} rootCommand -
|
||||
* Root of the clusterioctl command tree.
|
||||
*/
|
||||
async addCommands(rootCommand) { }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Plugin supplied login form
|
||||
* @typedef {Object} module:lib~LoginForm
|
||||
* @property {string} name -
|
||||
* Internal name of the login form, this should start with the
|
||||
* plugin name followed by a dot.
|
||||
* @property {string} title -
|
||||
* Name displayed above this form in the login window.
|
||||
* @property {React.Component} Component -
|
||||
* React component that's rendered for this login form. This is
|
||||
* supplied the setToken function via its props which should be
|
||||
* called when an authentication token is aquired via this
|
||||
* form.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Plugin supplied pages
|
||||
* @typedef {Object} module:lib~Page
|
||||
* @property {string} path - URL path to this page.
|
||||
* @property {string=} sidebarPath -
|
||||
* If present and this path matches one of the pages in the sidebar it
|
||||
* will cause that sidebar entry to be highlighted as active.
|
||||
* @property {string=} sidebarGroup -
|
||||
* If present group this entry under a group of the given name in the
|
||||
* sidebar.
|
||||
* @property {string=} sidebarName -
|
||||
* If present creates an entry in the sidebar for this page with the
|
||||
* given text.
|
||||
* @property {ReactNode} content -
|
||||
* A react node which is rendered when this page is navigated to.
|
||||
* Should render a PageLayout.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base class for web interface plugins
|
||||
*
|
||||
* @static
|
||||
*/
|
||||
class BaseWebPlugin {
|
||||
constructor(container, packageData, info, logger) {
|
||||
/**
|
||||
* Webpack container for this plugin
|
||||
*/
|
||||
this.container = container;
|
||||
|
||||
/**
|
||||
* Contents of the plugin's package.json file
|
||||
* @type {Object}
|
||||
*/
|
||||
this.package = packageData;
|
||||
|
||||
/**
|
||||
* The plugin's own info module
|
||||
*/
|
||||
this.info = info;
|
||||
|
||||
/**
|
||||
* Control link to the controller, not available until the
|
||||
* connect event in onControllerConnectionEvent is signaled.
|
||||
* @type {?module:lib.Link}
|
||||
*/
|
||||
this.control = null;
|
||||
|
||||
/**
|
||||
* Logger for this plugin
|
||||
*
|
||||
* Instance of winston Logger for sending log messages from this
|
||||
* plugin. Supported methods and their corresponding log levels are
|
||||
* `error`, `warn`, `audit`, `info` and `verbose`.
|
||||
*/
|
||||
this.logger = logger.child({ plugin: this.info.name });
|
||||
|
||||
/**
|
||||
* List of login forms provided by this plugin
|
||||
*
|
||||
* @type {Array<module:lib~LoginForm>}
|
||||
*/
|
||||
this.loginForms = [];
|
||||
|
||||
/**
|
||||
* List of pages provided by this plugin
|
||||
*
|
||||
* @type {Array<module:lib~Page>}
|
||||
*/
|
||||
this.pages = [];
|
||||
|
||||
/**
|
||||
* Extra react component to add to core components
|
||||
*
|
||||
* Interface to augment core components of the web UI. Setting a
|
||||
* component as one of the supported properties of this object will
|
||||
* cause the web UI to render it when displaying that component,
|
||||
* usually at the end. Each component will receive a `plugin` param
|
||||
* which is the instance of the web plugin that contained the
|
||||
* component extra.
|
||||
*
|
||||
* @type {object}
|
||||
* @property {React.ComponentType} ControllerPage -
|
||||
* Placed at the end of the controller page.
|
||||
* @property {React.ComponentType} HostsPage -
|
||||
* Placed at the end of the hosts list page.
|
||||
* @property {React.ComponentType} HostViewPage -
|
||||
* Placed at the end of each host page. Takes a `host` param
|
||||
* which is the host the page is displayed for.
|
||||
* @property {React.ComponentType} InstancesPage -
|
||||
* Placed at the end of the instance list page.
|
||||
* @property {React.ComponentType} InstanceViewPage -
|
||||
* Placed at the end of each instance page. Takes an `instance`
|
||||
* param which is the instance the page is displayed for.
|
||||
* @property {React.ComponentType} UsersPage -
|
||||
* Placed at the end of the users list page.
|
||||
* @property {React.ComponentType} UserViewPage -
|
||||
* Placed at the end of each user page. Takes a `user` param
|
||||
* which is the user object the page is displayed for.
|
||||
* @property {React.ComponentType} RolesPage -
|
||||
* Placed at the end of the roles list page.
|
||||
* @property {React.ComponentType} RoleViewPage -
|
||||
* Placed at the end of each role page. Takes a `role` param
|
||||
* which is the role object the page is displayed for.
|
||||
*/
|
||||
this.componentExtra = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Called immediately after the class is instantiated.
|
||||
*/
|
||||
async init() { }
|
||||
|
||||
/**
|
||||
* Called when an event on the controller connection happens
|
||||
*
|
||||
* The event param may be one of connect, drop, resume and close and has
|
||||
* the following meaning:
|
||||
*
|
||||
* ##### connect
|
||||
*
|
||||
* Invoked when a new connection to the controller has been established.
|
||||
*
|
||||
* ##### drop
|
||||
*
|
||||
* Invoked when a connection loss is detected between the control link
|
||||
* and the controller. Plugins should respond to this event by throtteling
|
||||
* messages it is sending to the controller to an absolute minimum.
|
||||
*
|
||||
* Messages sent over a dropped controller connection will get queued up in
|
||||
* memory in the browser and sent all in one go when the connection is
|
||||
* re-established again.
|
||||
*
|
||||
* ##### resume
|
||||
*
|
||||
* Invoked when the connection that had previously dropped is
|
||||
* re-established.
|
||||
*
|
||||
* ##### close
|
||||
*
|
||||
* Invoked when the connection to the controller has been closed. This
|
||||
* typically means the controller has shut down. Plugins should not
|
||||
* send any messages that goes to or via the controller after the
|
||||
* connection has been closed and before a new one is established.
|
||||
*
|
||||
* @param {string} event - one of connect, drop, resume and close
|
||||
*/
|
||||
onControllerConnectionEvent(event) { }
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the given hook on all plugins
|
||||
*
|
||||
* @param {Map<string, Object>} plugins -
|
||||
* Mapping of plugin names to plugins to invoke the hook on.
|
||||
* @param {string} hook - Name of hook to invoke.
|
||||
* @param {...*} args - Arguments to pass on to the hook.
|
||||
* @returns {Promise<Array>} Non-empty return values from the hooks.
|
||||
*/
|
||||
async function invokeHook(plugins, hook, ...args) {
|
||||
let results = [];
|
||||
for (let [name, plugin] of plugins) {
|
||||
try {
|
||||
// Use an object to detect if the hook failed on timeout or tried to return a value looking like an error
|
||||
const timeout = {};
|
||||
let result = await helpers.timeout(
|
||||
plugin[hook](...args),
|
||||
15000,
|
||||
timeout
|
||||
);
|
||||
if (result === timeout) {
|
||||
throw new Error(`Invoking hook ${hook} timed out for plugin ${name}`);
|
||||
} else if (result !== undefined) {
|
||||
results.push(result);
|
||||
}
|
||||
} catch (err) {
|
||||
plugin.logger.error(`Ignoring error from plugin ${name} in ${hook}:\n${err.stack}`);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
BaseInstancePlugin,
|
||||
BaseControllerPlugin,
|
||||
BaseControlPlugin,
|
||||
BaseWebPlugin,
|
||||
|
||||
invokeHook,
|
||||
};
|
||||
@@ -0,0 +1,119 @@
|
||||
/* eslint-disable node/global-require */
|
||||
/**
|
||||
* Helpers for loading plugins in Node.js
|
||||
* @module lib/plugin_loader
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
const libErrors = require("./errors");
|
||||
const libPlugin = require("./plugin");
|
||||
const { logger } = require("./logging");
|
||||
const path = require("path");
|
||||
|
||||
|
||||
/**
|
||||
* Load plugin information
|
||||
*
|
||||
* Loads plugin info modules for the paths to the given plugins. Once
|
||||
* loaded the info modules will not be reloaded should this function be
|
||||
* called again.
|
||||
*
|
||||
* @param {Map<string, string>} pluginList -
|
||||
* Mapping of plugin name to require path for the plugins to load.
|
||||
* @returns {Promise<Array<Object>>} Array of plugin info modules.
|
||||
* @static
|
||||
*/
|
||||
async function loadPluginInfos(pluginList) {
|
||||
let plugins = [];
|
||||
for (let [pluginName, pluginPath] of pluginList) {
|
||||
let pluginInfo;
|
||||
let pluginPackage;
|
||||
|
||||
try {
|
||||
pluginInfo = require(path.posix.join(pluginPath, "info"));
|
||||
pluginPackage = require(path.posix.join(pluginPath, "package.json"));
|
||||
|
||||
} catch (err) {
|
||||
throw new libErrors.PluginError(pluginName, err);
|
||||
}
|
||||
|
||||
if (pluginInfo.name !== pluginName) {
|
||||
throw new libErrors.EnvironmentError(
|
||||
`Expected plugin at ${pluginPath} to be named ${pluginName} but got ${pluginInfo.name}`
|
||||
);
|
||||
}
|
||||
|
||||
// migrate: ignore incompatible old plugins
|
||||
if (pluginInfo.messages && !(pluginInfo.messages instanceof Array)) {
|
||||
logger.warn(`Ignoring incompatible pre alpha.14 plugin ${pluginName}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
pluginInfo.requirePath = pluginPath;
|
||||
pluginInfo.version = pluginPackage.version;
|
||||
plugins.push(pluginInfo);
|
||||
}
|
||||
return plugins;
|
||||
}
|
||||
|
||||
function loadPluginClass(entrypointName, className, pluginClass, pluginInfo) {
|
||||
let resolvedPath = path.posix.join(pluginInfo.requirePath, pluginInfo[entrypointName]);
|
||||
let entrypoint = require(resolvedPath);
|
||||
if (!entrypoint[className]) {
|
||||
throw new libErrors.PluginError(pluginInfo.name,
|
||||
new Error(`Expected ${resolvedPath} to export a class named ${className}`)
|
||||
);
|
||||
}
|
||||
if (!(entrypoint[className].prototype instanceof pluginClass)) {
|
||||
throw new libErrors.PluginError(pluginInfo.name,
|
||||
new Error(`Expected ${className} exported from ${resolvedPath} to be a subclass of ${pluginClass.name}`)
|
||||
);
|
||||
}
|
||||
return entrypoint[className];
|
||||
}
|
||||
|
||||
/**
|
||||
* Load controller plugin class of a plugin
|
||||
*
|
||||
* @param {Object} pluginInfo -
|
||||
* Plugin info object returned from {@link
|
||||
* module:lib.loadPluginInfos} to load class from.
|
||||
* @returns {Promise<function>} plugin class
|
||||
* @static
|
||||
*/
|
||||
async function loadControllerPluginClass(pluginInfo) {
|
||||
return loadPluginClass("controllerEntrypoint", "ControllerPlugin", libPlugin.BaseControllerPlugin, pluginInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load instance plugin class of a plugin
|
||||
*
|
||||
* @param {Object} pluginInfo -
|
||||
* Plugin info object returned from {@link
|
||||
* module:lib.loadPluginInfos} to load class from.
|
||||
* @returns {Promise<function>} plugin class
|
||||
* @static
|
||||
*/
|
||||
async function loadInstancePluginClass(pluginInfo) {
|
||||
return loadPluginClass("instanceEntrypoint", "InstancePlugin", libPlugin.BaseInstancePlugin, pluginInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load control plugin class of a plugin
|
||||
*
|
||||
* @param {Object} pluginInfo -
|
||||
* Plugin info object returned from {@link
|
||||
* module:lib.loadPluginInfos} to load class from.
|
||||
* @returns {Promise<function>} plugin class
|
||||
* @static
|
||||
*/
|
||||
async function loadControlPluginClass(pluginInfo) {
|
||||
return loadPluginClass("controlEntrypoint", "ControlPlugin", libPlugin.BaseControlPlugin, pluginInfo);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
loadPluginInfos,
|
||||
loadControllerPluginClass,
|
||||
loadInstancePluginClass,
|
||||
loadControlPluginClass,
|
||||
};
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* JSON schemas used for validating link messages
|
||||
* @module lib/schema
|
||||
*/
|
||||
"use strict";
|
||||
const Ajv = require("ajv");
|
||||
const ajv = new Ajv({
|
||||
allowUnionTypes: true,
|
||||
strict: "log",
|
||||
strictTuples: false,
|
||||
verbose: true,
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* Compile JSON schema into validator
|
||||
* @function
|
||||
* @param {Object} schema - JSON schema to create validator for.
|
||||
* @returns {Function} Validator for the schema.
|
||||
*/
|
||||
compile: ajv.compile.bind(ajv),
|
||||
};
|
||||
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* Implementation of commands shared between controller/host/ctl.
|
||||
* @module lib/shared_commands
|
||||
*/
|
||||
"use strict";
|
||||
const path = require("path");
|
||||
|
||||
const libConfig = require("./config");
|
||||
const libFileOps = require("./file_ops");
|
||||
const { logger } = require("./logging");
|
||||
const libHelpers = require("./helpers");
|
||||
|
||||
|
||||
function print(...content) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(...content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Yargs plugin command
|
||||
*
|
||||
* Can be passed to yargs.command to implement a plugin list management
|
||||
* command. Use handlePluginCommand to do the requested action.
|
||||
*
|
||||
* @param {Object} yargs - yargs command builder.
|
||||
*/
|
||||
function pluginCommand(yargs) {
|
||||
yargs
|
||||
.command("add <path>", "Add plugin by require path")
|
||||
.command("remove <name>", "Remove plugin by name")
|
||||
.command("list", "List all plugins and their path")
|
||||
.demandCommand(1, "You need to specify a command to run")
|
||||
.help()
|
||||
.strict()
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle yargs plugin command
|
||||
*
|
||||
* Handle the actions that are made available by pluginCommand.
|
||||
*
|
||||
* @param {Object} args - yargs args object.
|
||||
* @param {Map<string, string>} pluginList - Current list of plugins.
|
||||
* @param {string} pluginListPath - Path to plugin list config file.
|
||||
*/
|
||||
async function handlePluginCommand(args, pluginList, pluginListPath) {
|
||||
let command = args._[1];
|
||||
|
||||
if (command === "add") {
|
||||
let pluginPath = args.path;
|
||||
if (/^\.\.?[\/\\]/.test(pluginPath)) {
|
||||
pluginPath = path.resolve(pluginPath);
|
||||
}
|
||||
|
||||
let pluginInfo;
|
||||
try {
|
||||
// eslint-disable-next-line node/global-require
|
||||
pluginInfo = require(path.posix.join(pluginPath, "info"));
|
||||
} catch (err) {
|
||||
logger.error(`Unable to import plugin info from ${args.path}:\n${err.stack}`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (pluginList.has(pluginInfo.name)) {
|
||||
logger.error(`Plugin with the same ${pluginInfo.name} already exists`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
pluginList.set(pluginInfo.name, pluginPath);
|
||||
await libFileOps.safeOutputFile(pluginListPath, JSON.stringify([...pluginList], null, 4));
|
||||
print(`Added ${pluginInfo.name}`);
|
||||
|
||||
} else if (command === "remove") {
|
||||
if (!pluginList.delete(args.name)) {
|
||||
logger.error(`Plugin with name ${args.name} does not exist`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
await libFileOps.safeOutputFile(pluginListPath, JSON.stringify([...pluginList], null, 4));
|
||||
print(`Removed ${args.name}`);
|
||||
|
||||
} else if (command === "list") {
|
||||
for (let [pluginName, pluginPath] of pluginList) {
|
||||
print(`${pluginName} - ${pluginPath}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Yargs config command
|
||||
*
|
||||
* Can be passed to yargs.command to implement a config command. Use
|
||||
* handleConfigCommand to do the requested action.
|
||||
*
|
||||
* @param {Object} yargs - yargs command builder.
|
||||
*/
|
||||
function configCommand(yargs) {
|
||||
yargs
|
||||
.command("set <field> [value]", "Set config field", yargs => {
|
||||
yargs.options({
|
||||
"stdin": { describe: "read value from stdin", nargs: 0, type: "boolean" },
|
||||
});
|
||||
})
|
||||
.command("show <field>", "Show value of the given config field")
|
||||
.command("list", "List all configuration fields and their values")
|
||||
.demandCommand(1, "You need to specify a command to run")
|
||||
.help()
|
||||
.strict()
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle yargs command
|
||||
*
|
||||
* Handle the actions that are made available by configCommand.
|
||||
*
|
||||
* @param {Object} args - yargs args object.
|
||||
* @param {module:lib.Config} instance - Config instance.
|
||||
* @param {string} configPath - Path to configuration file.
|
||||
*/
|
||||
async function handleConfigCommand(args, instance, configPath) {
|
||||
let command = args._[1];
|
||||
|
||||
if (command === "list") {
|
||||
for (let GroupClass of instance.constructor.groups.values()) {
|
||||
for (let def of GroupClass.definitions.values()) {
|
||||
let value = instance.get(def.fullName);
|
||||
print(`${def.fullName} ${JSON.stringify(value)}`);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (command === "show") {
|
||||
try {
|
||||
print(instance.get(args.field));
|
||||
} catch (err) {
|
||||
if (err instanceof libConfig.InvalidField) {
|
||||
logger.error(err.message);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (command === "set") {
|
||||
if (args.stdin) {
|
||||
args.value = (await libHelpers.readStream(process.stdin)).toString().replace(/\r?\n$/, "");
|
||||
|
||||
} else if (args.value === undefined) {
|
||||
args.value = null;
|
||||
}
|
||||
|
||||
try {
|
||||
instance.set(args.field, args.value);
|
||||
await libFileOps.safeOutputFile(configPath, JSON.stringify(instance.serialize(), null, 4));
|
||||
} catch (err) {
|
||||
if (err instanceof libConfig.InvalidField || err instanceof libConfig.InvalidValue) {
|
||||
logger.error(err.message);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
pluginCommand,
|
||||
handlePluginCommand,
|
||||
|
||||
configCommand,
|
||||
handleConfigCommand,
|
||||
};
|
||||
@@ -0,0 +1,168 @@
|
||||
"use strict";
|
||||
const assert = require("assert");
|
||||
const fs = require("fs-extra");
|
||||
const stream = require("stream");
|
||||
|
||||
|
||||
/**
|
||||
* Transform splitting a stream into lines
|
||||
*
|
||||
* Splits up a stream of chunks by newlines and passes on the lines one by
|
||||
* one removing the line feed and optional carriage return from the lines.
|
||||
*
|
||||
* @example
|
||||
* let lineStream = new libStream.LineSplitter({ readableObjectMode: true });
|
||||
* lineStream.on("data", line => { console.log(line.toString("utf8")); });
|
||||
* let fileStream = fs.createReadStream(path);
|
||||
* fileStream.pipe(lineStream);
|
||||
*
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class LineSplitter extends stream.Transform {
|
||||
constructor(options) {
|
||||
super(options);
|
||||
this._partial = null;
|
||||
}
|
||||
|
||||
_transform(chunk, encoding, callback) {
|
||||
if (this._partial) {
|
||||
chunk = Buffer.concat([this._partial, chunk]);
|
||||
this._partial = null;
|
||||
}
|
||||
|
||||
while (chunk.length) {
|
||||
let end = chunk.indexOf("\n");
|
||||
if (end === -1) {
|
||||
this._partial = chunk;
|
||||
break;
|
||||
}
|
||||
|
||||
let next = end + 1;
|
||||
// Eat carriage return as well if present
|
||||
if (end >= 1 && chunk[end-1] === "\r".charCodeAt(0)) {
|
||||
end -= 1;
|
||||
}
|
||||
|
||||
let line = chunk.slice(0, end);
|
||||
chunk = chunk.slice(next);
|
||||
this.push(line);
|
||||
}
|
||||
callback();
|
||||
}
|
||||
|
||||
_flush(callback) {
|
||||
if (this._partial) {
|
||||
this.push(this._partial);
|
||||
this._partial = null;
|
||||
}
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform splitting a reverse stream into lines
|
||||
*
|
||||
* Splits up a reverse stream of chunks created by {@link
|
||||
* module:lib.createReverseReadStream} by newlines and passes on the
|
||||
* lines one by one in reverse order removing the line feed and optional
|
||||
* carriage return from the lines.
|
||||
*
|
||||
* @example
|
||||
* let revLineStream = new libStream.ReverseLineSplitter({ readableObjectMode: true });
|
||||
* revLineStream.on("data", line => { console.log(line.toString("utf8")); });
|
||||
* let revFileStream = await libStream.createReverseReadStream(path);
|
||||
* revFileStream.pipe(revLineStream);
|
||||
*
|
||||
* @memberof module:lib
|
||||
*/
|
||||
class ReverseLineSplitter extends stream.Transform {
|
||||
constructor(options) {
|
||||
super(options);
|
||||
this._partial = null;
|
||||
}
|
||||
|
||||
_transform(chunk, encoding, callback) {
|
||||
if (this._partial) {
|
||||
chunk = Buffer.concat([chunk, this._partial]);
|
||||
this._partial = null;
|
||||
}
|
||||
|
||||
while (chunk.length) {
|
||||
let next = chunk.lastIndexOf("\n", -2);
|
||||
if (next === -1) {
|
||||
this._partial = chunk;
|
||||
break;
|
||||
}
|
||||
|
||||
let end = chunk.length - 1;
|
||||
if (chunk[end] === "\n".charCodeAt(0)) {
|
||||
end -= 1;
|
||||
// Eat carriage return as well if present
|
||||
if (end - next > 0 && chunk[end] === "\r".charCodeAt(0)) {
|
||||
end -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
let line = chunk.slice(next + 1, end + 1);
|
||||
chunk = chunk.slice(0, next + 1);
|
||||
this.push(line);
|
||||
}
|
||||
callback();
|
||||
}
|
||||
|
||||
_flush(callback) {
|
||||
if (this._partial) {
|
||||
let end = this._partial.length - 1;
|
||||
if (this._partial[end] === "\n".charCodeAt(0)) {
|
||||
end -= 1;
|
||||
// Eat carriage return as well if present
|
||||
if (end > 0 && this._partial[end] === "\r".charCodeAt(0)) {
|
||||
end -= 1;
|
||||
}
|
||||
}
|
||||
this.push(this._partial.slice(0, end + 1));
|
||||
this._partial = null;
|
||||
}
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a file read stream running in reverse
|
||||
*
|
||||
* Like fs.createReadStream but provides data chunks starting from the end
|
||||
* of the file and progressing towards the start of the file. The chunks
|
||||
* are not reversed, thus the file content can be recreated by concatenating
|
||||
* the chunks together in the reverse order they are read.
|
||||
*
|
||||
* @param {string} path -
|
||||
* Path to file to open. May be anything accepted by fs.open.
|
||||
* @param {object} options -
|
||||
* Options to pass to fs.createReadStream. Values passed for `fd` and
|
||||
* `fs` will be ignored.
|
||||
* @memberof module:lib
|
||||
*/
|
||||
async function createReverseReadStream(path, options) {
|
||||
const fileFd = await fs.open(path, "r");
|
||||
const fileSize = (await fs.fstat(fileFd)).size;
|
||||
let filePosition = fileSize;
|
||||
const reverseFs = {
|
||||
read(fd, buffer, offset, length, position, callback) {
|
||||
assert(fd === fileFd);
|
||||
assert(position === undefined);
|
||||
length = Math.min(length, filePosition);
|
||||
filePosition -= length;
|
||||
return fs.read(fd, buffer, offset, length, filePosition, callback);
|
||||
},
|
||||
open() { assert(false); },
|
||||
close(fd, callback) { return fs.close(fd, callback); },
|
||||
};
|
||||
return fs.createReadStream("", { ...options, fd: fileFd, fs: reverseFs });
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
LineSplitter,
|
||||
ReverseLineSplitter,
|
||||
createReverseReadStream,
|
||||
};
|
||||
@@ -0,0 +1,702 @@
|
||||
/**
|
||||
* User and permissions library
|
||||
*
|
||||
* Defines data types for users, roles and permissions.
|
||||
*
|
||||
* @author Hornwitser
|
||||
* @module lib/users
|
||||
*/
|
||||
"use strict";
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
const libErrors = require("./errors");
|
||||
|
||||
const PlayerStats = require("./PlayerStats");
|
||||
|
||||
|
||||
/**
|
||||
* Represents a permission that can be granted
|
||||
* @static
|
||||
*/
|
||||
class Permission {
|
||||
constructor(name, title, description, grantByDefault) {
|
||||
this.name = name;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.grantByDefault = grantByDefault;
|
||||
}
|
||||
}
|
||||
const permissions = new Map();
|
||||
|
||||
/**
|
||||
* Define a new user permission for Clusterio
|
||||
*
|
||||
* Defines a permission for use in Clusterio. Defined permissions can be
|
||||
* granted to roles, and checked on users. Name should be `lower_case`
|
||||
* style and start with the plugin name followed by a dot.
|
||||
*
|
||||
* @param {Object} def - The definition for the permission.
|
||||
* @param {string} def.name - The internal name for the permission.
|
||||
* @param {string} def.title - User facing name for permission.
|
||||
* @param {string} def.description - User facing description.
|
||||
* @param {boolean} def.grantByDefault -
|
||||
* If true this role is granted by the generated Player role by default.
|
||||
*
|
||||
*/
|
||||
function definePermission({ name, title, description, grantByDefault=false }) {
|
||||
if (typeof name !== "string" || !name.length) {
|
||||
throw new Error("Expected name to be a non-empty string");
|
||||
}
|
||||
if (typeof title !== "string" || !title.length) {
|
||||
throw new Error("Expected title to be a non-empty string");
|
||||
}
|
||||
if (typeof description !== "string" || !description.length) {
|
||||
throw new Error("Expected description to be a non-empty string");
|
||||
}
|
||||
if (typeof grantByDefault !== "boolean") {
|
||||
throw new Error("Expected grantByDefault to be a boolean");
|
||||
}
|
||||
|
||||
if (permissions.has(name)) {
|
||||
throw new Error(`Permission '${name}' is already defined`);
|
||||
}
|
||||
|
||||
permissions.set(name, new Permission(name, title, description, grantByDefault));
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a collection of granted permissions
|
||||
* @static
|
||||
*/
|
||||
class Role {
|
||||
constructor(serializedRole) {
|
||||
this.id = serializedRole.id;
|
||||
this.name = serializedRole.name;
|
||||
this.description = serializedRole.description;
|
||||
this.permissions = new Set(serializedRole.permissions);
|
||||
}
|
||||
|
||||
serialize() {
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
description: this.description,
|
||||
permissions: [...this.permissions],
|
||||
};
|
||||
}
|
||||
|
||||
grantDefaultPermissions() {
|
||||
for (let permission of permissions.values()) {
|
||||
if (permission.grantByDefault) {
|
||||
this.permissions.add(permission.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ensure the default admin role has full access
|
||||
*
|
||||
* Ensures the role with id 0 exisits and has the core.admin permission.
|
||||
*
|
||||
* @param {Map<number, module:lib.Role>} roles - role storage to modify.
|
||||
* @returns {module:lib.Role} the default admin role
|
||||
* @static
|
||||
*/
|
||||
function ensureDefaultAdminRole(roles) {
|
||||
let admin = roles.get(0);
|
||||
if (!admin) {
|
||||
admin = new Role({ id: 0, name: "Cluster Admin", description: "Cluster wide administrator." });
|
||||
roles.set(0, admin);
|
||||
}
|
||||
admin.permissions.add("core.admin");
|
||||
return admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the default player role has default accesses
|
||||
*
|
||||
* Ensures the role with id 1 exists and has all the permissions that are
|
||||
* assigned by default. Note that this may not be the same role as the one
|
||||
* that is assigned to new users by default.
|
||||
*
|
||||
* @param {Map<number, module:lib.Role>} roles - role storage to modify.
|
||||
* @static
|
||||
*/
|
||||
function ensureDefaultPlayerRole(roles) {
|
||||
let player = roles.get(1);
|
||||
if (!player) {
|
||||
player = new Role({ id: 1, name: "Player", description: "Default player role." });
|
||||
roles.set(1, player);
|
||||
}
|
||||
player.grantDefaultPermissions();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Represeents a user in the cluster
|
||||
*
|
||||
* Holds data about a Factorio user in the cluster.
|
||||
* @static
|
||||
*/
|
||||
class User {
|
||||
constructor(serializedUser, loadedRoles) {
|
||||
/**
|
||||
* Factorio user name.
|
||||
* @type {string}
|
||||
*/
|
||||
this.name = serializedUser.name;
|
||||
|
||||
/**
|
||||
* Instances this user is online on.
|
||||
* @type {Set<number>}
|
||||
*/
|
||||
this.instances = new Set();
|
||||
|
||||
/**
|
||||
* Unix time in seconds the user token must be issued after to be valid.
|
||||
* @type {number}
|
||||
*/
|
||||
this.tokenValidAfter = serializedUser.token_valid_after || 0;
|
||||
|
||||
/**
|
||||
* True if the user is promoted to admin on the Factorio instances.
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.isAdmin = Boolean(serializedUser.is_admin);
|
||||
|
||||
/**
|
||||
* True if the user is whitelisted on the Factorio instances.
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.isWhitelisted = Boolean(serializedUser.is_whitelisted);
|
||||
|
||||
/**
|
||||
* True if the user is banned from Factorio instances.
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.isBanned = Boolean(serializedUser.is_banned);
|
||||
|
||||
/**
|
||||
* Reason for being banned. Ignored if isBanned is false.
|
||||
* @type {string}
|
||||
*/
|
||||
this.banReason = serializedUser.ban_reason || "";
|
||||
|
||||
/**
|
||||
* Roles this user has
|
||||
* @type {Set<module:lib.Role>}
|
||||
*/
|
||||
this.roles = new Set();
|
||||
if (serializedUser.roles) {
|
||||
for (let roleId of serializedUser.roles) {
|
||||
let role = loadedRoles.get(roleId);
|
||||
if (role) {
|
||||
this.roles.add(role);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Per instance statistics for the player this user account is tied to.
|
||||
* @type {Map<number, module:lib.PlayerStats>}
|
||||
*/
|
||||
this.instanceStats = new Map(
|
||||
(serializedUser.instance_stats ? serializedUser.instance_stats : []).map(
|
||||
([id, stats]) => [id, new PlayerStats(stats)]
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* Combined statistics for the player this user account is tied to.
|
||||
* @type {module:lib.PlayerStats}
|
||||
*/
|
||||
this.playerStats = this._calculatePlayerStats();
|
||||
|
||||
/**
|
||||
* True if this user object has been removed from the cluster.
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.isDeleted = false;
|
||||
}
|
||||
|
||||
serialize() {
|
||||
let serialized = {
|
||||
name: this.name,
|
||||
};
|
||||
|
||||
if (this.roles.size) {
|
||||
serialized.roles = [...this.roles].map(role => role.id);
|
||||
}
|
||||
|
||||
if (this.tokenValidAfter) {
|
||||
serialized.token_valid_after = this.tokenValidAfter;
|
||||
}
|
||||
|
||||
if (this.isAdmin) {
|
||||
serialized.is_admin = true;
|
||||
}
|
||||
|
||||
if (this.isWhitelisted) {
|
||||
serialized.is_whitelisted = true;
|
||||
}
|
||||
|
||||
if (this.isBanned) {
|
||||
serialized.is_banned = true;
|
||||
}
|
||||
|
||||
if (this.banReason) {
|
||||
serialized.ban_reason = this.banReason;
|
||||
}
|
||||
|
||||
if (this.instanceStats.size) {
|
||||
serialized.instance_stats = [...this.instanceStats].map(([id, stats]) => [id, stats.toJSON()]);
|
||||
}
|
||||
|
||||
return serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate access token for this user
|
||||
*
|
||||
* @param {string} secret - Secret to sign token with.
|
||||
* @returns {string} JWT access token for the user.
|
||||
*/
|
||||
createToken(secret) {
|
||||
return jwt.sign({ aud: "user", user: this.name }, Buffer.from(secret, "base64"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate current tokens for the user
|
||||
*
|
||||
* Sets the tokenValidAfter property to the current time, which causes
|
||||
* all currently issued tokens for the user to become invalid.
|
||||
*/
|
||||
invalidateToken() {
|
||||
this.tokenValidAfter = Math.floor(Date.now() / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given permission is granted
|
||||
*
|
||||
* Checks the roles the user is member of for one that grants the given
|
||||
* permission. If the permission is not granted for the user a
|
||||
* "Permission denied" error is thrown.
|
||||
*
|
||||
* @param {string} permission - The permission to check for.
|
||||
* @throws {Error} If the given permission does not exist.
|
||||
* @throws {module:lib.PermissionError} if the user does noh have the given permission.
|
||||
*/
|
||||
checkPermission(permission) {
|
||||
if (!permissions.has(permission)) {
|
||||
throw new Error(`permission ${permission} does not exist`);
|
||||
}
|
||||
|
||||
for (let role of this.roles) {
|
||||
if (role.permissions.has("core.admin") || role.permissions.has(permission)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new libErrors.PermissionError("Permission denied");
|
||||
}
|
||||
|
||||
notifyJoin(instance_id) {
|
||||
this.instances.add(instance_id);
|
||||
User.onlineUsers.add(this);
|
||||
}
|
||||
|
||||
notifyLeave(instance_id) {
|
||||
this.instances.delete(instance_id);
|
||||
if (!this.instances.size) {
|
||||
User.onlineUsers.delete(this);
|
||||
}
|
||||
}
|
||||
|
||||
recalculatePlayerStats() {
|
||||
this.playerStats = this._calculatePlayerStats();
|
||||
}
|
||||
|
||||
_calculatePlayerStats() {
|
||||
let playerStats = new PlayerStats();
|
||||
for (let instanceStats of this.instanceStats.values()) {
|
||||
if (!playerStats.lastJoinAt || instanceStats.lastJoinAt > playerStats.lastJoinAt) {
|
||||
playerStats.lastJoinAt = instanceStats.lastJoinAt;
|
||||
}
|
||||
if (!playerStats.lastLeaveAt || instanceStats.lastLeaveAt > playerStats.lastLeaveAt) {
|
||||
playerStats.lastLeaveAt = instanceStats.lastLeaveAt;
|
||||
playerStats.lastLeaveReason = instanceStats.lastLeaveReason;
|
||||
}
|
||||
playerStats.joinCount += instanceStats.joinCount;
|
||||
playerStats.onlineTimeMs += instanceStats.onlineTimeMs;
|
||||
}
|
||||
return playerStats;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Set of users currently online in the cluster.
|
||||
* @type {module:lib.User}
|
||||
*/
|
||||
User.onlineUsers = new Set();
|
||||
|
||||
// Definitions for the built in permissions used in Clusterio.
|
||||
// description should answer "this permission allows you to ___"
|
||||
definePermission({
|
||||
name: "core.admin",
|
||||
title: "Administrator",
|
||||
description: "Bypass all permission checks.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.control.connect",
|
||||
title: "Connect to WebSocket",
|
||||
description: "Connect to the controller's WebSocket control interface.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
|
||||
definePermission({
|
||||
name: "core.controller.get_config",
|
||||
title: "Get controller config",
|
||||
description: "Get the config of controller.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.controller.update_config",
|
||||
title: "Modify controller config",
|
||||
description: "Modify the controller config or entries of the controller config.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.host.list",
|
||||
title: "List hosts",
|
||||
description: "Get the full list of hosts in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.host.subscribe",
|
||||
title: "Subscribe to host updates",
|
||||
description: "Subscribe to be notified on updates on the details of hosts.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.host.generate_token",
|
||||
title: "Generate host token",
|
||||
description: "Generate tokens for hosts to connect to the cluster with.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.host.create_config",
|
||||
title: "Create host config",
|
||||
description: "Create host configs via the controller.",
|
||||
});
|
||||
|
||||
definePermission({
|
||||
name: "core.instance.get",
|
||||
title: "Get instance",
|
||||
description: "Get the details of an instance in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.list",
|
||||
title: "List instances",
|
||||
description: "Get the full list of instances in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.subscribe",
|
||||
title: "Subscribe to instance updates",
|
||||
description: "Subscribe to be notified on updates on the details and status of instances.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.create",
|
||||
title: "Create instance",
|
||||
description: "Create new instances on the controller.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.get_config",
|
||||
title: "Get instance config",
|
||||
description: "Get the config of instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.update_config",
|
||||
title: "Modify instance config",
|
||||
description: "Modify the config or entries of the config for instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.assign",
|
||||
title: "Assign instance",
|
||||
description: "Assign or reassign instances to a host.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.save.list",
|
||||
title: "List saves",
|
||||
description: "List the saves currently on the instance.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.save.list_subscribe",
|
||||
title: "Subscribe to save list updatess",
|
||||
description: "Subscribe to be notifed on updates to the list of saves of instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.save.create",
|
||||
title: "Create new save",
|
||||
description: "Create new savegames on instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.save.rename",
|
||||
title: "Rename save",
|
||||
description: "Rename savegames on instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.save.copy",
|
||||
title: "Copy save",
|
||||
description: "Create copies of savegames on instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.save.delete",
|
||||
title: "Delete save",
|
||||
description: "Delete savegames on instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.save.upload",
|
||||
title: "Upload save",
|
||||
description: "Upload savegames to instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.save.transfer",
|
||||
title: "Transfer save",
|
||||
description: "Transfer savegames between instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.save.download",
|
||||
title: "Download save",
|
||||
description: "Download savegames from instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.export_data",
|
||||
title: "Export locale and icons",
|
||||
description: "Export the the locale and icons from an instance and upload it to the controller.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.extract_players",
|
||||
title: "Extract player stats from running save",
|
||||
description: "Run extraction to create a user for each player in the save and set the online time from the save.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.start",
|
||||
title: "Start instance",
|
||||
description: "Start instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.load_scenario",
|
||||
title: "Load scenario",
|
||||
description: "Start instances by loading a scenario.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.stop",
|
||||
title: "Stop instance",
|
||||
description: "Stop instances.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.kill",
|
||||
title: "Kill instance",
|
||||
description: "Terminate running instances without saving or cleanup.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.delete",
|
||||
title: "Delete instance",
|
||||
description: "Delete instances (includes config and all files stored like saves and logs).",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.instance.send_rcon",
|
||||
title: "Send RCON",
|
||||
description: "Send arbitrary RCON commands to instances.",
|
||||
});
|
||||
|
||||
definePermission({
|
||||
name: "core.mod_pack.get",
|
||||
title: "Retrieve mod pack",
|
||||
description: "Get the details of a mod pack in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.list",
|
||||
title: "List mod packs",
|
||||
description: "Get the full list of a mod packs in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.subscribe",
|
||||
title: "Subscribe to mod pack updates",
|
||||
description: "Subscribe to be notified on updates on the details of mod packs.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.create",
|
||||
title: "Create mod pack",
|
||||
description: "Create new mod pack in the cluster.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.update",
|
||||
title: "Update mod pack",
|
||||
description: "Edit existing mod pack definition in the cluster.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.delete",
|
||||
title: "Delete mod pack",
|
||||
description: "Delete mod pack definition in the cluster.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod.get",
|
||||
title: "Get mods",
|
||||
description: "Get the details of a mod stored in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod.list",
|
||||
title: "List mods",
|
||||
description: "Get the full list of mods stored on the controller.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod.search",
|
||||
title: "Search mods",
|
||||
description: "Search through the list of mods stored on the controller.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod.subscribe",
|
||||
title: "Subscribe to mod updates",
|
||||
description: "Subscribe to be notified on updates on the details of mods.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod.upload",
|
||||
title: "Upload mod",
|
||||
description: "Upload mods to the controller.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod.download",
|
||||
title: "Download mod",
|
||||
description: "Download mods stored on the controller.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod.delete",
|
||||
title: "Delete mod",
|
||||
description: "Delete mods stored on the controller.",
|
||||
});
|
||||
|
||||
definePermission({
|
||||
name: "core.permission.list",
|
||||
title: "List permissions",
|
||||
description: "Get the full list of permissions in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
|
||||
definePermission({
|
||||
name: "core.role.list",
|
||||
title: "List roles",
|
||||
description: "Get the full list of roles and their permissions in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.role.create",
|
||||
title: "Create role",
|
||||
description: "Create new roles with permissions.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.role.update",
|
||||
title: "Update role",
|
||||
description: "Edit existing roles including permissions they grant.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.role.delete",
|
||||
title: "Delete role",
|
||||
description: "Permanently delete roles.",
|
||||
});
|
||||
|
||||
definePermission({
|
||||
name: "core.user.get",
|
||||
title: "Get user",
|
||||
description: "Get the details of a user in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.list",
|
||||
title: "List users",
|
||||
description: "Get the full list of users in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.subscribe",
|
||||
title: "Subscribe to user updates",
|
||||
description: "Subscribe to be notified on updates on the details and status of users.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.create",
|
||||
title: "Create users",
|
||||
description: "Create user acounts with a given name.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.revoke_token",
|
||||
title: "Revoke user token",
|
||||
description: "Revoke access token and terminate all existing control connections for oneself.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.revoke_other_token",
|
||||
title: "Revoke other user's token",
|
||||
description: "Allow revoking tokens for other users than oneself. Requires Revoke user token.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.update_roles",
|
||||
title: "Update user roles",
|
||||
description: "Add or remove any role to any user.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.set_admin",
|
||||
title: "Set user admin status",
|
||||
description: "Promote or demote any user to Factorio admin.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.set_banned",
|
||||
title: "Set user ban status",
|
||||
description: "Ban or unban any user.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.set_whitelisted",
|
||||
title: "Set user whitelist status",
|
||||
description: "Add or remove any user to/from the whitelist.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.user.delete",
|
||||
title: "Delete user",
|
||||
description: "Delete users and all data stored for them.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.log.follow",
|
||||
title: "Follow cluster log",
|
||||
description: "Receive new entries in the cluster log. Required to see instance console.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.log.query",
|
||||
title: "Query cluster log",
|
||||
description: "Query past entries in the cluster log. Required to see past entries in instance console.",
|
||||
});
|
||||
|
||||
definePermission({
|
||||
name: "core.debug.dump_ws",
|
||||
title: "Dump WebSocket",
|
||||
description: "Dump all WebSocket communicatation from the controller.",
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
Permission,
|
||||
Role,
|
||||
User,
|
||||
|
||||
permissions,
|
||||
definePermission,
|
||||
ensureDefaultAdminRole,
|
||||
ensureDefaultPlayerRole,
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Returns the root folder in the zip file
|
||||
*
|
||||
* Returns the name of the folder that all files in the zip file is
|
||||
* contained in. Throws an error if there are multiple such folders.
|
||||
*
|
||||
* @param {JSZip} zip - Zip to search through.
|
||||
* @returns {string} name of the root folder.
|
||||
* @memberof module:lib
|
||||
* @private
|
||||
* @inner
|
||||
*/
|
||||
function findRoot(zip) {
|
||||
let root = null;
|
||||
zip.forEach((relativePath, file) => {
|
||||
let index = relativePath.indexOf("/");
|
||||
if (index === -1) {
|
||||
throw new Error(`Zip contains file '${relativePath}' in root dir`);
|
||||
}
|
||||
|
||||
let pathRoot = relativePath.slice(0, index);
|
||||
if (root === null) {
|
||||
root = pathRoot;
|
||||
} else if (root !== pathRoot) {
|
||||
throw new Error("Zip contains multiple root folders");
|
||||
}
|
||||
});
|
||||
|
||||
if (root === null) {
|
||||
throw new Error("Empty zip file");
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
findRoot,
|
||||
};
|
||||
Reference in new issue
Block a user