Rename to pzp and asycify

This commit is contained in:
Jacob Karlsson 2024-05-05 21:44:01 +02:00
parent 5b79b9566b
commit b1de4f6250
9 changed files with 74 additions and 70 deletions

View File

@ -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

13
.woodpecker.yaml Normal file
View File

@ -0,0 +1,13 @@
matrix:
NODE_VERSION:
- 18
- 20
steps:
test:
when:
event: [push]
image: node:${NODE_VERSION}
commands:
- npm install
- npm test

View File

@ -1,14 +1,14 @@
**Work in progress** # pzp-net
PZP plugin to manage connections with hubs and peers
## Installation ## Installation
We're not on npm yet. In your package.json, include this as ```
npm install pzp-net
```js
"ppppp-net": "github:staltz/ppppp-net"
``` ```
**TODO:** ## **TODO:**
- [x] connect - [x] connect
- [x] stage - [x] stage

View File

@ -1,4 +1,4 @@
const debug = require('debug')('ppppp:net:connections') const debug = require('debug')('pzp:net:connections')
const createNotify = require('pull-notify') const createNotify = require('pull-notify')
const run = require('promisify-tuple') const run = require('promisify-tuple')
const IP = require('ip') const IP = require('ip')

View File

@ -91,7 +91,7 @@ function initNet(peer, config) {
async function start() { async function start() {
await stats.loaded() await stats.loaded()
glue(infos, connections) glue(infos, connections)
queueMicrotask(scheduler.start.bind(scheduler)) queueMicrotask(() => scheduler.start.bind(scheduler))
} }
function stop() { function stop() {

View File

@ -1,22 +1,30 @@
const awaitable = require('pull-awaitable') const awaitable = require('pull-awaitable')
const run = require('promisify-tuple') const run = require('promisify-tuple')
const debug = require('debug')('ppppp:net:scheduler') const debug = require('debug')('pzp:net:scheduler')
/** /**
* @typedef {ReturnType<import('ppppp-db').init>} PPPPPDB * @typedef {ReturnType<import('pzp-db').init>} PZPDB
* @typedef {ReturnType<import('ppppp-set').init>} PPPPPSet * @typedef {ReturnType<import('pzp-set').init>} PZPSet
* @typedef {`/${string}`} Multiaddr * @typedef {`/${string}`} Multiaddr
* @typedef {import('./infos')} Infos * @typedef {import('./infos')} Infos
* @typedef {import('./connections')} Connections * @typedef {import('./connections')} Connections
* @typedef {{ * @typedef {{
* db?: PPPPPDB; * db?: PZPDB;
* set?: PPPPPSet; * set?: PZPSet;
* shse: { * shse: {
* pubkey: string; * pubkey: string;
* } * }
* }} Peer * }} Peer
*/ */
/**
* @template T
* @typedef {[T] extends [void] ?
* (...args: [Error] | []) => void :
* (...args: [Error] | [null, T]) => void
* } CB
*/
class Scheduler { class Scheduler {
/** @type {Peer} */ /** @type {Peer} */
#peer #peer
@ -62,10 +70,14 @@ class Scheduler {
} }
} }
#setupHubDiscovery() { /**
/** @type {Array<Multiaddr> | undefined} */ * @param {CB<void>} cb
const multiaddrs = this.#peer.set?.values('hubs') */
if (!multiaddrs) return #setupHubDiscovery(cb) {
this.#peer.set?.values('hubs', null, (err, /** @type {Array<Multiaddr> | undefined} */ multiaddrs) => {
if (err) return cb(err)
if (!multiaddrs) return cb()
for (const multiaddr of multiaddrs) { for (const multiaddr of multiaddrs) {
this.#scheduleWithHub(multiaddr) this.#scheduleWithHub(multiaddr)
} }
@ -75,12 +87,17 @@ class Scheduler {
this.#scheduleWithHub(value) this.#scheduleWithHub(value)
} }
}); });
cb()
})
} }
start() { /**
* @param {CB<void>} cb
*/
start(cb) {
if (!this.#closed) return if (!this.#closed) return
this.#closed = false this.#closed = false
this.#setupHubDiscovery(); this.#setupHubDiscovery(cb);
} }
stop() { stop() {

View File

@ -1,6 +1,6 @@
const Path = require('path') const Path = require('path')
const FS = require('fs') const FS = require('fs')
const debug = require('debug')('ppppp:net:stats') const debug = require('debug')('pzp:net:stats')
const atomic = require('atomic-file-rw') const atomic = require('atomic-file-rw')
/** /**
@ -56,13 +56,13 @@ const SelfHealingJSONCodec = {
if (!foundCorruption) { if (!foundCorruption) {
foundCorruption = true foundCorruption = true
// prettier-ignore // 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 continue
} }
} }
console.error( console.error(
`ERROR! ppppp-net failed to heal corrupted ${Stats.FILENAME} file` `ERROR! pzp-net failed to heal corrupted ${Stats.FILENAME} file`
) )
return {} return {}
}, },

View File

@ -1,13 +1,13 @@
{ {
"name": "ppppp-net", "name": "pzp-net",
"version": "1.0.0", "version": "0.0.1",
"description": "PPPPP plugin to manage connections with hubs and peers", "description": "PZP plugin to manage connections with hubs and peers",
"author": "Andre Staltz <contact@staltz.com>", "author": "Andre Staltz <contact@staltz.com>",
"license": "MIT", "license": "MIT",
"homepage": "https://github.com/staltz/ppppp-net", "homepage": "https://codeberg.org/pzp/pzp-net",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git@github.com:staltz/ppppp-net.git" "url": "git@codeberg.org:pzp/pzp-net.git"
}, },
"main": "lib/index.js", "main": "lib/index.js",
"files": [ "files": [
@ -41,8 +41,7 @@
"pull-pause": "~0.0.2", "pull-pause": "~0.0.2",
"pull-ping": "^2.0.3", "pull-ping": "^2.0.3",
"pull-stream": "^3.6.14", "pull-stream": "^3.6.14",
"statistics": "^3.3.0", "statistics": "^3.3.0"
"ziii": "~1.0.2"
}, },
"devDependencies": { "devDependencies": {
"@types/debug": "^4.1.12", "@types/debug": "^4.1.12",
@ -50,10 +49,10 @@
"@types/node": "18", "@types/node": "18",
"bs58": "^5.0.0", "bs58": "^5.0.0",
"c8": "7", "c8": "7",
"ppppp-caps": "github:staltz/ppppp-caps#93fa810b9a40b78aef4872d4c2a8412cccb52929", "pzp-caps": "^1.0.0",
"ppppp-db": "github:staltz/ppppp-db#667b33779d98aff12a9b0cd2d7c80469a95cd04e", "pzp-db": "^1.0.1",
"ppppp-keypair": "github:staltz/ppppp-keypair#61ef4420578f450dc2cc7b1efc1c5a691a871c74", "pzp-keypair": "^1.0.0",
"ppppp-set": "github:staltz/ppppp-set#8983ba29f03db95a76b4bd9a55aa4392b350fdbb", "pzp-set": "^1.0.0",
"prettier": "^2.6.2", "prettier": "^2.6.2",
"pretty-quick": "^3.1.3", "pretty-quick": "^3.1.3",
"rimraf": "^4.4.0", "rimraf": "^4.4.0",

View File

@ -2,8 +2,8 @@ const OS = require('node:os')
const FS = require('node:fs') const FS = require('node:fs')
const Path = require('node:path') const Path = require('node:path')
const rimraf = require('rimraf') const rimraf = require('rimraf')
const caps = require('ppppp-caps') const caps = require('pzp-caps')
const Keypair = require('ppppp-keypair') const Keypair = require('pzp-keypair')
const net = require('../lib/index') const net = require('../lib/index')
function createPeer(config) { function createPeer(config) {
@ -11,7 +11,7 @@ function createPeer(config) {
const name = config.name const name = config.name
const tmp = OS.tmpdir() const tmp = OS.tmpdir()
config.global ??= {} 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) config.global.keypair ??= Keypair.generate('ed25519', name)
delete config.name delete config.name
} }
@ -48,7 +48,7 @@ function createPeer(config) {
} }
function createPeerMock() { function createPeerMock() {
const testPath = FS.mkdtempSync(Path.join(OS.tmpdir(), 'ppppp-net-')) const testPath = FS.mkdtempSync(Path.join(OS.tmpdir(), 'pzp-net-'))
const mockPeer = { const mockPeer = {
addListener() {}, addListener() {},