@naeemo/capnp 0.5.0 → 0.5.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.
- package/dist/cli.js +69 -22
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +412 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +382 -24
- package/dist/index.js.map +1 -1
- package/dist/rpc-connection-CDzawjwJ.js +3 -0
- package/dist/{rpc-connection-CnyIwb0Z.js → rpc-connection-_eHtWsk2.js} +1 -25
- package/dist/{rpc-connection-CnyIwb0Z.js.map → rpc-connection-_eHtWsk2.js.map} +1 -1
- package/package.json +38 -26
- /package/dist/{rpc-connection-BPkslKv3.js → rpc-connection-jIPnPyT6.js} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
24
|
+
value: mod,
|
|
25
|
+
enumerable: true
|
|
26
|
+
}) : target, mod));
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
const require_rpc_connection = require('./rpc-connection-_eHtWsk2.js');
|
|
30
|
+
let node_net = require("node:net");
|
|
31
|
+
node_net = __toESM(node_net);
|
|
3
32
|
|
|
4
33
|
//#region src/core/pointer.ts
|
|
5
34
|
/**
|
|
@@ -128,6 +157,7 @@ var Segment = class Segment {
|
|
|
128
157
|
*/
|
|
129
158
|
getWord(wordOffset) {
|
|
130
159
|
const byteOffset = wordOffset * WORD_SIZE;
|
|
160
|
+
if (byteOffset + 8 > this._size) throw new Error(`Offset ${wordOffset} is outside the bounds of the segment (${this.wordCount} words)`);
|
|
131
161
|
const low = BigInt(this.view.getUint32(byteOffset, true));
|
|
132
162
|
return BigInt(this.view.getUint32(byteOffset + 4, true)) << BigInt(32) | low;
|
|
133
163
|
}
|
|
@@ -136,6 +166,7 @@ var Segment = class Segment {
|
|
|
136
166
|
*/
|
|
137
167
|
setWord(wordOffset, value) {
|
|
138
168
|
const byteOffset = wordOffset * WORD_SIZE;
|
|
169
|
+
if (byteOffset + 8 > this.buffer.byteLength) throw new Error(`Offset ${wordOffset} is outside the bounds of the segment`);
|
|
139
170
|
this.view.setUint32(byteOffset, Number(value & BigInt(4294967295)), true);
|
|
140
171
|
this.view.setUint32(byteOffset + 4, Number(value >> BigInt(32)), true);
|
|
141
172
|
}
|
|
@@ -637,73 +668,113 @@ var StructReader = class StructReader {
|
|
|
637
668
|
getBool(bitOffset) {
|
|
638
669
|
const byteOffset = Math.floor(bitOffset / 8);
|
|
639
670
|
const bitInByte = bitOffset % 8;
|
|
640
|
-
|
|
671
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
672
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
673
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
674
|
+
if (dataOffset < 0 || dataOffset >= dataSectionEnd) return false;
|
|
675
|
+
return (segment.dataView.getUint8(dataOffset) & 1 << bitInByte) !== 0;
|
|
641
676
|
}
|
|
642
677
|
/**
|
|
643
678
|
* 获取 int8 字段
|
|
644
679
|
*/
|
|
645
680
|
getInt8(byteOffset) {
|
|
646
|
-
|
|
681
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
682
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
683
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
684
|
+
if (dataOffset < 0 || dataOffset >= dataSectionEnd) return 0;
|
|
685
|
+
return segment.dataView.getInt8(dataOffset);
|
|
647
686
|
}
|
|
648
687
|
/**
|
|
649
688
|
* 获取 int16 字段
|
|
650
689
|
*/
|
|
651
690
|
getInt16(byteOffset) {
|
|
652
|
-
|
|
691
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
692
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
693
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
694
|
+
if (dataOffset < 0 || dataOffset + 2 > dataSectionEnd) return 0;
|
|
695
|
+
return segment.dataView.getInt16(dataOffset, true);
|
|
653
696
|
}
|
|
654
697
|
/**
|
|
655
698
|
* 获取 int32 字段
|
|
656
699
|
*/
|
|
657
700
|
getInt32(byteOffset) {
|
|
658
|
-
|
|
701
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
702
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
703
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
704
|
+
if (dataOffset < 0 || dataOffset + 4 > dataSectionEnd) return 0;
|
|
705
|
+
return segment.dataView.getInt32(dataOffset, true);
|
|
659
706
|
}
|
|
660
707
|
/**
|
|
661
708
|
* 获取 int64 字段
|
|
662
709
|
*/
|
|
663
710
|
getInt64(byteOffset) {
|
|
664
711
|
const segment = this.message.getSegment(this.segmentIndex);
|
|
665
|
-
const
|
|
666
|
-
const
|
|
667
|
-
|
|
712
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
713
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
714
|
+
if (dataOffset < 0 || dataOffset + 8 > dataSectionEnd) return BigInt(0);
|
|
715
|
+
const low = BigInt(segment.dataView.getUint32(dataOffset, true));
|
|
716
|
+
return BigInt(segment.dataView.getInt32(dataOffset + 4, true)) << BigInt(32) | low;
|
|
668
717
|
}
|
|
669
718
|
/**
|
|
670
719
|
* 获取 uint8 字段
|
|
671
720
|
*/
|
|
672
721
|
getUint8(byteOffset) {
|
|
673
|
-
|
|
722
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
723
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
724
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
725
|
+
if (dataOffset < 0 || dataOffset >= dataSectionEnd) return 0;
|
|
726
|
+
return segment.dataView.getUint8(dataOffset);
|
|
674
727
|
}
|
|
675
728
|
/**
|
|
676
729
|
* 获取 uint16 字段
|
|
677
730
|
*/
|
|
678
731
|
getUint16(byteOffset) {
|
|
679
|
-
|
|
732
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
733
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
734
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
735
|
+
if (dataOffset < 0 || dataOffset + 2 > dataSectionEnd) return 0;
|
|
736
|
+
return segment.dataView.getUint16(dataOffset, true);
|
|
680
737
|
}
|
|
681
738
|
/**
|
|
682
739
|
* 获取 uint32 字段
|
|
683
740
|
*/
|
|
684
741
|
getUint32(byteOffset) {
|
|
685
|
-
|
|
742
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
743
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
744
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
745
|
+
if (dataOffset < 0 || dataOffset + 4 > dataSectionEnd) return 0;
|
|
746
|
+
return segment.dataView.getUint32(dataOffset, true);
|
|
686
747
|
}
|
|
687
748
|
/**
|
|
688
749
|
* 获取 uint64 字段
|
|
689
750
|
*/
|
|
690
751
|
getUint64(byteOffset) {
|
|
691
752
|
const segment = this.message.getSegment(this.segmentIndex);
|
|
692
|
-
const
|
|
693
|
-
const
|
|
694
|
-
|
|
753
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
754
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
755
|
+
if (dataOffset < 0 || dataOffset + 8 > dataSectionEnd) return BigInt(0);
|
|
756
|
+
const low = BigInt(segment.dataView.getUint32(dataOffset, true));
|
|
757
|
+
return BigInt(segment.dataView.getUint32(dataOffset + 4, true)) << BigInt(32) | low;
|
|
695
758
|
}
|
|
696
759
|
/**
|
|
697
760
|
* 获取 float32 字段
|
|
698
761
|
*/
|
|
699
762
|
getFloat32(byteOffset) {
|
|
700
|
-
|
|
763
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
764
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
765
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
766
|
+
if (dataOffset < 0 || dataOffset + 4 > dataSectionEnd) return 0;
|
|
767
|
+
return segment.dataView.getFloat32(dataOffset, true);
|
|
701
768
|
}
|
|
702
769
|
/**
|
|
703
770
|
* 获取 float64 字段
|
|
704
771
|
*/
|
|
705
772
|
getFloat64(byteOffset) {
|
|
706
|
-
|
|
773
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
774
|
+
const dataOffset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
775
|
+
const dataSectionEnd = this.wordOffset * WORD_SIZE + this.dataWords * WORD_SIZE;
|
|
776
|
+
if (dataOffset < 0 || dataOffset + 8 > dataSectionEnd) return 0;
|
|
777
|
+
return segment.dataView.getFloat64(dataOffset, true);
|
|
707
778
|
}
|
|
708
779
|
/**
|
|
709
780
|
* 获取文本字段
|
|
@@ -748,13 +819,18 @@ var StructReader = class StructReader {
|
|
|
748
819
|
let actualStructSize = structSize;
|
|
749
820
|
const segment = this.message.getSegment(segmentIndex);
|
|
750
821
|
if (listPtr.elementSize === ElementSize.COMPOSITE) {
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
822
|
+
if (targetOffset < 0 || targetOffset >= segment.wordCount) return;
|
|
823
|
+
try {
|
|
824
|
+
const tagWord = segment.getWord(targetOffset);
|
|
825
|
+
elementCount = Number(tagWord & BigInt(4294967295));
|
|
826
|
+
actualStructSize = {
|
|
827
|
+
dataWords: Number(tagWord >> BigInt(32) & BigInt(65535)),
|
|
828
|
+
pointerCount: Number(tagWord >> BigInt(48) & BigInt(65535))
|
|
829
|
+
};
|
|
830
|
+
targetOffset += 1;
|
|
831
|
+
} catch {
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
758
834
|
}
|
|
759
835
|
return new ListReader(this.message, segmentIndex, listPtr.elementSize, elementCount, actualStructSize, targetOffset);
|
|
760
836
|
}
|
|
@@ -1694,6 +1770,316 @@ var WebSocketTransport = class WebSocketTransport {
|
|
|
1694
1770
|
}
|
|
1695
1771
|
};
|
|
1696
1772
|
|
|
1773
|
+
//#endregion
|
|
1774
|
+
//#region src/rpc/tcp-transport.ts
|
|
1775
|
+
/**
|
|
1776
|
+
* TCP Transport Implementation for Node.js
|
|
1777
|
+
*
|
|
1778
|
+
* Implements RpcTransport over raw TCP socket for C++ interop testing.
|
|
1779
|
+
* This allows direct communication with the official Cap'n Proto C++ implementation.
|
|
1780
|
+
*/
|
|
1781
|
+
/**
|
|
1782
|
+
* TCP Transport for Cap'n Proto RPC
|
|
1783
|
+
*
|
|
1784
|
+
* Uses length-prefixed binary message framing compatible with Cap'n Proto C++ implementation.
|
|
1785
|
+
* Format: [4 bytes: message length (little-endian)] [N bytes: message data]
|
|
1786
|
+
*/
|
|
1787
|
+
var TcpTransport = class TcpTransport {
|
|
1788
|
+
socket = null;
|
|
1789
|
+
messageQueue = [];
|
|
1790
|
+
receiveQueue = [];
|
|
1791
|
+
_connected = false;
|
|
1792
|
+
pendingBuffer = Buffer.alloc(0);
|
|
1793
|
+
pendingLength = 0;
|
|
1794
|
+
hasPendingLength = false;
|
|
1795
|
+
onClose;
|
|
1796
|
+
onError;
|
|
1797
|
+
constructor(host, port, options = {}) {
|
|
1798
|
+
this.host = host;
|
|
1799
|
+
this.port = port;
|
|
1800
|
+
this.options = options;
|
|
1801
|
+
}
|
|
1802
|
+
static async connect(host, port, options) {
|
|
1803
|
+
const transport = new TcpTransport(host, port, options);
|
|
1804
|
+
await transport.connect();
|
|
1805
|
+
return transport;
|
|
1806
|
+
}
|
|
1807
|
+
static fromSocket(socket, options) {
|
|
1808
|
+
const transport = new TcpTransport("", 0, options);
|
|
1809
|
+
transport.attachSocket(socket);
|
|
1810
|
+
return transport;
|
|
1811
|
+
}
|
|
1812
|
+
get connected() {
|
|
1813
|
+
return this._connected && this.socket?.readyState === "open";
|
|
1814
|
+
}
|
|
1815
|
+
connect() {
|
|
1816
|
+
return new Promise((resolve, reject) => {
|
|
1817
|
+
const timeout = setTimeout(() => {
|
|
1818
|
+
this.socket?.destroy();
|
|
1819
|
+
reject(/* @__PURE__ */ new Error("Connection timeout"));
|
|
1820
|
+
}, this.options.connectTimeoutMs ?? 1e4);
|
|
1821
|
+
this.socket = new node_net.Socket();
|
|
1822
|
+
this.socket.on("connect", () => {
|
|
1823
|
+
clearTimeout(timeout);
|
|
1824
|
+
this._connected = true;
|
|
1825
|
+
resolve();
|
|
1826
|
+
});
|
|
1827
|
+
this.socket.on("data", (data) => {
|
|
1828
|
+
this.handleData(data);
|
|
1829
|
+
});
|
|
1830
|
+
this.socket.on("close", (hadError) => {
|
|
1831
|
+
this._connected = false;
|
|
1832
|
+
this.flushReceiveQueue(null);
|
|
1833
|
+
if (hadError) this.onClose?.(/* @__PURE__ */ new Error("Connection closed with error"));
|
|
1834
|
+
else this.onClose?.();
|
|
1835
|
+
});
|
|
1836
|
+
this.socket.on("error", (err) => {
|
|
1837
|
+
clearTimeout(timeout);
|
|
1838
|
+
this._connected = false;
|
|
1839
|
+
this.onError?.(err);
|
|
1840
|
+
reject(err);
|
|
1841
|
+
});
|
|
1842
|
+
this.socket.on("end", () => {
|
|
1843
|
+
this._connected = false;
|
|
1844
|
+
});
|
|
1845
|
+
this.socket.connect(this.port, this.host);
|
|
1846
|
+
});
|
|
1847
|
+
}
|
|
1848
|
+
attachSocket(socket) {
|
|
1849
|
+
this.socket = socket;
|
|
1850
|
+
this._connected = socket.readyState === "open";
|
|
1851
|
+
socket.on("data", (data) => {
|
|
1852
|
+
this.handleData(data);
|
|
1853
|
+
});
|
|
1854
|
+
socket.on("close", (hadError) => {
|
|
1855
|
+
this._connected = false;
|
|
1856
|
+
this.flushReceiveQueue(null);
|
|
1857
|
+
if (hadError) this.onClose?.(/* @__PURE__ */ new Error("Connection closed with error"));
|
|
1858
|
+
else this.onClose?.();
|
|
1859
|
+
});
|
|
1860
|
+
socket.on("error", (err) => {
|
|
1861
|
+
this._connected = false;
|
|
1862
|
+
this.onError?.(err);
|
|
1863
|
+
});
|
|
1864
|
+
socket.on("end", () => {
|
|
1865
|
+
this._connected = false;
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
handleData(data) {
|
|
1869
|
+
this.pendingBuffer = Buffer.concat([this.pendingBuffer, data]);
|
|
1870
|
+
while (this.pendingBuffer.length >= 4) {
|
|
1871
|
+
if (!this.hasPendingLength) {
|
|
1872
|
+
this.pendingLength = this.pendingBuffer.readUInt32LE(0);
|
|
1873
|
+
this.hasPendingLength = true;
|
|
1874
|
+
if (this.pendingLength > 64 * 1024 * 1024) {
|
|
1875
|
+
this.onError?.(/* @__PURE__ */ new Error(`Message too large: ${this.pendingLength} bytes`));
|
|
1876
|
+
this.close(/* @__PURE__ */ new Error("Message too large"));
|
|
1877
|
+
return;
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
const totalLength = 4 + this.pendingLength;
|
|
1881
|
+
if (this.pendingBuffer.length < totalLength) break;
|
|
1882
|
+
const messageData = this.pendingBuffer.subarray(4, totalLength);
|
|
1883
|
+
this.pendingBuffer = this.pendingBuffer.subarray(totalLength);
|
|
1884
|
+
this.hasPendingLength = false;
|
|
1885
|
+
try {
|
|
1886
|
+
const message = deserializeRpcMessage(new Uint8Array(messageData));
|
|
1887
|
+
this.handleRpcMessage(message);
|
|
1888
|
+
} catch (err) {
|
|
1889
|
+
this.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
handleRpcMessage(message) {
|
|
1894
|
+
if (this.receiveQueue.length > 0) {
|
|
1895
|
+
const { resolve } = this.receiveQueue.shift();
|
|
1896
|
+
resolve(message);
|
|
1897
|
+
} else this.messageQueue.push(message);
|
|
1898
|
+
}
|
|
1899
|
+
async send(message) {
|
|
1900
|
+
if (!this.socket || this.socket.readyState !== "open") throw new Error("Socket not connected");
|
|
1901
|
+
const data = serializeRpcMessage(message);
|
|
1902
|
+
const frame = Buffer.allocUnsafe(4 + data.length);
|
|
1903
|
+
frame.writeUInt32LE(data.length, 0);
|
|
1904
|
+
frame.set(data, 4);
|
|
1905
|
+
return new Promise((resolve, reject) => {
|
|
1906
|
+
this.socket.write(frame, (err) => {
|
|
1907
|
+
if (err) reject(err);
|
|
1908
|
+
else resolve();
|
|
1909
|
+
});
|
|
1910
|
+
});
|
|
1911
|
+
}
|
|
1912
|
+
async receive() {
|
|
1913
|
+
if (this.messageQueue.length > 0) return this.messageQueue.shift();
|
|
1914
|
+
if (!this._connected) return null;
|
|
1915
|
+
return new Promise((resolve, reject) => {
|
|
1916
|
+
this.receiveQueue.push({
|
|
1917
|
+
resolve,
|
|
1918
|
+
reject
|
|
1919
|
+
});
|
|
1920
|
+
});
|
|
1921
|
+
}
|
|
1922
|
+
close(reason) {
|
|
1923
|
+
this._connected = false;
|
|
1924
|
+
this.socket?.end();
|
|
1925
|
+
this.socket?.destroy();
|
|
1926
|
+
this.flushReceiveQueue(null);
|
|
1927
|
+
this.onClose?.(reason);
|
|
1928
|
+
}
|
|
1929
|
+
flushReceiveQueue(value) {
|
|
1930
|
+
while (this.receiveQueue.length > 0) {
|
|
1931
|
+
const { resolve } = this.receiveQueue.shift();
|
|
1932
|
+
resolve(value);
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
};
|
|
1936
|
+
|
|
1937
|
+
//#endregion
|
|
1938
|
+
//#region src/rpc/ezrpc-transport.ts
|
|
1939
|
+
/**
|
|
1940
|
+
* EzRpc TCP Transport
|
|
1941
|
+
*
|
|
1942
|
+
* Implements RpcTransport over raw TCP socket for C++ EzRpc compatibility.
|
|
1943
|
+
* Does NOT use length-prefixed framing - sends raw Cap'n Proto messages.
|
|
1944
|
+
*/
|
|
1945
|
+
/**
|
|
1946
|
+
* EzRpc TCP Transport
|
|
1947
|
+
*
|
|
1948
|
+
* Compatible with Cap'n Proto C++ EzRpcServer/EzRpcClient.
|
|
1949
|
+
* Sends raw Cap'n Proto messages without length prefix.
|
|
1950
|
+
*/
|
|
1951
|
+
var EzRpcTransport = class EzRpcTransport {
|
|
1952
|
+
socket = null;
|
|
1953
|
+
messageQueue = [];
|
|
1954
|
+
receiveQueue = [];
|
|
1955
|
+
_connected = false;
|
|
1956
|
+
pendingBuffer = Buffer.alloc(0);
|
|
1957
|
+
onClose;
|
|
1958
|
+
onError;
|
|
1959
|
+
constructor(host, port, options = {}) {
|
|
1960
|
+
this.host = host;
|
|
1961
|
+
this.port = port;
|
|
1962
|
+
this.options = options;
|
|
1963
|
+
}
|
|
1964
|
+
static async connect(host, port, options) {
|
|
1965
|
+
const transport = new EzRpcTransport(host, port, options);
|
|
1966
|
+
await transport.doConnect();
|
|
1967
|
+
return transport;
|
|
1968
|
+
}
|
|
1969
|
+
get connected() {
|
|
1970
|
+
return this._connected && this.socket !== null && !this.socket.destroyed;
|
|
1971
|
+
}
|
|
1972
|
+
doConnect() {
|
|
1973
|
+
return new Promise((resolve, reject) => {
|
|
1974
|
+
const timeout = setTimeout(() => {
|
|
1975
|
+
this.socket?.destroy();
|
|
1976
|
+
reject(/* @__PURE__ */ new Error(`Connection timeout: failed to connect to ${this.host}:${this.port}`));
|
|
1977
|
+
}, this.options.connectTimeoutMs ?? 1e4);
|
|
1978
|
+
this.socket = new node_net.Socket();
|
|
1979
|
+
this.socket.on("connect", () => {
|
|
1980
|
+
clearTimeout(timeout);
|
|
1981
|
+
this._connected = true;
|
|
1982
|
+
resolve();
|
|
1983
|
+
});
|
|
1984
|
+
this.socket.on("data", (data) => {
|
|
1985
|
+
try {
|
|
1986
|
+
this.handleData(data);
|
|
1987
|
+
} catch (err) {
|
|
1988
|
+
this.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
1989
|
+
}
|
|
1990
|
+
});
|
|
1991
|
+
this.socket.on("close", (hadError) => {
|
|
1992
|
+
const wasConnected = this._connected;
|
|
1993
|
+
this._connected = false;
|
|
1994
|
+
const error = hadError ? /* @__PURE__ */ new Error("Connection closed with error") : /* @__PURE__ */ new Error("Connection closed");
|
|
1995
|
+
this.flushReceiveQueueWithError(error);
|
|
1996
|
+
if (wasConnected) this.onClose?.(hadError ? error : void 0);
|
|
1997
|
+
});
|
|
1998
|
+
this.socket.on("error", (err) => {
|
|
1999
|
+
clearTimeout(timeout);
|
|
2000
|
+
this._connected = false;
|
|
2001
|
+
this.flushReceiveQueueWithError(err);
|
|
2002
|
+
this.onError?.(err);
|
|
2003
|
+
reject(err);
|
|
2004
|
+
});
|
|
2005
|
+
this.socket.connect(this.port, this.host);
|
|
2006
|
+
});
|
|
2007
|
+
}
|
|
2008
|
+
handleData(data) {
|
|
2009
|
+
if (this.pendingBuffer.length > 0) this.pendingBuffer = Buffer.concat([this.pendingBuffer, data]);
|
|
2010
|
+
else this.pendingBuffer = Buffer.from(data);
|
|
2011
|
+
while (this.pendingBuffer.length >= 8) {
|
|
2012
|
+
const segmentCount = this.pendingBuffer.readUInt32LE(0) + 1;
|
|
2013
|
+
const firstSegmentSize = this.pendingBuffer.readUInt32LE(4);
|
|
2014
|
+
let headerSize = 8;
|
|
2015
|
+
if (segmentCount > 1) headerSize += (segmentCount - 1) * 4;
|
|
2016
|
+
const messageSize = headerSize + firstSegmentSize * 8;
|
|
2017
|
+
if (this.pendingBuffer.length < messageSize) break;
|
|
2018
|
+
const messageData = this.pendingBuffer.subarray(0, messageSize);
|
|
2019
|
+
this.pendingBuffer = this.pendingBuffer.subarray(messageSize);
|
|
2020
|
+
try {
|
|
2021
|
+
const message = deserializeRpcMessage(new Uint8Array(messageData));
|
|
2022
|
+
if (this.receiveQueue.length > 0) {
|
|
2023
|
+
const { resolve } = this.receiveQueue.shift();
|
|
2024
|
+
resolve(message);
|
|
2025
|
+
} else this.messageQueue.push(message);
|
|
2026
|
+
} catch (err) {
|
|
2027
|
+
this.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
async send(message) {
|
|
2032
|
+
if (!this.connected) throw new Error(`Cannot send: socket not connected to ${this.host}:${this.port}`);
|
|
2033
|
+
const data = serializeRpcMessage(message);
|
|
2034
|
+
return new Promise((resolve, reject) => {
|
|
2035
|
+
this.socket.write(Buffer.from(data), (err) => {
|
|
2036
|
+
if (err) reject(/* @__PURE__ */ new Error(`Failed to send message: ${err.message}`));
|
|
2037
|
+
else resolve();
|
|
2038
|
+
});
|
|
2039
|
+
});
|
|
2040
|
+
}
|
|
2041
|
+
async receive() {
|
|
2042
|
+
if (this.messageQueue.length > 0) return this.messageQueue.shift();
|
|
2043
|
+
if (!this.connected) return null;
|
|
2044
|
+
return new Promise((resolve, reject) => {
|
|
2045
|
+
const timeoutId = setTimeout(() => {
|
|
2046
|
+
const index = this.receiveQueue.findIndex((item) => item.resolve === resolve);
|
|
2047
|
+
if (index !== -1) this.receiveQueue.splice(index, 1);
|
|
2048
|
+
reject(/* @__PURE__ */ new Error("Receive timeout"));
|
|
2049
|
+
}, 3e4);
|
|
2050
|
+
this.receiveQueue.push({
|
|
2051
|
+
resolve: (msg) => {
|
|
2052
|
+
clearTimeout(timeoutId);
|
|
2053
|
+
resolve(msg);
|
|
2054
|
+
},
|
|
2055
|
+
reject: (err) => {
|
|
2056
|
+
clearTimeout(timeoutId);
|
|
2057
|
+
reject(err);
|
|
2058
|
+
}
|
|
2059
|
+
});
|
|
2060
|
+
});
|
|
2061
|
+
}
|
|
2062
|
+
close(reason) {
|
|
2063
|
+
this._connected = false;
|
|
2064
|
+
this.socket?.end();
|
|
2065
|
+
this.socket?.destroy();
|
|
2066
|
+
this.flushReceiveQueue(null);
|
|
2067
|
+
this.onClose?.(reason);
|
|
2068
|
+
}
|
|
2069
|
+
flushReceiveQueueWithError(error) {
|
|
2070
|
+
while (this.receiveQueue.length > 0) {
|
|
2071
|
+
const { reject } = this.receiveQueue.shift();
|
|
2072
|
+
reject(error);
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
flushReceiveQueue(value) {
|
|
2076
|
+
while (this.receiveQueue.length > 0) {
|
|
2077
|
+
const { resolve } = this.receiveQueue.shift();
|
|
2078
|
+
resolve(value);
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
};
|
|
2082
|
+
|
|
1697
2083
|
//#endregion
|
|
1698
2084
|
//#region src/rpc/capability-client.ts
|
|
1699
2085
|
/** Base class for capability client implementations */
|
|
@@ -2306,7 +2692,7 @@ var ConnectionManager = class {
|
|
|
2306
2692
|
}
|
|
2307
2693
|
}
|
|
2308
2694
|
async doEstablishConnection(vatId, address) {
|
|
2309
|
-
const { RpcConnection } = await Promise.resolve().then(() => require("./rpc-connection-
|
|
2695
|
+
const { RpcConnection } = await Promise.resolve().then(() => require("./rpc-connection-CDzawjwJ.js"));
|
|
2310
2696
|
const connection = new RpcConnection(await this.options.connectionFactory(vatId, address), this.options.connectionOptions);
|
|
2311
2697
|
await connection.start();
|
|
2312
2698
|
this.registerConnection(vatId, connection);
|
|
@@ -5845,6 +6231,7 @@ exports.DEFAULT_STREAMING_CAPABILITIES = DEFAULT_STREAMING_CAPABILITIES;
|
|
|
5845
6231
|
exports.DropPolicy = DropPolicy;
|
|
5846
6232
|
exports.ElementSize = ElementSize;
|
|
5847
6233
|
exports.ExportTable = require_rpc_connection.ExportTable;
|
|
6234
|
+
exports.EzRpcTransport = EzRpcTransport;
|
|
5848
6235
|
exports.ImportTable = require_rpc_connection.ImportTable;
|
|
5849
6236
|
exports.Level3Handlers = Level3Handlers;
|
|
5850
6237
|
exports.Level4Handlers = Level4Handlers;
|
|
@@ -5878,6 +6265,7 @@ exports.StreamingRpcConnection = StreamingRpcConnection;
|
|
|
5878
6265
|
exports.StructBuilder = StructBuilder;
|
|
5879
6266
|
exports.StructReader = StructReader;
|
|
5880
6267
|
exports.SturdyRefManager = SturdyRefManager;
|
|
6268
|
+
exports.TcpTransport = TcpTransport;
|
|
5881
6269
|
exports.UnionBuilder = UnionBuilder;
|
|
5882
6270
|
exports.UnionReader = UnionReader;
|
|
5883
6271
|
exports.WORD_SIZE = WORD_SIZE;
|