@olane/o-node 0.7.12-alpha.29 → 0.7.12-alpha.30
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/o-node-connection.d.ts +1 -2
- package/dist/src/connection/o-node-connection.d.ts.map +1 -1
- package/dist/src/connection/o-node-connection.js +15 -37
- package/dist/src/connection/o-node-connection.manager.js +1 -1
- package/dist/src/router/o-node.router.d.ts.map +1 -1
- package/dist/src/router/o-node.router.js +4 -4
- package/dist/src/utils/stream.utils.js +8 -7
- package/package.json +6 -6
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Connection
|
|
1
|
+
import { Connection } from '@olane/o-config';
|
|
2
2
|
import { oConnection, oRequest, oResponse } from '@olane/o-core';
|
|
3
3
|
import { oNodeConnectionConfig } from './interfaces/o-node-connection.config.js';
|
|
4
4
|
export declare class oNodeConnection extends oConnection {
|
|
5
5
|
protected readonly config: oNodeConnectionConfig;
|
|
6
6
|
p2pConnection: Connection;
|
|
7
7
|
constructor(config: oNodeConnectionConfig);
|
|
8
|
-
read(source: Stream): Promise<any>;
|
|
9
8
|
validate(): void;
|
|
10
9
|
transmit(request: oRequest): Promise<oResponse>;
|
|
11
10
|
close(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o-node-connection.d.ts","sourceRoot":"","sources":["../../../src/connection/o-node-connection.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"o-node-connection.d.ts","sourceRoot":"","sources":["../../../src/connection/o-node-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAEL,WAAW,EAGX,QAAQ,EACR,SAAS,EACV,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAC;AAEjF,qBAAa,eAAgB,SAAQ,WAAW;IAGlC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,qBAAqB;IAFrD,aAAa,EAAE,UAAU,CAAC;gBAEF,MAAM,EAAE,qBAAqB;IAK5D,QAAQ;IAOF,QAAQ,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,SAAS,CAAC;IA6D/C,KAAK;CAKZ"}
|
|
@@ -1,20 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CoreUtils, oConnection, oError, oErrorCodes, oResponse, ResponseBuilder, } from '@olane/o-core';
|
|
1
|
+
import { CoreUtils, oConnection, oError, oErrorCodes, oResponse, } from '@olane/o-core';
|
|
3
2
|
export class oNodeConnection extends oConnection {
|
|
4
3
|
constructor(config) {
|
|
5
4
|
super(config);
|
|
6
5
|
this.config = config;
|
|
7
6
|
this.p2pConnection = config.p2pConnection;
|
|
8
7
|
}
|
|
9
|
-
async read(source) {
|
|
10
|
-
const bytes = byteStream(source);
|
|
11
|
-
const output = await bytes.read({
|
|
12
|
-
signal: AbortSignal.timeout(this.config.readTimeoutMs ?? 120000), // Default: 2 min timeout
|
|
13
|
-
});
|
|
14
|
-
const outputObj = output instanceof Uint8ArrayList ? output.subarray() : output;
|
|
15
|
-
const jsonStr = new TextDecoder().decode(outputObj);
|
|
16
|
-
return JSON.parse(jsonStr);
|
|
17
|
-
}
|
|
18
8
|
validate() {
|
|
19
9
|
if (this.config.p2pConnection.status !== 'open') {
|
|
20
10
|
throw new Error('Connection is not valid');
|
|
@@ -27,7 +17,6 @@ export class oNodeConnection extends oConnection {
|
|
|
27
17
|
maxOutboundStreams: Infinity,
|
|
28
18
|
runOnLimitedConnection: true, // TODO: should this be configurable?
|
|
29
19
|
});
|
|
30
|
-
const isStreamRequest = this.config.isStream;
|
|
31
20
|
if (!stream || (stream.status !== 'open' && stream.status !== 'reset')) {
|
|
32
21
|
throw new oError(oErrorCodes.FAILED_TO_DIAL_TARGET, 'Failed to dial target');
|
|
33
22
|
}
|
|
@@ -37,22 +26,20 @@ export class oNodeConnection extends oConnection {
|
|
|
37
26
|
// Send the data with backpressure handling (libp2p v3 best practice)
|
|
38
27
|
const data = new TextEncoder().encode(request.toString());
|
|
39
28
|
const sent = stream.send(data);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
});
|
|
29
|
+
let lastResponse;
|
|
30
|
+
await new Promise((resolve, reject) => {
|
|
31
|
+
// TODO: add timeout
|
|
32
|
+
stream.addEventListener('message', async (event) => {
|
|
33
|
+
const response = await CoreUtils.processStreamResponse(event);
|
|
34
|
+
this.emitter.emit('chunk', response);
|
|
35
|
+
// marked as the last chunk let's close
|
|
36
|
+
if (response.result._last || !response.result._isStreaming) {
|
|
37
|
+
lastResponse = response;
|
|
38
|
+
await stream.close();
|
|
39
|
+
resolve(true);
|
|
40
|
+
}
|
|
54
41
|
});
|
|
55
|
-
}
|
|
42
|
+
});
|
|
56
43
|
// If send() returns false, wait for the stream to drain before continuing
|
|
57
44
|
if (!sent) {
|
|
58
45
|
this.logger.debug('Stream buffer full, waiting for drain...');
|
|
@@ -60,16 +47,7 @@ export class oNodeConnection extends oConnection {
|
|
|
60
47
|
signal: AbortSignal.timeout(this.config.drainTimeoutMs ?? 30000),
|
|
61
48
|
}); // Default: 30 second timeout
|
|
62
49
|
}
|
|
63
|
-
|
|
64
|
-
const responseBuilder = ResponseBuilder.create();
|
|
65
|
-
return Promise.resolve(await responseBuilder.buildFinalChunk(request));
|
|
66
|
-
}
|
|
67
|
-
const res = await this.read(stream);
|
|
68
|
-
await stream.close();
|
|
69
|
-
// process the response
|
|
70
|
-
const response = new oResponse({
|
|
71
|
-
...res.result,
|
|
72
|
-
});
|
|
50
|
+
const response = oResponse.fromJSON(lastResponse);
|
|
73
51
|
return response;
|
|
74
52
|
}
|
|
75
53
|
catch (error) {
|
|
@@ -50,7 +50,7 @@ export class oNodeConnectionManager extends oConnectionManager {
|
|
|
50
50
|
callerAddress: callerAddress,
|
|
51
51
|
readTimeoutMs: readTimeoutMs ?? this.defaultReadTimeoutMs,
|
|
52
52
|
drainTimeoutMs: drainTimeoutMs ?? this.defaultDrainTimeoutMs,
|
|
53
|
-
isStream: config.isStream
|
|
53
|
+
isStream: config.isStream ?? false,
|
|
54
54
|
});
|
|
55
55
|
if (attempt > 0) {
|
|
56
56
|
this.logger.info(`Successfully connected to ${nextHopAddress.toString()} on retry attempt ${attempt}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o-node.router.d.ts","sourceRoot":"","sources":["../../../src/router/o-node.router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAML,cAAc,EAGd,aAAa,EACd,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAO5C,qBAAa,WAAY,SAAQ,WAAW;IAC1C,OAAO,CAAC,aAAa,CAAqB;;IAO1C;;;;;;;;OAQG;cACa,OAAO,CACrB,OAAO,EAAE,YAAY,EACrB,OAAO,EAAE,cAAc,EACvB,IAAI,EAAE,KAAK,GACV,OAAO,CAAC,GAAG,CAAC;IA6Bf;;;OAGG;YACW,kBAAkB;IA8DhC;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAS5B;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAWhC;;OAEG;YACW,eAAe;
|
|
1
|
+
{"version":3,"file":"o-node.router.d.ts","sourceRoot":"","sources":["../../../src/router/o-node.router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAML,cAAc,EAGd,aAAa,EACd,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAO5C,qBAAa,WAAY,SAAQ,WAAW;IAC1C,OAAO,CAAC,aAAa,CAAqB;;IAO1C;;;;;;;;OAQG;cACa,OAAO,CACrB,OAAO,EAAE,YAAY,EACrB,OAAO,EAAE,cAAc,EACvB,IAAI,EAAE,KAAK,GACV,OAAO,CAAC,GAAG,CAAC;IA6Bf;;;OAGG;YACW,kBAAkB;IA8DhC;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAS5B;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAWhC;;OAEG;YACW,eAAe;IA2C7B;;;OAGG;IACG,SAAS,CAAC,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,GAAG,OAAO,CAAC,aAAa,CAAC;IAyB3E;;;OAGG;IACH,UAAU,CAAC,qBAAqB,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,GAAG,OAAO;CAGtE"}
|
|
@@ -60,7 +60,7 @@ export class oNodeRouter extends oToolRouter {
|
|
|
60
60
|
// Create ResponseBuilder with metrics tracking
|
|
61
61
|
const responseBuilder = ResponseBuilder.create().withMetrics(node.metrics);
|
|
62
62
|
// Handle streaming requests
|
|
63
|
-
const isStream = request.params.
|
|
63
|
+
const isStream = request.params._isStreaming;
|
|
64
64
|
if (isStream && request.stream) {
|
|
65
65
|
// For streaming, we need to handle the stream chunks
|
|
66
66
|
try {
|
|
@@ -119,8 +119,8 @@ export class oNodeRouter extends oToolRouter {
|
|
|
119
119
|
*/
|
|
120
120
|
async dialAndTransmit(address, request, node) {
|
|
121
121
|
try {
|
|
122
|
-
const isStream = request.params.
|
|
123
|
-
request.params.payload?.params?.
|
|
122
|
+
const isStream = request.params._isStreaming ||
|
|
123
|
+
request.params.payload?.params?._isStreaming;
|
|
124
124
|
const connection = await node.p2pNode.dial(address.libp2pTransports.map((t) => t.toMultiaddr()));
|
|
125
125
|
const nodeConnection = new oNodeConnection({
|
|
126
126
|
p2pConnection: connection,
|
|
@@ -134,7 +134,7 @@ export class oNodeRouter extends oToolRouter {
|
|
|
134
134
|
if (!routeRequest.stream) {
|
|
135
135
|
throw new oError(oErrorCodes.INVALID_REQUEST, 'Stream is required');
|
|
136
136
|
}
|
|
137
|
-
nodeConnection.onChunk((response) => {
|
|
137
|
+
nodeConnection.onChunk(async (response) => {
|
|
138
138
|
CoreUtils.sendStreamResponse(response, routeRequest.stream);
|
|
139
139
|
});
|
|
140
140
|
// allow this to continue as we will tell the transmitter to stream the response and we will intercept via the above listener
|
|
@@ -3,19 +3,20 @@ export class StreamUtils extends oObject {
|
|
|
3
3
|
static async processGenerator(request, generator, stream) {
|
|
4
4
|
const utils = new StreamUtils();
|
|
5
5
|
const responseBuilder = ResponseBuilder.create();
|
|
6
|
+
let aggregatedResult = '';
|
|
6
7
|
try {
|
|
7
8
|
// Send each chunk from the generator
|
|
9
|
+
// result should not be an oResponse, but rather a key value pair dict
|
|
8
10
|
for await (const result of generator) {
|
|
11
|
+
if (result.delta) {
|
|
12
|
+
aggregatedResult += result.delta;
|
|
13
|
+
}
|
|
9
14
|
const chunkResponse = await responseBuilder.buildChunk(request, result);
|
|
10
15
|
await CoreUtils.sendStreamResponse(chunkResponse, stream);
|
|
11
16
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return Promise.resolve({
|
|
16
|
-
success: true,
|
|
17
|
-
response: 'Stream completed',
|
|
18
|
-
});
|
|
17
|
+
return {
|
|
18
|
+
message: aggregatedResult,
|
|
19
|
+
};
|
|
19
20
|
}
|
|
20
21
|
catch (error) {
|
|
21
22
|
// If error occurs during streaming, send error response
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olane/o-node",
|
|
3
|
-
"version": "0.7.12-alpha.
|
|
3
|
+
"version": "0.7.12-alpha.30",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"types": "dist/src/index.d.ts",
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
"typescript": "5.4.5"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@olane/o-config": "0.7.12-alpha.
|
|
58
|
-
"@olane/o-core": "0.7.12-alpha.
|
|
59
|
-
"@olane/o-protocol": "0.7.12-alpha.
|
|
60
|
-
"@olane/o-tool": "0.7.12-alpha.
|
|
57
|
+
"@olane/o-config": "0.7.12-alpha.30",
|
|
58
|
+
"@olane/o-core": "0.7.12-alpha.30",
|
|
59
|
+
"@olane/o-protocol": "0.7.12-alpha.30",
|
|
60
|
+
"@olane/o-tool": "0.7.12-alpha.30",
|
|
61
61
|
"debug": "^4.4.1",
|
|
62
62
|
"dotenv": "^16.5.0"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "066e6bda19e79a744779a797664481476b0ea655"
|
|
65
65
|
}
|