mirror of https://codeberg.org/pzp/pzp-sdk.git
Add createPeer
This commit is contained in:
parent
49918890bf
commit
0f77ff66d6
|
@ -0,0 +1,4 @@
|
||||||
|
declare module 'pzp-caps' {
|
||||||
|
declare const caps: string
|
||||||
|
export = caps
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
declare module 'secret-stack/bare' {
|
||||||
|
interface Stack {
|
||||||
|
use(module: any): Stack
|
||||||
|
call(a: null, config: unknown)
|
||||||
|
}
|
||||||
|
|
||||||
|
function SecretStack(config?: unknown): Stack
|
||||||
|
export = SecretStack
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module 'secret-stack/plugins/net' {
|
||||||
|
}
|
|
@ -0,0 +1,106 @@
|
||||||
|
const { join } = require('node:path')
|
||||||
|
const os = require('node:os')
|
||||||
|
const Keypair = require('pzp-keypair')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {ReturnType<import('pzp-db').init>} PZPDB
|
||||||
|
* @typedef {ReturnType<import('pzp-dict').init>} PZPDict
|
||||||
|
* @typedef {ReturnType<import('pzp-set').init>} PZPSet
|
||||||
|
* @typedef {ReturnType<import('pzp-goals').init>} PZPGoals
|
||||||
|
* @typedef {ReturnType<import('pzp-net').init>} PZPNet
|
||||||
|
* @typedef {ReturnType<import('pzp-sync').init>} PZPSync
|
||||||
|
* @typedef {ReturnType<import('pzp-gc').init>} PZPGc
|
||||||
|
* @typedef {ReturnType<import('pzp-conductor').init>} PZPConductor
|
||||||
|
* @typedef {ReturnType<import('pzp-hub-client/plugin').init>} PZPHubClient
|
||||||
|
* @typedef {ReturnType<import('pzp-promise').init>} PZPPromise
|
||||||
|
* @typedef {ReturnType<import('pzp-invite').init>} PZPInvite
|
||||||
|
* @typedef {import('node:events').EventEmitter} Emitter
|
||||||
|
* @typedef {{ pubkey: string }} SHSE
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Emitter & {
|
||||||
|
* db: PZPDB,
|
||||||
|
* dict: PZPDict,
|
||||||
|
* set: PZPSet,
|
||||||
|
* goals: PZPGoals,
|
||||||
|
* net: PZPNet,
|
||||||
|
* sync: PZPSync,
|
||||||
|
* gc: PZPGc,
|
||||||
|
* conductor: PZPConductor,
|
||||||
|
* hubClient: PZPHubClient,
|
||||||
|
* promise: PZPPromise,
|
||||||
|
* invite: PZPInvite,
|
||||||
|
* shse: SHSE
|
||||||
|
* }} Peer
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {{ path: string }} opts
|
||||||
|
* @returns {Promise<Peer>}
|
||||||
|
*/
|
||||||
|
async function createPeer({ path }) {
|
||||||
|
|
||||||
|
if (!path) {
|
||||||
|
if (process.env.PZP_DIR) {
|
||||||
|
path = process.env.PZP_DIR
|
||||||
|
} else {
|
||||||
|
path = join(os.tmpdir(), `pzp-${Math.random()}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const keypairPath = join(path, 'keypair.json')
|
||||||
|
const keypair = Keypair.loadOrCreateSync(keypairPath)
|
||||||
|
|
||||||
|
/** @type {Peer} */
|
||||||
|
const peer = require('secret-stack/bare')()
|
||||||
|
.use(require('secret-stack/plugins/net'))
|
||||||
|
.use(require('secret-handshake-ext/secret-stack'))
|
||||||
|
.use(require('pzp-net'))
|
||||||
|
.use(require('pzp-db'))
|
||||||
|
.use(require('pzp-set'))
|
||||||
|
.use(require('pzp-dict'))
|
||||||
|
.use(require('pzp-goals'))
|
||||||
|
.use(require('pzp-sync'))
|
||||||
|
.use(require('pzp-gc'))
|
||||||
|
.use(require('pzp-conductor'))
|
||||||
|
.use(require('pzp-hub-client'))
|
||||||
|
.use(require('pzp-promise'))
|
||||||
|
.use(require('pzp-invite'))
|
||||||
|
.call(null, {
|
||||||
|
shse: {
|
||||||
|
caps: require('pzp-caps'),
|
||||||
|
},
|
||||||
|
global: {
|
||||||
|
keypair,
|
||||||
|
path,
|
||||||
|
timers: {
|
||||||
|
inactivity: 10 * 60e3,
|
||||||
|
},
|
||||||
|
connections: {
|
||||||
|
incoming: {
|
||||||
|
tunnel: [{ transform: 'shse', scope: 'public' }],
|
||||||
|
},
|
||||||
|
outgoing: {
|
||||||
|
net: [{ transform: 'shse' }],
|
||||||
|
tunnel: [{ transform: 'shse' }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
net: {
|
||||||
|
autostart: false,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await peer.db.loaded()
|
||||||
|
|
||||||
|
await peer.net.start()
|
||||||
|
|
||||||
|
return peer
|
||||||
|
}
|
||||||
|
|
||||||
|
exports = {
|
||||||
|
createPeer,
|
||||||
|
}
|
|
@ -24,8 +24,8 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"pzp-caps": "^1.0.0",
|
"pzp-caps": "^1.0.0",
|
||||||
"pzp-conductor": "^1.0.0",
|
"pzp-conductor": "^1.0.2",
|
||||||
"pzp-db": "^1.0.1",
|
"pzp-db": "^1.0.2",
|
||||||
"pzp-dict": "^1.0.0",
|
"pzp-dict": "^1.0.0",
|
||||||
"pzp-gc": "^1.0.0",
|
"pzp-gc": "^1.0.0",
|
||||||
"pzp-goals": "^1.0.0",
|
"pzp-goals": "^1.0.0",
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
{
|
{
|
||||||
"include": [
|
"include": [
|
||||||
|
"declarations",
|
||||||
"lib/**/*.js"
|
"lib/**/*.js"
|
||||||
],
|
],
|
||||||
"exclude": [
|
"exclude": [
|
||||||
|
|
Loading…
Reference in New Issue