@persistica/flux-agent 0.0.24 → 0.0.25
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/index.js +23 -26
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -87,7 +87,7 @@ var validateNetworkIdOrThrow = (networkId) => {
|
|
|
87
87
|
// libs/flux/shared/types/src/lib/client-id.type.ts
|
|
88
88
|
var NANOID_LENGTH = 21;
|
|
89
89
|
var NANOID_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-";
|
|
90
|
-
var
|
|
90
|
+
var isClientId = (id) => {
|
|
91
91
|
if (typeof id !== "string") {
|
|
92
92
|
return false;
|
|
93
93
|
}
|
|
@@ -198,7 +198,7 @@ var WebSocketClient = class extends RPCServer {
|
|
|
198
198
|
);
|
|
199
199
|
setTimeout(() => {
|
|
200
200
|
this.reconnectAttempts++;
|
|
201
|
-
this.connect();
|
|
201
|
+
void this.connect();
|
|
202
202
|
}, delay);
|
|
203
203
|
} else if (this.options.autoReconnect) {
|
|
204
204
|
this.emit("error", new Error("Connection failed: retries exhausted"));
|
|
@@ -402,9 +402,7 @@ var FluxAgentNetworkConnection = class {
|
|
|
402
402
|
this._fluxWebSocketClientConnection,
|
|
403
403
|
this._webRTCStateChange
|
|
404
404
|
);
|
|
405
|
-
console.log(
|
|
406
|
-
this.iceConnection ? `RTCPeerConnection is available` : `RTCPeerConnection is not available`
|
|
407
|
-
);
|
|
405
|
+
console.log(`RTCPeerConnection is available`);
|
|
408
406
|
} else {
|
|
409
407
|
console.warn("\u2757WebRTC is not available in this environment.");
|
|
410
408
|
}
|
|
@@ -442,7 +440,7 @@ var FluxAgentNetworkConnection = class {
|
|
|
442
440
|
* Connects to a remote agent.
|
|
443
441
|
*/
|
|
444
442
|
connectToAgent(clientId) {
|
|
445
|
-
this._fluxWebSocketConnection.connectToAgent(clientId);
|
|
443
|
+
void this._fluxWebSocketConnection.connectToAgent(clientId);
|
|
446
444
|
return new FluxRemoteClient(this.iceConnection);
|
|
447
445
|
}
|
|
448
446
|
/**
|
|
@@ -458,25 +456,25 @@ var hasOverlap = (a, b) => a && b ? [...a].some((item) => (Array.isArray(b) ? b
|
|
|
458
456
|
var PicoLogger = /* @__PURE__ */ (() => {
|
|
459
457
|
let allowScopes;
|
|
460
458
|
const configure = (configuration) => {
|
|
461
|
-
allowScopes =
|
|
459
|
+
allowScopes = configuration.allowScopes === "*" ? "*" : new Set(configuration.allowScopes);
|
|
462
460
|
};
|
|
463
461
|
const log = (message, scopes) => {
|
|
464
462
|
if (allowScopes !== "*" && !hasOverlap(allowScopes, scopes)) {
|
|
465
463
|
return;
|
|
466
464
|
}
|
|
467
|
-
console.log(`[${scopes}] ${message}`);
|
|
465
|
+
console.log(`[${scopes?.toString()}] ${message}`);
|
|
468
466
|
};
|
|
469
467
|
const warn = (message, scopes) => {
|
|
470
468
|
if (allowScopes !== "*" && !hasOverlap(allowScopes, scopes)) {
|
|
471
469
|
return;
|
|
472
470
|
}
|
|
473
|
-
console.warn(`[${scopes}] ${message}`);
|
|
471
|
+
console.warn(`[${scopes?.toString()}] ${message}`);
|
|
474
472
|
};
|
|
475
473
|
const error = (message, scopes) => {
|
|
476
474
|
if (allowScopes !== "*" && !hasOverlap(allowScopes, scopes)) {
|
|
477
475
|
return;
|
|
478
476
|
}
|
|
479
|
-
console.error(`[${scopes}] ${message}`);
|
|
477
|
+
console.error(`[${scopes?.toString()}] ${message}`);
|
|
480
478
|
};
|
|
481
479
|
return {
|
|
482
480
|
configure,
|
|
@@ -579,8 +577,8 @@ var createWSConnection = (id, ticket, stateManager, onReconnectCallback, options
|
|
|
579
577
|
ticket,
|
|
580
578
|
{
|
|
581
579
|
domain: options.domain,
|
|
582
|
-
secretKey: options
|
|
583
|
-
retries: options
|
|
580
|
+
secretKey: options.secretKey,
|
|
581
|
+
retries: options.retries
|
|
584
582
|
}
|
|
585
583
|
);
|
|
586
584
|
};
|
|
@@ -593,8 +591,8 @@ var FluxWebSocketConnection = class {
|
|
|
593
591
|
this.options = options;
|
|
594
592
|
this.options = {
|
|
595
593
|
domain: this.options.domain,
|
|
596
|
-
secretKey: this.options
|
|
597
|
-
retries: this.options
|
|
594
|
+
secretKey: this.options.secretKey,
|
|
595
|
+
retries: this.options.retries ?? 1e4
|
|
598
596
|
};
|
|
599
597
|
const url = new URL(this.options.domain);
|
|
600
598
|
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
@@ -788,7 +786,7 @@ var FluxWebSocketConnection = class {
|
|
|
788
786
|
* Disconnects an agent from the network.
|
|
789
787
|
*/
|
|
790
788
|
disconnectAgent(id) {
|
|
791
|
-
if (!
|
|
789
|
+
if (!isClientId(id)) {
|
|
792
790
|
throw new Error(`Invalid agent id: '${id}'`);
|
|
793
791
|
}
|
|
794
792
|
if (this.webSocketClient) {
|
|
@@ -819,7 +817,7 @@ var FluxWebSocketConnection = class {
|
|
|
819
817
|
handleSocketConnecting(retryAttempt) {
|
|
820
818
|
this.stateManager.emitNetworkState("disconnected");
|
|
821
819
|
if (retryAttempt > 0) {
|
|
822
|
-
PicoLogger.log(`\u{1F504} Connecting attempt: #${retryAttempt} of ${this.options
|
|
820
|
+
PicoLogger.log(`\u{1F504} Connecting attempt: #${retryAttempt} of ${this.options.retries ?? "none"}`, this.fluxInstanceId);
|
|
823
821
|
} else {
|
|
824
822
|
PicoLogger.log(`\u{1F504} Connecting: ${this.fluxInstanceId}`);
|
|
825
823
|
}
|
|
@@ -865,26 +863,26 @@ var FluxWebSocketConnection = class {
|
|
|
865
863
|
}
|
|
866
864
|
}
|
|
867
865
|
}
|
|
868
|
-
|
|
866
|
+
return;
|
|
869
867
|
}
|
|
870
868
|
case RPC_REQUEST: {
|
|
871
869
|
const payload = JSON.parse(message_.substring(message_.indexOf(":") + 1));
|
|
872
|
-
this.socket.handleMessage(payload, (data) => {
|
|
870
|
+
void this.socket.handleMessage(payload, (data) => {
|
|
873
871
|
this.socket.send(`${RPC_RESPONSE}:${JSON.stringify(data)}`);
|
|
874
872
|
});
|
|
875
|
-
|
|
873
|
+
return;
|
|
876
874
|
}
|
|
877
875
|
case RPC_RESPONSE: {
|
|
878
876
|
const payload = message_.substring(message_.indexOf(":") + 1);
|
|
879
877
|
PicoLogger.log("\u{1F50C} Unhandled type rpc response", "ws-client");
|
|
880
878
|
PicoLogger.log(`payload: ${payload}`, "ws-client");
|
|
881
|
-
|
|
879
|
+
return;
|
|
882
880
|
}
|
|
883
881
|
default:
|
|
884
882
|
if (message_.startsWith(`${ERROR}:`)) {
|
|
885
883
|
}
|
|
886
884
|
PicoLogger.log(`\u{1F50C} Unhandled type: "${message_}"`, "ws-client");
|
|
887
|
-
|
|
885
|
+
return;
|
|
888
886
|
}
|
|
889
887
|
}
|
|
890
888
|
};
|
|
@@ -1021,9 +1019,8 @@ var StateManager = class {
|
|
|
1021
1019
|
|
|
1022
1020
|
// libs/flux/shared/utils/src/lib/promises.utils.ts
|
|
1023
1021
|
var retryOrThrow = async (fn, shouldRetry, options) => {
|
|
1024
|
-
let attempt = 0;
|
|
1025
1022
|
let overrideDelay;
|
|
1026
|
-
|
|
1023
|
+
for (let attempt = 0; attempt <= options.retries; attempt++) {
|
|
1027
1024
|
try {
|
|
1028
1025
|
if (attempt > 0) {
|
|
1029
1026
|
if (options.onRetry) {
|
|
@@ -1032,12 +1029,11 @@ var retryOrThrow = async (fn, shouldRetry, options) => {
|
|
|
1032
1029
|
}
|
|
1033
1030
|
return await fn();
|
|
1034
1031
|
} catch (err) {
|
|
1035
|
-
attempt
|
|
1036
|
-
if (attempt > options.retries || !shouldRetry(err)) {
|
|
1032
|
+
if (attempt >= options.retries || !shouldRetry(err)) {
|
|
1037
1033
|
throw err;
|
|
1038
1034
|
}
|
|
1039
1035
|
if (options.delayMs > 0 || overrideDelay && overrideDelay > 0) {
|
|
1040
|
-
|
|
1036
|
+
const delay = overrideDelay ?? options.delayMs;
|
|
1041
1037
|
await new Promise((res) => setTimeout(
|
|
1042
1038
|
res,
|
|
1043
1039
|
delay
|
|
@@ -1045,6 +1041,7 @@ var retryOrThrow = async (fn, shouldRetry, options) => {
|
|
|
1045
1041
|
}
|
|
1046
1042
|
}
|
|
1047
1043
|
}
|
|
1044
|
+
throw new Error("Retry loop exited unexpectedly.");
|
|
1048
1045
|
};
|
|
1049
1046
|
|
|
1050
1047
|
// libs/flux/shared/utils/src/lib/machine-id.util.ts
|