UNPKG

897 BJavaScriptView Raw
1'use strict'
2
3const stream = require('../lib/destroyable-stream')
4
5class SystemInfo {
6 constructor (systemInfo) {
7 this.clock = systemInfo.clock
8
9 const hrtime = this.clock.hrtime
10 const unixtime = this.clock.unixtime
11 // calcluate clock offset, but converting hrtime to milliseconds
12 // and substracting it from the unixtime
13 const hrtimeMS = (hrtime[0] * 1e3 + hrtime[1] * 1e-6)
14 this.clockOffset = unixtime - hrtimeMS
15 }
16}
17
18class SystemInfoDecoder extends stream.Transform {
19 constructor () {
20 super({
21 readableObjectMode: true,
22 writableObjectMode: false
23 })
24
25 this._data = []
26 }
27
28 _transform (chunk, encoding, callback) {
29 this._data.push(chunk)
30 callback(null)
31 }
32
33 _flush (callback) {
34 this.push(
35 new SystemInfo(JSON.parse(Buffer.concat(this._data).toString()))
36 )
37 callback(null)
38 }
39}
40module.exports = SystemInfoDecoder