mirror of https://codeberg.org/pzp/pzp-promise.git
init
This commit is contained in:
commit
99a1570c5a
|
@ -0,0 +1,27 @@
|
|||
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
|
|
@ -0,0 +1,4 @@
|
|||
node_modules
|
||||
.nyc_output
|
||||
coverage
|
||||
pnpm-lock.yaml
|
|
@ -0,0 +1,2 @@
|
|||
semi: false
|
||||
singleQuote: true
|
|
@ -0,0 +1,20 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2023 Andre 'Staltz' Medeiros
|
||||
|
||||
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.
|
|
@ -0,0 +1,5 @@
|
|||
# ppppp-promise
|
||||
|
||||
**Work in progress**
|
||||
|
||||
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.
|
|
@ -0,0 +1,160 @@
|
|||
// @ts-ignore
|
||||
const AtomicFileRW = require('atomic-file-rw')
|
||||
const Path = require('node:path')
|
||||
const crypto = require('node:crypto')
|
||||
const bs58 = require('bs58')
|
||||
const b4a = require('b4a')
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {(...args: [NodeJS.ErrnoException] | [null, T]) => void} CB<T>
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Buffer | Uint8Array} B4A
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{type: 'follow'}} FollowPromise
|
||||
* @typedef {FollowPromise} PPromise
|
||||
*/
|
||||
|
||||
const FILENAME = 'promises.json'
|
||||
|
||||
module.exports = {
|
||||
name: 'promise',
|
||||
manifest: {
|
||||
// management
|
||||
create: 'async',
|
||||
revoke: 'async',
|
||||
// promises
|
||||
follow: 'async',
|
||||
},
|
||||
permissions: {
|
||||
anonymous: {
|
||||
allow: ['follow'],
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {any} sstack
|
||||
* @param {any} config
|
||||
*/
|
||||
init(sstack, config) {
|
||||
const devicePromisesFile = Path.join(config.path, FILENAME)
|
||||
|
||||
const promises = /** @type {Map<string, PPromise>} */ (new Map())
|
||||
let loaded = false
|
||||
|
||||
// Initial load
|
||||
AtomicFileRW.readFile(
|
||||
devicePromisesFile,
|
||||
/** @type {CB<B4A | string>} */ function onLoad(err, buf) {
|
||||
if (err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
save((err, _) => {
|
||||
// prettier-ignore
|
||||
if (err) return console.log('Problem creating promises file:', err)
|
||||
else loaded = true
|
||||
})
|
||||
return
|
||||
}
|
||||
console.log('Problem loading promises file:', err)
|
||||
return
|
||||
}
|
||||
const json = typeof buf === 'string' ? buf : b4a.toString(buf, 'utf-8')
|
||||
const arr = JSON.parse(json)
|
||||
for (const [token, promise] of arr) {
|
||||
promises.set(token, promise)
|
||||
}
|
||||
loaded = true
|
||||
}
|
||||
)
|
||||
|
||||
/**
|
||||
* @param {PPromise} promise
|
||||
* @return {Error | null}
|
||||
*/
|
||||
function validatePromise(promise) {
|
||||
if (
|
||||
typeof promise !== 'object' ||
|
||||
typeof promise.type !== 'string' ||
|
||||
promise.type !== 'follow'
|
||||
) {
|
||||
return Error('Invalid promise created: ' + JSON.stringify(promise))
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CB<any>} cb
|
||||
*/
|
||||
function save(cb) {
|
||||
const json = JSON.stringify([...promises])
|
||||
AtomicFileRW.writeFile(devicePromisesFile, json, cb)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {PPromise} promise
|
||||
* @param {CB<string>} cb
|
||||
*/
|
||||
function create(promise, cb) {
|
||||
if (!loaded) {
|
||||
setTimeout(() => create(promise, cb), 100)
|
||||
return
|
||||
}
|
||||
let err
|
||||
if ((err = validatePromise(promise))) return cb(err)
|
||||
|
||||
const token = bs58.encode(crypto.randomBytes(32))
|
||||
promises.set(token, promise)
|
||||
save((err, _) => {
|
||||
if (err) return cb(err)
|
||||
cb(null, token)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} token
|
||||
* @param {string} id
|
||||
* @param {CB<boolean>} cb
|
||||
*/
|
||||
function follow(token, id, cb) {
|
||||
if (!loaded) {
|
||||
setTimeout(() => follow(token, id, cb), 100)
|
||||
return
|
||||
}
|
||||
|
||||
if (!promises.has(token)) {
|
||||
cb(new Error('Invalid token'))
|
||||
return
|
||||
}
|
||||
const promise = /** @type {PPromise} */ (promises.get(token))
|
||||
if (promise.type !== 'follow') {
|
||||
cb(new Error('Invalid token'))
|
||||
return
|
||||
}
|
||||
console.log('ppppp-promise mock follow') // FIXME: implement follow
|
||||
promises.delete(token)
|
||||
save(() => {
|
||||
cb(null, true)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} token
|
||||
* @param {CB<any>} cb
|
||||
*/
|
||||
function revoke(token, cb) {
|
||||
if (!loaded) {
|
||||
setTimeout(() => revoke(token, cb), 100)
|
||||
return
|
||||
}
|
||||
|
||||
promises.delete(token)
|
||||
save(cb)
|
||||
}
|
||||
|
||||
return { create, revoke, follow }
|
||||
},
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
{
|
||||
"name": "ppppp-promise",
|
||||
"version": "0.0.1",
|
||||
"description": "PPPPP promises are tokens that authorize others to gain something",
|
||||
"homepage": "https://github.com/staltz/ppppp-promise",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/staltz/ppppp-promise.git"
|
||||
},
|
||||
"author": "Andre 'Staltz' Medeiros <contact@staltz.com>",
|
||||
"license": "MIT",
|
||||
"type": "commonjs",
|
||||
"main": "lib/index.js",
|
||||
"files": [
|
||||
"lib/**/*"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./lib/index.js"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"atomic-file-rw": "~0.3.0",
|
||||
"b4a": "^1.6.4",
|
||||
"bs58": "~5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/b4a": "^1.6.0",
|
||||
"@types/node": "^20.2.5",
|
||||
"c8": "^7.11.0",
|
||||
"husky": "^4.3.0",
|
||||
"ppppp-caps": "github:staltz/ppppp-caps",
|
||||
"ppppp-keypair": "github:staltz/ppppp-keypair",
|
||||
"prettier": "^2.6.2",
|
||||
"pretty-quick": "^3.1.3",
|
||||
"rimraf": "^5.0.1",
|
||||
"secret-handshake-ext": "~0.0.6",
|
||||
"secret-stack": "^6.4.2",
|
||||
"typescript": "^5.0.2"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "node --test",
|
||||
"format-code": "prettier --write \"*.js\" \"(test|lib)/*.js\"",
|
||||
"format-code-staged": "pretty-quick --staged --pattern \"*.js\" --pattern \"(test|lib)/*.js\"",
|
||||
"coverage": "c8 --reporter=lcov npm run test"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "pretty-quick --staged"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const Path = require('node:path')
|
||||
const os = require('node:os')
|
||||
const fs = require('node:fs')
|
||||
const p = require('node:util').promisify
|
||||
const rimraf = require('rimraf')
|
||||
const Keypair = require('ppppp-keypair')
|
||||
const caps = require('ppppp-caps')
|
||||
|
||||
function setup() {
|
||||
setup.counter ??= 0
|
||||
setup.counter += 1
|
||||
const path = Path.join(os.tmpdir(), 'ppppp-promise-' + setup.counter)
|
||||
rimraf.sync(path)
|
||||
const keypair = Keypair.generate('ed25519', 'alice')
|
||||
|
||||
const stack = require('secret-stack/lib/api')([], {})
|
||||
.use(require('secret-stack/lib/core'))
|
||||
.use(require('secret-stack/lib/plugins/net'))
|
||||
.use(require('secret-handshake-ext/secret-stack'))
|
||||
.use(require('../lib'))
|
||||
.call(null, {
|
||||
path,
|
||||
caps,
|
||||
keypair,
|
||||
})
|
||||
|
||||
return { stack, path, keypair }
|
||||
}
|
||||
|
||||
test('create()', async (t) => {
|
||||
const { stack, path } = setup()
|
||||
|
||||
const promise = { type: 'follow' }
|
||||
const token = await p(stack.promise.create)(promise)
|
||||
assert.strictEqual(typeof token, 'string')
|
||||
assert.ok(token.length > 42)
|
||||
|
||||
const file = Path.join(path, 'promises.json')
|
||||
assert.ok(fs.existsSync(file), 'file exists')
|
||||
const contents = fs.readFileSync(file, 'utf-8')
|
||||
assert.strictEqual(contents, JSON.stringify([[token, promise]]))
|
||||
|
||||
await p(stack.close)()
|
||||
})
|
||||
|
||||
test('follow()', async (t) => {
|
||||
const { stack, path } = setup()
|
||||
|
||||
assert.rejects(() => p(stack.promise.follow)('randomnottoken', 'MY_ID'))
|
||||
|
||||
const promise = { type: 'follow' }
|
||||
const token = await p(stack.promise.create)(promise)
|
||||
|
||||
const file = Path.join(path, 'promises.json')
|
||||
const contentsBefore = fs.readFileSync(file, 'utf-8')
|
||||
assert.strictEqual(contentsBefore, JSON.stringify([[token, promise]]))
|
||||
|
||||
const result1 = await p(stack.promise.follow)(token, 'MY_ID')
|
||||
assert.strictEqual(result1, true)
|
||||
|
||||
const contentsAfter = fs.readFileSync(file, 'utf-8')
|
||||
assert.strictEqual(contentsAfter, '[]')
|
||||
|
||||
assert.rejects(() => p(stack.promise.follow)(token, 'MY_ID'));
|
||||
|
||||
await p(stack.close)()
|
||||
})
|
||||
|
||||
test('revoke()', async (t) => {
|
||||
const { stack, path } = setup()
|
||||
|
||||
const promise = { type: 'follow' }
|
||||
const token = await p(stack.promise.create)(promise)
|
||||
|
||||
const file = Path.join(path, 'promises.json')
|
||||
const contentsBefore = fs.readFileSync(file, 'utf-8')
|
||||
assert.strictEqual(contentsBefore, JSON.stringify([[token, promise]]))
|
||||
|
||||
await p(stack.promise.revoke)(token)
|
||||
|
||||
const contentsAfter = fs.readFileSync(file, 'utf-8')
|
||||
assert.strictEqual(contentsAfter, '[]')
|
||||
|
||||
await p(stack.close)()
|
||||
})
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"include": ["lib/**/*.js"],
|
||||
"exclude": ["coverage/", "node_modules/", "test/"],
|
||||
"compilerOptions": {
|
||||
"checkJs": true,
|
||||
"noEmit": true,
|
||||
"exactOptionalPropertyTypes": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"lib": ["es2021", "dom"],
|
||||
"module": "node16",
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"target": "es2021"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue