From eeb07d96f549dad385fd1bc14664c26781816816 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 10 Jun 2022 11:25:59 +0100 Subject: [PATCH] chore: initial commit [skip ci] --- .github/dependabot.yml | 8 + .github/workflows/automerge.yml | 8 + .github/workflows/js-test-and-release.yml | 152 ++++++++ .gitignore | 7 + LICENSE | 4 + LICENSE-APACHE | 5 + LICENSE-MIT | 19 + README.md | 43 +++ package.json | 166 ++++++++ src/index.ts | 444 ++++++++++++++++++++++ tsconfig.json | 9 + 11 files changed, 865 insertions(+) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/automerge.yml create mode 100644 .github/workflows/js-test-and-release.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT create mode 100644 README.md create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..290ad02 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,8 @@ +version: 2 +updates: +- package-ecosystem: npm + directory: "/" + schedule: + interval: daily + time: "10:00" + open-pull-requests-limit: 10 diff --git a/.github/workflows/automerge.yml b/.github/workflows/automerge.yml new file mode 100644 index 0000000..d57c2a0 --- /dev/null +++ b/.github/workflows/automerge.yml @@ -0,0 +1,8 @@ +name: Automerge +on: [ pull_request ] + +jobs: + automerge: + uses: protocol/.github/.github/workflows/automerge.yml@master + with: + job: 'automerge' diff --git a/.github/workflows/js-test-and-release.yml b/.github/workflows/js-test-and-release.yml new file mode 100644 index 0000000..f3f9e72 --- /dev/null +++ b/.github/workflows/js-test-and-release.yml @@ -0,0 +1,152 @@ +name: test & maybe release +on: + push: + branches: + - master # with #262 - ${{{ github.default_branch }}} + pull_request: + branches: + - master # with #262 - ${{{ github.default_branch }}} + +jobs: + + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present lint + - run: npm run --if-present dep-check + + test-node: + needs: check + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest, ubuntu-latest, macos-latest] + node: [16] + fail-fast: true + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node }} + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:node + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + directory: ./.nyc_output + flags: node + + test-chrome: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:chrome + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + directory: ./.nyc_output + flags: chrome + + test-chrome-webworker: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:chrome-webworker + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + directory: ./.nyc_output + flags: chrome-webworker + + test-firefox: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:firefox + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + directory: ./.nyc_output + flags: firefox + + test-firefox-webworker: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:firefox-webworker + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + directory: ./.nyc_output + flags: firefox-webworker + + test-electron-main: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npx xvfb-maybe npm run --if-present test:electron-main + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + directory: ./.nyc_output + flags: electron-main + + test-electron-renderer: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npx xvfb-maybe npm run --if-present test:electron-renderer + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + directory: ./.nyc_output + flags: electron-renderer + + release: + needs: [test-node, test-chrome, test-chrome-webworker, test-firefox, test-firefox-webworker, test-electron-main, test-electron-renderer] + runs-on: ubuntu-latest + if: github.event_name == 'push' && github.ref == 'refs/heads/master' # with #262 - 'refs/heads/${{{ github.default_branch }}}' + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - uses: actions/setup-node@v2 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - uses: ipfs/aegir/actions/docker-login@master + with: + docker-token: ${{ secrets.DOCKER_TOKEN }} + docker-username: ${{ secrets.DOCKER_USERNAME }} + - run: npm run --if-present release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..db79d1f --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +node_modules +coverage +.nyc_output +package-lock.json +yarn.lock +docs +dist diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..20ce483 --- /dev/null +++ b/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/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..14478a3 --- /dev/null +++ b/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/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..72dc60d --- /dev/null +++ b/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/README.md b/README.md new file mode 100644 index 0000000..41855ba --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# @libp2p/components + +[![test & maybe release](https://github.com/libp2p/js-libp2p-components/actions/workflows/js-test-and-release.yml/badge.svg)](https://github.com/libp2p/js-libp2p-components/actions/workflows/js-test-and-release.yml) + +> Container for libp2p components + +## Table of contents + +- [Installation](#installation) +- [Usage](#usage) +- [License](#license) + - [Contribution](#contribution) + +## Installation + +```js +$ npm i @libp2p/components +``` + +## Usage + +```js +import { Components } from '@libp2p/components' + +const components = new Components({ + // .. components here +}) + +// later + +components.getPeerId() +``` + +## License + +Licensed under either of + + * Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0) + * MIT ([LICENSE-MIT](LICENSE-MIT) / http://opensource.org/licenses/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/package.json b/package.json new file mode 100644 index 0000000..92623a9 --- /dev/null +++ b/package.json @@ -0,0 +1,166 @@ +{ + "name": "@libp2p/components", + "version": "0.0.1", + "description": "Container for libp2p components", + "license": "Apache-2.0 OR MIT", + "homepage": "https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-interfaces#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/libp2p/js-libp2p-interfaces.git" + }, + "bugs": { + "url": "https://github.com/libp2p/js-libp2p-interfaces/issues" + }, + "keywords": [ + "interface", + "libp2p" + ], + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + }, + "type": "module", + "types": "./dist/src/index.d.ts", + "typesVersions": { + "*": { + "*": [ + "*", + "dist/*", + "dist/src/*", + "dist/src/*/index" + ], + "src/*": [ + "*", + "dist/*", + "dist/src/*", + "dist/src/*/index" + ] + } + }, + "files": [ + "src", + "dist/src", + "!dist/test", + "!**/*.tsbuildinfo" + ], + "exports": { + ".": { + "import": "./dist/src/index.js", + "types": "./dist/src/index.d.ts" + } + }, + "eslintConfig": { + "extends": "ipfs", + "parserOptions": { + "sourceType": "module" + } + }, + "release": { + "branches": [ + "master" + ], + "plugins": [ + [ + "@semantic-release/commit-analyzer", + { + "preset": "conventionalcommits", + "releaseRules": [ + { + "breaking": true, + "release": "major" + }, + { + "revert": true, + "release": "patch" + }, + { + "type": "feat", + "release": "minor" + }, + { + "type": "fix", + "release": "patch" + }, + { + "type": "chore", + "release": "patch" + }, + { + "type": "docs", + "release": "patch" + }, + { + "type": "test", + "release": "patch" + }, + { + "scope": "no-release", + "release": false + } + ] + } + ], + [ + "@semantic-release/release-notes-generator", + { + "preset": "conventionalcommits", + "presetConfig": { + "types": [ + { + "type": "feat", + "section": "Features" + }, + { + "type": "fix", + "section": "Bug Fixes" + }, + { + "type": "chore", + "section": "Trivial Changes" + }, + { + "type": "docs", + "section": "Trivial Changes" + }, + { + "type": "test", + "section": "Tests" + } + ] + } + } + ], + "@semantic-release/changelog", + "@semantic-release/npm", + "@semantic-release/github", + "@semantic-release/git" + ] + }, + "scripts": { + "clean": "aegir clean", + "lint": "aegir lint", + "dep-check": "aegir dep-check", + "build": "aegir build", + "release": "aegir release" + }, + "dependencies": { + "err-code": "^3.0.1", + "@libp2p/interface-connection": "*", + "@libp2p/interface-content-routing": "*", + "@libp2p/interface-address-manager": "*", + "@libp2p/interfaces": "^2.0.3", + "@libp2p/interface-metrics": "*", + "@libp2p/interface-peer-id": "*", + "@libp2p/interface-peer-store": "*", + "@libp2p/interface-peer-routing": "*", + "@libp2p/interface-registrar": "*", + "@libp2p/interface-transport": "*", + "@libp2p/interface-pubsub": "*", + "@libp2p/interface-dht": "*", + "@libp2p/interface-connection-manager": "*", + "interface-datastore": "^6.1.0" + }, + "devDependencies": { + "aegir": "^37.0.7" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..20353a0 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,444 @@ +import errCode from 'err-code' +import type { ConnectionGater, ConnectionProtector } from '@libp2p/interface-connection' +import type { ContentRouting } from '@libp2p/interface-content-routing' +import type { AddressManager } from '@libp2p/interface-address-manager' +import { isStartable, Startable } from '@libp2p/interfaces/startable' +import type { Metrics } from '@libp2p/interface-metrics' +import type { PeerId } from '@libp2p/interface-peer-id' +import type { PeerRouting } from '@libp2p/interface-peer-routing' +import type { PeerStore } from '@libp2p/interface-peer-store' +import type { Registrar } from '@libp2p/interface-registrar' +import type { TransportManager, Upgrader } from '@libp2p/interface-transport' +import type { Datastore } from 'interface-datastore' +import type { PubSub } from '@libp2p/interface-pubsub' +import type { DualDHT } from '@libp2p/interface-dht' +import type { ConnectionManager } from '@libp2p/interface-connection-manager' + +export interface Initializable { + init: (components: Components) => void +} + +export function isInitializable (obj: any): obj is Initializable { + return obj != null && typeof obj.init === 'function' +} + +export interface ComponentsInit { + peerId?: PeerId + addressManager?: AddressManager + peerStore?: PeerStore + upgrader?: Upgrader + metrics?: Metrics + registrar?: Registrar + connectionManager?: ConnectionManager + transportManager?: TransportManager + connectionGater?: ConnectionGater + contentRouting?: ContentRouting + peerRouting?: PeerRouting + datastore?: Datastore + connectionProtector?: ConnectionProtector + dht?: DualDHT + pubsub?: PubSub +} + +export class Components implements Startable { + private peerId?: PeerId + private addressManager?: AddressManager + private peerStore?: PeerStore + private upgrader?: Upgrader + private metrics?: Metrics + private registrar?: Registrar + private connectionManager?: ConnectionManager + private transportManager?: TransportManager + private connectionGater?: ConnectionGater + private contentRouting?: ContentRouting + private peerRouting?: PeerRouting + private datastore?: Datastore + private connectionProtector?: ConnectionProtector + private dht?: DualDHT + private pubsub?: PubSub + private started = false + + constructor (init: ComponentsInit = {}) { + if (init.peerId != null) { + this.setPeerId(init.peerId) + } + + if (init.addressManager != null) { + this.setAddressManager(init.addressManager) + } + + if (init.peerStore != null) { + this.setPeerStore(init.peerStore) + } + + if (init.upgrader != null) { + this.setUpgrader(init.upgrader) + } + + if (init.metrics != null) { + this.setMetrics(init.metrics) + } + + if (init.registrar != null) { + this.setRegistrar(init.registrar) + } + + if (init.connectionManager != null) { + this.setConnectionManager(init.connectionManager) + } + + if (init.transportManager != null) { + this.setTransportManager(init.transportManager) + } + + if (init.connectionGater != null) { + this.setConnectionGater(init.connectionGater) + } + + if (init.contentRouting != null) { + this.setContentRouting(init.contentRouting) + } + + if (init.peerRouting != null) { + this.setPeerRouting(init.peerRouting) + } + + if (init.datastore != null) { + this.setDatastore(init.datastore) + } + + if (init.connectionProtector != null) { + this.setConnectionProtector(init.connectionProtector) + } + + if (init.dht != null) { + this.setDHT(init.dht) + } + + if (init.pubsub != null) { + this.setPubSub(init.pubsub) + } + } + + isStarted () { + return this.started + } + + async beforeStart () { + await Promise.all( + Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { + if (startable.beforeStart != null) { + await startable.beforeStart() + } + }) + ) + } + + async start () { + await Promise.all( + Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { + await startable.start() + }) + ) + + this.started = true + } + + async afterStart () { + await Promise.all( + Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { + if (startable.afterStart != null) { + await startable.afterStart() + } + }) + ) + } + + async beforeStop () { + await Promise.all( + Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { + if (startable.beforeStop != null) { + await startable.beforeStop() + } + }) + ) + } + + async stop () { + await Promise.all( + Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { + await startable.stop() + }) + ) + + this.started = false + } + + async afterStop () { + await Promise.all( + Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { + if (startable.afterStop != null) { + await startable.afterStop() + } + }) + ) + } + + setPeerId (peerId: PeerId) { + this.peerId = peerId + + return peerId + } + + getPeerId (): PeerId { + if (this.peerId == null) { + throw errCode(new Error('peerId not set'), 'ERR_SERVICE_MISSING') + } + + return this.peerId + } + + setMetrics (metrics: Metrics) { + this.metrics = metrics + + if (isInitializable(metrics)) { + metrics.init(this) + } + + return metrics + } + + getMetrics (): Metrics | undefined { + return this.metrics + } + + setAddressManager (addressManager: AddressManager) { + this.addressManager = addressManager + + if (isInitializable(addressManager)) { + addressManager.init(this) + } + + return addressManager + } + + getAddressManager (): AddressManager { + if (this.addressManager == null) { + throw errCode(new Error('addressManager not set'), 'ERR_SERVICE_MISSING') + } + + return this.addressManager + } + + setPeerStore (peerStore: PeerStore) { + this.peerStore = peerStore + + if (isInitializable(peerStore)) { + peerStore.init(this) + } + + return peerStore + } + + getPeerStore (): PeerStore { + if (this.peerStore == null) { + throw errCode(new Error('peerStore not set'), 'ERR_SERVICE_MISSING') + } + + return this.peerStore + } + + setUpgrader (upgrader: Upgrader) { + this.upgrader = upgrader + + if (isInitializable(upgrader)) { + upgrader.init(this) + } + + return upgrader + } + + getUpgrader (): Upgrader { + if (this.upgrader == null) { + throw errCode(new Error('upgrader not set'), 'ERR_SERVICE_MISSING') + } + + return this.upgrader + } + + setRegistrar (registrar: Registrar) { + this.registrar = registrar + + if (isInitializable(registrar)) { + registrar.init(this) + } + + return registrar + } + + getRegistrar (): Registrar { + if (this.registrar == null) { + throw errCode(new Error('registrar not set'), 'ERR_SERVICE_MISSING') + } + + return this.registrar + } + + setConnectionManager (connectionManager: ConnectionManager) { + this.connectionManager = connectionManager + + if (isInitializable(connectionManager)) { + connectionManager.init(this) + } + + return connectionManager + } + + getConnectionManager (): ConnectionManager { + if (this.connectionManager == null) { + throw errCode(new Error('connectionManager not set'), 'ERR_SERVICE_MISSING') + } + + return this.connectionManager + } + + setTransportManager (transportManager: TransportManager) { + this.transportManager = transportManager + + if (isInitializable(transportManager)) { + transportManager.init(this) + } + + return transportManager + } + + getTransportManager (): TransportManager { + if (this.transportManager == null) { + throw errCode(new Error('transportManager not set'), 'ERR_SERVICE_MISSING') + } + + return this.transportManager + } + + setConnectionGater (connectionGater: ConnectionGater) { + this.connectionGater = connectionGater + + if (isInitializable(connectionGater)) { + connectionGater.init(this) + } + + return connectionGater + } + + getConnectionGater (): ConnectionGater { + if (this.connectionGater == null) { + throw errCode(new Error('connectionGater not set'), 'ERR_SERVICE_MISSING') + } + + return this.connectionGater + } + + setContentRouting (contentRouting: ContentRouting) { + this.contentRouting = contentRouting + + if (isInitializable(contentRouting)) { + contentRouting.init(this) + } + + return contentRouting + } + + getContentRouting (): ContentRouting { + if (this.contentRouting == null) { + throw errCode(new Error('contentRouting not set'), 'ERR_SERVICE_MISSING') + } + + return this.contentRouting + } + + setPeerRouting (peerRouting: PeerRouting) { + this.peerRouting = peerRouting + + if (isInitializable(peerRouting)) { + peerRouting.init(this) + } + + return peerRouting + } + + getPeerRouting (): PeerRouting { + if (this.peerRouting == null) { + throw errCode(new Error('peerRouting not set'), 'ERR_SERVICE_MISSING') + } + + return this.peerRouting + } + + setDatastore (datastore: Datastore) { + this.datastore = datastore + + if (isInitializable(datastore)) { + datastore.init(this) + } + + return datastore + } + + getDatastore (): Datastore { + if (this.datastore == null) { + throw errCode(new Error('datastore not set'), 'ERR_SERVICE_MISSING') + } + + return this.datastore + } + + setConnectionProtector (connectionProtector: ConnectionProtector) { + this.connectionProtector = connectionProtector + + if (isInitializable(connectionProtector)) { + connectionProtector.init(this) + } + + return connectionProtector + } + + getConnectionProtector (): ConnectionProtector | undefined { + return this.connectionProtector + } + + setDHT (dht: DualDHT) { + this.dht = dht + + if (isInitializable(dht)) { + dht.init(this) + } + + return dht + } + + getDHT (): DualDHT { + if (this.dht == null) { + throw errCode(new Error('dht not set'), 'ERR_SERVICE_MISSING') + } + + return this.dht + } + + setPubSub (pubsub: PubSub) { + this.pubsub = pubsub + + if (isInitializable(pubsub)) { + pubsub.init(this) + } + + return pubsub + } + + getPubSub (): PubSub { + if (this.pubsub == null) { + throw errCode(new Error('pubsub not set'), 'ERR_SERVICE_MISSING') + } + + return this.pubsub + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..5fe8ea4 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "src" + ] +}