UNPKG

1.49 kBJavaScriptView Raw
1/*
2 * Copyright 2012 Amadeus s.a.s.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16var http = require('http');
17
18module.exports = {
19 // This function is a wrapper around http.createServer to also close all connections
20 // when the server close method is called
21 createServer: function () {
22 var server = http.createServer.apply(http, arguments);
23
24 var connections = [];
25 server.on('connection', function (socket) {
26 connections.push(socket);
27 socket.on('close', function () {
28 var i = connections.indexOf(socket);
29 if (i > -1) {
30 connections.splice(i, 1);
31 }
32 });
33 });
34
35 var realClose = server.close;
36 server.close = function (cb) {
37 realClose.call(this, cb);
38
39 for (var i = connections.length - 1; i >= 0; i--) {
40 connections[i].end();
41 }
42 };
43
44 return server;
45 }
46};