@noony-serverless/core 0.4.3 → 0.4.5

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)
@@ -6,5 +6,5 @@ export { asString, asStringArray, asNumber, asBoolean, } from './query-param.uti
6
6
  export { isPubSubMessage, extractTraceContext, injectTraceContext, createParentContext, type PubSubMessage, type TraceContext, } from './pubsub-trace.utils';
7
7
  export { createOTELMixin, getOTELContext, getOTELContextFromSpan, getOTELContextFromContext, formatTraceIdForCloudLogging, createCloudLoggingEntry, isOTELActive, isOTELInstalled, type OTELLogContext, } from './otel.helper';
8
8
  export { createHttpFunction, wrapNoonyHandler } from './wrapper-utils';
9
- export { createFastifyHandler } from './fastify-wrapper';
9
+ export { createFastifyHandler, extractAndStoreRequestBody, } from './fastify-wrapper';
10
10
  //# sourceMappingURL=index.d.ts.map
@@ -3,7 +3,7 @@
3
3
  * Utility functions for Noony Core
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createFastifyHandler = exports.wrapNoonyHandler = exports.createHttpFunction = exports.isOTELInstalled = exports.isOTELActive = exports.createCloudLoggingEntry = exports.formatTraceIdForCloudLogging = exports.getOTELContextFromContext = exports.getOTELContextFromSpan = exports.getOTELContext = exports.createOTELMixin = exports.createParentContext = exports.injectTraceContext = exports.extractTraceContext = exports.isPubSubMessage = exports.asBoolean = exports.asNumber = exports.asStringArray = exports.asString = exports.getService = void 0;
6
+ exports.extractAndStoreRequestBody = exports.createFastifyHandler = exports.wrapNoonyHandler = exports.createHttpFunction = exports.isOTELInstalled = exports.isOTELActive = exports.createCloudLoggingEntry = exports.formatTraceIdForCloudLogging = exports.getOTELContextFromContext = exports.getOTELContextFromSpan = exports.getOTELContext = exports.createOTELMixin = exports.createParentContext = exports.injectTraceContext = exports.extractTraceContext = exports.isPubSubMessage = exports.asBoolean = exports.asNumber = exports.asStringArray = exports.asString = exports.getService = void 0;
7
7
  // Container utilities
8
8
  var container_utils_1 = require("./container.utils");
9
9
  Object.defineProperty(exports, "getService", { enumerable: true, get: function () { return container_utils_1.getService; } });
@@ -35,4 +35,5 @@ Object.defineProperty(exports, "createHttpFunction", { enumerable: true, get: fu
35
35
  Object.defineProperty(exports, "wrapNoonyHandler", { enumerable: true, get: function () { return wrapper_utils_1.wrapNoonyHandler; } });
36
36
  var fastify_wrapper_1 = require("./fastify-wrapper");
37
37
  Object.defineProperty(exports, "createFastifyHandler", { enumerable: true, get: function () { return fastify_wrapper_1.createFastifyHandler; } });
38
+ Object.defineProperty(exports, "extractAndStoreRequestBody", { enumerable: true, get: function () { return fastify_wrapper_1.extractAndStoreRequestBody; } });
38
39
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noony-serverless/core",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "description": "A Middy base framework compatible with Firebase and GCP Cloud Functions with TypeScript",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",