@olane/o-node 0.7.40 → 0.7.42

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.
Files changed (45) hide show
  1. package/dist/src/connection/index.d.ts +1 -1
  2. package/dist/src/connection/index.d.ts.map +1 -1
  3. package/dist/src/connection/index.js +1 -1
  4. package/dist/src/connection/interfaces/o-node-connection-stream.config.d.ts +8 -0
  5. package/dist/src/connection/interfaces/o-node-connection-stream.config.d.ts.map +1 -0
  6. package/dist/src/connection/interfaces/o-node-connection-stream.config.js +1 -0
  7. package/dist/src/connection/o-node-connection-stream.d.ts +34 -0
  8. package/dist/src/connection/o-node-connection-stream.d.ts.map +1 -0
  9. package/dist/src/connection/o-node-connection-stream.js +68 -0
  10. package/dist/src/connection/o-node-connection.d.ts +10 -4
  11. package/dist/src/connection/o-node-connection.d.ts.map +1 -1
  12. package/dist/src/connection/o-node-connection.js +51 -34
  13. package/dist/src/connection/o-node-connection.manager.d.ts +4 -0
  14. package/dist/src/connection/o-node-connection.manager.d.ts.map +1 -1
  15. package/dist/src/connection/o-node-connection.manager.js +24 -0
  16. package/dist/src/connection/stream-handler.d.ts +1 -59
  17. package/dist/src/connection/stream-handler.d.ts.map +1 -1
  18. package/dist/src/connection/stream-handler.js +104 -179
  19. package/dist/src/o-node.tool.d.ts +3 -1
  20. package/dist/src/o-node.tool.d.ts.map +1 -1
  21. package/dist/src/o-node.tool.js +44 -11
  22. package/dist/src/router/o-node.address.d.ts +1 -0
  23. package/dist/src/router/o-node.address.d.ts.map +1 -1
  24. package/dist/src/router/o-node.address.js +4 -0
  25. package/dist/src/utils/connection.utils.d.ts +9 -0
  26. package/dist/src/utils/connection.utils.d.ts.map +1 -0
  27. package/dist/src/utils/connection.utils.js +48 -0
  28. package/dist/src/utils/index.d.ts +1 -0
  29. package/dist/src/utils/index.d.ts.map +1 -1
  30. package/dist/src/utils/index.js +1 -0
  31. package/dist/test/astream-reuse.spec.d.ts +2 -0
  32. package/dist/test/astream-reuse.spec.d.ts.map +1 -0
  33. package/dist/test/astream-reuse.spec.js +107 -0
  34. package/dist/test/connection-management.spec.js +1 -0
  35. package/dist/test/helpers/network-builder.d.ts.map +1 -1
  36. package/package.json +7 -7
  37. package/dist/src/connection/o-managed-stream.d.ts +0 -57
  38. package/dist/src/connection/o-managed-stream.d.ts.map +0 -1
  39. package/dist/src/connection/o-managed-stream.js +0 -76
  40. package/dist/test/o-managed-stream.spec.d.ts +0 -2
  41. package/dist/test/o-managed-stream.spec.d.ts.map +0 -1
  42. package/dist/test/o-managed-stream.spec.js +0 -122
  43. package/dist/test/stream-handler-caching.spec.d.ts +0 -2
  44. package/dist/test/stream-handler-caching.spec.d.ts.map +0 -1
  45. package/dist/test/stream-handler-caching.spec.js +0 -261
@@ -1,261 +0,0 @@
1
- import { expect } from 'chai';
2
- import { StreamHandler } from '../src/connection/stream-handler.js';
3
- import { oAddress } from '@olane/o-core';
4
- describe('StreamHandler Address-Based Caching', () => {
5
- let streamHandler;
6
- let mockConnection;
7
- let mockStream;
8
- let callerAddress;
9
- let receiverAddress;
10
- beforeEach(() => {
11
- streamHandler = new StreamHandler();
12
- // Create mock connection
13
- mockConnection = {
14
- status: 'open',
15
- remotePeer: {
16
- toString: () => 'test-peer-id',
17
- },
18
- remoteAddr: {
19
- toString: () => '/memory/test',
20
- },
21
- streams: [],
22
- newStream: async () => mockStream,
23
- };
24
- // Create mock stream
25
- mockStream = {
26
- id: 'test-stream-id',
27
- status: 'open',
28
- writeStatus: 'writable',
29
- remoteReadStatus: 'readable',
30
- direction: 'outbound',
31
- protocol: '/o/test',
32
- addEventListener: () => { },
33
- };
34
- callerAddress = new oAddress('o://caller');
35
- receiverAddress = new oAddress('o://receiver');
36
- });
37
- describe('getOrCreateStream with Address-Based Caching', () => {
38
- it('should create new stream when cache is empty', async () => {
39
- const config = {
40
- reusePolicy: 'reuse',
41
- };
42
- const streamAddresses = {
43
- callerAddress,
44
- receiverAddress,
45
- direction: 'outbound',
46
- };
47
- const stream = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
48
- expect(stream).to.equal(mockStream);
49
- });
50
- it('should reuse cached stream with same caller-receiver pair', async () => {
51
- const config = {
52
- reusePolicy: 'reuse',
53
- };
54
- const streamAddresses = {
55
- callerAddress,
56
- receiverAddress,
57
- direction: 'outbound',
58
- };
59
- // First call creates and caches
60
- const stream1 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
61
- // Second call should reuse
62
- const stream2 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
63
- expect(stream1).to.equal(stream2);
64
- });
65
- it('should reuse stream bidirectionally (A→B same as B→A)', async () => {
66
- const config = {
67
- reusePolicy: 'reuse',
68
- };
69
- // First: caller → receiver
70
- const streamAtoB = {
71
- callerAddress,
72
- receiverAddress,
73
- direction: 'outbound',
74
- };
75
- const stream1 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAtoB);
76
- // Second: receiver → caller (reversed)
77
- const streamBtoA = {
78
- callerAddress: receiverAddress,
79
- receiverAddress: callerAddress,
80
- direction: 'inbound',
81
- };
82
- const stream2 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamBtoA);
83
- // Should be the same stream due to bidirectional caching
84
- expect(stream1).to.equal(stream2);
85
- });
86
- it('should create new stream for different address pairs', async () => {
87
- const config = {
88
- reusePolicy: 'reuse',
89
- };
90
- const otherReceiver = new oAddress('o://other-receiver');
91
- // Create new mock stream for second call
92
- const mockStream2 = {
93
- ...mockStream,
94
- id: 'test-stream-id-2',
95
- };
96
- let streamCount = 0;
97
- mockConnection.newStream = async () => {
98
- streamCount++;
99
- return (streamCount === 1 ? mockStream : mockStream2);
100
- };
101
- // First pair
102
- const stream1 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, {
103
- callerAddress,
104
- receiverAddress,
105
- direction: 'outbound',
106
- });
107
- // Different pair
108
- const stream2 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, {
109
- callerAddress,
110
- receiverAddress: otherReceiver,
111
- direction: 'outbound',
112
- });
113
- // Should be different streams
114
- expect(stream1.id).to.not.equal(stream2.id);
115
- });
116
- it('should not cache when reusePolicy is none', async () => {
117
- const config = {
118
- reusePolicy: 'none',
119
- };
120
- const streamAddresses = {
121
- callerAddress,
122
- receiverAddress,
123
- direction: 'outbound',
124
- };
125
- let callCount = 0;
126
- mockConnection.newStream = async () => {
127
- callCount++;
128
- return {
129
- ...mockStream,
130
- id: `stream-${callCount}`,
131
- };
132
- };
133
- const stream1 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
134
- const stream2 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
135
- // Should be different streams (not cached)
136
- expect(stream1.id).to.not.equal(stream2.id);
137
- });
138
- it('should remove non-reusable streams from cache', async () => {
139
- const config = {
140
- reusePolicy: 'reuse',
141
- };
142
- const streamAddresses = {
143
- callerAddress,
144
- receiverAddress,
145
- direction: 'outbound',
146
- };
147
- // First call creates and caches
148
- const stream1 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
149
- // Make stream non-reusable
150
- mockStream.status = 'closed';
151
- // Create new mock stream for second call
152
- const mockStream2 = {
153
- ...mockStream,
154
- id: 'test-stream-id-2',
155
- status: 'open',
156
- };
157
- mockConnection.newStream = async () => mockStream2;
158
- // Second call should create new stream (old one not reusable)
159
- const stream2 = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
160
- expect(stream1.id).to.not.equal(stream2.id);
161
- });
162
- it('should fall back to protocol-based check when addresses not provided', async () => {
163
- const config = {
164
- reusePolicy: 'reuse',
165
- };
166
- // Add mock stream to connection streams
167
- mockConnection.streams = [mockStream];
168
- // Call without streamAddresses
169
- const stream = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config);
170
- // Should find and return the existing stream by protocol
171
- expect(stream).to.equal(mockStream);
172
- });
173
- });
174
- describe('extractRemotePeerAddress', () => {
175
- it('should extract address from connection peer ID', () => {
176
- const address = streamHandler.extractRemotePeerAddress(mockConnection);
177
- expect(address.value).to.include('o://peer/');
178
- expect(address.value).to.include('test-peer-id');
179
- });
180
- });
181
- describe('cacheInboundStream', () => {
182
- it('should cache inbound stream when reuse is enabled', async () => {
183
- streamHandler.cacheInboundStream(mockStream, callerAddress, receiverAddress, 'reuse');
184
- // Try to get the cached stream
185
- const config = {
186
- reusePolicy: 'reuse',
187
- };
188
- const streamAddresses = {
189
- callerAddress,
190
- receiverAddress,
191
- direction: 'outbound',
192
- };
193
- const stream = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
194
- // Should reuse the cached inbound stream
195
- expect(stream).to.equal(mockStream);
196
- });
197
- it('should not cache when reusePolicy is none', async () => {
198
- streamHandler.cacheInboundStream(mockStream, callerAddress, receiverAddress, 'none');
199
- // Try to get stream - should create new one
200
- const config = {
201
- reusePolicy: 'reuse',
202
- };
203
- const newMockStream = { ...mockStream, id: 'new-stream-id' };
204
- mockConnection.newStream = async () => newMockStream;
205
- const streamAddresses = {
206
- callerAddress,
207
- receiverAddress,
208
- direction: 'outbound',
209
- };
210
- const stream = await streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
211
- // Should not reuse (was not cached)
212
- expect(stream.id).to.equal('new-stream-id');
213
- });
214
- it('should not cache duplicate streams', () => {
215
- // Cache first time
216
- streamHandler.cacheInboundStream(mockStream, callerAddress, receiverAddress, 'reuse');
217
- // Try to cache again - should be no-op
218
- streamHandler.cacheInboundStream(mockStream, callerAddress, receiverAddress, 'reuse');
219
- // No error should occur, and stream should still be cached
220
- // (This test mainly ensures no exception is thrown)
221
- });
222
- });
223
- describe('Stream Cleanup', () => {
224
- it('should remove stream from cache on close event', (done) => {
225
- const config = {
226
- reusePolicy: 'reuse',
227
- };
228
- let closeListener;
229
- // Override addEventListener to capture the close listener
230
- mockStream.addEventListener = (event, listener) => {
231
- if (event === 'close') {
232
- closeListener = listener;
233
- }
234
- };
235
- const streamAddresses = {
236
- callerAddress,
237
- receiverAddress,
238
- direction: 'outbound',
239
- };
240
- streamHandler
241
- .getOrCreateStream(mockConnection, '/o/test', config, streamAddresses)
242
- .then(() => {
243
- expect(closeListener).to.not.be.undefined;
244
- // Simulate close event
245
- if (closeListener) {
246
- closeListener();
247
- }
248
- // Create new stream for next call
249
- const newMockStream = { ...mockStream, id: 'new-stream-after-close' };
250
- mockConnection.newStream = async () => newMockStream;
251
- // Try to get stream again - should create new one since old one was removed
252
- return streamHandler.getOrCreateStream(mockConnection, '/o/test', config, streamAddresses);
253
- })
254
- .then((stream) => {
255
- expect(stream.id).to.equal('new-stream-after-close');
256
- done();
257
- })
258
- .catch(done);
259
- });
260
- });
261
- });