userver.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.uServer = void 0;
  4. const debug_1 = require("debug");
  5. const server_1 = require("./server");
  6. const transports_uws_1 = require("./transports-uws");
  7. const debug = (0, debug_1.default)("engine:uws");
  8. class uServer extends server_1.BaseServer {
  9. init() { }
  10. cleanup() { }
  11. /**
  12. * Prepares a request by processing the query string.
  13. *
  14. * @api private
  15. */
  16. prepare(req, res) {
  17. req.method = req.getMethod().toUpperCase();
  18. const params = new URLSearchParams(req.getQuery());
  19. req._query = Object.fromEntries(params.entries());
  20. req.headers = {};
  21. req.forEach((key, value) => {
  22. req.headers[key] = value;
  23. });
  24. req.connection = {
  25. remoteAddress: Buffer.from(res.getRemoteAddressAsText()).toString()
  26. };
  27. res.onAborted(() => {
  28. debug("response has been aborted");
  29. });
  30. }
  31. createTransport(transportName, req) {
  32. return new transports_uws_1.default[transportName](req);
  33. }
  34. /**
  35. * Attach the engine to a µWebSockets.js server
  36. * @param app
  37. * @param options
  38. */
  39. attach(app /* : TemplatedApp */, options = {}) {
  40. const path = (options.path || "/engine.io").replace(/\/$/, "") + "/";
  41. app
  42. .any(path, this.handleRequest.bind(this))
  43. //
  44. .ws(path, {
  45. compression: options.compression,
  46. idleTimeout: options.idleTimeout,
  47. maxBackpressure: options.maxBackpressure,
  48. maxPayloadLength: this.opts.maxHttpBufferSize,
  49. upgrade: this.handleUpgrade.bind(this),
  50. open: ws => {
  51. ws.transport.socket = ws;
  52. ws.transport.writable = true;
  53. ws.transport.emit("drain");
  54. },
  55. message: (ws, message, isBinary) => {
  56. ws.transport.onData(isBinary ? message : Buffer.from(message).toString());
  57. },
  58. close: (ws, code, message) => {
  59. ws.transport.onClose(code, message);
  60. }
  61. });
  62. }
  63. handleRequest(res, req) {
  64. debug('handling "%s" http request "%s"', req.getMethod(), req.getUrl());
  65. this.prepare(req, res);
  66. req.res = res;
  67. const callback = (errorCode, errorContext) => {
  68. if (errorCode !== undefined) {
  69. this.emit("connection_error", {
  70. req,
  71. code: errorCode,
  72. message: server_1.Server.errorMessages[errorCode],
  73. context: errorContext
  74. });
  75. this.abortRequest(req.res, errorCode, errorContext);
  76. return;
  77. }
  78. if (req._query.sid) {
  79. debug("setting new request for existing client");
  80. this.clients[req._query.sid].transport.onRequest(req);
  81. }
  82. else {
  83. const closeConnection = (errorCode, errorContext) => this.abortRequest(res, errorCode, errorContext);
  84. this.handshake(req._query.transport, req, closeConnection);
  85. }
  86. };
  87. if (this.corsMiddleware) {
  88. // needed to buffer headers until the status is computed
  89. req.res = new ResponseWrapper(res);
  90. this.corsMiddleware.call(null, req, req.res, () => {
  91. this.verify(req, false, callback);
  92. });
  93. }
  94. else {
  95. this.verify(req, false, callback);
  96. }
  97. }
  98. handleUpgrade(res, req, context) {
  99. debug("on upgrade");
  100. this.prepare(req, res);
  101. // @ts-ignore
  102. req.res = res;
  103. this.verify(req, true, async (errorCode, errorContext) => {
  104. if (errorCode) {
  105. this.emit("connection_error", {
  106. req,
  107. code: errorCode,
  108. message: server_1.Server.errorMessages[errorCode],
  109. context: errorContext
  110. });
  111. this.abortRequest(res, errorCode, errorContext);
  112. return;
  113. }
  114. const id = req._query.sid;
  115. let transport;
  116. if (id) {
  117. const client = this.clients[id];
  118. if (!client) {
  119. debug("upgrade attempt for closed client");
  120. res.close();
  121. }
  122. else if (client.upgrading) {
  123. debug("transport has already been trying to upgrade");
  124. res.close();
  125. }
  126. else if (client.upgraded) {
  127. debug("transport had already been upgraded");
  128. res.close();
  129. }
  130. else {
  131. debug("upgrading existing transport");
  132. transport = this.createTransport(req._query.transport, req);
  133. client.maybeUpgrade(transport);
  134. }
  135. }
  136. else {
  137. transport = await this.handshake(req._query.transport, req, (errorCode, errorContext) => this.abortRequest(res, errorCode, errorContext));
  138. if (!transport) {
  139. return;
  140. }
  141. }
  142. res.upgrade({
  143. transport
  144. }, req.getHeader("sec-websocket-key"), req.getHeader("sec-websocket-protocol"), req.getHeader("sec-websocket-extensions"), context);
  145. });
  146. }
  147. abortRequest(res, errorCode, errorContext) {
  148. const statusCode = errorCode === server_1.Server.errors.FORBIDDEN
  149. ? "403 Forbidden"
  150. : "400 Bad Request";
  151. const message = errorContext && errorContext.message
  152. ? errorContext.message
  153. : server_1.Server.errorMessages[errorCode];
  154. res.writeStatus(statusCode);
  155. res.writeHeader("Content-Type", "application/json");
  156. res.end(JSON.stringify({
  157. code: errorCode,
  158. message
  159. }));
  160. }
  161. }
  162. exports.uServer = uServer;
  163. class ResponseWrapper {
  164. constructor(res) {
  165. this.res = res;
  166. this.statusWritten = false;
  167. this.headers = [];
  168. }
  169. set statusCode(status) {
  170. this.writeStatus(status === 200 ? "200 OK" : "204 No Content");
  171. }
  172. setHeader(key, value) {
  173. this.writeHeader(key, value);
  174. }
  175. // needed by vary: https://github.com/jshttp/vary/blob/5d725d059b3871025cf753e9dfa08924d0bcfa8f/index.js#L134
  176. getHeader() { }
  177. writeStatus(status) {
  178. this.res.writeStatus(status);
  179. this.statusWritten = true;
  180. this.writeBufferedHeaders();
  181. }
  182. writeHeader(key, value) {
  183. if (key === "Content-Length") {
  184. // the content length is automatically added by uWebSockets.js
  185. return;
  186. }
  187. if (this.statusWritten) {
  188. this.res.writeHeader(key, value);
  189. }
  190. else {
  191. this.headers.push([key, value]);
  192. }
  193. }
  194. writeBufferedHeaders() {
  195. this.headers.forEach(([key, value]) => {
  196. this.res.writeHeader(key, value);
  197. });
  198. }
  199. end(data) {
  200. if (!this.statusWritten) {
  201. // status will be inferred as "200 OK"
  202. this.writeBufferedHeaders();
  203. }
  204. this.res.end(data);
  205. }
  206. onData(fn) {
  207. this.res.onData(fn);
  208. }
  209. onAborted(fn) {
  210. this.res.onAborted(fn);
  211. }
  212. }