| … | … | @@ -26,6 +26,11 @@ const TIMEOUT = 15000 |
| 26 | 26 | let passed = 0 |
| 27 | 27 | let failed = 0 |
| 28 | 28 | |
| 29 | // Catch uncaught errors from streams — prevent crash |
| 30 | process.on('uncaughtException', (err) => { |
| 31 | console.log(` [uncaught] ${err.code || err.message}`) |
| 32 | }) |
| 33 | |
| 29 | 34 | function ok (name) { passed++; console.log(` ✓ ${name}`) } |
| 30 | 35 | function fail (name, reason) { failed++; console.log(` ✗ ${name}: ${reason}`) } |
| 31 | 36 | |
| … | … | @@ -100,21 +105,31 @@ try { |
| 100 | 105 | const topic = crypto.hash(Buffer.concat([keyBuf, Buffer.from(REPO, 'utf-8')])).subarray(0, 32) |
| 101 | 106 | |
| 102 | 107 | const swarm3 = new Hyperswarm() |
| 108 | swarm3.on('error', () => {}) // prevent uncaught |
| 109 | |
| 103 | 110 | swarm3.join(topic, { server: false, client: true }) |
| 104 | 111 | |
| 105 | | const conn = await withTimeout( |
| 112 | const result = await withTimeout( |
| 106 | 113 | new Promise((resolve, reject) => { |
| 107 | | swarm3.once('connection', (conn, info) => { |
| 108 | | resolve({ conn, key: info.publicKey.toString('hex').slice(0, 8) }) |
| 114 | swarm3.on('connection', (conn, info) => { |
| 115 | const peerKey = info.publicKey.toString('hex') |
| 116 | conn.on('error', () => {}) // prevent uncaught ECONNRESET |
| 117 | resolve({ conn, peerKey }) |
| 109 | 118 | }) |
| 110 | | swarm3.once('error', reject) |
| 111 | 119 | }), |
| 112 | 120 | TIMEOUT, |
| 113 | 121 | 'remote connection' |
| 114 | 122 | ) |
| 115 | 123 | |
| 116 | | ok(`Connected to peer ${conn.key}...`) |
| 117 | | conn.conn.destroy() |
| 124 | const shortKey = result.peerKey.slice(0, 12) |
| 125 | const isSelf = result.peerKey === swarm3.keyPair.publicKey.toString('hex') |
| 126 | if (isSelf) { |
| 127 | fail('Remote connection', `connected to SELF (${shortKey}...) — local pear-git serving same topic?`) |
| 128 | } else { |
| 129 | ok(`Connected to peer ${shortKey}... (not self)`) |
| 130 | } |
| 131 | result.conn.on('error', () => {}) |
| 132 | result.conn.destroy() |
| 118 | 133 | await swarm3.destroy() |
| 119 | 134 | } catch (err) { |
| 120 | 135 | fail('Remote connection', err.message) |