uws.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.serveFile = exports.restoreAdapter = exports.patchAdapter = void 0;
  7. const socket_io_adapter_1 = require("socket.io-adapter");
  8. const fs_1 = require("fs");
  9. const debug_1 = __importDefault(require("debug"));
  10. const debug = (0, debug_1.default)("socket.io:adapter-uws");
  11. const SEPARATOR = "\x1f"; // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
  12. const { addAll, del, broadcast } = socket_io_adapter_1.Adapter.prototype;
  13. function patchAdapter(app /* : TemplatedApp */) {
  14. socket_io_adapter_1.Adapter.prototype.addAll = function (id, rooms) {
  15. const isNew = !this.sids.has(id);
  16. addAll.call(this, id, rooms);
  17. const socket = this.nsp.sockets.get(id);
  18. if (!socket) {
  19. return;
  20. }
  21. if (socket.conn.transport.name === "websocket") {
  22. subscribe(this.nsp.name, socket, isNew, rooms);
  23. return;
  24. }
  25. if (isNew) {
  26. socket.conn.on("upgrade", () => {
  27. const rooms = this.sids.get(id);
  28. if (rooms) {
  29. subscribe(this.nsp.name, socket, isNew, rooms);
  30. }
  31. });
  32. }
  33. };
  34. socket_io_adapter_1.Adapter.prototype.del = function (id, room) {
  35. del.call(this, id, room);
  36. const socket = this.nsp.sockets.get(id);
  37. if (socket && socket.conn.transport.name === "websocket") {
  38. // @ts-ignore
  39. const sessionId = socket.conn.id;
  40. // @ts-ignore
  41. const websocket = socket.conn.transport.socket;
  42. const topic = `${this.nsp.name}${SEPARATOR}${room}`;
  43. debug("unsubscribe connection %s from topic %s", sessionId, topic);
  44. websocket.unsubscribe(topic);
  45. }
  46. };
  47. socket_io_adapter_1.Adapter.prototype.broadcast = function (packet, opts) {
  48. const useFastPublish = opts.rooms.size <= 1 && opts.except.size === 0;
  49. if (!useFastPublish) {
  50. broadcast.call(this, packet, opts);
  51. return;
  52. }
  53. const flags = opts.flags || {};
  54. const basePacketOpts = {
  55. preEncoded: true,
  56. volatile: flags.volatile,
  57. compress: flags.compress,
  58. };
  59. packet.nsp = this.nsp.name;
  60. const encodedPackets = this.encoder.encode(packet);
  61. const topic = opts.rooms.size === 0
  62. ? this.nsp.name
  63. : `${this.nsp.name}${SEPARATOR}${opts.rooms.keys().next().value}`;
  64. debug("fast publish to %s", topic);
  65. // fast publish for clients connected with WebSocket
  66. encodedPackets.forEach((encodedPacket) => {
  67. const isBinary = typeof encodedPacket !== "string";
  68. // "4" being the message type in the Engine.IO protocol, see https://github.com/socketio/engine.io-protocol
  69. app.publish(topic, isBinary ? encodedPacket : "4" + encodedPacket, isBinary);
  70. });
  71. this.apply(opts, (socket) => {
  72. if (socket.conn.transport.name !== "websocket") {
  73. // classic publish for clients connected with HTTP long-polling
  74. socket.client.writeToEngine(encodedPackets, basePacketOpts);
  75. }
  76. });
  77. };
  78. }
  79. exports.patchAdapter = patchAdapter;
  80. function subscribe(namespaceName, socket, isNew, rooms) {
  81. // @ts-ignore
  82. const sessionId = socket.conn.id;
  83. // @ts-ignore
  84. const websocket = socket.conn.transport.socket;
  85. if (isNew) {
  86. debug("subscribe connection %s to topic %s", sessionId, namespaceName);
  87. websocket.subscribe(namespaceName);
  88. }
  89. rooms.forEach((room) => {
  90. const topic = `${namespaceName}${SEPARATOR}${room}`; // '#' can be used as wildcard
  91. debug("subscribe connection %s to topic %s", sessionId, topic);
  92. websocket.subscribe(topic);
  93. });
  94. }
  95. function restoreAdapter() {
  96. socket_io_adapter_1.Adapter.prototype.addAll = addAll;
  97. socket_io_adapter_1.Adapter.prototype.del = del;
  98. socket_io_adapter_1.Adapter.prototype.broadcast = broadcast;
  99. }
  100. exports.restoreAdapter = restoreAdapter;
  101. const toArrayBuffer = (buffer) => {
  102. const { buffer: arrayBuffer, byteOffset, byteLength } = buffer;
  103. return arrayBuffer.slice(byteOffset, byteOffset + byteLength);
  104. };
  105. // imported from https://github.com/kolodziejczak-sz/uwebsocket-serve
  106. function serveFile(res /* : HttpResponse */, filepath) {
  107. const { size } = (0, fs_1.statSync)(filepath);
  108. const readStream = (0, fs_1.createReadStream)(filepath);
  109. const destroyReadStream = () => !readStream.destroyed && readStream.destroy();
  110. const onError = (error) => {
  111. destroyReadStream();
  112. throw error;
  113. };
  114. const onDataChunk = (chunk) => {
  115. const arrayBufferChunk = toArrayBuffer(chunk);
  116. const lastOffset = res.getWriteOffset();
  117. const [ok, done] = res.tryEnd(arrayBufferChunk, size);
  118. if (!done && !ok) {
  119. readStream.pause();
  120. res.onWritable((offset) => {
  121. const [ok, done] = res.tryEnd(arrayBufferChunk.slice(offset - lastOffset), size);
  122. if (!done && ok) {
  123. readStream.resume();
  124. }
  125. return ok;
  126. });
  127. }
  128. };
  129. res.onAborted(destroyReadStream);
  130. readStream
  131. .on("data", onDataChunk)
  132. .on("error", onError)
  133. .on("end", destroyReadStream);
  134. }
  135. exports.serveFile = serveFile;