@olane/o-node 0.7.12-alpha.24 → 0.7.12-alpha.26

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.
@@ -1 +1 @@
1
- {"version":3,"file":"o-node-connection.d.ts","sourceRoot":"","sources":["../../../src/connection/o-node-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EAIV,MAAM,EAEP,MAAM,iBAAiB,CAAC;AACzB,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;IAKtD,IAAI,CAAC,MAAM,EAAE,MAAM;IAWzB,QAAQ;IAOF,QAAQ,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,SAAS,CAAC;IA0E/C,KAAK;CAKZ"}
1
+ {"version":3,"file":"o-node-connection.d.ts","sourceRoot":"","sources":["../../../src/connection/o-node-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EAIV,MAAM,EAEP,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAEL,WAAW,EAGX,QAAQ,EACR,SAAS,EAEV,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;IAKtD,IAAI,CAAC,MAAM,EAAE,MAAM;IAWzB,QAAQ;IAOF,QAAQ,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,SAAS,CAAC;IA6E/C,KAAK;CAKZ"}
@@ -1,5 +1,5 @@
1
1
  import { Uint8ArrayList, byteStream, } from '@olane/o-config';
2
- import { CoreUtils, oConnection, oError, oErrorCodes, oResponse, } from '@olane/o-core';
2
+ import { CoreUtils, oConnection, oError, oErrorCodes, oResponse, ResponseBuilder, } from '@olane/o-core';
3
3
  export class oNodeConnection extends oConnection {
4
4
  constructor(config) {
5
5
  super(config);
@@ -27,7 +27,7 @@ export class oNodeConnection extends oConnection {
27
27
  maxOutboundStreams: Infinity,
28
28
  runOnLimitedConnection: true, // TODO: should this be configurable?
29
29
  });
30
- const isStreamRequest = request.params._isStream;
30
+ const isStreamRequest = this.config.isStream;
31
31
  if (!stream || (stream.status !== 'open' && stream.status !== 'reset')) {
32
32
  throw new oError(oErrorCodes.FAILED_TO_DIAL_TARGET, 'Failed to dial target');
33
33
  }
@@ -38,13 +38,19 @@ export class oNodeConnection extends oConnection {
38
38
  const data = new TextEncoder().encode(request.toString());
39
39
  const sent = stream.send(data);
40
40
  if (isStreamRequest) {
41
- stream.addEventListener('message', async (event) => {
42
- const response = await CoreUtils.processStreamResponse(event);
43
- this.emitter.emit('chunk', response);
44
- // marked as the last chunk let's close
45
- if (response.result._last) {
46
- await stream.close();
47
- }
41
+ this.logger.debug('Detected stream request, attaching message listener');
42
+ await new Promise((resolve, reject) => {
43
+ // TODO: add timeout
44
+ stream.addEventListener('message', async (event) => {
45
+ const response = await CoreUtils.processStreamResponse(event);
46
+ this.emitter.emit('chunk', response);
47
+ // marked as the last chunk let's close
48
+ if (response.result._last) {
49
+ this.logger.debug('Last chunk received, closing stream');
50
+ await stream.close();
51
+ resolve(true);
52
+ }
53
+ });
48
54
  });
49
55
  }
50
56
  // If send() returns false, wait for the stream to drain before continuing
@@ -55,7 +61,8 @@ export class oNodeConnection extends oConnection {
55
61
  }); // Default: 30 second timeout
56
62
  }
57
63
  if (isStreamRequest) {
58
- return Promise.resolve(CoreUtils.buildResponse(request, 'request is streaming, this response is not used', null));
64
+ const responseBuilder = ResponseBuilder.create();
65
+ return Promise.resolve(await responseBuilder.buildFinalChunk(request));
59
66
  }
60
67
  const res = await this.read(stream);
61
68
  await stream.close();
@@ -3,7 +3,7 @@ import { PeerId } from '@olane/o-config';
3
3
  import { oNodeHierarchyManager } from './o-node.hierarchy-manager.js';
4
4
  import { oNodeConfig } from './interfaces/o-node.config.js';
5
5
  import { oNodeTransport } from './router/o-node.transport.js';
6
- import { oAddress, oRequest, oNotificationManager } from '@olane/o-core';
6
+ import { oAddress, oRequest, oNotificationManager, UseOptions } from '@olane/o-core';
7
7
  import { oNodeAddress } from './router/o-node.address.js';
8
8
  import { oNodeConnection } from './connection/o-node-connection.js';
9
9
  import { oNodeConnectionManager } from './connection/o-node-connection.manager.js';
@@ -61,9 +61,7 @@ export declare class oNode extends oToolBase {
61
61
  [key: string]: any;
62
62
  };
63
63
  id?: string;
64
- }, options?: {
65
- noRouting?: boolean;
66
- }): Promise<any>;
64
+ }, options?: UseOptions): Promise<any>;
67
65
  teardown(): Promise<void>;
68
66
  getLeaders(): oNodeAddress[];
69
67
  getParents(): oNodeAddress[];
@@ -1 +1 @@
1
- {"version":3,"file":"o-node.d.ts","sourceRoot":"","sources":["../../src/o-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,MAAM,EACN,YAAY,EACb,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAIL,QAAQ,EACR,QAAQ,EAER,oBAAoB,EAErB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACpE,O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AE,CAAC;QAChC,EAAE,CAAC,EAAE,MAAM,CAAC;KACb,EACD,OAAO,CAAC,EAAE,UAAU,GACnB,OAAO,CAAC,GAAG,CAAC;IAST,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAc/B,UAAU,IAAI,YAAY,EAAE;IAI5B,UAAU,IAAI,YAAY,EAAE;IAI5B,WAAW,IAAI,YAAY,EAAE;IAI7B,WAAW,CAAC,YAAY,EAAE,YAAY,GAAG,IAAI;CAG9C"}
@@ -123,7 +123,7 @@ export class oNode extends oToolBase {
123
123
  // if no parent transports, register with the parent to get them
124
124
  // TODO: should we remove the transports check to make this more consistent?
125
125
  if (this.config.parent) {
126
- this.logger.debug('Registering node with parent...', this.config.parent);
126
+ this.logger.debug('Registering node with parent...', this.config.parent?.toString());
127
127
  await this.use(this.config.parent, {
128
128
  method: 'child_register',
129
129
  params: {
@@ -197,6 +197,7 @@ export class oNode extends oToolBase {
197
197
  listeners: (this.config.network?.listeners ||
198
198
  defaultLibp2pConfig.listeners ||
199
199
  []).concat(`/memory/${uuidv4()}`), // ensure we allow for local in-memory communication
200
+ prometheusRegistry: this.config.network?.prometheusRegistry,
200
201
  };
201
202
  // if the seed is provided, use it to generate the private key
202
203
  if (this.config.seed) {
@@ -346,7 +347,7 @@ export class oNode extends oToolBase {
346
347
  await this.initializeRouter();
347
348
  // need to wait until our libpp2 node is initialized before calling super.initialize
348
349
  await super.initialize();
349
- this.logger.debug('Node initializedddd!', this.transports.map((t) => t.toString()));
350
+ this.logger.debug('Node initialized!', this.transports.map((t) => t.toString()));
350
351
  this.address.setTransports(this.transports);
351
352
  this.peerId = this.p2pNode.peerId;
352
353
  // initialize connection manager
@@ -385,7 +386,7 @@ export class oNode extends oToolBase {
385
386
  */
386
387
  async use(address, data, options) {
387
388
  // Wrap leader/registry requests with retry logic
388
- return this.leaderRequestWrapper.execute(() => super.use(address, data, { noRouting: options?.noRouting ?? false }), address, data?.method);
389
+ return this.leaderRequestWrapper.execute(() => super.use(address, data, options), address, data?.method);
389
390
  }
390
391
  async teardown() {
391
392
  // Stop heartbeat before parent teardown
@@ -1 +1 @@
1
- {"version":3,"file":"o-node.tool.d.ts","sourceRoot":"","sources":["../../src/o-node.tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,QAAQ,EAGR,QAAQ,EAGT,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;;AAIrD;;;;GAIG;AACH,qBAAa,SAAU,SAAQ,cAAkB;IACzC,cAAc,CAAC,OAAO,EAAE,QAAQ;IAQhC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAW3B,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAoDnE,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC;IAQ9B,oBAAoB,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC;CA2B5D"}
1
+ {"version":3,"file":"o-node.tool.d.ts","sourceRoot":"","sources":["../../src/o-node.tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,QAAQ,EAGR,QAAQ,EAIT,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;;AAIrD;;;;GAIG;AACH,qBAAa,SAAU,SAAQ,cAAkB;IACzC,cAAc,CAAC,OAAO,EAAE,QAAQ;IAQhC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAW3B,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAuCnE,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC;IAQ9B,oBAAoB,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC;CA2B5D"}
@@ -1,4 +1,4 @@
1
- import { CoreUtils, oError, oErrorCodes, oRequest, ChildJoinedEvent, } from '@olane/o-core';
1
+ import { CoreUtils, oRequest, ResponseBuilder, ChildJoinedEvent, } from '@olane/o-core';
2
2
  import { oTool } from '@olane/o-tool';
3
3
  import { oServerNode } from './nodes/server.node.js';
4
4
  import { oNodeTransport } from './router/o-node.transport.js';
@@ -33,29 +33,20 @@ export class oNodeTool extends oTool(oServerNode) {
33
33
  this.logger.warn('Malformed event data');
34
34
  return;
35
35
  }
36
- const requestConfig = await CoreUtils.processStreamRequest(event);
36
+ const requestConfig = await CoreUtils.processStream(event);
37
37
  const request = new oRequest(requestConfig);
38
- let success = true;
39
- const result = await this.execute(request, stream).catch((error) => {
40
- this.logger.error('Error executing tool: ', request.toString(), error, typeof error);
41
- success = false;
42
- const responseError = error instanceof oError
43
- ? error
44
- : new oError(oErrorCodes.UNKNOWN, error.message);
45
- return {
46
- error: responseError.toJSON(),
47
- };
48
- });
49
- // Non-streaming response - original behavior
50
- if (success) {
51
- this.metrics.successCount++;
38
+ // Use ResponseBuilder with automatic error handling and metrics tracking
39
+ const responseBuilder = ResponseBuilder.create().withMetrics(this.metrics);
40
+ let response;
41
+ try {
42
+ const result = await this.execute(request, stream);
43
+ response = await responseBuilder.build(request, result, null);
52
44
  }
53
- else {
54
- this.metrics.errorCount++;
45
+ catch (error) {
46
+ this.logger.error('Error executing tool: ', request.toString(), error, typeof error);
47
+ response = await responseBuilder.buildError(request, error);
55
48
  }
56
- // compose the response & add the expected connection + request fields
57
- const response = CoreUtils.buildResponse(request, result, result?.error);
58
- // add the request method to the response
49
+ // Send the response
59
50
  await CoreUtils.sendResponse(response, stream);
60
51
  };
61
52
  // Attach listener synchronously before any async operations
@@ -17,6 +17,7 @@ export declare class oNodeRouter extends oToolRouter {
17
17
  protected forward(address: oNodeAddress, request: oRouterRequest, node: oNode): Promise<any>;
18
18
  /**
19
19
  * Executes a request locally when routing to self.
20
+ * Now uses ResponseBuilder for consistency with useSelf() behavior.
20
21
  */
21
22
  private executeSelfRouting;
22
23
  /**
@@ -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,EACd,aAAa,EACd,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAM5C,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;IA0Bf;;OAEG;YACW,kBAAkB;IAgBhC;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAS5B;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAUhC;;OAEG;YACW,eAAe;IAsC7B;;;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"}
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;IA0C7B;;;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"}
@@ -1,7 +1,8 @@
1
- import { CoreUtils, oAddress, oError, oErrorCodes, oRequest, } from '@olane/o-core';
1
+ import { CoreUtils, oAddress, oError, oErrorCodes, oRequest, ResponseBuilder, } from '@olane/o-core';
2
2
  import { oToolRouter } from '@olane/o-tool';
3
3
  import { oNodeConnection } from '../connection/o-node-connection.js';
4
4
  import { oNodeRoutingPolicy } from './o-node.routing-policy.js';
5
+ import { oStreamRequest } from '../connection/o-stream.request.js';
5
6
  export class oNodeRouter extends oToolRouter {
6
7
  constructor() {
7
8
  super();
@@ -25,6 +26,9 @@ export class oNodeRouter extends oToolRouter {
25
26
  params: request.params,
26
27
  id: request.id,
27
28
  });
29
+ if (request.stream) {
30
+ nextHopRequest.stream = request.stream;
31
+ }
28
32
  // Handle self-routing: execute locally instead of dialing
29
33
  if (this.routingPolicy.isSelfAddress(address, node)) {
30
34
  return this.executeSelfRouting(request, node);
@@ -39,17 +43,55 @@ export class oNodeRouter extends oToolRouter {
39
43
  }
40
44
  /**
41
45
  * Executes a request locally when routing to self.
46
+ * Now uses ResponseBuilder for consistency with useSelf() behavior.
42
47
  */
43
48
  async executeSelfRouting(request, node) {
44
49
  const { payload } = request.params;
45
50
  const params = payload.params;
46
51
  const localRequest = new oRequest({
47
52
  method: payload.method,
48
- params: { ...params },
53
+ params: {
54
+ ...params,
55
+ _connectionId: request.params._connectionId,
56
+ _requestMethod: payload.method,
57
+ },
49
58
  id: request.id,
50
59
  });
51
- const result = await node.execute(localRequest);
52
- return result;
60
+ // Create ResponseBuilder with metrics tracking
61
+ const responseBuilder = ResponseBuilder.create().withMetrics(node.metrics);
62
+ // Handle streaming requests
63
+ const isStream = request.params._isStream;
64
+ if (isStream && request.stream) {
65
+ // For streaming, we need to handle the stream chunks
66
+ try {
67
+ const result = await node.execute(localRequest, request.stream);
68
+ const response = await responseBuilder.build(localRequest, result, null, {
69
+ isStream: true,
70
+ });
71
+ // Return unwrapped data for consistency with dialAndTransmit
72
+ return response.result.data;
73
+ }
74
+ catch (error) {
75
+ const errorResponse = await responseBuilder.buildError(localRequest, error, {
76
+ isStream: true,
77
+ });
78
+ // For errors, throw to match remote behavior
79
+ throw responseBuilder.normalizeError(error);
80
+ }
81
+ }
82
+ // Handle non-streaming requests with error handling
83
+ try {
84
+ const result = await node.execute(localRequest);
85
+ const response = await responseBuilder.build(localRequest, result, null);
86
+ // Return unwrapped data to match dialAndTransmit behavior
87
+ return response.result.data;
88
+ }
89
+ catch (error) {
90
+ // Build error response for metrics tracking
91
+ await responseBuilder.buildError(localRequest, error);
92
+ // Then throw the normalized error
93
+ throw responseBuilder.normalizeError(error);
94
+ }
53
95
  }
54
96
  /**
55
97
  * Checks if the next hop is the final destination address.
@@ -65,10 +107,11 @@ export class oNodeRouter extends oToolRouter {
65
107
  unwrapDestinationRequest(request) {
66
108
  const { payload } = request.params;
67
109
  const params = payload.params;
68
- return new oRequest({
110
+ return new oStreamRequest({
69
111
  method: payload.method,
70
112
  params: { ...params },
71
113
  id: request.id,
114
+ stream: request.stream,
72
115
  });
73
116
  }
74
117
  /**
@@ -76,14 +119,17 @@ export class oNodeRouter extends oToolRouter {
76
119
  */
77
120
  async dialAndTransmit(address, request, node) {
78
121
  try {
122
+ const isStream = request.params._isStream ||
123
+ request.params.payload?.params?._isStream;
79
124
  const connection = await node.p2pNode.dial(address.libp2pTransports.map((t) => t.toMultiaddr()));
80
125
  const nodeConnection = new oNodeConnection({
81
126
  p2pConnection: connection,
82
127
  nextHopAddress: address,
83
128
  address: node.address,
84
129
  callerAddress: node.address,
130
+ isStream: isStream,
85
131
  });
86
- if (request.params._isStream) {
132
+ if (isStream) {
87
133
  const routeRequest = request;
88
134
  if (!routeRequest.stream) {
89
135
  throw new oError(oErrorCodes.INVALID_REQUEST, 'Stream is required');
@@ -1 +1 @@
1
- {"version":3,"file":"o-node.resolver.d.ts","sourceRoot":"","sources":["../../../../src/router/resolvers/o-node.resolver.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAEhB,aAAa,EAEd,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,qBAAa,aAAc,SAAQ,gBAAgB;IACrC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY;gBAArB,OAAO,EAAE,YAAY;IAIpD,IAAI,mBAAmB,IAAI,aAAa,EAAE,CAEzC;IAEK,OAAO,CAAC,YAAY,EAAE,cAAc,GAAG,OAAO,CAAC,mBAAmB,CAAC;CAsC1E"}
1
+ {"version":3,"file":"o-node.resolver.d.ts","sourceRoot":"","sources":["../../../../src/router/resolvers/o-node.resolver.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAEhB,aAAa,EAId,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,qBAAa,aAAc,SAAQ,gBAAgB;IACrC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY;gBAArB,OAAO,EAAE,YAAY;IAIpD,IAAI,mBAAmB,IAAI,aAAa,EAAE,CAEzC;IAEK,OAAO,CAAC,YAAY,EAAE,cAAc,GAAG,OAAO,CAAC,mBAAmB,CAAC;CA8C1E"}
@@ -1,4 +1,4 @@
1
- import { oAddressResolver, TransportType, } from '@olane/o-core';
1
+ import { oAddressResolver, oAddress, TransportType, oError, oErrorCodes, } from '@olane/o-core';
2
2
  import { oNodeAddress } from '../o-node.address.js';
3
3
  export class oNodeResolver extends oAddressResolver {
4
4
  constructor(address) {
@@ -32,6 +32,10 @@ export class oNodeResolver extends oAddressResolver {
32
32
  requestOverride: request,
33
33
  };
34
34
  }
35
+ // no child address, and we have already been to the leader, fail
36
+ if (address.toString().indexOf(oAddress.leader().toString()) > -1) {
37
+ throw new oError(oErrorCodes.NOT_FOUND, targetAddress.toString() + ' node not found.');
38
+ }
35
39
  return {
36
40
  nextHopAddress: address,
37
41
  targetAddress: targetAddress,
@@ -1 +1 @@
1
- {"version":3,"file":"stream.utils.d.ts","sourceRoot":"","sources":["../../../src/utils/stream.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAa,OAAO,EAAE,QAAQ,EAAa,MAAM,eAAe,CAAC;AAExE,qBAAa,WAAY,SAAQ,OAAO;WAClB,gBAAgB,CAClC,OAAO,EAAE,QAAQ,EACjB,SAAS,EAAE,cAAc,CAAC,GAAG,CAAC,EAC9B,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,GAAG,CAAC;CA8BhB"}
1
+ {"version":3,"file":"stream.utils.d.ts","sourceRoot":"","sources":["../../../src/utils/stream.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAEL,OAAO,EACP,QAAQ,EAGT,MAAM,eAAe,CAAC;AAEvB,qBAAa,WAAY,SAAQ,OAAO;WAClB,gBAAgB,CAClC,OAAO,EAAE,QAAQ,EACjB,SAAS,EAAE,cAAc,CAAC,GAAG,CAAC,EAC9B,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,GAAG,CAAC;CA6BhB"}
@@ -1,27 +1,30 @@
1
- import { CoreUtils, oObject, oResponse } from '@olane/o-core';
1
+ import { CoreUtils, oObject, ResponseBuilder, } from '@olane/o-core';
2
2
  export class StreamUtils extends oObject {
3
3
  static async processGenerator(request, generator, stream) {
4
4
  const utils = new StreamUtils();
5
- for await (const result of generator) {
6
- utils.logger.debug('Sending stream response: ', result);
7
- await CoreUtils.sendStreamResponse(new oResponse({
8
- id: request.id,
9
- data: result,
10
- _last: false,
11
- _requestMethod: request.method,
12
- _connectionId: request.params?._connectionId,
13
- }), stream);
5
+ const responseBuilder = ResponseBuilder.create();
6
+ try {
7
+ // Send each chunk from the generator
8
+ for await (const result of generator) {
9
+ const chunkResponse = await responseBuilder.buildChunk(request, result);
10
+ await CoreUtils.sendStreamResponse(chunkResponse, stream);
11
+ }
12
+ // Send final chunk
13
+ const finalResponse = await responseBuilder.buildFinalChunk(request);
14
+ await CoreUtils.sendStreamResponse(finalResponse, stream);
15
+ return Promise.resolve({
16
+ success: true,
17
+ response: 'Stream completed',
18
+ });
19
+ }
20
+ catch (error) {
21
+ // If error occurs during streaming, send error response
22
+ const errorResponse = await responseBuilder.buildError(request, error, {
23
+ isStream: true,
24
+ isLast: true,
25
+ });
26
+ await CoreUtils.sendStreamResponse(errorResponse, stream);
27
+ throw error;
14
28
  }
15
- await CoreUtils.sendStreamResponse(new oResponse({
16
- id: request.id,
17
- data: 'Stream completed',
18
- _last: true,
19
- _requestMethod: request.method,
20
- _connectionId: request.params?._connectionId,
21
- }), stream);
22
- return Promise.resolve({
23
- success: true,
24
- response: 'Stream started',
25
- });
26
29
  }
27
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olane/o-node",
3
- "version": "0.7.12-alpha.24",
3
+ "version": "0.7.12-alpha.26",
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.24",
58
- "@olane/o-core": "0.7.12-alpha.24",
59
- "@olane/o-protocol": "0.7.12-alpha.24",
60
- "@olane/o-tool": "0.7.12-alpha.24",
57
+ "@olane/o-config": "0.7.12-alpha.26",
58
+ "@olane/o-core": "0.7.12-alpha.26",
59
+ "@olane/o-protocol": "0.7.12-alpha.26",
60
+ "@olane/o-tool": "0.7.12-alpha.26",
61
61
  "debug": "^4.4.1",
62
62
  "dotenv": "^16.5.0"
63
63
  },
64
- "gitHead": "ac75e0acc3a0a232a5cdeb9271060d90cf1e5a2c"
64
+ "gitHead": "d0436ce9eed9cad0e64561cf0428b0e791c57058"
65
65
  }