Skip to content

Commit

Permalink
fix(socket/server): Allow .exit() to immediately shut down all server…
Browse files Browse the repository at this point in the history
…s as expected

note: This is a hacky workaround for #1121

closes #1121
  • Loading branch information
shakyShane committed Jun 7, 2016
1 parent 2282cad commit 4b17d60
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
38 changes: 36 additions & 2 deletions lib/server/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"use strict";

var enableDestroy = require("server-destroy");
var _ = require("../../lodash.custom");

/**
* Browsersync server
* Three available modes: Snippet, Server or Proxy
Expand All @@ -21,9 +24,40 @@ module.exports.plugin = function (bs, scripts) {
}

if (bsServer) {

/**
* Allow server to be destroyed gracefully
*/
enableDestroy("bs", bsServer.server);

/**
* Listen on the available port
*/
bsServer.server.listen(bs.options.get("port"));

/**
* Hack to deal with https://github.com/socketio/socket.io/issues/1602#issuecomment-224270022
*/
bs.registerCleanupTask(function () {
bsServer.server.close();
if (bs.io && bs.io.sockets) {
setCloseReceived(bs.io.sockets);
}
if (bs.ui && bs.ui.socket) {
setCloseReceived(bs.ui.socket);
}
});

/**
* Destroy the server on cleanup
*/
bs.registerCleanupTask(function () {
bsServer.server.destroy();
});
}

function setCloseReceived(io) {
Object.keys(io.sockets).forEach(function (key) {
_.set(io.sockets[key], "conn.transport.socket._closeReceived", true);
});
}

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

var proxy = bs.options.get("proxy");
var server = bs.options.get("server");

if (!proxy && !server) {
return require("./snippet-server")(bs, clientScripts);
}
Expand Down
73 changes: 72 additions & 1 deletion lodash.custom.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license
* lodash (Custom Build) <https://lodash.com/>
* Build: `lodash include="isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject" exports="node"`
* Build: `lodash include="isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject,set" exports="node"`
* Copyright jQuery Foundation and other contributors <https://jquery.org/>
* Released under MIT license <https://lodash.com/license>
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
Expand Down Expand Up @@ -1896,6 +1896,44 @@
};
}

/**
* The base implementation of `_.set`.
*
* @private
* @param {Object} object The object to query.
* @param {Array|string} path The path of the property to set.
* @param {*} value The value to set.
* @param {Function} [customizer] The function to customize path creation.
* @returns {Object} Returns `object`.
*/
function baseSet(object, path, value, customizer) {
path = isKey(path, object) ? [path] : castPath(path);

var index = -1,
length = path.length,
lastIndex = length - 1,
nested = object;

while (nested != null && ++index < length) {
var key = toKey(path[index]);
if (isObject(nested)) {
var newValue = value;
if (index != lastIndex) {
var objValue = nested[key];
newValue = customizer ? customizer(objValue, key, nested) : undefined;
if (newValue === undefined) {
newValue = objValue == null
? (isIndex(path[index + 1]) ? [] : {})
: objValue;
}
}
assignValue(nested, key, newValue);
}
nested = nested[key];
}
return object;
}

/**
* The base implementation of `_.toString` which doesn't convert nullish
* values to empty strings.
Expand Down Expand Up @@ -3993,6 +4031,38 @@
baseMerge(object, source, srcIndex);
});

/**
* Sets the value at `path` of `object`. If a portion of `path` doesn't exist,
* it's created. Arrays are created for missing index properties while objects
* are created for all other missing properties. Use `_.setWith` to customize
* `path` creation.
*
* **Note:** This method mutates `object`.
*
* @static
* @memberOf _
* @since 3.7.0
* @category Object
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {*} value The value to set.
* @returns {Object} Returns `object`.
* @example
*
* var object = { 'a': [{ 'b': { 'c': 3 } }] };
*
* _.set(object, 'a[0].b.c', 4);
* console.log(object.a[0].b.c);
* // => 4
*
* _.set(object, ['x', '0', 'y', 'z'], 5);
* console.log(object.x[0].y.z);
* // => 5
*/
function set(object, path, value) {
return object == null ? object : baseSet(object, path, value);
}

/**
* Creates an array of the own enumerable string keyed property values of `object`.
*
Expand Down Expand Up @@ -4182,6 +4252,7 @@
lodash.merge = merge;
lodash.property = property;
lodash.rest = rest;
lodash.set = set;
lodash.toArray = toArray;
lodash.toPlainObject = toPlainObject;
lodash.union = union;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"cover-local": "istanbul cover -x lodash.custom.js _mocha -- --timeout 10000 --recursive ./test/specs",
"coveralls": "istanbul-coveralls",
"pre-release": "npm test && npm run pro-local && npm run pro",
"lodash": "lodash include=isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject exports=node"
"lodash": "lodash include=isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject,set exports=node"
},
"dependencies": {
"browser-sync-client": "^2.3.3",
"browser-sync-ui": "0.5.19",
"browser-sync-ui": "0.6.0",
"bs-recipes": "1.2.2",
"chokidar": "1.5.1",
"connect": "3.4.1",
Expand All @@ -53,6 +53,7 @@
"rx": "4.1.0",
"serve-index": "1.7.3",
"serve-static": "1.10.2",
"server-destroy": "1.0.1",
"socket.io": "1.4.6",
"ua-parser-js": "0.7.10",
"yargs": "4.7.1"
Expand Down

0 comments on commit 4b17d60

Please sign in to comment.