source: trip-planner-front/node_modules/socket.io/dist/index.js@ 571e0df

Last change on this file since 571e0df was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

  • Property mode set to 100644
File size: 18.4 KB
Line 
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21var __importDefault = (this && this.__importDefault) || function (mod) {
22 return (mod && mod.__esModule) ? mod : { "default": mod };
23};
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.Namespace = exports.Socket = exports.Server = void 0;
26const http = require("http");
27const fs_1 = require("fs");
28const zlib_1 = require("zlib");
29const accepts = require("accepts");
30const stream_1 = require("stream");
31const path = require("path");
32const engine_io_1 = require("engine.io");
33const client_1 = require("./client");
34const events_1 = require("events");
35const namespace_1 = require("./namespace");
36Object.defineProperty(exports, "Namespace", { enumerable: true, get: function () { return namespace_1.Namespace; } });
37const parent_namespace_1 = require("./parent-namespace");
38const socket_io_adapter_1 = require("socket.io-adapter");
39const parser = __importStar(require("socket.io-parser"));
40const debug_1 = __importDefault(require("debug"));
41const socket_1 = require("./socket");
42Object.defineProperty(exports, "Socket", { enumerable: true, get: function () { return socket_1.Socket; } });
43const typed_events_1 = require("./typed-events");
44const uws_js_1 = require("./uws.js");
45const debug = (0, debug_1.default)("socket.io:server");
46const clientVersion = require("../package.json").version;
47const dotMapRegex = /\.map/;
48class Server extends typed_events_1.StrictEventEmitter {
49 constructor(srv, opts = {}) {
50 super();
51 /**
52 * @private
53 */
54 this._nsps = new Map();
55 this.parentNsps = new Map();
56 if ("object" === typeof srv &&
57 srv instanceof Object &&
58 !srv.listen) {
59 opts = srv;
60 srv = undefined;
61 }
62 this.path(opts.path || "/socket.io");
63 this.connectTimeout(opts.connectTimeout || 45000);
64 this.serveClient(false !== opts.serveClient);
65 this._parser = opts.parser || parser;
66 this.encoder = new this._parser.Encoder();
67 this.adapter(opts.adapter || socket_io_adapter_1.Adapter);
68 this.sockets = this.of("/");
69 this.opts = opts;
70 if (srv || typeof srv == "number")
71 this.attach(srv);
72 }
73 serveClient(v) {
74 if (!arguments.length)
75 return this._serveClient;
76 this._serveClient = v;
77 return this;
78 }
79 /**
80 * Executes the middleware for an incoming namespace not already created on the server.
81 *
82 * @param name - name of incoming namespace
83 * @param auth - the auth parameters
84 * @param fn - callback
85 *
86 * @private
87 */
88 _checkNamespace(name, auth, fn) {
89 if (this.parentNsps.size === 0)
90 return fn(false);
91 const keysIterator = this.parentNsps.keys();
92 const run = () => {
93 const nextFn = keysIterator.next();
94 if (nextFn.done) {
95 return fn(false);
96 }
97 nextFn.value(name, auth, (err, allow) => {
98 if (err || !allow) {
99 return run();
100 }
101 if (this._nsps.has(name)) {
102 // the namespace was created in the meantime
103 debug("dynamic namespace %s already exists", name);
104 return fn(this._nsps.get(name));
105 }
106 const namespace = this.parentNsps.get(nextFn.value).createChild(name);
107 debug("dynamic namespace %s was created", name);
108 // @ts-ignore
109 this.sockets.emitReserved("new_namespace", namespace);
110 fn(namespace);
111 });
112 };
113 run();
114 }
115 path(v) {
116 if (!arguments.length)
117 return this._path;
118 this._path = v.replace(/\/$/, "");
119 const escapedPath = this._path.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
120 this.clientPathRegex = new RegExp("^" +
121 escapedPath +
122 "/socket\\.io(\\.msgpack|\\.esm)?(\\.min)?\\.js(\\.map)?(?:\\?|$)");
123 return this;
124 }
125 connectTimeout(v) {
126 if (v === undefined)
127 return this._connectTimeout;
128 this._connectTimeout = v;
129 return this;
130 }
131 adapter(v) {
132 if (!arguments.length)
133 return this._adapter;
134 this._adapter = v;
135 for (const nsp of this._nsps.values()) {
136 nsp._initAdapter();
137 }
138 return this;
139 }
140 /**
141 * Attaches socket.io to a server or port.
142 *
143 * @param srv - server or port
144 * @param opts - options passed to engine.io
145 * @return self
146 * @public
147 */
148 listen(srv, opts = {}) {
149 return this.attach(srv, opts);
150 }
151 /**
152 * Attaches socket.io to a server or port.
153 *
154 * @param srv - server or port
155 * @param opts - options passed to engine.io
156 * @return self
157 * @public
158 */
159 attach(srv, opts = {}) {
160 if ("function" == typeof srv) {
161 const msg = "You are trying to attach socket.io to an express " +
162 "request handler function. Please pass a http.Server instance.";
163 throw new Error(msg);
164 }
165 // handle a port as a string
166 if (Number(srv) == srv) {
167 srv = Number(srv);
168 }
169 if ("number" == typeof srv) {
170 debug("creating http server and binding to %d", srv);
171 const port = srv;
172 srv = http.createServer((req, res) => {
173 res.writeHead(404);
174 res.end();
175 });
176 srv.listen(port);
177 }
178 // merge the options passed to the Socket.IO server
179 Object.assign(opts, this.opts);
180 // set engine.io path to `/socket.io`
181 opts.path = opts.path || this._path;
182 this.initEngine(srv, opts);
183 return this;
184 }
185 attachApp(app /*: TemplatedApp */, opts = {}) {
186 // merge the options passed to the Socket.IO server
187 Object.assign(opts, this.opts);
188 // set engine.io path to `/socket.io`
189 opts.path = opts.path || this._path;
190 // initialize engine
191 debug("creating uWebSockets.js-based engine with opts %j", opts);
192 const engine = new engine_io_1.uServer(opts);
193 engine.attach(app, opts);
194 // bind to engine events
195 this.bind(engine);
196 if (this._serveClient) {
197 // attach static file serving
198 app.get(`${this._path}/*`, (res, req) => {
199 if (!this.clientPathRegex.test(req.getUrl())) {
200 req.setYield(true);
201 return;
202 }
203 const filename = req
204 .getUrl()
205 .replace(this._path, "")
206 .replace(/\?.*$/, "")
207 .replace(/^\//, "");
208 const isMap = dotMapRegex.test(filename);
209 const type = isMap ? "map" : "source";
210 // Per the standard, ETags must be quoted:
211 // https://tools.ietf.org/html/rfc7232#section-2.3
212 const expectedEtag = '"' + clientVersion + '"';
213 const weakEtag = "W/" + expectedEtag;
214 const etag = req.getHeader("if-none-match");
215 if (etag) {
216 if (expectedEtag === etag || weakEtag === etag) {
217 debug("serve client %s 304", type);
218 res.writeStatus("304 Not Modified");
219 res.end();
220 return;
221 }
222 }
223 debug("serve client %s", type);
224 res.writeHeader("cache-control", "public, max-age=0");
225 res.writeHeader("content-type", "application/" + (isMap ? "json" : "javascript"));
226 res.writeHeader("etag", expectedEtag);
227 const filepath = path.join(__dirname, "../client-dist/", filename);
228 (0, uws_js_1.serveFile)(res, filepath);
229 });
230 }
231 (0, uws_js_1.patchAdapter)(app);
232 }
233 /**
234 * Initialize engine
235 *
236 * @param srv - the server to attach to
237 * @param opts - options passed to engine.io
238 * @private
239 */
240 initEngine(srv, opts) {
241 // initialize engine
242 debug("creating engine.io instance with opts %j", opts);
243 this.eio = (0, engine_io_1.attach)(srv, opts);
244 // attach static file serving
245 if (this._serveClient)
246 this.attachServe(srv);
247 // Export http server
248 this.httpServer = srv;
249 // bind to engine events
250 this.bind(this.eio);
251 }
252 /**
253 * Attaches the static file serving.
254 *
255 * @param srv http server
256 * @private
257 */
258 attachServe(srv) {
259 debug("attaching client serving req handler");
260 const evs = srv.listeners("request").slice(0);
261 srv.removeAllListeners("request");
262 srv.on("request", (req, res) => {
263 if (this.clientPathRegex.test(req.url)) {
264 this.serve(req, res);
265 }
266 else {
267 for (let i = 0; i < evs.length; i++) {
268 evs[i].call(srv, req, res);
269 }
270 }
271 });
272 }
273 /**
274 * Handles a request serving of client source and map
275 *
276 * @param req
277 * @param res
278 * @private
279 */
280 serve(req, res) {
281 const filename = req.url.replace(this._path, "").replace(/\?.*$/, "");
282 const isMap = dotMapRegex.test(filename);
283 const type = isMap ? "map" : "source";
284 // Per the standard, ETags must be quoted:
285 // https://tools.ietf.org/html/rfc7232#section-2.3
286 const expectedEtag = '"' + clientVersion + '"';
287 const weakEtag = "W/" + expectedEtag;
288 const etag = req.headers["if-none-match"];
289 if (etag) {
290 if (expectedEtag === etag || weakEtag === etag) {
291 debug("serve client %s 304", type);
292 res.writeHead(304);
293 res.end();
294 return;
295 }
296 }
297 debug("serve client %s", type);
298 res.setHeader("Cache-Control", "public, max-age=0");
299 res.setHeader("Content-Type", "application/" + (isMap ? "json" : "javascript"));
300 res.setHeader("ETag", expectedEtag);
301 Server.sendFile(filename, req, res);
302 }
303 /**
304 * @param filename
305 * @param req
306 * @param res
307 * @private
308 */
309 static sendFile(filename, req, res) {
310 const readStream = (0, fs_1.createReadStream)(path.join(__dirname, "../client-dist/", filename));
311 const encoding = accepts(req).encodings(["br", "gzip", "deflate"]);
312 const onError = (err) => {
313 if (err) {
314 res.end();
315 }
316 };
317 switch (encoding) {
318 case "br":
319 res.writeHead(200, { "content-encoding": "br" });
320 readStream.pipe((0, zlib_1.createBrotliCompress)()).pipe(res);
321 (0, stream_1.pipeline)(readStream, (0, zlib_1.createBrotliCompress)(), res, onError);
322 break;
323 case "gzip":
324 res.writeHead(200, { "content-encoding": "gzip" });
325 (0, stream_1.pipeline)(readStream, (0, zlib_1.createGzip)(), res, onError);
326 break;
327 case "deflate":
328 res.writeHead(200, { "content-encoding": "deflate" });
329 (0, stream_1.pipeline)(readStream, (0, zlib_1.createDeflate)(), res, onError);
330 break;
331 default:
332 res.writeHead(200);
333 (0, stream_1.pipeline)(readStream, res, onError);
334 }
335 }
336 /**
337 * Binds socket.io to an engine.io instance.
338 *
339 * @param {engine.Server} engine engine.io (or compatible) server
340 * @return self
341 * @public
342 */
343 bind(engine) {
344 this.engine = engine;
345 this.engine.on("connection", this.onconnection.bind(this));
346 return this;
347 }
348 /**
349 * Called with each incoming transport connection.
350 *
351 * @param {engine.Socket} conn
352 * @return self
353 * @private
354 */
355 onconnection(conn) {
356 debug("incoming connection with id %s", conn.id);
357 const client = new client_1.Client(this, conn);
358 if (conn.protocol === 3) {
359 // @ts-ignore
360 client.connect("/");
361 }
362 return this;
363 }
364 /**
365 * Looks up a namespace.
366 *
367 * @param {String|RegExp|Function} name nsp name
368 * @param fn optional, nsp `connection` ev handler
369 * @public
370 */
371 of(name, fn) {
372 if (typeof name === "function" || name instanceof RegExp) {
373 const parentNsp = new parent_namespace_1.ParentNamespace(this);
374 debug("initializing parent namespace %s", parentNsp.name);
375 if (typeof name === "function") {
376 this.parentNsps.set(name, parentNsp);
377 }
378 else {
379 this.parentNsps.set((nsp, conn, next) => next(null, name.test(nsp)), parentNsp);
380 }
381 if (fn) {
382 // @ts-ignore
383 parentNsp.on("connect", fn);
384 }
385 return parentNsp;
386 }
387 if (String(name)[0] !== "/")
388 name = "/" + name;
389 let nsp = this._nsps.get(name);
390 if (!nsp) {
391 debug("initializing namespace %s", name);
392 nsp = new namespace_1.Namespace(this, name);
393 this._nsps.set(name, nsp);
394 if (name !== "/") {
395 // @ts-ignore
396 this.sockets.emitReserved("new_namespace", nsp);
397 }
398 }
399 if (fn)
400 nsp.on("connect", fn);
401 return nsp;
402 }
403 /**
404 * Closes server connection
405 *
406 * @param [fn] optional, called as `fn([err])` on error OR all conns closed
407 * @public
408 */
409 close(fn) {
410 for (const socket of this.sockets.sockets.values()) {
411 socket._onclose("server shutting down");
412 }
413 this.engine.close();
414 // restore the Adapter prototype
415 (0, uws_js_1.restoreAdapter)();
416 if (this.httpServer) {
417 this.httpServer.close(fn);
418 }
419 else {
420 fn && fn();
421 }
422 }
423 /**
424 * Sets up namespace middleware.
425 *
426 * @return self
427 * @public
428 */
429 use(fn) {
430 this.sockets.use(fn);
431 return this;
432 }
433 /**
434 * Targets a room when emitting.
435 *
436 * @param room
437 * @return self
438 * @public
439 */
440 to(room) {
441 return this.sockets.to(room);
442 }
443 /**
444 * Targets a room when emitting.
445 *
446 * @param room
447 * @return self
448 * @public
449 */
450 in(room) {
451 return this.sockets.in(room);
452 }
453 /**
454 * Excludes a room when emitting.
455 *
456 * @param name
457 * @return self
458 * @public
459 */
460 except(name) {
461 return this.sockets.except(name);
462 }
463 /**
464 * Sends a `message` event to all clients.
465 *
466 * @return self
467 * @public
468 */
469 send(...args) {
470 this.sockets.emit("message", ...args);
471 return this;
472 }
473 /**
474 * Sends a `message` event to all clients.
475 *
476 * @return self
477 * @public
478 */
479 write(...args) {
480 this.sockets.emit("message", ...args);
481 return this;
482 }
483 /**
484 * Emit a packet to other Socket.IO servers
485 *
486 * @param ev - the event name
487 * @param args - an array of arguments, which may include an acknowledgement callback at the end
488 * @public
489 */
490 serverSideEmit(ev, ...args) {
491 return this.sockets.serverSideEmit(ev, ...args);
492 }
493 /**
494 * Gets a list of socket ids.
495 *
496 * @public
497 */
498 allSockets() {
499 return this.sockets.allSockets();
500 }
501 /**
502 * Sets the compress flag.
503 *
504 * @param compress - if `true`, compresses the sending data
505 * @return self
506 * @public
507 */
508 compress(compress) {
509 return this.sockets.compress(compress);
510 }
511 /**
512 * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
513 * receive messages (because of network slowness or other issues, or because they’re connected through long polling
514 * and is in the middle of a request-response cycle).
515 *
516 * @return self
517 * @public
518 */
519 get volatile() {
520 return this.sockets.volatile;
521 }
522 /**
523 * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
524 *
525 * @return self
526 * @public
527 */
528 get local() {
529 return this.sockets.local;
530 }
531 /**
532 * Returns the matching socket instances
533 *
534 * @public
535 */
536 fetchSockets() {
537 return this.sockets.fetchSockets();
538 }
539 /**
540 * Makes the matching socket instances join the specified rooms
541 *
542 * @param room
543 * @public
544 */
545 socketsJoin(room) {
546 return this.sockets.socketsJoin(room);
547 }
548 /**
549 * Makes the matching socket instances leave the specified rooms
550 *
551 * @param room
552 * @public
553 */
554 socketsLeave(room) {
555 return this.sockets.socketsLeave(room);
556 }
557 /**
558 * Makes the matching socket instances disconnect
559 *
560 * @param close - whether to close the underlying connection
561 * @public
562 */
563 disconnectSockets(close = false) {
564 return this.sockets.disconnectSockets(close);
565 }
566}
567exports.Server = Server;
568/**
569 * Expose main namespace (/).
570 */
571const emitterMethods = Object.keys(events_1.EventEmitter.prototype).filter(function (key) {
572 return typeof events_1.EventEmitter.prototype[key] === "function";
573});
574emitterMethods.forEach(function (fn) {
575 Server.prototype[fn] = function () {
576 return this.sockets[fn].apply(this.sockets, arguments);
577 };
578});
579module.exports = (srv, opts) => new Server(srv, opts);
580module.exports.Server = Server;
581module.exports.Namespace = namespace_1.Namespace;
582module.exports.Socket = socket_1.Socket;
Note: See TracBrowser for help on using the repository browser.