fst – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | var SyncMessage = function (peer, action, jail, protocol, port, ip) { |
2 | // Check if all parameters are defined. |
||
3 | if (!Array.from(arguments).every(isDefined)) |
||
4 | throw 'Argument exception' |
||
5 | |||
6 | // Public |
||
7 | this.peer = peer |
||
8 | this.action = action |
||
9 | this.jail = jail |
||
10 | this.protocol = protocol |
||
11 | this.port = port |
||
12 | this.ip = ip |
||
13 | |||
14 | // Flatten the object to an array. |
||
15 | this.toArray = function () { |
||
16 | // Flatten the payload to an array. |
||
17 | var clone = JSON.parse(JSON.stringify(this)) |
||
18 | return Object.keys(clone).map(function (item) { |
||
19 | return clone[item] |
||
20 | }) |
||
21 | } |
||
22 | |||
23 | // Helper function to check if parameter is defined. |
||
24 | function isDefined(param) { |
||
25 | return param !== null && param !== undefined |
||
26 | } |
||
27 | } |
||
28 | |||
29 | // Deserialize a SyncMessage from JSON. |
||
30 | SyncMessage.fromJSON = function (data) { |
||
31 | const payload = JSON.parse(data) |
||
32 | |||
33 | if (payload === null || typeof payload === undefined) |
||
34 | throw 'Deserialization failed' |
||
35 | |||
36 | for (var key in new SyncMessage()) { |
||
37 | if (typeof SyncMessage[key] !== 'undefined' && !payload.hasOwnProperty(key)) { |
||
38 | throw 'Deserialization failed' |
||
39 | } |
||
40 | } |
||
41 | |||
42 | var syncMessage |
||
43 | |||
44 | try { |
||
45 | syncMessage = new SyncMessage( |
||
46 | payload.peer, |
||
47 | payload.action, |
||
48 | payload.jail, |
||
49 | payload.protocol, |
||
50 | payload.port, |
||
51 | payload.ip |
||
52 | ) |
||
53 | } catch (error) { |
||
54 | throw error |
||
55 | } |
||
56 | |||
57 | return syncMessage |
||
58 | } |
||
59 | |||
60 | module.exports = SyncMessage |