Skip to content

Commit 3f8f0f7

Browse files
committed
handle case where websocket does not have on method; add tests
1 parent e63e718 commit 3f8f0f7

File tree

4 files changed

+58
-62
lines changed

4 files changed

+58
-62
lines changed

dist/sockjs-esm.js

+31-59
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
22
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
33
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
44
}) : x)(function(x) {
5-
if (typeof require !== "undefined")
6-
return require.apply(this, arguments);
5+
if (typeof require !== "undefined") return require.apply(this, arguments);
76
throw Error('Dynamic require of "' + x + '" is not supported');
87
});
98
var __commonJS = (cb, mod) => function __require2() {
@@ -33,16 +32,14 @@ var require_sockjs = __commonJS({
3332
}
3433
})(function() {
3534
var define2, module2, exports2;
36-
return function() {
35+
return (/* @__PURE__ */ function() {
3736
function r(e, n, t) {
3837
function o(i2, f) {
3938
if (!n[i2]) {
4039
if (!e[i2]) {
4140
var c = "function" == typeof __require && __require;
42-
if (!f && c)
43-
return c(i2, true);
44-
if (u)
45-
return u(i2, true);
41+
if (!f && c) return c(i2, true);
42+
if (u) return u(i2, true);
4643
var a = new Error("Cannot find module '" + i2 + "'");
4744
throw a.code = "MODULE_NOT_FOUND", a;
4845
}
@@ -54,12 +51,11 @@ var require_sockjs = __commonJS({
5451
}
5552
return n[i2].exports;
5653
}
57-
for (var u = "function" == typeof __require && __require, i = 0; i < t.length; i++)
58-
o(t[i]);
54+
for (var u = "function" == typeof __require && __require, i = 0; i < t.length; i++) o(t[i]);
5955
return o;
6056
}
6157
return r;
62-
}()({ 1: [function(require2, module3, exports3) {
58+
}())({ 1: [function(require2, module3, exports3) {
6359
(function(global2) {
6460
(function() {
6561
"use strict";
@@ -2874,7 +2870,7 @@ var require_sockjs = __commonJS({
28742870
exports3.load = load;
28752871
exports3.useColors = useColors;
28762872
exports3.storage = localstorage();
2877-
exports3.destroy = (() => {
2873+
exports3.destroy = /* @__PURE__ */ (() => {
28782874
let warned = false;
28792875
return () => {
28802876
if (!warned) {
@@ -3240,17 +3236,15 @@ var require_sockjs = __commonJS({
32403236
var parser = /([^=?&]+)=?([^&]*)/g, result = {}, part;
32413237
while (part = parser.exec(query)) {
32423238
var key = decode(part[1]), value = decode(part[2]);
3243-
if (key === null || value === null || key in result)
3244-
continue;
3239+
if (key === null || value === null || key in result) continue;
32453240
result[key] = value;
32463241
}
32473242
return result;
32483243
}
32493244
function querystringify(obj, prefix) {
32503245
prefix = prefix || "";
32513246
var pairs = [], value, key;
3252-
if ("string" !== typeof prefix)
3253-
prefix = "?";
3247+
if ("string" !== typeof prefix) prefix = "?";
32543248
for (key in obj) {
32553249
if (has.call(obj, key)) {
32563250
value = obj[key];
@@ -3259,8 +3253,7 @@ var require_sockjs = __commonJS({
32593253
}
32603254
key = encodeURIComponent(key);
32613255
value = encodeURIComponent(value);
3262-
if (key === null || value === null)
3263-
continue;
3256+
if (key === null || value === null) continue;
32643257
pairs.push(key + "=" + value);
32653258
}
32663259
}
@@ -3273,8 +3266,7 @@ var require_sockjs = __commonJS({
32733266
module3.exports = function required(port, protocol) {
32743267
protocol = protocol.split(":")[0];
32753268
port = +port;
3276-
if (!port)
3277-
return false;
3269+
if (!port) return false;
32783270
switch (protocol) {
32793271
case "http":
32803272
case "ws":
@@ -3321,27 +3313,21 @@ var require_sockjs = __commonJS({
33213313
var ignore = { hash: 1, query: 1 };
33223314
function lolcation(loc) {
33233315
var globalVar;
3324-
if (typeof window !== "undefined")
3325-
globalVar = window;
3326-
else if (typeof global2 !== "undefined")
3327-
globalVar = global2;
3328-
else if (typeof self !== "undefined")
3329-
globalVar = self;
3330-
else
3331-
globalVar = {};
3316+
if (typeof window !== "undefined") globalVar = window;
3317+
else if (typeof global2 !== "undefined") globalVar = global2;
3318+
else if (typeof self !== "undefined") globalVar = self;
3319+
else globalVar = {};
33323320
var location = globalVar.location || {};
33333321
loc = loc || location;
33343322
var finaldestination = {}, type = typeof loc, key;
33353323
if ("blob:" === loc.protocol) {
33363324
finaldestination = new Url(unescape(loc.pathname), {});
33373325
} else if ("string" === type) {
33383326
finaldestination = new Url(loc, {});
3339-
for (key in ignore)
3340-
delete finaldestination[key];
3327+
for (key in ignore) delete finaldestination[key];
33413328
} else if ("object" === type) {
33423329
for (key in loc) {
3343-
if (key in ignore)
3344-
continue;
3330+
if (key in ignore) continue;
33453331
finaldestination[key] = loc[key];
33463332
}
33473333
if (finaldestination.slashes === void 0) {
@@ -3400,8 +3386,7 @@ var require_sockjs = __commonJS({
34003386
};
34013387
}
34023388
function resolve(relative, base) {
3403-
if (relative === "")
3404-
return base;
3389+
if (relative === "") return base;
34053390
var path = (base || "/").split("/").slice(0, -1).concat(relative.split("/")), i = path.length, last = path[i - 1], unshift = false, up = 0;
34063391
while (i--) {
34073392
if (path[i] === ".") {
@@ -3410,16 +3395,13 @@ var require_sockjs = __commonJS({
34103395
path.splice(i, 1);
34113396
up++;
34123397
} else if (up) {
3413-
if (i === 0)
3414-
unshift = true;
3398+
if (i === 0) unshift = true;
34153399
path.splice(i, 1);
34163400
up--;
34173401
}
34183402
}
3419-
if (unshift)
3420-
path.unshift("");
3421-
if (last === "." || last === "..")
3422-
path.push("");
3403+
if (unshift) path.unshift("");
3404+
if (last === "." || last === "..") path.push("");
34233405
return path.join("/");
34243406
}
34253407
function Url(address, location, parser) {
@@ -3433,8 +3415,7 @@ var require_sockjs = __commonJS({
34333415
parser = location;
34343416
location = null;
34353417
}
3436-
if (parser && "function" !== typeof parser)
3437-
parser = qs.parse;
3418+
if (parser && "function" !== typeof parser) parser = qs.parse;
34383419
location = lolcation(location);
34393420
extracted = extractProtocol(address || "", location);
34403421
relative = !extracted.protocol && !extracted.slashes;
@@ -3470,11 +3451,9 @@ var require_sockjs = __commonJS({
34703451
address = address.slice(0, index.index);
34713452
}
34723453
url[key] = url[key] || (relative && instruction[3] ? location[key] || "" : "");
3473-
if (instruction[4])
3474-
url[key] = url[key].toLowerCase();
3454+
if (instruction[4]) url[key] = url[key].toLowerCase();
34753455
}
3476-
if (parser)
3477-
url.query = parser(url.query);
3456+
if (parser) url.query = parser(url.query);
34783457
if (relative && location.slashes && url.pathname.charAt(0) !== "/" && (url.pathname !== "" || location.pathname !== "")) {
34793458
url.pathname = resolve(url.pathname, location.pathname);
34803459
}
@@ -3521,8 +3500,7 @@ var require_sockjs = __commonJS({
35213500
break;
35223501
case "hostname":
35233502
url[part] = value;
3524-
if (url.port)
3525-
value += ":" + url.port;
3503+
if (url.port) value += ":" + url.port;
35263504
url.host = value;
35273505
break;
35283506
case "host":
@@ -3566,25 +3544,21 @@ var require_sockjs = __commonJS({
35663544
}
35673545
for (var i = 0; i < rules.length; i++) {
35683546
var ins = rules[i];
3569-
if (ins[4])
3570-
url[ins[1]] = url[ins[1]].toLowerCase();
3547+
if (ins[4]) url[ins[1]] = url[ins[1]].toLowerCase();
35713548
}
35723549
url.auth = url.password ? url.username + ":" + url.password : url.username;
35733550
url.origin = url.protocol !== "file:" && isSpecial(url.protocol) && url.host ? url.protocol + "//" + url.host : "null";
35743551
url.href = url.toString();
35753552
return url;
35763553
}
35773554
function toString(stringify) {
3578-
if (!stringify || "function" !== typeof stringify)
3579-
stringify = qs.stringify;
3555+
if (!stringify || "function" !== typeof stringify) stringify = qs.stringify;
35803556
var query, url = this, host = url.host, protocol = url.protocol;
3581-
if (protocol && protocol.charAt(protocol.length - 1) !== ":")
3582-
protocol += ":";
3557+
if (protocol && protocol.charAt(protocol.length - 1) !== ":") protocol += ":";
35833558
var result = protocol + (url.protocol && url.slashes || isSpecial(url.protocol) ? "//" : "");
35843559
if (url.username) {
35853560
result += url.username;
3586-
if (url.password)
3587-
result += ":" + url.password;
3561+
if (url.password) result += ":" + url.password;
35883562
result += "@";
35893563
} else if (url.password) {
35903564
result += ":" + url.password;
@@ -3597,10 +3571,8 @@ var require_sockjs = __commonJS({
35973571
}
35983572
result += host + url.pathname;
35993573
query = "object" === typeof url.query ? stringify(url.query) : url.query;
3600-
if (query)
3601-
result += "?" !== query.charAt(0) ? "?" + query : query;
3602-
if (url.hash)
3603-
result += url.hash;
3574+
if (query) result += "?" !== query.charAt(0) ? "?" + query : query;
3575+
if (url.hash) result += url.hash;
36043576
return result;
36053577
}
36063578
Url.prototype = { set, toString };

lib/transport/websocket.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ function WebSocketTransport(transUrl, ignore, options) {
5454
self.emit('close', 1006, 'WebSocket connection broken');
5555
self._cleanup();
5656
};
57-
this.ws.on('drain', function() {
58-
self.emit('drain');
59-
})
57+
if (this.ws.on)
58+
this.ws.on('drain', function() {
59+
self.emit('drain');
60+
});
6061
}
6162

6263
inherits(WebSocketTransport, EventEmitter);

tests/lib/senders.js

+18
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ function ajaxStreaming (Obj) {
6363
});
6464
}
6565

66+
function ajaxCustomHeaders (Obj) {
67+
it('simple', function (done) {
68+
var x = new Obj('GET', testUtils.getSameOriginUrl() + '/echo-custom-data-header.txt', null,
69+
{ headers: { 'X-Custom-Data': 'abc' }});
70+
x.on('finish', function (status, text) {
71+
try {
72+
expect(text).to.equal('abc');
73+
} catch (e) {
74+
done(e);
75+
return;
76+
}
77+
done();
78+
});
79+
});
80+
}
81+
6682
function wrongUrl(Obj, url, statuses) {
6783
it('wrong url ' + url, function (done) {
6884
var test = this.runnable();
@@ -110,6 +126,7 @@ describe('Senders', function () {
110126
describe('xhr-local', function () {
111127
ajaxSimple(XhrLocal);
112128
ajaxStreaming(XhrLocal);
129+
ajaxCustomHeaders(XhrLocal);
113130
// TODO senders don't have a timeouts so these tests can fail
114131
// BUT info-receiver has a timeout so they will never not-return
115132
// wrongPort(XhrLocal);
@@ -123,6 +140,7 @@ describe('Senders', function () {
123140
}
124141
ajaxSimple(Xdr);
125142
ajaxStreaming(Xdr);
143+
ajaxCustomHeaders(Xdr);
126144
wrongPort(Xdr);
127145
wrongUrl(Xdr, testUtils.getSameOriginUrl() + '/wrong_url_indeed.txt', [0, 400]);
128146
});

tests/support/sockjs_server.js

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ function startServer(port, config, prefix) {
4242
res.setHeader('Access-Control-Allow-Origin', '*');
4343
res.writeHead(200);
4444
res.end(new Array(2049).join('a') + '\nb\n');
45+
} else if ( /\/echo-custom-data-header.txt/.test(req.url) ) {
46+
res.setHeader('content-type', 'text/plain');
47+
res.setHeader('Access-Control-Allow-Origin', '*');
48+
res.writeHead(200);
49+
res.end(req.headers['x-custom-data'] || '');
4550
} else if (req.url === '/config.js') {
4651
if (req.headers.referer) {
4752
var parsedOrigin = new URL(req.headers.referer);

0 commit comments

Comments
 (0)