fied 0.1.4 → 0.1.5
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/dist/bin.js +39 -14
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -175,49 +175,66 @@ async function createSession(relay) {
|
|
|
175
175
|
const data = await res.json();
|
|
176
176
|
return data.sessionId;
|
|
177
177
|
}
|
|
178
|
+
var WS_CONNECT_TIMEOUT_MS = 1e4;
|
|
178
179
|
var RelayBridge = class {
|
|
179
180
|
constructor(relay, key, keyFragment, pty) {
|
|
180
181
|
this.relay = relay;
|
|
181
182
|
this.key = key;
|
|
182
183
|
this.keyFragment = keyFragment;
|
|
183
184
|
this.pty = pty;
|
|
185
|
+
this.pty.onData((data) => {
|
|
186
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
187
|
+
this.sendEncrypted(MSG_TERMINAL_OUTPUT, this.encoder.encode(data));
|
|
188
|
+
}
|
|
189
|
+
});
|
|
184
190
|
}
|
|
185
191
|
ws = null;
|
|
186
192
|
destroyed = false;
|
|
187
193
|
backoff = RECONNECT_BASE_MS;
|
|
188
194
|
reconnectTimer = null;
|
|
195
|
+
connectTimeout = null;
|
|
189
196
|
encoder = new TextEncoder();
|
|
190
197
|
decoder = new TextDecoder();
|
|
198
|
+
firstConnect = true;
|
|
191
199
|
async connect() {
|
|
192
200
|
if (this.destroyed) return;
|
|
193
201
|
let sessionId;
|
|
194
202
|
try {
|
|
195
203
|
sessionId = await createSession(this.relay);
|
|
196
204
|
} catch {
|
|
197
|
-
|
|
205
|
+
if (this.firstConnect) {
|
|
206
|
+
console.error(" \x1B[31mRelay unreachable, retrying...\x1B[0m");
|
|
207
|
+
}
|
|
198
208
|
this.scheduleReconnect();
|
|
199
209
|
return;
|
|
200
210
|
}
|
|
201
211
|
const url = `${this.relay}/s/${sessionId}#${this.keyFragment}`;
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
212
|
+
if (this.firstConnect) {
|
|
213
|
+
console.log(` \x1B[1mShare this link:\x1B[0m`);
|
|
214
|
+
console.log(` \x1B[4m\x1B[36m${url}\x1B[0m`);
|
|
215
|
+
console.log("");
|
|
216
|
+
console.log(" \x1B[2mThe encryption key is in the URL fragment (#) \u2014 the server never sees it.\x1B[0m");
|
|
217
|
+
console.log(" \x1B[2mPress Ctrl+C to stop sharing.\x1B[0m");
|
|
218
|
+
console.log("");
|
|
219
|
+
} else {
|
|
220
|
+
console.error(` \x1B[32mReconnected.\x1B[0m New link: \x1B[4m\x1B[36m${url}\x1B[0m`);
|
|
221
|
+
}
|
|
222
|
+
this.firstConnect = false;
|
|
208
223
|
const wsUrl = this.relay.replace(/^http/, "ws") + `/api/sessions/${sessionId}/ws?role=host`;
|
|
209
224
|
const ws = new WebSocket(wsUrl);
|
|
210
225
|
ws.binaryType = "arraybuffer";
|
|
211
226
|
this.ws = ws;
|
|
227
|
+
this.connectTimeout = setTimeout(() => {
|
|
228
|
+
if (ws.readyState !== WebSocket.OPEN) {
|
|
229
|
+
ws.terminate();
|
|
230
|
+
}
|
|
231
|
+
}, WS_CONNECT_TIMEOUT_MS);
|
|
212
232
|
ws.on("open", () => {
|
|
213
|
-
this.
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
});
|
|
217
|
-
this.pty.onData((data) => {
|
|
218
|
-
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
219
|
-
this.sendEncrypted(MSG_TERMINAL_OUTPUT, this.encoder.encode(data));
|
|
233
|
+
if (this.connectTimeout) {
|
|
234
|
+
clearTimeout(this.connectTimeout);
|
|
235
|
+
this.connectTimeout = null;
|
|
220
236
|
}
|
|
237
|
+
this.backoff = RECONNECT_BASE_MS;
|
|
221
238
|
});
|
|
222
239
|
ws.on("message", async (raw, isBinary) => {
|
|
223
240
|
if (!isBinary) {
|
|
@@ -242,6 +259,10 @@ var RelayBridge = class {
|
|
|
242
259
|
}
|
|
243
260
|
});
|
|
244
261
|
ws.on("close", () => {
|
|
262
|
+
if (this.connectTimeout) {
|
|
263
|
+
clearTimeout(this.connectTimeout);
|
|
264
|
+
this.connectTimeout = null;
|
|
265
|
+
}
|
|
245
266
|
this.ws = null;
|
|
246
267
|
if (!this.destroyed) {
|
|
247
268
|
console.error(" \x1B[33mConnection lost, reconnecting...\x1B[0m");
|
|
@@ -257,6 +278,10 @@ var RelayBridge = class {
|
|
|
257
278
|
clearTimeout(this.reconnectTimer);
|
|
258
279
|
this.reconnectTimer = null;
|
|
259
280
|
}
|
|
281
|
+
if (this.connectTimeout) {
|
|
282
|
+
clearTimeout(this.connectTimeout);
|
|
283
|
+
this.connectTimeout = null;
|
|
284
|
+
}
|
|
260
285
|
if (this.ws) {
|
|
261
286
|
this.ws.close();
|
|
262
287
|
this.ws = null;
|