forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_client_common.js
266 lines (213 loc) · 7.57 KB
/
stream_client_common.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// XXX from Underscore.String (http://epeli.github.com/underscore.string/)
var startsWith = function(str, starts) {
return str.length >= starts.length &&
str.substring(0, starts.length) === starts;
};
var endsWith = function(str, ends) {
return str.length >= ends.length &&
str.substring(str.length - ends.length) === ends;
};
// @param url {String} URL to Meteor app, eg:
// "/" or "madewith.meteor.com" or "https://foo.meteor.com"
// or "ddp+sockjs://ddp--****-foo.meteor.com/sockjs"
// @returns {String} URL to the endpoint with the specific scheme and subPath, e.g.
// for scheme "http" and subPath "sockjs"
// "http://subdomain.meteor.com/sockjs" or "/sockjs"
// or "https://ddp--1234-foo.meteor.com/sockjs"
var translateUrl = function(url, newSchemeBase, subPath) {
if (! newSchemeBase) {
newSchemeBase = "http";
}
var ddpUrlMatch = url.match(/^ddp(i?)\+sockjs:\/\//);
var httpUrlMatch = url.match(/^http(s?):\/\//);
var newScheme;
if (ddpUrlMatch) {
// Remove scheme and split off the host.
var urlAfterDDP = url.substr(ddpUrlMatch[0].length);
newScheme = ddpUrlMatch[1] === "i" ? newSchemeBase : newSchemeBase + "s";
var slashPos = urlAfterDDP.indexOf('/');
var host =
slashPos === -1 ? urlAfterDDP : urlAfterDDP.substr(0, slashPos);
var rest = slashPos === -1 ? '' : urlAfterDDP.substr(slashPos);
// In the host (ONLY!), change '*' characters into random digits. This
// allows different stream connections to connect to different hostnames
// and avoid browser per-hostname connection limits.
host = host.replace(/\*/g, function () {
return Math.floor(Random.fraction()*10);
});
return newScheme + '://' + host + rest;
} else if (httpUrlMatch) {
newScheme = !httpUrlMatch[1] ? newSchemeBase : newSchemeBase + "s";
var urlAfterHttp = url.substr(httpUrlMatch[0].length);
url = newScheme + "://" + urlAfterHttp;
}
// Prefix FQDNs but not relative URLs
if (url.indexOf("://") === -1 && !startsWith(url, "/")) {
url = newSchemeBase + "://" + url;
}
// XXX This is not what we should be doing: if I have a site
// deployed at "/foo", then DDP.connect("/") should actually connect
// to "/", not to "/foo". "/" is an absolute path. (Contrast: if
// deployed at "/foo", it would be reasonable for DDP.connect("bar")
// to connect to "/foo/bar").
//
// We should make this properly honor absolute paths rather than
// forcing the path to be relative to the site root. Simultaneously,
// we should set DDP_DEFAULT_CONNECTION_URL to include the site
// root. See also client_convenience.js #RationalizingRelativeDDPURLs
url = Meteor._relativeToSiteRootUrl(url);
if (endsWith(url, "/"))
return url + subPath;
else
return url + "/" + subPath;
};
toSockjsUrl = function (url) {
return translateUrl(url, "http", "sockjs");
};
toWebsocketUrl = function (url) {
var ret = translateUrl(url, "ws", "websocket");
return ret;
};
LivedataTest.toSockjsUrl = toSockjsUrl;
_.extend(LivedataTest.ClientStream.prototype, {
// Register for callbacks.
on: function (name, callback) {
var self = this;
if (name !== 'message' && name !== 'reset' && name !== 'disconnect')
throw new Error("unknown event type: " + name);
if (!self.eventCallbacks[name])
self.eventCallbacks[name] = [];
self.eventCallbacks[name].push(callback);
},
_initCommon: function (options) {
var self = this;
options = options || {};
//// Constants
// how long to wait until we declare the connection attempt
// failed.
self.CONNECT_TIMEOUT = options.connectTimeoutMs || 10000;
self.eventCallbacks = {}; // name -> [callback]
self._forcedToDisconnect = false;
//// Reactive status
self.currentStatus = {
status: "connecting",
connected: false,
retryCount: 0
};
self.statusListeners = typeof Tracker !== 'undefined' && new Tracker.Dependency;
self.statusChanged = function () {
if (self.statusListeners)
self.statusListeners.changed();
};
//// Retry logic
self._retry = new Retry;
self.connectionTimer = null;
},
// Trigger a reconnect.
reconnect: function (options) {
var self = this;
options = options || {};
if (options.url) {
self._changeUrl(options.url);
}
if (options._sockjsOptions) {
self.options._sockjsOptions = options._sockjsOptions;
}
if (self.currentStatus.connected) {
if (options._force || options.url) {
// force reconnect.
self._lostConnection(new DDP.ForcedReconnectError);
} // else, noop.
return;
}
// if we're mid-connection, stop it.
if (self.currentStatus.status === "connecting") {
// Pretend it's a clean close.
self._lostConnection();
}
self._retry.clear();
self.currentStatus.retryCount -= 1; // don't count manual retries
self._retryNow();
},
disconnect: function (options) {
var self = this;
options = options || {};
// Failed is permanent. If we're failed, don't let people go back
// online by calling 'disconnect' then 'reconnect'.
if (self._forcedToDisconnect)
return;
// If _permanent is set, permanently disconnect a stream. Once a stream
// is forced to disconnect, it can never reconnect. This is for
// error cases such as ddp version mismatch, where trying again
// won't fix the problem.
if (options._permanent) {
self._forcedToDisconnect = true;
}
self._cleanup();
self._retry.clear();
self.currentStatus = {
status: (options._permanent ? "failed" : "offline"),
connected: false,
retryCount: 0
};
if (options._permanent && options._error)
self.currentStatus.reason = options._error;
self.statusChanged();
},
// maybeError is set unless it's a clean protocol-level close.
_lostConnection: function (maybeError) {
var self = this;
self._cleanup(maybeError);
self._retryLater(maybeError); // sets status. no need to do it here.
},
// fired when we detect that we've gone online. try to reconnect
// immediately.
_online: function () {
// if we've requested to be offline by disconnecting, don't reconnect.
if (this.currentStatus.status != "offline")
this.reconnect();
},
_retryLater: function (maybeError) {
var self = this;
var timeout = 0;
if (self.options.retry ||
(maybeError && maybeError.errorType === "DDP.ForcedReconnectError")) {
timeout = self._retry.retryLater(
self.currentStatus.retryCount,
_.bind(self._retryNow, self)
);
self.currentStatus.status = "waiting";
self.currentStatus.retryTime = (new Date()).getTime() + timeout;
} else {
self.currentStatus.status = "failed";
delete self.currentStatus.retryTime;
}
self.currentStatus.connected = false;
self.statusChanged();
},
_retryNow: function () {
var self = this;
if (self._forcedToDisconnect)
return;
self.currentStatus.retryCount += 1;
self.currentStatus.status = "connecting";
self.currentStatus.connected = false;
delete self.currentStatus.retryTime;
self.statusChanged();
self._launchConnection();
},
// Get current status. Reactive.
status: function () {
var self = this;
if (self.statusListeners)
self.statusListeners.depend();
return self.currentStatus;
}
});
DDP.ConnectionError = Meteor.makeErrorType(
"DDP.ConnectionError", function (message) {
var self = this;
self.message = message;
});
DDP.ForcedReconnectError = Meteor.makeErrorType(
"DDP.ForcedReconnectError", function () {});