userver.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. req.url = req.getUrl();
  19. const params = new URLSearchParams(req.getQuery());
  20. req._query = Object.fromEntries(params.entries());
  21. req.headers = {};
  22. req.forEach((key, value) => {
  23. req.headers[key] = value;
  24. });
  25. req.connection = {
  26. remoteAddress: Buffer.from(res.getRemoteAddressAsText()).toString(),
  27. };
  28. res.onAborted(() => {
  29. debug("response has been aborted");
  30. });
  31. }
  32. createTransport(transportName, req) {
  33. return new transports_uws_1.default[transportName](req);
  34. }
  35. /**
  36. * Attach the engine to a µWebSockets.js server
  37. * @param app
  38. * @param options
  39. */
  40. attach(app /* : TemplatedApp */, options = {}) {
  41. const path = this._computePath(options);
  42. app
  43. .any(path, this.handleRequest.bind(this))
  44. //
  45. .ws(path, {
  46. compression: options.compression,
  47. idleTimeout: options.idleTimeout,
  48. maxBackpressure: options.maxBackpressure,
  49. maxPayloadLength: this.opts.maxHttpBufferSize,
  50. upgrade: this.handleUpgrade.bind(this),
  51. open: (ws) => {
  52. const transport = ws.getUserData().transport;
  53. transport.socket = ws;
  54. transport.writable = true;
  55. transport.emit("drain");
  56. },
  57. message: (ws, message, isBinary) => {
  58. ws.getUserData().transport.onData(isBinary ? message : Buffer.from(message).toString());
  59. },
  60. close: (ws, code, message) => {
  61. ws.getUserData().transport.onClose(code, message);
  62. },
  63. });
  64. }
  65. _applyMiddlewares(req, res, callback) {
  66. if (this.middlewares.length === 0) {
  67. return callback();
  68. }
  69. // needed to buffer headers until the status is computed
  70. req.res = new ResponseWrapper(res);
  71. super._applyMiddlewares(req, req.res, (err) => {
  72. // some middlewares (like express-session) wait for the writeHead() call to flush their headers
  73. // see https://github.com/expressjs/session/blob/1010fadc2f071ddf2add94235d72224cf65159c6/index.js#L220-L244
  74. req.res.writeHead();
  75. callback(err);
  76. });
  77. }
  78. handleRequest(res, req) {
  79. debug('handling "%s" http request "%s"', req.getMethod(), req.getUrl());
  80. this.prepare(req, res);
  81. req.res = res;
  82. const callback = (errorCode, errorContext) => {
  83. if (errorCode !== undefined) {
  84. this.emit("connection_error", {
  85. req,
  86. code: errorCode,
  87. message: server_1.Server.errorMessages[errorCode],
  88. context: errorContext,
  89. });
  90. this.abortRequest(req.res, errorCode, errorContext);
  91. return;
  92. }
  93. if (req._query.sid) {
  94. debug("setting new request for existing client");
  95. this.clients[req._query.sid].transport.onRequest(req);
  96. }
  97. else {
  98. const closeConnection = (errorCode, errorContext) => this.abortRequest(res, errorCode, errorContext);
  99. this.handshake(req._query.transport, req, closeConnection);
  100. }
  101. };
  102. this._applyMiddlewares(req, res, (err) => {
  103. if (err) {
  104. callback(server_1.Server.errors.BAD_REQUEST, { name: "MIDDLEWARE_FAILURE" });
  105. }
  106. else {
  107. this.verify(req, false, callback);
  108. }
  109. });
  110. }
  111. handleUpgrade(res, req, context) {
  112. debug("on upgrade");
  113. this.prepare(req, res);
  114. req.res = res;
  115. const callback = async (errorCode, errorContext) => {
  116. if (errorCode !== undefined) {
  117. this.emit("connection_error", {
  118. req,
  119. code: errorCode,
  120. message: server_1.Server.errorMessages[errorCode],
  121. context: errorContext,
  122. });
  123. this.abortRequest(res, errorCode, errorContext);
  124. return;
  125. }
  126. const id = req._query.sid;
  127. let transport;
  128. if (id) {
  129. const client = this.clients[id];
  130. if (!client) {
  131. debug("upgrade attempt for closed client");
  132. res.close();
  133. }
  134. else if (client.upgrading) {
  135. debug("transport has already been trying to upgrade");
  136. res.close();
  137. }
  138. else if (client.upgraded) {
  139. debug("transport had already been upgraded");
  140. res.close();
  141. }
  142. else {
  143. debug("upgrading existing transport");
  144. transport = this.createTransport(req._query.transport, req);
  145. client.maybeUpgrade(transport);
  146. }
  147. }
  148. else {
  149. transport = await this.handshake(req._query.transport, req, (errorCode, errorContext) => this.abortRequest(res, errorCode, errorContext));
  150. if (!transport) {
  151. return;
  152. }
  153. }
  154. // calling writeStatus() triggers the flushing of any header added in a middleware
  155. req.res.writeStatus("101 Switching Protocols");
  156. res.upgrade({
  157. transport,
  158. }, req.getHeader("sec-websocket-key"), req.getHeader("sec-websocket-protocol"), req.getHeader("sec-websocket-extensions"), context);
  159. };
  160. this._applyMiddlewares(req, res, (err) => {
  161. if (err) {
  162. callback(server_1.Server.errors.BAD_REQUEST, { name: "MIDDLEWARE_FAILURE" });
  163. }
  164. else {
  165. this.verify(req, true, callback);
  166. }
  167. });
  168. }
  169. abortRequest(res, errorCode, errorContext) {
  170. const statusCode = errorCode === server_1.Server.errors.FORBIDDEN
  171. ? "403 Forbidden"
  172. : "400 Bad Request";
  173. const message = errorContext && errorContext.message
  174. ? errorContext.message
  175. : server_1.Server.errorMessages[errorCode];
  176. res.writeStatus(statusCode);
  177. res.writeHeader("Content-Type", "application/json");
  178. res.end(JSON.stringify({
  179. code: errorCode,
  180. message,
  181. }));
  182. }
  183. }
  184. exports.uServer = uServer;
  185. class ResponseWrapper {
  186. constructor(res) {
  187. this.res = res;
  188. this.statusWritten = false;
  189. this.headers = [];
  190. this.isAborted = false;
  191. }
  192. set statusCode(status) {
  193. if (!status) {
  194. return;
  195. }
  196. // FIXME: handle all status codes?
  197. this.writeStatus(status === 200 ? "200 OK" : "204 No Content");
  198. }
  199. writeHead(status) {
  200. this.statusCode = status;
  201. }
  202. setHeader(key, value) {
  203. if (Array.isArray(value)) {
  204. value.forEach((val) => {
  205. this.writeHeader(key, val);
  206. });
  207. }
  208. else {
  209. this.writeHeader(key, value);
  210. }
  211. }
  212. removeHeader() {
  213. // FIXME: not implemented
  214. }
  215. // needed by vary: https://github.com/jshttp/vary/blob/5d725d059b3871025cf753e9dfa08924d0bcfa8f/index.js#L134
  216. getHeader() { }
  217. writeStatus(status) {
  218. if (this.isAborted)
  219. return;
  220. this.res.writeStatus(status);
  221. this.statusWritten = true;
  222. this.writeBufferedHeaders();
  223. return this;
  224. }
  225. writeHeader(key, value) {
  226. if (this.isAborted)
  227. return;
  228. if (key === "Content-Length") {
  229. // the content length is automatically added by uWebSockets.js
  230. return;
  231. }
  232. if (this.statusWritten) {
  233. this.res.writeHeader(key, value);
  234. }
  235. else {
  236. this.headers.push([key, value]);
  237. }
  238. }
  239. writeBufferedHeaders() {
  240. this.headers.forEach(([key, value]) => {
  241. this.res.writeHeader(key, value);
  242. });
  243. }
  244. end(data) {
  245. if (this.isAborted)
  246. return;
  247. this.res.cork(() => {
  248. if (!this.statusWritten) {
  249. // status will be inferred as "200 OK"
  250. this.writeBufferedHeaders();
  251. }
  252. this.res.end(data);
  253. });
  254. }
  255. onData(fn) {
  256. if (this.isAborted)
  257. return;
  258. this.res.onData(fn);
  259. }
  260. onAborted(fn) {
  261. if (this.isAborted)
  262. return;
  263. this.res.onAborted(() => {
  264. // Any attempt to use the UWS response object after abort will throw!
  265. this.isAborted = true;
  266. fn();
  267. });
  268. }
  269. cork(fn) {
  270. if (this.isAborted)
  271. return;
  272. this.res.cork(fn);
  273. }
  274. }