@noony-serverless/core 0.4.3 → 0.4.4
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,5 +1,16 @@
|
|
|
1
1
|
import type { FastifyRequest, FastifyReply } from 'fastify';
|
|
2
2
|
import { Handler } from '../core/handler';
|
|
3
3
|
export declare const requestBodyMap: WeakMap<any, FastifyRequest<import("fastify").RouteGenericInterface, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, import("fastify").RouteGenericInterface>>>;
|
|
4
|
+
export interface CloudFunctionRequest {
|
|
5
|
+
method?: string;
|
|
6
|
+
body?: unknown;
|
|
7
|
+
url?: string;
|
|
8
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
9
|
+
__rawBody?: string;
|
|
10
|
+
rawBody?: string | Buffer;
|
|
11
|
+
readableEnded?: boolean;
|
|
12
|
+
complete?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare function extractAndStoreRequestBody(req: CloudFunctionRequest): void;
|
|
4
15
|
export declare function createFastifyHandler(noonyHandler: Handler<unknown>, functionName: string, initializeDependencies: () => Promise<void>): (req: FastifyRequest, reply: FastifyReply) => Promise<void>;
|
|
5
16
|
//# sourceMappingURL=fastify-wrapper.d.ts.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.requestBodyMap = void 0;
|
|
4
|
+
exports.extractAndStoreRequestBody = extractAndStoreRequestBody;
|
|
4
5
|
exports.createFastifyHandler = createFastifyHandler;
|
|
5
6
|
const logger_1 = require("../core/logger");
|
|
6
7
|
// Global WeakMap to store the original Fastify request for each GenericRequest
|
|
@@ -178,10 +179,49 @@ const INTERNAL_ERROR_RESPONSE = Object.freeze({
|
|
|
178
179
|
});
|
|
179
180
|
// Constant for performance-critical string comparison
|
|
180
181
|
const RESPONSE_SENT_MESSAGE = 'RESPONSE_SENT';
|
|
182
|
+
// Helper: Extract and store request body for later use
|
|
183
|
+
function extractAndStoreRequestBody(req) {
|
|
184
|
+
if (!['POST', 'PUT', 'PATCH'].includes(req.method || '')) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
// Check if already has __rawBody from previous processing
|
|
188
|
+
if (req.__rawBody) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const body = req.body;
|
|
192
|
+
if (!body)
|
|
193
|
+
return;
|
|
194
|
+
// Use safe JSON stringify to handle circular references
|
|
195
|
+
try {
|
|
196
|
+
req.__rawBody = typeof body === 'string' ? body : JSON.stringify(body);
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
// If circular reference or other serialization error, skip storing __rawBody
|
|
200
|
+
if (process.env.NODE_ENV === 'development') {
|
|
201
|
+
logger_1.logger.debug('[helper] Failed to stringify body, skipping __rawBody storage', {
|
|
202
|
+
error: error instanceof Error ? error.message : String(error),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (process.env.NODE_ENV === 'development') {
|
|
208
|
+
logger_1.logger.debug('[helper] Extracted and stored __rawBody', {
|
|
209
|
+
bodyLength: req.__rawBody.length,
|
|
210
|
+
bodyType: typeof body,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
181
214
|
function createFastifyHandler(noonyHandler, functionName, initializeDependencies) {
|
|
182
215
|
// Pre-bind the error log prefix to avoid string concatenation in hot path
|
|
183
216
|
const errorLogPrefix = `${functionName} handler error`;
|
|
184
217
|
return async (req, reply) => {
|
|
218
|
+
const requestId = `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
|
|
219
|
+
console.log(`[FASTIFY-WRAPPER] ${errorLogPrefix} called`, {
|
|
220
|
+
requestId,
|
|
221
|
+
url: req.url,
|
|
222
|
+
method: req.method,
|
|
223
|
+
timestamp: new Date().toISOString(),
|
|
224
|
+
});
|
|
185
225
|
try {
|
|
186
226
|
// Ensure dependencies are initialized
|
|
187
227
|
await initializeDependencies();
|
|
@@ -190,6 +230,10 @@ function createFastifyHandler(noonyHandler, functionName, initializeDependencies
|
|
|
190
230
|
const genericRes = adaptFastifyResponse(reply);
|
|
191
231
|
// Execute Noony handler with adapted request/response
|
|
192
232
|
await noonyHandler.executeGeneric(genericReq, genericRes);
|
|
233
|
+
console.log(`[FASTIFY-WRAPPER] ${errorLogPrefix} completed`, {
|
|
234
|
+
requestId,
|
|
235
|
+
timestamp: new Date().toISOString(),
|
|
236
|
+
});
|
|
193
237
|
}
|
|
194
238
|
catch (error) {
|
|
195
239
|
// Fast path: check RESPONSE_SENT first (most common error to ignore)
|
package/package.json
CHANGED