wispjs 3.1.0 → 3.2.1

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.
@@ -39,10 +39,11 @@ export declare class GitSocket {
39
39
  *
40
40
  * @param dir The full directory path to pull
41
41
  * @param timeout In milliseconds, how long to wait before timing out
42
+ * @param resetHard When true, forces the pull by discarding local changes first
42
43
  *
43
44
  * @public
44
45
  */
45
- pull(dir: string, timeout?: number): Promise<GitPullResult>;
46
+ pull(dir: string, timeout?: number, resetHard?: boolean): Promise<GitPullResult>;
46
47
  /**
47
48
  * Clones a repository into the given parent directory.
48
49
  *
@@ -50,11 +50,16 @@ export class GitSocket {
50
50
  *
51
51
  * @param dir The full directory path to pull
52
52
  * @param timeout In milliseconds, how long to wait before timing out
53
+ * @param resetHard When true, forces the pull by discarding local changes first
53
54
  *
54
55
  * @public
55
56
  */
56
- async pull(dir, timeout = 10000) {
57
- return await this.withAuthRetry("git-pull", { directory: dir }, timeout);
57
+ async pull(dir, timeout = 10000, resetHard = false) {
58
+ const params = { directory: dir };
59
+ if (resetHard) {
60
+ params.reset_hard = true;
61
+ }
62
+ return await this.withAuthRetry("git-pull", params, timeout);
58
63
  }
59
64
  /**
60
65
  * Clones a repository into the given parent directory.
@@ -267,13 +267,17 @@ declare class PoolWorker {
267
267
  private reconnectAttempts;
268
268
  private readonly maxReconnectAttempts;
269
269
  private reauthTimer?;
270
+ private reauthInFlight;
271
+ private socketGeneration;
270
272
  constructor(pool: WebsocketPool);
271
273
  private createSocket;
272
274
  private start;
273
275
  connect(): Promise<void>;
274
276
  private scheduleReauth;
275
277
  private reauth;
278
+ private refreshAndReauth;
276
279
  private handleDisconnect;
280
+ private retireSocket;
277
281
  disconnect(): Promise<void>;
278
282
  private processWork;
279
283
  }
@@ -17,6 +17,8 @@ class PoolWorker {
17
17
  this.intentionalDisconnect = false;
18
18
  this.reconnectAttempts = 0;
19
19
  this.maxReconnectAttempts = 3;
20
+ this.reauthInFlight = false;
21
+ this.socketGeneration = 0;
20
22
  this.pool = pool;
21
23
  this.connected = false;
22
24
  this.busy = false;
@@ -38,6 +40,7 @@ class PoolWorker {
38
40
  this.processWork();
39
41
  }
40
42
  createSocket() {
43
+ this.socketGeneration++;
41
44
  this.socket = new WispWebSocket(this.url, {
42
45
  origin: this.pool.origin
43
46
  });
@@ -54,6 +57,8 @@ class PoolWorker {
54
57
  connect() {
55
58
  const socket = this.socket;
56
59
  const logger = this.logger;
60
+ const generation = this.socketGeneration;
61
+ const isCurrentSocket = () => generation === this.socketGeneration;
57
62
  socket.onAnyOutgoing(this.logger.log);
58
63
  logger.log("Connecting to websocket...");
59
64
  return new Promise((resolve, reject) => {
@@ -75,9 +80,18 @@ class PoolWorker {
75
80
  });
76
81
  socket.on("disconnect", (reason) => {
77
82
  logger.log(`Disconnected from WebSocket: ${reason}`);
83
+ if (!isCurrentSocket())
84
+ return;
78
85
  this.connected = false;
79
86
  this.handleDisconnect(reason);
80
87
  });
88
+ // the daemon keeps the socket open after expiry but stops sending console output
89
+ socket.on("token expiring", () => this.reauth());
90
+ socket.on("token expired", () => this.reauth());
91
+ socket.on("jwt error", (message) => {
92
+ logger.error(`JWT error: ${message}`);
93
+ this.reauth();
94
+ });
81
95
  socket.on("auth success", () => {
82
96
  logger.log("Auth success");
83
97
  this.reconnectAttempts = 0;
@@ -97,9 +111,18 @@ class PoolWorker {
97
111
  // Re-auths the live socket with a fresh token. A failed re-auth is treated
98
112
  // as a dead connection and handed to the reconnect path.
99
113
  async reauth() {
100
- if (!this.connected || this.intentionalDisconnect || this.done) {
114
+ if (!this.connected || this.intentionalDisconnect || this.done || this.reauthInFlight) {
101
115
  return;
102
116
  }
117
+ this.reauthInFlight = true;
118
+ try {
119
+ await this.refreshAndReauth();
120
+ }
121
+ finally {
122
+ this.reauthInFlight = false;
123
+ }
124
+ }
125
+ async refreshAndReauth() {
103
126
  if (this.pool.refreshDetails) {
104
127
  try {
105
128
  const details = await this.pool.refreshDetails();
@@ -139,6 +162,7 @@ class PoolWorker {
139
162
  if (this.intentionalDisconnect || this.done) {
140
163
  return;
141
164
  }
165
+ this.retireSocket();
142
166
  if (this.reconnectAttempts >= this.maxReconnectAttempts) {
143
167
  this.logger.error(`Giving up after ${this.reconnectAttempts} reconnect attempts (${reason})`);
144
168
  this.done = true;
@@ -164,6 +188,11 @@ class PoolWorker {
164
188
  }
165
189
  this.start();
166
190
  }
191
+ // lets jobs bound to the old socket see "disconnect" and move to the replacement
192
+ retireSocket() {
193
+ this.socketGeneration++;
194
+ this.socket.terminate();
195
+ }
167
196
  disconnect() {
168
197
  this.intentionalDisconnect = true;
169
198
  this.connected = false;
@@ -69,4 +69,8 @@ export declare class WispWebSocket {
69
69
  */
70
70
  onAnyOutgoing(listener: (...args: any[]) => void): this;
71
71
  disconnect(): this;
72
+ /**
73
+ * Destroys the underlying socket immediately, without a close handshake.
74
+ */
75
+ terminate(): this;
72
76
  }
@@ -127,4 +127,11 @@ export class WispWebSocket {
127
127
  this.ws?.close();
128
128
  return this;
129
129
  }
130
+ /**
131
+ * Destroys the underlying socket immediately, without a close handshake.
132
+ */
133
+ terminate() {
134
+ this.ws?.terminate();
135
+ return this;
136
+ }
130
137
  }
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "wispjs",
3
- "version": "3.1.0",
3
+ "version": "3.2.1",
4
4
  "type": "module",
5
5
  "description": "A package for interacting with Wisp-based server panels",
6
6
  "main": "dist/wisp.js",
7
7
  "types": "dist/wisp.d.ts",
8
8
  "scripts": {
9
9
  "build": "tsc",
10
+ "prepare": "npm run build",
10
11
  "build-docs": "typedoc wisp.ts wisp_api/index.ts wisp_socket/index.ts wisp_socket/git.ts wisp_socket/filesystem.ts wisp_api/apis/*"
11
12
  },
12
13
  "repository": {