Merge pull request 'Rename to pzp and fix async fns' (#1) from async-db into master

Reviewed-on: https://codeberg.org/pzp/pzp-promise/pulls/1
This commit is contained in:
Powersource 2024-05-07 15:50:47 +00:00
commit 0cadc46b3c
6 changed files with 88 additions and 78 deletions

View File

@ -1,27 +0,0 @@
name: CI
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
test:
strategy:
matrix:
node-version: [16.x, 18.x, 20.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
timeout-minutes: 2
steps:
- name: Checkout the repo
uses: actions/checkout@v2
- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
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,5 +1,11 @@
# ppppp-promise # pzp-promise
**Work in progress** PZP promises are tokens that authorize others to gain something
Not to be confused with JavaScript Promises. PPPPP Promises are tokens that can be redeemed by other peers, which authorize the execution of some code on the peer that issued the promise. Not to be confused with JavaScript Promises. PZP promises are tokens that can be redeemed by other peers, which authorize the execution of some code on the peer that issued the promise.
## Installation
```
npm install pzp-promise
```

View File

@ -6,9 +6,9 @@ const bs58 = require('bs58')
const b4a = require('b4a') const b4a = require('b4a')
/** /**
* @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 {import('ppppp-db/msg-v4').AccountAdd} AccountAdd * @typedef {import('pzp-db/msg-v4').AccountAdd} AccountAdd
* @typedef {Buffer | Uint8Array} B4A * @typedef {Buffer | Uint8Array} B4A
* @typedef {{global: {path: string}}} ExpectedConfig * @typedef {{global: {path: string}}} ExpectedConfig
* @typedef {{global: {path?: string}}} Config * @typedef {{global: {path?: string}}} Config
@ -33,7 +33,7 @@ function assertValidConfig(config) {
} }
/** /**
* @param {{ db: PPPPPDB; set: PPPPPSet }} peer * @param {{ db: PZPDB; set: PZPSet }} peer
* @param {Config} config * @param {Config} config
*/ */
function initPromise(peer, config) { function initPromise(peer, config) {
@ -143,23 +143,28 @@ function initPromise(peer, config) {
peer.set.load(myAccountID, (err) => { peer.set.load(myAccountID, (err) => {
// prettier-ignore // prettier-ignore
if (err) return cb(new Error(`Failed to load ppppp-set with account "${myAccountID}" when executing follow promise`, { cause: err })) if (err) return cb(new Error(`Failed to load pzp-set with account "${myAccountID}" when executing follow promise`, { cause: err }))
if (peer.set.has('follows', theirAccountID)) {
promises.delete(token) peer.set.has('follows', theirAccountID, null, (err, peerHas) => {
cb(null, false) if (err) return cb(err)
return
} else { if (peerHas) {
peer.set.add('follows', theirAccountID, (err, _) => {
// prettier-ignore
if (err) return cb(new Error(`Failed to follow account "${theirAccountID}" in ppppp-set from account "${myAccountID}" when executing follow promise`, { cause: err }))
promises.delete(token) promises.delete(token)
save((err, _) => { cb(null, false)
return
} else {
peer.set.add('follows', theirAccountID, (err, _) => {
// prettier-ignore // prettier-ignore
if (err) return cb(new Error('Failed to save promise file when executing follow promise', { cause: err })) if (err) return cb(new Error(`Failed to follow account "${theirAccountID}" in pzp-set from account "${myAccountID}" when executing follow promise`, { cause: err }))
cb(null, true) promises.delete(token)
save((err, _) => {
// prettier-ignore
if (err) return cb(new Error('Failed to save promise file when executing follow promise', { cause: err }))
cb(null, true)
})
}) })
}) }
} })
}) })
} }
@ -237,21 +242,26 @@ function initPromise(peer, config) {
curve: /**@type {const}*/ ('ed25519'), curve: /**@type {const}*/ ('ed25519'),
public: addition.key.bytes, public: addition.key.bytes,
} }
if (peer.db.account.has({ account, keypair })) { peer.db.account.has({ account, keypair }, (err, accountHas) => {
cb(null, false) if (err) return cb(err)
return
}
peer.db.account.add( if (accountHas) {
{ account, keypair, consent: addition.consent }, cb(null, false)
(err, rec) => { return
if (err) return cb(err)
promises.delete(token)
save(() => {
cb(null, true)
})
} }
)
if (!addition.consent) return cb(Error('Consent disappeared'))
peer.db.account.add(
{ account, keypair, consent: addition.consent },
(err, rec) => {
if (err) return cb(err)
promises.delete(token)
save(() => {
cb(null, true)
})
}
)
})
} }
/** /**

View File

@ -1,11 +1,11 @@
{ {
"name": "ppppp-promise", "name": "pzp-promise",
"version": "0.0.1", "version": "0.0.1",
"description": "PPPPP promises are tokens that authorize others to gain something", "description": "PZP promises are tokens that authorize others to gain something",
"homepage": "https://github.com/staltz/ppppp-promise", "homepage": "https://codeberg.org/pzp/pzp-promise",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/staltz/ppppp-promise.git" "url": "git@codeberg.org:pzp/pzp-promise.git"
}, },
"author": "Andre 'Staltz' Medeiros <contact@staltz.com>", "author": "Andre 'Staltz' Medeiros <contact@staltz.com>",
"license": "MIT", "license": "MIT",
@ -32,10 +32,10 @@
"@types/node": "^20.2.5", "@types/node": "^20.2.5",
"c8": "^7.11.0", "c8": "^7.11.0",
"husky": "^4.3.0", "husky": "^4.3.0",
"ppppp-caps": "github:staltz/ppppp-caps", "pzp-caps": "^1.0.0",
"ppppp-db": "github:staltz/ppppp-db", "pzp-db": "^1.0.1",
"ppppp-keypair": "github:staltz/ppppp-keypair", "pzp-keypair": "^1.0.0",
"ppppp-set": "github:staltz/ppppp-set", "pzp-set": "^1.0.0",
"prettier": "^2.6.2", "prettier": "^2.6.2",
"pretty-quick": "^3.1.3", "pretty-quick": "^3.1.3",
"rimraf": "^5.0.1", "rimraf": "^5.0.1",

View File

@ -5,21 +5,21 @@ const os = require('node:os')
const fs = require('node:fs') const fs = require('node:fs')
const p = require('node:util').promisify const p = require('node:util').promisify
const rimraf = require('rimraf') const rimraf = require('rimraf')
const Keypair = require('ppppp-keypair') const Keypair = require('pzp-keypair')
const caps = require('ppppp-caps') const caps = require('pzp-caps')
async function setup() { async function setup() {
setup.counter ??= 0 setup.counter ??= 0
setup.counter += 1 setup.counter += 1
const path = Path.join(os.tmpdir(), 'ppppp-promise-' + setup.counter) const path = Path.join(os.tmpdir(), 'pzp-promise-' + setup.counter)
rimraf.sync(path) rimraf.sync(path)
const keypair = Keypair.generate('ed25519', 'alice') const keypair = Keypair.generate('ed25519', 'alice')
const peer = require('secret-stack/bare')() const peer = require('secret-stack/bare')()
.use(require('secret-stack/plugins/net')) .use(require('secret-stack/plugins/net'))
.use(require('secret-handshake-ext/secret-stack')) .use(require('secret-handshake-ext/secret-stack'))
.use(require('ppppp-db')) .use(require('pzp-db'))
.use(require('ppppp-set')) .use(require('pzp-set'))
.use(require('../lib')) .use(require('../lib'))
.call(null, { .call(null, {
shse: { caps }, shse: { caps },
@ -71,12 +71,12 @@ test('follow()', async (t) => {
const contentsBefore = fs.readFileSync(file, 'utf-8') const contentsBefore = fs.readFileSync(file, 'utf-8')
assert.strictEqual(contentsBefore, JSON.stringify([[token, promise]])) assert.strictEqual(contentsBefore, JSON.stringify([[token, promise]]))
assert.equal(peer.set.has('follows', 'FRIEND_ID'), false, 'not following') assert.equal(await p(peer.set.has)('follows', 'FRIEND_ID', null), false, 'not following')
const result1 = await p(peer.promise.follow)(token, 'FRIEND_ID') const result1 = await p(peer.promise.follow)(token, 'FRIEND_ID')
assert.strictEqual(result1, true) assert.strictEqual(result1, true)
assert.equal(peer.set.has('follows', 'FRIEND_ID'), true, 'following') assert.equal(await p(peer.set.has)('follows', 'FRIEND_ID', null), true, 'following')
const contentsAfter = fs.readFileSync(file, 'utf-8') const contentsAfter = fs.readFileSync(file, 'utf-8')
assert.strictEqual(contentsAfter, '[]') assert.strictEqual(contentsAfter, '[]')
@ -102,7 +102,11 @@ test('accountAdd()', async (t) => {
const contentsBefore = fs.readFileSync(file, 'utf-8') const contentsBefore = fs.readFileSync(file, 'utf-8')
assert.strictEqual(contentsBefore, JSON.stringify([[token, promise]])) assert.strictEqual(contentsBefore, JSON.stringify([[token, promise]]))
const dbBefore = [...peer.db.msgs()].map(({ data }) => data) const msgs = []
for await (msg of peer.db.msgs()) {
msgs.push(msg)
}
const dbBefore = msgs.map(({ data }) => data)
assert.equal(dbBefore.length, 1) assert.equal(dbBefore.length, 1)
assert.equal(dbBefore[0].action, 'add') assert.equal(dbBefore[0].action, 'add')
assert.equal(dbBefore[0].key.algorithm, 'ed25519') assert.equal(dbBefore[0].key.algorithm, 'ed25519')
@ -122,7 +126,11 @@ test('accountAdd()', async (t) => {
}) })
assert.strictEqual(result1, true) assert.strictEqual(result1, true)
const dbAfter = [...peer.db.msgs()].map(({ data }) => data) const msgs2 = []
for await (msg of peer.db.msgs()) {
msgs2.push(msg)
}
const dbAfter = msgs2.map(({ data }) => data)
assert.equal(dbAfter.length, 2) assert.equal(dbAfter.length, 2)
assert.equal(dbAfter[0].action, 'add') assert.equal(dbAfter[0].action, 'add')
assert.equal(dbAfter[0].key.algorithm, 'ed25519') assert.equal(dbAfter[0].key.algorithm, 'ed25519')