@olane/o-core 0.7.12-alpha.22 → 0.7.12-alpha.24
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/connection-manager.config.d.ts +12 -0
- package/dist/src/connection/interfaces/connection-manager.config.d.ts.map +1 -1
- package/dist/src/connection/interfaces/connection-send-params.interface.d.ts +1 -0
- package/dist/src/connection/interfaces/connection-send-params.interface.d.ts.map +1 -1
- package/dist/src/connection/interfaces/connection.config.d.ts +11 -0
- package/dist/src/connection/interfaces/connection.config.d.ts.map +1 -1
- package/dist/src/connection/o-connection.d.ts +3 -0
- package/dist/src/connection/o-connection.d.ts.map +1 -1
- package/dist/src/connection/o-connection.js +7 -2
- package/dist/src/core/o-core.d.ts +18 -1
- package/dist/src/core/o-core.d.ts.map +1 -1
- package/dist/src/core/o-core.js +29 -3
- package/dist/src/error/enums/codes.error.d.ts +2 -1
- package/dist/src/error/enums/codes.error.d.ts.map +1 -1
- package/dist/src/error/enums/codes.error.js +1 -0
- package/dist/src/streaming/index.d.ts +11 -0
- package/dist/src/streaming/index.d.ts.map +1 -0
- package/dist/src/streaming/index.js +14 -0
- package/dist/src/streaming/protocol-builder.d.ts +62 -0
- package/dist/src/streaming/protocol-builder.d.ts.map +1 -0
- package/dist/src/streaming/protocol-builder.js +93 -0
- package/dist/src/streaming/stream-config.d.ts +36 -0
- package/dist/src/streaming/stream-config.d.ts.map +1 -0
- package/dist/src/streaming/stream-config.js +19 -0
- package/dist/src/streaming/stream-handler.base.d.ts +85 -0
- package/dist/src/streaming/stream-handler.base.d.ts.map +1 -0
- package/dist/src/streaming/stream-handler.base.js +112 -0
- package/dist/src/streaming/stream-transport.interface.d.ts +61 -0
- package/dist/src/streaming/stream-transport.interface.d.ts.map +1 -0
- package/dist/src/streaming/stream-transport.interface.js +10 -0
- package/dist/src/utils/core.utils.d.ts +5 -2
- package/dist/src/utils/core.utils.d.ts.map +1 -1
- package/dist/src/utils/core.utils.js +15 -1
- package/dist/src/utils/streaming.utils.d.ts +22 -0
- package/dist/src/utils/streaming.utils.d.ts.map +1 -0
- package/dist/src/utils/streaming.utils.js +45 -0
- package/package.json +11 -3
|
@@ -1,3 +1,15 @@
|
|
|
1
1
|
export interface oConnectionManagerConfig {
|
|
2
|
+
/**
|
|
3
|
+
* Default timeout in milliseconds for reading response data from a stream.
|
|
4
|
+
* Can be overridden per connection.
|
|
5
|
+
* Default: 120000 (2 minutes)
|
|
6
|
+
*/
|
|
7
|
+
defaultReadTimeoutMs?: number;
|
|
8
|
+
/**
|
|
9
|
+
* Default timeout in milliseconds for waiting for stream buffer to drain when backpressure occurs.
|
|
10
|
+
* Can be overridden per connection.
|
|
11
|
+
* Default: 30000 (30 seconds)
|
|
12
|
+
*/
|
|
13
|
+
defaultDrainTimeoutMs?: number;
|
|
2
14
|
}
|
|
3
15
|
//# sourceMappingURL=connection-manager.config.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection-manager.config.d.ts","sourceRoot":"","sources":["../../../../src/connection/interfaces/connection-manager.config.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;
|
|
1
|
+
{"version":3,"file":"connection-manager.config.d.ts","sourceRoot":"","sources":["../../../../src/connection/interfaces/connection-manager.config.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;CAChC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection-send-params.interface.d.ts","sourceRoot":"","sources":["../../../../src/connection/interfaces/connection-send-params.interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE;QACP,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;IACF,EAAE,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"connection-send-params.interface.d.ts","sourceRoot":"","sources":["../../../../src/connection/interfaces/connection-send-params.interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE;QACP,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;IACF,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
|
@@ -3,5 +3,16 @@ export interface oConnectionConfig {
|
|
|
3
3
|
nextHopAddress: oAddress;
|
|
4
4
|
callerAddress?: oAddress;
|
|
5
5
|
address: oAddress;
|
|
6
|
+
/**
|
|
7
|
+
* Timeout in milliseconds for reading response data from a stream.
|
|
8
|
+
* Default: 120000 (2 minutes)
|
|
9
|
+
*/
|
|
10
|
+
readTimeoutMs?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Timeout in milliseconds for waiting for stream buffer to drain when backpressure occurs.
|
|
13
|
+
* Default: 30000 (30 seconds)
|
|
14
|
+
*/
|
|
15
|
+
drainTimeoutMs?: number;
|
|
16
|
+
isStream?: boolean;
|
|
6
17
|
}
|
|
7
18
|
//# sourceMappingURL=connection.config.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.config.d.ts","sourceRoot":"","sources":["../../../../src/connection/interfaces/connection.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAErD,MAAM,WAAW,iBAAiB;IAChC,cAAc,EAAE,QAAQ,CAAC;IACzB,aAAa,CAAC,EAAE,QAAQ,CAAC;IACzB,OAAO,EAAE,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"connection.config.d.ts","sourceRoot":"","sources":["../../../../src/connection/interfaces/connection.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAErD,MAAM,WAAW,iBAAiB;IAChC,cAAc,EAAE,QAAQ,CAAC;IACzB,aAAa,CAAC,EAAE,QAAQ,CAAC;IACzB,OAAO,EAAE,QAAQ,CAAC;IAClB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
|
@@ -4,13 +4,16 @@ import { oResponse } from './o-response.js';
|
|
|
4
4
|
import { ConnectionSendParams } from './interfaces/connection-send-params.interface.js';
|
|
5
5
|
import { oObject } from '../core/o-object.js';
|
|
6
6
|
import { oConnectionConfig } from './interfaces/connection.config.js';
|
|
7
|
+
import { EventEmitter } from 'events';
|
|
7
8
|
export declare abstract class oConnection extends oObject {
|
|
8
9
|
protected readonly config: oConnectionConfig;
|
|
9
10
|
readonly id: string;
|
|
10
11
|
readonly address: oAddress;
|
|
11
12
|
readonly nextHopAddress: oAddress;
|
|
12
13
|
readonly callerAddress: oAddress | undefined;
|
|
14
|
+
protected readonly emitter: EventEmitter;
|
|
13
15
|
constructor(config: oConnectionConfig);
|
|
16
|
+
onChunk(listener: (response: oResponse) => void): void;
|
|
14
17
|
validate(): void;
|
|
15
18
|
createRequest(method: string, params: ConnectionSendParams): oRequest;
|
|
16
19
|
abstract transmit(request: oRequest): Promise<oResponse>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o-connection.d.ts","sourceRoot":"","sources":["../../../src/connection/o-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,kDAAkD,CAAC;AAExF,OAAO,EAAE,O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-2,10 +2,12 @@ import { oRequest } from './o-request.js';
|
|
|
2
2
|
import { oProtocolMethods } from '@olane/o-protocol';
|
|
3
3
|
import { v4 as uuidv4 } from 'uuid';
|
|
4
4
|
import { oObject } from '../core/o-object.js';
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
5
6
|
export class oConnection extends oObject {
|
|
6
7
|
constructor(config) {
|
|
7
8
|
super();
|
|
8
9
|
this.config = config;
|
|
10
|
+
this.emitter = new EventEmitter();
|
|
9
11
|
this.id = uuidv4();
|
|
10
12
|
this.address = config.address;
|
|
11
13
|
this.nextHopAddress = config.nextHopAddress;
|
|
@@ -17,6 +19,9 @@ export class oConnection extends oObject {
|
|
|
17
19
|
']:' +
|
|
18
20
|
this.id);
|
|
19
21
|
}
|
|
22
|
+
onChunk(listener) {
|
|
23
|
+
this.emitter.addListener('chunk', listener);
|
|
24
|
+
}
|
|
20
25
|
validate() {
|
|
21
26
|
if (!this.address || !this.nextHopAddress || !this.callerAddress) {
|
|
22
27
|
throw new Error('Connection configuration is invalid');
|
|
@@ -29,6 +34,7 @@ export class oConnection extends oObject {
|
|
|
29
34
|
_connectionId: this.id,
|
|
30
35
|
_requestMethod: method,
|
|
31
36
|
_callerAddress: this.callerAddress?.value,
|
|
37
|
+
_isStream: this.config.isStream || false,
|
|
32
38
|
...params,
|
|
33
39
|
},
|
|
34
40
|
id: params.id || uuidv4(),
|
|
@@ -37,8 +43,7 @@ export class oConnection extends oObject {
|
|
|
37
43
|
async send(data) {
|
|
38
44
|
// proxy through the router tool
|
|
39
45
|
const request = this.createRequest(oProtocolMethods.ROUTE, data);
|
|
40
|
-
|
|
41
|
-
return result;
|
|
46
|
+
return this.transmit(request);
|
|
42
47
|
}
|
|
43
48
|
async close() {
|
|
44
49
|
this.logger.debug('Closing connection...');
|
|
@@ -16,6 +16,7 @@ import { oRouter } from '../router/o-router.js';
|
|
|
16
16
|
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
|
+
import { oConnectionConfig } from '../connection/index.js';
|
|
19
20
|
export declare abstract class oCore extends oObject {
|
|
20
21
|
readonly config: oCoreConfig;
|
|
21
22
|
address: oAddress;
|
|
@@ -32,6 +33,15 @@ export declare abstract class oCore extends oObject {
|
|
|
32
33
|
get isLeader(): boolean;
|
|
33
34
|
get leader(): oAddress | null;
|
|
34
35
|
abstract configureTransports(): any[];
|
|
36
|
+
useStream(address: oAddress, data: {
|
|
37
|
+
method?: string;
|
|
38
|
+
params?: {
|
|
39
|
+
[key: string]: any;
|
|
40
|
+
};
|
|
41
|
+
id?: string;
|
|
42
|
+
}, options: {
|
|
43
|
+
onChunk?: (chunk: oResponse) => void;
|
|
44
|
+
}): Promise<oResponse>;
|
|
35
45
|
useDirect(address: oAddress, data?: {
|
|
36
46
|
method?: string;
|
|
37
47
|
params?: {
|
|
@@ -100,6 +110,10 @@ export declare abstract class oCore extends oObject {
|
|
|
100
110
|
id?: string;
|
|
101
111
|
}, options?: {
|
|
102
112
|
noRouting?: boolean;
|
|
113
|
+
readTimeoutMs?: number;
|
|
114
|
+
drainTimeoutMs?: number;
|
|
115
|
+
isStream?: boolean;
|
|
116
|
+
onChunk?: (chunk: oResponse) => void;
|
|
103
117
|
}): Promise<oResponse>;
|
|
104
118
|
abstract execute(request: oRequest): Promise<any>;
|
|
105
119
|
useSelf(data?: {
|
|
@@ -115,10 +129,13 @@ export declare abstract class oCore extends oObject {
|
|
|
115
129
|
[key: string]: any;
|
|
116
130
|
};
|
|
117
131
|
id?: string;
|
|
132
|
+
}, options?: {
|
|
133
|
+
isStream?: boolean;
|
|
134
|
+
onChunk?: (chunk: oResponse) => void;
|
|
118
135
|
}): Promise<oResponse>;
|
|
119
136
|
addChildNode(node: oCore): void;
|
|
120
137
|
removeChildNode(node: oCore): void;
|
|
121
|
-
abstract connect(
|
|
138
|
+
abstract connect(config: oConnectionConfig): Promise<oConnection>;
|
|
122
139
|
abstract initializeRouter(): void;
|
|
123
140
|
abstract unregister(): Promise<void>;
|
|
124
141
|
abstract register(): Promise<void>;
|
|
@@ -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;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,mBAAmB,EACnB,YAAY,EACb,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"o-core.d.ts","sourceRoot":"","s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,12 @@ export class oCore extends oObject {
|
|
|
30
30
|
get leader() {
|
|
31
31
|
return this.isLeader ? this.address : this.config?.leader || null;
|
|
32
32
|
}
|
|
33
|
+
async useStream(address, data, options) {
|
|
34
|
+
return this.use(address, data, {
|
|
35
|
+
isStream: true,
|
|
36
|
+
onChunk: options.onChunk,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
33
39
|
async useDirect(address, data) {
|
|
34
40
|
return this.use(address, data, { noRouting: true });
|
|
35
41
|
}
|
|
@@ -109,13 +115,29 @@ export class oCore extends oObject {
|
|
|
109
115
|
if (nextHopAddress.toStaticAddress().equals(this.address.toStaticAddress())) {
|
|
110
116
|
return this.useSelf(data);
|
|
111
117
|
}
|
|
112
|
-
const connection = await this.connect(
|
|
118
|
+
const connection = await this.connect({
|
|
119
|
+
nextHopAddress: nextHopAddress,
|
|
120
|
+
address: targetAddress,
|
|
121
|
+
callerAddress: this.address,
|
|
122
|
+
readTimeoutMs: options?.readTimeoutMs,
|
|
123
|
+
drainTimeoutMs: options?.drainTimeoutMs,
|
|
124
|
+
isStream: options?.isStream,
|
|
125
|
+
});
|
|
126
|
+
if (options?.isStream) {
|
|
127
|
+
connection.onChunk((response) => {
|
|
128
|
+
options.onChunk?.(response);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
113
131
|
// communicate the payload to the target node
|
|
114
132
|
const response = await connection.send({
|
|
115
133
|
address: targetAddress?.toString() || '',
|
|
116
134
|
payload: data || {},
|
|
117
135
|
id: data?.id,
|
|
118
136
|
});
|
|
137
|
+
// we handle streaming response differently
|
|
138
|
+
if (options?.isStream) {
|
|
139
|
+
return response;
|
|
140
|
+
}
|
|
119
141
|
// if there is an error, throw it to continue to bubble up
|
|
120
142
|
if (response.result.error) {
|
|
121
143
|
this.logger.error('response.result.error', JSON.stringify(response.result.error, null, 2));
|
|
@@ -157,7 +179,7 @@ export class oCore extends oObject {
|
|
|
157
179
|
}
|
|
158
180
|
return CoreUtils.buildResponse(request, result, result?.error);
|
|
159
181
|
}
|
|
160
|
-
async useChild(childAddress, data) {
|
|
182
|
+
async useChild(childAddress, data, options) {
|
|
161
183
|
if (!this.isRunning) {
|
|
162
184
|
throw new Error('Node is not running');
|
|
163
185
|
}
|
|
@@ -171,7 +193,11 @@ export class oCore extends oObject {
|
|
|
171
193
|
childAddress.setTransports(child?.transports || []);
|
|
172
194
|
}
|
|
173
195
|
}
|
|
174
|
-
const connection = await this.connect(
|
|
196
|
+
const connection = await this.connect({
|
|
197
|
+
nextHopAddress: childAddress,
|
|
198
|
+
address: childAddress,
|
|
199
|
+
callerAddress: this.address,
|
|
200
|
+
});
|
|
175
201
|
// communicate the payload to the target node
|
|
176
202
|
const response = await connection.send({
|
|
177
203
|
address: childAddress?.toString() || '',
|
|
@@ -14,6 +14,7 @@ export declare enum oErrorCodes {
|
|
|
14
14
|
TRANSPORT_NOT_SUPPORTED = 1013,
|
|
15
15
|
FAILED_TO_DIAL_TARGET = 1014,
|
|
16
16
|
INVALID_CAPABILITY = 1015,
|
|
17
|
-
CONNECTION_LIMIT_REACHED = 1016
|
|
17
|
+
CONNECTION_LIMIT_REACHED = 1016,
|
|
18
|
+
TIMEOUT = 1017
|
|
18
19
|
}
|
|
19
20
|
//# sourceMappingURL=codes.error.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codes.error.d.ts","sourceRoot":"","sources":["../../../../src/error/enums/codes.error.ts"],"names":[],"mappings":"AAAA,oBAAY,WAAW;IACrB,OAAO,OAAO;IACd,SAAS,OAAO;IAChB,eAAe,OAAO;IACtB,cAAc,OAAO;IACrB,cAAc,OAAO;IACrB,kBAAkB,OAAO;IACzB,kBAAkB,OAAO;IACzB,gBAAgB,OAAO;IACvB,eAAe,OAAO;IACtB,aAAa,OAAO;IACpB,cAAc,OAAO;IACrB,mBAAmB,OAAO;IAC1B,uBAAuB,OAAO;IAC9B,qBAAqB,OAAO;IAC5B,kBAAkB,OAAO;IACzB,wBAAwB,OAAO;
|
|
1
|
+
{"version":3,"file":"codes.error.d.ts","sourceRoot":"","sources":["../../../../src/error/enums/codes.error.ts"],"names":[],"mappings":"AAAA,oBAAY,WAAW;IACrB,OAAO,OAAO;IACd,SAAS,OAAO;IAChB,eAAe,OAAO;IACtB,cAAc,OAAO;IACrB,cAAc,OAAO;IACrB,kBAAkB,OAAO;IACzB,kBAAkB,OAAO;IACzB,gBAAgB,OAAO;IACvB,eAAe,OAAO;IACtB,aAAa,OAAO;IACpB,cAAc,OAAO;IACrB,mBAAmB,OAAO;IAC1B,uBAAuB,OAAO;IAC9B,qBAAqB,OAAO;IAC5B,kBAAkB,OAAO;IACzB,wBAAwB,OAAO;IAC/B,OAAO,OAAO;CACf"}
|
|
@@ -16,4 +16,5 @@ export var oErrorCodes;
|
|
|
16
16
|
oErrorCodes[oErrorCodes["FAILED_TO_DIAL_TARGET"] = 1014] = "FAILED_TO_DIAL_TARGET";
|
|
17
17
|
oErrorCodes[oErrorCodes["INVALID_CAPABILITY"] = 1015] = "INVALID_CAPABILITY";
|
|
18
18
|
oErrorCodes[oErrorCodes["CONNECTION_LIMIT_REACHED"] = 1016] = "CONNECTION_LIMIT_REACHED";
|
|
19
|
+
oErrorCodes[oErrorCodes["TIMEOUT"] = 1017] = "TIMEOUT";
|
|
19
20
|
})(oErrorCodes || (oErrorCodes = {}));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming functionality for o-core
|
|
3
|
+
*
|
|
4
|
+
* This module provides transport-agnostic abstractions for streaming operations
|
|
5
|
+
* that can be used by o-node, o-client, o-browser, and other implementations.
|
|
6
|
+
*/
|
|
7
|
+
export { IStreamTransport, StreamStatus, StreamTransportConfig, } from './stream-transport.interface.js';
|
|
8
|
+
export { StreamConfig, DEFAULT_STREAM_CONFIG, mergeStreamConfig, } from './stream-config.js';
|
|
9
|
+
export { ProtocolBuilder, ParsedStreamChunk, } from './protocol-builder.js';
|
|
10
|
+
export { StreamHandlerBase, StreamHandlerState, StreamHandlerOptions, } from './stream-handler.base.js';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/streaming/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,qBAAqB,GACtB,MAAM,iCAAiC,CAAC;AAGzC,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,iBAAiB,GAClB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACL,eAAe,EACf,iBAAiB,GAClB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming functionality for o-core
|
|
3
|
+
*
|
|
4
|
+
* This module provides transport-agnostic abstractions for streaming operations
|
|
5
|
+
* that can be used by o-node, o-client, o-browser, and other implementations.
|
|
6
|
+
*/
|
|
7
|
+
// Stream transport interface and types
|
|
8
|
+
export { StreamStatus, } from './stream-transport.interface.js';
|
|
9
|
+
// Stream configuration
|
|
10
|
+
export { DEFAULT_STREAM_CONFIG, mergeStreamConfig, } from './stream-config.js';
|
|
11
|
+
// Protocol builder for JSON-RPC streaming messages
|
|
12
|
+
export { ProtocolBuilder, } from './protocol-builder.js';
|
|
13
|
+
// Base stream handler class
|
|
14
|
+
export { StreamHandlerBase, StreamHandlerState, } from './stream-handler.base.js';
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { JSONRPCStreamChunk, RequestId, ConnectionId, oRequest } from '@olane/o-protocol';
|
|
2
|
+
/**
|
|
3
|
+
* Parsed streaming chunk data
|
|
4
|
+
*/
|
|
5
|
+
export interface ParsedStreamChunk {
|
|
6
|
+
data: unknown;
|
|
7
|
+
sequence: number;
|
|
8
|
+
isLast: boolean;
|
|
9
|
+
connectionId: ConnectionId;
|
|
10
|
+
requestMethod: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Builder class for constructing and parsing JSON-RPC streaming protocol messages.
|
|
14
|
+
* Provides type-safe message construction and parsing for streaming responses.
|
|
15
|
+
*/
|
|
16
|
+
export declare class ProtocolBuilder {
|
|
17
|
+
/**
|
|
18
|
+
* Build a JSON-RPC streaming chunk message
|
|
19
|
+
* @param data - The data payload for this chunk
|
|
20
|
+
* @param sequence - The sequence number of this chunk (starts at 1)
|
|
21
|
+
* @param isLast - Whether this is the final chunk in the stream
|
|
22
|
+
* @param requestId - The JSON-RPC request ID this chunk is responding to
|
|
23
|
+
* @param connectionId - The connection ID from the original request
|
|
24
|
+
* @param requestMethod - The method name from the original request
|
|
25
|
+
* @returns A complete JSON-RPC streaming chunk message
|
|
26
|
+
*/
|
|
27
|
+
static buildStreamChunk(data: unknown, sequence: number, isLast: boolean, requestId: RequestId, connectionId: ConnectionId, requestMethod: string): JSONRPCStreamChunk;
|
|
28
|
+
/**
|
|
29
|
+
* Build a streaming chunk from an oRequest object
|
|
30
|
+
* @param data - The data payload for this chunk
|
|
31
|
+
* @param sequence - The sequence number of this chunk
|
|
32
|
+
* @param isLast - Whether this is the final chunk
|
|
33
|
+
* @param request - The original oRequest
|
|
34
|
+
* @returns A complete JSON-RPC streaming chunk message
|
|
35
|
+
*/
|
|
36
|
+
static buildStreamChunkFromRequest(data: unknown, sequence: number, isLast: boolean, request: oRequest): JSONRPCStreamChunk;
|
|
37
|
+
/**
|
|
38
|
+
* Parse a JSON-RPC response to extract streaming chunk data
|
|
39
|
+
* @param message - The parsed JSON-RPC message
|
|
40
|
+
* @returns Parsed chunk data if this is a streaming response, null otherwise
|
|
41
|
+
*/
|
|
42
|
+
static parseStreamChunk(message: any): ParsedStreamChunk | null;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a message is a streaming response
|
|
45
|
+
* @param message - The message to check
|
|
46
|
+
* @returns true if the message is a streaming response
|
|
47
|
+
*/
|
|
48
|
+
static isStreamingResponse(message: any): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Encode a message to UTF-8 bytes for transmission
|
|
51
|
+
* @param message - The message to encode
|
|
52
|
+
* @returns UTF-8 encoded bytes
|
|
53
|
+
*/
|
|
54
|
+
static encodeMessage(message: JSONRPCStreamChunk): Uint8Array;
|
|
55
|
+
/**
|
|
56
|
+
* Decode UTF-8 bytes to a JSON message
|
|
57
|
+
* @param data - The UTF-8 encoded data
|
|
58
|
+
* @returns Parsed JSON message
|
|
59
|
+
*/
|
|
60
|
+
static decodeMessage(data: Uint8Array): any;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=protocol-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol-builder.d.ts","sourceRoot":"","sources":["../../../src/streaming/protocol-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAElB,SAAS,EACT,YAAY,EAEZ,QAAQ,EACT,MAAM,mBAAmB,CAAC;AAE3B;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;IAChB,YAAY,EAAE,YAAY,CAAC;IAC3B,aAAa,EAAE,MAAM,CAAC;CACvB;AAED;;;GAGG;AACH,qBAAa,eAAe;IAC1B;;;;;;;;;OASG;WACW,gBAAgB,CAC5B,IAAI,EAAE,OAAO,EACb,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,SAAS,EACpB,YAAY,EAAE,YAAY,EAC1B,aAAa,EAAE,MAAM,GACpB,kBAAkB;IAiBrB;;;;;;;OAOG;WACW,2BAA2B,CACvC,IAAI,EAAE,OAAO,EACb,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,OAAO,EACf,OAAO,EAAE,QAAQ,GAChB,kBAAkB;IAWrB;;;;OAIG;WACW,gBAAgB,CAAC,OAAO,EAAE,GAAG,GAAG,iBAAiB,GAAG,IAAI;IAgBtE;;;;OAIG;WACW,mBAAmB,CAAC,OAAO,EAAE,GAAG,GAAG,OAAO;IAaxD;;;;OAIG;WACW,aAAa,CAAC,OAAO,EAAE,kBAAkB,GAAG,UAAU;IAIpE;;;;OAIG;WACW,aAAa,CAAC,IAAI,EAAE,UAAU,GAAG,GAAG;CAInD"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { JSONRPC_VERSION, } from '@olane/o-protocol';
|
|
2
|
+
/**
|
|
3
|
+
* Builder class for constructing and parsing JSON-RPC streaming protocol messages.
|
|
4
|
+
* Provides type-safe message construction and parsing for streaming responses.
|
|
5
|
+
*/
|
|
6
|
+
export class ProtocolBuilder {
|
|
7
|
+
/**
|
|
8
|
+
* Build a JSON-RPC streaming chunk message
|
|
9
|
+
* @param data - The data payload for this chunk
|
|
10
|
+
* @param sequence - The sequence number of this chunk (starts at 1)
|
|
11
|
+
* @param isLast - Whether this is the final chunk in the stream
|
|
12
|
+
* @param requestId - The JSON-RPC request ID this chunk is responding to
|
|
13
|
+
* @param connectionId - The connection ID from the original request
|
|
14
|
+
* @param requestMethod - The method name from the original request
|
|
15
|
+
* @returns A complete JSON-RPC streaming chunk message
|
|
16
|
+
*/
|
|
17
|
+
static buildStreamChunk(data, sequence, isLast, requestId, connectionId, requestMethod) {
|
|
18
|
+
const result = {
|
|
19
|
+
_connectionId: connectionId,
|
|
20
|
+
_requestMethod: requestMethod,
|
|
21
|
+
_streaming: true,
|
|
22
|
+
_sequence: sequence,
|
|
23
|
+
_isLast: isLast,
|
|
24
|
+
_data: data,
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
jsonrpc: JSONRPC_VERSION,
|
|
28
|
+
id: requestId,
|
|
29
|
+
result,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Build a streaming chunk from an oRequest object
|
|
34
|
+
* @param data - The data payload for this chunk
|
|
35
|
+
* @param sequence - The sequence number of this chunk
|
|
36
|
+
* @param isLast - Whether this is the final chunk
|
|
37
|
+
* @param request - The original oRequest
|
|
38
|
+
* @returns A complete JSON-RPC streaming chunk message
|
|
39
|
+
*/
|
|
40
|
+
static buildStreamChunkFromRequest(data, sequence, isLast, request) {
|
|
41
|
+
return this.buildStreamChunk(data, sequence, isLast, request.id, request.params._connectionId, request.method);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Parse a JSON-RPC response to extract streaming chunk data
|
|
45
|
+
* @param message - The parsed JSON-RPC message
|
|
46
|
+
* @returns Parsed chunk data if this is a streaming response, null otherwise
|
|
47
|
+
*/
|
|
48
|
+
static parseStreamChunk(message) {
|
|
49
|
+
if (!this.isStreamingResponse(message)) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const result = message.result;
|
|
53
|
+
return {
|
|
54
|
+
data: result._data,
|
|
55
|
+
sequence: result._sequence,
|
|
56
|
+
isLast: result._isLast,
|
|
57
|
+
connectionId: result._connectionId,
|
|
58
|
+
requestMethod: result._requestMethod,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Check if a message is a streaming response
|
|
63
|
+
* @param message - The message to check
|
|
64
|
+
* @returns true if the message is a streaming response
|
|
65
|
+
*/
|
|
66
|
+
static isStreamingResponse(message) {
|
|
67
|
+
return (message &&
|
|
68
|
+
typeof message === 'object' &&
|
|
69
|
+
message.result &&
|
|
70
|
+
typeof message.result === 'object' &&
|
|
71
|
+
message.result._streaming === true &&
|
|
72
|
+
typeof message.result._sequence === 'number' &&
|
|
73
|
+
typeof message.result._isLast === 'boolean' &&
|
|
74
|
+
'_data' in message.result);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Encode a message to UTF-8 bytes for transmission
|
|
78
|
+
* @param message - The message to encode
|
|
79
|
+
* @returns UTF-8 encoded bytes
|
|
80
|
+
*/
|
|
81
|
+
static encodeMessage(message) {
|
|
82
|
+
return new TextEncoder().encode(JSON.stringify(message));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Decode UTF-8 bytes to a JSON message
|
|
86
|
+
* @param data - The UTF-8 encoded data
|
|
87
|
+
* @returns Parsed JSON message
|
|
88
|
+
*/
|
|
89
|
+
static decodeMessage(data) {
|
|
90
|
+
const text = new TextDecoder().decode(data);
|
|
91
|
+
return JSON.parse(text);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for streaming operations
|
|
3
|
+
*/
|
|
4
|
+
export interface StreamConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Timeout in milliseconds for waiting to read the first chunk
|
|
7
|
+
* @default 120000 (2 minutes)
|
|
8
|
+
*/
|
|
9
|
+
readTimeoutMs: number;
|
|
10
|
+
/**
|
|
11
|
+
* Timeout in milliseconds for drain operations when handling backpressure
|
|
12
|
+
* @default 30000 (30 seconds)
|
|
13
|
+
*/
|
|
14
|
+
drainTimeoutMs: number;
|
|
15
|
+
/**
|
|
16
|
+
* Maximum size of a single chunk in bytes (optional)
|
|
17
|
+
* If not set, chunks can be any size
|
|
18
|
+
*/
|
|
19
|
+
maxChunkSize?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Enable metrics tracking for streaming operations
|
|
22
|
+
* @default true
|
|
23
|
+
*/
|
|
24
|
+
enableMetrics?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Default configuration for streaming operations
|
|
28
|
+
*/
|
|
29
|
+
export declare const DEFAULT_STREAM_CONFIG: StreamConfig;
|
|
30
|
+
/**
|
|
31
|
+
* Merge user config with defaults
|
|
32
|
+
* @param userConfig - Partial user configuration
|
|
33
|
+
* @returns Complete configuration with defaults applied
|
|
34
|
+
*/
|
|
35
|
+
export declare function mergeStreamConfig(userConfig?: Partial<StreamConfig>): StreamConfig;
|
|
36
|
+
//# sourceMappingURL=stream-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-config.d.ts","sourceRoot":"","sources":["../../../src/streaming/stream-config.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,aAAa,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,cAAc,EAAE,MAAM,CAAC;IAEvB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,eAAO,MAAM,qBAAqB,EAAE,YAInC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,UAAU,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,GACjC,YAAY,CAKd"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default configuration for streaming operations
|
|
3
|
+
*/
|
|
4
|
+
export const DEFAULT_STREAM_CONFIG = {
|
|
5
|
+
readTimeoutMs: 120000, // 2 minutes
|
|
6
|
+
drainTimeoutMs: 30000, // 30 seconds
|
|
7
|
+
enableMetrics: true,
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Merge user config with defaults
|
|
11
|
+
* @param userConfig - Partial user configuration
|
|
12
|
+
* @returns Complete configuration with defaults applied
|
|
13
|
+
*/
|
|
14
|
+
export function mergeStreamConfig(userConfig) {
|
|
15
|
+
return {
|
|
16
|
+
...DEFAULT_STREAM_CONFIG,
|
|
17
|
+
...userConfig,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { oError } from '../error/o-error.js';
|
|
2
|
+
import { oRequest } from '@olane/o-protocol';
|
|
3
|
+
/**
|
|
4
|
+
* State machine states for streaming operations
|
|
5
|
+
*/
|
|
6
|
+
export declare enum StreamHandlerState {
|
|
7
|
+
IDLE = "idle",
|
|
8
|
+
STREAMING = "streaming",
|
|
9
|
+
COMPLETED = "completed",
|
|
10
|
+
ERROR = "error"
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Options for stream handler operations
|
|
14
|
+
*/
|
|
15
|
+
export interface StreamHandlerOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Enable metrics tracking
|
|
18
|
+
*/
|
|
19
|
+
enableMetrics?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Abstract base class for handling streaming operations.
|
|
23
|
+
* Provides transport-agnostic AsyncGenerator iteration, chunk sequencing,
|
|
24
|
+
* error handling, and state management.
|
|
25
|
+
*
|
|
26
|
+
* Implementations must provide the sendChunk method for transport-specific
|
|
27
|
+
* chunk transmission.
|
|
28
|
+
*/
|
|
29
|
+
export declare abstract class StreamHandlerBase {
|
|
30
|
+
protected state: StreamHandlerState;
|
|
31
|
+
protected currentSequence: number;
|
|
32
|
+
protected options: StreamHandlerOptions;
|
|
33
|
+
constructor(options?: StreamHandlerOptions);
|
|
34
|
+
/**
|
|
35
|
+
* Get the current state of the stream handler
|
|
36
|
+
*/
|
|
37
|
+
getState(): StreamHandlerState;
|
|
38
|
+
/**
|
|
39
|
+
* Get the current sequence number
|
|
40
|
+
*/
|
|
41
|
+
getCurrentSequence(): number;
|
|
42
|
+
/**
|
|
43
|
+
* Abstract method that implementations must provide to send a chunk.
|
|
44
|
+
* This method should handle the actual transmission using the specific transport.
|
|
45
|
+
*
|
|
46
|
+
* @param chunk - The data chunk to send
|
|
47
|
+
* @param sequence - The sequence number of this chunk
|
|
48
|
+
* @param isLast - Whether this is the final chunk
|
|
49
|
+
* @param request - The original request
|
|
50
|
+
*/
|
|
51
|
+
protected abstract sendChunk(chunk: unknown, sequence: number, isLast: boolean, request: oRequest): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Optional hook called when streaming starts
|
|
54
|
+
*/
|
|
55
|
+
protected onStreamStart?(request: oRequest): void;
|
|
56
|
+
/**
|
|
57
|
+
* Optional hook called when streaming completes successfully
|
|
58
|
+
*/
|
|
59
|
+
protected onStreamComplete?(request: oRequest, totalChunks: number): void;
|
|
60
|
+
/**
|
|
61
|
+
* Optional hook called when streaming encounters an error
|
|
62
|
+
*/
|
|
63
|
+
protected onStreamError?(request: oRequest, error: oError): void;
|
|
64
|
+
/**
|
|
65
|
+
* Handle streaming of an AsyncGenerator.
|
|
66
|
+
* Iterates through the generator, sends each chunk with proper sequencing,
|
|
67
|
+
* and handles errors appropriately.
|
|
68
|
+
*
|
|
69
|
+
* @param generator - The AsyncGenerator to stream
|
|
70
|
+
* @param request - The original request
|
|
71
|
+
* @returns Promise that resolves when streaming is complete
|
|
72
|
+
*/
|
|
73
|
+
handleStream(generator: AsyncGenerator<any>, request: oRequest): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Convert any error to an oError instance
|
|
76
|
+
* @param error - The error to convert
|
|
77
|
+
* @returns An oError instance
|
|
78
|
+
*/
|
|
79
|
+
protected convertToOError(error: unknown): oError;
|
|
80
|
+
/**
|
|
81
|
+
* Reset the handler state for reuse
|
|
82
|
+
*/
|
|
83
|
+
reset(): void;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=stream-handler.base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-handler.base.d.ts","sourceRoot":"","sources":["../../../src/streaming/stream-handler.base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAE7C,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C;;GAEG;AACH,oBAAY,kBAAkB;IAC5B,IAAI,SAAS;IACb,SAAS,cAAc;IACvB,SAAS,cAAc;IACvB,KAAK,UAAU;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;;GAOG;AACH,8BAAsB,iBAAiB;IACrC,SAAS,CAAC,KAAK,EAAE,kBAAkB,CAA2B;IAC9D,SAAS,CAAC,eAAe,EAAE,MAAM,CAAK;IACtC,SAAS,CAAC,OAAO,EAAE,oBAAoB,CAAC;gBAE5B,OAAO,GAAE,oBAAyB;IAO9C;;OAEG;IACI,QAAQ,IAAI,kBAAkB;IAIrC;;OAEG;IACI,kBAAkB,IAAI,MAAM;IAInC;;;;;;;;OAQG;IACH,SAAS,CAAC,QAAQ,CAAC,SAAS,CAC1B,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,OAAO,EACf,OAAO,EAAE,QAAQ,GAChB,OAAO,CAAC,IAAI,CAAC;IAEhB;;OAEG;IACH,SAAS,CAAC,aAAa,CAAC,CAAC,OAAO,EAAE,QAAQ,GAAG,IAAI;IAEjD;;OAEG;IACH,SAAS,CAAC,gBAAgB,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI;IAEzE;;OAEG;IACH,SAAS,CAAC,aAAa,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAEhE;;;;;;;;OAQG;IACU,YAAY,CACvB,SAAS,EAAE,cAAc,CAAC,GAAG,CAAC,EAC9B,OAAO,EAAE,QAAQ,GAChB,OAAO,CAAC,IAAI,CAAC;IAqDhB;;;;OAIG;IACH,SAAS,CAAC,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM;IAoBjD;;OAEG;IACI,KAAK,IAAI,IAAI;CAIrB"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { oError } from '../error/o-error.js';
|
|
2
|
+
import { oErrorCodes } from '../error/enums/codes.error.js';
|
|
3
|
+
/**
|
|
4
|
+
* State machine states for streaming operations
|
|
5
|
+
*/
|
|
6
|
+
export var StreamHandlerState;
|
|
7
|
+
(function (StreamHandlerState) {
|
|
8
|
+
StreamHandlerState["IDLE"] = "idle";
|
|
9
|
+
StreamHandlerState["STREAMING"] = "streaming";
|
|
10
|
+
StreamHandlerState["COMPLETED"] = "completed";
|
|
11
|
+
StreamHandlerState["ERROR"] = "error";
|
|
12
|
+
})(StreamHandlerState || (StreamHandlerState = {}));
|
|
13
|
+
/**
|
|
14
|
+
* Abstract base class for handling streaming operations.
|
|
15
|
+
* Provides transport-agnostic AsyncGenerator iteration, chunk sequencing,
|
|
16
|
+
* error handling, and state management.
|
|
17
|
+
*
|
|
18
|
+
* Implementations must provide the sendChunk method for transport-specific
|
|
19
|
+
* chunk transmission.
|
|
20
|
+
*/
|
|
21
|
+
export class StreamHandlerBase {
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.state = StreamHandlerState.IDLE;
|
|
24
|
+
this.currentSequence = 0;
|
|
25
|
+
this.options = {
|
|
26
|
+
enableMetrics: true,
|
|
27
|
+
...options,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Get the current state of the stream handler
|
|
32
|
+
*/
|
|
33
|
+
getState() {
|
|
34
|
+
return this.state;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get the current sequence number
|
|
38
|
+
*/
|
|
39
|
+
getCurrentSequence() {
|
|
40
|
+
return this.currentSequence;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Handle streaming of an AsyncGenerator.
|
|
44
|
+
* Iterates through the generator, sends each chunk with proper sequencing,
|
|
45
|
+
* and handles errors appropriately.
|
|
46
|
+
*
|
|
47
|
+
* @param generator - The AsyncGenerator to stream
|
|
48
|
+
* @param request - The original request
|
|
49
|
+
* @returns Promise that resolves when streaming is complete
|
|
50
|
+
*/
|
|
51
|
+
async handleStream(generator, request) {
|
|
52
|
+
this.state = StreamHandlerState.STREAMING;
|
|
53
|
+
this.currentSequence = 0;
|
|
54
|
+
// Call optional hook
|
|
55
|
+
if (this.onStreamStart) {
|
|
56
|
+
this.onStreamStart(request);
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
// Iterate through the generator and send each chunk
|
|
60
|
+
for await (const chunk of generator) {
|
|
61
|
+
this.currentSequence++;
|
|
62
|
+
await this.sendChunk(chunk, this.currentSequence, false, request);
|
|
63
|
+
}
|
|
64
|
+
// Send final empty chunk to signal completion
|
|
65
|
+
await this.sendChunk(null, this.currentSequence, true, request);
|
|
66
|
+
this.state = StreamHandlerState.COMPLETED;
|
|
67
|
+
// Call optional hook
|
|
68
|
+
if (this.onStreamComplete) {
|
|
69
|
+
this.onStreamComplete(request, this.currentSequence);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
this.state = StreamHandlerState.ERROR;
|
|
74
|
+
// Convert error to oError if needed
|
|
75
|
+
const oErrorInstance = this.convertToOError(error);
|
|
76
|
+
// Call optional hook
|
|
77
|
+
if (this.onStreamError) {
|
|
78
|
+
this.onStreamError(request, oErrorInstance);
|
|
79
|
+
}
|
|
80
|
+
// Send error as final chunk
|
|
81
|
+
try {
|
|
82
|
+
await this.sendChunk(oErrorInstance.toJSON(), this.currentSequence, true, request);
|
|
83
|
+
}
|
|
84
|
+
catch (sendError) {
|
|
85
|
+
console.error('Failed to send error chunk:', sendError);
|
|
86
|
+
throw oErrorInstance;
|
|
87
|
+
}
|
|
88
|
+
throw oErrorInstance;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Convert any error to an oError instance
|
|
93
|
+
* @param error - The error to convert
|
|
94
|
+
* @returns An oError instance
|
|
95
|
+
*/
|
|
96
|
+
convertToOError(error) {
|
|
97
|
+
if (error instanceof oError) {
|
|
98
|
+
return error;
|
|
99
|
+
}
|
|
100
|
+
if (error instanceof Error) {
|
|
101
|
+
return new oError(oErrorCodes.UNKNOWN, error.message, { stack: error.stack });
|
|
102
|
+
}
|
|
103
|
+
return new oError(oErrorCodes.UNKNOWN, String(error), { originalError: error });
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Reset the handler state for reuse
|
|
107
|
+
*/
|
|
108
|
+
reset() {
|
|
109
|
+
this.state = StreamHandlerState.IDLE;
|
|
110
|
+
this.currentSequence = 0;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stream status states
|
|
3
|
+
*/
|
|
4
|
+
export declare enum StreamStatus {
|
|
5
|
+
OPEN = "open",
|
|
6
|
+
CLOSING = "closing",
|
|
7
|
+
CLOSED = "closed",
|
|
8
|
+
RESET = "reset"
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Transport-agnostic interface for stream operations.
|
|
12
|
+
* Implementations can use libp2p, WebSockets, HTTP/2, or any other streaming transport.
|
|
13
|
+
*/
|
|
14
|
+
export interface IStreamTransport {
|
|
15
|
+
/**
|
|
16
|
+
* Send data through the stream with backpressure handling
|
|
17
|
+
* @param data - The data to send (UTF-8 encoded bytes)
|
|
18
|
+
* @returns Promise that resolves when data is sent (handles backpressure internally)
|
|
19
|
+
*/
|
|
20
|
+
send(data: Uint8Array): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Set up a message handler for incoming data
|
|
23
|
+
* @param handler - Callback function to handle incoming messages
|
|
24
|
+
*/
|
|
25
|
+
onMessage(handler: (data: Uint8Array) => void): void;
|
|
26
|
+
/**
|
|
27
|
+
* Remove the message handler
|
|
28
|
+
*/
|
|
29
|
+
removeMessageHandler(): void;
|
|
30
|
+
/**
|
|
31
|
+
* Close the stream and clean up resources
|
|
32
|
+
* @returns Promise that resolves when stream is closed
|
|
33
|
+
*/
|
|
34
|
+
close(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Get the current status of the stream
|
|
37
|
+
* @returns The current stream status
|
|
38
|
+
*/
|
|
39
|
+
getStatus(): StreamStatus;
|
|
40
|
+
/**
|
|
41
|
+
* Check if the stream is in a usable state
|
|
42
|
+
* @returns true if stream can send/receive data
|
|
43
|
+
*/
|
|
44
|
+
isOpen(): boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Configuration options for stream transport operations
|
|
48
|
+
*/
|
|
49
|
+
export interface StreamTransportConfig {
|
|
50
|
+
/**
|
|
51
|
+
* Timeout in milliseconds for drain operations when handling backpressure
|
|
52
|
+
* @default 30000 (30 seconds)
|
|
53
|
+
*/
|
|
54
|
+
drainTimeoutMs?: number;
|
|
55
|
+
/**
|
|
56
|
+
* Maximum time in milliseconds to wait for the first message
|
|
57
|
+
* @default 120000 (2 minutes)
|
|
58
|
+
*/
|
|
59
|
+
readTimeoutMs?: number;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=stream-transport.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-transport.interface.d.ts","sourceRoot":"","sources":["../../../src/streaming/stream-transport.interface.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,YAAY;IACtB,IAAI,SAAS;IACb,OAAO,YAAY;IACnB,MAAM,WAAW;IACjB,KAAK,UAAU;CAChB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEtC;;;OAGG;IACH,SAAS,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,GAAG,IAAI,CAAC;IAErD;;OAEG;IACH,oBAAoB,IAAI,IAAI,CAAC;IAE7B;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB;;;OAGG;IACH,SAAS,IAAI,YAAY,CAAC;IAE1B;;;OAGG;IACH,MAAM,IAAI,OAAO,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stream status states
|
|
3
|
+
*/
|
|
4
|
+
export var StreamStatus;
|
|
5
|
+
(function (StreamStatus) {
|
|
6
|
+
StreamStatus["OPEN"] = "open";
|
|
7
|
+
StreamStatus["CLOSING"] = "closing";
|
|
8
|
+
StreamStatus["CLOSED"] = "closed";
|
|
9
|
+
StreamStatus["RESET"] = "reset";
|
|
10
|
+
})(StreamStatus || (StreamStatus = {}));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Stream
|
|
1
|
+
import { Stream } from '@olane/o-config';
|
|
2
2
|
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';
|
|
@@ -25,7 +25,10 @@ export declare class CoreUtils {
|
|
|
25
25
|
static phraseToSeed(phrase: string): string;
|
|
26
26
|
static childAddress(parentAddress: oAddress, childAddress: oAddress): oAddress;
|
|
27
27
|
static sendResponse(response: oResponse, stream: Stream): Promise<void>;
|
|
28
|
-
static
|
|
28
|
+
static sendStreamResponse(response: oResponse, stream: Stream): Promise<void>;
|
|
29
|
+
static processStream(event: any): Promise<any>;
|
|
30
|
+
static processStreamRequest(event: any): Promise<oRequest>;
|
|
31
|
+
static processStreamResponse(event: any): Promise<oResponse>;
|
|
29
32
|
static toCID(data: any): Promise<CID>;
|
|
30
33
|
}
|
|
31
34
|
//# sourceMappingURL=core.utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.utils.d.ts","sourceRoot":"","sources":["../../../src/utils/core.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,MAAM,
|
|
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,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,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,SAAS,CAAC;WAKrD,KAAK,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;CAMnD"}
|
|
@@ -100,9 +100,23 @@ export class CoreUtils {
|
|
|
100
100
|
console.error('Error sending response: ', error);
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
+
static async sendStreamResponse(response, stream) {
|
|
104
|
+
if (stream.status !== 'open') {
|
|
105
|
+
throw new Error('Stream is not open');
|
|
106
|
+
}
|
|
107
|
+
await stream.send(new TextEncoder().encode(response.toString()));
|
|
108
|
+
}
|
|
103
109
|
static async processStream(event) {
|
|
104
110
|
const bytes = event.data instanceof Uint8ArrayList ? event.data.subarray() : event.data;
|
|
105
|
-
return
|
|
111
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
112
|
+
}
|
|
113
|
+
static async processStreamRequest(event) {
|
|
114
|
+
const req = await CoreUtils.processStream(event);
|
|
115
|
+
return new oRequest(req);
|
|
116
|
+
}
|
|
117
|
+
static async processStreamResponse(event) {
|
|
118
|
+
const res = await CoreUtils.processStream(event);
|
|
119
|
+
return new oResponse(res);
|
|
106
120
|
}
|
|
107
121
|
static async toCID(data) {
|
|
108
122
|
const bytes = json.encode(data);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard to check if a value is an AsyncGenerator
|
|
3
|
+
* @param value - The value to check
|
|
4
|
+
* @returns true if the value is an AsyncGenerator
|
|
5
|
+
*/
|
|
6
|
+
export declare function isAsyncGenerator(value: any): value is AsyncGenerator;
|
|
7
|
+
/**
|
|
8
|
+
* Safely iterate through an AsyncGenerator with error handling
|
|
9
|
+
* @param generator - The AsyncGenerator to iterate
|
|
10
|
+
* @param onChunk - Callback for each chunk
|
|
11
|
+
* @param onError - Optional error handler
|
|
12
|
+
* @returns Promise that resolves when iteration is complete
|
|
13
|
+
*/
|
|
14
|
+
export declare function iterateAsyncGenerator<T>(generator: AsyncGenerator<T>, onChunk: (chunk: T) => Promise<void> | void, onError?: (error: unknown) => void): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Collect all chunks from an AsyncGenerator into an array
|
|
17
|
+
* Useful for testing or converting streaming responses to non-streaming
|
|
18
|
+
* @param generator - The AsyncGenerator to collect from
|
|
19
|
+
* @returns Promise resolving to array of all chunks
|
|
20
|
+
*/
|
|
21
|
+
export declare function collectStreamChunks<T>(generator: AsyncGenerator<T>): Promise<T[]>;
|
|
22
|
+
//# sourceMappingURL=streaming.utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.utils.d.ts","sourceRoot":"","sources":["../../../src/utils/streaming.utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,cAAc,CAMpE;AAED;;;;;;GAMG;AACH,wBAAsB,qBAAqB,CAAC,CAAC,EAC3C,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,EAC5B,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,EAC3C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GACjC,OAAO,CAAC,IAAI,CAAC,CAYf;AAED;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,CAAC,EACzC,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,GAC3B,OAAO,CAAC,CAAC,EAAE,CAAC,CAMd"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard to check if a value is an AsyncGenerator
|
|
3
|
+
* @param value - The value to check
|
|
4
|
+
* @returns true if the value is an AsyncGenerator
|
|
5
|
+
*/
|
|
6
|
+
export function isAsyncGenerator(value) {
|
|
7
|
+
return (value != null &&
|
|
8
|
+
typeof value === 'object' &&
|
|
9
|
+
typeof value[Symbol.asyncIterator] === 'function');
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Safely iterate through an AsyncGenerator with error handling
|
|
13
|
+
* @param generator - The AsyncGenerator to iterate
|
|
14
|
+
* @param onChunk - Callback for each chunk
|
|
15
|
+
* @param onError - Optional error handler
|
|
16
|
+
* @returns Promise that resolves when iteration is complete
|
|
17
|
+
*/
|
|
18
|
+
export async function iterateAsyncGenerator(generator, onChunk, onError) {
|
|
19
|
+
try {
|
|
20
|
+
for await (const chunk of generator) {
|
|
21
|
+
await onChunk(chunk);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (onError) {
|
|
26
|
+
onError(error);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Collect all chunks from an AsyncGenerator into an array
|
|
35
|
+
* Useful for testing or converting streaming responses to non-streaming
|
|
36
|
+
* @param generator - The AsyncGenerator to collect from
|
|
37
|
+
* @returns Promise resolving to array of all chunks
|
|
38
|
+
*/
|
|
39
|
+
export async function collectStreamChunks(generator) {
|
|
40
|
+
const chunks = [];
|
|
41
|
+
for await (const chunk of generator) {
|
|
42
|
+
chunks.push(chunk);
|
|
43
|
+
}
|
|
44
|
+
return chunks;
|
|
45
|
+
}
|
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.24",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"types": "dist/src/index.d.ts",
|
|
@@ -8,6 +8,14 @@
|
|
|
8
8
|
".": {
|
|
9
9
|
"types": "./dist/src/index.d.ts",
|
|
10
10
|
"default": "./dist/src/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./streaming": {
|
|
13
|
+
"types": "./dist/src/streaming/index.d.ts",
|
|
14
|
+
"default": "./dist/src/streaming/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./utils/streaming.utils": {
|
|
17
|
+
"types": "./dist/src/utils/streaming.utils.d.ts",
|
|
18
|
+
"default": "./dist/src/utils/streaming.utils.js"
|
|
11
19
|
}
|
|
12
20
|
},
|
|
13
21
|
"files": [
|
|
@@ -55,11 +63,11 @@
|
|
|
55
63
|
"typescript": "^5.8.3"
|
|
56
64
|
},
|
|
57
65
|
"dependencies": {
|
|
58
|
-
"@olane/o-protocol": "0.7.12-alpha.
|
|
66
|
+
"@olane/o-protocol": "0.7.12-alpha.24",
|
|
59
67
|
"chalk": "^5.4.1",
|
|
60
68
|
"debug": "^4.4.1",
|
|
61
69
|
"dotenv": "^16.5.0",
|
|
62
70
|
"multiformats": "^13.3.7"
|
|
63
71
|
},
|
|
64
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "ac75e0acc3a0a232a5cdeb9271060d90cf1e5a2c"
|
|
65
73
|
}
|