@tsed/platform-fastify 8.46.2 → 8.46.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.
@@ -1,7 +1,7 @@
1
1
  import "@tsed/platform-multer/fastify";
2
2
  import * as Http from "node:http";
3
3
  import * as Https from "node:https";
4
- import { $alter, $asyncEmit } from "@tsed/hooks";
4
+ import { $alter, $asyncEmit, hooks } from "@tsed/hooks";
5
5
  import { isFunction, isString } from "@tsed/core";
6
6
  import Fastify from "fastify";
7
7
  import { adapter, createContext, createServer, PlatformAdapter, PlatformBuilder, PlatformRequest, PlatformResponse } from "@tsed/platform-http";
@@ -15,6 +15,20 @@ import { PlatformFastifyResponse } from "../services/PlatformFastifyResponse.js"
15
15
  import { convertPath } from "../utils/convertPath.js";
16
16
  import fastifyMiddie from "@fastify/middie";
17
17
  import { toPrefix } from "../utils/toPrefix.js";
18
+ /**
19
+ * Finish the request context once the Node.js response ends. Shared by every request: the context is read from
20
+ * the request attached to the response, so no closure is kept alive until the response ends.
21
+ */
22
+ function finishContext() {
23
+ const req = this.req;
24
+ const $ctx = req.$ctx;
25
+ // "error" may follow "finish": the context is finished once
26
+ if (!$ctx) {
27
+ return;
28
+ }
29
+ req.$ctx = undefined;
30
+ $ctx.finish().catch((error) => logger().error({ event: "FINISH_ERROR", reqId: $ctx.id, error }));
31
+ }
18
32
  function callNext(next, metadata, $ctx) {
19
33
  if (metadata.type !== PlatformHandlerType.RESPONSE_FN) {
20
34
  return next && $ctx.error && !$ctx.isDone() ? next($ctx.error) : next();
@@ -99,18 +113,21 @@ export class PlatformFastify extends PlatformAdapter {
99
113
  const { app } = this;
100
114
  const invoke = createContext();
101
115
  const rawApp = app.getApp();
102
- rawApp.addHook("onRequest", async (request, reply) => {
116
+ // Callback hooks: Fastify continues synchronously when no $onRequest listener has to be awaited.
117
+ rawApp.addHook("onRequest", (request, reply, done) => {
103
118
  const $ctx = invoke({
104
119
  request: request,
105
120
  response: reply
106
121
  });
107
- $ctx.request.getReq().$ctx = $ctx;
108
- await $ctx.start();
109
- });
110
- rawApp.addHook("onResponse", async (request, reply) => {
111
- const { $ctx } = request;
112
- $ctx.request.getReq().$ctx = undefined;
113
- await $ctx.finish();
122
+ request.raw.$ctx = $ctx;
123
+ // Listen on the Node.js response instead of a Fastify onResponse hook: without onResponse hook
124
+ // (and without Fastify logger), Fastify skips its own response listeners and hook runner.
125
+ reply.raw.on("finish", finishContext);
126
+ reply.raw.on("error", finishContext);
127
+ if (!hooks.has("$onRequest")) {
128
+ return done();
129
+ }
130
+ $ctx.start().then(() => done(), done);
114
131
  });
115
132
  await rawApp.register(fastifyMiddie, {
116
133
  hook: "onRequest"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/platform-fastify",
3
- "version": "8.46.2",
3
+ "version": "8.46.3",
4
4
  "description": "Fastify package for Ts.ED framework",
5
5
  "keywords": [
6
6
  "Controller",
@@ -61,11 +61,11 @@
61
61
  "@fastify/cookie": "11.0.1",
62
62
  "@fastify/formbody": "8.0.1",
63
63
  "@fastify/session": "11.0.1",
64
- "@tsed/barrels": "8.46.2",
65
- "@tsed/core": "8.46.2",
66
- "@tsed/di": "8.46.2",
67
- "@tsed/platform-http": "8.46.2",
68
- "@tsed/platform-test-sdk": "8.46.2",
64
+ "@tsed/barrels": "8.46.3",
65
+ "@tsed/core": "8.46.3",
66
+ "@tsed/di": "8.46.3",
67
+ "@tsed/platform-http": "8.46.3",
68
+ "@tsed/platform-test-sdk": "8.46.3",
69
69
  "@types/content-disposition": "^0.5.4",
70
70
  "cross-env": "7.0.3",
71
71
  "fastify": "5.8.3",