fostrom 0.0.18 → 0.0.20
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/dl-agent.sh +1 -1
- package/index.js +31 -30
- package/package.json +1 -1
package/dl-agent.sh
CHANGED
package/index.js
CHANGED
|
@@ -40,6 +40,7 @@ export default class Fostrom {
|
|
|
40
40
|
#log = true
|
|
41
41
|
#creds = {}
|
|
42
42
|
#sseBuffer = ""
|
|
43
|
+
#sseEvent = {}
|
|
43
44
|
#sseReq = null
|
|
44
45
|
#reconnectTimer = null
|
|
45
46
|
#stopped = true
|
|
@@ -52,7 +53,7 @@ export default class Fostrom {
|
|
|
52
53
|
onMail = async mail => {
|
|
53
54
|
if (this.#log) {
|
|
54
55
|
console.warn(`[Fostrom] Received Mail (Mailbox Size: ${mail.mailbox_size}): ${mail.name} -> ID ${mail.id}`)
|
|
55
|
-
console.warn(" Auto-Acknowledging Mail. Define Mail Handler to handle incoming mail.\n `fostrom.
|
|
56
|
+
console.warn(" Auto-Acknowledging Mail. Define Mail Handler to handle incoming mail.\n `fostrom.onMail = async (mail) => { ...; await mail.ack(); }`\n")
|
|
56
57
|
}
|
|
57
58
|
await mail.ack()
|
|
58
59
|
}
|
|
@@ -192,6 +193,8 @@ export default class Fostrom {
|
|
|
192
193
|
}
|
|
193
194
|
} catch { }
|
|
194
195
|
this.#sseReq = null
|
|
196
|
+
this.#sseBuffer = ""
|
|
197
|
+
this.#sseEvent = {}
|
|
195
198
|
const doStop = (stopAgent === null) ? this.#stopAgentOnExit : Boolean(stopAgent)
|
|
196
199
|
if (doStop) Fostrom.stopAgent()
|
|
197
200
|
}
|
|
@@ -281,6 +284,8 @@ export default class Fostrom {
|
|
|
281
284
|
async #open_event_stream() {
|
|
282
285
|
if (this.#stopped) return
|
|
283
286
|
if (this.#sseReq) return
|
|
287
|
+
this.#sseBuffer = ""
|
|
288
|
+
this.#sseEvent = {}
|
|
284
289
|
const { fleet_id, device_id } = this.#creds
|
|
285
290
|
const options = {
|
|
286
291
|
socketPath: Fostrom.#SOCK,
|
|
@@ -294,38 +299,34 @@ export default class Fostrom {
|
|
|
294
299
|
}
|
|
295
300
|
}
|
|
296
301
|
|
|
302
|
+
const scheduleReconnect = (delay) => {
|
|
303
|
+
this.#sseReq = null
|
|
304
|
+
this.#sseBuffer = ""
|
|
305
|
+
this.#sseEvent = {}
|
|
306
|
+
if (!this.#stopped) {
|
|
307
|
+
this.#reconnectTimer = setTimeout(() => this.#open_event_stream(), delay)
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
297
311
|
const req = http.request(options, (res) => {
|
|
298
312
|
res.setEncoding('utf8')
|
|
299
313
|
res.on('data', (chunk) => {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
res.on('aborted', () => {
|
|
309
|
-
this.#sseReq = null
|
|
310
|
-
if (!this.#stopped) {
|
|
311
|
-
this.#reconnectTimer = setTimeout(() => this.#open_event_stream(), 500)
|
|
312
|
-
}
|
|
313
|
-
})
|
|
314
|
-
res.on('end', () => {
|
|
315
|
-
this.#sseReq = null
|
|
316
|
-
if (!this.#stopped) {
|
|
317
|
-
this.#reconnectTimer = setTimeout(() => this.#open_event_stream(), 250)
|
|
318
|
-
}
|
|
314
|
+
const parsed = Fostrom.#parse_events(
|
|
315
|
+
this.#sseBuffer,
|
|
316
|
+
chunk,
|
|
317
|
+
this.#sseEvent,
|
|
318
|
+
this.#event_handler.bind(this)
|
|
319
|
+
)
|
|
320
|
+
this.#sseBuffer = parsed.buffer
|
|
321
|
+
this.#sseEvent = parsed.event
|
|
319
322
|
})
|
|
323
|
+
res.on('error', () => scheduleReconnect(500))
|
|
324
|
+
res.on('aborted', () => scheduleReconnect(500))
|
|
325
|
+
res.on('close', () => scheduleReconnect(500))
|
|
326
|
+
res.on('end', () => scheduleReconnect(250))
|
|
320
327
|
})
|
|
321
328
|
|
|
322
|
-
req.on('error', (_err) =>
|
|
323
|
-
this.#sseReq = null
|
|
324
|
-
if (!this.#stopped) {
|
|
325
|
-
this.#reconnectTimer = setTimeout(() => this.#open_event_stream(), 500)
|
|
326
|
-
}
|
|
327
|
-
})
|
|
328
|
-
|
|
329
|
+
req.on('error', (_err) => scheduleReconnect(500))
|
|
329
330
|
req.end()
|
|
330
331
|
this.#sseReq = req
|
|
331
332
|
}
|
|
@@ -387,12 +388,12 @@ export default class Fostrom {
|
|
|
387
388
|
})
|
|
388
389
|
}
|
|
389
390
|
|
|
390
|
-
static #parse_events(buffer, chunk, event_handler) {
|
|
391
|
+
static #parse_events(buffer, chunk, currentEvent, event_handler) {
|
|
391
392
|
buffer += chunk
|
|
392
393
|
const lines = buffer.split('\n')
|
|
393
394
|
buffer = lines.pop() || ''
|
|
394
395
|
|
|
395
|
-
let event = {}
|
|
396
|
+
let event = currentEvent || {}
|
|
396
397
|
for (const raw of lines) {
|
|
397
398
|
const line = raw.replace(/\r$/, '')
|
|
398
399
|
if (line === '') {
|
|
@@ -411,7 +412,7 @@ export default class Fostrom {
|
|
|
411
412
|
}
|
|
412
413
|
}
|
|
413
414
|
|
|
414
|
-
return buffer
|
|
415
|
+
return { buffer, event }
|
|
415
416
|
}
|
|
416
417
|
|
|
417
418
|
async #deliverMail(mail) {
|