wispjs 2.1.4 → 2.3.2
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.
|
@@ -79,28 +79,31 @@ export declare class WispSocket {
|
|
|
79
79
|
* Searches all file contents for the given query
|
|
80
80
|
*
|
|
81
81
|
* @param query The query string to search for
|
|
82
|
+
* @param timeout How long to wait (in ms) for results before timing out
|
|
82
83
|
*
|
|
83
84
|
* @public
|
|
84
85
|
*/
|
|
85
|
-
filesearch(query: string): Promise<FilesearchResults>;
|
|
86
|
+
filesearch(query: string, timeout?: number): Promise<FilesearchResults>;
|
|
86
87
|
/**
|
|
87
88
|
* Performs a git pull operation on the given directory
|
|
88
89
|
*
|
|
89
90
|
* @param dir The full directory path to perform a pull on
|
|
91
|
+
* @param timeout In milliseconds, how long to wait before timing out
|
|
90
92
|
*
|
|
91
93
|
* @public
|
|
92
94
|
*/
|
|
93
|
-
gitPull(dir: string, useAuth?: boolean): Promise<any>;
|
|
95
|
+
gitPull(dir: string, useAuth?: boolean, timeout?: number): Promise<any>;
|
|
94
96
|
/**
|
|
95
97
|
* Clones a new Repo to the given directory
|
|
96
98
|
*
|
|
97
99
|
* @param url The HTTPS URL of the repository
|
|
98
100
|
* @param dir The full path of the directory to clone the repository to
|
|
99
101
|
* @param branch The branch of the repository to clone
|
|
102
|
+
* @param timeout In milliseconds, how long to wait before timing out
|
|
100
103
|
*
|
|
101
104
|
* @public
|
|
102
105
|
*/
|
|
103
|
-
gitClone(url: string, dir: string, branch: string): Promise<any>;
|
|
106
|
+
gitClone(url: string, dir: string, branch: string, timeout?: number): Promise<any>;
|
|
104
107
|
/**
|
|
105
108
|
* Sets up the console listener worker
|
|
106
109
|
*
|
|
@@ -153,7 +156,7 @@ export declare class WispSocket {
|
|
|
153
156
|
*
|
|
154
157
|
* @param nonce The short, unique string that your output will be prefixed with
|
|
155
158
|
* @param command The full command string to send
|
|
156
|
-
* @param timeout
|
|
159
|
+
* @param timeout In milliseconds, how long to wait for output before timing out
|
|
157
160
|
*
|
|
158
161
|
* @public
|
|
159
162
|
*/
|
|
@@ -89,10 +89,11 @@ export class WispSocket {
|
|
|
89
89
|
* Searches all file contents for the given query
|
|
90
90
|
*
|
|
91
91
|
* @param query The query string to search for
|
|
92
|
+
* @param timeout How long to wait (in ms) for results before timing out
|
|
92
93
|
*
|
|
93
94
|
* @public
|
|
94
95
|
*/
|
|
95
|
-
async filesearch(query) {
|
|
96
|
+
async filesearch(query, timeout = 10000) {
|
|
96
97
|
this.logger.info("Running filesearch with: ", query);
|
|
97
98
|
await this.verifyPool();
|
|
98
99
|
return await this.pool.run((worker) => {
|
|
@@ -100,13 +101,13 @@ export class WispSocket {
|
|
|
100
101
|
const logger = worker.logger;
|
|
101
102
|
logger.log("Running filesearch:", query);
|
|
102
103
|
return new Promise((resolve, reject) => {
|
|
103
|
-
const
|
|
104
|
+
const timeoutObj = setTimeout(() => {
|
|
104
105
|
socket.off("filesearch-results");
|
|
105
106
|
logger.error("Rejected filesearch: 'Timeout'");
|
|
106
107
|
reject();
|
|
107
|
-
},
|
|
108
|
+
}, timeout);
|
|
108
109
|
socket.once("filesearch-results", (data) => {
|
|
109
|
-
clearTimeout(
|
|
110
|
+
clearTimeout(timeoutObj);
|
|
110
111
|
resolve(data);
|
|
111
112
|
});
|
|
112
113
|
socket.emit("filesearch-start", query);
|
|
@@ -117,10 +118,11 @@ export class WispSocket {
|
|
|
117
118
|
* Performs a git pull operation on the given directory
|
|
118
119
|
*
|
|
119
120
|
* @param dir The full directory path to perform a pull on
|
|
121
|
+
* @param timeout In milliseconds, how long to wait before timing out
|
|
120
122
|
*
|
|
121
123
|
* @public
|
|
122
124
|
*/
|
|
123
|
-
async gitPull(dir, useAuth = false) {
|
|
125
|
+
async gitPull(dir, useAuth = false, timeout = 10000) {
|
|
124
126
|
await this.verifyPool();
|
|
125
127
|
const pullResult = await this.pool.run((worker) => {
|
|
126
128
|
const socket = worker.socket;
|
|
@@ -128,7 +130,12 @@ export class WispSocket {
|
|
|
128
130
|
logger.log("Running gitPull:", dir);
|
|
129
131
|
return new Promise((resolve, reject) => {
|
|
130
132
|
let isPrivate = false;
|
|
131
|
-
|
|
133
|
+
let finished;
|
|
134
|
+
const timeoutObj = setTimeout(() => {
|
|
135
|
+
logger.error("Rejected gitPull: 'Timeout'");
|
|
136
|
+
finished(false, "Timeout");
|
|
137
|
+
}, timeout);
|
|
138
|
+
finished = (success, output) => {
|
|
132
139
|
socket.removeAllListeners("git-pull");
|
|
133
140
|
socket.removeAllListeners("git-error");
|
|
134
141
|
socket.removeAllListeners("git-success");
|
|
@@ -143,6 +150,7 @@ export class WispSocket {
|
|
|
143
150
|
logger.error("Rejected gitPull:", dir, output);
|
|
144
151
|
reject(output);
|
|
145
152
|
}
|
|
153
|
+
clearTimeout(timeoutObj);
|
|
146
154
|
};
|
|
147
155
|
const sendRequest = (includeAuth = false) => {
|
|
148
156
|
const data = { dir: dir };
|
|
@@ -187,10 +195,11 @@ export class WispSocket {
|
|
|
187
195
|
* @param url The HTTPS URL of the repository
|
|
188
196
|
* @param dir The full path of the directory to clone the repository to
|
|
189
197
|
* @param branch The branch of the repository to clone
|
|
198
|
+
* @param timeout In milliseconds, how long to wait before timing out
|
|
190
199
|
*
|
|
191
200
|
* @public
|
|
192
201
|
*/
|
|
193
|
-
async gitClone(url, dir, branch) {
|
|
202
|
+
async gitClone(url, dir, branch, timeout = 20000) {
|
|
194
203
|
await this.verifyPool();
|
|
195
204
|
return await this.pool.run((worker) => {
|
|
196
205
|
const socket = worker.socket;
|
|
@@ -198,7 +207,12 @@ export class WispSocket {
|
|
|
198
207
|
logger.log("Running gitClone:", url, dir, branch);
|
|
199
208
|
return new Promise((resolve, reject) => {
|
|
200
209
|
let isPrivate = false;
|
|
201
|
-
|
|
210
|
+
let finished;
|
|
211
|
+
const timeoutObj = setTimeout(() => {
|
|
212
|
+
logger.error("Rejected gitClone: 'Timeout'");
|
|
213
|
+
finished(false, "Timeout");
|
|
214
|
+
}, timeout);
|
|
215
|
+
finished = (success, message) => {
|
|
202
216
|
socket.removeAllListeners("git-clone");
|
|
203
217
|
socket.removeAllListeners("git-error");
|
|
204
218
|
socket.removeAllListeners("git-success");
|
|
@@ -212,6 +226,7 @@ export class WispSocket {
|
|
|
212
226
|
logger.error("Rejected gitClone:", url, dir, branch, message);
|
|
213
227
|
reject(message);
|
|
214
228
|
}
|
|
229
|
+
clearTimeout(timeoutObj);
|
|
215
230
|
};
|
|
216
231
|
const sendRequest = (includeAuth = false) => {
|
|
217
232
|
const data = { dir: dir, url: url, branch: branch };
|
|
@@ -332,7 +347,7 @@ export class WispSocket {
|
|
|
332
347
|
*
|
|
333
348
|
* @param nonce The short, unique string that your output will be prefixed with
|
|
334
349
|
* @param command The full command string to send
|
|
335
|
-
* @param timeout
|
|
350
|
+
* @param timeout In milliseconds, how long to wait for output before timing out
|
|
336
351
|
*
|
|
337
352
|
* @public
|
|
338
353
|
*/
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { Socket } from "socket.io-client";
|
|
2
|
+
type Logger = {
|
|
3
|
+
log(...args: any[]): void;
|
|
4
|
+
error(...args: any[]): void;
|
|
5
|
+
debug(...args: any[]): void;
|
|
6
|
+
};
|
|
2
7
|
/**
|
|
3
8
|
* The struct used to define the events that can be sent from the server to the client
|
|
4
9
|
*
|
|
@@ -133,10 +138,8 @@ interface PoolWorker {
|
|
|
133
138
|
idx: number;
|
|
134
139
|
token: string;
|
|
135
140
|
ready: boolean;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
error(...args: any[]): void;
|
|
139
|
-
};
|
|
141
|
+
done: boolean;
|
|
142
|
+
logger: Logger;
|
|
140
143
|
}
|
|
141
144
|
/**
|
|
142
145
|
* A single Worker within a {@link WebsocketPool}
|
|
@@ -147,10 +150,9 @@ interface PoolWorker {
|
|
|
147
150
|
*/
|
|
148
151
|
declare class PoolWorker {
|
|
149
152
|
constructor(pool: WebsocketPool);
|
|
150
|
-
available(): boolean;
|
|
151
153
|
connect(): Promise<void>;
|
|
152
154
|
disconnect(): Promise<void>;
|
|
153
|
-
|
|
155
|
+
private processWork;
|
|
154
156
|
}
|
|
155
157
|
/**
|
|
156
158
|
* Struct used to manage a pool of WebSocket workers
|
|
@@ -160,6 +162,7 @@ export interface WebsocketPool {
|
|
|
160
162
|
token: string;
|
|
161
163
|
url: string;
|
|
162
164
|
maxWorkers: number;
|
|
165
|
+
logger: Logger;
|
|
163
166
|
queue: ((worker: PoolWorker) => Promise<any>)[];
|
|
164
167
|
}
|
|
165
168
|
/**
|
|
@@ -175,9 +178,8 @@ export interface WebsocketPool {
|
|
|
175
178
|
*/
|
|
176
179
|
export declare class WebsocketPool {
|
|
177
180
|
constructor(url: string, token: string);
|
|
178
|
-
|
|
181
|
+
getWork(): ((worker: PoolWorker) => Promise<any>) | undefined;
|
|
179
182
|
disconnect(): Promise<void>;
|
|
180
|
-
processQueue(): Promise<any>;
|
|
181
183
|
run(work: (worker: PoolWorker) => Promise<any>): Promise<any>;
|
|
182
184
|
}
|
|
183
185
|
export {};
|
package/dist/wisp_socket/pool.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { io } from "socket.io-client";
|
|
2
|
+
const WISP_DEBUG = process.env.WISP_DEBUG === "true";
|
|
2
3
|
/**
|
|
3
4
|
* A single Worker within a {@link WebsocketPool}
|
|
4
5
|
*
|
|
@@ -10,6 +11,7 @@ class PoolWorker {
|
|
|
10
11
|
constructor(pool) {
|
|
11
12
|
this.pool = pool;
|
|
12
13
|
this.ready = false;
|
|
14
|
+
this.done = false;
|
|
13
15
|
this.idx = pool.workers.length;
|
|
14
16
|
this.token = pool.token;
|
|
15
17
|
this.socket = io(pool.url, {
|
|
@@ -22,10 +24,13 @@ class PoolWorker {
|
|
|
22
24
|
this.logger = {
|
|
23
25
|
log: (...args) => console.log(logPrefix, args),
|
|
24
26
|
error: (...args) => console.error(logPrefix, args),
|
|
27
|
+
debug: (...args) => {
|
|
28
|
+
if (!WISP_DEBUG)
|
|
29
|
+
return;
|
|
30
|
+
console.debug(logPrefix, args);
|
|
31
|
+
}
|
|
25
32
|
};
|
|
26
|
-
|
|
27
|
-
available() {
|
|
28
|
-
return this.ready && this.socket.connected;
|
|
33
|
+
this.connect().then(() => this.processWork()).catch(err => this.logger.error(err));
|
|
29
34
|
}
|
|
30
35
|
connect() {
|
|
31
36
|
const socket = this.socket;
|
|
@@ -33,12 +38,9 @@ class PoolWorker {
|
|
|
33
38
|
socket.onAnyOutgoing(this.logger.log);
|
|
34
39
|
logger.log("Connecting to websocket...");
|
|
35
40
|
return new Promise((resolve, reject) => {
|
|
36
|
-
let connectedOnce = false;
|
|
37
41
|
const timeout = setTimeout(() => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
reject("Connection Timeout");
|
|
41
|
-
}
|
|
42
|
+
logger.error("Socket didn't connect in time");
|
|
43
|
+
reject("Connection Timeout");
|
|
42
44
|
}, 10000);
|
|
43
45
|
socket.on("connect", () => {
|
|
44
46
|
logger.log("Connected to WebSocket");
|
|
@@ -50,23 +52,18 @@ class PoolWorker {
|
|
|
50
52
|
});
|
|
51
53
|
socket.on("connect_error", (error) => {
|
|
52
54
|
logger.error(`WebSocket Connect error: ${error.toString()}`);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
reject(`Connection error: ${error.toString()}`);
|
|
57
|
-
}
|
|
55
|
+
this.done = true;
|
|
56
|
+
clearTimeout(timeout);
|
|
57
|
+
reject(`Connection error: ${error.toString()}`);
|
|
58
58
|
});
|
|
59
59
|
socket.on("disconnect", (reason) => {
|
|
60
60
|
logger.log(`Disconnected from WebSocket: ${reason}`);
|
|
61
61
|
});
|
|
62
62
|
socket.on("auth_success", () => {
|
|
63
63
|
logger.log("Auth success");
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
clearTimeout(timeout);
|
|
68
|
-
resolve();
|
|
69
|
-
}
|
|
64
|
+
this.ready = true;
|
|
65
|
+
clearTimeout(timeout);
|
|
66
|
+
resolve();
|
|
70
67
|
});
|
|
71
68
|
socket.connect();
|
|
72
69
|
});
|
|
@@ -79,25 +76,38 @@ class PoolWorker {
|
|
|
79
76
|
reject();
|
|
80
77
|
}, 5000);
|
|
81
78
|
this.socket.once("disconnect", () => {
|
|
79
|
+
this.done = true;
|
|
82
80
|
clearTimeout(timeout);
|
|
83
81
|
resolve();
|
|
84
82
|
});
|
|
85
83
|
this.socket.disconnect();
|
|
86
84
|
});
|
|
87
85
|
}
|
|
88
|
-
async
|
|
89
|
-
this.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
86
|
+
async processWork() {
|
|
87
|
+
while (!this.done) {
|
|
88
|
+
if (!this.ready) {
|
|
89
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const work = this.pool.getWork();
|
|
93
|
+
if (work) {
|
|
94
|
+
this.ready = false;
|
|
95
|
+
try {
|
|
96
|
+
this.logger.debug("Running my work");
|
|
97
|
+
await work(this);
|
|
98
|
+
this.logger.debug("Done with my work, ready for more");
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
this.logger.error("Failed to run work");
|
|
102
|
+
this.logger.error(e);
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
this.ready = true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
110
|
+
}
|
|
101
111
|
}
|
|
102
112
|
}
|
|
103
113
|
}
|
|
@@ -114,44 +124,34 @@ class PoolWorker {
|
|
|
114
124
|
*/
|
|
115
125
|
export class WebsocketPool {
|
|
116
126
|
constructor(url, token) {
|
|
117
|
-
|
|
127
|
+
const envMaxWorkers = process.env.WISP_MAX_WORKERS;
|
|
128
|
+
this.maxWorkers = envMaxWorkers ? parseInt(envMaxWorkers) : 5;
|
|
118
129
|
this.token = token;
|
|
119
130
|
this.url = url;
|
|
131
|
+
const logPrefix = "[Pool]";
|
|
132
|
+
this.logger = {
|
|
133
|
+
log: (...args) => console.log(logPrefix, args),
|
|
134
|
+
error: (...args) => console.error(logPrefix, args),
|
|
135
|
+
debug: (...args) => {
|
|
136
|
+
if (!WISP_DEBUG)
|
|
137
|
+
return;
|
|
138
|
+
console.debug(logPrefix, args);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
120
141
|
this.workers = [];
|
|
121
142
|
this.queue = [];
|
|
143
|
+
this.logger.log(`Creating a new Pool with ${this.maxWorkers} workers`);
|
|
144
|
+
for (let i = 0; i < this.maxWorkers; i++) {
|
|
145
|
+
this.workers.push(new PoolWorker(this));
|
|
146
|
+
}
|
|
122
147
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const worker = new PoolWorker(this);
|
|
126
|
-
this.workers.push(worker);
|
|
127
|
-
await worker.connect();
|
|
128
|
-
return worker;
|
|
148
|
+
getWork() {
|
|
149
|
+
return this.queue.shift();
|
|
129
150
|
}
|
|
130
151
|
async disconnect() {
|
|
152
|
+
this.logger.log("Disconnecting all workers...");
|
|
131
153
|
await Promise.all(this.workers.map((worker) => worker.disconnect()));
|
|
132
|
-
|
|
133
|
-
async processQueue() {
|
|
134
|
-
if (this.queue.length == 0) {
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
const work = this.queue.shift();
|
|
138
|
-
if (!work) {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
let worker;
|
|
142
|
-
if (this.workers.length == 0) {
|
|
143
|
-
worker = await this.createWorker();
|
|
144
|
-
}
|
|
145
|
-
worker = worker || this.workers.find((worker) => worker.available());
|
|
146
|
-
if (!worker) {
|
|
147
|
-
if (this.workers.length < this.maxWorkers) {
|
|
148
|
-
worker = await this.createWorker();
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return await worker.run(work);
|
|
154
|
+
this.logger.log("All workers disconnected");
|
|
155
155
|
}
|
|
156
156
|
async run(work) {
|
|
157
157
|
return new Promise(async (resolve, reject) => {
|
|
@@ -165,7 +165,6 @@ export class WebsocketPool {
|
|
|
165
165
|
reject(e);
|
|
166
166
|
}
|
|
167
167
|
});
|
|
168
|
-
return this.processQueue();
|
|
169
168
|
});
|
|
170
169
|
}
|
|
171
170
|
}
|