@rivetkit/engine-runner 25.7.1-rc.1 → 25.7.2-rc.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.
- package/.turbo/turbo-build.log +10 -10
- package/LICENSE +203 -0
- package/dist/mod.cjs +68 -289
- package/dist/mod.cjs.map +1 -1
- package/dist/mod.d.cts +3 -0
- package/dist/mod.d.ts +3 -0
- package/dist/mod.js +67 -288
- package/dist/mod.js.map +1 -1
- package/package.json +36 -37
- package/src/mod.ts +42 -65
- package/src/tunnel.ts +53 -298
package/dist/mod.cjs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }
|
|
2
|
-
var _ws = require('ws'); var _ws2 = _interopRequireDefault(_ws);
|
|
3
|
-
|
|
4
|
-
// src/log.ts
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } var _class;// src/log.ts
|
|
5
2
|
var LOGGER = void 0;
|
|
6
3
|
function setLogger(logger2) {
|
|
7
4
|
LOGGER = logger2;
|
|
@@ -64,10 +61,6 @@ function calculateBackoff(attempt, options = {}) {
|
|
|
64
61
|
return Math.floor(delay);
|
|
65
62
|
}
|
|
66
63
|
|
|
67
|
-
// src/tunnel.ts
|
|
68
|
-
|
|
69
|
-
var _enginetunnelprotocol = require('@rivetkit/engine-tunnel-protocol'); var tunnel = _interopRequireWildcard(_enginetunnelprotocol);
|
|
70
|
-
|
|
71
64
|
// src/websocket-tunnel-adapter.ts
|
|
72
65
|
var WebSocketTunnelAdapter = (_class = class {
|
|
73
66
|
#webSocketId;
|
|
@@ -455,44 +448,22 @@ var _uuid = require('uuid');
|
|
|
455
448
|
var GC_INTERVAL = 6e4;
|
|
456
449
|
var MESSAGE_ACK_TIMEOUT = 5e3;
|
|
457
450
|
var Tunnel = class {
|
|
458
|
-
#pegboardTunnelUrl;
|
|
459
451
|
#runner;
|
|
460
|
-
#tunnelWs;
|
|
461
|
-
#shutdown = false;
|
|
462
|
-
#reconnectTimeout;
|
|
463
|
-
#reconnectAttempt = 0;
|
|
464
452
|
#actorPendingRequests = /* @__PURE__ */ new Map();
|
|
465
453
|
#actorWebSockets = /* @__PURE__ */ new Map();
|
|
466
454
|
#pendingMessages = /* @__PURE__ */ new Map();
|
|
467
455
|
#gcInterval;
|
|
468
|
-
|
|
469
|
-
constructor(runner, pegboardTunnelUrl, callbacks) {
|
|
470
|
-
this.#pegboardTunnelUrl = pegboardTunnelUrl;
|
|
456
|
+
constructor(runner) {
|
|
471
457
|
this.#runner = runner;
|
|
472
|
-
this.#callbacks = callbacks;
|
|
473
458
|
}
|
|
474
459
|
start() {
|
|
475
|
-
var _a;
|
|
476
|
-
if (((_a = this.#tunnelWs) == null ? void 0 : _a.readyState) === _ws2.default.OPEN) {
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
this.#connect();
|
|
480
460
|
this.#startGarbageCollector();
|
|
481
461
|
}
|
|
482
462
|
shutdown() {
|
|
483
|
-
this.#shutdown = true;
|
|
484
|
-
if (this.#reconnectTimeout) {
|
|
485
|
-
clearTimeout(this.#reconnectTimeout);
|
|
486
|
-
this.#reconnectTimeout = void 0;
|
|
487
|
-
}
|
|
488
463
|
if (this.#gcInterval) {
|
|
489
464
|
clearInterval(this.#gcInterval);
|
|
490
465
|
this.#gcInterval = void 0;
|
|
491
466
|
}
|
|
492
|
-
if (this.#tunnelWs) {
|
|
493
|
-
this.#tunnelWs.close();
|
|
494
|
-
this.#tunnelWs = void 0;
|
|
495
|
-
}
|
|
496
467
|
for (const [_, request] of this.#actorPendingRequests) {
|
|
497
468
|
request.reject(new Error("Tunnel shutting down"));
|
|
498
469
|
}
|
|
@@ -503,7 +474,7 @@ var Tunnel = class {
|
|
|
503
474
|
this.#actorWebSockets.clear();
|
|
504
475
|
}
|
|
505
476
|
#sendMessage(requestId, messageKind) {
|
|
506
|
-
if (!this.#
|
|
477
|
+
if (!this.#runner.__webSocketReady()) {
|
|
507
478
|
console.warn("Cannot send tunnel message, WebSocket not connected");
|
|
508
479
|
return;
|
|
509
480
|
}
|
|
@@ -514,24 +485,28 @@ var Tunnel = class {
|
|
|
514
485
|
requestIdStr
|
|
515
486
|
});
|
|
516
487
|
const message = {
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
488
|
+
tag: "ToServerTunnelMessage",
|
|
489
|
+
val: {
|
|
490
|
+
requestId,
|
|
491
|
+
messageId,
|
|
492
|
+
messageKind
|
|
493
|
+
}
|
|
520
494
|
};
|
|
521
|
-
|
|
522
|
-
this.#tunnelWs.send(encoded);
|
|
495
|
+
this.#runner.__sendToServer(message);
|
|
523
496
|
}
|
|
524
497
|
#sendAck(requestId, messageId) {
|
|
525
|
-
if (!this.#
|
|
498
|
+
if (!this.#runner.__webSocketReady()) {
|
|
526
499
|
return;
|
|
527
500
|
}
|
|
528
501
|
const message = {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
502
|
+
tag: "ToServerTunnelMessage",
|
|
503
|
+
val: {
|
|
504
|
+
requestId,
|
|
505
|
+
messageId,
|
|
506
|
+
messageKind: { tag: "TunnelAck", val: null }
|
|
507
|
+
}
|
|
532
508
|
};
|
|
533
|
-
|
|
534
|
-
this.#tunnelWs.send(encoded);
|
|
509
|
+
this.#runner.__sendToServer(message);
|
|
535
510
|
}
|
|
536
511
|
#startGarbageCollector() {
|
|
537
512
|
if (this.#gcInterval) {
|
|
@@ -606,68 +581,8 @@ var Tunnel = class {
|
|
|
606
581
|
}
|
|
607
582
|
return fetchHandler;
|
|
608
583
|
}
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
if (this.#shutdown) return;
|
|
612
|
-
try {
|
|
613
|
-
this.#tunnelWs = new (0, _ws2.default)(this.#pegboardTunnelUrl, {
|
|
614
|
-
headers: {
|
|
615
|
-
"x-rivet-target": "tunnel"
|
|
616
|
-
}
|
|
617
|
-
});
|
|
618
|
-
this.#tunnelWs.binaryType = "arraybuffer";
|
|
619
|
-
this.#tunnelWs.addEventListener("open", () => {
|
|
620
|
-
this.#reconnectAttempt = 0;
|
|
621
|
-
if (this.#reconnectTimeout) {
|
|
622
|
-
clearTimeout(this.#reconnectTimeout);
|
|
623
|
-
this.#reconnectTimeout = void 0;
|
|
624
|
-
}
|
|
625
|
-
this.#callbacks.onConnected();
|
|
626
|
-
});
|
|
627
|
-
this.#tunnelWs.addEventListener("message", async (event) => {
|
|
628
|
-
var _a2;
|
|
629
|
-
try {
|
|
630
|
-
await this.#handleMessage(event.data);
|
|
631
|
-
} catch (error) {
|
|
632
|
-
(_a2 = logger()) == null ? void 0 : _a2.error({
|
|
633
|
-
msg: "error handling tunnel message",
|
|
634
|
-
error
|
|
635
|
-
});
|
|
636
|
-
}
|
|
637
|
-
});
|
|
638
|
-
this.#tunnelWs.addEventListener("error", (event) => {
|
|
639
|
-
var _a2;
|
|
640
|
-
(_a2 = logger()) == null ? void 0 : _a2.error({ msg: "tunnel websocket error", event });
|
|
641
|
-
});
|
|
642
|
-
this.#tunnelWs.addEventListener("close", () => {
|
|
643
|
-
this.#callbacks.onDisconnected();
|
|
644
|
-
if (!this.#shutdown) {
|
|
645
|
-
this.#scheduleReconnect();
|
|
646
|
-
}
|
|
647
|
-
});
|
|
648
|
-
} catch (error) {
|
|
649
|
-
(_a = logger()) == null ? void 0 : _a.error({ msg: "failed to connect tunnel", error });
|
|
650
|
-
if (!this.#shutdown) {
|
|
651
|
-
this.#scheduleReconnect();
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
#scheduleReconnect() {
|
|
656
|
-
if (this.#shutdown) return;
|
|
657
|
-
const delay = calculateBackoff(this.#reconnectAttempt, {
|
|
658
|
-
initialDelay: 1e3,
|
|
659
|
-
maxDelay: 3e4,
|
|
660
|
-
multiplier: 2,
|
|
661
|
-
jitter: true
|
|
662
|
-
});
|
|
663
|
-
this.#reconnectAttempt++;
|
|
664
|
-
this.#reconnectTimeout = setTimeout(() => {
|
|
665
|
-
this.#connect();
|
|
666
|
-
}, delay);
|
|
667
|
-
}
|
|
668
|
-
async #handleMessage(data) {
|
|
669
|
-
const message = tunnel.decodeRunnerMessage(new Uint8Array(data));
|
|
670
|
-
if (message.messageKind.tag === "Ack") {
|
|
584
|
+
async handleTunnelMessage(message) {
|
|
585
|
+
if (message.messageKind.tag === "TunnelAck") {
|
|
671
586
|
const msgIdStr = bufferToString(message.messageId);
|
|
672
587
|
const pending = this.#pendingMessages.get(msgIdStr);
|
|
673
588
|
if (pending) {
|
|
@@ -676,72 +591,41 @@ var Tunnel = class {
|
|
|
676
591
|
} else {
|
|
677
592
|
this.#sendAck(message.requestId, message.messageId);
|
|
678
593
|
switch (message.messageKind.tag) {
|
|
679
|
-
case "
|
|
594
|
+
case "ToClientRequestStart":
|
|
680
595
|
await this.#handleRequestStart(
|
|
681
596
|
message.requestId,
|
|
682
597
|
message.messageKind.val
|
|
683
598
|
);
|
|
684
599
|
break;
|
|
685
|
-
case "
|
|
600
|
+
case "ToClientRequestChunk":
|
|
686
601
|
await this.#handleRequestChunk(
|
|
687
602
|
message.requestId,
|
|
688
603
|
message.messageKind.val
|
|
689
604
|
);
|
|
690
605
|
break;
|
|
691
|
-
case "
|
|
606
|
+
case "ToClientRequestAbort":
|
|
692
607
|
await this.#handleRequestAbort(message.requestId);
|
|
693
608
|
break;
|
|
694
|
-
case "ToServerWebSocketOpen":
|
|
695
|
-
await this.#handleWebSocketOpen(
|
|
696
|
-
message.requestId,
|
|
697
|
-
message.messageKind.val
|
|
698
|
-
);
|
|
699
|
-
break;
|
|
700
|
-
case "ToServerWebSocketMessage":
|
|
701
|
-
await this.#handleWebSocketMessage(
|
|
702
|
-
message.requestId,
|
|
703
|
-
message.messageKind.val
|
|
704
|
-
);
|
|
705
|
-
break;
|
|
706
|
-
case "ToServerWebSocketClose":
|
|
707
|
-
await this.#handleWebSocketClose(
|
|
708
|
-
message.requestId,
|
|
709
|
-
message.messageKind.val
|
|
710
|
-
);
|
|
711
|
-
break;
|
|
712
|
-
case "ToClientResponseStart":
|
|
713
|
-
this.#handleResponseStart(
|
|
714
|
-
message.requestId,
|
|
715
|
-
message.messageKind.val
|
|
716
|
-
);
|
|
717
|
-
break;
|
|
718
|
-
case "ToClientResponseChunk":
|
|
719
|
-
this.#handleResponseChunk(
|
|
720
|
-
message.requestId,
|
|
721
|
-
message.messageKind.val
|
|
722
|
-
);
|
|
723
|
-
break;
|
|
724
|
-
case "ToClientResponseAbort":
|
|
725
|
-
this.#handleResponseAbort(message.requestId);
|
|
726
|
-
break;
|
|
727
609
|
case "ToClientWebSocketOpen":
|
|
728
|
-
this.#
|
|
610
|
+
await this.#handleWebSocketOpen(
|
|
729
611
|
message.requestId,
|
|
730
612
|
message.messageKind.val
|
|
731
613
|
);
|
|
732
614
|
break;
|
|
733
615
|
case "ToClientWebSocketMessage":
|
|
734
|
-
this.#
|
|
616
|
+
await this.#handleWebSocketMessage(
|
|
735
617
|
message.requestId,
|
|
736
618
|
message.messageKind.val
|
|
737
619
|
);
|
|
738
620
|
break;
|
|
739
621
|
case "ToClientWebSocketClose":
|
|
740
|
-
this.#
|
|
622
|
+
await this.#handleWebSocketClose(
|
|
741
623
|
message.requestId,
|
|
742
624
|
message.messageKind.val
|
|
743
625
|
);
|
|
744
626
|
break;
|
|
627
|
+
default:
|
|
628
|
+
unreachable(message.messageKind);
|
|
745
629
|
}
|
|
746
630
|
}
|
|
747
631
|
}
|
|
@@ -833,7 +717,7 @@ var Tunnel = class {
|
|
|
833
717
|
headers.set("content-length", String(body.byteLength));
|
|
834
718
|
}
|
|
835
719
|
this.#sendMessage(requestId, {
|
|
836
|
-
tag: "
|
|
720
|
+
tag: "ToServerResponseStart",
|
|
837
721
|
val: {
|
|
838
722
|
status: response.status,
|
|
839
723
|
headers,
|
|
@@ -846,7 +730,7 @@ var Tunnel = class {
|
|
|
846
730
|
const headers = /* @__PURE__ */ new Map();
|
|
847
731
|
headers.set("content-type", "text/plain");
|
|
848
732
|
this.#sendMessage(requestId, {
|
|
849
|
-
tag: "
|
|
733
|
+
tag: "ToServerResponseStart",
|
|
850
734
|
val: {
|
|
851
735
|
status,
|
|
852
736
|
headers,
|
|
@@ -865,7 +749,7 @@ var Tunnel = class {
|
|
|
865
749
|
actorId: open.actorId
|
|
866
750
|
});
|
|
867
751
|
this.#sendMessage(requestId, {
|
|
868
|
-
tag: "
|
|
752
|
+
tag: "ToServerWebSocketClose",
|
|
869
753
|
val: {
|
|
870
754
|
code: 1011,
|
|
871
755
|
reason: "Actor not found"
|
|
@@ -880,7 +764,7 @@ var Tunnel = class {
|
|
|
880
764
|
msg: "no websocket handler configured for tunnel"
|
|
881
765
|
});
|
|
882
766
|
this.#sendMessage(requestId, {
|
|
883
|
-
tag: "
|
|
767
|
+
tag: "ToServerWebSocketClose",
|
|
884
768
|
val: {
|
|
885
769
|
code: 1011,
|
|
886
770
|
reason: "Not Implemented"
|
|
@@ -897,7 +781,7 @@ var Tunnel = class {
|
|
|
897
781
|
(data, isBinary) => {
|
|
898
782
|
const dataBuffer = typeof data === "string" ? new TextEncoder().encode(data).buffer : data;
|
|
899
783
|
this.#sendMessage(requestId, {
|
|
900
|
-
tag: "
|
|
784
|
+
tag: "ToServerWebSocketMessage",
|
|
901
785
|
val: {
|
|
902
786
|
data: dataBuffer,
|
|
903
787
|
binary: isBinary
|
|
@@ -906,7 +790,7 @@ var Tunnel = class {
|
|
|
906
790
|
},
|
|
907
791
|
(code, reason) => {
|
|
908
792
|
this.#sendMessage(requestId, {
|
|
909
|
-
tag: "
|
|
793
|
+
tag: "ToServerWebSocketClose",
|
|
910
794
|
val: {
|
|
911
795
|
code: code || null,
|
|
912
796
|
reason: reason || null
|
|
@@ -920,7 +804,7 @@ var Tunnel = class {
|
|
|
920
804
|
);
|
|
921
805
|
this.#actorWebSockets.set(webSocketId, adapter);
|
|
922
806
|
this.#sendMessage(requestId, {
|
|
923
|
-
tag: "
|
|
807
|
+
tag: "ToServerWebSocketOpen",
|
|
924
808
|
val: null
|
|
925
809
|
});
|
|
926
810
|
adapter._handleOpen();
|
|
@@ -940,7 +824,7 @@ var Tunnel = class {
|
|
|
940
824
|
} catch (error) {
|
|
941
825
|
(_c = logger()) == null ? void 0 : _c.error({ msg: "error handling websocket open", error });
|
|
942
826
|
this.#sendMessage(requestId, {
|
|
943
|
-
tag: "
|
|
827
|
+
tag: "ToServerWebSocketClose",
|
|
944
828
|
val: {
|
|
945
829
|
code: 1011,
|
|
946
830
|
reason: "Server Error"
|
|
@@ -971,87 +855,6 @@ var Tunnel = class {
|
|
|
971
855
|
this.#actorWebSockets.delete(webSocketId);
|
|
972
856
|
}
|
|
973
857
|
}
|
|
974
|
-
#handleResponseStart(requestId, resp) {
|
|
975
|
-
var _a;
|
|
976
|
-
const requestIdStr = bufferToString(requestId);
|
|
977
|
-
const pending = this.#actorPendingRequests.get(requestIdStr);
|
|
978
|
-
if (!pending) {
|
|
979
|
-
(_a = logger()) == null ? void 0 : _a.warn({
|
|
980
|
-
msg: "received response for unknown request",
|
|
981
|
-
requestId: requestIdStr
|
|
982
|
-
});
|
|
983
|
-
return;
|
|
984
|
-
}
|
|
985
|
-
const headers = new Headers();
|
|
986
|
-
for (const [key, value] of resp.headers) {
|
|
987
|
-
headers.append(key, value);
|
|
988
|
-
}
|
|
989
|
-
if (resp.stream) {
|
|
990
|
-
const stream = new ReadableStream({
|
|
991
|
-
start: (controller) => {
|
|
992
|
-
pending.streamController = controller;
|
|
993
|
-
}
|
|
994
|
-
});
|
|
995
|
-
const response = new Response(stream, {
|
|
996
|
-
status: resp.status,
|
|
997
|
-
headers
|
|
998
|
-
});
|
|
999
|
-
pending.resolve(response);
|
|
1000
|
-
} else {
|
|
1001
|
-
const body = resp.body ? new Uint8Array(resp.body) : null;
|
|
1002
|
-
const response = new Response(body, {
|
|
1003
|
-
status: resp.status,
|
|
1004
|
-
headers
|
|
1005
|
-
});
|
|
1006
|
-
pending.resolve(response);
|
|
1007
|
-
this.#actorPendingRequests.delete(requestIdStr);
|
|
1008
|
-
}
|
|
1009
|
-
}
|
|
1010
|
-
#handleResponseChunk(requestId, chunk) {
|
|
1011
|
-
const requestIdStr = bufferToString(requestId);
|
|
1012
|
-
const pending = this.#actorPendingRequests.get(requestIdStr);
|
|
1013
|
-
if (pending == null ? void 0 : pending.streamController) {
|
|
1014
|
-
pending.streamController.enqueue(new Uint8Array(chunk.body));
|
|
1015
|
-
if (chunk.finish) {
|
|
1016
|
-
pending.streamController.close();
|
|
1017
|
-
this.#actorPendingRequests.delete(requestIdStr);
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
#handleResponseAbort(requestId) {
|
|
1022
|
-
const requestIdStr = bufferToString(requestId);
|
|
1023
|
-
const pending = this.#actorPendingRequests.get(requestIdStr);
|
|
1024
|
-
if (pending == null ? void 0 : pending.streamController) {
|
|
1025
|
-
pending.streamController.error(new Error("Response aborted"));
|
|
1026
|
-
}
|
|
1027
|
-
this.#actorPendingRequests.delete(requestIdStr);
|
|
1028
|
-
}
|
|
1029
|
-
#handleWebSocketOpenResponse(requestId, open) {
|
|
1030
|
-
const webSocketId = bufferToString(requestId);
|
|
1031
|
-
const adapter = this.#actorWebSockets.get(webSocketId);
|
|
1032
|
-
if (adapter) {
|
|
1033
|
-
adapter._handleOpen();
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
#handleWebSocketMessageResponse(requestId, msg) {
|
|
1037
|
-
const webSocketId = bufferToString(requestId);
|
|
1038
|
-
const adapter = this.#actorWebSockets.get(webSocketId);
|
|
1039
|
-
if (adapter) {
|
|
1040
|
-
const data = msg.binary ? new Uint8Array(msg.data) : new TextDecoder().decode(new Uint8Array(msg.data));
|
|
1041
|
-
adapter._handleMessage(data, msg.binary);
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
#handleWebSocketCloseResponse(requestId, close) {
|
|
1045
|
-
const webSocketId = bufferToString(requestId);
|
|
1046
|
-
const adapter = this.#actorWebSockets.get(webSocketId);
|
|
1047
|
-
if (adapter) {
|
|
1048
|
-
adapter._handleClose(
|
|
1049
|
-
close.code || void 0,
|
|
1050
|
-
close.reason || void 0
|
|
1051
|
-
);
|
|
1052
|
-
this.#actorWebSockets.delete(webSocketId);
|
|
1053
|
-
}
|
|
1054
|
-
}
|
|
1055
858
|
};
|
|
1056
859
|
function bufferToString(buffer) {
|
|
1057
860
|
return Buffer.from(buffer).toString("base64");
|
|
@@ -1098,6 +901,7 @@ var Runner = class {
|
|
|
1098
901
|
constructor(config) {
|
|
1099
902
|
this.#config = config;
|
|
1100
903
|
if (this.#config.logger) setLogger(this.#config.logger);
|
|
904
|
+
this.#tunnel = new Tunnel(this);
|
|
1101
905
|
this.#eventPruneInterval = setInterval(() => {
|
|
1102
906
|
this.#pruneOldEvents();
|
|
1103
907
|
}, 6e4);
|
|
@@ -1114,9 +918,7 @@ var Runner = class {
|
|
|
1114
918
|
async stopActor(actorId, generation) {
|
|
1115
919
|
const actor = this.#removeActor(actorId, generation);
|
|
1116
920
|
if (!actor) return;
|
|
1117
|
-
|
|
1118
|
-
this.#tunnel.unregisterActor(actor);
|
|
1119
|
-
}
|
|
921
|
+
this.#tunnel.unregisterActor(actor);
|
|
1120
922
|
try {
|
|
1121
923
|
await this.#config.onActorStop(actorId, actor.generation);
|
|
1122
924
|
} catch (err) {
|
|
@@ -1202,8 +1004,8 @@ var Runner = class {
|
|
|
1202
1004
|
if (this.#started) throw new Error("Cannot call runner.start twice");
|
|
1203
1005
|
this.#started = true;
|
|
1204
1006
|
(_a = logger()) == null ? void 0 : _a.info("starting runner");
|
|
1007
|
+
this.#tunnel.start();
|
|
1205
1008
|
try {
|
|
1206
|
-
await this.#openTunnelAndWait();
|
|
1207
1009
|
await this.#openPegboardWebSocket();
|
|
1208
1010
|
} catch (error) {
|
|
1209
1011
|
this.#started = false;
|
|
@@ -1249,7 +1051,7 @@ var Runner = class {
|
|
|
1249
1051
|
);
|
|
1250
1052
|
}
|
|
1251
1053
|
this.#kvRequests.clear();
|
|
1252
|
-
if (this.#pegboardWebSocket && this.#pegboardWebSocket.readyState ===
|
|
1054
|
+
if (this.#pegboardWebSocket && this.#pegboardWebSocket.readyState === 1) {
|
|
1253
1055
|
const pegboardWebSocket = this.#pegboardWebSocket;
|
|
1254
1056
|
if (immediate) {
|
|
1255
1057
|
pegboardWebSocket.close(1e3, "Stopping");
|
|
@@ -1263,7 +1065,7 @@ var Runner = class {
|
|
|
1263
1065
|
tag: "ToServerStopping",
|
|
1264
1066
|
val: null
|
|
1265
1067
|
});
|
|
1266
|
-
if (this.#pegboardWebSocket && this.#pegboardWebSocket.readyState ===
|
|
1068
|
+
if (this.#pegboardWebSocket && this.#pegboardWebSocket.readyState === 1) {
|
|
1267
1069
|
this.#pegboardWebSocket.send(encoded);
|
|
1268
1070
|
} else {
|
|
1269
1071
|
(_c = logger()) == null ? void 0 : _c.error(
|
|
@@ -1316,44 +1118,12 @@ var Runner = class {
|
|
|
1316
1118
|
const wsEndpoint = endpoint.replace("http://", "ws://").replace("https://", "wss://");
|
|
1317
1119
|
return `${wsEndpoint}?protocol_version=1&namespace=${encodeURIComponent(this.#config.namespace)}&runner_name=${encodeURIComponent(this.#config.runnerName)}&runner_key=${encodeURIComponent(this.#config.runnerKey)}`;
|
|
1318
1120
|
}
|
|
1319
|
-
async #openTunnelAndWait() {
|
|
1320
|
-
return new Promise((resolve, reject) => {
|
|
1321
|
-
var _a, _b, _c;
|
|
1322
|
-
const url = this.pegboardTunnelUrl;
|
|
1323
|
-
(_a = logger()) == null ? void 0 : _a.info({ msg: "opening tunnel to:", url });
|
|
1324
|
-
(_b = logger()) == null ? void 0 : _b.info({
|
|
1325
|
-
msg: "current runner id:",
|
|
1326
|
-
runnerId: this.runnerId || "none"
|
|
1327
|
-
});
|
|
1328
|
-
(_c = logger()) == null ? void 0 : _c.info({
|
|
1329
|
-
msg: "active actors count:",
|
|
1330
|
-
actors: this.#actors.size
|
|
1331
|
-
});
|
|
1332
|
-
let connected = false;
|
|
1333
|
-
this.#tunnel = new Tunnel(this, url, {
|
|
1334
|
-
onConnected: () => {
|
|
1335
|
-
var _a2;
|
|
1336
|
-
if (!connected) {
|
|
1337
|
-
connected = true;
|
|
1338
|
-
(_a2 = logger()) == null ? void 0 : _a2.info("tunnel connected");
|
|
1339
|
-
resolve();
|
|
1340
|
-
}
|
|
1341
|
-
},
|
|
1342
|
-
onDisconnected: () => {
|
|
1343
|
-
if (!connected) {
|
|
1344
|
-
reject(new Error("Tunnel connection failed"));
|
|
1345
|
-
}
|
|
1346
|
-
}
|
|
1347
|
-
});
|
|
1348
|
-
this.#tunnel.start();
|
|
1349
|
-
});
|
|
1350
|
-
}
|
|
1351
1121
|
// MARK: Runner protocol
|
|
1352
1122
|
async #openPegboardWebSocket() {
|
|
1353
1123
|
const WS = await importWebSocket();
|
|
1354
1124
|
const ws = new WS(this.pegboardUrl, {
|
|
1355
1125
|
headers: {
|
|
1356
|
-
"x-rivet-target": "runner
|
|
1126
|
+
"x-rivet-target": "runner"
|
|
1357
1127
|
}
|
|
1358
1128
|
});
|
|
1359
1129
|
this.#pegboardWebSocket = ws;
|
|
@@ -1384,7 +1154,7 @@ var Runner = class {
|
|
|
1384
1154
|
),
|
|
1385
1155
|
metadata: JSON.stringify(this.#config.metadata)
|
|
1386
1156
|
};
|
|
1387
|
-
this
|
|
1157
|
+
this.__sendToServer({
|
|
1388
1158
|
tag: "ToServerInit",
|
|
1389
1159
|
val: init
|
|
1390
1160
|
});
|
|
@@ -1392,8 +1162,8 @@ var Runner = class {
|
|
|
1392
1162
|
const pingInterval = 1e3;
|
|
1393
1163
|
const pingLoop = setInterval(() => {
|
|
1394
1164
|
var _a2;
|
|
1395
|
-
if (ws.readyState ===
|
|
1396
|
-
this
|
|
1165
|
+
if (ws.readyState === 1) {
|
|
1166
|
+
this.__sendToServer({
|
|
1397
1167
|
tag: "ToServerPing",
|
|
1398
1168
|
val: {
|
|
1399
1169
|
ts: BigInt(Date.now())
|
|
@@ -1408,7 +1178,7 @@ var Runner = class {
|
|
|
1408
1178
|
const ackInterval = 5 * 60 * 1e3;
|
|
1409
1179
|
const ackLoop = setInterval(() => {
|
|
1410
1180
|
var _a2;
|
|
1411
|
-
if (ws.readyState ===
|
|
1181
|
+
if (ws.readyState === 1) {
|
|
1412
1182
|
this.#sendCommandAcknowledgment();
|
|
1413
1183
|
} else {
|
|
1414
1184
|
clearInterval(ackLoop);
|
|
@@ -1418,19 +1188,18 @@ var Runner = class {
|
|
|
1418
1188
|
this.#ackInterval = ackLoop;
|
|
1419
1189
|
});
|
|
1420
1190
|
ws.addEventListener("message", async (ev) => {
|
|
1421
|
-
var _a, _b;
|
|
1191
|
+
var _a, _b, _c;
|
|
1422
1192
|
let buf;
|
|
1423
1193
|
if (ev.data instanceof Blob) {
|
|
1424
1194
|
buf = new Uint8Array(await ev.data.arrayBuffer());
|
|
1425
1195
|
} else if (Buffer.isBuffer(ev.data)) {
|
|
1426
1196
|
buf = new Uint8Array(ev.data);
|
|
1427
1197
|
} else {
|
|
1428
|
-
throw new Error(
|
|
1198
|
+
throw new Error(`expected binary data, got ${typeof ev.data}`);
|
|
1429
1199
|
}
|
|
1430
1200
|
const message = protocol.decodeToClient(buf);
|
|
1431
1201
|
if (message.tag === "ToClientInit") {
|
|
1432
1202
|
const init = message.val;
|
|
1433
|
-
const hadRunnerId = !!this.runnerId;
|
|
1434
1203
|
this.runnerId = init.runnerId;
|
|
1435
1204
|
this.#runnerLostThreshold = ((_a = init.metadata) == null ? void 0 : _a.runnerLostThreshold) ? Number(init.metadata.runnerLostThreshold) : void 0;
|
|
1436
1205
|
(_b = logger()) == null ? void 0 : _b.info({
|
|
@@ -1449,6 +1218,11 @@ var Runner = class {
|
|
|
1449
1218
|
} else if (message.tag === "ToClientKvResponse") {
|
|
1450
1219
|
const kvResponse = message.val;
|
|
1451
1220
|
this.#handleKvResponse(kvResponse);
|
|
1221
|
+
} else if (message.tag === "ToClientTunnelMessage") {
|
|
1222
|
+
(_c = this.#tunnel) == null ? void 0 : _c.handleTunnelMessage(message.val);
|
|
1223
|
+
} else if (message.tag === "ToClientClose") {
|
|
1224
|
+
} else {
|
|
1225
|
+
unreachable(message);
|
|
1452
1226
|
}
|
|
1453
1227
|
});
|
|
1454
1228
|
ws.addEventListener("error", (ev) => {
|
|
@@ -1497,6 +1271,8 @@ var Runner = class {
|
|
|
1497
1271
|
this.#handleCommandStartActor(commandWrapper);
|
|
1498
1272
|
} else if (commandWrapper.inner.tag === "CommandStopActor") {
|
|
1499
1273
|
this.#handleCommandStopActor(commandWrapper);
|
|
1274
|
+
} else {
|
|
1275
|
+
unreachable(commandWrapper.inner);
|
|
1500
1276
|
}
|
|
1501
1277
|
this.#lastCommandIdx = Number(commandWrapper.index);
|
|
1502
1278
|
}
|
|
@@ -1577,7 +1353,7 @@ var Runner = class {
|
|
|
1577
1353
|
tag: eventWrapper.inner.tag,
|
|
1578
1354
|
val: eventWrapper.inner.val
|
|
1579
1355
|
});
|
|
1580
|
-
this
|
|
1356
|
+
this.__sendToServer({
|
|
1581
1357
|
tag: "ToServerEvents",
|
|
1582
1358
|
val: [eventWrapper]
|
|
1583
1359
|
});
|
|
@@ -1627,7 +1403,7 @@ var Runner = class {
|
|
|
1627
1403
|
tag: eventWrapper.inner.tag,
|
|
1628
1404
|
val: eventWrapper.inner.val
|
|
1629
1405
|
});
|
|
1630
|
-
this
|
|
1406
|
+
this.__sendToServer({
|
|
1631
1407
|
tag: "ToServerEvents",
|
|
1632
1408
|
val: [eventWrapper]
|
|
1633
1409
|
});
|
|
@@ -1643,7 +1419,7 @@ var Runner = class {
|
|
|
1643
1419
|
if (this.#lastCommandIdx < 0) {
|
|
1644
1420
|
return;
|
|
1645
1421
|
}
|
|
1646
|
-
this
|
|
1422
|
+
this.__sendToServer({
|
|
1647
1423
|
tag: "ToServerAckCommands",
|
|
1648
1424
|
val: {
|
|
1649
1425
|
lastCommandIdx: BigInt(this.#lastCommandIdx)
|
|
@@ -1898,7 +1674,7 @@ var Runner = class {
|
|
|
1898
1674
|
event: eventWrapper,
|
|
1899
1675
|
timestamp: Date.now()
|
|
1900
1676
|
});
|
|
1901
|
-
this
|
|
1677
|
+
this.__sendToServer({
|
|
1902
1678
|
tag: "ToServerEvents",
|
|
1903
1679
|
val: [eventWrapper]
|
|
1904
1680
|
});
|
|
@@ -1913,7 +1689,7 @@ var Runner = class {
|
|
|
1913
1689
|
return;
|
|
1914
1690
|
}
|
|
1915
1691
|
const requestId = this.#nextRequestId++;
|
|
1916
|
-
const isConnected = this.#pegboardWebSocket && this.#pegboardWebSocket.readyState ===
|
|
1692
|
+
const isConnected = this.#pegboardWebSocket && this.#pegboardWebSocket.readyState === 1;
|
|
1917
1693
|
const requestEntry = {
|
|
1918
1694
|
actorId,
|
|
1919
1695
|
data: requestData,
|
|
@@ -1937,7 +1713,7 @@ var Runner = class {
|
|
|
1937
1713
|
requestId,
|
|
1938
1714
|
data: request.data
|
|
1939
1715
|
};
|
|
1940
|
-
this
|
|
1716
|
+
this.__sendToServer({
|
|
1941
1717
|
tag: "ToServerKvRequest",
|
|
1942
1718
|
val: kvRequest
|
|
1943
1719
|
});
|
|
@@ -1949,7 +1725,7 @@ var Runner = class {
|
|
|
1949
1725
|
}
|
|
1950
1726
|
}
|
|
1951
1727
|
#processUnsentKvRequests() {
|
|
1952
|
-
if (!this.#pegboardWebSocket || this.#pegboardWebSocket.readyState !==
|
|
1728
|
+
if (!this.#pegboardWebSocket || this.#pegboardWebSocket.readyState !== 1) {
|
|
1953
1729
|
return;
|
|
1954
1730
|
}
|
|
1955
1731
|
let processedCount = 0;
|
|
@@ -1962,7 +1738,10 @@ var Runner = class {
|
|
|
1962
1738
|
if (processedCount > 0) {
|
|
1963
1739
|
}
|
|
1964
1740
|
}
|
|
1965
|
-
|
|
1741
|
+
__webSocketReady() {
|
|
1742
|
+
return this.#pegboardWebSocket ? this.#pegboardWebSocket.readyState === 1 : false;
|
|
1743
|
+
}
|
|
1744
|
+
__sendToServer(message) {
|
|
1966
1745
|
var _a, _b;
|
|
1967
1746
|
if (this.#shutdown) {
|
|
1968
1747
|
(_a = logger()) == null ? void 0 : _a.warn(
|
|
@@ -1971,7 +1750,7 @@ var Runner = class {
|
|
|
1971
1750
|
return;
|
|
1972
1751
|
}
|
|
1973
1752
|
const encoded = protocol.encodeToServer(message);
|
|
1974
|
-
if (this.#pegboardWebSocket && this.#pegboardWebSocket.readyState ===
|
|
1753
|
+
if (this.#pegboardWebSocket && this.#pegboardWebSocket.readyState === 1) {
|
|
1975
1754
|
this.#pegboardWebSocket.send(encoded);
|
|
1976
1755
|
} else {
|
|
1977
1756
|
(_b = logger()) == null ? void 0 : _b.error(
|
|
@@ -2002,7 +1781,7 @@ var Runner = class {
|
|
|
2002
1781
|
);
|
|
2003
1782
|
if (eventsToResend.length === 0) return;
|
|
2004
1783
|
const events = eventsToResend.map((item) => item.event);
|
|
2005
|
-
this
|
|
1784
|
+
this.__sendToServer({
|
|
2006
1785
|
tag: "ToServerEvents",
|
|
2007
1786
|
val: events
|
|
2008
1787
|
});
|