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/work.js CHANGED
@@ -16,27 +16,21 @@ limitations under the License.
16
16
 
17
17
  // What one request actually made this framework do, read from state it already keeps.
18
18
  //
19
- // Most of what makes this faster than Express is work that does not happen: the Readable and the
20
- // Writable are not built, the headers are not folded into an object, the query is not parsed, the
21
- // socket stand-in is not allocated. None of that is visible from the outside, and all of it is one
22
- // careless middleware away from coming back: a `req.headers.host` where `req.get("host")` would do
23
- // puts the folded object back on every request, and the answer stays correct, so nothing fails.
19
+ // Most of the speed here is work that does not happen: no Readable, no Writable, no folded headers
20
+ // object, no parsed query, no socket stand-in. One careless middleware brings it back, and the
21
+ // answer stays correct, so nothing fails. Every field below is already kept for other reasons, so
22
+ // asking costs a load and nothing is counted or wrapped for the sake of being asked.
24
23
  //
25
- // Every field below is a property this framework already had to keep for its own reasons, so
26
- // asking costs a load and nothing is counted, stamped or wrapped for the sake of being asked. That
27
- // is the whole design rule here: a probe that charges the requests nobody is probing would be
28
- // paid for by everyone, forever, to be read once.
24
+ // Not here: whether the constructor copied the headers out of uWS. That is about the chain and
25
+ // `routeReport().skipHeaders` reports it already.
29
26
  //
30
- // What is deliberately not here is whether the constructor copied the headers out of µWS. That is
31
- // a decision about the chain rather than about the request, `routeReport().skipHeaders` reports it
32
- // already, and the one case where the two differ, a granted route whose request declares a body,
33
- // would cost a flag written on every request to be read on almost none.
34
- //
35
- // The two readers are `express.testing.expectLazy`, which fails a build that lost one of these,
36
- // and `express.serverTiming()`, which writes them into the header for a browser to show.
27
+ // Read by `express.testing.expectLazy` and by `express.serverTiming()`.
37
28
 
38
29
  "use strict";
39
30
 
31
+ /** @typedef {import("./request.js")} Request */
32
+ /** @typedef {import("./response.js")} Response */
33
+
40
34
  /**
41
35
  * @typedef {object} Work
42
36
  * @property {boolean} native whether µWS matched this route itself
@@ -50,29 +44,30 @@ limitations under the License.
50
44
  */
51
45
 
52
46
  /**
53
- * What this request did, as it stands right now: the answer changes while the chain runs, so a
54
- * reader that wants the whole picture asks at the end of it.
47
+ * What this request did so far. The answer changes while the chain runs, so ask at the end of it.
55
48
  *
56
- * @param {any} req
57
- * @param {any} res the response, since half of this is about the response
49
+ * @param {Request} req
50
+ * @param {Response} res the response, since half of this is about the response
58
51
  * @returns {Work}
59
52
  */
60
53
  function work(req, res) {
61
54
  const native = req.route?._native;
55
+ // cast for the three the classes do not declare: `body` is deliberately not a field of
56
+ // Request, and the two stream states are node's own, written when a lazy stream is built
57
+ const loose = /** @type {any} */ (req);
62
58
  return {
63
59
  native: Boolean(native),
64
60
  declarative: Boolean(native?.declarative),
65
61
  headers: req._headersBuilt,
66
62
  query: req._queryParsed,
67
- body: req.body !== undefined,
68
- requestStream: req._readableState !== undefined,
69
- responseStream: res._writableState !== undefined,
63
+ body: loose.body !== undefined,
64
+ requestStream: loose._readableState !== undefined,
65
+ responseStream: /** @type {any} */ (res)._writableState !== undefined,
70
66
  socket: req._socketBuilt || res._socketBuilt
71
67
  };
72
68
  }
73
69
 
74
- // The order the two readers list them in: what the request was made to do, cheapest first, so a
75
- // header and a failure message read the same way.
70
+ // The order both readers list them in, cheapest first, so a header and a failure message agree.
76
71
  const NAMES = [
77
72
  ["headers", "headers"],
78
73
  ["query", "query"],
@@ -83,8 +78,7 @@ const NAMES = [
83
78
  ];
84
79
 
85
80
  /**
86
- * The names of everything that did happen, for a message or a header. Empty for the request that
87
- * did none of it, which is the one this framework is built to serve.
81
+ * The names of everything that did happen, for a message or a header. Empty is the good case.
88
82
  *
89
83
  * @param {Work} done
90
84
  * @returns {string[]}