diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml deleted file mode 100644 index b3558e2..0000000 --- a/.github/workflows/node.js.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: CI - -on: - push: - branches: [master] - pull_request: - branches: [master] - -jobs: - test: - runs-on: ubuntu-latest - timeout-minutes: 10 - - strategy: - matrix: - node-version: [18.x, 20.x] - - steps: - - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - - run: npm install - - run: npm test diff --git a/.woodpecker.yaml b/.woodpecker.yaml new file mode 100644 index 0000000..d254f8d --- /dev/null +++ b/.woodpecker.yaml @@ -0,0 +1,13 @@ +matrix: + NODE_VERSION: + - 18 + - 20 + +steps: + test: + when: + event: [push] + image: node:${NODE_VERSION} + commands: + - npm install + - npm test \ No newline at end of file diff --git a/README.md b/README.md index 388c0a1..7f01e34 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ -**Work in progress** +# pzp-net + +PZP plugin to manage connections with hubs and peers ## Installation -We're not on npm yet. In your package.json, include this as - -```js -"ppppp-net": "github:staltz/ppppp-net" +``` +npm install pzp-net ``` -**TODO:** +## **TODO:** - [x] connect - [x] stage diff --git a/lib/connections.js b/lib/connections.js index daeaead..f7a29eb 100644 --- a/lib/connections.js +++ b/lib/connections.js @@ -1,4 +1,4 @@ -const debug = require('debug')('ppppp:net:connections') +const debug = require('debug')('pzp:net:connections') const createNotify = require('pull-notify') const run = require('promisify-tuple') const IP = require('ip') diff --git a/lib/index.js b/lib/index.js index dcd9e9d..3181770 100644 --- a/lib/index.js +++ b/lib/index.js @@ -91,7 +91,7 @@ function initNet(peer, config) { async function start() { await stats.loaded() glue(infos, connections) - queueMicrotask(scheduler.start.bind(scheduler)) + queueMicrotask(() => scheduler.start.bind(scheduler)) } function stop() { diff --git a/lib/scheduler.js b/lib/scheduler.js index a3aa2d9..35cc1fb 100644 --- a/lib/scheduler.js +++ b/lib/scheduler.js @@ -1,22 +1,30 @@ const awaitable = require('pull-awaitable') const run = require('promisify-tuple') -const debug = require('debug')('ppppp:net:scheduler') +const debug = require('debug')('pzp:net:scheduler') /** - * @typedef {ReturnType} PPPPPDB - * @typedef {ReturnType} PPPPPSet + * @typedef {ReturnType} PZPDB + * @typedef {ReturnType} PZPSet * @typedef {`/${string}`} Multiaddr * @typedef {import('./infos')} Infos * @typedef {import('./connections')} Connections * @typedef {{ - * db?: PPPPPDB; - * set?: PPPPPSet; + * db?: PZPDB; + * set?: PZPSet; * shse: { * pubkey: string; * } * }} Peer */ +/** + * @template T + * @typedef {[T] extends [void] ? + * (...args: [Error] | []) => void : + * (...args: [Error] | [null, T]) => void + * } CB + */ + class Scheduler { /** @type {Peer} */ #peer @@ -62,25 +70,34 @@ class Scheduler { } } - #setupHubDiscovery() { - /** @type {Array | undefined} */ - const multiaddrs = this.#peer.set?.values('hubs') - if (!multiaddrs) return - for (const multiaddr of multiaddrs) { - this.#scheduleWithHub(multiaddr) - } - // @ts-ignore - const stopWatch = this.#peer.set?.watch(({subdomain, event, value}) => { - if (subdomain === 'hubs' && event === 'add') { - this.#scheduleWithHub(value) + /** + * @param {CB} cb + */ + #setupHubDiscovery(cb) { + this.#peer.set?.values('hubs', null, (err, /** @type {Array | undefined} */ multiaddrs) => { + if (err) return cb(err) + + if (!multiaddrs) return cb() + for (const multiaddr of multiaddrs) { + this.#scheduleWithHub(multiaddr) } - }); + // @ts-ignore + const stopWatch = this.#peer.set?.watch(({subdomain, event, value}) => { + if (subdomain === 'hubs' && event === 'add') { + this.#scheduleWithHub(value) + } + }); + cb() + }) } - start() { + /** + * @param {CB} cb + */ + start(cb) { if (!this.#closed) return this.#closed = false - this.#setupHubDiscovery(); + this.#setupHubDiscovery(cb); } stop() { diff --git a/lib/stats.js b/lib/stats.js index 77b2781..488310c 100644 --- a/lib/stats.js +++ b/lib/stats.js @@ -1,6 +1,6 @@ const Path = require('path') const FS = require('fs') -const debug = require('debug')('ppppp:net:stats') +const debug = require('debug')('pzp:net:stats') const atomic = require('atomic-file-rw') /** @@ -56,13 +56,13 @@ const SelfHealingJSONCodec = { if (!foundCorruption) { foundCorruption = true // prettier-ignore - console.warn(`WARNING: ppppp-net found a corrupted ${Stats.FILENAME} file and is attempting to heal it`) + console.warn(`WARNING: pzp-net found a corrupted ${Stats.FILENAME} file and is attempting to heal it`) } continue } } console.error( - `ERROR! ppppp-net failed to heal corrupted ${Stats.FILENAME} file` + `ERROR! pzp-net failed to heal corrupted ${Stats.FILENAME} file` ) return {} }, diff --git a/package.json b/package.json index 5c778a7..205065b 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { - "name": "ppppp-net", - "version": "1.0.0", - "description": "PPPPP plugin to manage connections with hubs and peers", + "name": "pzp-net", + "version": "0.0.1", + "description": "PZP plugin to manage connections with hubs and peers", "author": "Andre Staltz ", "license": "MIT", - "homepage": "https://github.com/staltz/ppppp-net", + "homepage": "https://codeberg.org/pzp/pzp-net", "repository": { "type": "git", - "url": "git@github.com:staltz/ppppp-net.git" + "url": "git@codeberg.org:pzp/pzp-net.git" }, "main": "lib/index.js", "files": [ @@ -41,8 +41,7 @@ "pull-pause": "~0.0.2", "pull-ping": "^2.0.3", "pull-stream": "^3.6.14", - "statistics": "^3.3.0", - "ziii": "~1.0.2" + "statistics": "^3.3.0" }, "devDependencies": { "@types/debug": "^4.1.12", @@ -50,10 +49,10 @@ "@types/node": "18", "bs58": "^5.0.0", "c8": "7", - "ppppp-caps": "github:staltz/ppppp-caps#93fa810b9a40b78aef4872d4c2a8412cccb52929", - "ppppp-db": "github:staltz/ppppp-db#667b33779d98aff12a9b0cd2d7c80469a95cd04e", - "ppppp-keypair": "github:staltz/ppppp-keypair#61ef4420578f450dc2cc7b1efc1c5a691a871c74", - "ppppp-set": "github:staltz/ppppp-set#8983ba29f03db95a76b4bd9a55aa4392b350fdbb", + "pzp-caps": "^1.0.0", + "pzp-db": "^1.0.1", + "pzp-keypair": "^1.0.0", + "pzp-set": "^1.0.0", "prettier": "^2.6.2", "pretty-quick": "^3.1.3", "rimraf": "^4.4.0", diff --git a/test/util.js b/test/util.js index c715c62..99c785a 100644 --- a/test/util.js +++ b/test/util.js @@ -2,8 +2,8 @@ const OS = require('node:os') const FS = require('node:fs') const Path = require('node:path') const rimraf = require('rimraf') -const caps = require('ppppp-caps') -const Keypair = require('ppppp-keypair') +const caps = require('pzp-caps') +const Keypair = require('pzp-keypair') const net = require('../lib/index') function createPeer(config) { @@ -11,7 +11,7 @@ function createPeer(config) { const name = config.name const tmp = OS.tmpdir() config.global ??= {} - config.global.path ??= Path.join(tmp, `ppppp-net-${name}-${Date.now()}`) + config.global.path ??= Path.join(tmp, `pzp-net-${name}-${Date.now()}`) config.global.keypair ??= Keypair.generate('ed25519', name) delete config.name } @@ -48,7 +48,7 @@ function createPeer(config) { } function createPeerMock() { - const testPath = FS.mkdtempSync(Path.join(OS.tmpdir(), 'ppppp-net-')) + const testPath = FS.mkdtempSync(Path.join(OS.tmpdir(), 'pzp-net-')) const mockPeer = { addListener() {},