Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 10x 17x 17x 17x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x | /**
* Represent a package of data to send over the network
*/
class Payload
{
/**
* @callback Payload~from
* @param {string} event
* @param {*} data
*/
static from(event, data)
{
return new Payload(event, data)
}
/**
* @param {string} event
* @param {*} data
*/
constructor(event, data)
{
this.event = event
this.data = data
Object.freeze(this)
}
/**
* @returns {string} Stringified JSON
*/
toStringifiedJson()
{
const
event = this.event,
data = this.data
return JSON.stringify({ event, data })
}
/**
* Composes a binary package with a header and body
* @returns {Buffer}
*/
toBuffer()
{
const
dto = this.toStringifiedJson(),
body = Buffer.from(dto),
header = Buffer.alloc(Payload.HEADER_SIZE)
header.writeInt32BE(body.length, 0)
return Buffer.concat([header, body])
}
}
/**
* The byte size of the header
* @const
*/
Payload.HEADER_SIZE = 4
module.exports = Object.freeze(Payload)
|