-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
255 lines (208 loc) · 5.91 KB
/
index.js
File metadata and controls
255 lines (208 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"use strict";
// ===================================================================
const EventEmitter = require("events").EventEmitter;
const formatUrl = require("url").format;
const fromEvent = require("promise-toolbox/fromEvent");
const inherits = require("util").inherits;
const resolvePath = require("path").resolve;
// ===================================================================
const forwardedEvents = [
"checkContinue",
"checkExpectation",
"clientError",
"connect",
"connection",
"listening",
"request",
"upgrade",
];
function extractProperty(obj, prop) {
const value = obj[prop];
if (value !== undefined) {
delete obj[prop];
return value;
}
}
function forOwn(obj, cb) {
const keys = Object.keys(obj);
for (let i = 0, n = keys.length; i < n; ++i) {
cb(obj[keys[i]]);
}
}
function getSystemdFd(index) {
if (process.pid !== +process.env.LISTEN_PID) {
throw new Error("systemd sockets are not meant for us (LISTEN_PID)");
}
const listenFds = process.env.LISTEN_FDS;
if (listenFds == null) {
throw new Error("no systemd sockets found (LISTEN_FDS)");
}
const max = listenFds - 1;
if (index > max) {
throw new Error("no such systemd socket found");
}
return index + 3; // 3 = SD_LISTEN_FDS_START
}
const isEmpty = (obj) => Object.keys(obj).length === 0;
// ===================================================================
function onNewListener(event, listener) {
if (forwardedEvents.includes(event)) {
forOwn(this._servers, (server) => {
server.on(event, listener);
});
}
}
function onRemoveListener(event, listener) {
if (forwardedEvents.includes(event)) {
forOwn(this._servers, (server) => {
server.off(event, listener);
});
}
}
function Server(opts) {
EventEmitter.call(this);
this._createServer =
(opts != null && opts.createServer) || require("http").createServer;
this._createSecureServer =
(opts != null && opts.createSecureServer) || require("https").createServer;
this._servers = Object.create(null);
this.on("removeListener", onRemoveListener).on("newListener", onNewListener);
}
inherits(Server, EventEmitter);
const proto = Server.prototype;
function getAddress(serverId) {
return this[serverId].address();
}
proto.addresses = function Server$addresses() {
const servers = this._servers;
return Object.keys(servers).map(getAddress, servers);
};
function close(server) {
server.close();
}
proto.close = function Server$close(callback) {
if (callback) {
this.once("close", callback);
}
// Emit the close event even if there are no registered servers.
if (isEmpty(this._servers)) {
const self = this;
setImmediate(function () {
self.emit("close");
});
return Promise.resolve();
}
// Closes each servers.
forOwn(this._servers, close);
return fromEvent(this, "close");
};
let nextId = 0;
proto.listen = function Server$listen(opts) {
opts = Object.assign({}, opts);
if (opts.hostname === "localhost") {
return Promise.all([
this.listen(Object.assign(opts, { hostname: "::1" })),
this.listen(Object.assign(opts, { hostname: "127.0.0.1" })),
]).then((addresses) => addresses[0]);
}
const hostname = extractProperty(opts, "hostname");
let port = extractProperty(opts, "port");
const systemdSocket = extractProperty(opts, "systemdSocket");
let fd = extractProperty(opts, "fd");
let socket = extractProperty(opts, "socket");
const servers = this._servers;
let server, protocol;
if (opts.pfx || opts.SNICallback || (opts.cert && opts.key)) {
server = this._createSecureServer(opts);
protocol = "https";
} else {
server = this._createServer(opts);
protocol = "http";
}
const id = nextId++;
servers[id] = server;
if (systemdSocket != null) {
fd = getSystemdFd(systemdSocket);
}
// Compute a temporary nice address to display in case of error.
let niceAddress;
if (fd != null) {
server.listen({ fd });
niceAddress = protocol + "://<fd:" + fd + ">";
} else if (socket != null) {
socket = resolvePath(socket);
server.listen(socket);
niceAddress = protocol + "://" + socket;
} else {
if (port == null) {
port = protocol === "https" ? 443 : 80;
}
server.listen(port, hostname);
niceAddress = formatUrl({
protocol,
// Hostname default to localhost.
hostname: hostname || "localhost",
// No port means random, unknown for now.
port: port || "<unknown>",
});
}
const emit = this.emit.bind(this);
server.once("close", function onClose() {
delete servers[id];
if (isEmpty(servers)) {
emit("close");
}
});
server.on("error", function onError() {
delete servers[id];
// FIXME: Should it be forwarded and be fatal if there are no
// listeners?
});
forwardedEvents.forEach((event) => {
this.listeners(event).forEach((listener) => {
server.on(event, listener);
});
});
return fromEvent(server, "listening").then(
function () {
const address = server.address();
if (typeof address === "string") {
return protocol + "://" + address;
}
return formatUrl({
protocol,
hostname: address.address,
port: address.port,
});
},
function (error) {
error.niceAddress = niceAddress;
throw error;
}
);
};
function ref(server) {
server.ref();
}
proto.ref = function Server$ref() {
forOwn(this._servers, ref);
};
function unref(server) {
server.unref();
}
proto.unref = function Server$unref() {
forOwn(this._servers, unref);
};
// ===================================================================
module.exports = exports = Server;
exports.create = function create(opts, requestListener) {
if (typeof opts === "function") {
requestListener = opts;
opts = undefined;
}
const server = new Server(opts);
if (requestListener !== undefined) {
server.on("request", requestListener);
}
return server;
};