ituran-axi 0.1.3 → 0.1.4
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 +7 -0
- package/dist/bin/ituran-axi.js +6 -1
- package/dist/cli/run.js +7 -1
- package/dist/command-lock.js +225 -0
- package/dist/http.js +13 -4
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -109,6 +109,12 @@ ituran-axi handles these, and says so in its output where it matters:
|
|
|
109
109
|
- Keypad driver codes above 2^53 stay exact.
|
|
110
110
|
- A model year or odometer of 0 means "not set".
|
|
111
111
|
|
|
112
|
+
Commands sharing a user's cache run their HTTP reads one command at a time, including sign-in. Requests within a command keep the existing limit of two in flight, starting at least 400 ms apart. The next command waits at least 400 ms after the previous command finishes. The command lock is `~/.cache/ituran-axi/command.lock` (or under `XDG_CACHE_HOME`). Waiting is limited to 30 seconds, or the remaining `COMIS_DEADLINE` minus one second. A busy lock returns `busy` without sending a request. Wait for the other command to finish before trying again.
|
|
113
|
+
|
|
114
|
+
The lock records its owner's PID, hostname and a random token. The holder refreshes its heartbeat every two seconds. A dead PID on the same host, or a heartbeat missing for ten seconds, permits automatic recovery. Release checks the owner token, so an old holder cannot remove a replacement lock. If cache storage is unavailable, the client tries a shared per-user lock under the system temporary directory. If that is also unavailable, it continues without a process lock, with the same output. Cache and cleanup errors never replace a successful result. Separate users, cache directories and hosts need their own coordination; the 400 ms gap between commands also depends on writable cache storage.
|
|
115
|
+
|
|
116
|
+
Request timeouts cover the response body too: 60 seconds for reads, 30 seconds for sign-in, shortened by `COMIS_DEADLINE` when set. A timeout aborts the request and cancels the response body. The CLI flushes its output and exits, releasing Node's proxy dispatcher sockets even if a proxy has not answered CONNECT.
|
|
117
|
+
|
|
112
118
|
## Output and errors
|
|
113
119
|
|
|
114
120
|
- **Size:** a value is cut at 500 characters and the whole output at about 4,000. `--full` lifts both limits, up to 256 KB.
|
|
@@ -127,6 +133,7 @@ ituran-axi handles these, and says so in its output where it matters:
|
|
|
127
133
|
| `not_entitled`, `feature_off`, `needs_app_id` | This account does not include that service |
|
|
128
134
|
| `rejected` | Ituran refused the request; its message is shown |
|
|
129
135
|
| `rate_limited`, `ituran_error`, `unreachable` | Ituran limited the request, failed, or could not be reached |
|
|
136
|
+
| `busy` | Another local command holds the process lock. Wait for it to finish before trying again. |
|
|
130
137
|
| `reconnect_needed`, `refused` | Inside Comis: the owner must reconnect, or the Gate refused |
|
|
131
138
|
| `output_too_large` | Even `--full` would exceed 256 KB; narrow the query |
|
|
132
139
|
| `internal_error` | A bug in ituran-axi; please [report it](https://github.com/comisai/ituran-axi/issues) |
|
package/dist/bin/ituran-axi.js
CHANGED
|
@@ -7,7 +7,7 @@ process.stdout.on('error', (error) => {
|
|
|
7
7
|
process.exit(0);
|
|
8
8
|
throw error;
|
|
9
9
|
});
|
|
10
|
-
|
|
10
|
+
const code = await main(process.argv.slice(2), {
|
|
11
11
|
stdout: (text) => {
|
|
12
12
|
process.stdout.write(text);
|
|
13
13
|
},
|
|
@@ -15,3 +15,8 @@ process.exitCode = await main(process.argv.slice(2), {
|
|
|
15
15
|
now: () => new Date(),
|
|
16
16
|
http: defaultDeps,
|
|
17
17
|
});
|
|
18
|
+
// Node's environment-proxy dispatcher can keep a CONNECT socket alive even
|
|
19
|
+
// after fetch has aborted. This is a one-command process: once cleanup has
|
|
20
|
+
// finished and stdout has drained, exit to release the dispatcher and sockets.
|
|
21
|
+
// Setting only exitCode leaves that socket holding the process open.
|
|
22
|
+
process.stdout.end(() => process.exit(code));
|
package/dist/cli/run.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { encode } from '@toon-format/toon';
|
|
4
4
|
import { cacheDir, noteAvailable, noteUnavailable } from '../account.js';
|
|
5
5
|
import { forgetToken } from '../auth.js';
|
|
6
|
+
import { withCommandLock } from '../command-lock.js';
|
|
6
7
|
import { alerts, event, events, groups } from '../commands/catalog.js';
|
|
7
8
|
import { centers } from '../commands/centers.js';
|
|
8
9
|
import { driver, drivers } from '../commands/drivers.js';
|
|
@@ -526,7 +527,12 @@ export async function main(argv, io) {
|
|
|
526
527
|
io.stdout(commandHelp(found));
|
|
527
528
|
return 0;
|
|
528
529
|
}
|
|
529
|
-
|
|
530
|
+
// Help and local logout do not need network serialization. Validate the
|
|
531
|
+
// connection before creating a lock so missing settings stay actionable.
|
|
532
|
+
const input = { ...parsed, io };
|
|
533
|
+
const out = spec.name === 'logout'
|
|
534
|
+
? await spec.run(input)
|
|
535
|
+
: await withCommandLock(io.env, connected(io.env).deadline, () => spec.run(input));
|
|
530
536
|
remember(spec, io, null);
|
|
531
537
|
io.stdout(out);
|
|
532
538
|
return 0;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// One network command at a time per user's cache, across accounts and tokens.
|
|
2
|
+
// A heartbeat lease and owner token let killed processes recover without
|
|
3
|
+
// deleting or refreshing a replacement owner's lock.
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { closeSync, fstatSync, futimesSync, linkSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync, } from 'node:fs';
|
|
6
|
+
import { homedir, hostname, tmpdir } from 'node:os';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
9
|
+
import { AxiError } from './errors.js';
|
|
10
|
+
import { GAP_MS } from './pace.js';
|
|
11
|
+
export const LOCK_WAIT_MS = 30_000;
|
|
12
|
+
const POLL_MS = 100;
|
|
13
|
+
const HEARTBEAT_MS = 2000;
|
|
14
|
+
const LEASE_MS = 10_000;
|
|
15
|
+
function snapshot(file) {
|
|
16
|
+
const fd = openSync(file, 'r');
|
|
17
|
+
try {
|
|
18
|
+
const stat = fstatSync(fd);
|
|
19
|
+
let owner;
|
|
20
|
+
try {
|
|
21
|
+
const value = JSON.parse(readFileSync(fd, 'utf8'));
|
|
22
|
+
if (Number.isSafeInteger(value?.pid) &&
|
|
23
|
+
value.pid > 0 &&
|
|
24
|
+
typeof value.hostname === 'string' &&
|
|
25
|
+
typeof value.token === 'string' &&
|
|
26
|
+
value.token.length > 0)
|
|
27
|
+
owner = value;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// An interrupted creation or an old-format file still has a lease.
|
|
31
|
+
}
|
|
32
|
+
return { stat, owner };
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
closeSync(fd);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function stale(value) {
|
|
39
|
+
if (Date.now() - value.stat.mtimeMs > LEASE_MS)
|
|
40
|
+
return true;
|
|
41
|
+
if (value.owner?.hostname === hostname()) {
|
|
42
|
+
try {
|
|
43
|
+
process.kill(value.owner.pid, 0);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
return error.code === 'ESRCH';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
/** Move out of the way, then re-check the inode, owner and heartbeat. */
|
|
52
|
+
function recover(lock) {
|
|
53
|
+
const before = snapshot(lock);
|
|
54
|
+
if (!stale(before))
|
|
55
|
+
return;
|
|
56
|
+
const moved = `${lock}.${randomUUID()}.stale`;
|
|
57
|
+
renameSync(lock, moved);
|
|
58
|
+
try {
|
|
59
|
+
const after = snapshot(moved);
|
|
60
|
+
if (before.stat.dev === after.stat.dev &&
|
|
61
|
+
before.stat.ino === after.stat.ino &&
|
|
62
|
+
before.owner?.token === after.owner?.token &&
|
|
63
|
+
stale(after))
|
|
64
|
+
return;
|
|
65
|
+
// A heartbeat or another contender changed the lock before the rename.
|
|
66
|
+
// Restore without overwriting any owner that has since acquired the path.
|
|
67
|
+
linkSync(moved, lock);
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
rmSync(moved, { force: true });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const busy = () => new AxiError('busy', 'Another ituran-axi command is running. Wait for it to finish, then run this command again.');
|
|
74
|
+
async function acquire(dir, until, owner) {
|
|
75
|
+
const lock = join(dir, 'command.lock');
|
|
76
|
+
try {
|
|
77
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
for (;;) {
|
|
83
|
+
if (performance.now() >= until)
|
|
84
|
+
throw busy();
|
|
85
|
+
let fd;
|
|
86
|
+
try {
|
|
87
|
+
fd = openSync(lock, 'wx', 0o600);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
if (error.code !== 'EEXIST')
|
|
91
|
+
return null;
|
|
92
|
+
try {
|
|
93
|
+
recover(lock);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
const code = error.code;
|
|
97
|
+
if (code !== 'ENOENT' && code !== 'EEXIST')
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
await sleep(Math.min(POLL_MS, Math.max(0, until - performance.now())));
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
writeFileSync(fd, JSON.stringify(owner));
|
|
105
|
+
return { fd, lock, next: join(dir, 'command.next') };
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
try {
|
|
109
|
+
const current = snapshot(lock).stat;
|
|
110
|
+
const created = fstatSync(fd);
|
|
111
|
+
if (current.dev === created.dev && current.ino === created.ino)
|
|
112
|
+
rmSync(lock, { force: true });
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
/* cache failure */
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
closeSync(fd);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
/* cache failure */
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
export async function withCommandLock(env, deadline, fn) {
|
|
128
|
+
const waitMs = Math.min(LOCK_WAIT_MS, deadline === undefined ? LOCK_WAIT_MS : deadline - Date.now() - 1000);
|
|
129
|
+
if (waitMs <= 0) {
|
|
130
|
+
throw new AxiError('unreachable', 'The time for this command ran out before Ituran was asked.');
|
|
131
|
+
}
|
|
132
|
+
const until = performance.now() + waitMs;
|
|
133
|
+
const root = env.XDG_CACHE_HOME || join(env.HOME || homedir(), '.cache');
|
|
134
|
+
const owner = {
|
|
135
|
+
pid: process.pid,
|
|
136
|
+
hostname: hostname(),
|
|
137
|
+
token: randomUUID(),
|
|
138
|
+
};
|
|
139
|
+
const held = (await acquire(join(root, 'ituran-axi'), until, owner)) ??
|
|
140
|
+
(await acquire(join(tmpdir(), `ituran-axi-${process.getuid?.() ?? 'user'}`), until, owner));
|
|
141
|
+
// Cache storage is an optimization. Preserve reads even if both locations
|
|
142
|
+
// are unavailable; contention on a usable lock never takes this fallback.
|
|
143
|
+
if (!held)
|
|
144
|
+
return fn();
|
|
145
|
+
const ownsPath = () => {
|
|
146
|
+
try {
|
|
147
|
+
return snapshot(held.lock).owner?.token === owner.token;
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const heartbeat = setInterval(() => {
|
|
154
|
+
try {
|
|
155
|
+
const now = new Date();
|
|
156
|
+
// Refresh this inode, even if the pathname now belongs to someone else.
|
|
157
|
+
futimesSync(held.fd, now, now);
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
/* a failed refresh expires the lease */
|
|
161
|
+
}
|
|
162
|
+
}, HEARTBEAT_MS);
|
|
163
|
+
heartbeat.unref();
|
|
164
|
+
let released = false;
|
|
165
|
+
const release = () => {
|
|
166
|
+
if (released)
|
|
167
|
+
return;
|
|
168
|
+
released = true;
|
|
169
|
+
clearInterval(heartbeat);
|
|
170
|
+
try {
|
|
171
|
+
if (ownsPath()) {
|
|
172
|
+
try {
|
|
173
|
+
writeFileSync(held.next, String(Date.now() + GAP_MS), {
|
|
174
|
+
mode: 0o600,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
/* a cache failure must not replace the command result */
|
|
179
|
+
}
|
|
180
|
+
finally {
|
|
181
|
+
if (ownsPath())
|
|
182
|
+
rmSync(held.lock, { force: true });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
/* cleanup must not replace the command result */
|
|
188
|
+
}
|
|
189
|
+
finally {
|
|
190
|
+
try {
|
|
191
|
+
closeSync(held.fd);
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
/* already closed */
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
const interrupt = () => process.exit(130);
|
|
199
|
+
const terminate = () => process.exit(143);
|
|
200
|
+
process.once('exit', release);
|
|
201
|
+
process.once('SIGINT', interrupt);
|
|
202
|
+
process.once('SIGTERM', terminate);
|
|
203
|
+
try {
|
|
204
|
+
let notBefore = 0;
|
|
205
|
+
try {
|
|
206
|
+
notBefore = Number(readFileSync(held.next, 'utf8'));
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
/* an unavailable gap cache does not block reads */
|
|
210
|
+
}
|
|
211
|
+
const gap = Math.min(GAP_MS, notBefore - Date.now());
|
|
212
|
+
if (gap > 0) {
|
|
213
|
+
if (performance.now() + gap >= until)
|
|
214
|
+
throw busy();
|
|
215
|
+
await sleep(gap);
|
|
216
|
+
}
|
|
217
|
+
return await fn();
|
|
218
|
+
}
|
|
219
|
+
finally {
|
|
220
|
+
process.removeListener('SIGINT', interrupt);
|
|
221
|
+
process.removeListener('SIGTERM', terminate);
|
|
222
|
+
process.removeListener('exit', release);
|
|
223
|
+
release();
|
|
224
|
+
}
|
|
225
|
+
}
|
package/dist/http.js
CHANGED
|
@@ -77,22 +77,31 @@ export async function fetchAnswer(deps, url, init, timeoutMs) {
|
|
|
77
77
|
throw new AxiError('unreachable', 'The time for this command ran out before Ituran was asked.');
|
|
78
78
|
}
|
|
79
79
|
const started = performance.now();
|
|
80
|
+
const controller = new AbortController();
|
|
81
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
80
82
|
let response;
|
|
81
83
|
let buffer;
|
|
82
84
|
try {
|
|
83
85
|
response = await deps.fetch(url, {
|
|
84
86
|
...init,
|
|
85
87
|
redirect: 'manual',
|
|
86
|
-
signal:
|
|
88
|
+
signal: controller.signal,
|
|
87
89
|
});
|
|
88
90
|
buffer = Buffer.from(await response.arrayBuffer());
|
|
89
91
|
}
|
|
90
|
-
catch
|
|
91
|
-
|
|
92
|
-
throw new AxiError('unreachable', timedOut
|
|
92
|
+
catch {
|
|
93
|
+
throw new AxiError('unreachable', controller.signal.aborted
|
|
93
94
|
? `Ituran did not answer within ${Math.round(timeoutMs / 1000)} s.`
|
|
94
95
|
: 'Ituran could not be reached. Check the network, or whether the Gate is running.');
|
|
95
96
|
}
|
|
97
|
+
finally {
|
|
98
|
+
clearTimeout(timer);
|
|
99
|
+
// Stop receiving even when buffering failed. The body may already be
|
|
100
|
+
// consumed or cancelled by fetch; cancelling it again is harmless.
|
|
101
|
+
controller.abort();
|
|
102
|
+
if (response?.body)
|
|
103
|
+
await response.body.cancel().catch(() => { });
|
|
104
|
+
}
|
|
96
105
|
const text = buffer.toString('utf8');
|
|
97
106
|
let json;
|
|
98
107
|
try {
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Kept equal to package.json by test/unit/version.test.ts.
|
|
2
|
-
export const VERSION = '0.1.
|
|
2
|
+
export const VERSION = '0.1.4';
|