Skip to content

Commit bd2936f

Browse files
wemeetagainmaschad
authored andcommitted
refactor!: move autonat into separate package (#2107)
Co-authored-by: chad <[email protected]>
1 parent ae9843a commit bd2936f

28 files changed

+237
-136
lines changed

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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1+
<!--Specify versions for migration below-->
12
# Migrating to [email protected] <!-- 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
1919
import { autoNATService } from 'libp2p/autonat'

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",
@@ -149,7 +144,6 @@
149144
"it-map": "^3.0.3",
150145
"it-merge": "^3.0.0",
151146
"it-pair": "^2.0.6",
152-
"it-parallel": "^3.0.0",
153147
"it-pipe": "^3.0.1",
154148
"it-protobuf-stream": "^1.0.0",
155149
"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,5 +1,5 @@
11
import { setMaxListeners } from 'events'
2-
import { AbortError, CodeError } from '@libp2p/interface/errors'
2+
import { AbortError, CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
33
import { logger } from '@libp2p/logger'
44
import { defaultAddressSort } from '@libp2p/utils/address-sort'
55
import { type Multiaddr, type Resolver, resolvers } from '@multiformats/multiaddr'
@@ -250,7 +250,7 @@ export class DialQueue {
250250

251251
// Error is a timeout
252252
if (signal.aborted) {
253-
const error = new CodeError(err.message, codes.ERR_TIMEOUT)
253+
const error = new CodeError(err.message, ERR_TIMEOUT)
254254
throw error
255255
}
256256

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,5 +1,5 @@
11
import { setMaxListeners } from 'events'
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 * as lp from 'it-length-prefixed'
@@ -158,7 +158,7 @@ class DefaultFetchService implements Startable, FetchService {
158158
})
159159

160160
onAbort = () => {
161-
stream?.abort(new CodeError('fetch timeout', codes.ERR_TIMEOUT))
161+
stream?.abort(new CodeError('fetch timeout', ERR_TIMEOUT))
162162
}
163163

164164
// 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'
@@ -118,7 +118,7 @@ class DefaultPingService implements Startable, PingService {
118118
})
119119

120120
onAbort = () => {
121-
stream?.abort(new CodeError('ping timeout', codes.ERR_TIMEOUT))
121+
stream?.abort(new CodeError('ping timeout', ERR_TIMEOUT))
122122
}
123123

124124
// make stream abortable

packages/libp2p/src/upgrader.ts

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

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

171171
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)