@nestjs/platform-fastify 12.0.2 → 12.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/platform-fastify",
3
- "version": "12.0.2",
3
+ "version": "12.0.3",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@platform-fastify)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "license": "MIT",
@@ -49,5 +49,5 @@
49
49
  "optional": true
50
50
  }
51
51
  },
52
- "gitHead": "ee168a569d30e600d301ed38abb14d2171a6f315"
52
+ "gitHead": "14151790e1abe870abc3bbf00f88786fc3ebe509"
53
53
  }
@@ -1,46 +0,0 @@
1
- import { FastifyInstance, FastifyPluginCallback } from 'fastify';
2
- import * as http from 'node:http';
3
- export type MiddlewareFn<Req extends {
4
- url: string;
5
- originalUrl?: string;
6
- }, Res extends {
7
- finished?: boolean;
8
- writableEnded?: boolean;
9
- }, Ctx = unknown> = (req: Req, res: Res, next: (err?: unknown) => void) => void;
10
- declare const supportedHooks: readonly ["onError", "onSend", "preParsing", "preSerialization", "onRequest", "onResponse", "onTimeout", "preHandler", "preValidation"];
11
- type SupportedHook = (typeof supportedHooks)[number];
12
- interface MiddieOptions {
13
- hook?: SupportedHook;
14
- }
15
- declare function fastifyMiddie(fastify: FastifyInstance, options: MiddieOptions, next: (err?: Error) => void): void;
16
- declare namespace fastifyMiddie {
17
- export interface FastifyMiddieOptions {
18
- hook?: 'onRequest' | 'preParsing' | 'preValidation' | 'preHandler' | 'preSerialization' | 'onSend' | 'onResponse' | 'onTimeout' | 'onError';
19
- }
20
- type FastifyMiddie = FastifyPluginCallback<fastifyMiddie.FastifyMiddieOptions>;
21
- export interface IncomingMessageExtended {
22
- body?: any;
23
- query?: any;
24
- }
25
- export type NextFunction = (err?: any) => void;
26
- export type SimpleHandleFunction = (req: http.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse) => void;
27
- export type NextHandleFunction = (req: http.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse, next: NextFunction) => void;
28
- export type Handler = SimpleHandleFunction | NextHandleFunction;
29
- export const fastifyMiddie: FastifyMiddie;
30
- export { fastifyMiddie as default };
31
- }
32
- declare module 'fastify' {
33
- interface FastifyInstance {
34
- use(fn: fastifyMiddie.Handler): this;
35
- use(route: string, fn: fastifyMiddie.Handler): this;
36
- use(routes: string[], fn: fastifyMiddie.Handler): this;
37
- }
38
- }
39
- /**
40
- * A clone of `@fastify/middie` engine https://github.com/fastify/middie
41
- * with an extra vulnerability fix. Path is now decoded before matching to
42
- * avoid bypassing middleware with encoded characters.
43
- */
44
- declare const _default: typeof fastifyMiddie;
45
- export default _default;
46
- export { fastifyMiddie };
@@ -1,251 +0,0 @@
1
- import fp from 'fastify-plugin';
2
- import urlSanitizer from 'find-my-way/lib/url-sanitizer.js';
3
- import { pathToRegexp } from 'path-to-regexp';
4
- import reusify from 'reusify';
5
- const { safeDecodeURI } = urlSanitizer;
6
- function bindLast(fn, last) {
7
- return (...args) => fn(...args, last);
8
- }
9
- /**
10
- * A clone of `@fastify/middie` engine https://github.com/fastify/middie
11
- * with an extra vulnerability fix. Path is now decoded before matching to
12
- * avoid bypassing middleware with encoded characters.
13
- */
14
- function middie(complete, initialConfig) {
15
- const middlewares = [];
16
- const pool = reusify(Holder);
17
- return {
18
- use,
19
- run: bindLast(run, initialConfig),
20
- };
21
- function use(url, f) {
22
- if (f === undefined) {
23
- f = url;
24
- url = null;
25
- }
26
- let regexp;
27
- if (typeof url === 'string') {
28
- const pathRegExp = pathToRegexp(sanitizePrefixUrl(url), {
29
- end: false,
30
- });
31
- regexp = pathRegExp.regexp;
32
- }
33
- if (Array.isArray(f)) {
34
- for (const val of f) {
35
- middlewares.push({ regexp, fn: val });
36
- }
37
- }
38
- else {
39
- middlewares.push({ regexp, fn: f });
40
- }
41
- return this;
42
- }
43
- function run(req, res, ctx, initialConfig) {
44
- if (!middlewares.length) {
45
- complete(null, req, res, ctx);
46
- return;
47
- }
48
- req.originalUrl = req.url;
49
- const holder = pool.get();
50
- holder.req = req;
51
- holder.res = res;
52
- holder.url = sanitizeUrl(req.url);
53
- holder.context = ctx;
54
- holder.initialConfig = initialConfig;
55
- holder.done();
56
- }
57
- function Holder() {
58
- this.req = null;
59
- this.res = null;
60
- this.url = null;
61
- this.context = null;
62
- this.initialConfig = null;
63
- this.i = 0;
64
- const that = this;
65
- this.done = function (err) {
66
- const req = that.req;
67
- const res = that.res;
68
- const url = that.url;
69
- const context = that.context;
70
- const i = that.i++;
71
- req.url = req.originalUrl;
72
- if (res.finished === true || res.writableEnded === true) {
73
- cleanup();
74
- return;
75
- }
76
- if (err || middlewares.length === i) {
77
- complete(err, req, res, context);
78
- cleanup();
79
- }
80
- else {
81
- const { fn, regexp } = middlewares[i];
82
- if (regexp) {
83
- // Decode URL before matching to avoid bypassing middleware
84
- let sanitizedUrl = url;
85
- if (that.initialConfig.ignoreDuplicateSlashes ||
86
- that.initialConfig.routerOptions?.ignoreDuplicateSlashes) {
87
- sanitizedUrl = removeDuplicateSlashes(sanitizedUrl);
88
- }
89
- if (that.initialConfig.ignoreTrailingSlash ||
90
- that.initialConfig.routerOptions?.ignoreTrailingSlash) {
91
- sanitizedUrl = trimLastSlash(sanitizedUrl);
92
- }
93
- if (that.initialConfig.caseSensitive === false ||
94
- that.initialConfig.routerOptions?.caseSensitive === false) {
95
- sanitizedUrl = sanitizedUrl.toLowerCase();
96
- }
97
- const decodedUrl = safeDecodeURI(sanitizedUrl, that.initialConfig?.routerOptions?.useSemicolonDelimiter ||
98
- that.initialConfig?.useSemicolonDelimiter).path;
99
- const result = regexp.exec(decodedUrl);
100
- if (result) {
101
- req.url = req.url.replace(result[0], '');
102
- if (req.url[0] !== '/')
103
- req.url = '/' + req.url;
104
- fn(req, res, that.done);
105
- }
106
- else {
107
- that.done();
108
- }
109
- }
110
- else {
111
- fn(req, res, that.done);
112
- }
113
- }
114
- };
115
- function cleanup() {
116
- that.req = null;
117
- that.res = null;
118
- that.context = null;
119
- that.initialConfig = null;
120
- that.i = 0;
121
- pool.release(that);
122
- }
123
- }
124
- }
125
- function removeDuplicateSlashes(path) {
126
- const REMOVE_DUPLICATE_SLASHES_REGEXP = /\/\/+/g;
127
- return path.indexOf('//') !== -1
128
- ? path.replace(REMOVE_DUPLICATE_SLASHES_REGEXP, '/')
129
- : path;
130
- }
131
- function trimLastSlash(path) {
132
- if (path.length > 1 && path.charCodeAt(path.length - 1) === 47) {
133
- return path.slice(0, -1);
134
- }
135
- return path;
136
- }
137
- function sanitizeUrl(url) {
138
- for (let i = 0, len = url.length; i < len; i++) {
139
- const charCode = url.charCodeAt(i);
140
- if (charCode === 63 || charCode === 35) {
141
- return url.slice(0, i);
142
- }
143
- }
144
- return url;
145
- }
146
- function sanitizePrefixUrl(url) {
147
- if (url === '/')
148
- return '';
149
- if (url[url.length - 1] === '/')
150
- return url.slice(0, -1);
151
- return url;
152
- }
153
- const kMiddlewares = Symbol('fastify-middie-middlewares');
154
- const kMiddie = Symbol('fastify-middie-instance');
155
- const kMiddieHasMiddlewares = Symbol('fastify-middie-has-middlewares');
156
- const supportedHooksWithPayload = [
157
- 'onError',
158
- 'onSend',
159
- 'preParsing',
160
- 'preSerialization',
161
- ];
162
- const supportedHooksWithoutPayload = [
163
- 'onRequest',
164
- 'onResponse',
165
- 'onTimeout',
166
- 'preHandler',
167
- 'preValidation',
168
- ];
169
- const supportedHooks = [
170
- ...supportedHooksWithPayload,
171
- ...supportedHooksWithoutPayload,
172
- ];
173
- function fastifyMiddie(fastify, options, next) {
174
- fastify.decorate('use', use);
175
- fastify[kMiddlewares] = [];
176
- fastify[kMiddieHasMiddlewares] = false;
177
- fastify[kMiddie] = middie(onMiddieEnd, fastify.initialConfig);
178
- const hook = options.hook || 'onRequest';
179
- if (!supportedHooks.includes(hook)) {
180
- next(new Error(`The hook "${hook}" is not supported by fastify-middie`));
181
- return;
182
- }
183
- fastify
184
- .addHook(hook, supportedHooksWithPayload.includes(hook)
185
- ? runMiddieWithPayload
186
- : runMiddie)
187
- .addHook('onRegister', onRegister);
188
- function use(path, fn) {
189
- if (typeof path === 'string') {
190
- const prefix = this.prefix;
191
- path = prefix + (path === '/' && prefix.length > 0 ? '' : path);
192
- }
193
- this[kMiddlewares].push([path, fn]);
194
- if (fn == null) {
195
- this[kMiddie].use(path);
196
- }
197
- else {
198
- this[kMiddie].use(path, fn);
199
- }
200
- this[kMiddieHasMiddlewares] = true;
201
- return this;
202
- }
203
- function runMiddie(req, reply, next) {
204
- if (this[kMiddieHasMiddlewares]) {
205
- const raw = req.raw;
206
- raw.id = req.id;
207
- raw.hostname = req.hostname;
208
- raw.protocol = req.protocol;
209
- raw.ip = req.ip;
210
- raw.ips = req.ips;
211
- raw.log = req.log;
212
- req.raw.query = req.query;
213
- reply.raw.log = req.log;
214
- if (req.body !== undefined)
215
- req.raw.body = req.body;
216
- this[kMiddie].run(req.raw, reply.raw, next);
217
- }
218
- else {
219
- next();
220
- }
221
- }
222
- function runMiddieWithPayload(req, reply, _payload, next) {
223
- runMiddie.bind(this)(req, reply, next);
224
- }
225
- function onMiddieEnd(err, _req, _res, next) {
226
- next(err);
227
- }
228
- function onRegister(instance) {
229
- const middlewares = instance[kMiddlewares].slice();
230
- instance[kMiddlewares] = [];
231
- instance[kMiddie] = middie(onMiddieEnd, instance.initialConfig);
232
- instance[kMiddieHasMiddlewares] = false;
233
- instance.decorate('use', use);
234
- for (const middleware of middlewares) {
235
- instance[kMiddlewares].push(middleware);
236
- instance[kMiddie].use(...middleware);
237
- }
238
- instance[kMiddieHasMiddlewares] = middlewares.length > 0;
239
- }
240
- next();
241
- }
242
- /**
243
- * A clone of `@fastify/middie` engine https://github.com/fastify/middie
244
- * with an extra vulnerability fix. Path is now decoded before matching to
245
- * avoid bypassing middleware with encoded characters.
246
- */
247
- export default fp(fastifyMiddie, {
248
- fastify: '5.x',
249
- name: '@fastify/middie',
250
- });
251
- export { fastifyMiddie };