|
3 | 3 |
|
4 | 4 | import { noise } from '@chainsafe/libp2p-noise'
|
5 | 5 | import { yamux } from '@chainsafe/libp2p-yamux'
|
6 |
| -import { circuitRelayTransport } from '@libp2p/circuit-relay-v2' |
7 | 6 | import { identify } from '@libp2p/identify'
|
8 |
| -import { tcp } from '@libp2p/tcp' |
9 |
| -import { webRTC } from '@libp2p/webrtc' |
10 | 7 | import { expect } from 'aegir/chai'
|
11 | 8 | import { createLibp2p } from 'libp2p'
|
12 | 9 | import { createSandbox } from 'sinon'
|
13 | 10 | import { HttpServerFactory } from '../src/http-server-factory.js'
|
14 | 11 | import { Router } from '../src/router.js'
|
| 12 | +import { getNodeConfig } from './common/node-config.js' |
15 | 13 | import type { http } from '../src/http-proto-api.js'
|
16 | 14 | import type { HttpServerInterface } from '../src/interfaces/http-server-interface.js'
|
17 | 15 | import type { RequestHandler } from '../src/interfaces/request-handler-interface.js'
|
18 | 16 | import type { Libp2p, ServiceMap } from '@libp2p/interface'
|
19 | 17 |
|
20 |
| -// Configure node based on environment |
21 |
| -interface NodeConfig { |
22 |
| - addresses: { |
23 |
| - listen: string[] |
24 |
| - } |
25 |
| - transports: any[] |
26 |
| - connectionGater?: { |
27 |
| - denyDialMultiaddr(): Promise<boolean> |
28 |
| - } |
29 |
| -} |
30 |
| - |
31 |
| -const getNodeConfig = (): NodeConfig => { |
32 |
| - if (typeof window === 'undefined') { |
33 |
| - // Node.js configuration |
34 |
| - return { |
35 |
| - addresses: { |
36 |
| - listen: ['/ip4/127.0.0.1/tcp/0'] |
37 |
| - }, |
38 |
| - transports: [tcp()] |
39 |
| - } |
40 |
| - } |
41 |
| - |
42 |
| - // Browser configuration |
43 |
| - return { |
44 |
| - addresses: { |
45 |
| - listen: ['/webrtc'] |
46 |
| - }, |
47 |
| - transports: [ |
48 |
| - webRTC({ |
49 |
| - rtcConfiguration: { |
50 |
| - iceServers: [{ urls: ['stun:stun.l.google.com:19302'] }] |
51 |
| - } |
52 |
| - }), |
53 |
| - circuitRelayTransport() |
54 |
| - ], |
55 |
| - connectionGater: { |
56 |
| - denyDialMultiaddr: async () => false |
57 |
| - } |
58 |
| - } |
59 |
| -} |
60 |
| - |
61 | 18 | describe('006-HTTP Server Implementation', () => {
|
62 | 19 | const sandbox = createSandbox()
|
63 | 20 | let node: Libp2p<ServiceMap>
|
64 | 21 | let server: HttpServerInterface
|
65 | 22 |
|
66 | 23 | beforeEach(async () => {
|
67 |
| - // Create a libp2p node with appropriate configuration |
68 |
| - const config = getNodeConfig() |
69 |
| - node = await createLibp2p({ |
70 |
| - ...config, |
71 |
| - streamMuxers: [yamux()], |
72 |
| - connectionEncrypters: [noise()], |
73 |
| - services: { |
74 |
| - identify: identify() |
75 |
| - } |
76 |
| - }) |
77 |
| - |
78 |
| - // Create an HTTP server using real components from the node |
79 |
| - const createServer = HttpServerFactory.createServer() |
80 |
| - server = createServer({ |
81 |
| - logger: node.logger, |
82 |
| - connectionManager: (node as any).components.connectionManager, |
83 |
| - registrar: (node as any).components.registrar |
84 |
| - }) |
85 |
| - |
86 |
| - // Start the node |
87 |
| - await node.start() |
| 24 | + try { |
| 25 | + // Use the existing node config utility which properly handles different environments |
| 26 | + const nodeConfig = getNodeConfig() |
| 27 | + |
| 28 | + node = await createLibp2p({ |
| 29 | + ...nodeConfig, |
| 30 | + streamMuxers: [yamux()], |
| 31 | + connectionEncrypters: [noise()], |
| 32 | + services: { |
| 33 | + identify: identify() |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + // Create an HTTP server using real components from the node |
| 38 | + const createServer = HttpServerFactory.createServer() |
| 39 | + server = createServer({ |
| 40 | + logger: node.logger, |
| 41 | + connectionManager: (node as any).components.connectionManager, |
| 42 | + registrar: (node as any).components.registrar |
| 43 | + }) |
| 44 | + |
| 45 | + // Start the node |
| 46 | + await node.start() |
| 47 | + } catch (err) { |
| 48 | + console.error('Error in beforeEach:', err) |
| 49 | + throw err |
| 50 | + } |
88 | 51 | })
|
89 | 52 |
|
90 | 53 | afterEach(async () => {
|
91 | 54 | sandbox.restore()
|
92 |
| - await node.stop() |
| 55 | + if (node != null) { |
| 56 | + await node.stop() |
| 57 | + } |
93 | 58 | })
|
94 | 59 |
|
95 | 60 | it('should create server instance', () => {
|
|
0 commit comments