exaroton 1.8.3 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -291,6 +291,40 @@ try {
291
291
  }
292
292
  ```
293
293
 
294
+ #### Config files
295
+ Config files are files that contain server configuration and are shown as forms rather than plain text files in the exaroton panel.
296
+ You can get a config file object by using the `getConfig()` method on a file object.
297
+ Whether a file is a config file can be checked using the `isConfigFile` property.
298
+
299
+ ```js
300
+ let file = server.getFile("server.properties");
301
+ let config = file.getConfig();
302
+ ```
303
+
304
+ #### Get config file options
305
+ ```js
306
+ let options = await config.getOptions();
307
+ for(let [key, option] of options) {
308
+ console.log(key, option.getValue());
309
+ }
310
+ ```
311
+
312
+ #### Update config file options
313
+ ```js
314
+ let options = await config.getOptions();
315
+
316
+ options.get("max-players").setValue(26);
317
+ options.get("pvp").setValue(false);
318
+
319
+ await config.save();
320
+ ```
321
+
322
+ Options of type `select` or `multiselect` have a list of possible values.
323
+ ```js
324
+ let options = await config.getOptions();
325
+ console.log(options.get("difficulty").getOptions());
326
+ ```
327
+
294
328
  ### Websocket API
295
329
  The websocket API allows a constant connection to our websocket service to receive
296
330
  events in real time without polling (e.g. trying to get the server status every few seconds).
@@ -372,4 +406,4 @@ You can unsubscribe from one, multiple or all streams using the `server.unsubscr
372
406
  server.unsubscribe("console");
373
407
  server.unsubscribe(["tick", "heap"]);
374
408
  server.unsubscribe(); // this disconnects the websocket connection
375
- ```
409
+ ```
package/index.js CHANGED
@@ -4,5 +4,6 @@ module.exports = {
4
4
  Software: require('./src/Server/Software'),
5
5
  ServerStatus: require('./src/Server/ServerStatus'),
6
6
  Request: require('./src/Request/Request'),
7
- Response: require('./src/Response/Response')
8
- }
7
+ Response: require('./src/Response/Response'),
8
+ ConfigOptionType: require('./src/Server/Config/ConfigOptionType')
9
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exaroton",
3
- "version": "1.8.3",
3
+ "version": "1.10.0",
4
4
  "description": "exaroton API client",
5
5
  "homepage": "https://exaroton.com",
6
6
  "main": "index.js",
@@ -11,7 +11,6 @@
11
11
  "node": ">=12.0.0"
12
12
  },
13
13
  "dependencies": {
14
- "form-data": "^3.0.1",
15
14
  "got": "^11.8.2",
16
15
  "ws": "^7.4.5"
17
16
  }
package/src/Client.js CHANGED
@@ -106,15 +106,10 @@ class Client {
106
106
  async request(request) {
107
107
  request.client = this;
108
108
  const url = this.baseURL + request.getEndpoint();
109
- const headers = Object.assign({
110
- "authorization": "Bearer " + this.#apiToken,
111
- "user-agent": this.#userAgent
112
- }, request.headers);
113
109
 
114
110
  let gotOptions = {
115
111
  method: request.method,
116
112
  retry: 0,
117
- headers: headers,
118
113
  responseType: request.responseType
119
114
  };
120
115
 
@@ -122,6 +117,11 @@ class Client {
122
117
  gotOptions.body = request.getBody();
123
118
  }
124
119
 
120
+ gotOptions.headers = Object.assign({
121
+ "authorization": "Bearer " + this.#apiToken,
122
+ "user-agent": this.#userAgent
123
+ }, request.headers);
124
+
125
125
  let response;
126
126
  try {
127
127
  if (request.hasOutputStream()) {
@@ -1,4 +1,3 @@
1
- const FormData = require('form-data');
2
1
  const {createReadStream} = require('fs');
3
2
  const {createWriteStream} = require("fs");
4
3
 
@@ -149,20 +148,8 @@ class Request {
149
148
  return this.data;
150
149
  }
151
150
 
152
- let body = new FormData();
153
- for (let key in this.data) {
154
- if (!this.data.hasOwnProperty(key)) {
155
- continue;
156
- }
157
- if (Array.isArray(this.data[key])) {
158
- for (let element of this.data[key]) {
159
- body.append(key + "[]", element);
160
- }
161
- continue;
162
- }
163
- body.append(key, this.data[key]);
164
- }
165
- return body;
151
+ this.setHeader("content-type", "application/json");
152
+ return JSON.stringify(this.data);
166
153
  }
167
154
 
168
155
  /**
@@ -0,0 +1,7 @@
1
+ const FileRequest = require("../FileRequest");
2
+
3
+ class GetConfigFileOptionsRequest extends FileRequest {
4
+ endpoint = "servers/{id}/files/config/{path}";
5
+ }
6
+
7
+ module.exports = GetConfigFileOptionsRequest;
@@ -0,0 +1,21 @@
1
+ const FileRequest = require("../FileRequest");
2
+
3
+ class UpdateConfigFileOptionsRequest extends FileRequest {
4
+ endpoint = "servers/{id}/files/config/{path}";
5
+ method = "POST";
6
+
7
+ /**
8
+ * UpdateConfigFileOptionsRequest constructor
9
+ *
10
+ * @param {string} id
11
+ * @param {string} path
12
+ * @param {Object.<string, string|number|boolean|string[]>} options
13
+ */
14
+ constructor(id, path, options) {
15
+ super(id, path);
16
+
17
+ this.data = options;
18
+ }
19
+ }
20
+
21
+ module.exports = UpdateConfigFileOptionsRequest;
@@ -2,6 +2,20 @@ const ServerRequest = require('./ServerRequest');
2
2
 
3
3
  class StartServerRequest extends ServerRequest {
4
4
  endpoint = "servers/{id}/start";
5
+ method = "POST";
6
+
7
+ /**
8
+ * StartServerRequest constructor
9
+ *
10
+ * @param {string} id
11
+ * @param {boolean} useOwnCredits
12
+ */
13
+ constructor(id, useOwnCredits = false) {
14
+ super(id);
15
+ this.data = {
16
+ useOwnCredits: useOwnCredits
17
+ };
18
+ }
5
19
  }
6
20
 
7
21
  module.exports = StartServerRequest;
@@ -0,0 +1,115 @@
1
+ const GetConfigFileOptionsRequest = require("../../Request/Server/Files/Config/GetConfigFileOptionsRequest");
2
+ const UpdateConfigFileOptionsRequest = require("../../Request/Server/Files/Config/UpdateConfigFileOptionsRequest");
3
+ const ConfigOption = require("./ConfigOption");
4
+
5
+ class Config {
6
+ /**
7
+ * @type {File}
8
+ */
9
+ #file;
10
+
11
+ /**
12
+ * @type {null|Map<string, ConfigOption>}
13
+ */
14
+ #options = null;
15
+
16
+ /**
17
+ * @type {null|Map<string, ConfigOptionValue>}
18
+ */
19
+ #originalValues = null;
20
+
21
+ /**
22
+ * @param {File} file
23
+ */
24
+ constructor(file) {
25
+ this.#file = file;
26
+ }
27
+
28
+ /**
29
+ * @param {Object} object
30
+ * @return {this}
31
+ */
32
+ applyData(object) {
33
+ this.#options = new Map();
34
+ this.#originalValues = new Map();
35
+
36
+ if (!Array.isArray(object)) {
37
+ return this;
38
+ }
39
+
40
+ for (const option of object) {
41
+ if (typeof option !== "object") {
42
+ continue;
43
+ }
44
+
45
+ let {key, label, type, value, options} = option;
46
+ if (typeof key !== "string" || typeof label !== "string" || typeof type !== "string" || options && !Array.isArray(options)) {
47
+ continue;
48
+ }
49
+
50
+ this.#options.set(key, new ConfigOption(key, label, type, value, options));
51
+ this.#originalValues.set(key, value);
52
+ }
53
+ return this;
54
+ }
55
+
56
+ /**
57
+ * @return {Promise<this>}
58
+ */
59
+ async #loadOptions() {
60
+ const response = await this.#file.getClient().request(new GetConfigFileOptionsRequest(this.#file.getServer().id, this.#file.path));
61
+ this.applyData(response.getData());
62
+ return this;
63
+ }
64
+
65
+ /**
66
+ * @param {boolean} update
67
+ * @return {Promise<Map<string, ConfigOption>>}
68
+ */
69
+ async getOptions(update = false) {
70
+ if (update || this.#options === null) {
71
+ await this.#loadOptions();
72
+ }
73
+ return this.#options;
74
+ }
75
+
76
+ /**
77
+ * @param {string} key
78
+ * @return {Promise<ConfigOption|null>}
79
+ */
80
+ async getOption(key) {
81
+ const options = await this.getOptions();
82
+ return options.get(key) ?? null;
83
+ }
84
+
85
+ /**
86
+ * Save all changes made to this config file
87
+ *
88
+ * @return {Promise<Response|null>} null if no changes were made
89
+ */
90
+ async save() {
91
+ let updated = false;
92
+ let changed = {};
93
+ for (let [key, option] of this.#options) {
94
+ if (option.getValue() !== this.#originalValues.get(key)) {
95
+ updated = true;
96
+ changed[key] = option.getValue();
97
+ }
98
+ }
99
+
100
+ if (!updated) {
101
+ return null;
102
+ }
103
+
104
+ let response = await this.#file.getClient().request(
105
+ new UpdateConfigFileOptionsRequest(this.#file.getServer().id, this.#file.path, changed));
106
+
107
+ for (let [key, value] of Object.entries(changed)) {
108
+ this.#originalValues.set(key, value);
109
+ }
110
+
111
+ return response;
112
+ }
113
+ }
114
+
115
+ module.exports = Config;
@@ -0,0 +1,103 @@
1
+ /**
2
+ * @typedef {string|number|boolean|string[]} ConfigOptionValue
3
+ */
4
+
5
+ class ConfigOption {
6
+ /**
7
+ * Key of this config option
8
+ *
9
+ * @type {string}
10
+ */
11
+ #key;
12
+
13
+ /**
14
+ * Label of this config option
15
+ *
16
+ * @type {string}
17
+ */
18
+ #label;
19
+
20
+ /**
21
+ * Option type
22
+ *
23
+ * @type {string}
24
+ */
25
+ #type;
26
+
27
+ /**
28
+ * Current option value
29
+ *
30
+ * @type {ConfigOptionValue}
31
+ */
32
+ #value;
33
+
34
+ /**
35
+ * Available options for select/multiselect
36
+ *
37
+ * @type {string[]|null}
38
+ */
39
+ #options;
40
+
41
+ /**
42
+ * ConfigOption constructor
43
+ *
44
+ * @param {string} key
45
+ * @param {string} label
46
+ * @param {string} type
47
+ * @param {ConfigOptionValue} value
48
+ * @param {string[]|null} options
49
+ */
50
+ constructor(key, label, type, value, options = null) {
51
+ this.#key = key;
52
+ this.#label = label;
53
+ this.#type = type;
54
+ this.#value = value;
55
+ this.#options = options;
56
+ }
57
+
58
+ /**
59
+ * @return {string}
60
+ */
61
+ getKey() {
62
+ return this.#key;
63
+ }
64
+
65
+ /**
66
+ * @return {string}
67
+ */
68
+ getLabel() {
69
+ return this.#label;
70
+ }
71
+
72
+ /**
73
+ * @return {string}
74
+ */
75
+ getType() {
76
+ return this.#type;
77
+ }
78
+
79
+ /**
80
+ * @return {ConfigOptionValue}
81
+ */
82
+ getValue() {
83
+ return this.#value;
84
+ }
85
+
86
+ /**
87
+ * @param {ConfigOptionValue} value
88
+ * @return {ConfigOption}
89
+ */
90
+ setValue(value) {
91
+ this.#value = value;
92
+ return this;
93
+ }
94
+
95
+ /**
96
+ * @return {string[]|null}
97
+ */
98
+ getOptions() {
99
+ return this.#options;
100
+ }
101
+ }
102
+
103
+ module.exports = ConfigOption;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @enum {string}
3
+ */
4
+ class ConfigOptionType {
5
+ static STRING = "string";
6
+ static INTEGER = "number";
7
+ static FLOAT = "float";
8
+ static BOOLEAN = "boolean";
9
+ static MULTI_SELECT = "multiselect";
10
+ static SELECT = "select";
11
+ }
12
+
13
+ module.exports = ConfigOptionType;
@@ -3,6 +3,7 @@ const GetFileDataRequest = require("../Request/Server/Files/GetFileDataRequest")
3
3
  const PutFileDataRequest = require("../Request/Server/Files/PutFileDataRequest");
4
4
  const DeleteFileDataRequest = require("../Request/Server/Files/DeleteFileDataRequest");
5
5
  const CreateDirectoryRequest = require("../Request/Server/Files/CreateDirectoryRequest");
6
+ const Config = require("./Config/Config");
6
7
 
7
8
  class File {
8
9
  /**
@@ -60,15 +61,20 @@ class File {
60
61
  children = null;
61
62
 
62
63
  /**
63
- * @type {{Server}}
64
+ * @type {Server}
64
65
  */
65
66
  #server;
66
67
 
67
68
  /**
68
- * @type {{Client}}
69
+ * @type {Client}
69
70
  */
70
71
  #client;
71
72
 
73
+ /**
74
+ * @type {Config|null}
75
+ */
76
+ #config = null;
77
+
72
78
  /**
73
79
  * @param {string|null} path
74
80
  */
@@ -113,7 +119,7 @@ class File {
113
119
  /**
114
120
  * Set the server
115
121
  *
116
- * @param server
122
+ * @param {Server} server
117
123
  * @returns {this}
118
124
  */
119
125
  setServer(server) {
@@ -129,7 +135,7 @@ class File {
129
135
  /**
130
136
  * Set the API client
131
137
  *
132
- * @param client
138
+ * @param {Client} client
133
139
  * @returns {this}
134
140
  */
135
141
  setClient(client) {
@@ -142,6 +148,20 @@ class File {
142
148
  return this;
143
149
  }
144
150
 
151
+ /**
152
+ * @return {Client}
153
+ */
154
+ getClient() {
155
+ return this.#client;
156
+ }
157
+
158
+ /**
159
+ * @return {Server}
160
+ */
161
+ getServer() {
162
+ return this.#server;
163
+ }
164
+
145
165
  /**
146
166
  * Get file information from the API
147
167
  *
@@ -251,6 +271,19 @@ class File {
251
271
 
252
272
  return this.children;
253
273
  }
274
+
275
+ /**
276
+ * Get Config object for this file
277
+ * Only available if the file is a config file
278
+ *
279
+ * @return {Config}
280
+ */
281
+ getConfig() {
282
+ if (this.#config === null) {
283
+ this.#config = new Config(this);
284
+ }
285
+ return this.#config;
286
+ }
254
287
  }
255
288
 
256
- module.exports = File;
289
+ module.exports = File;
@@ -149,11 +149,12 @@ class Server extends EventEmitter {
149
149
  /**
150
150
  * Start the server
151
151
  *
152
+ * @param {boolean} useOwnCredits
152
153
  * @return {Promise<Response>}
153
154
  * @throws {RequestError}
154
155
  */
155
- async start() {
156
- return this.#client.request(new StartServerRequest(this.id));
156
+ async start(useOwnCredits = false) {
157
+ return this.#client.request(new StartServerRequest(this.id, useOwnCredits));
157
158
  }
158
159
 
159
160
  /**