Skip to content

Commit 5f47a50

Browse files
refactor: refactor the handling of the options
1 parent 41a1bcf commit 5f47a50

File tree

10 files changed

+155
-231
lines changed

10 files changed

+155
-231
lines changed

lib/socket.js

Lines changed: 49 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class Socket extends Emitter {
4242
opts.port = this.secure ? "443" : "80";
4343
}
4444

45-
this.agent = opts.agent || false;
4645
this.hostname =
4746
opts.hostname ||
4847
(typeof location !== "undefined" ? location.hostname : "localhost");
@@ -53,62 +52,42 @@ class Socket extends Emitter {
5352
: this.secure
5453
? 443
5554
: 80);
56-
this.query = opts.query || {};
57-
if ("string" === typeof this.query) this.query = parseqs.decode(this.query);
58-
this.upgrade = false !== opts.upgrade;
59-
this.path = (opts.path || "/engine.io").replace(/\/$/, "") + "/";
60-
this.forceJSONP = !!opts.forceJSONP;
61-
this.jsonp = false !== opts.jsonp;
62-
this.forceBase64 = !!opts.forceBase64;
63-
this.enablesXDR = !!opts.enablesXDR;
64-
this.withCredentials = false !== opts.withCredentials;
65-
this.timestampParam = opts.timestampParam || "t";
66-
this.timestampRequests = opts.timestampRequests;
55+
6756
this.transports = opts.transports || ["polling", "websocket"];
68-
this.transportOptions = opts.transportOptions || {};
6957
this.readyState = "";
7058
this.writeBuffer = [];
7159
this.prevBufferLen = 0;
72-
this.policyPort = opts.policyPort || 843;
73-
this.rememberUpgrade = opts.rememberUpgrade || false;
74-
this.binaryType = null;
75-
this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades;
76-
this.perMessageDeflate =
77-
false !== opts.perMessageDeflate ? opts.perMessageDeflate || {} : false;
78-
79-
if (true === this.perMessageDeflate) this.perMessageDeflate = {};
80-
if (this.perMessageDeflate && null == this.perMessageDeflate.threshold) {
81-
this.perMessageDeflate.threshold = 1024;
82-
}
8360

84-
// SSL options for Node.js client
85-
this.pfx = opts.pfx || null;
86-
this.key = opts.key || null;
87-
this.passphrase = opts.passphrase || null;
88-
this.cert = opts.cert || null;
89-
this.ca = opts.ca || null;
90-
this.ciphers = opts.ciphers || null;
91-
this.rejectUnauthorized =
92-
opts.rejectUnauthorized === undefined ? true : opts.rejectUnauthorized;
93-
this.forceNode = !!opts.forceNode;
61+
this.opts = Object.assign(
62+
{
63+
path: "/engine.io",
64+
agent: false,
65+
upgrade: true,
66+
jsonp: true,
67+
timestampParam: "t",
68+
policyPort: 843,
69+
rememberUpgrade: false,
70+
rejectUnauthorized: true,
71+
perMessageDeflate: {
72+
threshold: 1024
73+
},
74+
transportOptions: {}
75+
},
76+
opts
77+
);
78+
79+
this.opts.path = this.opts.path.replace(/\/$/, "") + "/";
80+
81+
if (typeof this.opts.query === "string") {
82+
this.opts.query = parseqs.decode(this.opts.query);
83+
}
9484

9585
// detect ReactNative environment
96-
this.isReactNative =
86+
this.opts.isReactNative =
9787
typeof navigator !== "undefined" &&
9888
typeof navigator.product === "string" &&
9989
navigator.product.toLowerCase() === "reactnative";
10090

101-
// other options for Node.js or ReactNative client
102-
if (typeof self === "undefined" || this.isReactNative) {
103-
if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) {
104-
this.extraHeaders = opts.extraHeaders;
105-
}
106-
107-
if (opts.localAddress) {
108-
this.localAddress = opts.localAddress;
109-
}
110-
}
111-
11291
// set on handshake
11392
this.id = null;
11493
this.upgrades = null;
@@ -130,53 +109,33 @@ class Socket extends Emitter {
130109
*/
131110
createTransport(name) {
132111
debug('creating transport "%s"', name);
133-
const query = clone(this.query);
112+
const query = clone(this.opts.query);
134113

135114
// append engine.io protocol identifier
136115
query.EIO = parser.protocol;
137116

138117
// transport name
139118
query.transport = name;
140119

141-
// per-transport options
142-
const options = this.transportOptions[name] || {};
143-
144120
// session id if we already have one
145121
if (this.id) query.sid = this.id;
146122

147-
const transport = new transports[name]({
148-
query: query,
149-
socket: this,
150-
agent: options.agent || this.agent,
151-
hostname: options.hostname || this.hostname,
152-
port: options.port || this.port,
153-
secure: options.secure || this.secure,
154-
path: options.path || this.path,
155-
forceJSONP: options.forceJSONP || this.forceJSONP,
156-
jsonp: options.jsonp || this.jsonp,
157-
forceBase64: options.forceBase64 || this.forceBase64,
158-
enablesXDR: options.enablesXDR || this.enablesXDR,
159-
withCredentials: options.withCredentials || this.withCredentials,
160-
timestampRequests: options.timestampRequests || this.timestampRequests,
161-
timestampParam: options.timestampParam || this.timestampParam,
162-
policyPort: options.policyPort || this.policyPort,
163-
pfx: options.pfx || this.pfx,
164-
key: options.key || this.key,
165-
passphrase: options.passphrase || this.passphrase,
166-
cert: options.cert || this.cert,
167-
ca: options.ca || this.ca,
168-
ciphers: options.ciphers || this.ciphers,
169-
rejectUnauthorized: options.rejectUnauthorized || this.rejectUnauthorized,
170-
perMessageDeflate: options.perMessageDeflate || this.perMessageDeflate,
171-
extraHeaders: options.extraHeaders || this.extraHeaders,
172-
forceNode: options.forceNode || this.forceNode,
173-
localAddress: options.localAddress || this.localAddress,
174-
requestTimeout: options.requestTimeout || this.requestTimeout,
175-
protocols: options.protocols || void 0,
176-
isReactNative: this.isReactNative
177-
});
178-
179-
return transport;
123+
const opts = Object.assign(
124+
{
125+
query,
126+
socket: this,
127+
hostname: this.hostname,
128+
secure: this.secure,
129+
port: this.port
130+
},
131+
this.opts.transportOptions[name],
132+
this.opts
133+
);
134+
135+
// console.log(opts);
136+
debug("options: %j", opts);
137+
138+
return new transports[name](opts);
180139
}
181140

182141
/**
@@ -187,7 +146,7 @@ class Socket extends Emitter {
187146
open() {
188147
let transport;
189148
if (
190-
this.rememberUpgrade &&
149+
this.opts.rememberUpgrade &&
191150
Socket.priorWebsocketSuccess &&
192151
this.transports.indexOf("websocket") !== -1
193152
) {
@@ -208,6 +167,7 @@ class Socket extends Emitter {
208167
try {
209168
transport = this.createTransport(transport);
210169
} catch (e) {
170+
debug("error while creating transport: %s", e);
211171
this.transports.shift();
212172
this.open();
213173
return;
@@ -381,7 +341,11 @@ class Socket extends Emitter {
381341

382342
// we check for `readyState` in case an `open`
383343
// listener already closed the socket
384-
if ("open" === this.readyState && this.upgrade && this.transport.pause) {
344+
if (
345+
"open" === this.readyState &&
346+
this.opts.upgrade &&
347+
this.transport.pause
348+
) {
385349
debug("starting upgrade probes");
386350
let i = 0;
387351
const l = this.upgrades.length;

lib/transport.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,10 @@ class Transport extends Emitter {
1111
constructor(opts) {
1212
super();
1313

14-
this.path = opts.path;
15-
this.hostname = opts.hostname;
16-
this.port = opts.port;
17-
this.secure = opts.secure;
14+
this.opts = opts;
1815
this.query = opts.query;
19-
this.timestampParam = opts.timestampParam;
20-
this.timestampRequests = opts.timestampRequests;
2116
this.readyState = "";
22-
this.agent = opts.agent || false;
2317
this.socket = opts.socket;
24-
this.enablesXDR = opts.enablesXDR;
25-
this.withCredentials = opts.withCredentials;
26-
27-
// SSL options for Node.js client
28-
this.pfx = opts.pfx;
29-
this.key = opts.key;
30-
this.passphrase = opts.passphrase;
31-
this.cert = opts.cert;
32-
this.ca = opts.ca;
33-
this.ciphers = opts.ciphers;
34-
this.rejectUnauthorized = opts.rejectUnauthorized;
35-
this.forceNode = opts.forceNode;
36-
37-
// results of ReactNative environment detection
38-
this.isReactNative = opts.isReactNative;
39-
40-
// other options for Node.js client
41-
this.extraHeaders = opts.extraHeaders;
42-
this.localAddress = opts.localAddress;
4318
}
4419

4520
/**

0 commit comments

Comments
 (0)