From bd2936f5be9873422b5f251c2755fed40de8393c Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 23 Oct 2023 17:50:22 -0400 Subject: [PATCH] refactor!: move autonat into separate package (#2107) Co-authored-by: chad --- doc/CONFIGURATION.md | 2 +- doc/migrations/v0.46-v1.0.0.md | 10 +-- doc/migrations/v0.46-v1.0.md | 85 ------------------- packages/interface/src/errors.ts | 4 + packages/libp2p/package.json | 6 -- .../src/connection-manager/dial-queue.ts | 4 +- packages/libp2p/src/content-routing/index.ts | 2 +- packages/libp2p/src/errors.ts | 1 - packages/libp2p/src/fetch/index.ts | 4 +- packages/libp2p/src/ping/index.ts | 4 +- packages/libp2p/src/upgrader.ts | 4 +- .../test/connection-manager/direct.node.ts | 4 +- .../test/connection-manager/direct.spec.ts | 4 +- packages/libp2p/test/fetch/index.spec.ts | 3 +- packages/libp2p/test/ping/index.spec.ts | 3 +- packages/libp2p/typedoc.json | 1 - packages/protocol-autonat/LICENSE | 4 + packages/protocol-autonat/LICENSE-APACHE | 5 ++ packages/protocol-autonat/LICENSE-MIT | 19 +++++ packages/protocol-autonat/README.md | 64 ++++++++++++++ packages/protocol-autonat/package.json | 71 ++++++++++++++++ .../src}/constants.ts | 0 .../autonat => protocol-autonat/src}/index.ts | 9 +- .../src}/pb/index.proto | 0 .../src}/pb/index.ts | 0 .../test}/index.spec.ts | 28 +++--- packages/protocol-autonat/tsconfig.json | 27 ++++++ packages/protocol-autonat/typedoc.json | 5 ++ 28 files changed, 237 insertions(+), 136 deletions(-) delete mode 100644 doc/migrations/v0.46-v1.0.md create mode 100644 packages/protocol-autonat/LICENSE create mode 100644 packages/protocol-autonat/LICENSE-APACHE create mode 100644 packages/protocol-autonat/LICENSE-MIT create mode 100644 packages/protocol-autonat/README.md create mode 100644 packages/protocol-autonat/package.json rename packages/{libp2p/src/autonat => protocol-autonat/src}/constants.ts (100%) rename packages/{libp2p/src/autonat => protocol-autonat/src}/index.ts (98%) rename packages/{libp2p/src/autonat => protocol-autonat/src}/pb/index.proto (100%) rename packages/{libp2p/src/autonat => protocol-autonat/src}/pb/index.ts (100%) rename packages/{libp2p/test/autonat => protocol-autonat/test}/index.spec.ts (96%) create mode 100644 packages/protocol-autonat/tsconfig.json create mode 100644 packages/protocol-autonat/typedoc.json diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index 2173ac59be..6f82e087bd 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -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: { diff --git a/doc/migrations/v0.46-v1.0.0.md b/doc/migrations/v0.46-v1.0.0.md index b4bee48972..82132e798f 100644 --- a/doc/migrations/v0.46-v1.0.0.md +++ b/doc/migrations/v0.46-v1.0.0.md @@ -1,19 +1,19 @@ + # Migrating to libp2p@1.0.0 A migration guide for refactoring your application code from libp2p `v0.46` to `v1.0.0`. ## Table of Contents -- [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 { autoNATService } from 'libp2p/autonat' diff --git a/doc/migrations/v0.46-v1.0.md b/doc/migrations/v0.46-v1.0.md deleted file mode 100644 index 420048289a..0000000000 --- a/doc/migrations/v0.46-v1.0.md +++ /dev/null @@ -1,85 +0,0 @@ - -# Migrating to libp2p@1.0 - -A migration guide for refactoring your application code from libp2p v0.46 to v1.0. - -## Table of Contents - -- [API](#api) -- [Module Updates](#module-updates) -- [Metrics](#metrics) - -## API - - - -### KeyChain - -The KeyChain object is no longer included on Libp2p and must be instantiated explicitly if desired. - -**Before** - -```ts -import type { KeyChain } from '@libp2p/interface/keychain' - -const libp2p = await createLibp2p(...) - -const keychain: KeyChain = libp2p.keychain -``` - -***After*** - -```ts -import { DefaultKeyChain } from '@libp2p/keychain' -import type { KeyChain } from '@libp2p/interface/keychain' - -const libp2p = await createLibp2p({ - ... - services: { - keychain: (components) => new DefaultKeyChain(components, { - ...DefaultKeyChain.generateOptions() - }) - } -}) - -const keychain: KeyChain = libp2p.services.keychain -``` - -## Module Updates - -With this release you should update the following libp2p modules if you are relying on them: - - - -```json - -``` - -## Metrics - -The following metrics were renamed: - -`libp2p_dialler_pending_dials` => `libp2p_dial_queue_pending_dials` -`libp2p_dialler_in_progress_dials` => `libp2p_dial_queue_in_progress_dials` diff --git a/packages/interface/src/errors.ts b/packages/interface/src/errors.ts index a5911adb76..4f5f8b4c3a 100644 --- a/packages/interface/src/errors.ts +++ b/packages/interface/src/errors.ts @@ -65,3 +65,7 @@ export class InvalidCryptoTransmissionError extends Error { static readonly code = 'ERR_INVALID_CRYPTO_TRANSMISSION' } + +// Error codes + +export const ERR_TIMEOUT = 'ERR_TIMEOUT' diff --git a/packages/libp2p/package.json b/packages/libp2p/package.json index cb1330ab64..4af51b066b 100644 --- a/packages/libp2p/package.json +++ b/packages/libp2p/package.json @@ -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" @@ -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", @@ -149,7 +144,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", diff --git a/packages/libp2p/src/connection-manager/dial-queue.ts b/packages/libp2p/src/connection-manager/dial-queue.ts index a167c43ea0..f56cc6f651 100644 --- a/packages/libp2p/src/connection-manager/dial-queue.ts +++ b/packages/libp2p/src/connection-manager/dial-queue.ts @@ -1,5 +1,5 @@ import { setMaxListeners } from 'events' -import { AbortError, CodeError } from '@libp2p/interface/errors' +import { AbortError, CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { logger } from '@libp2p/logger' import { defaultAddressSort } from '@libp2p/utils/address-sort' import { type Multiaddr, type Resolver, resolvers } from '@multiformats/multiaddr' @@ -250,7 +250,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 } diff --git a/packages/libp2p/src/content-routing/index.ts b/packages/libp2p/src/content-routing/index.ts index f8a17a3cdd..a96c2bd9ab 100644 --- a/packages/libp2p/src/content-routing/index.ts +++ b/packages/libp2p/src/content-routing/index.ts @@ -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, diff --git a/packages/libp2p/src/errors.ts b/packages/libp2p/src/errors.ts index a895c1b5b2..0b84215aa9 100644 --- a/packages/libp2p/src/errors.ts +++ b/packages/libp2p/src/errors.ts @@ -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', diff --git a/packages/libp2p/src/fetch/index.ts b/packages/libp2p/src/fetch/index.ts index 3523b2b17d..d70e932451 100644 --- a/packages/libp2p/src/fetch/index.ts +++ b/packages/libp2p/src/fetch/index.ts @@ -1,5 +1,5 @@ import { setMaxListeners } from 'events' -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 * as lp from 'it-length-prefixed' @@ -158,7 +158,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 diff --git a/packages/libp2p/src/ping/index.ts b/packages/libp2p/src/ping/index.ts index ea8d23db5e..ea87229d9a 100644 --- a/packages/libp2p/src/ping/index.ts +++ b/packages/libp2p/src/ping/index.ts @@ -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' @@ -118,7 +118,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 diff --git a/packages/libp2p/src/upgrader.ts b/packages/libp2p/src/upgrader.ts index ad27ba02db..fe847478c0 100644 --- a/packages/libp2p/src/upgrader.ts +++ b/packages/libp2p/src/upgrader.ts @@ -1,5 +1,5 @@ import { setMaxListeners } from 'events' -import { CodeError } from '@libp2p/interface/errors' +import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { logger } from '@libp2p/logger' import * as mss from '@libp2p/multistream-select' import { peerIdFromString } from '@libp2p/peer-id' @@ -165,7 +165,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 }) diff --git a/packages/libp2p/test/connection-manager/direct.node.ts b/packages/libp2p/test/connection-manager/direct.node.ts index ae5ff3aed3..75e20b7436 100644 --- a/packages/libp2p/test/connection-manager/direct.node.ts +++ b/packages/libp2p/test/connection-manager/direct.node.ts @@ -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' @@ -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 () => { diff --git a/packages/libp2p/test/connection-manager/direct.spec.ts b/packages/libp2p/test/connection-manager/direct.spec.ts index 305ae4e524..7f9d9d1ab4 100644 --- a/packages/libp2p/test/connection-manager/direct.spec.ts +++ b/packages/libp2p/test/connection-manager/direct.spec.ts @@ -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' @@ -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 () => { diff --git a/packages/libp2p/test/fetch/index.spec.ts b/packages/libp2p/test/fetch/index.spec.ts index 7b90a0f7db..dd7d1d921a 100644 --- a/packages/libp2p/test/fetch/index.spec.ts +++ b/packages/libp2p/test/fetch/index.spec.ts @@ -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' @@ -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) diff --git a/packages/libp2p/test/ping/index.spec.ts b/packages/libp2p/test/ping/index.spec.ts index 9b6aef1385..dd6c877f50 100644 --- a/packages/libp2p/test/ping/index.spec.ts +++ b/packages/libp2p/test/ping/index.spec.ts @@ -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' @@ -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) diff --git a/packages/libp2p/typedoc.json b/packages/libp2p/typedoc.json index 8ed11ea9f3..5629b3c79d 100644 --- a/packages/libp2p/typedoc.json +++ b/packages/libp2p/typedoc.json @@ -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", diff --git a/packages/protocol-autonat/LICENSE b/packages/protocol-autonat/LICENSE new file mode 100644 index 0000000000..20ce483c86 --- /dev/null +++ b/packages/protocol-autonat/LICENSE @@ -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 diff --git a/packages/protocol-autonat/LICENSE-APACHE b/packages/protocol-autonat/LICENSE-APACHE new file mode 100644 index 0000000000..14478a3b60 --- /dev/null +++ b/packages/protocol-autonat/LICENSE-APACHE @@ -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. diff --git a/packages/protocol-autonat/LICENSE-MIT b/packages/protocol-autonat/LICENSE-MIT new file mode 100644 index 0000000000..72dc60d84b --- /dev/null +++ b/packages/protocol-autonat/LICENSE-MIT @@ -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. diff --git a/packages/protocol-autonat/README.md b/packages/protocol-autonat/README.md new file mode 100644 index 0000000000..e11bd46ab6 --- /dev/null +++ b/packages/protocol-autonat/README.md @@ -0,0 +1,64 @@ +[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/) +[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io) +[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p) +[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster) + +> Implementation of Autonat Protocol + +# About + +Use the `autoNATService` function to add support for the [AutoNAT protocol](https://docs.libp2p.io/concepts/nat/autonat/) +to libp2p. + +## Example + +```typescript +import { createLibp2p } from 'libp2p' +import { autoNATService } from '@libp2p/autonat' + +const node = await createLibp2p({ + // ...other options + services: { + autoNAT: autoNATService() + } +}) +``` + +## Table of contents + +- [About](#about) + - [Example](#example) +- [Install](#install) + - [Browser ` +``` + +# API Docs + +- + +# License + +Licensed under either of + +- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / ) +- MIT ([LICENSE-MIT](LICENSE-MIT) / ) + +# Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. diff --git a/packages/protocol-autonat/package.json b/packages/protocol-autonat/package.json new file mode 100644 index 0000000000..0648c94c11 --- /dev/null +++ b/packages/protocol-autonat/package.json @@ -0,0 +1,71 @@ +{ + "name": "@libp2p/autonat", + "version": "1.0.0", + "description": "Implementation of Autonat Protocol", + "license": "Apache-2.0 OR MIT", + "homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/protocol-autonat#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/libp2p/js-libp2p.git" + }, + "bugs": { + "url": "https://github.com/libp2p/js-libp2p/issues" + }, + "type": "module", + "types": "./dist/src/index.d.ts", + "files": [ + "src", + "dist", + "!dist/test", + "!**/*.tsbuildinfo" + ], + "exports": { + ".": { + "types": "./dist/src/index.d.ts", + "import": "./dist/src/index.js" + } + }, + "eslintConfig": { + "extends": "ipfs", + "parserOptions": { + "sourceType": "module" + } + }, + "scripts": { + "start": "node dist/src/main.js", + "build": "aegir build", + "test": "aegir test", + "clean": "aegir clean", + "generate": "protons ./src/pb/index.proto", + "lint": "aegir lint", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "dep-check": "aegir dep-check" + }, + "dependencies": { + "@libp2p/interface": "^0.1.2", + "@libp2p/interface-internal": "^0.1.5", + "@libp2p/peer-id": "^3.0.2" , + "@libp2p/peer-id-factory": "^3.0.4" , + "@libp2p/logger": "^3.0.2", + "@multiformats/multiaddr": "^12.1.5", + "it-first": "^3.0.1", + "it-length-prefixed": "^9.0.1", + "it-map": "^3.0.3", + "it-parallel": "^3.0.0", + "it-pipe": "^3.0.1", + "private-ip": "^3.0.0", + "protons-runtime": "^5.0.0", + "uint8arraylist": "^2.4.3" + }, + "devDependencies": { + "aegir": "^41.0.2", + "it-all": "^3.0.1", + "it-pushable": "^3.2.0", + "sinon": "^17.0.0", + "sinon-ts": "^1.0.0" + } +} diff --git a/packages/libp2p/src/autonat/constants.ts b/packages/protocol-autonat/src/constants.ts similarity index 100% rename from packages/libp2p/src/autonat/constants.ts rename to packages/protocol-autonat/src/constants.ts diff --git a/packages/libp2p/src/autonat/index.ts b/packages/protocol-autonat/src/index.ts similarity index 98% rename from packages/libp2p/src/autonat/index.ts rename to packages/protocol-autonat/src/index.ts index 3f70e6a850..0e35b51c7b 100644 --- a/packages/libp2p/src/autonat/index.ts +++ b/packages/protocol-autonat/src/index.ts @@ -8,7 +8,7 @@ * * ```typescript * import { createLibp2p } from 'libp2p' - * import { autoNATService } from 'libp2p/autonat' + * import { autoNATService } from '@libp2p/autonat' * * const node = await createLibp2p({ * // ...other options @@ -20,7 +20,7 @@ */ import { setMaxListeners } from 'events' -import { CodeError } from '@libp2p/interface/errors' +import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { logger } from '@libp2p/logger' import { peerIdFromBytes } from '@libp2p/peer-id' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -31,7 +31,6 @@ import map from 'it-map' import parallel from 'it-parallel' import { pipe } from 'it-pipe' import isPrivateIp from 'private-ip' -import { codes } from '../errors.js' import { MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, @@ -157,7 +156,7 @@ class DefaultAutoNATService implements Startable { const signal = AbortSignal.timeout(this.timeout) const onAbort = (): void => { - data.stream.abort(new CodeError('handleIncomingAutonatStream timeout', codes.ERR_TIMEOUT)) + data.stream.abort(new CodeError('handleIncomingAutonatStream timeout', ERR_TIMEOUT)) } signal.addEventListener('abort', onAbort, { once: true }) @@ -472,7 +471,7 @@ class DefaultAutoNATService implements Startable { signal }) - onAbort = () => { stream.abort(new CodeError('verifyAddress timeout', codes.ERR_TIMEOUT)) } + onAbort = () => { stream.abort(new CodeError('verifyAddress timeout', ERR_TIMEOUT)) } signal.addEventListener('abort', onAbort, { once: true }) diff --git a/packages/libp2p/src/autonat/pb/index.proto b/packages/protocol-autonat/src/pb/index.proto similarity index 100% rename from packages/libp2p/src/autonat/pb/index.proto rename to packages/protocol-autonat/src/pb/index.proto diff --git a/packages/libp2p/src/autonat/pb/index.ts b/packages/protocol-autonat/src/pb/index.ts similarity index 100% rename from packages/libp2p/src/autonat/pb/index.ts rename to packages/protocol-autonat/src/pb/index.ts diff --git a/packages/libp2p/test/autonat/index.spec.ts b/packages/protocol-autonat/test/index.spec.ts similarity index 96% rename from packages/libp2p/test/autonat/index.spec.ts rename to packages/protocol-autonat/test/index.spec.ts index 250033f7b3..09beda2c67 100644 --- a/packages/libp2p/test/autonat/index.spec.ts +++ b/packages/protocol-autonat/test/index.spec.ts @@ -12,20 +12,17 @@ import { pushable } from 'it-pushable' import sinon from 'sinon' import { stubInterface } from 'sinon-ts' import { Uint8ArrayList } from 'uint8arraylist' -import { PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION } from '../../src/autonat/constants.js' -import { autoNATService } from '../../src/autonat/index.js' -import { Message } from '../../src/autonat/pb/index.js' -import { defaultComponents } from '../../src/components.js' -import type { AutoNATServiceInit } from '../../src/autonat/index.js' -import type { Components } from '../../src/components.js' -import type { DefaultConnectionManager } from '../../src/connection-manager/index.js' +import { PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION } from '../src/constants.js' +import { autoNATService } from '../src/index.js' +import { Message } from '../src/pb/index.js' +import type { AutoNATComponents, AutoNATServiceInit } from '../src/index.js' import type { Connection, Stream } from '@libp2p/interface/connection' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerInfo } from '@libp2p/interface/peer-info' import type { PeerRouting } from '@libp2p/interface/peer-routing' -import type { PeerStore } from '@libp2p/interface/peer-store' import type { Transport } from '@libp2p/interface/transport' import type { AddressManager } from '@libp2p/interface-internal/address-manager' +import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager' import type { Registrar } from '@libp2p/interface-internal/registrar' import type { TransportManager } from '@libp2p/interface-internal/transport-manager' import type { Multiaddr } from '@multiformats/multiaddr' @@ -42,13 +39,12 @@ const defaultInit: AutoNATServiceInit = { describe('autonat', () => { let service: any - let components: Components + let components: AutoNATComponents let peerRouting: StubbedInstance let registrar: StubbedInstance let addressManager: StubbedInstance - let connectionManager: StubbedInstance + let connectionManager: StubbedInstance let transportManager: StubbedInstance - let peerStore: StubbedInstance beforeEach(async () => { peerRouting = stubInterface() @@ -56,19 +52,17 @@ describe('autonat', () => { addressManager = stubInterface() addressManager.getAddresses.returns([]) - connectionManager = stubInterface() + connectionManager = stubInterface() transportManager = stubInterface() - peerStore = stubInterface() - components = defaultComponents({ + components = { peerId: await createEd25519PeerId(), peerRouting, registrar, addressManager, connectionManager, - transportManager, - peerStore - }) + transportManager + } service = autoNATService(defaultInit)(components) diff --git a/packages/protocol-autonat/tsconfig.json b/packages/protocol-autonat/tsconfig.json new file mode 100644 index 0000000000..fd38c2c239 --- /dev/null +++ b/packages/protocol-autonat/tsconfig.json @@ -0,0 +1,27 @@ +{ + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "src", + "test" + ], + "references": [ + { + "path": "../interface" + }, + { + "path": "../interface-internal" + }, + { + "path": "../peer-id" + }, + { + "path": "../peer-id-factory" + }, + { + "path": "../logger" + } + ] +} diff --git a/packages/protocol-autonat/typedoc.json b/packages/protocol-autonat/typedoc.json new file mode 100644 index 0000000000..f599dc728d --- /dev/null +++ b/packages/protocol-autonat/typedoc.json @@ -0,0 +1,5 @@ +{ + "entryPoints": [ + "./src/index.ts" + ] +}