Resi Cues 1.0.3: a login without access to the venue stops signing in every poll
Every 401/403 was answered by signing in again on the spot. For a login that works but may not list the account's venues (media@waters.church at North Attleboro got 403 on /venues), that was a successful sign-in on every 1s poll -- the pattern that had just got another Resi account locked. - A refusal straight after signing in is a permission, not an expired session: it is held off like a failed login, with the message "This Resi login isn't allowed to see its venues (403) — ask a Resi admin for access". Only a session over a minute old is renewed. - 429 on any request holds off too, not only on the login. - The players cache-bust fallback no longer drops the parameter for good on a 401/403/429, which are about the login rather than the parameter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -41,6 +41,7 @@ const WINDOW_MS = 120000; // how long a reading stays useful
|
||||
const JUMP_FLOOR_MS = 5000; // the smallest move ever treated as a seek
|
||||
const LOGIN_BACKOFF_MS = 15000; // first wait after a failed Resi login
|
||||
const LOGIN_BACKOFF_MAX_MS = 300000; // longest wait between login attempts
|
||||
const SESSION_FRESH_MS = 60000; // a refusal younger than this is a permission, not an expired session
|
||||
|
||||
/**
|
||||
* Resi republishes a decoder's `position` only every few seconds, on an irregular
|
||||
@@ -225,7 +226,7 @@ function createResiClient(options) {
|
||||
token = jar.livingas1 || '';
|
||||
if (!token) throw new Error('Resi login returned no session cookie');
|
||||
cookieHeader = Object.keys(jar).map((k) => `${k}=${jar[k]}`).join('; ');
|
||||
loggedInAt = Date.now();
|
||||
loggedInAt = now();
|
||||
const me = await get('/api_v2.svc/users/me');
|
||||
customerId = (me && me.customerId) || '';
|
||||
if (!customerId) throw new Error('This Resi account has no customerId');
|
||||
@@ -240,16 +241,34 @@ function createResiClient(options) {
|
||||
}
|
||||
|
||||
async function ensureSession() {
|
||||
if (!token || Date.now() - loggedInAt > 50 * 60 * 1000) await login();
|
||||
if (!token || now() - loggedInAt > 50 * 60 * 1000) await login();
|
||||
}
|
||||
|
||||
/* The same hold-off covers every request, not just the login. A 401/403 used to
|
||||
sign in again on the spot, every time: for a login that simply isn't allowed to
|
||||
see a venue, that was a fresh sign-in on every poll. Now a refusal straight
|
||||
after signing in is taken as what it is -- a permission -- and held off like a
|
||||
failed login; only an older session is renewed. A 429 holds off too. */
|
||||
async function get(path) {
|
||||
const res = await doFetch(RESI + path, { method: 'GET', headers: headers() });
|
||||
if (res.status === 401 || res.status === 403) {
|
||||
if (now() < loginBlockedUntil) {
|
||||
const wait = Math.ceil((loginBlockedUntil - now()) / 1000);
|
||||
throw new Error(`${loginError} — retrying in ${wait}s`);
|
||||
}
|
||||
let res = await doFetch(RESI + path, { method: 'GET', headers: headers() });
|
||||
if ((res.status === 401 || res.status === 403) && now() - loggedInAt > SESSION_FRESH_MS) {
|
||||
await login();
|
||||
const retry = await doFetch(RESI + path, { method: 'GET', headers: headers() });
|
||||
if (!retry.ok) throw new Error(`Resi ${path} failed (${retry.status})`);
|
||||
return retry.json();
|
||||
res = await doFetch(RESI + path, { method: 'GET', headers: headers() });
|
||||
}
|
||||
if (res.status === 401 || res.status === 403) {
|
||||
const where = path.includes('/venues') ? 'its venues'
|
||||
: path.includes('/players') ? "this venue's decoders" : 'part of the account';
|
||||
holdOff(`This Resi login isn't allowed to see ${where} (${res.status}) — ask a Resi admin for access`, 0);
|
||||
throw new Error(loginError);
|
||||
}
|
||||
if (res.status === 429) {
|
||||
const retryAfter = Number(res.headers && res.headers.get && res.headers.get('retry-after')) || 0;
|
||||
holdOff('Resi is rate-limiting requests (429)', retryAfter * 1000);
|
||||
throw new Error(loginError);
|
||||
}
|
||||
if (!res.ok) throw new Error(`Resi ${path} failed (${res.status})`);
|
||||
return res.json();
|
||||
@@ -269,7 +288,10 @@ function createResiClient(options) {
|
||||
try {
|
||||
return (await get(`${path}?_=${Date.now()}`)) || [];
|
||||
} catch (err) {
|
||||
if (!/\((4\d\d)\)/.test(String(err.message))) throw err;
|
||||
// Only an objection to the parameter itself; a refusal or a "slow down"
|
||||
// (401/403/429) is about the login, and dropping it then lost fresh readings.
|
||||
const code = (String(err.message).match(/\((4\d\d)\)/) || [])[1];
|
||||
if (!code || ['401', '403', '429'].includes(code)) throw err;
|
||||
cacheBust = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user