-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver-sync.js
88 lines (76 loc) · 3 KB
/
server-sync.js
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
var assign = require('object-assign')
var BaseSync = require('./base-sync')
var DEFAULT_OPTIONS = {
timeout: 20000,
ping: 10000
}
/**
* Server node in synchronization pair.
*
* Instead of client node, it doesn’t initialize synchronization
* and destroy itself on disconnect.
*
* @param {string|number} nodeId Unique current node name.
* @param {Log} log Logux log instance to sync with other node log.
* @param {Connection} connection Connection to other node.
* @param {object} [options] Synchronization options.
* @param {object} [options.credentials] This node credentials.
* For example, access token.
* @param {authCallback} [options.auth] Function to check
* other node credentials.
* @param {number} [options.timeout=20000] Timeout in milliseconds
* to disconnect connection.
* @param {number} [options.ping=10000] Milliseconds since last message to test
* connection by sending ping.
* @param {filter} [options.inFilter] Function to filter events
* from other client. Best place
* for access control.
* @param {mapper} [options.inMap] Map function to change event
* before put it to current log.
* @param {filter} [options.outFilter] Filter function to select events
* to synchronization.
* @param {mapper} [options.outMap] Map function to change event
* before sending it to other client.
* @param {number[]} [options.subprotocol] Application subprotocol version.
* @param {string} [options.subprotocol] Application subprotocol version
* in SemVer format.
*
* @example
* import { ServerSync } from 'logux-sync'
* startServer(ws => {
* const connection = new ServerConnection(ws)
* const sync = new ServerSync('server' + id, log, connection)
* })
*
* @extends BaseSync
* @class
*/
function ServerSync (nodeId, log, connection, options) {
options = assign({ }, DEFAULT_OPTIONS, options)
BaseSync.call(this, nodeId, log, connection, options)
if (this.options.fixTime) {
throw new Error(
'Server could not fix time. Set opts.fixTime for Client node.')
}
if (options && (options.synced || options.otherSynced)) {
throw new Error(
'Server could not use synced and otherSynced options.')
}
this.state = 'connecting'
}
ServerSync.prototype = {
onConnect: function onConnect () {
BaseSync.prototype.onConnect.call(this)
this.startTimeout()
},
onDisconnect: function onDisconnect () {
BaseSync.prototype.onDisconnect.call(this)
this.destroy()
},
connectMessage: function connectMessage () {
BaseSync.prototype.connectMessage.apply(this, arguments)
this.endTimeout()
}
}
ServerSync.prototype = assign({ }, BaseSync.prototype, ServerSync.prototype)
module.exports = ServerSync