diff --git a/declarations/pzp-caps.d.ts b/declarations/pzp-caps.d.ts new file mode 100644 index 0000000..546d373 --- /dev/null +++ b/declarations/pzp-caps.d.ts @@ -0,0 +1,4 @@ +declare module 'pzp-caps' { + declare const caps: string + export = caps +} \ No newline at end of file diff --git a/declarations/secret-stack.d.ts b/declarations/secret-stack.d.ts new file mode 100644 index 0000000..2f1c0a9 --- /dev/null +++ b/declarations/secret-stack.d.ts @@ -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' { +} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..a8870ca --- /dev/null +++ b/lib/index.js @@ -0,0 +1,106 @@ +const { join } = require('node:path') +const os = require('node:os') +const Keypair = require('pzp-keypair') + +/** + * @typedef {ReturnType} PZPDB + * @typedef {ReturnType} PZPDict + * @typedef {ReturnType} PZPSet + * @typedef {ReturnType} PZPGoals + * @typedef {ReturnType} PZPNet + * @typedef {ReturnType} PZPSync + * @typedef {ReturnType} PZPGc + * @typedef {ReturnType} PZPConductor + * @typedef {ReturnType} PZPHubClient + * @typedef {ReturnType} PZPPromise + * @typedef {ReturnType} 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} + */ +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, +} diff --git a/package.json b/package.json index 4052d1e..9b931e7 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,8 @@ }, "dependencies": { "pzp-caps": "^1.0.0", - "pzp-conductor": "^1.0.0", - "pzp-db": "^1.0.1", + "pzp-conductor": "^1.0.2", + "pzp-db": "^1.0.2", "pzp-dict": "^1.0.0", "pzp-gc": "^1.0.0", "pzp-goals": "^1.0.0", diff --git a/tsconfig.json b/tsconfig.json index 02ae111..81044b4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "include": [ + "declarations", "lib/**/*.js" ], "exclude": [