serverless-spy 0.0.36 → 0.0.37

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 (105) hide show
  1. package/.jsii +3 -3
  2. package/cli/cli.ts +145 -75
  3. package/cli/icons/Arch_AWS-Lambda_16.svg +18 -0
  4. package/cli/icons/Arch_Amazon-DynamoDB_16.svg +18 -0
  5. package/cli/icons/Arch_Amazon-EventBridge_16.svg +18 -0
  6. package/cli/icons/Arch_Amazon-Simple-Notification-Service_16.svg +18 -0
  7. package/cli/icons/Arch_Amazon-Simple-Queue-Service_16.svg +18 -0
  8. package/cli/icons/Arch_Amazon-Simple-Storage-Service_16.svg +18 -0
  9. package/cli/index.html +84 -25
  10. package/cli/node_modules/commander/LICENSE +22 -0
  11. package/cli/node_modules/commander/Readme.md +1114 -0
  12. package/cli/node_modules/commander/esm.mjs +16 -0
  13. package/cli/node_modules/commander/index.js +27 -0
  14. package/cli/node_modules/commander/lib/argument.js +147 -0
  15. package/cli/node_modules/commander/lib/command.js +2161 -0
  16. package/cli/node_modules/commander/lib/error.js +45 -0
  17. package/cli/node_modules/commander/lib/help.js +406 -0
  18. package/cli/node_modules/commander/lib/option.js +324 -0
  19. package/cli/node_modules/commander/lib/suggestSimilar.js +100 -0
  20. package/cli/node_modules/commander/package-support.json +16 -0
  21. package/cli/node_modules/commander/package.json +80 -0
  22. package/cli/node_modules/commander/typings/index.d.ts +879 -0
  23. package/cli/package.json +23 -0
  24. package/cli/sampleData.ts +518 -0
  25. package/cli/style.css +66 -42
  26. package/cli/webServerlessSpy.ts +461 -0
  27. package/common/SpyEventSender.ts +291 -0
  28. package/common/getWebSocketUrl.ts +21 -4
  29. package/common/spyEvents/EventBridgeBaseSpyEvent.ts +13 -0
  30. package/common/spyEvents/EventBridgeRuleSpyEvent.ts +2 -7
  31. package/common/spyEvents/EventBridgeSpyEvent.ts +2 -7
  32. package/common/spyEvents/FunctionBaseSpyEvent.ts +7 -0
  33. package/common/spyEvents/FunctionConsole.ts +5 -0
  34. package/common/spyEvents/FunctionConsoleSpyEvent.ts +5 -8
  35. package/common/spyEvents/FunctionResponseSpyEvent.ts +2 -5
  36. package/common/spyEvents/SnsSpyEventBase.ts +11 -0
  37. package/common/spyEvents/SnsSubscriptionSpyEvent.ts +3 -9
  38. package/common/spyEvents/SnsTopicSpyEvent.ts +3 -9
  39. package/dist/releasetag.txt +1 -1
  40. package/extension/interceptor.ts +91 -14
  41. package/functions/sendMessage.ts +4 -2
  42. package/lib/cli/cli.js +124 -65
  43. package/lib/cli/cli.mjs +125 -66
  44. package/lib/cli/sampleData.d.ts +892 -0
  45. package/lib/cli/sampleData.js +481 -0
  46. package/lib/cli/sampleData.mjs +478 -0
  47. package/lib/cli/webServerlessSpy.js +5516 -0
  48. package/lib/cli/webServerlessSpy.js.map +7 -0
  49. package/lib/common/SpyEventSender.d.ts +17 -0
  50. package/lib/common/SpyEventSender.js +227 -0
  51. package/lib/common/SpyEventSender.mjs +223 -0
  52. package/lib/common/getWebSocketUrl.d.ts +1 -1
  53. package/lib/common/getWebSocketUrl.js +19 -7
  54. package/lib/common/getWebSocketUrl.mjs +17 -5
  55. package/lib/common/spyEvents/EventBridgeBaseSpyEvent.d.ts +9 -0
  56. package/lib/common/spyEvents/EventBridgeBaseSpyEvent.js +3 -0
  57. package/lib/common/spyEvents/EventBridgeBaseSpyEvent.mjs +2 -0
  58. package/lib/common/spyEvents/EventBridgeRuleSpyEvent.d.ts +2 -7
  59. package/lib/common/spyEvents/EventBridgeRuleSpyEvent.js +1 -1
  60. package/lib/common/spyEvents/EventBridgeRuleSpyEvent.mjs +1 -1
  61. package/lib/common/spyEvents/EventBridgeSpyEvent.d.ts +2 -7
  62. package/lib/common/spyEvents/EventBridgeSpyEvent.js +1 -1
  63. package/lib/common/spyEvents/EventBridgeSpyEvent.mjs +1 -1
  64. package/lib/common/spyEvents/FunctionBaseSpyEvent.d.ts +6 -0
  65. package/lib/common/spyEvents/FunctionBaseSpyEvent.js +3 -0
  66. package/lib/common/spyEvents/FunctionBaseSpyEvent.mjs +2 -0
  67. package/lib/common/spyEvents/FunctionConsole.d.ts +5 -0
  68. package/lib/common/spyEvents/FunctionConsole.js +3 -0
  69. package/lib/common/spyEvents/FunctionConsole.mjs +2 -0
  70. package/lib/common/spyEvents/FunctionConsoleSpyEvent.d.ts +4 -8
  71. package/lib/common/spyEvents/FunctionConsoleSpyEvent.js +1 -1
  72. package/lib/common/spyEvents/FunctionConsoleSpyEvent.mjs +1 -1
  73. package/lib/common/spyEvents/FunctionResponseSpyEvent.d.ts +2 -5
  74. package/lib/common/spyEvents/FunctionResponseSpyEvent.js +1 -1
  75. package/lib/common/spyEvents/FunctionResponseSpyEvent.mjs +1 -1
  76. package/lib/common/spyEvents/SnsSpyEventBase.d.ts +10 -0
  77. package/lib/common/spyEvents/SnsSpyEventBase.js +3 -0
  78. package/lib/common/spyEvents/SnsSpyEventBase.mjs +2 -0
  79. package/lib/common/spyEvents/SnsSubscriptionSpyEvent.d.ts +2 -9
  80. package/lib/common/spyEvents/SnsSubscriptionSpyEvent.js +1 -1
  81. package/lib/common/spyEvents/SnsSubscriptionSpyEvent.mjs +1 -1
  82. package/lib/common/spyEvents/SnsTopicSpyEvent.d.ts +2 -9
  83. package/lib/common/spyEvents/SnsTopicSpyEvent.js +1 -1
  84. package/lib/common/spyEvents/SnsTopicSpyEvent.mjs +1 -1
  85. package/lib/extension/dist/layer/nodejs/node_modules/interceptor.js +251 -181
  86. package/lib/extension/dist/layer/nodejs/node_modules/interceptor.js.map +3 -3
  87. package/lib/listener/SpyHandlers.ts.d.ts +30 -2
  88. package/lib/listener/SpyHandlers.ts.js +1 -1
  89. package/lib/listener/SpyHandlers.ts.mjs +1 -1
  90. package/lib/listener/WsListener.js +11 -11
  91. package/lib/listener/WsListener.mjs +12 -12
  92. package/lib/src/ServerlessSpy.js +2 -3
  93. package/lib/src/ServerlessSpy.mjs +1 -2
  94. package/listener/SpyHandlers.ts.ts +70 -9
  95. package/listener/WsListener.ts +21 -18
  96. package/package.json +5 -3
  97. package/cli/serverlessSpy.js +0 -73
  98. package/cli/ws.ts +0 -79
  99. package/common/publishSpyEvent.ts +0 -269
  100. package/lib/cli/ws.d.ts +0 -1
  101. package/lib/cli/ws.js +0 -68
  102. package/lib/cli/ws.mjs +0 -66
  103. package/lib/common/publishSpyEvent.d.ts +0 -4
  104. package/lib/common/publishSpyEvent.js +0 -211
  105. package/lib/common/publishSpyEvent.mjs +0 -205
@@ -25658,7 +25658,7 @@ __export(interceptor_exports, {
25658
25658
  });
25659
25659
  module.exports = __toCommonJS(interceptor_exports);
25660
25660
 
25661
- // common/publishSpyEvent.ts
25661
+ // common/SpyEventSender.ts
25662
25662
  var import_client_apigatewaymanagementapi = __toESM(require_dist_cjs44());
25663
25663
  var import_client_dynamodb = __toESM(require_dist_cjs48());
25664
25664
  var import_util_dynamodb = __toESM(require_dist_cjs49());
@@ -25674,192 +25674,204 @@ var envVariableNames = /* @__PURE__ */ ((envVariableNames2) => {
25674
25674
  return envVariableNames2;
25675
25675
  })(envVariableNames || {});
25676
25676
 
25677
- // common/publishSpyEvent.ts
25678
- var ddb = new import_client_dynamodb.DynamoDBClient({
25679
- region: process.env.AWS_REGION
25680
- });
25681
- var apigwManagementApi = new import_client_apigatewaymanagementapi.ApiGatewayManagementApi({
25682
- apiVersion: "2018-11-29",
25683
- endpoint: process.env["SSPY_WS_ENDPOINT" /* SSPY_WS_ENDPOINT */]
25684
- });
25685
- var connections;
25686
- async function publishSpyEvent(event) {
25687
- console.log("EVENT", JSON.stringify(event));
25688
- const mapping = JSON.parse(process.env["SSPY_INFRA_MAPPING" /* SSPY_INFRA_MAPPING */]);
25689
- console.log("mapping", JSON.stringify(mapping));
25690
- let connectionData;
25691
- const scanParams = new import_client_dynamodb.ScanCommand({
25692
- TableName: process.env["SSPY_WS_TABLE_NAME" /* SSPY_WS_TABLE_NAME */],
25693
- ProjectionExpression: "connectionId"
25694
- });
25695
- connectionData = await ddb.send(scanParams);
25696
- connections = connectionData.Items;
25697
- console.log(
25698
- `connections at ${new Date().toISOString()}`,
25699
- JSON.stringify(connections)
25700
- );
25701
- const postDataPromises = [];
25702
- if (event?.Records && event.Records[0]?.Sns) {
25703
- console.log("*** SNS ***");
25704
- const eventSns = event;
25705
- for (const record of eventSns.Records) {
25706
- const subscriptionArn = record.EventSubscriptionArn;
25707
- let serviceKey;
25708
- if (mapping[subscriptionArn]) {
25709
- serviceKey = mapping[subscriptionArn];
25710
- } else {
25711
- const topicArn = record.Sns.TopicArn;
25712
- serviceKey = mapping[topicArn];
25713
- }
25714
- let message;
25715
- try {
25716
- message = JSON.parse(record.Sns.Message);
25717
- } catch {
25718
- message = record.Sns.Message;
25677
+ // common/SpyEventSender.ts
25678
+ var SpyEventSender = class {
25679
+ constructor(params) {
25680
+ this.ddb = new import_client_dynamodb.DynamoDBClient({
25681
+ region: process.env.AWS_REGION
25682
+ });
25683
+ this.debugMode = process.env["SSPY_DEBUG" /* SSPY_DEBUG */] === "true";
25684
+ this.apigwManagementApi = new import_client_apigatewaymanagementapi.ApiGatewayManagementApi({
25685
+ apiVersion: "2018-11-29",
25686
+ endpoint: process.env["SSPY_WS_ENDPOINT" /* SSPY_WS_ENDPOINT */]
25687
+ });
25688
+ if (params?.log) {
25689
+ this.log = params.log;
25690
+ }
25691
+ if (params?.logError) {
25692
+ this.logError = params.logError;
25693
+ }
25694
+ }
25695
+ async publishSpyEvent(event) {
25696
+ this.log("Event", JSON.stringify(event));
25697
+ const mapping = JSON.parse(
25698
+ process.env["SSPY_INFRA_MAPPING" /* SSPY_INFRA_MAPPING */]
25699
+ );
25700
+ this.log("ARN to names mapping", JSON.stringify(mapping));
25701
+ let connectionData;
25702
+ const scanParams = new import_client_dynamodb.ScanCommand({
25703
+ TableName: process.env["SSPY_WS_TABLE_NAME" /* SSPY_WS_TABLE_NAME */],
25704
+ ProjectionExpression: "connectionId"
25705
+ });
25706
+ connectionData = await this.ddb.send(scanParams);
25707
+ this.connections = connectionData.Items;
25708
+ const postDataPromises = [];
25709
+ if (event?.Records && event.Records[0]?.Sns) {
25710
+ const eventSns = event;
25711
+ for (const record of eventSns.Records) {
25712
+ const subscriptionArn = record.EventSubscriptionArn;
25713
+ let serviceKey;
25714
+ if (mapping[subscriptionArn]) {
25715
+ serviceKey = mapping[subscriptionArn];
25716
+ } else {
25717
+ const topicArn = record.Sns.TopicArn;
25718
+ serviceKey = mapping[topicArn];
25719
+ }
25720
+ let message;
25721
+ try {
25722
+ message = JSON.parse(record.Sns.Message);
25723
+ } catch {
25724
+ message = record.Sns.Message;
25725
+ }
25726
+ const spyEventType = this.getSpyEventType(serviceKey);
25727
+ const data = {
25728
+ spyEventType,
25729
+ message,
25730
+ subject: record.Sns.Subject,
25731
+ timestamp: record.Sns.Timestamp,
25732
+ topicArn: record.Sns.TopicArn,
25733
+ messageId: record.Sns.MessageId,
25734
+ messageAttributes: record.Sns.MessageAttributes
25735
+ };
25736
+ const fluentEvent = {
25737
+ data,
25738
+ serviceKey
25739
+ };
25740
+ postDataPromises.push(this.postData(fluentEvent));
25741
+ }
25742
+ } else if (event?.Records && event.Records[0]?.eventSource === "aws:sqs") {
25743
+ const eventSqs = event;
25744
+ for (const record of eventSqs.Records) {
25745
+ const subscriptionArn = record.eventSourceARN;
25746
+ const serviceKey = mapping[subscriptionArn];
25747
+ let body;
25748
+ try {
25749
+ body = JSON.parse(record.body);
25750
+ } catch {
25751
+ body = record.body;
25752
+ }
25753
+ const data = {
25754
+ spyEventType: "Sqs",
25755
+ body,
25756
+ messageAttributes: record.messageAttributes
25757
+ };
25758
+ const fluentEvent = {
25759
+ data,
25760
+ serviceKey
25761
+ };
25762
+ postDataPromises.push(this.postData(fluentEvent));
25763
+ }
25764
+ } else if (event?.Records && event.Records[0]?.s3) {
25765
+ const eventS3 = event;
25766
+ for (const record of eventS3.Records) {
25767
+ const bucketArn = record.s3.bucket.arn;
25768
+ const data = {
25769
+ spyEventType: "S3",
25770
+ eventName: record.eventName,
25771
+ eventTime: record.eventTime,
25772
+ bucket: record.s3.bucket.name,
25773
+ key: record.s3.object.key
25774
+ };
25775
+ const fluentEvent = {
25776
+ data,
25777
+ serviceKey: mapping[bucketArn]
25778
+ };
25779
+ postDataPromises.push(this.postData(fluentEvent));
25780
+ }
25781
+ } else if (event.Records && event.Records[0]?.dynamodb) {
25782
+ const eventDynamoDB = event;
25783
+ for (const record of eventDynamoDB.Records) {
25784
+ let arn = record.eventSourceARN;
25785
+ arn = arn.substring(0, arn.indexOf("/stream/"));
25786
+ const data = {
25787
+ spyEventType: "DynamoDB",
25788
+ eventName: record.eventName,
25789
+ newImage: (0, import_util_dynamodb.unmarshall)(record.dynamodb?.NewImage),
25790
+ keys: (0, import_util_dynamodb.unmarshall)(record.dynamodb?.Keys),
25791
+ oldImage: record.dynamodb?.OldImage ? (0, import_util_dynamodb.unmarshall)(record.dynamodb?.OldImage) : void 0
25792
+ };
25793
+ const fluentEvent = {
25794
+ data,
25795
+ serviceKey: mapping[arn]
25796
+ };
25797
+ postDataPromises.push(this.postData(fluentEvent));
25719
25798
  }
25720
- const spyEventType = getSpyEventType(serviceKey);
25799
+ } else if (event.detail && event["detail-type"] && event.version && event.source) {
25800
+ const eventEb = event;
25801
+ const serviceKey = mapping.eventBridge;
25802
+ const spyEventType = this.getSpyEventType(serviceKey);
25803
+ const message = eventEb.detail;
25721
25804
  const data = {
25722
25805
  spyEventType,
25723
- message,
25724
- subject: record.Sns.Subject,
25725
- timestamp: record.Sns.Timestamp,
25726
- topicArn: record.Sns.TopicArn,
25727
- messageId: record.Sns.MessageId,
25728
- messageAttributes: record.Sns.MessageAttributes
25806
+ detail: message,
25807
+ detailType: eventEb["detail-type"],
25808
+ eventBridgeId: eventEb["id"],
25809
+ source: eventEb.source,
25810
+ time: eventEb.time,
25811
+ account: eventEb.account
25729
25812
  };
25730
25813
  const fluentEvent = {
25731
25814
  data,
25732
25815
  serviceKey
25733
25816
  };
25734
- postDataPromises.push(postData(fluentEvent));
25817
+ postDataPromises.push(this.postData(fluentEvent));
25818
+ } else {
25819
+ const fluentEvent = event;
25820
+ postDataPromises.push(this.postData(fluentEvent));
25735
25821
  }
25736
- } else if (event?.Records && event.Records[0]?.eventSource === "aws:sqs") {
25737
- console.log("*** SQS ***");
25738
- const eventSqs = event;
25739
- for (const record of eventSqs.Records) {
25740
- const subscriptionArn = record.eventSourceARN;
25741
- const serviceKey = mapping[subscriptionArn];
25742
- let body;
25822
+ await Promise.all(postDataPromises);
25823
+ }
25824
+ async postData(spyMessage) {
25825
+ this.log("Post spy message", JSON.stringify(spyMessage));
25826
+ if (!this.connections) {
25827
+ return;
25828
+ }
25829
+ const postCalls = this.connections.map(async ({ connectionId }) => {
25830
+ this.log(`Sending message to client: ${connectionId.S}`);
25743
25831
  try {
25744
- body = JSON.parse(record.body);
25745
- } catch {
25746
- body = record.body;
25832
+ const postToConnectionCommand = new import_client_apigatewaymanagementapi.PostToConnectionCommand({
25833
+ ConnectionId: connectionId.S,
25834
+ Data: JSON.stringify({
25835
+ timestamp: new Date().toISOString(),
25836
+ serviceKey: spyMessage.serviceKey,
25837
+ data: spyMessage.data
25838
+ })
25839
+ });
25840
+ await this.apigwManagementApi.send(postToConnectionCommand);
25841
+ } catch (e) {
25842
+ this.logError(`Faild sending spy message to: ${connectionId.S}`, e);
25843
+ if (e.$metadata.httpStatusCode === 410) {
25844
+ this.log(`Found stale connection, deleting ${connectionId}`);
25845
+ const deleteParams = new import_client_dynamodb.DeleteItemCommand({
25846
+ TableName: process.env["SSPY_WS_TABLE_NAME" /* SSPY_WS_TABLE_NAME */],
25847
+ Key: { connectionId }
25848
+ });
25849
+ await this.ddb.send(deleteParams);
25850
+ } else {
25851
+ throw e;
25852
+ }
25747
25853
  }
25748
- const data = {
25749
- spyEventType: "Sqs",
25750
- body,
25751
- messageAttributes: record.messageAttributes
25752
- };
25753
- const fluentEvent = {
25754
- data,
25755
- serviceKey
25756
- };
25757
- postDataPromises.push(postData(fluentEvent));
25758
- }
25759
- } else if (event?.Records && event.Records[0]?.s3) {
25760
- console.log("*** S3 ***");
25761
- const eventS3 = event;
25762
- for (const record of eventS3.Records) {
25763
- const bucketArn = record.s3.bucket.arn;
25764
- const data = {
25765
- spyEventType: "S3",
25766
- eventName: record.eventName,
25767
- eventTime: record.eventTime,
25768
- bucket: record.s3.bucket.name,
25769
- key: record.s3.object.key
25770
- };
25771
- const fluentEvent = {
25772
- data,
25773
- serviceKey: mapping[bucketArn]
25774
- };
25775
- postDataPromises.push(postData(fluentEvent));
25854
+ });
25855
+ await Promise.all(postCalls);
25856
+ this.log("Send spy message finish");
25857
+ }
25858
+ getSpyEventType(serviceKey) {
25859
+ if (!serviceKey) {
25860
+ throw new Error("Missing serviceKey");
25776
25861
  }
25777
- } else if (event.Records && event.Records[0]?.dynamodb) {
25778
- console.log("*** DYNAMODB ***");
25779
- const eventDynamoDB = event;
25780
- for (const record of eventDynamoDB.Records) {
25781
- let arn = record.eventSourceARN;
25782
- arn = arn.substring(0, arn.indexOf("/stream/"));
25783
- const data = {
25784
- spyEventType: "DynamoDB",
25785
- eventName: record.eventName,
25786
- newImage: (0, import_util_dynamodb.unmarshall)(record.dynamodb?.NewImage),
25787
- keys: (0, import_util_dynamodb.unmarshall)(record.dynamodb?.Keys),
25788
- oldImage: record.dynamodb?.OldImage ? (0, import_util_dynamodb.unmarshall)(record.dynamodb?.OldImage) : void 0
25789
- };
25790
- const fluentEvent = {
25791
- data,
25792
- serviceKey: mapping[arn]
25793
- };
25794
- postDataPromises.push(postData(fluentEvent));
25862
+ return serviceKey.substring(0, serviceKey.indexOf("#"));
25863
+ }
25864
+ log(message, ...optionalParams) {
25865
+ if (this.debugMode) {
25866
+ console.debug("SSPY EXTENSION", message, ...optionalParams);
25795
25867
  }
25796
- } else if (event.detail && event["detail-type"] && event.version && event.source) {
25797
- console.log("*** EventBridge ***");
25798
- const eventEb = event;
25799
- const serviceKey = mapping.eventBridge;
25800
- const spyEventType = getSpyEventType(serviceKey);
25801
- const message = eventEb.detail;
25802
- const data = {
25803
- spyEventType,
25804
- detail: message,
25805
- detailType: eventEb["detail-type"],
25806
- source: eventEb.source,
25807
- time: eventEb.time,
25808
- account: eventEb.account
25809
- };
25810
- const fluentEvent = {
25811
- data,
25812
- serviceKey
25813
- };
25814
- postDataPromises.push(postData(fluentEvent));
25815
- } else {
25816
- console.log("*** OTHER ***");
25817
- const fluentEvent = event;
25818
- postDataPromises.push(postData(fluentEvent));
25819
25868
  }
25820
- await Promise.all(postDataPromises);
25821
- }
25822
- async function postData(spyMessage) {
25823
- console.log("postData", JSON.stringify(spyMessage));
25824
- if (!connections) {
25825
- return;
25826
- }
25827
- const postCalls = connections.map(async ({ connectionId }) => {
25828
- console.log(`Sending message to: ${connectionId.S}`);
25829
- try {
25830
- const postToConnectionCommand = new import_client_apigatewaymanagementapi.PostToConnectionCommand({
25831
- ConnectionId: connectionId.S,
25832
- Data: JSON.stringify({
25833
- timestamp: new Date().toISOString(),
25834
- serviceKey: spyMessage.serviceKey,
25835
- data: spyMessage.data
25836
- })
25837
- });
25838
- await apigwManagementApi.send(postToConnectionCommand);
25839
- } catch (e) {
25840
- console.error(`Fails sending message to: ${connectionId.S}`, e);
25841
- console.error(`Status code: ${e.statusCode}`);
25842
- if (e.$metadata.httpStatusCode === 410) {
25843
- console.log(`Found stale connection, deleting ${connectionId}`);
25844
- const deleteParams = new import_client_dynamodb.DeleteItemCommand({
25845
- TableName: process.env["SSPY_WS_TABLE_NAME" /* SSPY_WS_TABLE_NAME */],
25846
- Key: { connectionId }
25847
- });
25848
- await ddb.send(deleteParams);
25849
- } else {
25850
- throw e;
25851
- }
25869
+ logError(message, ...optionalParams) {
25870
+ if (this.debugMode) {
25871
+ console.error("SSPY EXTENSION", message, ...optionalParams);
25852
25872
  }
25853
- });
25854
- await Promise.all(postCalls);
25855
- console.log("Send message finish");
25856
- }
25857
- function getSpyEventType(serviceKey) {
25858
- if (!serviceKey) {
25859
- throw new Error("Missing serviceKey");
25860
25873
  }
25861
- return serviceKey.substring(0, serviceKey.indexOf("#"));
25862
- }
25874
+ };
25863
25875
 
25864
25876
  // extension/aws/UserFunction.ts
25865
25877
  var import_fs = __toESM(require("fs"));
@@ -25976,20 +25988,35 @@ var load = function(appRoot, fullHandlerString) {
25976
25988
  var ORIGINAL_HANDLER_KEY = "ORIGINAL_HANDLER";
25977
25989
  var subscribedToSQS = process.env["SSPY_SUBSCRIBED_TO_SQS" /* SSPY_SUBSCRIBED_TO_SQS */] === "true";
25978
25990
  var debugMode = process.env["SSPY_DEBUG" /* SSPY_DEBUG */] === "true";
25991
+ var oldConsoleLog = console.log;
25992
+ var oldConsoleWarn = console.warn;
25993
+ var oldConsoleDebug = console.debug;
25994
+ var oldConsoleInfo = console.info;
25995
+ var oldConsoleError = console.error;
25996
+ var currentEvent;
25997
+ var currentContext;
25998
+ var promises = [];
25999
+ interceptConsole();
26000
+ var spyEventSender = new SpyEventSender({
26001
+ log,
26002
+ logError
26003
+ });
25979
26004
  var handler = (event, context, callback) => {
25980
- log("Request", JSON.stringify(event));
25981
- const promises = [];
25982
- if (subscribedToSQS) {
25983
- log("Send raw message for SQS");
25984
- const p2 = sendRawSpyEvent(event);
25985
- promises.push(p2);
25986
- }
25987
26005
  const contextSpy = {
25988
26006
  functionName: context.functionName,
25989
26007
  awsRequestId: context.awsRequestId,
25990
26008
  identity: context.identity,
25991
26009
  clientContext: context.clientContext
25992
26010
  };
26011
+ currentEvent = event;
26012
+ currentContext = contextSpy;
26013
+ promises = [];
26014
+ log("Request", JSON.stringify(event));
26015
+ if (subscribedToSQS) {
26016
+ log("Send raw message for SQS");
26017
+ const p2 = sendRawSpyEvent(event);
26018
+ promises.push(p2);
26019
+ }
25993
26020
  const key = `Function#${process.env["SSPY_FUNCTION_NAME" /* SSPY_FUNCTION_NAME */]}#Request`;
25994
26021
  const p = sendLambdaSpyEvent(key, {
25995
26022
  request: event,
@@ -26006,6 +26033,8 @@ var handler = (event, context, callback) => {
26006
26033
  context: contextSpy
26007
26034
  });
26008
26035
  promises.push(p2);
26036
+ currentEvent = void 0;
26037
+ currentContext = void 0;
26009
26038
  return Promise.all(promises);
26010
26039
  };
26011
26040
  const succeed = (response) => {
@@ -26017,6 +26046,8 @@ var handler = (event, context, callback) => {
26017
26046
  context: contextSpy
26018
26047
  });
26019
26048
  promises.push(p2);
26049
+ currentEvent = void 0;
26050
+ currentContext = void 0;
26020
26051
  return Promise.all(promises);
26021
26052
  };
26022
26053
  const newCallback = (err, data) => {
@@ -26047,6 +26078,45 @@ var handler = (event, context, callback) => {
26047
26078
  );
26048
26079
  }
26049
26080
  };
26081
+ function interceptConsole() {
26082
+ const sendLogs = (type, args) => {
26083
+ if (!currentContext)
26084
+ return;
26085
+ log(`Console ${type}`, JSON.stringify(args));
26086
+ const message = args == null ? void 0 : args.shift();
26087
+ const key = `Function#${process.env["SSPY_FUNCTION_NAME" /* SSPY_FUNCTION_NAME */]}#Console`;
26088
+ const p = sendLambdaSpyEvent(key, {
26089
+ request: currentEvent,
26090
+ context: currentContext,
26091
+ console: {
26092
+ type,
26093
+ message,
26094
+ optionalParams: args
26095
+ }
26096
+ });
26097
+ promises.push(p);
26098
+ };
26099
+ console.log = function(...args) {
26100
+ sendLogs("log", args);
26101
+ oldConsoleLog.apply(console, args);
26102
+ };
26103
+ console.warn = function(...args) {
26104
+ sendLogs("warn", args);
26105
+ oldConsoleWarn.apply(console, args);
26106
+ };
26107
+ console.debug = function(...args) {
26108
+ sendLogs("debug", args);
26109
+ oldConsoleDebug.apply(console, args);
26110
+ };
26111
+ console.info = function(...args) {
26112
+ sendLogs("info", args);
26113
+ oldConsoleInfo.apply(console, args);
26114
+ };
26115
+ console.error = function(...args) {
26116
+ sendLogs("error", args);
26117
+ oldConsoleError.apply(console, args);
26118
+ };
26119
+ }
26050
26120
  function isPromise(obj) {
26051
26121
  return typeof (obj == null ? void 0 : obj.then) === "function";
26052
26122
  }
@@ -26057,7 +26127,7 @@ async function sendLambdaSpyEvent(serviceKey, data) {
26057
26127
  });
26058
26128
  }
26059
26129
  async function sendRawSpyEvent(data) {
26060
- await publishSpyEvent(data);
26130
+ await spyEventSender.publishSpyEvent(data);
26061
26131
  }
26062
26132
  function getOriginalHandler() {
26063
26133
  log("Original handler", process.env[ORIGINAL_HANDLER_KEY]);
@@ -26070,12 +26140,12 @@ function getOriginalHandler() {
26070
26140
  }
26071
26141
  function log(message, ...optionalParams) {
26072
26142
  if (debugMode) {
26073
- console.debug("SSPY EXTENSION", message, ...optionalParams);
26143
+ oldConsoleDebug("SSPY EXTENSION", message, ...optionalParams);
26074
26144
  }
26075
26145
  }
26076
26146
  function logError(message, ...optionalParams) {
26077
26147
  if (debugMode) {
26078
- console.error("SSPY EXTENSION", message, ...optionalParams);
26148
+ oldConsoleError("SSPY EXTENSION", message, ...optionalParams);
26079
26149
  }
26080
26150
  }
26081
26151
  // Annotate the CommonJS export names for ESM import in node: