Repository navigation
Expand file tree
/
Copy pathCommLink.js
More file actions
439 lines (339 loc) · 15.3 KB
/
Copy pathCommLink.js
File metadata and controls
439 lines (339 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* CommLink.js
- Version: 1.1.1
- Author: Haka
- Description: A userscript library for cross-window communication via the userscript storage
- GitHub: https://github.com/AugmentedWeb/CommLink
*/
// How long after the last real (non keep-alive) packet the 1 ms fast polling stays on
const COMMLINK_FAST_POLL_WINDOW_MS = 500;
// A packet this old was abandoned by its sender; it is deleted and must not be handled
const COMMLINK_PACKET_EXPIRY_MS = 2e4;
// Idle poll interval when a peer that cannot ring the bell (older userscript) may exist
const COMMLINK_LEGACY_IDLE_POLL_MS = 10;
// Idle safety-net poll when every peer rings the bell
const COMMLINK_BELL_IDLE_POLL_MS = 250;
// How long one packet that arrived without a bell keeps the legacy idle poll active
const COMMLINK_LEGACY_PEER_MEMORY_MS = 60000;
class CommLinkHandler {
constructor(commlinkID, configObj) {
this.commlinkID = commlinkID;
this.singlePacketResponseWaitTime = configObj?.singlePacketResponseWaitTime || 1500;
this.maxSendAttempts = configObj?.maxSendAttempts || 3;
this.statusCheckInterval = configObj?.statusCheckInterval || 1;
this.silentMode = configObj?.silentMode || false;
this.commlinkValueIndicator = 'commlink-packet-';
this.commands = {};
this.listeners = [];
this.greasy = typeof GM === 'object' ? GM : {};
const getFunction = (funcNames, methodName) => {
for(const func of funcNames) {
if(typeof func === 'function') {
return func;
}
}
if(!this.silentMode) {
throw new Error(`No valid method found for ${methodName}`);
}
};
const getValueMethod = getFunction(
[
typeof GM_getValue !== 'undefined' ? GM_getValue : undefined,
this.greasy?.getValue,
configObj?.functions?.getValue
],
'getValue'
);
const setValueMethod = getFunction(
[
typeof GM_setValue !== 'undefined' ? GM_setValue : undefined,
this.greasy?.setValue,
configObj?.functions?.setValue
],
'setValue'
);
const deleteValueMethod = getFunction(
[
typeof GM_deleteValue !== 'undefined' ? GM_deleteValue : undefined,
this.greasy?.deleteValue,
configObj?.functions?.deleteValue
],
'deleteValue'
);
const listValuesMethod = getFunction(
[
typeof GM_listValues !== 'undefined' ? GM_listValues : undefined,
this.greasy?.listValues,
configObj?.functions?.listValues
],
'listValues'
);
// A storage call that never answers must not hang a polling loop forever.
this.storageTimeoutMs = configObj?.storageTimeoutMs || 3000;
const withTimeout = (promiseLike) => {
let timer;
const timeout = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error('CommLink storage call timed out')), this.storageTimeoutMs);
});
return Promise.race([Promise.resolve(promiseLike), timeout]).finally(() => clearTimeout(timer));
};
this.storage = {
getValue: async (key) => {
return await withTimeout(getValueMethod(key));
},
setValue: (key, value) => {
return withTimeout(setValueMethod(key, value));
},
deleteValue: (key) => {
return withTimeout(deleteValueMethod(key));
},
listValues: async () => {
return await withTimeout(listValuesMethod());
}
};
this.lastStaleCleanup = Date.now();
// Event-driven wake-up through a single shared "bell" key, used only if the manager
// supports value change listeners. Peers without the update never ring the bell, so
// until a ring is actually seen we keep polling for them (see getPollDelay).
this.bellKey = 'commlink-bell';
this.bellConfirmed = false;
this.pendingWake = false;
this.wake = null;
this.lastActivity = 0;
this.burstMs = configObj?.burstMs || COMMLINK_FAST_POLL_WINDOW_MS;
this.legacyIdleIntervalMs = configObj?.legacyIdleIntervalMs || COMMLINK_LEGACY_IDLE_POLL_MS;
this.bellFallbackIntervalMs = configObj?.bellFallbackIntervalMs || COMMLINK_BELL_IDLE_POLL_MS;
this.lastBellAt = 0;
this.legacyPeerSeenAt = 0;
this.receiverRunning = false;
// Keep-alive traffic must not keep the fast 1 ms polling on forever
this.keepAliveCommands = configObj?.keepAliveCommands || ['createInstance', 'ping'];
const addValueChangeListenerMethod = [
typeof GM_addValueChangeListener !== 'undefined' ? GM_addValueChangeListener : undefined,
this.greasy?.addValueChangeListener,
configObj?.functions?.addValueChangeListener
].find(func => typeof func === 'function');
this.hasBell = false;
if(addValueChangeListenerMethod) {
try {
const bell = CommLinkHandler.__bell || (CommLinkHandler.__bell = { subscribers: new Set(), registered: false });
bell.subscribers.add(this);
if(!bell.registered) {
bell.registered = true;
const result = addValueChangeListenerMethod(this.bellKey, () => {
bell.subscribers.forEach(handler => handler.onBell());
});
// Async APIs (GM.addValueChangeListener) may reject
if(result && typeof result.catch === 'function') result.catch(() => { bell.registered = false; });
}
this.hasBell = true;
} catch(e) {
this.hasBell = false;
}
}
if(typeof GM_info !== 'undefined') {
const grants = (GM_info?.script?.grant) || [];
const missingGrants = ['getValue', 'setValue', 'deleteValue', 'listValues']
.filter(grant => !grants.some(g => g.endsWith(grant)));
if(missingGrants.length > 0 && !this.silentMode)
alert(`[CommLink] The following userscript grants are missing: ${missingGrants.join(', ')}. CommLink might not work.`);
}
this.removeOldPackets().catch(() => {});
}
async removeOldPackets(packets) {
packets = packets || await this.getStoredPackets();
packets.filter(packet => Date.now() - packet?.date > COMMLINK_PACKET_EXPIRY_MS)
.forEach(packet => this.removePacketByID(packet.id));
}
onBell() {
this.bellConfirmed = true;
this.lastBellAt = Date.now();
this.pendingWake = true;
if(this.wake) this.wake();
}
ringBell() {
if(!this.hasBell) return;
try {
Promise.resolve(this.storage.setValue(this.bellKey, { t: Date.now(), r: Math.random() })).catch(() => {});
} catch(e) {}
}
getPollDelay() {
if(Date.now() - this.lastActivity < this.burstMs) return this.statusCheckInterval;
// A peer that rings the bell is answered instantly; the timer is only a safety net.
// unless a packet recently showed up without any bell (an older peer is present)
if(this.bellConfirmed && Date.now() - this.legacyPeerSeenAt > COMMLINK_LEGACY_PEER_MEMORY_MS)
return this.bellFallbackIntervalMs;
return Math.max(this.legacyIdleIntervalMs, this.statusCheckInterval);
}
startReceiver() {
if(this.receiverRunning) return;
this.receiverRunning = true;
const loop = async () => {
while(this.receiverRunning) {
let delay;
try {
this.pendingWake = false;
const wasIdle = Date.now() - this.lastActivity >= this.burstMs;
if(await this.receivePackets()) {
// Found by the idle timer with no bell shortly before: its sender cannot ring
if(wasIdle && !this.pendingWake && Date.now() - this.lastBellAt > 100)
this.legacyPeerSeenAt = Date.now();
this.lastActivity = Date.now();
}
delay = this.getPollDelay();
} catch(e) {
delay = 50; // back off, never a hot loop
}
if(this.pendingWake) continue;
await new Promise(resolve => {
const timer = setTimeout(() => { this.wake = null; resolve(); }, delay);
this.wake = () => { clearTimeout(timer); this.wake = null; resolve(); };
});
}
};
loop();
}
setIntervalAsync(callback, interval = this.statusCheckInterval) {
let running = true;
async function loop() {
while(running) {
try {
await callback();
await new Promise((resolve) => setTimeout(resolve, interval));
} catch (e) {
// Back off, otherwise a failing callback becomes a hot loop.
await new Promise((resolve) => setTimeout(resolve, Math.max(interval, 50)));
}
}
};
loop();
return { stop: () => running = false };
}
getUniqueID() {
return ([1e7]+-1e3+4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
)
}
getCommKey(packetID) {
return this.commlinkValueIndicator + packetID;
}
async getStoredPackets() {
const keys = await this.storage.listValues();
const storedPackets = [];
for(const key of keys) {
if(key.includes(this.commlinkValueIndicator)) {
let value;
try {
value = await this.storage.getValue(key);
} catch(e) {
continue;
}
if(value && typeof value === 'object') storedPackets.push(value);
}
}
return storedPackets;
}
addPacket(packet) {
try { Promise.resolve(this.storage.setValue(this.getCommKey(packet.id), packet)).catch(() => {}); } catch(e) {}
}
removePacketByID(packetID) {
try { Promise.resolve(this.storage.deleteValue(this.getCommKey(packetID))).catch(() => {}); } catch(e) {}
}
async findPacketByID(packetID) {
return await this.storage.getValue(this.getCommKey(packetID));
}
async editPacket(newPacket) {
// The sender may have given up and deleted the packet already. Writing
// the result then would re-create it as a packet nobody ever removes.
if(!await this.findPacketByID(newPacket.id)) return;
try { await this.storage.setValue(this.getCommKey(newPacket.id), newPacket); } catch(e) {}
}
send(platform, cmd, d) {
return new Promise(async resolve => {
const packetWaitTimeMs = this.singlePacketResponseWaitTime;
const maxAttempts = this.maxSendAttempts;
let attempts = 0;
for(;;) {
attempts++;
const packetID = this.getUniqueID();
const attemptStartDate = Date.now();
const packet = { sender: platform, id: packetID, command: cmd, data: d, date: attemptStartDate };
if(!this.silentMode)
console.log(`[CommLink Sender] Sending packet! (#${attempts} attempt):`, packet);
this.addPacket(packet);
this.ringBell();
if(!this.keepAliveCommands.includes(cmd)) this.lastActivity = Date.now();
for(;;) {
let poolPacket;
try {
poolPacket = await this.findPacketByID(packetID);
} catch(e) {
poolPacket = null; // storage unavailable: treat as no reply
}
const packetResult = poolPacket?.result;
// false, 0 and an empty string are replies too, not missing acknowledgements.
if(poolPacket && packetResult != null) {
if(!this.silentMode)
console.log(`[CommLink Sender] Got result for a packet (${packetID}):`, packetResult);
resolve(poolPacket.result);
attempts = maxAttempts; // stop main loop
break;
}
if(!poolPacket || Date.now() - attemptStartDate > packetWaitTimeMs) {
break;
}
await new Promise(res => setTimeout(res, this.statusCheckInterval));
}
this.removePacketByID(packetID);
if(attempts == maxAttempts) {
break;
}
}
return resolve(null);
});
}
registerSendCommand(name, obj) {
this.commands[name] = async data => await this.send(obj?.commlinkID || this.commlinkID , name, obj?.data || data);
}
registerListener(sender, commandHandler) {
const listener = { sender, commandHandler };
this.listeners.push(listener);
// One shared receive loop per handler, not one per listener
this.startReceiver();
}
async receivePackets() {
const packets = await this.getStoredPackets();
if(Date.now() - this.lastStaleCleanup > 1e4) {
this.lastStaleCleanup = Date.now();
this.removeOldPackets(packets).catch(() => {});
}
let handledAmount = 0;
for(const packet of packets) {
// e.g. a throttled background tab resuming: the reply could no longer be delivered anyway
if(Date.now() - packet?.date > COMMLINK_PACKET_EXPIRY_MS) continue;
for(const listener of this.listeners) {
if(packet.sender === listener.sender && !packet.hasOwnProperty('result')) {
try {
const result = await listener.commandHandler(packet);
packet.result = result === undefined ? null : result;
if(!this.keepAliveCommands.includes(packet.command)) handledAmount++;
await this.editPacket(packet);
if(!this.silentMode) {
if(packet.result == null)
console.log('[CommLink Receiver] Possibly failed to handle packet:', packet);
else
console.log('[CommLink Receiver] Successfully handled a packet:', packet);
}
} catch(error) {
if(!this.silentMode) console.error('[CommLink Receiver] Error handling packet:', error);
}
}
}
}
return handledAmount;
}
kill() {
this.receiverRunning = false;
if(this.wake) this.wake();
CommLinkHandler.__bell?.subscribers.delete(this);
}
}