MCPcopy
hub / github.com/socketio/socket.io / attachApp

Method attachApp

packages/socket.io/lib/index.ts:523–584  ·  view source on GitHub ↗

* Attaches socket.io to a uWebSockets.js app. * @param app * @param opts

(app /*: TemplatedApp */, opts: Partial<ServerOptions> = {})

Source from the content-addressed store, hash-verified

521 * @param opts
522 */
523 public attachApp(app /*: TemplatedApp */, opts: Partial<ServerOptions> = {}) {
524 // merge the options passed to the Socket.IO server
525 Object.assign(opts, this.opts);
526 // set engine.io path to `/socket.io`
527 opts.path = opts.path || this._path;
528
529 // initialize engine
530 debug("creating uWebSockets.js-based engine with opts %j", opts);
531 const engine = new uServer(opts);
532
533 engine.attach(app, opts);
534
535 // bind to engine events
536 this.bind(engine);
537
538 if (this._serveClient) {
539 // attach static file serving
540 app.get(`${this._path}/*`, (res, req) => {
541 if (!this.clientPathRegex.test(req.getUrl())) {
542 req.setYield(true);
543 return;
544 }
545
546 const filename = req
547 .getUrl()
548 .replace(this._path, "")
549 .replace(/\?.*$/, "")
550 .replace(/^\//, "");
551 const isMap = dotMapRegex.test(filename);
552 const type = isMap ? "map" : "source";
553
554 // Per the standard, ETags must be quoted:
555 // https://tools.ietf.org/html/rfc7232#section-2.3
556 const expectedEtag = '"' + clientVersion + '"';
557 const weakEtag = "W/" + expectedEtag;
558
559 const etag = req.getHeader("if-none-match");
560 if (etag) {
561 if (expectedEtag === etag || weakEtag === etag) {
562 debug("serve client %s 304", type);
563 res.writeStatus("304 Not Modified");
564 res.end();
565 return;
566 }
567 }
568
569 debug("serve client %s", type);
570
571 res.writeHeader("cache-control", "public, max-age=0");
572 res.writeHeader(
573 "content-type",
574 "application/" + (isMap ? "json" : "javascript") + "; charset=utf-8",
575 );
576 res.writeHeader("etag", expectedEtag);
577
578 const filepath = path.join(__dirname, "../client-dist/", filename);
579 serveFile(res, filepath);
580 });

Callers 1

uws.tsFile · 0.80

Calls 10

attachMethod · 0.95
bindMethod · 0.95
serveFileFunction · 0.90
patchAdapterFunction · 0.90
debugFunction · 0.85
writeStatusMethod · 0.80
writeHeaderMethod · 0.80
getHeaderMethod · 0.45
endMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected