forked from logux/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
145 lines (129 loc) · 3.35 KB
/
client.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var ServerSync = require('logux-sync').ServerSync
var SyncError = require('logux-sync').SyncError
var semver = require('semver')
var remoteAddress = require('./remote-address')
/**
* Logux client connected to server.
*
* @param {Server} app The server.
* @param {ServerConnection} connection The Logux connection.
* @param {number} key Client number used as `app.clients` key.
*
* @example
* const client = app.clients[0]
*
* @class
*/
function Client (app, connection, key) {
this.app = app
/**
* The Logux wrapper to WebSocket connection.
* @type {ServerConnection}
*
* @example
* console.log(client.connection.ws.upgradeReq.headers)
*/
this.connection = connection
/**
* Client number used as `app.clients` key.
* @type {string}
*
* @example
* function stillConnected (client) {
* return !!app.clients[client.key]
* }
*/
this.key = key.toString()
/**
* Client IP address.
* @type {string}
*
* @example
* var clientCity = detectLocation(client.remoteAddress)
*/
this.remoteAddress = remoteAddress(this.connection.ws)
/**
* Sync instance from `logux-sync` to synchronize logs.
* @type {ServerSync}
*
* @example
* if (client.sync.state === 'synchronized')
*/
this.sync = new ServerSync(app.nodeId, app.log, connection, {
subprotocol: app.options.subprotocol,
timeout: app.options.timeout,
ping: app.options.ping,
auth: this.auth.bind(this)
})
var client = this
this.sync.catch(function (err) {
client.app.reporter('syncError', client.app, client, err)
})
this.sync.on('connect', function () {
if (!client.isSubprotocol(client.app.options.supports)) {
throw new SyncError(client.sync, 'wrong-subprotocol', {
supported: client.app.options.supports,
used: client.sync.otherSubprotocol
})
}
})
this.sync.on('state', function () {
if (!client.sync.connected && !client.destroyed) client.destroy()
})
this.sync.on('clientError', function (err) {
client.app.reporter('clientError', client.app, client, err)
})
}
Client.prototype = {
/**
* Developer defined user object. It should have at least
* @type {object}
*/
user: undefined,
/**
* Check client subprotocol version. It uses `semver` npm package
* to parse requirements.
*
* @param {string} range npm’s version requirements.
*
* @return {boolean} Is version satisfies requirements.
*
* @example
* if (client.isSubprotocol('4.x')) {
* useOldAPI()
* }
*/
isSubprotocol: function isSubprotocol (range) {
return semver.satisfies(this.sync.otherSubprotocol, range)
},
/**
* Disconnect client.
*
* @return {undefined}
*/
destroy: function destroy () {
this.destroyed = true
this.app.reporter('disconnect', this.app, this)
if (this.sync.connected) this.sync.destroy()
delete this.app.clients[this.key]
},
auth: function auth (credentials, nodeId) {
/**
* Unique node name.
* @type {string|number}
*/
this.nodeId = nodeId
var client = this
return this.app.authenticator(credentials, nodeId, this)
.then(function (user) {
if (user) {
client.user = user
client.app.reporter('authenticated', client.app, client)
return true
} else {
return false
}
})
}
}
module.exports = Client