Debian Codegraph @

debian/missing-sources/livereload.js/connector.js

raw
1 const { Parser, PROTOCOL_6, PROTOCOL_7 } = require('./protocol');
2
3 const VERSION = process.env.npm_package_version;
4
5 class Connector {
6 constructor (options, WebSocket, Timer, handlers) {
7 this.options = options;
8 this.WebSocket = WebSocket;
9 this.Timer = Timer;
10 this.handlers = handlers;
11 const path = this.options.path ? `${this.options.path}` : 'livereload';
12 this._uri = `ws${this.options.https ? 's' : ''}://${this.options.host}:${this.options.port}/${path}`;
13
14 this._nextDelay = this.options.mindelay;
15 this._connectionDesired = false;
16 this.protocol = 0;
17
18 this.protocolParser = new Parser({
19 connected: protocol => {
20 this.protocol = protocol;
21 this._handshakeTimeout.stop();
22 this._nextDelay = this.options.mindelay;
23 this._disconnectionReason = 'broken';
24
25 return this.handlers.connected(this.protocol);
26 },
27 error: e => {
28 this.handlers.error(e);
29
30 return this._closeOnError();
31 },
32 message: message => {
33 return this.handlers.message(message);
34 }
35 });
36
37 this._handshakeTimeout = new this.Timer(() => {
38 if (!this._isSocketConnected()) {
39 return;
40 }
41
42 this._disconnectionReason = 'handshake-timeout';
43
44 return this.socket.close();
45 });
46
47 this._reconnectTimer = new this.Timer(() => {
48 if (!this._connectionDesired) {
49 // shouldn't hit this, but just in case
50 return;
51 }
52
53 return this.connect();
54 });
55
56 this.connect();
57 }
58
59 _isSocketConnected () {
60 return this.socket && (this.socket.readyState === this.WebSocket.OPEN);
61 }
62
63 connect () {
64 this._connectionDesired = true;
65
66 if (this._isSocketConnected()) {
67 return;
68 }
69
70 // prepare for a new connection
71 this._reconnectTimer.stop();
72 this._disconnectionReason = 'cannot-connect';
73 this.protocolParser.reset();
74
75 this.handlers.connecting();
76
77 this.socket = new this.WebSocket(this._uri);
78 this.socket.onopen = e => this._onopen(e);
79 this.socket.onclose = e => this._onclose(e);
80 this.socket.onmessage = e => this._onmessage(e);
81 this.socket.onerror = e => this._onerror(e);
82 }
83
84 disconnect () {
85 this._connectionDesired = false;
86 this._reconnectTimer.stop(); // in case it was running
87
88 if (!this._isSocketConnected()) {
89 return;
90 }
91
92 this._disconnectionReason = 'manual';
93
94 return this.socket.close();
95 }
96
97 _scheduleReconnection () {
98 if (!this._connectionDesired) {
99 // don't reconnect after manual disconnection
100 return;
101 }
102
103 if (!this._reconnectTimer.running) {
104 this._reconnectTimer.start(this._nextDelay);
105 this._nextDelay = Math.min(this.options.maxdelay, this._nextDelay * 2);
106 }
107 }
108
109 sendCommand (command) {
110 if (!this.protocol) {
111 return;
112 }
113
114 return this._sendCommand(command);
115 }
116
117 _sendCommand (command) {
118 return this.socket.send(JSON.stringify(command));
119 }
120
121 _closeOnError () {
122 this._handshakeTimeout.stop();
123 this._disconnectionReason = 'error';
124 return this.socket.close();
125 }
126
127 _onopen (e) {
128 this.handlers.socketConnected();
129 this._disconnectionReason = 'handshake-failed';
130
131 // start handshake
132 const hello = { command: 'hello', protocols: [PROTOCOL_6, PROTOCOL_7] };
133 hello.ver = VERSION;
134
135 if (this.options.ext) {
136 hello.ext = this.options.ext;
137 }
138
139 if (this.options.extver) {
140 hello.extver = this.options.extver;
141 }
142
143 if (this.options.snipver) {
144 hello.snipver = this.options.snipver;
145 }
146
147 this._sendCommand(hello);
148
149 return this._handshakeTimeout.start(this.options.handshake_timeout);
150 }
151
152 _onclose (e) {
153 this.protocol = 0;
154 this.handlers.disconnected(this._disconnectionReason, this._nextDelay);
155
156 return this._scheduleReconnection();
157 }
158
159 _onerror (e) {}
160
161 _onmessage (e) {
162 return this.protocolParser.process(e.data);
163 }
164 };
165
166 exports.Connector = Connector;