@walldock/agent 0.1.2 → 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/dist/main.js +26 -0
- package/dist/sync.d.ts +3 -0
- package/dist/sync.js +25 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -114,6 +114,32 @@ async function main() {
|
|
|
114
114
|
log(`[sync] ok at ${lastSyncAt}`);
|
|
115
115
|
},
|
|
116
116
|
onLog: log,
|
|
117
|
+
onInvalidToken() {
|
|
118
|
+
void (async () => {
|
|
119
|
+
log('Device token is no longer valid. Clearing credentials…');
|
|
120
|
+
await storage.deleteDeviceToken();
|
|
121
|
+
await storage.clearDeviceIdentity();
|
|
122
|
+
if (isDaemon) {
|
|
123
|
+
log('Run "walldock-agent" (without --daemon) to re-pair this device.');
|
|
124
|
+
await shutdown();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
console.log('\nDevice token is no longer valid. Please re-pair this device.\n');
|
|
128
|
+
let result;
|
|
129
|
+
try {
|
|
130
|
+
result = await (0, auth_1.runPairingFlow)(api);
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
console.error(`\nRe-pairing failed: ${err}`);
|
|
134
|
+
await shutdown();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
await storage.setDeviceToken(result.token);
|
|
138
|
+
await storage.setDeviceIdentity({ deviceId: result.deviceId, deviceName: result.deviceName });
|
|
139
|
+
console.log(`\n✓ Re-linked as "${result.deviceName}"\n`);
|
|
140
|
+
sync.start({ token: result.token, deviceId: result.deviceId });
|
|
141
|
+
})();
|
|
142
|
+
},
|
|
117
143
|
});
|
|
118
144
|
// Graceful shutdown
|
|
119
145
|
let stopping = false;
|
package/dist/sync.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type ConnectionStatus = 'idle' | 'online' | 'error';
|
|
|
7
7
|
export interface SyncCallbacks {
|
|
8
8
|
onTick(status: ConnectionStatus, lastSyncAt?: string): void;
|
|
9
9
|
onLog(msg: string): void;
|
|
10
|
+
onInvalidToken(): void;
|
|
10
11
|
}
|
|
11
12
|
export declare class SyncService {
|
|
12
13
|
private readonly api;
|
|
@@ -16,6 +17,7 @@ export declare class SyncService {
|
|
|
16
17
|
private sse;
|
|
17
18
|
private tickInFlight;
|
|
18
19
|
private lastScreensFingerprint;
|
|
20
|
+
private invalidTokenHandled;
|
|
19
21
|
/** Per-process cache: stableId → wallpaperId we successfully applied */
|
|
20
22
|
private readonly applied;
|
|
21
23
|
constructor(api: AgentApi, callbacks: SyncCallbacks);
|
|
@@ -25,6 +27,7 @@ export declare class SyncService {
|
|
|
25
27
|
private scheduleNext;
|
|
26
28
|
private runTick;
|
|
27
29
|
private doTick;
|
|
30
|
+
private notifyInvalidToken;
|
|
28
31
|
private handlePush;
|
|
29
32
|
private reportScreensIfChanged;
|
|
30
33
|
private fetchCommands;
|
package/dist/sync.js
CHANGED
|
@@ -6,6 +6,9 @@ const index_2 = require("./wallpaper/index");
|
|
|
6
6
|
const cache_1 = require("./cache");
|
|
7
7
|
const sse_1 = require("./sse");
|
|
8
8
|
const POLL_INTERVAL_MS = 15_000;
|
|
9
|
+
function isInvalidToken(err) {
|
|
10
|
+
return err instanceof Error && err.message.toLowerCase().includes('invalid device token');
|
|
11
|
+
}
|
|
9
12
|
class SyncService {
|
|
10
13
|
api;
|
|
11
14
|
callbacks;
|
|
@@ -14,6 +17,7 @@ class SyncService {
|
|
|
14
17
|
sse = null;
|
|
15
18
|
tickInFlight = null;
|
|
16
19
|
lastScreensFingerprint = null;
|
|
20
|
+
invalidTokenHandled = false;
|
|
17
21
|
/** Per-process cache: stableId → wallpaperId we successfully applied */
|
|
18
22
|
applied = new Map();
|
|
19
23
|
constructor(api, callbacks) {
|
|
@@ -24,6 +28,7 @@ class SyncService {
|
|
|
24
28
|
this.stop();
|
|
25
29
|
this.context = context;
|
|
26
30
|
this.lastScreensFingerprint = null;
|
|
31
|
+
this.invalidTokenHandled = false;
|
|
27
32
|
this.applied.clear();
|
|
28
33
|
const sseUrl = `${this.api.endpoint.replace('/api/graphql', '')}/api/agent/events?token=${encodeURIComponent(context.token)}`;
|
|
29
34
|
this.sse = (0, sse_1.connectSse)(sseUrl, (type) => {
|
|
@@ -72,6 +77,13 @@ class SyncService {
|
|
|
72
77
|
this.tickInFlight = null;
|
|
73
78
|
}
|
|
74
79
|
}
|
|
80
|
+
notifyInvalidToken() {
|
|
81
|
+
if (this.invalidTokenHandled)
|
|
82
|
+
return;
|
|
83
|
+
this.invalidTokenHandled = true;
|
|
84
|
+
this.stop();
|
|
85
|
+
this.callbacks.onInvalidToken();
|
|
86
|
+
}
|
|
75
87
|
async handlePush() {
|
|
76
88
|
if (this.tickInFlight || !this.context)
|
|
77
89
|
return;
|
|
@@ -95,6 +107,10 @@ class SyncService {
|
|
|
95
107
|
this.lastScreensFingerprint = fp;
|
|
96
108
|
}
|
|
97
109
|
catch (err) {
|
|
110
|
+
if (isInvalidToken(err)) {
|
|
111
|
+
this.notifyInvalidToken();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
98
114
|
this.callbacks.onLog(`[sync] reportScreens failed: ${err}`);
|
|
99
115
|
}
|
|
100
116
|
}
|
|
@@ -106,7 +122,11 @@ class SyncService {
|
|
|
106
122
|
}
|
|
107
123
|
this.callbacks.onTick('online', new Date().toISOString());
|
|
108
124
|
}
|
|
109
|
-
catch {
|
|
125
|
+
catch (err) {
|
|
126
|
+
if (isInvalidToken(err)) {
|
|
127
|
+
this.notifyInvalidToken();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
110
130
|
this.callbacks.onTick('error');
|
|
111
131
|
}
|
|
112
132
|
}
|
|
@@ -116,6 +136,10 @@ class SyncService {
|
|
|
116
136
|
assignments = await this.api.getDeviceScreenAssignments(ctx.token, ctx.deviceId);
|
|
117
137
|
}
|
|
118
138
|
catch (err) {
|
|
139
|
+
if (isInvalidToken(err)) {
|
|
140
|
+
this.notifyInvalidToken();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
119
143
|
this.callbacks.onLog(`[sync] getAssignments failed: ${err}`);
|
|
120
144
|
return;
|
|
121
145
|
}
|