@olane/o-node 0.7.53 → 0.7.55
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/src/connection/interfaces/o-node-connection.config.d.ts +2 -1
- package/dist/src/connection/interfaces/o-node-connection.config.d.ts.map +1 -1
- package/dist/src/connection/interfaces/stream-init-message.d.ts +39 -4
- package/dist/src/connection/interfaces/stream-init-message.d.ts.map +1 -1
- package/dist/src/connection/interfaces/stream-init-message.js +11 -1
- package/dist/src/connection/o-node-connection.d.ts +49 -3
- package/dist/src/connection/o-node-connection.d.ts.map +1 -1
- package/dist/src/connection/o-node-connection.js +120 -2
- package/dist/src/connection/o-node-connection.manager.d.ts +6 -55
- package/dist/src/connection/o-node-connection.manager.d.ts.map +1 -1
- package/dist/src/connection/o-node-connection.manager.js +47 -183
- package/dist/src/connection/o-node-stream.d.ts.map +1 -1
- package/dist/src/connection/o-node-stream.js +1 -0
- package/dist/src/connection/o-node-stream.manager.d.ts +34 -5
- package/dist/src/connection/o-node-stream.manager.d.ts.map +1 -1
- package/dist/src/connection/o-node-stream.manager.js +190 -20
- package/dist/src/connection/stream-manager.events.d.ts +13 -1
- package/dist/src/connection/stream-manager.events.d.ts.map +1 -1
- package/dist/src/connection/stream-manager.events.js +2 -0
- package/dist/src/o-node.tool.d.ts.map +1 -1
- package/dist/src/o-node.tool.js +14 -19
- package/dist/src/utils/connection.utils.d.ts +3 -3
- package/dist/src/utils/connection.utils.d.ts.map +1 -1
- package/dist/src/utils/connection.utils.js +46 -19
- package/package.json +7 -7
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { oConnectionManager } from '@olane/o-core';
|
|
2
2
|
import { oNodeConnection } from './o-node-connection.js';
|
|
3
|
+
/**
|
|
4
|
+
* Manages oNodeConnection instances, reusing connections when possible.
|
|
5
|
+
*/
|
|
3
6
|
export class oNodeConnectionManager extends oConnectionManager {
|
|
4
7
|
constructor(config) {
|
|
5
8
|
super(config);
|
|
@@ -24,35 +27,9 @@ export class oNodeConnectionManager extends oConnectionManager {
|
|
|
24
27
|
return;
|
|
25
28
|
}
|
|
26
29
|
const connectionId = connection.id;
|
|
27
|
-
|
|
28
|
-
for (const [key, conns] of this.cachedConnections.entries()) {
|
|
29
|
-
const filtered = conns.filter((c) => c.p2pConnection.id !== connectionId);
|
|
30
|
-
if (filtered.length === 0) {
|
|
31
|
-
this.logger.debug('Connection closed, removing all cached connections for address:', key);
|
|
32
|
-
this.cachedConnections.delete(key);
|
|
33
|
-
}
|
|
34
|
-
else if (filtered.length !== conns.length) {
|
|
35
|
-
this.logger.debug('Connection closed, updating cached connections for address:', key);
|
|
36
|
-
this.cachedConnections.set(key, filtered);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
30
|
+
this.cachedConnections.delete(connectionId);
|
|
39
31
|
});
|
|
40
32
|
}
|
|
41
|
-
/**
|
|
42
|
-
* Build a stable cache key from an address.
|
|
43
|
-
*
|
|
44
|
-
* We key the cache by address value (e.g., "o://my-tool") to maintain
|
|
45
|
-
* a simple one-to-one mapping between addresses and connections.
|
|
46
|
-
*/
|
|
47
|
-
getAddressKey(address) {
|
|
48
|
-
try {
|
|
49
|
-
return address.value || null;
|
|
50
|
-
}
|
|
51
|
-
catch (error) {
|
|
52
|
-
this.logger.debug('Error extracting address key from address:', error);
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
33
|
/**
|
|
57
34
|
* Extract peer ID string from an address
|
|
58
35
|
* @param address - The address to extract peer ID from
|
|
@@ -71,71 +48,22 @@ export class oNodeConnectionManager extends oConnectionManager {
|
|
|
71
48
|
return null;
|
|
72
49
|
}
|
|
73
50
|
}
|
|
74
|
-
/**
|
|
75
|
-
* Get the first valid (open) connection for the given address key.
|
|
76
|
-
* Cleans up stale connections from the cache automatically.
|
|
77
|
-
*
|
|
78
|
-
* @param addressKey - The address key to look up
|
|
79
|
-
* @returns A valid oNodeConnection or null if none found
|
|
80
|
-
*/
|
|
81
|
-
getValidConnection(addressKey) {
|
|
82
|
-
const connections = this.cachedConnections.get(addressKey) || [];
|
|
83
|
-
// Filter to open connections
|
|
84
|
-
const valid = connections.filter((c) => c.p2pConnection?.status === 'open');
|
|
85
|
-
// Update cache if we cleaned any stale connections
|
|
86
|
-
if (valid.length !== connections.length) {
|
|
87
|
-
if (valid.length === 0) {
|
|
88
|
-
this.cachedConnections.delete(addressKey);
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
this.cachedConnections.set(addressKey, valid);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return valid[0] ?? null;
|
|
95
|
-
}
|
|
96
51
|
/**
|
|
97
52
|
* Cache an oNodeConnection by its address key.
|
|
98
53
|
* @param conn - The oNodeConnection to cache
|
|
99
54
|
* @param addressKey - The address key to cache under
|
|
100
55
|
*/
|
|
101
|
-
cacheConnection(conn
|
|
102
|
-
this.logger.debug('Caching connection for address:',
|
|
103
|
-
|
|
104
|
-
existing.push(conn);
|
|
105
|
-
this.cachedConnections.set(addressKey, existing);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Get oNodeConnection by libp2p Connection reference
|
|
109
|
-
* Used to find the correct oNodeConnection for incoming streams
|
|
110
|
-
* @param p2pConnection - The libp2p connection to search for
|
|
111
|
-
* @returns The oNodeConnection or undefined if not found
|
|
112
|
-
*/
|
|
113
|
-
getConnectionByP2pConnection(p2pConnection) {
|
|
114
|
-
// Search through all cached connections
|
|
115
|
-
for (const connections of this.cachedConnections.values()) {
|
|
116
|
-
const found = connections.find((conn) => conn.p2pConnection.id === p2pConnection.id);
|
|
117
|
-
if (found) {
|
|
118
|
-
return found;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return undefined;
|
|
56
|
+
cacheConnection(conn) {
|
|
57
|
+
this.logger.debug('Caching connection for address:', conn.p2pConnection.id, conn.p2pConnection.direction, conn.nextHopAddress.value, conn.p2pConnection.streams.map((s) => s.protocol).join(', '));
|
|
58
|
+
this.cachedConnections.set(conn.p2pConnection.id, conn);
|
|
122
59
|
}
|
|
123
60
|
/**
|
|
124
61
|
* Get or create a raw p2p connection to the given address.
|
|
125
62
|
* Subclasses can override connect() and use this method to get the underlying p2p connection.
|
|
126
63
|
*/
|
|
127
64
|
async getOrCreateP2pConnection(nextHopAddress, addressKey) {
|
|
128
|
-
// Check if libp2p already has an active connection for this peer
|
|
129
|
-
const peerId = this.getPeerIdFromAddress(nextHopAddress);
|
|
130
|
-
if (peerId) {
|
|
131
|
-
const connections = this.p2pNode.getConnections();
|
|
132
|
-
const existingConnection = connections.find((conn) => conn.remotePeer?.toString() === peerId && conn.status === 'open');
|
|
133
|
-
if (existingConnection) {
|
|
134
|
-
this.logger.debug('Found existing libp2p connection for address:', addressKey);
|
|
135
|
-
return existingConnection;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
65
|
// Check if dial is already in progress for this address key
|
|
66
|
+
this.logger.debug('Checking for pending dial for address:', addressKey);
|
|
139
67
|
const pendingDial = this.pendingDialsByAddress.get(addressKey);
|
|
140
68
|
if (pendingDial) {
|
|
141
69
|
this.logger.debug('Awaiting existing dial for address:', addressKey);
|
|
@@ -166,25 +94,23 @@ export class oNodeConnectionManager extends oConnectionManager {
|
|
|
166
94
|
}
|
|
167
95
|
async answer(config) {
|
|
168
96
|
const { address, nextHopAddress, callerAddress, readTimeoutMs, drainTimeoutMs, p2pConnection, reuse, } = config;
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
97
|
+
this.logger.debug('Answering connection for address:', {
|
|
98
|
+
address: nextHopAddress?.value,
|
|
99
|
+
connectionId: p2pConnection.id,
|
|
100
|
+
direction: p2pConnection.direction,
|
|
101
|
+
reuse,
|
|
102
|
+
});
|
|
174
103
|
// Check if we already have a cached connection for this address with the same connection id
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
c.p2pConnection?.status === 'open');
|
|
179
|
-
if (validConnections.length > 0) {
|
|
180
|
-
const existingConnection = validConnections[0];
|
|
181
|
-
this.logger.debug('Reusing cached connection for answer:', addressKey, existingConnection.p2pConnection.id);
|
|
104
|
+
const existingConnection = this.cachedConnections.get(p2pConnection.id);
|
|
105
|
+
if (existingConnection) {
|
|
106
|
+
this.logger.debug('Reusing cached connection for answer:', existingConnection.p2pConnection.id);
|
|
182
107
|
return existingConnection;
|
|
183
108
|
}
|
|
184
109
|
const connection = new oNodeConnection({
|
|
185
110
|
nextHopAddress: nextHopAddress,
|
|
186
111
|
address: address,
|
|
187
112
|
p2pConnection: p2pConnection,
|
|
113
|
+
p2pNode: this.p2pNode,
|
|
188
114
|
callerAddress: callerAddress,
|
|
189
115
|
readTimeoutMs: readTimeoutMs ?? this.defaultReadTimeoutMs,
|
|
190
116
|
drainTimeoutMs: drainTimeoutMs ?? this.defaultDrainTimeoutMs,
|
|
@@ -194,9 +120,26 @@ export class oNodeConnectionManager extends oConnectionManager {
|
|
|
194
120
|
reusePolicy: reuse ? 'reuse' : 'none',
|
|
195
121
|
});
|
|
196
122
|
// Cache the new connection
|
|
197
|
-
this.cacheConnection(connection
|
|
123
|
+
this.cacheConnection(connection);
|
|
198
124
|
return connection;
|
|
199
125
|
}
|
|
126
|
+
getConnectionFromAddress(address) {
|
|
127
|
+
const protocol = address.protocol;
|
|
128
|
+
this.logger.debug('Searching cached connections for protocol:', protocol);
|
|
129
|
+
for (const conn of this.cachedConnections.values()) {
|
|
130
|
+
// if nextHopAddress protocol matches, return conn
|
|
131
|
+
if (conn.nextHopAddress.protocol === protocol) {
|
|
132
|
+
this.logger.debug('local reuse cache found:', protocol);
|
|
133
|
+
return conn;
|
|
134
|
+
}
|
|
135
|
+
// if remote protocols include protocol, return conn
|
|
136
|
+
if (conn.remoteProtocols.includes(protocol)) {
|
|
137
|
+
this.logger.debug('remote reuse cache found', protocol);
|
|
138
|
+
return conn;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
200
143
|
/**
|
|
201
144
|
* Connect to a given address, reusing oNodeConnection when possible
|
|
202
145
|
* @param config - Connection configuration
|
|
@@ -207,22 +150,24 @@ export class oNodeConnectionManager extends oConnectionManager {
|
|
|
207
150
|
if (!nextHopAddress) {
|
|
208
151
|
throw new Error('Invalid address passed');
|
|
209
152
|
}
|
|
210
|
-
const addressKey = this.getAddressKey(nextHopAddress);
|
|
211
|
-
if (!addressKey) {
|
|
212
|
-
throw new Error(`Unable to extract address key from address: ${nextHopAddress.toString()}`);
|
|
213
|
-
}
|
|
214
153
|
// Check for existing valid cached connection
|
|
215
|
-
const existingConnection = this.
|
|
154
|
+
const existingConnection = this.getConnectionFromAddress(nextHopAddress);
|
|
216
155
|
if (existingConnection) {
|
|
217
|
-
this.logger.debug('Reusing cached connection for address:',
|
|
156
|
+
this.logger.debug('Reusing cached connection for address:', existingConnection.p2pConnection.id);
|
|
218
157
|
return existingConnection;
|
|
219
158
|
}
|
|
159
|
+
else {
|
|
160
|
+
this.logger.debug('No cached connection found for address:', {
|
|
161
|
+
address: nextHopAddress.value,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
220
164
|
// Get or create the underlying p2p connection
|
|
221
|
-
const p2pConnection = await this.getOrCreateP2pConnection(nextHopAddress,
|
|
165
|
+
const p2pConnection = await this.getOrCreateP2pConnection(nextHopAddress, nextHopAddress.value);
|
|
222
166
|
// Create new oNodeConnection
|
|
223
167
|
const connection = new oNodeConnection({
|
|
224
168
|
nextHopAddress: nextHopAddress,
|
|
225
169
|
address: address,
|
|
170
|
+
p2pNode: this.p2pNode,
|
|
226
171
|
p2pConnection: p2pConnection,
|
|
227
172
|
callerAddress: callerAddress,
|
|
228
173
|
readTimeoutMs: readTimeoutMs ?? this.defaultReadTimeoutMs,
|
|
@@ -230,91 +175,10 @@ export class oNodeConnectionManager extends oConnectionManager {
|
|
|
230
175
|
isStream: config.isStream ?? false,
|
|
231
176
|
abortSignal: config.abortSignal,
|
|
232
177
|
runOnLimitedConnection: this.config.runOnLimitedConnection ?? false,
|
|
178
|
+
requestHandler: config.requestHandler,
|
|
233
179
|
});
|
|
234
180
|
// Cache the new connection
|
|
235
|
-
this.cacheConnection(connection
|
|
181
|
+
this.cacheConnection(connection);
|
|
236
182
|
return connection;
|
|
237
183
|
}
|
|
238
|
-
/**
|
|
239
|
-
* Check if we have an active connection to the target peer
|
|
240
|
-
* @param address - The address to check
|
|
241
|
-
* @returns true if an active connection exists
|
|
242
|
-
*/
|
|
243
|
-
isCached(address) {
|
|
244
|
-
try {
|
|
245
|
-
const addressKey = this.getAddressKey(address);
|
|
246
|
-
if (!addressKey) {
|
|
247
|
-
return false;
|
|
248
|
-
}
|
|
249
|
-
return this.getValidConnection(addressKey) !== null;
|
|
250
|
-
}
|
|
251
|
-
catch (error) {
|
|
252
|
-
this.logger.debug('Error checking cached connection:', error);
|
|
253
|
-
return false;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Get an existing cached oNodeConnection for the target address
|
|
258
|
-
* @param address - The address to get a connection for
|
|
259
|
-
* @returns The oNodeConnection or null if not found
|
|
260
|
-
*/
|
|
261
|
-
getCachedConnection(address) {
|
|
262
|
-
try {
|
|
263
|
-
const addressKey = this.getAddressKey(address);
|
|
264
|
-
if (!addressKey) {
|
|
265
|
-
return null;
|
|
266
|
-
}
|
|
267
|
-
return this.getValidConnection(addressKey);
|
|
268
|
-
}
|
|
269
|
-
catch (error) {
|
|
270
|
-
this.logger.debug('Error getting cached connection:', error);
|
|
271
|
-
return null;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Get cache statistics for monitoring and debugging
|
|
276
|
-
* @returns Object containing cache statistics
|
|
277
|
-
*/
|
|
278
|
-
getCacheStats() {
|
|
279
|
-
const allConnections = [];
|
|
280
|
-
for (const [addressKey, connections] of this.cachedConnections.entries()) {
|
|
281
|
-
for (const conn of connections) {
|
|
282
|
-
allConnections.push({
|
|
283
|
-
peerId: conn.p2pConnection?.remotePeer?.toString() ?? 'unknown',
|
|
284
|
-
status: conn.p2pConnection?.status ?? 'unknown',
|
|
285
|
-
addressKey,
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return {
|
|
290
|
-
cachedAddresses: this.cachedConnections.size,
|
|
291
|
-
totalCachedConnections: allConnections.length,
|
|
292
|
-
pendingDials: this.pendingDialsByAddress.size,
|
|
293
|
-
connectionsByPeer: allConnections,
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Clean up all stale (non-open) connections from cache
|
|
298
|
-
* @returns Number of connections removed
|
|
299
|
-
*/
|
|
300
|
-
cleanupStaleConnections() {
|
|
301
|
-
let removed = 0;
|
|
302
|
-
for (const [addressKey, connections] of this.cachedConnections.entries()) {
|
|
303
|
-
const openConnections = connections.filter((conn) => conn.p2pConnection?.status === 'open');
|
|
304
|
-
const staleCount = connections.length - openConnections.length;
|
|
305
|
-
if (staleCount > 0) {
|
|
306
|
-
removed += staleCount;
|
|
307
|
-
if (openConnections.length === 0) {
|
|
308
|
-
this.cachedConnections.delete(addressKey);
|
|
309
|
-
}
|
|
310
|
-
else {
|
|
311
|
-
this.cachedConnections.set(addressKey, openConnections);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
if (removed > 0) {
|
|
316
|
-
this.logger.debug(`Cleaned up ${removed} stale connections`);
|
|
317
|
-
}
|
|
318
|
-
return removed;
|
|
319
|
-
}
|
|
320
184
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o-node-stream.d.ts","sourceRoot":"","sources":["../../../src/connection/o-node-stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAuB,OAAO,EAAiB,MAAM,eAAe,CAAC;AAC5E,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AAEzE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IAAI,OAAO,CAAC,IAAI,CAAC;CAiB7B"}
|
|
@@ -76,6 +76,7 @@ export class oNodeStream extends oObject {
|
|
|
76
76
|
if (this.p2pStream.status === 'open') {
|
|
77
77
|
try {
|
|
78
78
|
// force the close for now until we can implement a proper close
|
|
79
|
+
this.logger.debug('Closing p2p stream');
|
|
79
80
|
await this.p2pStream.abort(new Error('Stream closed'));
|
|
80
81
|
}
|
|
81
82
|
catch (error) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
3
|
import type { Connection, Stream } from '@libp2p/interface';
|
|
4
|
-
import { oObject, oResponse } from '@olane/o-core';
|
|
4
|
+
import { oObject, oRequest, oResponse } from '@olane/o-core';
|
|
5
5
|
import type { oRouterRequest, oConnection } from '@olane/o-core';
|
|
6
6
|
import { oNodeStream } from './o-node-stream.js';
|
|
7
7
|
import { StreamManagerConfig } from './interfaces/stream-manager.config.js';
|
|
@@ -21,10 +21,13 @@ export declare class oNodeStreamManager extends oObject {
|
|
|
21
21
|
readonly config: StreamManagerConfig;
|
|
22
22
|
private streams;
|
|
23
23
|
protected eventEmitter: EventEmitter;
|
|
24
|
-
|
|
24
|
+
isInitialized: boolean;
|
|
25
25
|
private p2pConnection;
|
|
26
26
|
private activeStreamHandlers;
|
|
27
27
|
protected callerReaderStream?: Stream;
|
|
28
|
+
protected callerWriterStream?: Stream;
|
|
29
|
+
private streamMonitoringIntervals;
|
|
30
|
+
private id;
|
|
28
31
|
constructor(config: StreamManagerConfig);
|
|
29
32
|
/**
|
|
30
33
|
* Initialize the stream manager
|
|
@@ -65,6 +68,22 @@ export declare class oNodeStreamManager extends oObject {
|
|
|
65
68
|
* @returns Array of wrapped streams
|
|
66
69
|
*/
|
|
67
70
|
getAllStreams(): oNodeStream[];
|
|
71
|
+
/**
|
|
72
|
+
* Gets a stream by its ID
|
|
73
|
+
* Checks persistent caller streams (reader/writer) and tracked streams
|
|
74
|
+
*
|
|
75
|
+
* @param streamId - The ID of the stream to retrieve
|
|
76
|
+
* @returns The libp2p Stream or undefined if not found
|
|
77
|
+
*/
|
|
78
|
+
getStreamById(streamId: string): Stream | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Sets up monitoring for stream closure and emits events when detected
|
|
81
|
+
* Periodically checks stream status and cleans up when stream closes
|
|
82
|
+
*
|
|
83
|
+
* @param stream - The stream to monitor
|
|
84
|
+
* @param role - The role of the stream ('reader' or 'writer')
|
|
85
|
+
*/
|
|
86
|
+
private setupStreamCloseMonitoring;
|
|
68
87
|
/**
|
|
69
88
|
* Emits an async event and waits for the first listener to return a result
|
|
70
89
|
* This enables event-based request handling with async responses
|
|
@@ -88,11 +107,12 @@ export declare class oNodeStreamManager extends oObject {
|
|
|
88
107
|
/**
|
|
89
108
|
* Handles a stream initialization message
|
|
90
109
|
* Stores reference to caller's reader stream for bidirectional communication
|
|
110
|
+
* Sends acknowledgment back to confirm stream registration
|
|
91
111
|
*
|
|
92
112
|
* @param message - The decoded stream init message
|
|
93
113
|
* @param stream - The stream that sent the message
|
|
94
114
|
*/
|
|
95
|
-
protected handleStreamInitMessage(message: StreamInitMessage, stream: Stream): void
|
|
115
|
+
protected handleStreamInitMessage(message: StreamInitMessage, stream: Stream): Promise<void>;
|
|
96
116
|
/**
|
|
97
117
|
* Extracts and parses JSON from various formats including:
|
|
98
118
|
* - Already parsed objects
|
|
@@ -105,7 +125,7 @@ export declare class oNodeStreamManager extends oObject {
|
|
|
105
125
|
* @returns Parsed JSON object
|
|
106
126
|
* @throws Error if JSON parsing fails even with JSON5 fallback
|
|
107
127
|
*/
|
|
108
|
-
|
|
128
|
+
protected extractAndParseJSON(decoded: string | any): any;
|
|
109
129
|
/**
|
|
110
130
|
* Sends data through a stream using length-prefixed encoding (libp2p v3 best practice)
|
|
111
131
|
* Each message is automatically prefixed with a varint indicating the message length
|
|
@@ -133,6 +153,15 @@ export declare class oNodeStreamManager extends oObject {
|
|
|
133
153
|
* @param connection - The connection the stream belongs to
|
|
134
154
|
*/
|
|
135
155
|
handleIncomingStream(stream: Stream, connection: Connection): Promise<void>;
|
|
156
|
+
/**
|
|
157
|
+
* Determines which stream to use for sending the response
|
|
158
|
+
* Checks for _streamId in request params and routes accordingly
|
|
159
|
+
*
|
|
160
|
+
* @param request - The incoming request
|
|
161
|
+
* @param defaultStream - The stream the request came on (fallback)
|
|
162
|
+
* @returns The stream to use for the response
|
|
163
|
+
*/
|
|
164
|
+
protected getResponseStream(request: oRequest, defaultStream: Stream): Stream;
|
|
136
165
|
/**
|
|
137
166
|
* Handles a request message by emitting an event and sending response
|
|
138
167
|
*
|
|
@@ -140,7 +169,7 @@ export declare class oNodeStreamManager extends oObject {
|
|
|
140
169
|
* @param stream - The stream to send the response on
|
|
141
170
|
* @param connection - The connection the stream belongs to
|
|
142
171
|
*/
|
|
143
|
-
|
|
172
|
+
protected handleRequestMessage(message: any, stream: Stream, connection: Connection): Promise<void>;
|
|
144
173
|
/**
|
|
145
174
|
* Handles an outgoing stream on the client side using length-prefixed protocol
|
|
146
175
|
* Uses async read loops to process responses with proper message boundaries
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o-node-stream.manager.d.ts","sourceRoot":"","sources":["../../../src/connection/o-node-stream.manager.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EACL,OAAO,
|
|
1
|
+
{"version":3,"file":"o-node-stream.manager.d.ts","sourceRoot":"","sources":["../../../src/connection/o-node-stream.manager.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EACL,OAAO,EAGP,QAAQ,EACR,SAAS,EAGV,MAAM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