wispjs 2.1.3 → 2.1.4

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.
Files changed (49) hide show
  1. package/README.md +4 -4
  2. package/{wisp.ts → dist/wisp.d.ts} +11 -28
  3. package/dist/wisp.js +34 -0
  4. package/dist/wisp_api/apis/allocations.d.ts +55 -0
  5. package/dist/wisp_api/apis/allocations.js +32 -0
  6. package/dist/wisp_api/apis/audit_log.d.ts +68 -0
  7. package/dist/wisp_api/apis/audit_log.js +21 -0
  8. package/{wisp_api/apis/backups.ts → dist/wisp_api/apis/backups.d.ts} +13 -51
  9. package/dist/wisp_api/apis/backups.js +93 -0
  10. package/dist/wisp_api/apis/databases.d.ts +61 -0
  11. package/dist/wisp_api/apis/databases.js +43 -0
  12. package/dist/wisp_api/apis/fastdl.d.ts +19 -0
  13. package/dist/wisp_api/apis/fastdl.js +21 -0
  14. package/dist/wisp_api/apis/filesystem.d.ts +200 -0
  15. package/dist/wisp_api/apis/filesystem.js +182 -0
  16. package/dist/wisp_api/apis/index.d.ts +52 -0
  17. package/dist/wisp_api/apis/index.js +100 -0
  18. package/dist/wisp_api/apis/mods.d.ts +40 -0
  19. package/dist/wisp_api/apis/mods.js +30 -0
  20. package/dist/wisp_api/apis/schedules.d.ts +179 -0
  21. package/dist/wisp_api/apis/schedules.js +167 -0
  22. package/dist/wisp_api/apis/servers.d.ts +120 -0
  23. package/dist/wisp_api/apis/servers.js +76 -0
  24. package/dist/wisp_api/apis/startup.d.ts +52 -0
  25. package/dist/wisp_api/apis/startup.js +35 -0
  26. package/dist/wisp_api/apis/subusers.d.ts +106 -0
  27. package/dist/wisp_api/apis/subusers.js +87 -0
  28. package/dist/wisp_api/index.d.ts +39 -0
  29. package/dist/wisp_api/index.js +41 -0
  30. package/dist/wisp_socket/index.d.ts +161 -0
  31. package/{wisp_socket/index.ts → dist/wisp_socket/index.js} +130 -236
  32. package/dist/wisp_socket/pool.d.ts +183 -0
  33. package/dist/wisp_socket/pool.js +171 -0
  34. package/package.json +1 -1
  35. package/.github/workflows/release.yml +0 -72
  36. package/tsconfig.json +0 -19
  37. package/wisp_api/apis/allocations.ts +0 -71
  38. package/wisp_api/apis/audit_log.ts +0 -81
  39. package/wisp_api/apis/databases.ts +0 -80
  40. package/wisp_api/apis/fastdl.ts +0 -22
  41. package/wisp_api/apis/filesystem.ts +0 -291
  42. package/wisp_api/apis/index.ts +0 -135
  43. package/wisp_api/apis/mods.ts +0 -53
  44. package/wisp_api/apis/schedules.ts +0 -270
  45. package/wisp_api/apis/servers.ts +0 -155
  46. package/wisp_api/apis/startup.ts +0 -65
  47. package/wisp_api/apis/subusers.ts +0 -159
  48. package/wisp_api/index.ts +0 -57
  49. package/wisp_socket/pool.ts +0 -387
@@ -1,54 +1,16 @@
1
1
  import { WebsocketPool } from "./pool.js";
2
- import { ConsoleMessage, FilesearchResults } from "./pool";
3
- import { GitPullData, GitPullResult } from "./pool.js";
4
- import { GitCloneData, GitCloneResult } from "./pool.js";
5
-
6
- import type { WispAPI } from "../wisp_api/index.js";
7
-
8
-
9
- /**
10
- * The Websocket information returned from the API
11
- *
12
- * @param token The token to use when authenticating with the `auth` command in the Websocket
13
- * @param url The actual URL of the Websocket
14
- *
15
- * @internal
16
- */
17
- export interface WebsocketInfo {
18
- token: string;
19
- url: string;
20
- }
21
-
22
-
23
- export type WebsocketDetailsPreprocessor = (info: WebsocketInfo) => void;
24
-
25
-
26
- export interface WispSocket {
27
- pool: WebsocketPool;
28
- logger: any;
29
- api: WispAPI;
30
- url: string | undefined;
31
- token: string | undefined;
32
- ghToken: string | undefined;
33
- consoleCallbacks: ((message: string) => void)[];
34
- detailsPreprocessor: WebsocketDetailsPreprocessor | undefined;
35
- }
36
-
37
-
38
2
  /**
39
3
  * The primary interface to the Websocket API
40
4
  *
41
5
  * @internal
42
6
  */
43
7
  export class WispSocket {
44
- constructor(logger: any, api: any, ghToken: string | undefined) {
45
- this.logger = logger
46
- this.api = api
47
- this.ghToken = ghToken
48
- this.consoleCallbacks = []
8
+ constructor(logger, api, ghToken) {
9
+ this.logger = logger;
10
+ this.api = api;
11
+ this.ghToken = ghToken;
12
+ this.consoleCallbacks = [];
49
13
  }
50
-
51
-
52
14
  /**
53
15
  * Sets a callback to run on the Websocket Info before saving the details.
54
16
  *
@@ -67,11 +29,9 @@ export class WispSocket {
67
29
  *
68
30
  * @public
69
31
  */
70
- setWebsocketDetailsPreprocessor(preprocessor: WebsocketDetailsPreprocessor) {
32
+ setWebsocketDetailsPreprocessor(preprocessor) {
71
33
  this.detailsPreprocessor = preprocessor;
72
34
  }
73
-
74
-
75
35
  /**
76
36
  * Creates a new Websocket Pool
77
37
  *
@@ -80,13 +40,10 @@ export class WispSocket {
80
40
  */
81
41
  createPool() {
82
42
  if (!this.url || !this.token) {
83
- throw new Error("Attempted to create a pool without a URL or token")
43
+ throw new Error("Attempted to create a pool without a URL or token");
84
44
  }
85
-
86
- this.pool = new WebsocketPool(this.url, this.token)
45
+ this.pool = new WebsocketPool(this.url, this.token);
87
46
  }
88
-
89
-
90
47
  /**
91
48
  * Requests and saves the Websocket details from the API
92
49
  *
@@ -94,22 +51,19 @@ export class WispSocket {
94
51
  */
95
52
  async setDetails() {
96
53
  try {
97
- const websocketInfo: WebsocketInfo = await this.api.Servers.GetWebsocketDetails()
54
+ const websocketInfo = await this.api.Servers.GetWebsocketDetails();
98
55
  if (this.detailsPreprocessor) {
99
56
  this.detailsPreprocessor(websocketInfo);
100
57
  }
101
-
102
- this.url = websocketInfo.url
103
- this.token = websocketInfo.token
104
-
105
- this.logger.info("Got Websocket Details.", this.url, this.token)
106
- } catch(e) {
107
- this.logger.error(`Failed to get websocket details: ${e}`)
108
- throw(e)
58
+ this.url = websocketInfo.url;
59
+ this.token = websocketInfo.token;
60
+ this.logger.info("Got Websocket Details.", this.url, this.token);
61
+ }
62
+ catch (e) {
63
+ this.logger.error(`Failed to get websocket details: ${e}`);
64
+ throw (e);
109
65
  }
110
66
  }
111
-
112
-
113
67
  /**
114
68
  * Disconnects from the websocket
115
69
  *
@@ -117,11 +71,9 @@ export class WispSocket {
117
71
  */
118
72
  async disconnect() {
119
73
  if (this.pool) {
120
- await this.pool.disconnect()
74
+ await this.pool.disconnect();
121
75
  }
122
76
  }
123
-
124
-
125
77
  /**
126
78
  * Verifies that the pool is created and ready to use
127
79
  *
@@ -129,12 +81,10 @@ export class WispSocket {
129
81
  */
130
82
  async verifyPool() {
131
83
  if (!this.pool) {
132
- await this.setDetails()
133
- this.createPool()
84
+ await this.setDetails();
85
+ this.createPool();
134
86
  }
135
87
  }
136
-
137
-
138
88
  /**
139
89
  * Searches all file contents for the given query
140
90
  *
@@ -142,38 +92,27 @@ export class WispSocket {
142
92
  *
143
93
  * @public
144
94
  */
145
- async filesearch(query: string): Promise<FilesearchResults> {
146
- this.logger.info("Running filesearch with: ", query)
147
- await this.verifyPool()
148
-
95
+ async filesearch(query) {
96
+ this.logger.info("Running filesearch with: ", query);
97
+ await this.verifyPool();
149
98
  return await this.pool.run((worker) => {
150
- const socket = worker.socket
151
- const logger = worker.logger
152
- logger.log("Running filesearch:", query)
153
-
154
- return new Promise<FilesearchResults>((resolve, reject) => {
155
- let done = false
156
-
99
+ const socket = worker.socket;
100
+ const logger = worker.logger;
101
+ logger.log("Running filesearch:", query);
102
+ return new Promise((resolve, reject) => {
103
+ const timeout = setTimeout(() => {
104
+ socket.off("filesearch-results");
105
+ logger.error("Rejected filesearch: 'Timeout'");
106
+ reject();
107
+ }, 10000);
157
108
  socket.once("filesearch-results", (data) => {
158
- done = true
159
- resolve(data)
160
- })
161
-
162
- socket.emit("filesearch-start", query)
163
-
164
- // This uses a longer timeout because filesearch can take a while
165
- setTimeout(() => {
166
- if (!done) {
167
- socket.off("filesearch-results")
168
- logger.error("Rejected filesearch: 'Timeout'")
169
- reject()
170
- }
171
- }, 10000)
172
- })
173
- })
109
+ clearTimeout(timeout);
110
+ resolve(data);
111
+ });
112
+ socket.emit("filesearch-start", query);
113
+ });
114
+ });
174
115
  }
175
-
176
-
177
116
  /**
178
117
  * Performs a git pull operation on the given directory
179
118
  *
@@ -181,83 +120,67 @@ export class WispSocket {
181
120
  *
182
121
  * @public
183
122
  */
184
- async gitPull(dir: string, useAuth: boolean = false) {
185
- await this.verifyPool()
186
-
123
+ async gitPull(dir, useAuth = false) {
124
+ await this.verifyPool();
187
125
  const pullResult = await this.pool.run((worker) => {
188
126
  const socket = worker.socket;
189
127
  const logger = worker.logger;
190
128
  logger.log("Running gitPull:", dir);
191
-
192
- return new Promise<GitPullResult>((resolve, reject) => {
193
- let isPrivate = false
194
-
195
- const finished = (success: boolean, output: string) => {
196
- socket.removeAllListeners("git-pull")
197
- socket.removeAllListeners("git-error")
198
- socket.removeAllListeners("git-success")
199
-
200
- const result: GitPullResult = {
129
+ return new Promise((resolve, reject) => {
130
+ let isPrivate = false;
131
+ const finished = (success, output) => {
132
+ socket.removeAllListeners("git-pull");
133
+ socket.removeAllListeners("git-error");
134
+ socket.removeAllListeners("git-success");
135
+ const result = {
201
136
  output: output,
202
137
  isPrivate: isPrivate
203
- }
204
-
138
+ };
205
139
  if (success) {
206
- resolve(result)
207
- } else {
140
+ resolve(result);
141
+ }
142
+ else {
208
143
  logger.error("Rejected gitPull:", dir, output);
209
- reject(output)
144
+ reject(output);
210
145
  }
211
- }
212
-
213
- const sendRequest = (includeAuth: boolean = false) => {
214
- const data: GitPullData = { dir: dir }
215
-
146
+ };
147
+ const sendRequest = (includeAuth = false) => {
148
+ const data = { dir: dir };
216
149
  if (includeAuth) {
217
150
  if (!this.ghToken) {
218
- logger.error("No GitHub token set, can't authenticate")
219
- return finished(false, "Authentication is required, but no GitHub token was set. Can't pull!")
151
+ logger.error("No GitHub token set, can't authenticate");
152
+ return finished(false, "Authentication is required, but no GitHub token was set. Can't pull!");
220
153
  }
221
-
222
- isPrivate = true
223
- data.authkey = this.ghToken
154
+ isPrivate = true;
155
+ data.authkey = this.ghToken;
224
156
  }
225
-
226
- socket.emit("git-pull", data)
227
- }
228
-
157
+ socket.emit("git-pull", data);
158
+ };
229
159
  socket.once("git-pull", (data) => {
230
- logger.log(`Updating ${data}`)
160
+ logger.log(`Updating ${data}`);
231
161
  });
232
-
233
162
  socket.once("git-success", (commit) => {
234
- logger.log(`Addon updated to ${commit}`)
235
-
163
+ logger.log(`Addon updated to ${commit}`);
236
164
  if (!commit) {
237
- logger.log("No commit given!")
165
+ logger.log("No commit given!");
238
166
  }
239
-
240
- finished(true, commit || "")
167
+ finished(true, commit || "");
241
168
  });
242
-
243
169
  socket.on("git-error", (message) => {
244
170
  if (message === "Remote authentication required but no callback set") {
245
- logger.log(`Remote authentication required, trying again with authkey: ${dir}`)
246
- sendRequest(true)
247
- } else {
248
- logger.log(`Error updating addon: ${message}`)
249
- finished(false, message)
171
+ logger.log(`Remote authentication required, trying again with authkey: ${dir}`);
172
+ sendRequest(true);
173
+ }
174
+ else {
175
+ logger.log(`Error updating addon: ${message}`);
176
+ finished(false, message);
250
177
  }
251
- })
252
-
253
- sendRequest(useAuth)
254
- })
255
- })
256
-
257
- return pullResult
178
+ });
179
+ sendRequest(useAuth);
180
+ });
181
+ });
182
+ return pullResult;
258
183
  }
259
-
260
-
261
184
  /**
262
185
  * Clones a new Repo to the given directory
263
186
  *
@@ -267,75 +190,62 @@ export class WispSocket {
267
190
  *
268
191
  * @public
269
192
  */
270
- async gitClone(url: string, dir: string, branch: string) {
271
- await this.verifyPool()
272
-
193
+ async gitClone(url, dir, branch) {
194
+ await this.verifyPool();
273
195
  return await this.pool.run((worker) => {
274
196
  const socket = worker.socket;
275
197
  const logger = worker.logger;
276
198
  logger.log("Running gitClone:", url, dir, branch);
277
-
278
- return new Promise<GitCloneResult>((resolve, reject) => {
199
+ return new Promise((resolve, reject) => {
279
200
  let isPrivate = false;
280
-
281
- const finished = (success: boolean, message?: string) => {
201
+ const finished = (success, message) => {
282
202
  socket.removeAllListeners("git-clone");
283
203
  socket.removeAllListeners("git-error");
284
204
  socket.removeAllListeners("git-success");
285
-
286
205
  if (success) {
287
- const result: GitCloneResult = {
206
+ const result = {
288
207
  isPrivate: isPrivate
289
- }
290
-
208
+ };
291
209
  resolve(result);
292
- } else {
210
+ }
211
+ else {
293
212
  logger.error("Rejected gitClone:", url, dir, branch, message);
294
213
  reject(message);
295
214
  }
296
- }
297
-
298
- const sendRequest = (includeAuth: boolean = false) => {
299
- const data: GitCloneData = { dir: dir, url: url, branch: branch };
300
-
215
+ };
216
+ const sendRequest = (includeAuth = false) => {
217
+ const data = { dir: dir, url: url, branch: branch };
301
218
  if (includeAuth) {
302
219
  if (!this.ghToken) {
303
- logger.error("No GitHub token set, can't authenticate")
304
- return finished(false, "Authentication is required, but no GitHub token was set. Can't clone!")
220
+ logger.error("No GitHub token set, can't authenticate");
221
+ return finished(false, "Authentication is required, but no GitHub token was set. Can't clone!");
305
222
  }
306
-
307
223
  isPrivate = true;
308
224
  data.authkey = this.ghToken;
309
225
  }
310
-
311
226
  socket.emit("git-clone", data);
312
- }
313
-
227
+ };
314
228
  socket.once("git-clone", (data) => {
315
229
  logger.log(`Cloning ${data}`);
316
230
  });
317
-
318
231
  socket.once("git-success", () => {
319
232
  logger.log("Project successfully cloned");
320
233
  finished(true);
321
234
  });
322
-
323
235
  socket.on("git-error", (message) => {
324
236
  if (message === "Remote authentication required but no callback set") {
325
237
  logger.log(`Remote authentication required, trying again with authkey: ${dir}`);
326
238
  sendRequest(true);
327
- } else {
239
+ }
240
+ else {
328
241
  logger.log("Error cloning repo:", url, dir, branch, message);
329
242
  finished(false, message);
330
243
  }
331
244
  });
332
-
333
245
  sendRequest();
334
246
  });
335
247
  });
336
248
  }
337
-
338
-
339
249
  /**
340
250
  * Sets up the console listener worker
341
251
  *
@@ -346,29 +256,25 @@ export class WispSocket {
346
256
  this.pool.run((worker) => {
347
257
  const logger = worker.logger;
348
258
  logger.log("Running setupConsoleListener");
349
-
350
- return new Promise<void>((resolve) => {
351
- worker.socket.on("console", (data: ConsoleMessage) => {
259
+ return new Promise((resolve) => {
260
+ worker.socket.on("console", (data) => {
352
261
  const line = data.line;
353
-
354
262
  if (this.consoleCallbacks.length == 0) {
355
263
  return resolve();
356
264
  }
357
-
358
265
  this.consoleCallbacks.forEach((callback) => {
359
266
  try {
360
267
  callback(line);
361
- } catch(e) {
268
+ }
269
+ catch (e) {
362
270
  logger.error("Failed to run console callback", e);
363
271
  }
364
272
  });
365
273
  });
366
274
  });
367
275
  });
368
- })
276
+ });
369
277
  }
370
-
371
-
372
278
  /**
373
279
  * Adds a new callback that will run any time a console message is rececived
374
280
  *
@@ -376,15 +282,12 @@ export class WispSocket {
376
282
  *
377
283
  * @public
378
284
  */
379
- addConsoleListener(callback: (message: string) => void) {
285
+ addConsoleListener(callback) {
380
286
  if (this.consoleCallbacks.length == 0) {
381
287
  this.setupConsoleListener();
382
288
  }
383
-
384
289
  this.consoleCallbacks.push(callback);
385
290
  }
386
-
387
-
388
291
  /**
389
292
  * Removes a previously added console message callback
390
293
  *
@@ -392,14 +295,13 @@ export class WispSocket {
392
295
  *
393
296
  * @public
394
297
  */
395
- removeConsoleListener(callback: (message: string) => void) {
396
- const index = this.consoleCallbacks.indexOf(callback)
397
- if (index == -1) { return }
398
-
399
- this.consoleCallbacks.splice(index, 1)
298
+ removeConsoleListener(callback) {
299
+ const index = this.consoleCallbacks.indexOf(callback);
300
+ if (index == -1) {
301
+ return;
302
+ }
303
+ this.consoleCallbacks.splice(index, 1);
400
304
  }
401
-
402
-
403
305
  /**
404
306
  * Sends a command to the server and then waits until output with the given prefix is seen in a console message
405
307
  *
@@ -434,46 +336,38 @@ export class WispSocket {
434
336
  *
435
337
  * @public
436
338
  */
437
- async sendCommandNonce(nonce: string, command: string, timeout: number = 1000) {
438
- await this.verifyPool()
439
-
339
+ async sendCommandNonce(nonce, command, timeout = 1000) {
340
+ await this.verifyPool();
440
341
  return await this.pool.run((worker) => {
441
- const socket = worker.socket
442
- const logger = worker.logger
443
- logger.log("Running sendCommandNonce: ", nonce, command)
444
-
445
- return new Promise<string>((resolve: Function, reject: Function) => {
446
- let timeoutObj: NodeJS.Timeout
447
- let callback: (data: ConsoleMessage) => void;
448
-
449
- let output = ""
450
-
451
- callback = (data: ConsoleMessage) => {
452
- const line = data.line
453
- if (line.startsWith(nonce)) {
454
- const message = line.slice(nonce.length)
455
-
456
- if (message === "Done.") {
457
- socket.off("console", callback)
458
- clearTimeout(timeoutObj)
459
-
460
- resolve(output)
461
- } else {
462
- output += message
463
- timeoutObj.refresh()
464
- }
465
- }
466
- }
467
-
468
- socket.on("console", callback)
469
- socket.emit("send command", command)
470
-
471
- timeoutObj = setTimeout(() => {
342
+ const socket = worker.socket;
343
+ const logger = worker.logger;
344
+ logger.log("Running sendCommandNonce: ", nonce, command);
345
+ return new Promise((resolve, reject) => {
346
+ let output = "";
347
+ let callback;
348
+ const timeoutObj = setTimeout(() => {
472
349
  logger.error(`Command timed out current output: '${output}'`);
473
350
  socket.off("console", callback);
474
- logger.log("Rejected sendCommandNonce 'Timeout'", nonce, command)
351
+ logger.log("Rejected sendCommandNonce 'Timeout'", nonce, command);
475
352
  reject("Timeout");
476
353
  }, timeout);
354
+ callback = (data) => {
355
+ const line = data.line;
356
+ if (line.startsWith(nonce)) {
357
+ const message = line.slice(nonce.length);
358
+ if (message === "Done.") {
359
+ socket.off("console", callback);
360
+ clearTimeout(timeoutObj);
361
+ resolve(output);
362
+ }
363
+ else {
364
+ output += message;
365
+ timeoutObj.refresh();
366
+ }
367
+ }
368
+ };
369
+ socket.on("console", callback);
370
+ socket.emit("send command", command);
477
371
  });
478
372
  });
479
373
  }