diff options
Diffstat (limited to 'platform/node/index.js')
-rw-r--r-- | platform/node/index.js | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/platform/node/index.js b/platform/node/index.js index e36a47943d..5944a0a27d 100644 --- a/platform/node/index.js +++ b/platform/node/index.js @@ -19,14 +19,29 @@ var Map = function(options) { return new constructor(Object.assign(options, { request: function(req) { - request(req, function() { + // Protect against `request` implementations that call the callback synchronously, + // call it multiple times, or throw exceptions. + // http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony + + var responded = false; + var callback = function() { var args = arguments; - // Protect against `request` implementations that call the callback synchronously. - // http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony - process.nextTick(function() { - req.respond.apply(req, args); - }); - }); + if (!responded) { + responded = true; + process.nextTick(function() { + req.respond.apply(req, args); + }); + } else { + console.warn('request function responded multiple times; it should call the callback only once'); + } + }; + + try { + request(req, callback); + } catch (e) { + console.warn('request function threw an exception; it should call the callback with an error instead'); + callback(e); + } } })); }; |