kamoji-dev 0.1.0 → 0.1.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/README.md +30 -11
- package/package.json +7 -2
- package/src/check.js +8 -207
- package/src/cli.js +38 -16
- package/src/core/bytes.js +112 -0
- package/src/core/check.js +222 -0
- package/src/core/dev.js +398 -0
- package/src/core/protocol.js +749 -0
- package/src/dev.js +14 -266
- package/src/logfilter.js +35 -0
- package/src/protocol.js +28 -555
package/src/dev.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
// One development session: a Kamoji link (reconnected when it drops), the app folder,
|
|
2
|
-
// Used by `kamoji dev` (watch), `kamoji install` (once), `kamoji apps`, `kamoji shot`, `kamoji
|
|
1
|
+
// One development session of the command line: a Kamoji link (reconnected when it drops), the app folder,
|
|
2
|
+
// the USB log. Used by `kamoji dev` (watch), `kamoji install` (once), `kamoji apps`, `kamoji shot`, `kamoji
|
|
3
|
+
// forget`. The session itself is core/dev.js (shared with the web IDE); this file reads the folder, runs
|
|
4
|
+
// luac, saves screenshots to disk and prints to the terminal.
|
|
3
5
|
import fs from 'node:fs'
|
|
4
6
|
import path from 'node:path'
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
import { checkApp } from './check.js'
|
|
8
|
+
import { DevSession as CoreDevSession } from './core/dev.js'
|
|
9
|
+
import { Session } from './protocol.js'
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
export const kb = (n) => `${(n / 1024).toFixed(n < 10240 ? 1 : 0)} KB`
|
|
11
|
-
const secs = (ms) => `${(ms / 1000).toFixed(1)} s`
|
|
11
|
+
export * from './core/dev.js'
|
|
12
12
|
|
|
13
13
|
/** Terminal output: one place, so tests can collect it. */
|
|
14
14
|
export function makeUi({ stream = process.stdout, color = stream.isTTY && !process.env.NO_COLOR, verbose = false } = {}) {
|
|
@@ -44,271 +44,19 @@ export function makeUi({ stream = process.stdout, color = stream.isTTY && !proce
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (p.level === 'error') ui.error(`${where}${p.message} [${p.id}]`)
|
|
51
|
-
else if (p.level === 'warning') ui.warn(`${where}${p.message} [${p.id}]`)
|
|
52
|
-
else ui.info(`${where}${p.message}`)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export class DevSession {
|
|
57
|
-
/**
|
|
58
|
-
* @param {object} o
|
|
59
|
-
* @param {string} [o.folder]
|
|
60
|
-
* @param {ReturnType<typeof makeUi>} o.ui
|
|
61
|
-
* @param {(opts:{onDisconnect:Function}) => Promise<{link:any, board:string, name?:string, revision?:string, firmware?:string}>} o.connector
|
|
62
|
-
* @param {(board:string) => {key:Buffer, created:boolean}} o.keys
|
|
63
|
-
* @param {import('./serial.js').SerialLog} [o.serial]
|
|
64
|
-
* @param {object} [o.timeouts]
|
|
65
|
-
* @param {boolean} [o.reboot] restart Kamoji by itself when the app's Bluetooth services need it
|
|
66
|
-
* @param {boolean} [o.lua]
|
|
67
|
-
*/
|
|
68
|
-
constructor(o) {
|
|
69
|
-
Object.assign(this, { reboot: false, lua: true, ...o })
|
|
70
|
-
this.session = null
|
|
71
|
-
this.conn = null
|
|
72
|
-
this.connected = false
|
|
73
|
-
this.connecting = null
|
|
74
|
-
this.lastHash = null
|
|
75
|
-
this.lastList = null
|
|
76
|
-
this.busy = false
|
|
77
|
-
this.closed = false
|
|
78
|
-
this.appId = null
|
|
79
|
-
this.keepalive = null
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// ------------------------------------------------------------------ connection
|
|
83
|
-
|
|
84
|
-
async connect() {
|
|
85
|
-
if (this.connected) return
|
|
86
|
-
this.connecting ??= this._connect().finally(() => (this.connecting = null))
|
|
87
|
-
return this.connecting
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async _connect() {
|
|
91
|
-
const ui = this.ui
|
|
92
|
-
ui.info('Looking for Kamoji over Bluetooth…')
|
|
93
|
-
const conn = await this.connector({ onDisconnect: (reason) => this._lost(reason) })
|
|
94
|
-
this.conn = conn
|
|
95
|
-
const session = new Session(conn.link, { timeouts: this.timeouts })
|
|
96
|
-
this.session = session
|
|
97
|
-
const { key, created } = this.keys(conn.board)
|
|
98
|
-
let auth
|
|
99
|
-
try {
|
|
100
|
-
auth = await session.authorize(key)
|
|
101
|
-
} catch (e) {
|
|
102
|
-
await conn.link.disconnect?.()
|
|
103
|
-
throw e
|
|
104
|
-
}
|
|
105
|
-
this.connected = true
|
|
106
|
-
if (auth.status === 'authorized' && auth.enrolled)
|
|
107
|
-
ui.ok(`${conn.name ?? conn.board} trusts this computer now (signal ${auth.rssi ?? '?'} dBm; ${auth.devices} trusted device(s)).`)
|
|
108
|
-
else if (created && auth.status === 'authorized') ui.info('Kamoji already trusted this key.')
|
|
109
|
-
await session.linkFast()
|
|
110
|
-
const info = await session.info()
|
|
111
|
-
this.firmware = info.fw
|
|
112
|
-
ui.ok(
|
|
113
|
-
`Connected to ${conn.name ?? conn.board}${info.nick ? ` "${info.nick}"` : ''}: firmware ${info.fw}${conn.revision ? `, ${conn.revision}` : ''}.`,
|
|
114
|
-
)
|
|
115
|
-
this.lastList = await session.listApps()
|
|
116
|
-
const st = this.lastList.storage
|
|
117
|
-
ui.info(
|
|
118
|
-
`${this.lastList.apps.length} mini app(s) on it${st ? `, storage ${kb(st.used)} of ${kb(st.total)} used` : ''}.`,
|
|
119
|
-
)
|
|
120
|
-
this._startKeepalive()
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
_lost(reason) {
|
|
124
|
-
if (!this.connected) return
|
|
125
|
-
this.connected = false
|
|
126
|
-
this.session?.close()
|
|
127
|
-
if (this.closed) return
|
|
128
|
-
this.ui.warn(`Bluetooth link to Kamoji lost${reason !== undefined ? ` (${reason})` : ''}: it restarted, went out of range, or another device connected.`)
|
|
129
|
-
if (this.watching) this._reconnectLoop()
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
async _reconnectLoop() {
|
|
133
|
-
for (let attempt = 1; !this.closed && !this.connected; attempt++) {
|
|
134
|
-
await sleep(attempt <= 10 ? 3000 : 10000)
|
|
135
|
-
if (this.closed || this.connected) return
|
|
136
|
-
try {
|
|
137
|
-
await this.connect()
|
|
138
|
-
if (this.appId) await this.open()
|
|
139
|
-
return
|
|
140
|
-
} catch (e) {
|
|
141
|
-
if (attempt === 1 || attempt % 10 === 0) this.ui.info(`Reconnecting… (${e.message})`)
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
_startKeepalive() {
|
|
147
|
-
if (this.keepalive) clearInterval(this.keepalive)
|
|
148
|
-
this.keepalive = setInterval(() => {
|
|
149
|
-
if (!this.connected) return
|
|
150
|
-
this.session.linkAlive()
|
|
151
|
-
// While watching (`kamoji dev`), keep the link fast between saves: Kamoji holds it LINK_FAST_HOLD_MS (8 s)
|
|
152
|
-
// after the last LINK_FAST, then relaxes to an 800 ms interval, and the next reload waited ~8 s at that
|
|
153
|
-
// pace for the fast parameters to come back (12.7 s installs instead of 2-3 s, R8 test 2026-11-17).
|
|
154
|
-
if (this.busy || this.watching) this.session.linkFast()
|
|
155
|
-
}, 5000)
|
|
156
|
-
this.keepalive.unref?.()
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
async close() {
|
|
160
|
-
this.closed = true
|
|
161
|
-
if (this.keepalive) clearInterval(this.keepalive)
|
|
162
|
-
this.session?.close()
|
|
163
|
-
await this.conn?.link.disconnect?.()
|
|
164
|
-
await this.serial?.close()
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// ------------------------------------------------------------------ the app
|
|
168
|
-
|
|
169
|
-
/** Check the folder; print problems; returns the check result (null = errors). */
|
|
170
|
-
check() {
|
|
171
|
-
const r = checkApp(this.folder, { lua: this.lua })
|
|
172
|
-
const shown = r.problems.filter((p) => p.level !== 'info' || p.id !== 'skipped' || this.ui.verbose)
|
|
173
|
-
printProblems(this.ui, { problems: shown })
|
|
174
|
-
if (r.errors.length) {
|
|
175
|
-
this.ui.error(`${r.errors.length} problem(s) to fix first: nothing was sent.`)
|
|
176
|
-
return null
|
|
177
|
-
}
|
|
178
|
-
return r
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Check, install and open. Returns true when the app is on screen.
|
|
183
|
-
* @param {{force?: boolean}} [o]
|
|
184
|
-
*/
|
|
185
|
-
async deploy({ force = false } = {}) {
|
|
186
|
-
const ui = this.ui
|
|
187
|
-
const r = this.check()
|
|
188
|
-
if (!r) return false
|
|
189
|
-
const hash = bundleHash(r.files)
|
|
190
|
-
if (!force && hash === this.lastHash) {
|
|
191
|
-
ui.info('No change in the files: nothing sent.')
|
|
192
|
-
return true
|
|
193
|
-
}
|
|
194
|
-
this.appId = r.appId
|
|
195
|
-
const mf = r.manifest.min_firmware
|
|
196
|
-
await this.connect()
|
|
197
|
-
if (typeof mf === 'string' && this.firmware && compareVersions(this.firmware, mf) < 0) {
|
|
198
|
-
ui.error(
|
|
199
|
-
`${r.appId} needs firmware ${mf} or newer; this Kamoji runs ${this.firmware} [firmware-too-old]`,
|
|
200
|
-
'Update Kamoji in the Kamoji app (Settings > Firmware), or lower min_firmware if the app does not need it.',
|
|
201
|
-
)
|
|
202
|
-
return false
|
|
203
|
-
}
|
|
204
|
-
const st = this.lastList?.storage
|
|
205
|
-
if (st && r.bytes > st.total - st.used) {
|
|
206
|
-
ui.error(
|
|
207
|
-
`${r.appId} is ${kb(r.bytes)}; Kamoji has ${kb(Math.max(0, st.total - st.used))} free [no-space]`,
|
|
208
|
-
'Remove a mini app in the Kamoji app (Apps), or make this one smaller.',
|
|
209
|
-
)
|
|
210
|
-
return false
|
|
211
|
-
}
|
|
212
|
-
this.busy = true
|
|
213
|
-
const t0 = Date.now()
|
|
214
|
-
try {
|
|
215
|
-
await this.session.linkFast()
|
|
216
|
-
const res = await this.session.install(r.appId, r.files, (p) =>
|
|
217
|
-
ui.progress(`sending ${p.file} (${p.index + 1}/${p.files}): ${kb(p.sent)} of ${kb(p.total)}`),
|
|
218
|
-
)
|
|
219
|
-
const t1 = Date.now()
|
|
220
|
-
this.lastHash = hash
|
|
221
|
-
const rate = res.bytes / Math.max(0.001, (t1 - t0) / 1000)
|
|
222
|
-
ui.ok(`Installed ${r.appId} ${res.version} in ${secs(t1 - t0)} (${r.files.length} file(s), ${kb(res.bytes)}, ${kb(rate)}/s).`)
|
|
223
|
-
await this.afterInstall(r, t0)
|
|
224
|
-
return true
|
|
225
|
-
} catch (e) {
|
|
226
|
-
this._explain(e)
|
|
227
|
-
return false
|
|
228
|
-
} finally {
|
|
229
|
-
this.busy = false
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
async afterInstall(r, t0) {
|
|
234
|
-
const ui = this.ui
|
|
235
|
-
this.lastList = await this.session.listApps()
|
|
236
|
-
const app = this.lastList.apps.find((a) => a.id === r.appId)
|
|
237
|
-
if (app && !app.ok) {
|
|
238
|
-
ui.error(`Kamoji installed ${r.appId} but can't run it: ${app.reason || 'see the USB log'}.`)
|
|
239
|
-
return
|
|
240
|
-
}
|
|
241
|
-
if (r.manifest.ble !== undefined) {
|
|
242
|
-
const status = await this.session.status(r.appId)
|
|
243
|
-
if (status === 'pending_restart') {
|
|
244
|
-
if (this.reboot) {
|
|
245
|
-
ui.info('Its Bluetooth services are new or changed: restarting Kamoji (--reboot)…')
|
|
246
|
-
await this.restart()
|
|
247
|
-
return
|
|
248
|
-
}
|
|
249
|
-
ui.warn('Its Bluetooth services are new or changed: they go live after a restart of Kamoji. Press b + Enter to restart it (or use --reboot).')
|
|
250
|
-
} else if (status === 'profile_error') ui.error('Its Bluetooth services could not start: details in the USB log.')
|
|
251
|
-
}
|
|
252
|
-
await this.open(t0)
|
|
47
|
+
export class DevSession extends CoreDevSession {
|
|
48
|
+
makeSession(link, opts) {
|
|
49
|
+
return new Session(link, opts)
|
|
253
50
|
}
|
|
254
51
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const total = t0 ? `, ${secs(Date.now() - t0)} after the first byte` : ''
|
|
258
|
-
this.ui.ok(`${this.appId} is on screen (mode ${r.mode}${r.confirmed ? '' : ', not confirmed'}${total}).`)
|
|
259
|
-
return r
|
|
52
|
+
runChecks() {
|
|
53
|
+
return checkApp(this.folder, { lua: this.lua })
|
|
260
54
|
}
|
|
261
55
|
|
|
262
|
-
|
|
263
|
-
await this.session.reboot()
|
|
264
|
-
this.connected = false
|
|
265
|
-
this.session.close()
|
|
266
|
-
await this.conn?.link.disconnect?.()
|
|
267
|
-
await sleep(8000)
|
|
268
|
-
for (let i = 0; i < 10 && !this.connected; i++) {
|
|
269
|
-
try {
|
|
270
|
-
await this.connect()
|
|
271
|
-
} catch {
|
|
272
|
-
await sleep(3000)
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
if (!this.connected) throw new KamojiError('timeout', 'Kamoji did not come back after the restart.')
|
|
276
|
-
if (this.appId) await this.open()
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
_explain(e) {
|
|
280
|
-
if (e instanceof KamojiError) this.ui.error(e.message, e.hint)
|
|
281
|
-
else this.ui.error(e?.message ?? String(e))
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// ------------------------------------------------------------------ screenshots, USB log
|
|
285
|
-
|
|
286
|
-
async screenshot(dir) {
|
|
287
|
-
const name = `${this.appId ?? 'kamoji'}-${new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19)}.jpg`
|
|
288
|
-
let jpeg
|
|
289
|
-
let via
|
|
290
|
-
if (this.serial) {
|
|
291
|
-
jpeg = await this.serial.screenshot(90)
|
|
292
|
-
via = 'USB'
|
|
293
|
-
} else {
|
|
294
|
-
await this.connect()
|
|
295
|
-
jpeg = await this.session.screenshot()
|
|
296
|
-
via = 'Bluetooth'
|
|
297
|
-
}
|
|
56
|
+
saveShot(dir, name, jpeg) {
|
|
298
57
|
fs.mkdirSync(dir, { recursive: true })
|
|
299
58
|
const file = path.join(dir, name)
|
|
300
59
|
fs.writeFileSync(file, jpeg)
|
|
301
|
-
this.ui.ok(`Screenshot (${via}, ${kb(jpeg.length)}): ${file}`)
|
|
302
60
|
return file
|
|
303
61
|
}
|
|
304
|
-
|
|
305
|
-
/** The USB log line printer (filtered to the app unless verbose). */
|
|
306
|
-
logLine(line) {
|
|
307
|
-
if (this.ui.verbose) {
|
|
308
|
-
this.ui.raw(line)
|
|
309
|
-
return
|
|
310
|
-
}
|
|
311
|
-
const c = classify(line, this.appId)
|
|
312
|
-
if (c) this.ui.log(c.kind, c.text)
|
|
313
|
-
}
|
|
314
62
|
}
|
package/src/logfilter.js
CHANGED
|
@@ -54,3 +54,38 @@ export function classify(raw, appId) {
|
|
|
54
54
|
if (ECODE.test(ln)) return { kind: 'error', text: redact(message(ln)) }
|
|
55
55
|
return null
|
|
56
56
|
}
|
|
57
|
+
|
|
58
|
+
const DROPPED = /^(\d+) line\(s\) dropped$/
|
|
59
|
+
const LIFECYCLE = /^(entered in|exited|stopped)\b/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* An app log line over Bluetooth (firmware 0.3.3: `APP:LOG:<L>:<app_id>:<text>`, parsed by
|
|
63
|
+
* protocol.parseLogLine) → the same { kind, text } as classify() gives for the USB line, or null.
|
|
64
|
+
* L = I (print, lifecycle, timings), W (CPU budget, long operations, warnings), E (Lua errors, E30xx/E36xx).
|
|
65
|
+
* Text is a print() line, or a "[<id>] …" line (a bare "entered in …" / "stopped: …" counts as one too).
|
|
66
|
+
* @param {{level:string, appId:string, text:string}} log
|
|
67
|
+
* @param {string|null} [appId] only this app's lines
|
|
68
|
+
*/
|
|
69
|
+
export function classifyBle(log, appId) {
|
|
70
|
+
if (appId && log.appId !== appId) return null
|
|
71
|
+
let text = clean(log.text).replace(/^\[app\.lua[^\]]*\]:?\s*/, '')
|
|
72
|
+
const tag = `[${log.appId}]`
|
|
73
|
+
const dropped = DROPPED.exec(text)
|
|
74
|
+
if (dropped && log.level === 'W')
|
|
75
|
+
return { kind: 'warn', text: `[${log.appId}] Kamoji dropped ${dropped[1]} log line(s) (more than 20 a second)` }
|
|
76
|
+
let tagged = text.startsWith(tag)
|
|
77
|
+
let rest = tagged ? text.slice(tag.length).trim() : text
|
|
78
|
+
if (!tagged && LIFECYCLE.test(text)) {
|
|
79
|
+
tagged = true
|
|
80
|
+
text = `${tag} ${text}`
|
|
81
|
+
}
|
|
82
|
+
if (tagged) {
|
|
83
|
+
if (rest.startsWith('stopped:')) return { kind: 'stopped', text: redact(text) }
|
|
84
|
+
if (rest.includes('CPU budget exceeded')) return { kind: 'cpu', text: redact(text) }
|
|
85
|
+
if (rest.startsWith('entered in')) return { kind: 'entered', text }
|
|
86
|
+
}
|
|
87
|
+
if (log.level === 'E') return { kind: 'error', text: redact(text) }
|
|
88
|
+
if (log.level === 'W') return { kind: rest.includes('CPU budget exceeded') ? 'cpu' : 'warn', text: redact(text) }
|
|
89
|
+
if (tagged) return { kind: 'app', text: redact(text) }
|
|
90
|
+
return { kind: 'lua', text: redact(text).slice(0, 400) }
|
|
91
|
+
}
|