Skip to content

Commit 4b17d60

Browse files
committed
fix(socket/server): Allow .exit() to immediately shut down all servers as expected
note: This is a hacky workaround for #1121 closes #1121
1 parent 2282cad commit 4b17d60

3 files changed

Lines changed: 111 additions & 5 deletions

File tree

lib/server/index.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"use strict";
22

3+
var enableDestroy = require("server-destroy");
4+
var _ = require("../../lodash.custom");
5+
36
/**
47
* Browsersync server
58
* Three available modes: Snippet, Server or Proxy
@@ -21,9 +24,40 @@ module.exports.plugin = function (bs, scripts) {
2124
}
2225

2326
if (bsServer) {
27+
28+
/**
29+
* Allow server to be destroyed gracefully
30+
*/
31+
enableDestroy("bs", bsServer.server);
32+
33+
/**
34+
* Listen on the available port
35+
*/
2436
bsServer.server.listen(bs.options.get("port"));
37+
38+
/**
39+
* Hack to deal with https://github.com/socketio/socket.io/issues/1602#issuecomment-224270022
40+
*/
2541
bs.registerCleanupTask(function () {
26-
bsServer.server.close();
42+
if (bs.io && bs.io.sockets) {
43+
setCloseReceived(bs.io.sockets);
44+
}
45+
if (bs.ui && bs.ui.socket) {
46+
setCloseReceived(bs.ui.socket);
47+
}
48+
});
49+
50+
/**
51+
* Destroy the server on cleanup
52+
*/
53+
bs.registerCleanupTask(function () {
54+
bsServer.server.destroy();
55+
});
56+
}
57+
58+
function setCloseReceived(io) {
59+
Object.keys(io.sockets).forEach(function (key) {
60+
_.set(io.sockets[key], "conn.transport.socket._closeReceived", true);
2761
});
2862
}
2963

@@ -45,7 +79,7 @@ function createServer (bs, clientScripts) {
4579

4680
var proxy = bs.options.get("proxy");
4781
var server = bs.options.get("server");
48-
82+
4983
if (!proxy && !server) {
5084
return require("./snippet-server")(bs, clientScripts);
5185
}

lodash.custom.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license
33
* lodash (Custom Build) <https://lodash.com/>
4-
* Build: `lodash include="isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject" exports="node"`
4+
* Build: `lodash include="isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject,set" exports="node"`
55
* Copyright jQuery Foundation and other contributors <https://jquery.org/>
66
* Released under MIT license <https://lodash.com/license>
77
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
@@ -1896,6 +1896,44 @@
18961896
};
18971897
}
18981898

1899+
/**
1900+
* The base implementation of `_.set`.
1901+
*
1902+
* @private
1903+
* @param {Object} object The object to query.
1904+
* @param {Array|string} path The path of the property to set.
1905+
* @param {*} value The value to set.
1906+
* @param {Function} [customizer] The function to customize path creation.
1907+
* @returns {Object} Returns `object`.
1908+
*/
1909+
function baseSet(object, path, value, customizer) {
1910+
path = isKey(path, object) ? [path] : castPath(path);
1911+
1912+
var index = -1,
1913+
length = path.length,
1914+
lastIndex = length - 1,
1915+
nested = object;
1916+
1917+
while (nested != null && ++index < length) {
1918+
var key = toKey(path[index]);
1919+
if (isObject(nested)) {
1920+
var newValue = value;
1921+
if (index != lastIndex) {
1922+
var objValue = nested[key];
1923+
newValue = customizer ? customizer(objValue, key, nested) : undefined;
1924+
if (newValue === undefined) {
1925+
newValue = objValue == null
1926+
? (isIndex(path[index + 1]) ? [] : {})
1927+
: objValue;
1928+
}
1929+
}
1930+
assignValue(nested, key, newValue);
1931+
}
1932+
nested = nested[key];
1933+
}
1934+
return object;
1935+
}
1936+
18991937
/**
19001938
* The base implementation of `_.toString` which doesn't convert nullish
19011939
* values to empty strings.
@@ -3993,6 +4031,38 @@
39934031
baseMerge(object, source, srcIndex);
39944032
});
39954033

4034+
/**
4035+
* Sets the value at `path` of `object`. If a portion of `path` doesn't exist,
4036+
* it's created. Arrays are created for missing index properties while objects
4037+
* are created for all other missing properties. Use `_.setWith` to customize
4038+
* `path` creation.
4039+
*
4040+
* **Note:** This method mutates `object`.
4041+
*
4042+
* @static
4043+
* @memberOf _
4044+
* @since 3.7.0
4045+
* @category Object
4046+
* @param {Object} object The object to modify.
4047+
* @param {Array|string} path The path of the property to set.
4048+
* @param {*} value The value to set.
4049+
* @returns {Object} Returns `object`.
4050+
* @example
4051+
*
4052+
* var object = { 'a': [{ 'b': { 'c': 3 } }] };
4053+
*
4054+
* _.set(object, 'a[0].b.c', 4);
4055+
* console.log(object.a[0].b.c);
4056+
* // => 4
4057+
*
4058+
* _.set(object, ['x', '0', 'y', 'z'], 5);
4059+
* console.log(object.x[0].y.z);
4060+
* // => 5
4061+
*/
4062+
function set(object, path, value) {
4063+
return object == null ? object : baseSet(object, path, value);
4064+
}
4065+
39964066
/**
39974067
* Creates an array of the own enumerable string keyed property values of `object`.
39984068
*
@@ -4182,6 +4252,7 @@
41824252
lodash.merge = merge;
41834253
lodash.property = property;
41844254
lodash.rest = rest;
4255+
lodash.set = set;
41854256
lodash.toArray = toArray;
41864257
lodash.toPlainObject = toPlainObject;
41874258
lodash.union = union;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
"cover-local": "istanbul cover -x lodash.custom.js _mocha -- --timeout 10000 --recursive ./test/specs",
3030
"coveralls": "istanbul-coveralls",
3131
"pre-release": "npm test && npm run pro-local && npm run pro",
32-
"lodash": "lodash include=isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject exports=node"
32+
"lodash": "lodash include=isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject,set exports=node"
3333
},
3434
"dependencies": {
3535
"browser-sync-client": "^2.3.3",
36-
"browser-sync-ui": "0.5.19",
36+
"browser-sync-ui": "0.6.0",
3737
"bs-recipes": "1.2.2",
3838
"chokidar": "1.5.1",
3939
"connect": "3.4.1",
@@ -53,6 +53,7 @@
5353
"rx": "4.1.0",
5454
"serve-index": "1.7.3",
5555
"serve-static": "1.10.2",
56+
"server-destroy": "1.0.1",
5657
"socket.io": "1.4.6",
5758
"ua-parser-js": "0.7.10",
5859
"yargs": "4.7.1"

0 commit comments

Comments
 (0)