@dotrino/identity 0.136.1 → 0.136.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/node.js CHANGED
@@ -21,14 +21,24 @@ import os from 'node:os'
21
21
  import { createIdentityCore } from '../vault/core.js'
22
22
  import { IdentityFacade } from '../vault/facade.js'
23
23
 
24
+ /**
25
+ * Cifrado en reposo del archivo de la identidad (lo pone quien hospeda, p. ej. la bóveda).
26
+ * @typedef {{ encrypt (text: string): string, decrypt (blob: string): string }} AtRest
27
+ */
28
+
24
29
  /**
25
30
  * kv síncrono respaldado por un archivo JSON (estilo localStorage).
26
31
  *
27
32
  * `atRest` (opcional) cifra el archivo entero en disco: quien lo provee decide con qué
28
33
  * (el vault lo liga a la máquina, ver `dotrino-vault/src/atrest.js`). Si el archivo estaba
29
34
  * en claro se lee igual y queda cifrado en la primera escritura, sin pedir nada.
35
+ *
36
+ * @param {string} filePath
37
+ * @param {AtRest | null} [atRest]
38
+ * @returns {import('../vault/core.js').Kv}
30
39
  */
31
40
  function fileKv (filePath, atRest = null) {
41
+ /** @type {Record<string, string>} */
32
42
  let data = {}
33
43
  try {
34
44
  if (fs.existsSync(filePath)) {
@@ -48,10 +58,17 @@ function fileKv (filePath, atRest = null) {
48
58
  }
49
59
  }
50
60
 
51
- /** Peer book respaldado por un archivo JSON (interfaz de vault/peerStore.js). */
61
+ /**
62
+ * Peer book respaldado por un archivo JSON (interfaz de vault/peerStore.js).
63
+ * @param {string} filePath
64
+ * @returns {import('../vault/core.js').CorePeers}
65
+ */
52
66
  function filePeers (filePath) {
67
+ /** @type {import('../vault/peerStore.js').PeerBook} */
53
68
  let peers = {}
69
+ /** @type {(() => void) | null} */
54
70
  let markDirty = null
71
+ /** @type {string | null} */
55
72
  let pid = null // multi-perfil: el peer book se namespacea por perfil
56
73
  const fileFor = () => pid ? path.join(path.dirname(filePath), `peers.${pid}.json`) : filePath
57
74
  const flush = () => {
@@ -60,7 +77,9 @@ function filePeers (filePath) {
60
77
  fs.writeFileSync(f, JSON.stringify(peers))
61
78
  }
62
79
  return {
80
+ /** @param {string | null} p */
63
81
  setProfile (p) { pid = p || null },
82
+ /** @param {string} newPid */
64
83
  adoptLegacy (newPid) {
65
84
  // Migración: copia el peers.json viejo (sin namespace) al perfil newPid.
66
85
  try {
@@ -79,16 +98,16 @@ function filePeers (filePath) {
79
98
  return peers
80
99
  },
81
100
  loadPeers: () => peers,
82
- savePeers: (next) => { peers = next; flush(); if (markDirty) markDirty() },
83
- setPeersDirect: (next) => { peers = next; flush() },
84
- upsertPeer: (publickey, patch) => {
101
+ savePeers: (/** @type {import('../vault/peerStore.js').PeerBook} */ next) => { peers = next; flush(); if (markDirty) markDirty() },
102
+ setPeersDirect: (/** @type {import('../vault/peerStore.js').PeerBook} */ next) => { peers = next; flush() },
103
+ upsertPeer: (/** @type {string} */ publickey, /** @type {Record<string, any>} */ patch) => {
85
104
  const existing = peers[publickey] || { publickey, firstSeen: Date.now() }
86
105
  peers[publickey] = { ...existing, ...patch, publickey, lastSeen: Date.now() }
87
106
  flush()
88
107
  if (markDirty) markDirty()
89
108
  return peers[publickey]
90
109
  },
91
- onDirty: (fn) => { markDirty = fn }
110
+ onDirty: (/** @type {(() => void) | null} */ fn) => { markDirty = fn }
92
111
  }
93
112
  }
94
113
 
@@ -99,6 +118,8 @@ export class Identity extends IdentityFacade {
99
118
  * @param {Object} [options]
100
119
  * @param {string} [options.dir] Directorio de persistencia de esta identidad.
101
120
  * Cada directorio = un usuario distinto. Default: ~/.dotrino/identity
121
+ * @param {AtRest | null} [options.atRest] cifrado en reposo del archivo de la identidad
122
+ * @param {import('../vault/core.js').KeyLock | null} [options.keyLock] candado de la maestra
102
123
  */
103
124
  constructor (options = {}) {
104
125
  super(null)
@@ -114,6 +135,9 @@ export class Identity extends IdentityFacade {
114
135
  * Crea/abre una identidad headless. A diferencia del navegador NO es singleton:
115
136
  * cada `dir` distinto devuelve una identidad independiente (para correr muchos
116
137
  * bots-usuario en el mismo proceso).
138
+ *
139
+ * @param {{ dir?: string, atRest?: AtRest | null, keyLock?: import('../vault/core.js').KeyLock | null }} [options]
140
+ * @returns {Promise<Identity>}
117
141
  */
118
142
  static async connect (options = {}) {
119
143
  const inst = new Identity(options)