@olane/o-node 0.7.11 → 0.7.12-alpha.3

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/LICENSE ADDED
@@ -0,0 +1,34 @@
1
+ # Dual License: MIT OR Apache-2.0
2
+
3
+ Copyright (c) 2025 Olane Inc.
4
+
5
+ Olane OS is dual-licensed under your choice of either:
6
+
7
+ - **MIT License** (LICENSE-MIT or https://opensource.org/licenses/MIT)
8
+ - **Apache License, Version 2.0** (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
9
+
10
+ ## Why Dual Licensing?
11
+
12
+ Dual licensing provides developers with flexibility when integrating Olane OS into their projects:
13
+
14
+ - **Choose MIT**: If you prefer a simple, permissive license without patent provisions
15
+ - **Choose Apache 2.0**: If you desire explicit patent protection and other features of the Apache license
16
+
17
+ You may choose either license to govern your use of this software.
18
+
19
+ ## License Terms
20
+
21
+ ### MIT License
22
+ The MIT license is very permissive and allows users to do almost anything with the software, including using, copying, modifying, merging, publishing, distributing, and selling it. The primary requirement is that the original copyright and license notice must be included in all copies of the software.
23
+
24
+ See LICENSE-MIT for the full license text.
25
+
26
+ ### Apache License 2.0
27
+ The Apache License 2.0 is also a permissive free software license. It grants users the right to use, modify, and distribute the software. It also includes provisions regarding patent grants, which are not present in the MIT license.
28
+
29
+ See LICENSE-APACHE for the full license text.
30
+
31
+ ## Contributing
32
+
33
+ Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual-licensed as above, without any additional terms or conditions.
34
+
@@ -1 +1 @@
1
- {"version":3,"file":"o-node.search-resolver.d.ts","sourceRoot":"","sources":["../../../../src/router/resolvers/o-node.search-resolver.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,gBAAgB,EAChB,KAAK,EAEL,UAAU,EACV,cAAc,EAEd,aAAa,EAEd,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AA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AE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC;CA8D/D"}
@@ -228,7 +228,6 @@ export class oSearchResolver extends oAddressResolver {
228
228
  // Filter and select result
229
229
  const filteredResults = this.filterSearchResults(searchResponse.result.data, node);
230
230
  const selectedResult = this.selectResult(filteredResults);
231
- this.logger.debug('Selecting result:', selectedResult?.address || 'no result found');
232
231
  // Early return: if no result found, return original address
233
232
  if (!selectedResult) {
234
233
  return {
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=search-resolver.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"search-resolver.spec.d.ts","sourceRoot":"","sources":["../../test/search-resolver.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,614 @@
1
+ import { expect } from 'chai';
2
+ import { oSearchResolver } from '../src/router/resolvers/o-node.search-resolver.js';
3
+ import { oAddress, RestrictedAddresses, NodeState, TransportType, } from '@olane/o-core';
4
+ import { oNodeTransport } from '../src/router/o-node.transport.js';
5
+ import { oNodeAddress } from '../src/router/o-node.address.js';
6
+ import { oProtocolMethods } from '@olane/o-protocol';
7
+ describe('oSearchResolver', () => {
8
+ let resolver;
9
+ let mockNode;
10
+ const testAddress = new oNodeAddress('o://test-node');
11
+ // Helper to create a proper oRouterRequest
12
+ const createRouterRequest = () => ({
13
+ method: oProtocolMethods.ROUTE,
14
+ params: {
15
+ _connectionId: 'test-connection',
16
+ _requestMethod: 'test',
17
+ address: 'o://test',
18
+ payload: {},
19
+ },
20
+ jsonrpc: '2.0',
21
+ id: '1',
22
+ state: {},
23
+ connectionId: 'test-connection',
24
+ toJSON: () => ({}),
25
+ setState: () => { },
26
+ logger: {},
27
+ });
28
+ // Helper to create a proper oResponse
29
+ const createResponse = (result) => ({
30
+ jsonrpc: '2.0',
31
+ id: '1',
32
+ result,
33
+ });
34
+ beforeEach(() => {
35
+ resolver = new oSearchResolver(testAddress);
36
+ // Create mock node with necessary properties
37
+ mockNode = {
38
+ address: testAddress,
39
+ state: NodeState.RUNNING,
40
+ leader: new oNodeAddress(RestrictedAddresses.LEADER, [
41
+ new oNodeTransport('/ip4/127.0.0.1/tcp/4001'),
42
+ ]),
43
+ hierarchyManager: {
44
+ getChild: (address) => null,
45
+ },
46
+ use: async (address, request) => {
47
+ // Mock registry search response
48
+ return createResponse({
49
+ data: [],
50
+ });
51
+ },
52
+ };
53
+ });
54
+ describe('Basic resolver configuration', () => {
55
+ it('should have custom transport for /search', () => {
56
+ const transports = resolver.customTransports;
57
+ expect(transports).to.have.lengthOf(1);
58
+ expect(transports[0].value).to.equal('/search');
59
+ });
60
+ it('should use registry as default search target', () => {
61
+ const registryAddress = resolver.getRegistryAddress();
62
+ expect(registryAddress.value).to.equal(RestrictedAddresses.REGISTRY);
63
+ });
64
+ it('should use "search" as default method', () => {
65
+ const method = resolver.getSearchMethod();
66
+ expect(method).to.equal('search');
67
+ });
68
+ });
69
+ describe('resolve() - Basic flow', () => {
70
+ it('should skip search if address already has transports', async () => {
71
+ const addressWithTransports = new oNodeAddress('o://test', [
72
+ new oNodeTransport('/ip4/127.0.0.1/tcp/5001'),
73
+ ]);
74
+ const result = await resolver.resolve({
75
+ address: addressWithTransports,
76
+ targetAddress: addressWithTransports,
77
+ node: mockNode,
78
+ request: createRouterRequest(),
79
+ });
80
+ expect(result.nextHopAddress.value).to.equal('o://test');
81
+ expect(result.nextHopAddress.transports).to.have.lengthOf(1);
82
+ });
83
+ it('should return original address when no search results found', async () => {
84
+ const address = new oNodeAddress('o://unknown-service');
85
+ mockNode.use = async () => createResponse({ data: [] });
86
+ const result = await resolver.resolve({
87
+ address,
88
+ targetAddress: address,
89
+ node: mockNode,
90
+ request: createRouterRequest(),
91
+ });
92
+ expect(result.nextHopAddress.value).to.equal('o://unknown-service');
93
+ });
94
+ it('should resolve address using registry search results', async () => {
95
+ const address = new oNodeAddress('o://embeddings-text');
96
+ mockNode.use = async () => createResponse({
97
+ data: [
98
+ {
99
+ address: 'o://leader/services/embeddings',
100
+ staticAddress: 'o://embeddings-text',
101
+ transports: [
102
+ {
103
+ value: '/ip4/127.0.0.1/tcp/6001',
104
+ type: TransportType.LIBP2P,
105
+ },
106
+ ],
107
+ },
108
+ ],
109
+ });
110
+ const result = await resolver.resolve({
111
+ address,
112
+ targetAddress: address,
113
+ node: mockNode,
114
+ request: createRouterRequest(),
115
+ });
116
+ expect(result.targetAddress.value).to.equal('o://leader/services/embeddings');
117
+ expect(result.targetAddress.transports).to.have.lengthOf(1);
118
+ });
119
+ it('should preserve extra path parameters in resolved address', async () => {
120
+ const address = new oNodeAddress('o://embeddings-text/method/params');
121
+ mockNode.use = async () => createResponse({
122
+ data: [
123
+ {
124
+ address: 'o://leader/services/embeddings',
125
+ staticAddress: 'o://embeddings-text',
126
+ transports: [
127
+ {
128
+ value: '/ip4/127.0.0.1/tcp/6001',
129
+ type: TransportType.LIBP2P,
130
+ },
131
+ ],
132
+ },
133
+ ],
134
+ });
135
+ const result = await resolver.resolve({
136
+ address,
137
+ targetAddress: address,
138
+ node: mockNode,
139
+ request: createRouterRequest(),
140
+ });
141
+ expect(result.targetAddress.value).to.equal('o://leader/services/embeddings/method/params');
142
+ });
143
+ it('should handle hierarchical address resolution', async () => {
144
+ const address = new oNodeAddress('o://leader/services/embeddings-text');
145
+ mockNode.use = async () => createResponse({
146
+ data: [
147
+ {
148
+ address: 'o://leader/services/embeddings-text',
149
+ transports: [
150
+ {
151
+ value: '/ip4/127.0.0.1/tcp/6001',
152
+ type: TransportType.LIBP2P,
153
+ },
154
+ ],
155
+ },
156
+ ],
157
+ });
158
+ const result = await resolver.resolve({
159
+ address,
160
+ targetAddress: address,
161
+ node: mockNode,
162
+ request: createRouterRequest(),
163
+ });
164
+ expect(result.targetAddress.transports).to.have.lengthOf(1);
165
+ });
166
+ });
167
+ describe('buildSearchParams()', () => {
168
+ it('should include both staticAddress and address', () => {
169
+ const address = new oNodeAddress('o://leader/services/embeddings-text');
170
+ const params = resolver.buildSearchParams(address);
171
+ expect(params.staticAddress).to.equal('o://leader');
172
+ expect(params.address).to.equal('o://leader/services/embeddings-text');
173
+ });
174
+ it('should use root address for staticAddress', () => {
175
+ const address = new oNodeAddress('o://leader/deep/nested/path');
176
+ const params = resolver.buildSearchParams(address);
177
+ expect(params.staticAddress).to.equal('o://leader');
178
+ expect(params.address).to.equal('o://leader/deep/nested/path');
179
+ });
180
+ it('should handle static addresses', () => {
181
+ const address = new oNodeAddress('o://embeddings-text');
182
+ const params = resolver.buildSearchParams(address);
183
+ expect(params.staticAddress).to.equal('o://embeddings-text');
184
+ expect(params.address).to.equal('o://embeddings-text');
185
+ });
186
+ });
187
+ describe('filterSearchResults()', () => {
188
+ it('should filter out registry address to prevent loops', () => {
189
+ const results = [
190
+ {
191
+ staticAddress: RestrictedAddresses.REGISTRY,
192
+ address: 'o://registry',
193
+ },
194
+ { staticAddress: 'o://service', address: 'o://service' },
195
+ ];
196
+ const filtered = resolver.filterSearchResults(results, mockNode);
197
+ expect(filtered).to.have.lengthOf(1);
198
+ expect(filtered[0].staticAddress).to.equal('o://service');
199
+ });
200
+ it('should filter out self address to prevent loops', () => {
201
+ const results = [
202
+ { address: 'o://test-node', staticAddress: 'o://test' },
203
+ { address: 'o://other-node', staticAddress: 'o://other' },
204
+ ];
205
+ const filtered = resolver.filterSearchResults(results, mockNode);
206
+ expect(filtered).to.have.lengthOf(1);
207
+ expect(filtered[0].address).to.equal('o://other-node');
208
+ });
209
+ it('should allow all valid results', () => {
210
+ const results = [
211
+ { address: 'o://service-1', staticAddress: 'o://s1' },
212
+ { address: 'o://service-2', staticAddress: 'o://s2' },
213
+ { address: 'o://service-3', staticAddress: 'o://s3' },
214
+ ];
215
+ const filtered = resolver.filterSearchResults(results, mockNode);
216
+ expect(filtered).to.have.lengthOf(3);
217
+ });
218
+ it('should handle empty results array', () => {
219
+ const filtered = resolver.filterSearchResults([], mockNode);
220
+ expect(filtered).to.be.an('array').that.is.empty;
221
+ });
222
+ });
223
+ describe('selectResult()', () => {
224
+ it('should select first result from multiple options', () => {
225
+ const results = [
226
+ { address: 'o://service-1', peerId: 'peer-1' },
227
+ { address: 'o://service-2', peerId: 'peer-2' },
228
+ { address: 'o://service-3', peerId: 'peer-3' },
229
+ ];
230
+ const selected = resolver.selectResult(results);
231
+ expect(selected).to.equal(results[0]);
232
+ expect(selected.peerId).to.equal('peer-1');
233
+ });
234
+ it('should return null for empty results', () => {
235
+ const selected = resolver.selectResult([]);
236
+ expect(selected).to.be.null;
237
+ });
238
+ it('should return single result', () => {
239
+ const results = [{ address: 'o://only-service', peerId: 'only-peer' }];
240
+ const selected = resolver.selectResult(results);
241
+ expect(selected).to.equal(results[0]);
242
+ });
243
+ });
244
+ describe('mapTransports()', () => {
245
+ it('should map transport objects to oNodeTransport instances', () => {
246
+ const result = {
247
+ transports: [
248
+ { value: '/ip4/127.0.0.1/tcp/4001', type: TransportType.LIBP2P },
249
+ { value: '/ip4/192.168.1.1/tcp/4002', type: TransportType.LIBP2P },
250
+ ],
251
+ };
252
+ const transports = resolver.mapTransports(result);
253
+ expect(transports).to.have.lengthOf(2);
254
+ expect(transports[0]).to.be.instanceOf(oNodeTransport);
255
+ expect(transports[0].value).to.equal('/ip4/127.0.0.1/tcp/4001');
256
+ expect(transports[1].value).to.equal('/ip4/192.168.1.1/tcp/4002');
257
+ });
258
+ it('should handle empty transports array', () => {
259
+ const result = { transports: [] };
260
+ const transports = resolver.mapTransports(result);
261
+ expect(transports).to.be.an('array').that.is.empty;
262
+ });
263
+ it('should handle single transport', () => {
264
+ const result = {
265
+ transports: [{ value: '/memory', type: TransportType.CUSTOM }],
266
+ };
267
+ const transports = resolver.mapTransports(result);
268
+ expect(transports).to.have.lengthOf(1);
269
+ expect(transports[0].value).to.equal('/memory');
270
+ });
271
+ });
272
+ describe('resolveNextHopTransports()', () => {
273
+ it('should use leader transports when next hop is leader', () => {
274
+ const nextHop = new oNodeAddress(RestrictedAddresses.LEADER);
275
+ const targetTransports = [
276
+ new oNodeTransport('/ip4/192.168.1.1/tcp/9999'),
277
+ ];
278
+ const resolved = resolver.resolveNextHopTransports(nextHop, targetTransports, mockNode);
279
+ expect(resolved).to.have.lengthOf(1);
280
+ expect(resolved[0].value).to.equal('/ip4/127.0.0.1/tcp/4001'); // Leader transport from mock
281
+ });
282
+ it('should use child transports when next hop is known child', () => {
283
+ const childAddress = new oNodeAddress('o://known-child', [
284
+ new oNodeTransport('/ip4/10.0.0.1/tcp/7001'),
285
+ ]);
286
+ mockNode.hierarchyManager = {
287
+ getChild: (address) => {
288
+ if (address.value === 'o://known-child') {
289
+ return childAddress;
290
+ }
291
+ return null;
292
+ },
293
+ };
294
+ const nextHop = new oNodeAddress('o://known-child');
295
+ const targetTransports = [
296
+ new oNodeTransport('/ip4/192.168.1.1/tcp/9999'),
297
+ ];
298
+ const resolved = resolver.resolveNextHopTransports(nextHop, targetTransports, mockNode);
299
+ expect(resolved).to.have.lengthOf(1);
300
+ expect(resolved[0].value).to.equal('/ip4/10.0.0.1/tcp/7001');
301
+ });
302
+ it('should use target transports for unknown next hop', () => {
303
+ const nextHop = new oNodeAddress('o://unknown-node');
304
+ const targetTransports = [
305
+ new oNodeTransport('/ip4/192.168.1.1/tcp/8001'),
306
+ ];
307
+ const resolved = resolver.resolveNextHopTransports(nextHop, targetTransports, mockNode);
308
+ expect(resolved).to.have.lengthOf(1);
309
+ expect(resolved[0].value).to.equal('/ip4/192.168.1.1/tcp/8001');
310
+ });
311
+ it('should return empty array when leader has no transports', () => {
312
+ // Create a new mock node with leader that has no transports
313
+ const mockNodeNoLeaderTransports = {
314
+ ...mockNode,
315
+ leader: new oNodeAddress(RestrictedAddresses.LEADER, []),
316
+ };
317
+ const nextHop = new oNodeAddress(RestrictedAddresses.LEADER);
318
+ const targetTransports = [
319
+ new oNodeTransport('/ip4/192.168.1.1/tcp/9999'),
320
+ ];
321
+ const resolved = resolver.resolveNextHopTransports(nextHop, targetTransports, mockNodeNoLeaderTransports);
322
+ expect(resolved).to.be.an('array').that.is.empty;
323
+ });
324
+ });
325
+ describe('determineNextHop()', () => {
326
+ it('should use oAddress.next() for hierarchy routing', async () => {
327
+ const resolvedTarget = new oNodeAddress('o://leader/services/embeddings');
328
+ const searchResult = {
329
+ transports: [
330
+ { value: '/ip4/127.0.0.1/tcp/6001', type: TransportType.LIBP2P },
331
+ ],
332
+ };
333
+ mockNode.address = new oNodeAddress(RestrictedAddresses.LEADER);
334
+ const nextHop = resolver.determineNextHop(mockNode, resolvedTarget, searchResult);
335
+ // From leader to o://leader/services/embeddings, next hop should be o://leader/services
336
+ expect(nextHop.value).to.equal('o://leader/services');
337
+ expect(nextHop.transports).to.not.be.empty;
338
+ });
339
+ it('should set transports on next hop address', async () => {
340
+ const resolvedTarget = new oNodeAddress('o://leader/services');
341
+ const searchResult = {
342
+ transports: [
343
+ { value: '/ip4/127.0.0.1/tcp/6001', type: TransportType.LIBP2P },
344
+ ],
345
+ };
346
+ mockNode.address = new oNodeAddress(RestrictedAddresses.LEADER);
347
+ const nextHop = resolver.determineNextHop(mockNode, resolvedTarget, searchResult);
348
+ expect(nextHop.transports).to.not.be.undefined;
349
+ expect(nextHop.transports).to.not.be.empty;
350
+ });
351
+ it('should handle routing to immediate child', async () => {
352
+ const resolvedTarget = new oNodeAddress('o://leader/services');
353
+ const searchResult = {
354
+ transports: [
355
+ { value: '/ip4/127.0.0.1/tcp/6001', type: TransportType.LIBP2P },
356
+ ],
357
+ };
358
+ mockNode.address = new oNodeAddress(RestrictedAddresses.LEADER);
359
+ const nextHop = resolver.determineNextHop(mockNode, resolvedTarget, searchResult);
360
+ expect(nextHop.value).to.equal('o://leader/services');
361
+ });
362
+ });
363
+ describe('Custom resolver subclassing', () => {
364
+ it('should allow overriding getRegistryAddress()', () => {
365
+ class CustomRegistryResolver extends oSearchResolver {
366
+ getRegistryAddress() {
367
+ return new oAddress('o://custom-registry');
368
+ }
369
+ }
370
+ const customResolver = new CustomRegistryResolver(testAddress);
371
+ const registryAddress = customResolver.getRegistryAddress();
372
+ expect(registryAddress.value).to.equal('o://custom-registry');
373
+ });
374
+ it('should allow overriding getSearchMethod()', () => {
375
+ class CustomMethodResolver extends oSearchResolver {
376
+ getSearchMethod() {
377
+ return 'custom_search';
378
+ }
379
+ }
380
+ const customResolver = new CustomMethodResolver(testAddress);
381
+ const method = customResolver.getSearchMethod();
382
+ expect(method).to.equal('custom_search');
383
+ });
384
+ it('should allow custom result selection (e.g., round-robin)', () => {
385
+ class RoundRobinResolver extends oSearchResolver {
386
+ constructor() {
387
+ super(...arguments);
388
+ this.currentIndex = 0;
389
+ }
390
+ selectResult(results) {
391
+ if (results.length === 0)
392
+ return null;
393
+ const result = results[this.currentIndex % results.length];
394
+ this.currentIndex++;
395
+ return result;
396
+ }
397
+ }
398
+ const customResolver = new RoundRobinResolver(testAddress);
399
+ const results = [
400
+ { address: 'o://service-1' },
401
+ { address: 'o://service-2' },
402
+ { address: 'o://service-3' },
403
+ ];
404
+ // First call
405
+ let selected = customResolver.selectResult(results);
406
+ expect(selected.address).to.equal('o://service-1');
407
+ // Second call
408
+ selected = customResolver.selectResult(results);
409
+ expect(selected.address).to.equal('o://service-2');
410
+ // Third call
411
+ selected = customResolver.selectResult(results);
412
+ expect(selected.address).to.equal('o://service-3');
413
+ // Fourth call - wraps around
414
+ selected = customResolver.selectResult(results);
415
+ expect(selected.address).to.equal('o://service-1');
416
+ });
417
+ it('should allow custom filtering logic', () => {
418
+ class StatusFilterResolver extends oSearchResolver {
419
+ filterSearchResults(results, node) {
420
+ // First apply parent filtering
421
+ const filtered = super.filterSearchResults(results, node);
422
+ // Then apply custom filtering
423
+ return filtered.filter((result) => result.status === 'active');
424
+ }
425
+ }
426
+ const customResolver = new StatusFilterResolver(testAddress);
427
+ const results = [
428
+ { address: 'o://service-1', status: 'active', staticAddress: 'o://s1' },
429
+ {
430
+ address: 'o://service-2',
431
+ status: 'inactive',
432
+ staticAddress: 'o://s2',
433
+ },
434
+ { address: 'o://service-3', status: 'active', staticAddress: 'o://s3' },
435
+ ];
436
+ const filtered = customResolver.filterSearchResults(results, mockNode);
437
+ expect(filtered).to.have.lengthOf(2);
438
+ expect(filtered.every((r) => r.status === 'active')).to.be.true;
439
+ });
440
+ it('should allow direct routing by overriding determineNextHop()', () => {
441
+ class DirectRoutingResolver extends oSearchResolver {
442
+ determineNextHop(node, resolvedTargetAddress, searchResult) {
443
+ // Always route directly to target, bypassing hierarchy
444
+ const targetTransports = this.mapTransports(searchResult);
445
+ resolvedTargetAddress.setTransports(targetTransports);
446
+ return resolvedTargetAddress;
447
+ }
448
+ }
449
+ const customResolver = new DirectRoutingResolver(testAddress);
450
+ const resolvedTarget = new oNodeAddress('o://leader/services/embeddings');
451
+ const searchResult = {
452
+ transports: [
453
+ { value: '/ip4/127.0.0.1/tcp/6001', type: TransportType.LIBP2P },
454
+ ],
455
+ };
456
+ mockNode.address = new oNodeAddress(RestrictedAddresses.LEADER);
457
+ const nextHop = customResolver.determineNextHop(mockNode, resolvedTarget, searchResult);
458
+ // Direct routing: next hop is the target itself
459
+ expect(nextHop.value).to.equal('o://leader/services/embeddings');
460
+ expect(nextHop.transports).to.have.lengthOf(1);
461
+ });
462
+ });
463
+ describe('Integration with registry calls', () => {
464
+ it('should call node.use() with correct registry address', async () => {
465
+ const address = new oNodeAddress('o://test-service');
466
+ let capturedAddress;
467
+ let capturedRequest;
468
+ mockNode.use = async (addr, req) => {
469
+ capturedAddress = addr;
470
+ capturedRequest = req;
471
+ return createResponse({ data: [] });
472
+ };
473
+ await resolver.resolve({
474
+ address,
475
+ targetAddress: address,
476
+ node: mockNode,
477
+ request: createRouterRequest(),
478
+ });
479
+ expect(capturedAddress?.value).to.equal(RestrictedAddresses.REGISTRY);
480
+ expect(capturedRequest.method).to.equal('search');
481
+ });
482
+ it('should pass correct search parameters to registry', async () => {
483
+ const address = new oNodeAddress('o://leader/services/embeddings');
484
+ let capturedParams;
485
+ mockNode.use = async (addr, req) => {
486
+ capturedParams = req.params;
487
+ return createResponse({ data: [] });
488
+ };
489
+ await resolver.resolve({
490
+ address,
491
+ targetAddress: address,
492
+ node: mockNode,
493
+ request: createRouterRequest(),
494
+ });
495
+ expect(capturedParams.staticAddress).to.equal('o://leader');
496
+ expect(capturedParams.address).to.equal('o://leader/services/embeddings');
497
+ });
498
+ it('should handle registry errors gracefully', async () => {
499
+ const address = new oNodeAddress('o://test-service');
500
+ mockNode.use = async () => {
501
+ throw new Error('Registry unavailable');
502
+ };
503
+ // Should throw error since registry is unavailable
504
+ try {
505
+ await resolver.resolve({
506
+ address,
507
+ targetAddress: address,
508
+ node: mockNode,
509
+ request: createRouterRequest(),
510
+ });
511
+ expect.fail('Should have thrown error');
512
+ }
513
+ catch (error) {
514
+ expect(error.message).to.equal('Registry unavailable');
515
+ }
516
+ });
517
+ });
518
+ describe('Edge cases', () => {
519
+ it('should handle address with long nested paths', async () => {
520
+ const address = new oNodeAddress('o://leader/a/b/c/d/e/f');
521
+ mockNode.use = async () => createResponse({
522
+ data: [
523
+ {
524
+ address: 'o://leader/a/b/c/d/e/f',
525
+ transports: [{ value: '/memory', type: TransportType.CUSTOM }],
526
+ },
527
+ ],
528
+ });
529
+ const result = await resolver.resolve({
530
+ address,
531
+ targetAddress: address,
532
+ node: mockNode,
533
+ request: createRouterRequest(),
534
+ });
535
+ expect(result.targetAddress.value).to.equal('o://leader/a/b/c/d/e/f');
536
+ });
537
+ it('should handle static addresses correctly', async () => {
538
+ const address = new oNodeAddress('o://embeddings-text');
539
+ mockNode.use = async () => createResponse({
540
+ data: [
541
+ {
542
+ address: 'o://leader/services/embeddings',
543
+ staticAddress: 'o://embeddings-text',
544
+ transports: [
545
+ {
546
+ value: '/ip4/127.0.0.1/tcp/6001',
547
+ type: TransportType.LIBP2P,
548
+ },
549
+ ],
550
+ },
551
+ ],
552
+ });
553
+ const result = await resolver.resolve({
554
+ address,
555
+ targetAddress: address,
556
+ node: mockNode,
557
+ request: createRouterRequest(),
558
+ });
559
+ expect(result.targetAddress.value).to.equal('o://leader/services/embeddings');
560
+ });
561
+ it('should handle when leader is undefined', async () => {
562
+ const address = new oNodeAddress('o://test');
563
+ // Create a new mock node with undefined leader
564
+ const mockNodeNoLeader = {
565
+ ...mockNode,
566
+ leader: undefined,
567
+ };
568
+ mockNodeNoLeader.use = async () => createResponse({
569
+ data: [
570
+ {
571
+ address: 'o://test',
572
+ transports: [{ value: '/memory', type: TransportType.CUSTOM }],
573
+ },
574
+ ],
575
+ });
576
+ const result = await resolver.resolve({
577
+ address,
578
+ targetAddress: address,
579
+ node: mockNodeNoLeader,
580
+ request: createRouterRequest(),
581
+ });
582
+ // Should still work, just won't have leader transports
583
+ expect(result.targetAddress.value).to.equal('o://test');
584
+ });
585
+ it('should handle multiple transports in search result', async () => {
586
+ const address = new oNodeAddress('o://multi-transport');
587
+ mockNode.use = async () => createResponse({
588
+ data: [
589
+ {
590
+ address: 'o://multi-transport',
591
+ transports: [
592
+ {
593
+ value: '/ip4/127.0.0.1/tcp/4001',
594
+ type: TransportType.LIBP2P,
595
+ },
596
+ {
597
+ value: '/ip4/192.168.1.1/tcp/4002',
598
+ type: TransportType.LIBP2P,
599
+ },
600
+ { value: '/memory', type: TransportType.CUSTOM },
601
+ ],
602
+ },
603
+ ],
604
+ });
605
+ const result = await resolver.resolve({
606
+ address,
607
+ targetAddress: address,
608
+ node: mockNode,
609
+ request: createRouterRequest(),
610
+ });
611
+ expect(result.targetAddress.transports).to.have.lengthOf(3);
612
+ });
613
+ });
614
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olane/o-node",
3
- "version": "0.7.11",
3
+ "version": "0.7.12-alpha.3",
4
4
  "type": "module",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -24,8 +24,6 @@
24
24
  "deep:clean": "rm -rf node_modules && rm package-lock.json",
25
25
  "start:prod": "node dist/index.js",
26
26
  "prepublishOnly": "npm run build",
27
- "update:lib": "npm install @olane/o-core@latest",
28
- "update:peers": "npm install @olane/o-core@latest @olane/o-config@latest @olane/o-protocol@latest @olane/o-tool@latest --save-peer",
29
27
  "lint": "eslint src/**/*.ts"
30
28
  },
31
29
  "repository": {
@@ -55,14 +53,13 @@
55
53
  "tsx": "^4.20.3",
56
54
  "typescript": "5.4.5"
57
55
  },
58
- "peerDependencies": {
59
- "@olane/o-config": "^0.7.10",
60
- "@olane/o-core": "^0.7.10",
61
- "@olane/o-protocol": "^0.7.10",
62
- "@olane/o-tool": "^0.7.10"
63
- },
64
56
  "dependencies": {
57
+ "@olane/o-config": "^0.7.12-alpha.3",
58
+ "@olane/o-core": "0.7.12-alpha.3",
59
+ "@olane/o-protocol": "^0.7.12-alpha.3",
60
+ "@olane/o-tool": "0.7.12-alpha.3",
65
61
  "debug": "^4.4.1",
66
62
  "dotenv": "^16.5.0"
67
- }
63
+ },
64
+ "gitHead": "5c05e5f248001abc38f34c77bd97c9a325231503"
68
65
  }