@olane/o-core 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.
- package/dist/src/connection/o-response.d.ts +1 -0
- package/dist/src/connection/o-response.d.ts.map +1 -1
- package/dist/src/connection/o-response.js +7 -0
- package/dist/src/core/interfaces/index.d.ts +3 -0
- package/dist/src/core/interfaces/index.d.ts.map +1 -1
- package/dist/src/core/interfaces/index.js +3 -0
- package/dist/src/core/interfaces/use-options.interface.d.ts +9 -0
- package/dist/src/core/interfaces/use-options.interface.d.ts.map +1 -0
- package/dist/src/core/interfaces/use-options.interface.js +1 -0
- package/dist/src/core/interfaces/use-stream-options.interface.d.ts +4 -0
- package/dist/src/core/interfaces/use-stream-options.interface.d.ts.map +1 -0
- package/dist/src/core/interfaces/use-stream-options.interface.js +1 -0
- package/dist/src/core/o-core.d.ts +15 -10
- package/dist/src/core/o-core.d.ts.map +1 -1
- package/dist/src/core/o-core.js +44 -33
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/response/index.d.ts +2 -0
- package/dist/src/response/index.d.ts.map +1 -0
- package/dist/src/response/index.js +1 -0
- package/dist/src/response/response-builder.d.ts +140 -0
- package/dist/src/response/response-builder.d.ts.map +1 -0
- package/dist/src/response/response-builder.js +189 -0
- package/dist/src/utils/core.utils.d.ts +17 -1
- package/dist/src/utils/core.utils.d.ts.map +1 -1
- package/dist/src/utils/core.utils.js +23 -4
- package/dist/src/utils/logger.d.ts +1 -0
- package/dist/src/utils/logger.d.ts.map +1 -1
- package/dist/src/utils/logger.js +3 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o-response.d.ts","sourceRoot":"","sources":["../../../src/connection/o-response.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,SAAS,EACT,SAAS,IAAI,QAAQ,EACrB,MAAM,EACP,MAAM,mBAAmB,CAAC;AAE3B,qBAAa,SAAU,YAAW,QAAQ;IACxC,OAAO,EAAE,OAAO,eAAe,CAAC;IAChC,EAAE,EAAE,SAAS,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;gBAEH,MAAM,EAAE,MAAM,GAAG;QAAE,EAAE,EAAE,SAAS,CAAA;KAAE;IAM9C,MAAM,IAAI,GAAG;IAQb,QAAQ,IAAI,MAAM;
|
|
1
|
+
{"version":3,"file":"o-response.d.ts","sourceRoot":"","sources":["../../../src/connection/o-response.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,SAAS,EACT,SAAS,IAAI,QAAQ,EACrB,MAAM,EACP,MAAM,mBAAmB,CAAC;AAE3B,qBAAa,SAAU,YAAW,QAAQ;IACxC,OAAO,EAAE,OAAO,eAAe,CAAC;IAChC,EAAE,EAAE,SAAS,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;gBAEH,MAAM,EAAE,MAAM,GAAG;QAAE,EAAE,EAAE,SAAS,CAAA;KAAE;IAM9C,MAAM,IAAI,GAAG;IAQb,QAAQ,IAAI,MAAM;IAIlB,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,GAAG,SAAS;CAOtC"}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export * from './o-core.config.js';
|
|
2
2
|
export * from './node-type.enum.js';
|
|
3
3
|
export * from './state.enum.js';
|
|
4
|
+
export * from './use-options.interface.js';
|
|
5
|
+
export * from './o-core.interface.js';
|
|
6
|
+
export * from './use-stream-options.interface.js';
|
|
4
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/core/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/core/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,uBAAuB,CAAC;AACtC,cAAc,mCAAmC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { oResponse } from '../../connection/o-response.js';
|
|
2
|
+
export interface UseOptions {
|
|
3
|
+
readTimeoutMs?: number;
|
|
4
|
+
drainTimeoutMs?: number;
|
|
5
|
+
isStream?: boolean;
|
|
6
|
+
onChunk?: (chunk: oResponse) => void;
|
|
7
|
+
noRouting?: boolean;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=use-options.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-options.interface.d.ts","sourceRoot":"","sources":["../../../../src/core/interfaces/use-options.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gCAAgC,CAAC;AAE3D,MAAM,WAAW,UAAU;IACzB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-stream-options.interface.d.ts","sourceRoot":"","sources":["../../../../src/core/interfaces/use-stream-options.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAErD,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC;CAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -17,6 +17,8 @@ import { oRequest } from '../connection/o-request.js';
|
|
|
17
17
|
import { oNotificationManager } from './lib/o-notification.manager.js';
|
|
18
18
|
import { oNotificationEvent, EventFilter, NotificationHandler, Subscription } from './lib/events/index.js';
|
|
19
19
|
import { oConnectionConfig } from '../connection/index.js';
|
|
20
|
+
import { UseOptions } from './interfaces/use-options.interface.js';
|
|
21
|
+
import { UseStreamOptions } from './interfaces/use-stream-options.interface.js';
|
|
20
22
|
export declare abstract class oCore extends oObject {
|
|
21
23
|
readonly config: oCoreConfig;
|
|
22
24
|
address: oAddress;
|
|
@@ -39,9 +41,7 @@ export declare abstract class oCore extends oObject {
|
|
|
39
41
|
[key: string]: any;
|
|
40
42
|
};
|
|
41
43
|
id?: string;
|
|
42
|
-
}, options:
|
|
43
|
-
onChunk?: (chunk: oResponse) => void;
|
|
44
|
-
}): Promise<oResponse>;
|
|
44
|
+
}, options: UseStreamOptions): Promise<oResponse>;
|
|
45
45
|
useDirect(address: oAddress, data?: {
|
|
46
46
|
method?: string;
|
|
47
47
|
params?: {
|
|
@@ -108,14 +108,19 @@ export declare abstract class oCore extends oObject {
|
|
|
108
108
|
[key: string]: any;
|
|
109
109
|
};
|
|
110
110
|
id?: string;
|
|
111
|
-
}, options?:
|
|
112
|
-
noRouting?: boolean;
|
|
113
|
-
readTimeoutMs?: number;
|
|
114
|
-
drainTimeoutMs?: number;
|
|
115
|
-
isStream?: boolean;
|
|
116
|
-
onChunk?: (chunk: oResponse) => void;
|
|
117
|
-
}): Promise<oResponse>;
|
|
111
|
+
}, options?: UseOptions): Promise<oResponse>;
|
|
118
112
|
abstract execute(request: oRequest): Promise<any>;
|
|
113
|
+
/**
|
|
114
|
+
* Helper method to handle response errors
|
|
115
|
+
* @param response The response to check for errors
|
|
116
|
+
* @throws oError if the response contains an error
|
|
117
|
+
*/
|
|
118
|
+
private handleResponseError;
|
|
119
|
+
/**
|
|
120
|
+
* Helper method to validate node is running
|
|
121
|
+
* @throws Error if node is not running
|
|
122
|
+
*/
|
|
123
|
+
private validateRunning;
|
|
119
124
|
useSelf(data?: {
|
|
120
125
|
method?: string;
|
|
121
126
|
params?: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o-core.d.ts","sourceRoot":"","sources":["../../../src/core/o-core.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAGhD,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"o-core.d.ts","sourceRoot":"","sources":["../../../src/core/o-core.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,4BA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,KAAK,EAAE,kBAAkB,GAAG,IAAI;IAMjD;;OAEG;IACH,SAAS,CAAC,cAAc,CACtB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,mBAAmB,EAC5B,MAAM,CAAC,EAAE,WAAW,GACnB,YAAY;IAQT,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAMjC,IAAI,SAAS,IAAI,OAAO,CAMvB;IAEK,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACU,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA8BnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACU,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAyBrB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAyBtC,IAAI,YAAY,IAAI,WAAW,EAAE,CAEhC;IAED;;;OAGG;IACH,OAAO,CAAC,cAAc;IAmCtB,IAAI,OAAO,IAAI;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAExC;IAED,IAAI,WAAW,IAAI,MAAM,CAExB;IAED,IAAI,aAAa,IAAI,QAAQ,CAE5B;IAED,IAAI,IAAI,IAAI,QAAQ,CAEnB;IAED,IAAI,UAAU,IAAI,UAAU,EAAE,CAE7B;IAED,IAAI,MAAM,IAAI,QAAQ,GAAG,IAAI,CAE5B;IAED,IAAI,gBAAgB,IAAI,UAAU,EAAE,CAEnC;IAEK,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC;CAQ7B"}
|
package/dist/src/core/o-core.js
CHANGED
|
@@ -7,9 +7,8 @@ import { oObject } from './o-object.js';
|
|
|
7
7
|
import { oMetrics } from './lib/o-metrics.js';
|
|
8
8
|
import { oHierarchyManager } from './lib/o-hierarchy.manager.js';
|
|
9
9
|
import { oRequestManager } from './lib/o-request.manager.js';
|
|
10
|
-
import { CoreUtils } from '../utils/core.utils.js';
|
|
11
|
-
import { oErrorCodes } from '../error/index.js';
|
|
12
10
|
import { oRequest } from '../connection/o-request.js';
|
|
11
|
+
import { ResponseBuilder } from '../response/response-builder.js';
|
|
13
12
|
export class oCore extends oObject {
|
|
14
13
|
constructor(config) {
|
|
15
14
|
super((config.name ? `:${config.name}` : '') + ':' + config.address.toString());
|
|
@@ -31,9 +30,15 @@ export class oCore extends oObject {
|
|
|
31
30
|
return this.isLeader ? this.address : this.config?.leader || null;
|
|
32
31
|
}
|
|
33
32
|
async useStream(address, data, options) {
|
|
34
|
-
return this.use(address,
|
|
33
|
+
return this.use(address, {
|
|
34
|
+
...data,
|
|
35
|
+
params: {
|
|
36
|
+
...data.params,
|
|
37
|
+
_isStream: true,
|
|
38
|
+
},
|
|
39
|
+
}, {
|
|
40
|
+
...options,
|
|
35
41
|
isStream: true,
|
|
36
|
-
onChunk: options.onChunk,
|
|
37
42
|
});
|
|
38
43
|
}
|
|
39
44
|
async useDirect(address, data) {
|
|
@@ -93,10 +98,7 @@ export class oCore extends oObject {
|
|
|
93
98
|
* ```
|
|
94
99
|
*/
|
|
95
100
|
async use(address, data, options) {
|
|
96
|
-
|
|
97
|
-
this.logger.error('Node is not running', this.state);
|
|
98
|
-
throw new Error('Node is not running');
|
|
99
|
-
}
|
|
101
|
+
this.validateRunning();
|
|
100
102
|
if (!address.validate()) {
|
|
101
103
|
throw new Error('Invalid address');
|
|
102
104
|
}
|
|
@@ -139,16 +141,32 @@ export class oCore extends oObject {
|
|
|
139
141
|
return response;
|
|
140
142
|
}
|
|
141
143
|
// if there is an error, throw it to continue to bubble up
|
|
144
|
+
this.handleResponseError(response);
|
|
145
|
+
return response;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Helper method to handle response errors
|
|
149
|
+
* @param response The response to check for errors
|
|
150
|
+
* @throws oError if the response contains an error
|
|
151
|
+
*/
|
|
152
|
+
handleResponseError(response) {
|
|
142
153
|
if (response.result.error) {
|
|
143
154
|
this.logger.error('response.result.error', JSON.stringify(response.result.error, null, 2));
|
|
144
155
|
throw oError.fromJSON(response.result.error);
|
|
145
156
|
}
|
|
146
|
-
return response;
|
|
147
157
|
}
|
|
148
|
-
|
|
158
|
+
/**
|
|
159
|
+
* Helper method to validate node is running
|
|
160
|
+
* @throws Error if node is not running
|
|
161
|
+
*/
|
|
162
|
+
validateRunning() {
|
|
149
163
|
if (!this.isRunning) {
|
|
164
|
+
this.logger.error('Node is not running', this.state);
|
|
150
165
|
throw new Error('Node is not running');
|
|
151
166
|
}
|
|
167
|
+
}
|
|
168
|
+
async useSelf(data) {
|
|
169
|
+
this.validateRunning();
|
|
152
170
|
// let's call our own tool
|
|
153
171
|
this.logger.debug('Calling ourselves, skipping...', data);
|
|
154
172
|
const request = new oRequest({
|
|
@@ -160,29 +178,19 @@ export class oCore extends oObject {
|
|
|
160
178
|
},
|
|
161
179
|
id: 0,
|
|
162
180
|
});
|
|
163
|
-
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
? error
|
|
169
|
-
: new oError(oErrorCodes.UNKNOWN, error.message);
|
|
170
|
-
return {
|
|
171
|
-
error: responseError.toJSON(),
|
|
172
|
-
};
|
|
173
|
-
});
|
|
174
|
-
if (success) {
|
|
175
|
-
this.metrics.successCount++;
|
|
181
|
+
// Use ResponseBuilder with automatic error handling and metrics tracking
|
|
182
|
+
const responseBuilder = ResponseBuilder.create().withMetrics(this.metrics);
|
|
183
|
+
try {
|
|
184
|
+
const result = await this.execute(request);
|
|
185
|
+
return await responseBuilder.build(request, result, null);
|
|
176
186
|
}
|
|
177
|
-
|
|
178
|
-
this.
|
|
187
|
+
catch (error) {
|
|
188
|
+
this.logger.error('Error executing tool [self]: ', error);
|
|
189
|
+
return await responseBuilder.buildError(request, error);
|
|
179
190
|
}
|
|
180
|
-
return CoreUtils.buildResponse(request, result, result?.error);
|
|
181
191
|
}
|
|
182
192
|
async useChild(childAddress, data, options) {
|
|
183
|
-
|
|
184
|
-
throw new Error('Node is not running');
|
|
185
|
-
}
|
|
193
|
+
this.validateRunning();
|
|
186
194
|
if (!childAddress.transports) {
|
|
187
195
|
const child = this.hierarchyManager.getChild(childAddress);
|
|
188
196
|
if (!child) {
|
|
@@ -197,7 +205,13 @@ export class oCore extends oObject {
|
|
|
197
205
|
nextHopAddress: childAddress,
|
|
198
206
|
address: childAddress,
|
|
199
207
|
callerAddress: this.address,
|
|
208
|
+
isStream: options?.isStream,
|
|
200
209
|
});
|
|
210
|
+
if (options?.isStream) {
|
|
211
|
+
connection.onChunk((response) => {
|
|
212
|
+
options.onChunk?.(response);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
201
215
|
// communicate the payload to the target node
|
|
202
216
|
const response = await connection.send({
|
|
203
217
|
address: childAddress?.toString() || '',
|
|
@@ -205,10 +219,7 @@ export class oCore extends oObject {
|
|
|
205
219
|
id: data?.id,
|
|
206
220
|
});
|
|
207
221
|
// if there is an error, throw it to continue to bubble up
|
|
208
|
-
|
|
209
|
-
this.logger.error('response.result.error', JSON.stringify(response.result.error, null, 2));
|
|
210
|
-
throw oError.fromJSON(response.result.error);
|
|
211
|
-
}
|
|
222
|
+
this.handleResponseError(response);
|
|
212
223
|
return response;
|
|
213
224
|
}
|
|
214
225
|
// hierarchy
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC"}
|
package/dist/src/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/response/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,eAAe,EACf,kBAAkB,EAClB,cAAc,EACd,qBAAqB,GACtB,MAAM,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ResponseBuilder, DefaultMetricsTracker, } from './response-builder.js';
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { oRequest } from '../connection/o-request.js';
|
|
2
|
+
import { oResponse } from '../connection/o-response.js';
|
|
3
|
+
import { oError } from '../error/o-error.js';
|
|
4
|
+
/**
|
|
5
|
+
* Response context for building responses with proper metadata
|
|
6
|
+
*/
|
|
7
|
+
export interface ResponseContext {
|
|
8
|
+
/** Whether this is a streaming response */
|
|
9
|
+
isStream?: boolean;
|
|
10
|
+
/** Whether this is the last chunk in a stream */
|
|
11
|
+
isLast?: boolean;
|
|
12
|
+
/** Whether the operation was successful */
|
|
13
|
+
success?: boolean;
|
|
14
|
+
/** Request ID for correlation */
|
|
15
|
+
requestId?: string | number;
|
|
16
|
+
/** Connection ID from the request */
|
|
17
|
+
connectionId?: string | number;
|
|
18
|
+
/** Request method for tracking */
|
|
19
|
+
requestMethod?: string | number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Middleware function that can intercept and transform responses
|
|
23
|
+
*/
|
|
24
|
+
export type ResponseMiddleware = (response: oResponse, context: ResponseContext) => oResponse | Promise<oResponse>;
|
|
25
|
+
/**
|
|
26
|
+
* Metrics tracker interface for extensible metrics tracking
|
|
27
|
+
*/
|
|
28
|
+
export interface MetricsTracker {
|
|
29
|
+
trackSuccess(context: ResponseContext): void;
|
|
30
|
+
trackError(context: ResponseContext, error: oError): void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Default metrics tracker that increments counters
|
|
34
|
+
*/
|
|
35
|
+
export declare class DefaultMetricsTracker implements MetricsTracker {
|
|
36
|
+
private metrics;
|
|
37
|
+
constructor(metrics: {
|
|
38
|
+
successCount: number;
|
|
39
|
+
errorCount: number;
|
|
40
|
+
});
|
|
41
|
+
trackSuccess(context: ResponseContext): void;
|
|
42
|
+
trackError(context: ResponseContext, error: oError): void;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* ResponseBuilder - Unified response generation for all Olane routing paths
|
|
46
|
+
*
|
|
47
|
+
* This class provides a single source of truth for creating responses across:
|
|
48
|
+
* - Local execution (useSelf)
|
|
49
|
+
* - Remote execution (use, useChild)
|
|
50
|
+
* - Router forwarding (forward, executeSelfRouting)
|
|
51
|
+
* - Streaming responses
|
|
52
|
+
*
|
|
53
|
+
* Features:
|
|
54
|
+
* - Consistent error normalization
|
|
55
|
+
* - Automatic metrics tracking
|
|
56
|
+
* - Context-aware response building (streaming, success/error states)
|
|
57
|
+
* - Extensible middleware system
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const builder = new ResponseBuilder()
|
|
62
|
+
* .withMetrics(node.metrics)
|
|
63
|
+
* .use(loggingMiddleware);
|
|
64
|
+
*
|
|
65
|
+
* const response = await builder.build(request, result, error);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare class ResponseBuilder {
|
|
69
|
+
private middlewares;
|
|
70
|
+
private metricsTracker?;
|
|
71
|
+
/**
|
|
72
|
+
* Add a middleware function to intercept responses
|
|
73
|
+
*/
|
|
74
|
+
use(middleware: ResponseMiddleware): this;
|
|
75
|
+
/**
|
|
76
|
+
* Configure metrics tracking
|
|
77
|
+
* @param metrics The metrics object to track success/error counts
|
|
78
|
+
*/
|
|
79
|
+
withMetrics(metrics: {
|
|
80
|
+
successCount: number;
|
|
81
|
+
errorCount: number;
|
|
82
|
+
}): this;
|
|
83
|
+
/**
|
|
84
|
+
* Configure custom metrics tracker
|
|
85
|
+
* @param tracker Custom metrics tracker implementation
|
|
86
|
+
*/
|
|
87
|
+
withCustomMetrics(tracker: MetricsTracker): this;
|
|
88
|
+
/**
|
|
89
|
+
* Normalize any error into an oError instance
|
|
90
|
+
* @param error The error to normalize
|
|
91
|
+
* @returns An oError instance
|
|
92
|
+
*/
|
|
93
|
+
normalizeError(error: any): oError;
|
|
94
|
+
/**
|
|
95
|
+
* Build a complete response from a request and result
|
|
96
|
+
* @param request The original request
|
|
97
|
+
* @param result The result data (or error object)
|
|
98
|
+
* @param error Optional error object
|
|
99
|
+
* @param context Additional response context
|
|
100
|
+
* @returns An oResponse instance
|
|
101
|
+
*/
|
|
102
|
+
build(request: oRequest, result: any, error?: any, context?: Partial<ResponseContext>): Promise<oResponse>;
|
|
103
|
+
/**
|
|
104
|
+
* Build a streaming chunk response
|
|
105
|
+
* @param request The original request
|
|
106
|
+
* @param chunkData The chunk data
|
|
107
|
+
* @param context Additional response context
|
|
108
|
+
* @returns An oResponse instance with _last: false
|
|
109
|
+
*/
|
|
110
|
+
buildChunk(request: oRequest, chunkData: any, context?: Partial<ResponseContext>): Promise<oResponse>;
|
|
111
|
+
/**
|
|
112
|
+
* Build the final chunk in a stream
|
|
113
|
+
* @param request The original request
|
|
114
|
+
* @param context Additional response context
|
|
115
|
+
* @returns An oResponse instance with _last: true
|
|
116
|
+
*/
|
|
117
|
+
buildFinalChunk(request: oRequest, context?: Partial<ResponseContext>): Promise<oResponse>;
|
|
118
|
+
/**
|
|
119
|
+
* Build an error response
|
|
120
|
+
* @param request The original request
|
|
121
|
+
* @param error The error that occurred
|
|
122
|
+
* @param context Additional response context
|
|
123
|
+
* @returns An oResponse instance with error details
|
|
124
|
+
*/
|
|
125
|
+
buildError(request: oRequest, error: any, context?: Partial<ResponseContext>): Promise<oResponse>;
|
|
126
|
+
/**
|
|
127
|
+
* Execute a function and automatically build a response based on the outcome
|
|
128
|
+
* @param request The request being executed
|
|
129
|
+
* @param executor The function to execute
|
|
130
|
+
* @param context Additional response context
|
|
131
|
+
* @returns An oResponse instance
|
|
132
|
+
*/
|
|
133
|
+
execute(request: oRequest, executor: () => Promise<any>, context?: Partial<ResponseContext>): Promise<oResponse>;
|
|
134
|
+
/**
|
|
135
|
+
* Create a new ResponseBuilder instance (for chaining)
|
|
136
|
+
* @returns A new ResponseBuilder instance
|
|
137
|
+
*/
|
|
138
|
+
static create(): ResponseBuilder;
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=response-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-builder.d.ts","sourceRoot":"","sources":["../../../src/response/response-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAG7C;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,iDAAiD;IACjD,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,2CAA2C;IAC3C,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,iCAAiC;IACjC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,qCAAqC;IACrC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC/B,kCAAkC;IAClC,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACjC;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAC/B,QAAQ,EAAE,SAAS,EACnB,OAAO,EAAE,eAAe,KACrB,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AAEpC;;GAEG;AACH,MAAM,WAAW,cAAc;I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;IACG,eAAe,CACnB,OAAO,EAAE,QAAQ,EACjB,OAAO,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,GACjC,OAAO,CAAC,SAAS,CAAC;IAcrB;;;;;;OAMG;IACG,UAAU,CACd,OAAO,EAAE,QAAQ,EACjB,KAAK,EAAE,GAAG,EACV,OAAO,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,GACjC,OAAO,CAAC,SAAS,CAAC;IAUrB;;;;;;OAMG;IACG,OAAO,CACX,OAAO,EAAE,QAAQ,EACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,EAC5B,OAAO,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,GACjC,OAAO,CAAC,SAAS,CAAC;IASrB;;;OAGG;IACH,MAAM,CAAC,MAAM,IAAI,eAAe;CAGjC"}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { oResponse } from '../connection/o-response.js';
|
|
2
|
+
import { oError } from '../error/o-error.js';
|
|
3
|
+
import { oErrorCodes } from '../error/enums/codes.error.js';
|
|
4
|
+
/**
|
|
5
|
+
* Default metrics tracker that increments counters
|
|
6
|
+
*/
|
|
7
|
+
export class DefaultMetricsTracker {
|
|
8
|
+
constructor(metrics) {
|
|
9
|
+
this.metrics = metrics;
|
|
10
|
+
}
|
|
11
|
+
trackSuccess(context) {
|
|
12
|
+
this.metrics.successCount++;
|
|
13
|
+
}
|
|
14
|
+
trackError(context, error) {
|
|
15
|
+
this.metrics.errorCount++;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* ResponseBuilder - Unified response generation for all Olane routing paths
|
|
20
|
+
*
|
|
21
|
+
* This class provides a single source of truth for creating responses across:
|
|
22
|
+
* - Local execution (useSelf)
|
|
23
|
+
* - Remote execution (use, useChild)
|
|
24
|
+
* - Router forwarding (forward, executeSelfRouting)
|
|
25
|
+
* - Streaming responses
|
|
26
|
+
*
|
|
27
|
+
* Features:
|
|
28
|
+
* - Consistent error normalization
|
|
29
|
+
* - Automatic metrics tracking
|
|
30
|
+
* - Context-aware response building (streaming, success/error states)
|
|
31
|
+
* - Extensible middleware system
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const builder = new ResponseBuilder()
|
|
36
|
+
* .withMetrics(node.metrics)
|
|
37
|
+
* .use(loggingMiddleware);
|
|
38
|
+
*
|
|
39
|
+
* const response = await builder.build(request, result, error);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export class ResponseBuilder {
|
|
43
|
+
constructor() {
|
|
44
|
+
this.middlewares = [];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Add a middleware function to intercept responses
|
|
48
|
+
*/
|
|
49
|
+
use(middleware) {
|
|
50
|
+
this.middlewares.push(middleware);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Configure metrics tracking
|
|
55
|
+
* @param metrics The metrics object to track success/error counts
|
|
56
|
+
*/
|
|
57
|
+
withMetrics(metrics) {
|
|
58
|
+
this.metricsTracker = new DefaultMetricsTracker(metrics);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Configure custom metrics tracker
|
|
63
|
+
* @param tracker Custom metrics tracker implementation
|
|
64
|
+
*/
|
|
65
|
+
withCustomMetrics(tracker) {
|
|
66
|
+
this.metricsTracker = tracker;
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Normalize any error into an oError instance
|
|
71
|
+
* @param error The error to normalize
|
|
72
|
+
* @returns An oError instance
|
|
73
|
+
*/
|
|
74
|
+
normalizeError(error) {
|
|
75
|
+
if (error instanceof oError) {
|
|
76
|
+
return error;
|
|
77
|
+
}
|
|
78
|
+
return new oError(oErrorCodes.UNKNOWN, error?.message || String(error), error?.stack);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Build a complete response from a request and result
|
|
82
|
+
* @param request The original request
|
|
83
|
+
* @param result The result data (or error object)
|
|
84
|
+
* @param error Optional error object
|
|
85
|
+
* @param context Additional response context
|
|
86
|
+
* @returns An oResponse instance
|
|
87
|
+
*/
|
|
88
|
+
async build(request, result, error, context) {
|
|
89
|
+
const isError = !!error;
|
|
90
|
+
const success = !isError;
|
|
91
|
+
// Build context
|
|
92
|
+
const responseContext = {
|
|
93
|
+
isStream: context?.isStream ?? false,
|
|
94
|
+
isLast: context?.isLast ?? true,
|
|
95
|
+
success,
|
|
96
|
+
requestId: request.id,
|
|
97
|
+
connectionId: request.params?._connectionId,
|
|
98
|
+
requestMethod: request.method,
|
|
99
|
+
...context,
|
|
100
|
+
};
|
|
101
|
+
// Create response
|
|
102
|
+
let response = new oResponse({
|
|
103
|
+
id: request.id,
|
|
104
|
+
data: result,
|
|
105
|
+
error: result?.error,
|
|
106
|
+
success,
|
|
107
|
+
_last: responseContext.isLast,
|
|
108
|
+
_requestMethod: String(responseContext.requestMethod || request.method || ''),
|
|
109
|
+
_connectionId: String(responseContext.connectionId || ''),
|
|
110
|
+
});
|
|
111
|
+
// Apply middleware
|
|
112
|
+
for (const middleware of this.middlewares) {
|
|
113
|
+
response = await middleware(response, responseContext);
|
|
114
|
+
}
|
|
115
|
+
// Track metrics
|
|
116
|
+
if (this.metricsTracker) {
|
|
117
|
+
if (success) {
|
|
118
|
+
this.metricsTracker.trackSuccess(responseContext);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
this.metricsTracker.trackError(responseContext, this.normalizeError(error));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return response;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Build a streaming chunk response
|
|
128
|
+
* @param request The original request
|
|
129
|
+
* @param chunkData The chunk data
|
|
130
|
+
* @param context Additional response context
|
|
131
|
+
* @returns An oResponse instance with _last: false
|
|
132
|
+
*/
|
|
133
|
+
async buildChunk(request, chunkData, context) {
|
|
134
|
+
return this.build(request, chunkData, null, {
|
|
135
|
+
...context,
|
|
136
|
+
isStream: true,
|
|
137
|
+
isLast: false,
|
|
138
|
+
success: true,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Build the final chunk in a stream
|
|
143
|
+
* @param request The original request
|
|
144
|
+
* @param context Additional response context
|
|
145
|
+
* @returns An oResponse instance with _last: true
|
|
146
|
+
*/
|
|
147
|
+
async buildFinalChunk(request, context) {
|
|
148
|
+
return this.build(request, { success: true, response: 'Stream completed' }, null, {
|
|
149
|
+
...context,
|
|
150
|
+
isStream: true,
|
|
151
|
+
isLast: true,
|
|
152
|
+
success: true,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Build an error response
|
|
157
|
+
* @param request The original request
|
|
158
|
+
* @param error The error that occurred
|
|
159
|
+
* @param context Additional response context
|
|
160
|
+
* @returns An oResponse instance with error details
|
|
161
|
+
*/
|
|
162
|
+
async buildError(request, error, context) {
|
|
163
|
+
const normalizedError = this.normalizeError(error);
|
|
164
|
+
return this.build(request, { error: normalizedError.toJSON() }, normalizedError, context);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Execute a function and automatically build a response based on the outcome
|
|
168
|
+
* @param request The request being executed
|
|
169
|
+
* @param executor The function to execute
|
|
170
|
+
* @param context Additional response context
|
|
171
|
+
* @returns An oResponse instance
|
|
172
|
+
*/
|
|
173
|
+
async execute(request, executor, context) {
|
|
174
|
+
try {
|
|
175
|
+
const result = await executor();
|
|
176
|
+
return await this.build(request, result, null, context);
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
return await this.buildError(request, error, context);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Create a new ResponseBuilder instance (for chaining)
|
|
184
|
+
* @returns A new ResponseBuilder instance
|
|
185
|
+
*/
|
|
186
|
+
static create() {
|
|
187
|
+
return new ResponseBuilder();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -3,7 +3,8 @@ import { oAddress } from '../router/o-address.js';
|
|
|
3
3
|
import { oResponse } from '../connection/o-response.js';
|
|
4
4
|
import { oRequest } from '../connection/o-request.js';
|
|
5
5
|
import { CID } from 'multiformats';
|
|
6
|
-
|
|
6
|
+
import { oObject } from '../core/o-object.js';
|
|
7
|
+
export declare class CoreUtils extends oObject {
|
|
7
8
|
static generatePeerId(): Promise<any>;
|
|
8
9
|
static doHealthCheck(address: oAddress, node: any): Promise<boolean>;
|
|
9
10
|
static healthCheck(config: {
|
|
@@ -12,6 +13,21 @@ export declare class CoreUtils {
|
|
|
12
13
|
node: any;
|
|
13
14
|
timeout?: number;
|
|
14
15
|
}): Promise<boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated Use ResponseBuilder.build() instead for consistent response generation with metrics tracking and error handling.
|
|
18
|
+
*
|
|
19
|
+
* Example migration:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Old:
|
|
22
|
+
* const response = CoreUtils.buildResponse(request, result, error);
|
|
23
|
+
*
|
|
24
|
+
* // New:
|
|
25
|
+
* const responseBuilder = ResponseBuilder.create().withMetrics(node.metrics);
|
|
26
|
+
* const response = await responseBuilder.build(request, result, error);
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* This method will be removed in a future major version.
|
|
30
|
+
*/
|
|
15
31
|
static buildResponse(request: oRequest, result: any, error: any): oResponse;
|
|
16
32
|
static generatePrivateKey(seed: string): Promise<any>;
|
|
17
33
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.utils.d.ts","sourceRoot":"","sources":["../../../src/utils/core.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,MAAM,EAGP,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxD,OAAO,EAAE,Q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xB,aAAa,EAAE,QAAQ,EACvB,YAAY,EAAE,QAAQ,GACrB,QAAQ;WAIS,YAAY,CAAC,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM;WAahD,kBAAkB,CAAC,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM;WAYtD,aAAa,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;WAMvC,oBAAoB,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC;WAKnD,qBAAqB,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,SAAS,CAAC;WAKrD,KAAK,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;CAMnD"}
|
|
@@ -6,7 +6,8 @@ import { oRequest } from '../connection/o-request.js';
|
|
|
6
6
|
import { CID } from 'multiformats';
|
|
7
7
|
import * as json from 'multiformats/codecs/json';
|
|
8
8
|
import { sha256 } from 'multiformats/hashes/sha2';
|
|
9
|
-
|
|
9
|
+
import { oObject } from '../core/o-object.js';
|
|
10
|
+
export class CoreUtils extends oObject {
|
|
10
11
|
static async generatePeerId() {
|
|
11
12
|
const peerId = await createEd25519PeerId();
|
|
12
13
|
return peerId;
|
|
@@ -46,6 +47,21 @@ export class CoreUtils {
|
|
|
46
47
|
}
|
|
47
48
|
throw new Error('Address is required');
|
|
48
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated Use ResponseBuilder.build() instead for consistent response generation with metrics tracking and error handling.
|
|
52
|
+
*
|
|
53
|
+
* Example migration:
|
|
54
|
+
* ```typescript
|
|
55
|
+
* // Old:
|
|
56
|
+
* const response = CoreUtils.buildResponse(request, result, error);
|
|
57
|
+
*
|
|
58
|
+
* // New:
|
|
59
|
+
* const responseBuilder = ResponseBuilder.create().withMetrics(node.metrics);
|
|
60
|
+
* const response = await responseBuilder.build(request, result, error);
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* This method will be removed in a future major version.
|
|
64
|
+
*/
|
|
49
65
|
static buildResponse(request, result, error) {
|
|
50
66
|
let success = true;
|
|
51
67
|
if (error) {
|
|
@@ -56,6 +72,7 @@ export class CoreUtils {
|
|
|
56
72
|
data: result,
|
|
57
73
|
error: result?.error,
|
|
58
74
|
...{ success },
|
|
75
|
+
_last: true,
|
|
59
76
|
_requestMethod: request.method,
|
|
60
77
|
_connectionId: request.params?._connectionId,
|
|
61
78
|
});
|
|
@@ -101,8 +118,10 @@ export class CoreUtils {
|
|
|
101
118
|
}
|
|
102
119
|
}
|
|
103
120
|
static async sendStreamResponse(response, stream) {
|
|
104
|
-
|
|
105
|
-
|
|
121
|
+
const utils = new CoreUtils();
|
|
122
|
+
if (!stream || stream.status !== 'open') {
|
|
123
|
+
utils.logger.warn('Stream is not open. Status: ' + stream?.status || 'undefined');
|
|
124
|
+
return;
|
|
106
125
|
}
|
|
107
126
|
await stream.send(new TextEncoder().encode(response.toString()));
|
|
108
127
|
}
|
|
@@ -116,7 +135,7 @@ export class CoreUtils {
|
|
|
116
135
|
}
|
|
117
136
|
static async processStreamResponse(event) {
|
|
118
137
|
const res = await CoreUtils.processStream(event);
|
|
119
|
-
return new oResponse(res);
|
|
138
|
+
return res?.id ? oResponse.fromJSON(res) : new oResponse(res);
|
|
120
139
|
}
|
|
121
140
|
static async toCID(data) {
|
|
122
141
|
const bytes = json.encode(data);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/utils/logger.ts"],"names":[],"mappings":"AAGA,qBAAa,MAAM;
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/utils/logger.ts"],"names":[],"mappings":"AAGA,qBAAa,MAAM;IAIL,OAAO,CAAC,QAAQ,CAAC,IAAI;IAHjC,OAAO,CAAC,GAAG,CAAW;IACtB,OAAO,CAAC,MAAM,CAAc;gBAEC,IAAI,EAAE,MAAM;IAKzC,YAAY,CAAC,IAAI,EAAE,MAAM;IAIzB,OAAO,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAItB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAIpB,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAInB,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAInB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAIpB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;CAGrB"}
|
package/dist/src/utils/logger.js
CHANGED
|
@@ -3,7 +3,9 @@ import chalk from 'chalk';
|
|
|
3
3
|
export class Logger {
|
|
4
4
|
constructor(name) {
|
|
5
5
|
this.name = name;
|
|
6
|
-
this.
|
|
6
|
+
this.suffix = '';
|
|
7
|
+
this.suffix = process.env.LOG_ID ? process.env.LOG_ID + ':' : '';
|
|
8
|
+
this.log = debug('olane-os:' + this.suffix + name);
|
|
7
9
|
}
|
|
8
10
|
setNamespace(name) {
|
|
9
11
|
this.log = debug('olane-os:' + name);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olane/o-core",
|
|
3
|
-
"version": "0.7.12-alpha.
|
|
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",
|
|
@@ -63,11 +63,11 @@
|
|
|
63
63
|
"typescript": "^5.8.3"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"@olane/o-protocol": "0.7.12-alpha.
|
|
66
|
+
"@olane/o-protocol": "0.7.12-alpha.26",
|
|
67
67
|
"chalk": "^5.4.1",
|
|
68
68
|
"debug": "^4.4.1",
|
|
69
69
|
"dotenv": "^16.5.0",
|
|
70
70
|
"multiformats": "^13.3.7"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "d0436ce9eed9cad0e64561cf0428b0e791c57058"
|
|
73
73
|
}
|