@kizunajs/fastify 2.0.0-beta.2 → 2.0.0-beta.21

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/dist/index.cjs CHANGED
@@ -26,6 +26,19 @@ fastify_plugin = __toESM(fastify_plugin, 1);
26
26
  let node_stream = require("node:stream");
27
27
  let kizunajs_adapter = require("kizunajs/adapter");
28
28
  //#region src/server.ts
29
+ /**
30
+ * Read a `rawBody` route's body once, keep the text, and hand Fastify a fresh
31
+ * stream to parse, so the route gets both the text and the parsed body.
32
+ */
33
+ const keepRawBody = async (request, _reply, payload) => {
34
+ const chunks = [];
35
+ for await (const chunk of payload) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
36
+ const buffer = Buffer.concat(chunks);
37
+ request.kizunaRawBody = buffer.toString("utf8");
38
+ const replay = node_stream.Readable.from([buffer]);
39
+ replay.receivedEncodedLength = buffer.length;
40
+ return replay;
41
+ };
29
42
  const writeStream = (open, res, validate) => {
30
43
  const controller = new AbortController();
31
44
  res.on("close", () => {
@@ -53,7 +66,7 @@ const writeWebResponse = async (response, reply) => {
53
66
  }
54
67
  node_stream.Readable.fromWeb(response.body).pipe(reply.raw);
55
68
  };
56
- const adapter = (0, kizunajs_adapter.createAdapter)({
69
+ const adapter = (0, kizunajs_adapter.defineAdapter)({
57
70
  buildHandlerContext: (adapterRequest, { reply }) => ({
58
71
  request: adapterRequest.request,
59
72
  reply
@@ -102,6 +115,7 @@ const fastifyKizuna = (0, fastify_plugin.default)(async (app, options) => {
102
115
  preHandler: [async (request) => {
103
116
  request.kizunaRoute = route;
104
117
  }],
118
+ ...route.rawBody === true ? { preParsing: [keepRawBody] } : {},
105
119
  handler: async (request, reply) => {
106
120
  const adapterRequest = {
107
121
  request,
@@ -114,7 +128,8 @@ const fastifyKizuna = (0, fastify_plugin.default)(async (app, options) => {
114
128
  },
115
129
  query: request.query ?? {},
116
130
  headers: request.headers,
117
- readBody: () => request.body
131
+ readBody: () => request.body,
132
+ readRawBody: () => request.kizunaRawBody ?? ""
118
133
  };
119
134
  await adapter.handle({
120
135
  routes: lane,
package/dist/index.d.cts CHANGED
@@ -25,6 +25,10 @@ interface FastifyHandlerContext {
25
25
  declare module 'fastify' {
26
26
  interface FastifyRequest {
27
27
  kizunaRoute?: RouteDefinition;
28
+ /**
29
+ * The body as it was sent, kept for routes with `rawBody: true`.
30
+ */
31
+ kizunaRawBody?: string;
28
32
  }
29
33
  }
30
34
  type FastifyPreHandler = (request: FastifyRequest, reply: FastifyReply) => void | Promise<void>;
package/dist/index.d.mts CHANGED
@@ -25,6 +25,10 @@ interface FastifyHandlerContext {
25
25
  declare module 'fastify' {
26
26
  interface FastifyRequest {
27
27
  kizunaRoute?: RouteDefinition;
28
+ /**
29
+ * The body as it was sent, kept for routes with `rawBody: true`.
30
+ */
31
+ kizunaRawBody?: string;
28
32
  }
29
33
  }
30
34
  type FastifyPreHandler = (request: FastifyRequest, reply: FastifyReply) => void | Promise<void>;
package/dist/index.mjs CHANGED
@@ -1,7 +1,20 @@
1
1
  import fastifyPlugin from "fastify-plugin";
2
2
  import { Readable, pipeline } from "node:stream";
3
- import { GUARDS_META, JOBS_META, REQUEST_CONTEXT_META, ROUTER_META, SCHEMES_META, createAdapter, jobRouter, jobRoutes, jobRunnerFrom, pluginExportsOf, pluginRouterOf, pluginRoutesOf, renderJsonResult } from "kizunajs/adapter";
3
+ import { GUARDS_META, JOBS_META, REQUEST_CONTEXT_META, ROUTER_META, SCHEMES_META, defineAdapter, jobRouter, jobRoutes, jobRunnerFrom, pluginExportsOf, pluginRouterOf, pluginRoutesOf, renderJsonResult } from "kizunajs/adapter";
4
4
  //#region src/server.ts
5
+ /**
6
+ * Read a `rawBody` route's body once, keep the text, and hand Fastify a fresh
7
+ * stream to parse, so the route gets both the text and the parsed body.
8
+ */
9
+ const keepRawBody = async (request, _reply, payload) => {
10
+ const chunks = [];
11
+ for await (const chunk of payload) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
12
+ const buffer = Buffer.concat(chunks);
13
+ request.kizunaRawBody = buffer.toString("utf8");
14
+ const replay = Readable.from([buffer]);
15
+ replay.receivedEncodedLength = buffer.length;
16
+ return replay;
17
+ };
5
18
  const writeStream = (open, res, validate) => {
6
19
  const controller = new AbortController();
7
20
  res.on("close", () => {
@@ -29,7 +42,7 @@ const writeWebResponse = async (response, reply) => {
29
42
  }
30
43
  Readable.fromWeb(response.body).pipe(reply.raw);
31
44
  };
32
- const adapter = createAdapter({
45
+ const adapter = defineAdapter({
33
46
  buildHandlerContext: (adapterRequest, { reply }) => ({
34
47
  request: adapterRequest.request,
35
48
  reply
@@ -78,6 +91,7 @@ const fastifyKizuna = fastifyPlugin(async (app, options) => {
78
91
  preHandler: [async (request) => {
79
92
  request.kizunaRoute = route;
80
93
  }],
94
+ ...route.rawBody === true ? { preParsing: [keepRawBody] } : {},
81
95
  handler: async (request, reply) => {
82
96
  const adapterRequest = {
83
97
  request,
@@ -90,7 +104,8 @@ const fastifyKizuna = fastifyPlugin(async (app, options) => {
90
104
  },
91
105
  query: request.query ?? {},
92
106
  headers: request.headers,
93
- readBody: () => request.body
107
+ readBody: () => request.body,
108
+ readRawBody: () => request.kizunaRawBody ?? ""
94
109
  };
95
110
  await adapter.handle({
96
111
  routes: lane,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizunajs/fastify",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.21",
4
4
  "description": "Fastify adapter for Kizuna.js",
5
5
  "keywords": [
6
6
  "kizunajs",
@@ -58,16 +58,16 @@
58
58
  "fastify-plugin": "^5.1.0"
59
59
  },
60
60
  "peerDependencies": {
61
+ "kizunajs": "2.0.0-beta.21",
61
62
  "fastify": "^5.0.0",
62
- "zod": "^4.0.0",
63
- "kizunajs": "2.0.0-beta.2"
63
+ "zod": "^4.0.0"
64
64
  },
65
65
  "devDependencies": {
66
+ "kizunajs": "2.0.0-beta.21",
66
67
  "fastify": "^5.0.0",
67
68
  "tsdown": "^0.21.0",
68
69
  "typescript": "^6.0.3",
69
- "zod": "^4.0.0",
70
- "kizunajs": "2.0.0-beta.2"
70
+ "zod": "^4.0.0"
71
71
  },
72
72
  "scripts": {
73
73
  "build": "tsdown src/index.ts --format esm,cjs --dts --clean --external kizunajs --external fastify --external fastify-plugin --external zod",