fulmine.js 5.19.2 → 5.19.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/nest.js CHANGED
@@ -14,8 +14,7 @@ See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */
16
16
 
17
- // require("fulmine.js/nest"): the Nest HTTP adapter, so a Nest application runs on µWS without
18
- // anyone having to write this file themselves.
17
+ // require("fulmine.js/nest"): the Nest HTTP adapter, so a Nest application runs on uWS.
19
18
  //
20
19
  // import { NestFactory } from "@nestjs/core";
21
20
  // import { FulmineExpressAdapter } from "fulmine.js/nest";
@@ -23,24 +22,17 @@ limitations under the License.
23
22
  // const app = await NestFactory.create(AppModule, new FulmineExpressAdapter());
24
23
  // await app.listen(3000);
25
24
  //
26
- // @nestjs/platform-express takes any Express instance, and this is one, so everything above the
27
- // adapter - controllers, pipes, guards, interceptors - is untouched. Three things below it are not,
28
- // and they are the whole reason this file exists:
25
+ // @nestjs/platform-express takes any Express instance and this is one, so controllers, pipes,
26
+ // guards and interceptors are untouched. Only three methods below the adapter need overriding:
29
27
  //
30
- // - initHttpServer wraps the instance in http.createServer() and listens on that. Every request
31
- // would then arrive through node's parser and be replayed into µWS's shapes by node-shim.js,
32
- // which is the slow path that exists for supertest. The app already answers as an http.Server,
33
- // so it is the server instead of being put inside one.
34
- // - registerParserMiddleware decides whether Nest's body parsers are already in the chain by
35
- // scanning app.router.stack for them. There is no layer array here to scan, routes are compiled
36
- // rather than kept as layers, so the answer was always "no" and a second call added a second
37
- // pair. It is remembered here instead, which is the same answer by a different route.
38
- // - httpsOptions asks node to make a TLS server out of the instance. TLS here belongs to µWS and
39
- // is configured when the app is built, so that combination is refused with the line to write
40
- // rather than silently starting a plaintext server.
28
+ // - initHttpServer wraps the instance in http.createServer(). That would push every request
29
+ // through node's parser and node-shim.js, the slow path. The app is already an http.Server.
30
+ // - registerParserMiddleware looks for Nest's body parsers in app.router.stack. There is no
31
+ // layer array here, so the answer was always "no" and a second call added a second pair.
32
+ // - httpsOptions asks node for a TLS server. TLS belongs to uWS and is set when the app is
33
+ // built, so that combination is refused.
41
34
  //
42
- // @nestjs/platform-express is an optional peer dependency: this file is the only one that requires
43
- // it, and nothing loads this file unless you ask for it by name.
35
+ // @nestjs/platform-express is an optional peer dependency, only this file requires it.
44
36
 
45
37
  "use strict";
46
38
 
@@ -48,10 +40,10 @@ const { ExpressAdapter } = require("@nestjs/platform-express");
48
40
  const fulmine = require("./index.js");
49
41
 
50
42
  /**
51
- * Nest's Express adapter, listening on µWebSockets.js instead of on node.
43
+ * Nest's Express adapter, listening on uWebSockets.js instead of node.
52
44
  *
53
- * Pass a configured app when you need one, `new FulmineExpressAdapter(fulmine({ uwsOptions }))`;
54
- * with no argument it builds a default one, the same as `new ExpressAdapter()` does.
45
+ * Pass a configured app when you need one, `new FulmineExpressAdapter(fulmine({ uwsOptions }))`.
46
+ * With no argument it builds a default one, like `new ExpressAdapter()`.
55
47
  */
56
48
  class FulmineExpressAdapter extends ExpressAdapter {
57
49
  /**
@@ -60,8 +52,7 @@ class FulmineExpressAdapter extends ExpressAdapter {
60
52
  constructor(instance) {
61
53
  super(instance || fulmine());
62
54
  /**
63
- * Whether Nest's body parsers are in the chain, standing in for the layer array Express
64
- * has and this does not. See registerParserMiddleware below.
55
+ * Stands in for the layer array Express has. See registerParserMiddleware below.
65
56
  * @type {boolean}
66
57
  */
67
58
  this._parsersRegistered = false;
@@ -84,10 +75,7 @@ class FulmineExpressAdapter extends ExpressAdapter {
84
75
  this.httpServer = this.getInstance();
85
76
  if (options?.forceCloseConnections) {
86
77
  // trackOpenConnections() listens for 'connection', which nothing emits: the sockets
87
- // belong to µWS and never become node ones. Said out loud, because a shutdown that
88
- // quietly waits forever for what it thinks it can destroy is worse than one that does
89
- // not offer to. Through Nest's own logger, so the line arrives where every other line
90
- // from the framework does; it is private in the typings and inherited all the same
78
+ // belong to uWS. Warned through Nest's own logger, private in the typings but there
91
79
  /** @type {any} */ (this).logger.warn(
92
80
  "forceCloseConnections has no effect on fulmine.js: the sockets belong to µWS. " +
93
81
  "app.close() stops accepting and waits for the requests in flight; an idle keep-alive " +
@@ -97,11 +85,9 @@ class FulmineExpressAdapter extends ExpressAdapter {
97
85
  }
98
86
 
99
87
  /**
100
- * Nest's json and urlencoded parsers, added once however often this is called.
101
- *
102
- * Express answers "are they there already" by scanning `app.router.stack` for a layer whose
103
- * handler is named `jsonParser` or `urlencodedParser`. There is no such array here, so the scan
104
- * answered no every time and a second call put a second pair in front of every request.
88
+ * Nest's json and urlencoded parsers, added once however often this is called. Express looks
89
+ * for them in `app.router.stack`; there is no such array here, so a second call was putting a
90
+ * second pair in front of every request.
105
91
  *
106
92
  * @param {string} [prefix]
107
93
  * @param {boolean} [rawBody]
@@ -114,6 +100,5 @@ class FulmineExpressAdapter extends ExpressAdapter {
114
100
  }
115
101
  }
116
102
 
117
- // a named export and nothing else: `import { FulmineExpressAdapter } from "fulmine.js/nest"`, which
118
- // is how @nestjs/platform-express exports ExpressAdapter too
103
+ // a named export, the way @nestjs/platform-express exports ExpressAdapter
119
104
  module.exports = { FulmineExpressAdapter };
package/src/node-shim.js CHANGED
@@ -15,12 +15,12 @@ limitations under the License.
15
15
  */
16
16
 
17
17
  // A uWS-shaped request and response backed by node's own, so an app can serve what arrived through
18
- // http.createServer. Nothing on this path is fast, and it is not meant to be: it is what lets
19
- // supertest and http.createServer(app) work, which is what an app has to be a function for.
18
+ // http.createServer. Nothing here is fast and it does not need to be: it is what makes supertest
19
+ // and http.createServer(app) work.
20
20
  //
21
- // Request and Response ask uWS for eighteen things and this answers all eighteen. Where the two
22
- // models disagree node's gives way: cork only runs its callback, and a status is remembered rather
23
- // than sent, since node writes the head with the first byte of body.
21
+ // Request and Response ask uWS for eighteen things and this answers all eighteen. Where the models
22
+ // disagree node's gives way: cork only runs its callback, and a status is remembered rather than
23
+ // sent, since node writes the head with the first byte of body.
24
24
 
25
25
  const { IncomingMessage } = require("http");
26
26
 
@@ -92,9 +92,8 @@ function toArrayBuffer(chunk) {
92
92
  /**
93
93
  * What uWS calls an HttpRequest, over node's IncomingMessage.
94
94
  *
95
- * Only valid for as long as the response is, which here is longer than uWS allows: node keeps the
96
- * headers alive, so nothing has to be copied out in a hurry. Request copies them anyway, since it
97
- * cannot tell which kind of request it is holding.
95
+ * Valid as long as the response is, which is longer than uWS allows: node keeps the headers alive.
96
+ * Request copies them anyway, since it cannot tell which kind of request it is holding.
98
97
  */
99
98
  class NodeHttpRequest {
100
99
  /** @param {import("http").IncomingMessage} req */
@@ -162,9 +161,8 @@ class NodeHttpRequest {
162
161
  /**
163
162
  * What uWS calls an HttpResponse, over node's ServerResponse.
164
163
  *
165
- * The status and the headers are held until node writes the head on its own, which it does when the
166
- * first byte of body goes out. That is why writeStatus only remembers: sending it here would send
167
- * the headers too, before the ones still to come had been set.
164
+ * The status and the headers are held until node writes the head, which it does with the first byte
165
+ * of body. So writeStatus only remembers: sending it here would send the headers too early.
168
166
  */
169
167
  class NodeHttpResponse {
170
168
  /**
@@ -391,7 +389,7 @@ class NodeHttpResponse {
391
389
 
392
390
  /**
393
391
  * Whether these are node's own request and response rather than this project's.
394
- * @param {any} req
392
+ * @param {any} req anything a caller handed the router, which is the point of the check
395
393
  */
396
394
  function isNodeRequest(req) {
397
395
  return req instanceof IncomingMessage;
@@ -400,7 +398,7 @@ function isNodeRequest(req) {
400
398
  /**
401
399
  * Serves a request that arrived through node's HTTP server with the given router or app.
402
400
  *
403
- * @param {any} router
401
+ * @param {any} router the router or application serving this request
404
402
  * @param {import("http").IncomingMessage} nodeReq
405
403
  * @param {import("http").ServerResponse} nodeRes
406
404
  * @param {(err?: any) => void} [next] called when nothing in the router answered