@replayio-app-building/netlify-recorder 0.22.0 → 0.24.0

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.
Files changed (2) hide show
  1. package/dist/index.js +74 -39
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -572,10 +572,37 @@ function createRecordingRequestHandler(handler, options) {
572
572
  let response;
573
573
  try {
574
574
  response = await handler(event, context);
575
- } catch (err) {
575
+ } catch (handlerErr) {
576
+ const errorMessage = handlerErr instanceof Error ? handlerErr.message : String(handlerErr);
577
+ const errorResponse = {
578
+ statusCode: 500,
579
+ body: JSON.stringify({ error: errorMessage })
580
+ };
581
+ const finishOpts2 = { ...options, requestId };
582
+ const ctx2 = context;
583
+ if (ctx2 && typeof ctx2.waitUntil === "function") {
584
+ ctx2.waitUntil(
585
+ finishRequest(reqContext, options.callbacks, errorResponse, finishOpts2).catch(
586
+ (finishErr) => {
587
+ console.error(
588
+ `netlify-recorder: background finishRequest failed after handler error (handler: ${options.handlerPath ?? "unknown"})`,
589
+ finishErr
590
+ );
591
+ }
592
+ )
593
+ );
594
+ } else {
595
+ await finishRequest(reqContext, options.callbacks, errorResponse, finishOpts2).catch(
596
+ (finishErr) => {
597
+ console.error(
598
+ `netlify-recorder: finishRequest failed after handler error (handler: ${options.handlerPath ?? "unknown"})`,
599
+ finishErr
600
+ );
601
+ }
602
+ );
603
+ }
576
604
  setCurrentRequestId(null);
577
- reqContext.cleanup();
578
- throw err;
605
+ throw handlerErr;
579
606
  }
580
607
  reqContext.cleanup();
581
608
  setCurrentRequestId(null);
@@ -800,45 +827,53 @@ async function createRequestRecording(blobUrlOrData, handlerPath, requestInfo) {
800
827
  );
801
828
  }
802
829
  let rawResult;
803
- if (isV2) {
804
- const url = requestInfo.rawUrl ?? `https://localhost${requestInfo.url}`;
805
- const reqInit = {
806
- method: requestInfo.method,
807
- headers: requestInfo.headers
808
- };
809
- if (requestInfo.body && requestInfo.method !== "GET" && requestInfo.method !== "HEAD") {
810
- reqInit.body = requestInfo.body;
811
- }
812
- const RequestCtor = globalThis.Request;
813
- if (!RequestCtor) {
814
- throw new Error(
815
- "Handler uses Netlify Functions v2 signature but Request is not available in this environment. Ensure undici or a Request polyfill is installed."
816
- );
817
- }
818
- const req = new RequestCtor(url, reqInit);
819
- const context = { geo: {}, ip: "127.0.0.1", requestId: "replay", server: { region: "local" } };
820
- const response = await handler(req, context);
821
- if (response && typeof response === "object" && typeof response.status === "number" && typeof response.text === "function") {
822
- const res = response;
823
- rawResult = {
824
- statusCode: res.status,
825
- body: await res.text()
830
+ try {
831
+ if (isV2) {
832
+ const url = requestInfo.rawUrl ?? `https://localhost${requestInfo.url}`;
833
+ const reqInit = {
834
+ method: requestInfo.method,
835
+ headers: requestInfo.headers
826
836
  };
837
+ if (requestInfo.body && requestInfo.method !== "GET" && requestInfo.method !== "HEAD") {
838
+ reqInit.body = requestInfo.body;
839
+ }
840
+ const RequestCtor = globalThis.Request;
841
+ if (!RequestCtor) {
842
+ throw new Error(
843
+ "Handler uses Netlify Functions v2 signature but Request is not available in this environment. Ensure undici or a Request polyfill is installed."
844
+ );
845
+ }
846
+ const req = new RequestCtor(url, reqInit);
847
+ const context = { geo: {}, ip: "127.0.0.1", requestId: "replay", server: { region: "local" } };
848
+ const response = await handler(req, context);
849
+ if (response && typeof response === "object" && typeof response.status === "number" && typeof response.text === "function") {
850
+ const res = response;
851
+ rawResult = {
852
+ statusCode: res.status,
853
+ body: await res.text()
854
+ };
855
+ } else {
856
+ rawResult = response;
857
+ }
827
858
  } else {
828
- rawResult = response;
859
+ rawResult = await handler({
860
+ httpMethod: requestInfo.method,
861
+ path: requestInfo.url,
862
+ headers: requestInfo.headers,
863
+ body: requestInfo.body ?? null,
864
+ queryStringParameters: requestInfo.queryStringParameters ?? null,
865
+ multiValueQueryStringParameters: requestInfo.multiValueQueryStringParameters ?? null,
866
+ rawUrl: requestInfo.rawUrl ?? requestInfo.url,
867
+ rawQuery: requestInfo.rawQuery ?? "",
868
+ isBase64Encoded: requestInfo.isBase64Encoded ?? false
869
+ });
829
870
  }
830
- } else {
831
- rawResult = await handler({
832
- httpMethod: requestInfo.method,
833
- path: requestInfo.url,
834
- headers: requestInfo.headers,
835
- body: requestInfo.body ?? null,
836
- queryStringParameters: requestInfo.queryStringParameters ?? null,
837
- multiValueQueryStringParameters: requestInfo.multiValueQueryStringParameters ?? null,
838
- rawUrl: requestInfo.rawUrl ?? requestInfo.url,
839
- rawQuery: requestInfo.rawQuery ?? "",
840
- isBase64Encoded: requestInfo.isBase64Encoded ?? false
841
- });
871
+ } catch (handlerErr) {
872
+ const errorMessage = handlerErr instanceof Error ? handlerErr.message : String(handlerErr);
873
+ rawResult = {
874
+ statusCode: 500,
875
+ body: JSON.stringify({ error: errorMessage })
876
+ };
842
877
  }
843
878
  if (blobData.handlerResponse) {
844
879
  const replayRes = rawResult;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@replayio-app-building/netlify-recorder",
3
- "version": "0.22.0",
3
+ "version": "0.24.0",
4
4
  "description": "Capture and replay Netlify function executions as Replay recordings",
5
5
  "type": "module",
6
6
  "exports": {