Skip to content

Commit

Permalink
refactor!: move autonat into separate package (#2107)
Browse files Browse the repository at this point in the history
Co-authored-by: chad <chad.nehemiah94@gmail.com>
  • Loading branch information
2 people authored and achingbrain committed Nov 8, 2023
1 parent 343daa5 commit 8d18782
Show file tree
Hide file tree
Showing 30 changed files with 371 additions and 246 deletions.
1 change: 1 addition & 0 deletions .release-please.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"packages/peer-id-factory": {},
"packages/peer-record": {},
"packages/peer-store": {},
"packages/protocol-autonat": {},
"packages/protocol-perf": {},
"packages/pubsub": {},
"packages/pubsub-floodsub": {},
Expand Down
2 changes: 1 addition & 1 deletion doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ For more information see https://docs.libp2p.io/concepts/nat/autonat/#what-is-au

```ts
import { createLibp2p } from 'libp2p'
import { autoNATService } from 'libp2p/autonat'
import { autoNATService } from '@libp2p/autonat'

const node = await createLibp2p({
services: {
Expand Down
26 changes: 20 additions & 6 deletions doc/migrations/v0.46-v1.0.0.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
<!--Specify versions for migration below-->
# Migrating to libp2p@1.0.0 <!-- omit in toc -->

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

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

- [New features](#new-features)
- [Breaking changes](#breaking-changes)
- [AutoNAT](#autonat)
- [KeyChain](#keychain)
- [Metrics](#metrics)

## New features
## AutoNAT

...
The AutoNAT service is now published in its own package.

## Breaking changes
**Before**

```ts
import { createLibp2p } from 'libp2p'
import { autoNATService } from 'libp2p/autonat'

const node = await createLibp2p({
services: {
autoNAT: autoNATService()
}
})
```

**After**

```ts
import { autoNATService } from '@libp2p/autonat'
import { createLibp2p } from 'libp2p'
import { autoNAT } from '@libp2p/autonat'

const node = await createLibp2p({
services: {
autoNAT: autoNAT()
}
})
```

## KeyChain
Expand Down
85 changes: 0 additions & 85 deletions doc/migrations/v0.46-v1.0.md

This file was deleted.

4 changes: 4 additions & 0 deletions packages/interface/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ export class InvalidCryptoTransmissionError extends Error {

static readonly code = 'ERR_INVALID_CRYPTO_TRANSMISSION'
}

// Error codes

export const ERR_TIMEOUT = 'ERR_TIMEOUT'
6 changes: 0 additions & 6 deletions packages/libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
},
"./autonat": {
"types": "./dist/src/autonat/index.d.ts",
"import": "./dist/src/autonat/index.js"
},
"./circuit-relay": {
"types": "./dist/src/circuit-relay/index.d.ts",
"import": "./dist/src/circuit-relay/index.js"
Expand Down Expand Up @@ -104,7 +100,6 @@
"prepublishOnly": "node scripts/update-version.js && npm run build",
"build": "aegir build",
"generate": "run-s generate:proto:*",
"generate:proto:autonat": "protons ./src/autonat/pb/index.proto",
"generate:proto:circuit-relay": "protons ./src/circuit-relay/pb/index.proto",
"generate:proto:dcutr": "protons ./src/dcutr/pb/message.proto",
"generate:proto:fetch": "protons ./src/fetch/pb/proto.proto",
Expand Down Expand Up @@ -148,7 +143,6 @@
"it-map": "^3.0.3",
"it-merge": "^3.0.0",
"it-pair": "^2.0.6",
"it-parallel": "^3.0.0",
"it-pipe": "^3.0.1",
"it-protobuf-stream": "^1.0.0",
"it-stream-types": "^2.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/src/connection-manager/dial-queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbortError, CodeError } from '@libp2p/interface/errors'
import { AbortError, CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
import { setMaxListeners } from '@libp2p/interface/events'
import { PeerMap } from '@libp2p/peer-collections'
import { defaultAddressSort } from '@libp2p/utils/address-sort'
Expand Down Expand Up @@ -269,7 +269,7 @@ export class DialQueue {

// Error is a timeout
if (signal.aborted) {
const error = new CodeError(err.message, codes.ERR_TIMEOUT)
const error = new CodeError(err.message, ERR_TIMEOUT)
throw error
}

Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p/src/content-routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CodeError } from '@libp2p/interface/errors'
import merge from 'it-merge'
import { pipe } from 'it-pipe'
import { messages, codes } from '../errors.js'
import { codes, messages } from '../errors.js'
import {
storeAddresses,
uniquePeers,
Expand Down
1 change: 0 additions & 1 deletion packages/libp2p/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export enum codes {
ERR_INVALID_PEER = 'ERR_INVALID_PEER',
ERR_MUXER_UNAVAILABLE = 'ERR_MUXER_UNAVAILABLE',
ERR_NOT_FOUND = 'ERR_NOT_FOUND',
ERR_TIMEOUT = 'ERR_TIMEOUT',
ERR_TRANSPORT_UNAVAILABLE = 'ERR_TRANSPORT_UNAVAILABLE',
ERR_TRANSPORT_DIAL_FAILED = 'ERR_TRANSPORT_DIAL_FAILED',
ERR_UNSUPPORTED_PROTOCOL = 'ERR_UNSUPPORTED_PROTOCOL',
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/src/fetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodeError } from '@libp2p/interface/errors'
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
import { setMaxListeners } from '@libp2p/interface/events'
import { logger } from '@libp2p/logger'
import first from 'it-first'
Expand Down Expand Up @@ -155,7 +155,7 @@ class DefaultFetchService implements Startable, FetchService {
})

onAbort = () => {
stream?.abort(new CodeError('fetch timeout', codes.ERR_TIMEOUT))
stream?.abort(new CodeError('fetch timeout', ERR_TIMEOUT))
}

// make stream abortable
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/src/ping/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomBytes } from '@libp2p/crypto'
import { CodeError } from '@libp2p/interface/errors'
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
import { logger } from '@libp2p/logger'
import first from 'it-first'
import { pipe } from 'it-pipe'
Expand Down Expand Up @@ -125,7 +125,7 @@ class DefaultPingService implements Startable, PingService {
})

onAbort = () => {
stream?.abort(new CodeError('ping timeout', codes.ERR_TIMEOUT))
stream?.abort(new CodeError('ping timeout', ERR_TIMEOUT))
}

// make stream abortable
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/src/upgrader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodeError } from '@libp2p/interface/errors'
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
import { setMaxListeners } from '@libp2p/interface/events'
import * as mss from '@libp2p/multistream-select'
import { peerIdFromString } from '@libp2p/peer-id'
Expand Down Expand Up @@ -167,7 +167,7 @@ export class DefaultUpgrader implements Upgrader {
const signal = AbortSignal.timeout(this.inboundUpgradeTimeout)

const onAbort = (): void => {
maConn.abort(new CodeError('inbound upgrade timeout', codes.ERR_TIMEOUT))
maConn.abort(new CodeError('inbound upgrade timeout', ERR_TIMEOUT))
}

signal.addEventListener('abort', onAbort, { once: true })
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/test/connection-manager/direct.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import os from 'node:os'
import path from 'node:path'
import { yamux } from '@chainsafe/libp2p-yamux'
import { type Connection, type ConnectionProtector, isConnection } from '@libp2p/interface/connection'
import { AbortError } from '@libp2p/interface/errors'
import { AbortError, ERR_TIMEOUT } from '@libp2p/interface/errors'
import { TypedEventEmitter } from '@libp2p/interface/events'
import { start, stop } from '@libp2p/interface/startable'
import { mockConnection, mockConnectionGater, mockDuplex, mockMultiaddrConnection, mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
Expand Down Expand Up @@ -218,7 +218,7 @@ describe('dialing (direct, TCP)', () => {

await expect(dialer.dial(remoteAddr))
.to.eventually.be.rejectedWith(Error)
.and.to.have.property('code', ErrorCodes.ERR_TIMEOUT)
.and.to.have.property('code', ERR_TIMEOUT)
})

it('should dial to the max concurrency', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/test/connection-manager/direct.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */

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

await expect(connectionManager.openConnection(remoteAddr))
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_TIMEOUT)
.and.to.have.property('code', ERR_TIMEOUT)
})

it('should throw when a peer advertises more than the allowed number of addresses', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/libp2p/test/fetch/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env mocha */

import { ERR_TIMEOUT } from '@libp2p/interface/errors'
import { TypedEventEmitter } from '@libp2p/interface/events'
import { start, stop } from '@libp2p/interface/startable'
import { mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks'
Expand Down Expand Up @@ -135,7 +136,7 @@ describe('fetch', () => {
await expect(localFetch.fetch(remoteComponents.peerId, key, {
signal
}))
.to.eventually.be.rejected.with.property('code', 'ERR_TIMEOUT')
.to.eventually.be.rejected.with.property('code', ERR_TIMEOUT)

// should have closed stream
expect(newStreamSpy).to.have.property('callCount', 1)
Expand Down
3 changes: 2 additions & 1 deletion packages/libp2p/test/ping/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env mocha */

import { ERR_TIMEOUT } from '@libp2p/interface/errors'
import { TypedEventEmitter } from '@libp2p/interface/events'
import { start, stop } from '@libp2p/interface/startable'
import { mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks'
Expand Down Expand Up @@ -125,7 +126,7 @@ describe('ping', () => {
await expect(localPing.ping(remoteComponents.peerId, {
signal
}))
.to.eventually.be.rejected.with.property('code', 'ERR_TIMEOUT')
.to.eventually.be.rejected.with.property('code', ERR_TIMEOUT)

// should have closed stream
expect(newStreamSpy).to.have.property('callCount', 1)
Expand Down
1 change: 0 additions & 1 deletion packages/libp2p/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"entryPoints": [
"./src/index.ts",
"./src/autonat/index.ts",
"./src/circuit-relay/index.ts",
"./src/fetch/index.ts",
"./src/identify/index.ts",
Expand Down
4 changes: 4 additions & 0 deletions packages/protocol-autonat/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This project is dual licensed under MIT and Apache-2.0.

MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions packages/protocol-autonat/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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

http://www.apache.org/licenses/LICENSE-2.0

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.
19 changes: 19 additions & 0 deletions packages/protocol-autonat/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading

0 comments on commit 8d18782

Please sign in to comment.