@dotrino/terminal-agent 0.53.0 → 0.54.1

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/update.js CHANGED
@@ -20,11 +20,13 @@ import { loadLink } from './link.js'
20
20
  export const PKG = '@dotrino/terminal-agent'
21
21
  export const REPO = 'imdotrino/dotrino-terminal'
22
22
  /** Este repo también lleva la PWA y el escritorio: las releases del agente van con prefijo. */
23
+ /** @param {string} version */
23
24
  export const releaseTag = (version) => `agent-v${version}`
24
25
 
25
26
  /** Cada cuánto se mira si ya no quedan consolas para reiniciar con la versión instalada. */
26
27
  export const IDLE_CHECK_MS = 60_000
27
28
 
29
+ /** @type {(code: string, message: string) => Error & { code: string }} */
28
30
  const fail = (code, message) => Object.assign(new Error(message), { code })
29
31
 
30
32
  /**
@@ -33,8 +35,8 @@ const fail = (code, message) => Object.assign(new Error(message), { code })
33
35
  * del disco. El papel se renueva mientras el agente corre, y el agente puede arrancar sin
34
36
  * enlace y enlazarse después: una conexión fijada al arrancar se quedaría vieja.
35
37
  *
36
- * @param {{ dir: string, product?: string, log?: (m: string) => void, load?: (dir: string) => any, make?: (o: any) => any }} o
37
- * @returns {{ mayUpdate: Function, onUpdated: Function, onNeedsRoot: Function }}
38
+ * @param {{ dir: string, product?: string, log?: (m: string) => void, load?: (dir: string) => DotrinoDeviceLink | null, make?: typeof vaultUpdateHooks }} o
39
+ * @returns {import('@dotrino/vault/service').UpdateHooks}
38
40
  */
39
41
  export function vaultHooks ({ dir, product = PKG, log = console.log, load = loadLink, make = vaultUpdateHooks }) {
40
42
  const now = () => {
@@ -62,12 +64,14 @@ export function vaultHooks ({ dir, product = PKG, log = console.log, load = load
62
64
  * `dir` es la carpeta de datos de ESTA instancia (su enlace y sus ajustes) y `version` la
63
65
  * que está en marcha. Lo demás es para las pruebas.
64
66
  *
65
- * @param {{ dir: string, version: string, agent: { consoles: { consoles: Map<string, unknown> }, close: () => void }, exit?: (code: number) => void, log?: (m: string) => void, watch?: (o: any) => (() => void), hooks?: object, idleCheckMs?: number }} o
67
+ * @param {{ dir: string, version: string, agent: { consoles: { consoles: Map<string, unknown> }, close: () => void }, exit?: (code: number) => void, log?: (m: string) => void, watch?: typeof watchSelfUpdateNpm, hooks?: import('@dotrino/vault/service').UpdateHooks, idleCheckMs?: number }} o
66
68
  * @returns {() => void} para dejar de mirar
67
69
  */
68
70
  export function startSelfUpdate ({ dir, version, agent, exit = (code) => process.exit(code), log = console.log, watch = watchSelfUpdateNpm, hooks = vaultHooks({ dir, log }), idleCheckMs = IDLE_CHECK_MS }) {
71
+ /** @type {ReturnType<typeof setInterval> | null} */
69
72
  let waiting = null
70
73
  const open = () => agent.consoles.consoles.size
74
+ /** @param {string} v */
71
75
  const restart = (v) => {
72
76
  log(`[terminal-agent] restarting to run ${v}`)
73
77
  agent.close()
@@ -86,8 +90,8 @@ export function startSelfUpdate ({ dir, version, agent, exit = (code) => process
86
90
  if (!open()) return restart(v)
87
91
  if (waiting) return
88
92
  log(`[terminal-agent] ${v} is installed · ${open()} console(s) open, so it restarts when none is left`)
89
- waiting = setInterval(() => { if (!open()) { clearInterval(waiting); waiting = null; restart(v) } }, idleCheckMs)
90
- waiting.unref?.()
93
+ const timer = waiting = setInterval(() => { if (!open()) { clearInterval(timer); waiting = null; restart(v) } }, idleCheckMs)
94
+ timer.unref?.()
91
95
  },
92
96
  log
93
97
  })
package/uploads.js CHANGED
@@ -24,9 +24,14 @@ export const UPLOAD_CHUNK_MAX = 256 * 1024
24
24
  /** Lo que se guarda una imagen: después se borra sola. */
25
25
  export const UPLOAD_KEEP_MS = 7 * 24 * 60 * 60 * 1000
26
26
 
27
+ /** @type {(code: string, message: string) => Error & { code: string }} */
27
28
  const fail = (code, message) => Object.assign(new Error(message), { code })
28
29
 
29
30
  /** La extensión de una imagen por sus primeros bytes, o null si no es una de las admitidas. */
31
+ /**
32
+ * @param {Buffer} buf
33
+ * @returns {'png' | 'jpg' | 'gif' | 'webp' | null}
34
+ */
30
35
  export function imageExt (buf) {
31
36
  if (buf.length >= 8 && buf.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return 'png'
32
37
  if (buf.length >= 3 && buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) return 'jpg'
@@ -36,19 +41,25 @@ export function imageExt (buf) {
36
41
  }
37
42
 
38
43
  /** Borra las imágenes que ya cumplieron su tiempo. Un fallo aquí se dice y no para nada más. */
44
+ /**
45
+ * @param {string} dir
46
+ * @param {number} [now]
47
+ */
39
48
  export function pruneUploads (dir, now = Date.now()) {
40
49
  let names
41
- try { names = fs.readdirSync(dir) } catch (e) { if (e.code === 'ENOENT') return; throw e }
50
+ try { names = fs.readdirSync(dir) } catch (e) { if (/** @type {Thrown} */ (e).code === 'ENOENT') return; throw e }
42
51
  for (const name of names) {
43
52
  if (!/^img-.*\.(png|jpg|gif|webp)$/.test(name)) continue
44
53
  const file = path.join(dir, name)
45
54
  try { if (now - fs.statSync(file).mtimeMs > UPLOAD_KEEP_MS) fs.rmSync(file) } catch (e) {
46
- console.error('[terminal-agent] could not remove an old image', file, '-', e.message)
55
+ console.error('[terminal-agent] could not remove an old image', file, '-', /** @type {Thrown} */ (e).message)
47
56
  }
48
57
  }
49
58
  }
50
59
 
60
+ /** @param {Date} d */
51
61
  const stamp = (d) => {
62
+ /** @param {number} n */
52
63
  const p = (n) => String(n).padStart(2, '0')
53
64
  return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`
54
65
  }
@@ -59,10 +70,16 @@ const stamp = (d) => {
59
70
  * @param {string} dir la carpeta donde se guardan (se crea, solo para el usuario).
60
71
  * @returns {{ take: (msg: { up: string, seq: number, data: string, last?: boolean }) => { up: string, seq: number, path?: string }, drop: () => void }}
61
72
  */
73
+ /** @param {string} dir */
62
74
  export function makeUploads (dir) {
63
75
  /** @type {{ up: string, next: number, size: number, parts: Buffer[] } | null} */
64
76
  let cur = null
65
77
  return {
78
+ /**
79
+ * Un trozo de una imagen. `msg` llega de la red: se comprueba campo a campo.
80
+ * @param {any} msg
81
+ * @returns {{ up: string, seq: number, path?: string }}
82
+ */
66
83
  take (msg) {
67
84
  const up = typeof msg.up === 'string' ? msg.up.slice(0, 64) : ''
68
85
  const seq = msg.seq