Merge pull request #14 from efiten/fix/region-hold-timers

fix: the region hold's default timers threw in the browser (v1.18.3)
This commit is contained in:
efiten authored and GitHub committed 2026-09-18 10:57:46 +02:00
commit 9292d28a81
5 files changed
+98 -3

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
# CoreDrive RX v1.18.3
Fixes a browser-only error that cost the contact-path restore on the first region ask to
every saved-contact repeater.
## What's new
- **Fix: "regions: ask to … failed: Illegal invocation".** `holdUntilReply`'s default
argument was `timers = { setTimeout, clearTimeout }`, so the native timers were called
with that object literal as their receiver. Browsers require the global there and
Chromium rejects anything else with `TypeError: Illegal invocation`. Node's timers do
not check, which is why 279 tests passed over it.
The throw lands inside `askRepeater`'s `try`, after the request has already gone out,
so two things were skipped: the 20 s hold that keeps the queue still until the
repeater answers, and the write that restores that repeater's original `out_path`.
The restore record stayed in `localStorage` and was replayed on the next connect, so
the path came back — but only at the next connect, not at the end of the ask.
It fires once per repeater per session: only the ask that applies the path override
reaches the hold, and after that the contact's `out_path_len` is already 0. A field
log from 2026-09-18 shows exactly three, one each for `14036f`, `e3d3f4` and `cf7903`,
every one on "attempt 1 of 4".
`test/regionsched-timers.test.mjs` pins the receiver rule with a stub that throws
unless the timers are called the way a browser demands.
## Upgrade notes
- Service-worker cache bumped to `coredrive-rx-v52`.
- No configuration or data migration. The MQTT payload contract is unchanged.
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "coredrive-rx",
"version": "1.18.2",
"version": "1.18.3",
"private": false,
"license": "GPL-3.0-or-later",
"type": "module",
+1 -1
View File
@@ -1,7 +1,7 @@
// Minimal service worker: makes the app installable and lets the shell load
// offline. Network-first (always fresh online), cache fallback (offline).
// Only the static app assets go through fetch; MQTT runs over WSS (not cached).
const CACHE = 'coredrive-rx-v51';
const CACHE = 'coredrive-rx-v52';
// config.json is deliberately NEVER cached, in either direction. It is
// per-deployment runtime config, and a stale copy is worse than none: flags added
+13 -1
View File
@@ -243,7 +243,19 @@ export function markAnswered(answered, queue, target) {
// were visible and all arrived 1–2s after the ask. Ending the hold on the reply removes
// only time AFTER the answer is in hand; how early a restore is safe while no reply has
// come is a firmware question, so the timeout stays for that case.
export function holdUntilReply(waiters, target, timeoutMs, timers = { setTimeout, clearTimeout }) {
// The default timers are wrapped, not passed by reference. A browser requires the
// native timer functions to be called with the global as `this`, and
// `({ setTimeout }).setTimeout(fn, ms)` hands them the object literal instead, which
// Chromium rejects with "TypeError: Illegal invocation". Node's timers do not check,
// so every test here passed while the field log of 2026-09-18 lost the hold AND the
// contact-path restore on the first ask to each saved-contact repeater (three of them
// that morning). test/regionsched-timers.test.mjs pins the receiver rule.
const defaultTimers = () => ({
setTimeout: (fn, ms) => setTimeout(fn, ms),
clearTimeout: (id) => clearTimeout(id),
});
export function holdUntilReply(waiters, target, timeoutMs, timers = defaultTimers()) {
return new Promise((resolve) => {
const timer = timers.setTimeout(() => { waiters.delete(target); resolve('timeout'); }, timeoutMs);
waiters.set(target, () => { timers.clearTimeout(timer); waiters.delete(target); resolve('reply'); });
+52
View File
@@ -0,0 +1,52 @@
// holdUntilReply's DEFAULT timers, which every other test bypasses by passing its own.
//
// Browsers require the native timer functions to be called with the global object as
// `this`: `({ setTimeout }).setTimeout(fn, 0)` throws "TypeError: Illegal invocation"
// in Chromium. Node's setTimeout is an ordinary function and does not check, so a
// default built as an object literal passes every test here and throws on a phone.
//
// It did. Field log 2026-09-18 08:21:06, 08:26:23 and 08:26:54, one per repeater,
// each immediately after the ask went out and each on the one attempt where the
// contact-path override had been applied — so the hold was skipped and the repeater's
// original path was never restored in that session.
//
// The stub below is the browser's rule, not a mock of our code: it throws unless it is
// called with the global as its receiver.
// Run: node --test
import { test } from 'node:test';
import assert from 'node:assert';
import { holdUntilReply, releaseHold } from '../src/regionsched.js';
const A = 'a'.repeat(64);
function withStrictTimers(fn) {
const realSet = globalThis.setTimeout;
const realClear = globalThis.clearTimeout;
globalThis.setTimeout = function strictSetTimeout(cb, ms) {
if (this !== globalThis && this !== undefined) throw new TypeError('Illegal invocation');
return realSet(cb, ms);
};
globalThis.clearTimeout = function strictClearTimeout(id) {
if (this !== globalThis && this !== undefined) throw new TypeError('Illegal invocation');
return realClear(id);
};
try { return fn(); } finally {
globalThis.setTimeout = realSet;
globalThis.clearTimeout = realClear;
}
}
test('the default timers survive a browser receiver check', async () => {
const waiters = new Map();
const held = withStrictTimers(() => holdUntilReply(waiters, A, 50));
assert.strictEqual(typeof held.then, 'function');
assert.strictEqual(await held, 'timeout');
});
test('releasing a default-timer hold clears its timer without throwing', async () => {
const waiters = new Map();
const held = withStrictTimers(() => holdUntilReply(waiters, A, 20000));
withStrictTimers(() => assert.strictEqual(releaseHold(waiters, A), true));
assert.strictEqual(await held, 'reply');
assert.strictEqual(waiters.size, 0);
});