Skip to content

Commit b0e8f06

Browse files
wemeetagainmaschad
authored andcommitted
refactor!: move autonat into separate package (#2107)
Co-authored-by: chad <chad.nehemiah94@gmail.com>
1 parent 125c84b commit b0e8f06

30 files changed

+371
-246
lines changed

.release-please.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"packages/peer-id-factory": {},
2323
"packages/peer-record": {},
2424
"packages/peer-store": {},
25+
"packages/protocol-autonat": {},
2526
"packages/protocol-perf": {},
2627
"packages/pubsub": {},
2728
"packages/pubsub-floodsub": {},

doc/CONFIGURATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ For more information see https://docs.libp2p.io/concepts/nat/autonat/#what-is-au
930930

931931
```ts
932932
import { createLibp2p } from 'libp2p'
933-
import { autoNATService } from 'libp2p/autonat'
933+
import { autoNATService } from '@libp2p/autonat'
934934

935935
const node = await createLibp2p({
936936
services: {

doc/migrations/v0.46-v1.0.0.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1+
<!--Specify versions for migration below-->
12
# Migrating to libp2p@1.0.0 <!-- omit in toc -->
23

34
A migration guide for refactoring your application code from libp2p `v0.46` to `v1.0.0`.
45

56
## Table of Contents <!-- omit in toc -->
67

7-
- [New features](#new-features)
8-
- [Breaking changes](#breaking-changes)
8+
- [AutoNAT](#autonat)
99
- [KeyChain](#keychain)
1010
- [Metrics](#metrics)
1111

12-
## New features
12+
## AutoNAT
1313

14-
...
14+
The AutoNAT service is now published in its own package.
1515

16-
## Breaking changes
16+
**Before**
1717

1818
```ts
19+
import { createLibp2p } from 'libp2p'
1920
import { autoNATService } from 'libp2p/autonat'
21+
22+
const node = await createLibp2p({
23+
services: {
24+
autoNAT: autoNATService()
25+
}
26+
})
2027
```
2128

2229
**After**
2330

2431
```ts
25-
import { autoNATService } from '@libp2p/autonat'
32+
import { createLibp2p } from 'libp2p'
33+
import { autoNAT } from '@libp2p/autonat'
34+
35+
const node = await createLibp2p({
36+
services: {
37+
autoNAT: autoNAT()
38+
}
39+
})
2640
```
2741

2842
## KeyChain

doc/migrations/v0.46-v1.0.md

Lines changed: 0 additions & 85 deletions
This file was deleted.

packages/interface/src/errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ export class InvalidCryptoTransmissionError extends Error {
6565

6666
static readonly code = 'ERR_INVALID_CRYPTO_TRANSMISSION'
6767
}
68+
69+
// Error codes
70+
71+
export const ERR_TIMEOUT = 'ERR_TIMEOUT'

packages/libp2p/package.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@
4848
"types": "./dist/src/index.d.ts",
4949
"import": "./dist/src/index.js"
5050
},
51-
"./autonat": {
52-
"types": "./dist/src/autonat/index.d.ts",
53-
"import": "./dist/src/autonat/index.js"
54-
},
5551
"./circuit-relay": {
5652
"types": "./dist/src/circuit-relay/index.d.ts",
5753
"import": "./dist/src/circuit-relay/index.js"
@@ -104,7 +100,6 @@
104100
"prepublishOnly": "node scripts/update-version.js && npm run build",
105101
"build": "aegir build",
106102
"generate": "run-s generate:proto:*",
107-
"generate:proto:autonat": "protons ./src/autonat/pb/index.proto",
108103
"generate:proto:circuit-relay": "protons ./src/circuit-relay/pb/index.proto",
109104
"generate:proto:dcutr": "protons ./src/dcutr/pb/message.proto",
110105
"generate:proto:fetch": "protons ./src/fetch/pb/proto.proto",
@@ -148,7 +143,6 @@
148143
"it-map": "^3.0.3",
149144
"it-merge": "^3.0.0",
150145
"it-pair": "^2.0.6",
151-
"it-parallel": "^3.0.0",
152146
"it-pipe": "^3.0.1",
153147
"it-protobuf-stream": "^1.0.0",
154148
"it-stream-types": "^2.0.1",

packages/libp2p/src/connection-manager/dial-queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AbortError, CodeError } from '@libp2p/interface/errors'
1+
import { AbortError, CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
22
import { setMaxListeners } from '@libp2p/interface/events'
33
import { PeerMap } from '@libp2p/peer-collections'
44
import { defaultAddressSort } from '@libp2p/utils/address-sort'
@@ -269,7 +269,7 @@ export class DialQueue {
269269

270270
// Error is a timeout
271271
if (signal.aborted) {
272-
const error = new CodeError(err.message, codes.ERR_TIMEOUT)
272+
const error = new CodeError(err.message, ERR_TIMEOUT)
273273
throw error
274274
}
275275

packages/libp2p/src/content-routing/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CodeError } from '@libp2p/interface/errors'
22
import merge from 'it-merge'
33
import { pipe } from 'it-pipe'
4-
import { messages, codes } from '../errors.js'
4+
import { codes, messages } from '../errors.js'
55
import {
66
storeAddresses,
77
uniquePeers,

packages/libp2p/src/errors.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export enum codes {
3737
ERR_INVALID_PEER = 'ERR_INVALID_PEER',
3838
ERR_MUXER_UNAVAILABLE = 'ERR_MUXER_UNAVAILABLE',
3939
ERR_NOT_FOUND = 'ERR_NOT_FOUND',
40-
ERR_TIMEOUT = 'ERR_TIMEOUT',
4140
ERR_TRANSPORT_UNAVAILABLE = 'ERR_TRANSPORT_UNAVAILABLE',
4241
ERR_TRANSPORT_DIAL_FAILED = 'ERR_TRANSPORT_DIAL_FAILED',
4342
ERR_UNSUPPORTED_PROTOCOL = 'ERR_UNSUPPORTED_PROTOCOL',

packages/libp2p/src/fetch/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CodeError } from '@libp2p/interface/errors'
1+
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
22
import { setMaxListeners } from '@libp2p/interface/events'
33
import { logger } from '@libp2p/logger'
44
import first from 'it-first'
@@ -155,7 +155,7 @@ class DefaultFetchService implements Startable, FetchService {
155155
})
156156

157157
onAbort = () => {
158-
stream?.abort(new CodeError('fetch timeout', codes.ERR_TIMEOUT))
158+
stream?.abort(new CodeError('fetch timeout', ERR_TIMEOUT))
159159
}
160160

161161
// make stream abortable

packages/libp2p/src/ping/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { randomBytes } from '@libp2p/crypto'
2-
import { CodeError } from '@libp2p/interface/errors'
2+
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
33
import { logger } from '@libp2p/logger'
44
import first from 'it-first'
55
import { pipe } from 'it-pipe'
@@ -125,7 +125,7 @@ class DefaultPingService implements Startable, PingService {
125125
})
126126

127127
onAbort = () => {
128-
stream?.abort(new CodeError('ping timeout', codes.ERR_TIMEOUT))
128+
stream?.abort(new CodeError('ping timeout', ERR_TIMEOUT))
129129
}
130130

131131
// make stream abortable

packages/libp2p/src/upgrader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CodeError } from '@libp2p/interface/errors'
1+
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
22
import { setMaxListeners } from '@libp2p/interface/events'
33
import * as mss from '@libp2p/multistream-select'
44
import { peerIdFromString } from '@libp2p/peer-id'
@@ -167,7 +167,7 @@ export class DefaultUpgrader implements Upgrader {
167167
const signal = AbortSignal.timeout(this.inboundUpgradeTimeout)
168168

169169
const onAbort = (): void => {
170-
maConn.abort(new CodeError('inbound upgrade timeout', codes.ERR_TIMEOUT))
170+
maConn.abort(new CodeError('inbound upgrade timeout', ERR_TIMEOUT))
171171
}
172172

173173
signal.addEventListener('abort', onAbort, { once: true })

packages/libp2p/test/connection-manager/direct.node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import os from 'node:os'
55
import path from 'node:path'
66
import { yamux } from '@chainsafe/libp2p-yamux'
77
import { type Connection, type ConnectionProtector, isConnection } from '@libp2p/interface/connection'
8-
import { AbortError } from '@libp2p/interface/errors'
8+
import { AbortError, ERR_TIMEOUT } from '@libp2p/interface/errors'
99
import { TypedEventEmitter } from '@libp2p/interface/events'
1010
import { start, stop } from '@libp2p/interface/startable'
1111
import { mockConnection, mockConnectionGater, mockDuplex, mockMultiaddrConnection, mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
@@ -218,7 +218,7 @@ describe('dialing (direct, TCP)', () => {
218218

219219
await expect(dialer.dial(remoteAddr))
220220
.to.eventually.be.rejectedWith(Error)
221-
.and.to.have.property('code', ErrorCodes.ERR_TIMEOUT)
221+
.and.to.have.property('code', ERR_TIMEOUT)
222222
})
223223

224224
it('should dial to the max concurrency', async () => {

packages/libp2p/test/connection-manager/direct.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22

33
import { yamux } from '@chainsafe/libp2p-yamux'
4-
import { AbortError } from '@libp2p/interface/errors'
4+
import { AbortError, ERR_TIMEOUT } from '@libp2p/interface/errors'
55
import { TypedEventEmitter } from '@libp2p/interface/events'
66
import { mockConnectionGater, mockDuplex, mockMultiaddrConnection, mockUpgrader, mockConnection } from '@libp2p/interface-compliance-tests/mocks'
77
import { mplex } from '@libp2p/mplex'
@@ -167,7 +167,7 @@ describe('dialing (direct, WebSockets)', () => {
167167

168168
await expect(connectionManager.openConnection(remoteAddr))
169169
.to.eventually.be.rejected()
170-
.and.to.have.property('code', ErrorCodes.ERR_TIMEOUT)
170+
.and.to.have.property('code', ERR_TIMEOUT)
171171
})
172172

173173
it('should throw when a peer advertises more than the allowed number of addresses', async () => {

packages/libp2p/test/fetch/index.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-env mocha */
22

3+
import { ERR_TIMEOUT } from '@libp2p/interface/errors'
34
import { TypedEventEmitter } from '@libp2p/interface/events'
45
import { start, stop } from '@libp2p/interface/startable'
56
import { mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks'
@@ -135,7 +136,7 @@ describe('fetch', () => {
135136
await expect(localFetch.fetch(remoteComponents.peerId, key, {
136137
signal
137138
}))
138-
.to.eventually.be.rejected.with.property('code', 'ERR_TIMEOUT')
139+
.to.eventually.be.rejected.with.property('code', ERR_TIMEOUT)
139140

140141
// should have closed stream
141142
expect(newStreamSpy).to.have.property('callCount', 1)

packages/libp2p/test/ping/index.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-env mocha */
22

3+
import { ERR_TIMEOUT } from '@libp2p/interface/errors'
34
import { TypedEventEmitter } from '@libp2p/interface/events'
45
import { start, stop } from '@libp2p/interface/startable'
56
import { mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks'
@@ -125,7 +126,7 @@ describe('ping', () => {
125126
await expect(localPing.ping(remoteComponents.peerId, {
126127
signal
127128
}))
128-
.to.eventually.be.rejected.with.property('code', 'ERR_TIMEOUT')
129+
.to.eventually.be.rejected.with.property('code', ERR_TIMEOUT)
129130

130131
// should have closed stream
131132
expect(newStreamSpy).to.have.property('callCount', 1)

packages/libp2p/typedoc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"entryPoints": [
33
"./src/index.ts",
4-
"./src/autonat/index.ts",
54
"./src/circuit-relay/index.ts",
65
"./src/fetch/index.ts",
76
"./src/identify/index.ts",

packages/protocol-autonat/LICENSE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

packages/protocol-autonat/LICENSE-MIT

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

0 commit comments

Comments
 (0)