@geekmidas/constructs 10.0.0-alpha.52 → 10.0.0-alpha.53
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/dist/adaptors/aws.cjs +12 -195
- package/dist/adaptors/aws.cjs.map +1 -1
- package/dist/adaptors/aws.d.cts +0 -55
- package/dist/adaptors/aws.d.cts.map +1 -1
- package/dist/adaptors/aws.d.mts +0 -55
- package/dist/adaptors/aws.d.mts.map +1 -1
- package/dist/adaptors/aws.mjs +13 -195
- package/dist/adaptors/aws.mjs.map +1 -1
- package/dist/runSubscriber-BRp9a_qi.cjs +126 -0
- package/dist/runSubscriber-BRp9a_qi.cjs.map +1 -0
- package/dist/runSubscriber-BYEY_bfA.mjs +109 -0
- package/dist/runSubscriber-BYEY_bfA.mjs.map +1 -0
- package/dist/subscribers/index.cjs +107 -0
- package/dist/subscribers/index.cjs.map +1 -0
- package/dist/subscribers/index.d.cts +48 -1
- package/dist/subscribers/index.d.cts.map +1 -0
- package/dist/subscribers/index.d.mts +48 -1
- package/dist/subscribers/index.d.mts.map +1 -0
- package/dist/subscribers/index.mjs +107 -1
- package/dist/subscribers/index.mjs.map +1 -0
- package/package.json +31 -31
package/dist/adaptors/aws.cjs
CHANGED
|
@@ -4,6 +4,7 @@ const require_functions = require("../functions-CbP5ZKma.cjs");
|
|
|
4
4
|
const require_Endpoint = require("../Endpoint-DOi8c_Gy.cjs");
|
|
5
5
|
const require_surfaceEnv = require("../surfaceEnv-CNimK76p.cjs");
|
|
6
6
|
const require_lazyAccessors = require("../lazyAccessors-hJKGv8Ay.cjs");
|
|
7
|
+
const require_runSubscriber = require("../runSubscriber-BRp9a_qi.cjs");
|
|
7
8
|
const require_processAudits = require("../processAudits-D_2W5x8k.cjs");
|
|
8
9
|
let _geekmidas_errors = require("@geekmidas/errors");
|
|
9
10
|
let _geekmidas_services = require("@geekmidas/services");
|
|
@@ -14,7 +15,6 @@ let _geekmidas_audit = require("@geekmidas/audit");
|
|
|
14
15
|
let _middy_core = require("@middy/core");
|
|
15
16
|
_middy_core = require_rolldown_runtime.__toESM(_middy_core, 1);
|
|
16
17
|
let _geekmidas_audit_kysely = require("@geekmidas/audit/kysely");
|
|
17
|
-
let _geekmidas_events_sns = require("@geekmidas/events/sns");
|
|
18
18
|
//#region src/functions/FunctionExecutionWrapper.ts
|
|
19
19
|
var FunctionExecutionWrapper = class {
|
|
20
20
|
envParser;
|
|
@@ -713,7 +713,6 @@ var AWSLambdaSubscriber = class {
|
|
|
713
713
|
envParser;
|
|
714
714
|
subscriber;
|
|
715
715
|
_logger;
|
|
716
|
-
_services;
|
|
717
716
|
constructor(envParser, subscriber) {
|
|
718
717
|
this.envParser = envParser;
|
|
719
718
|
this.subscriber = subscriber;
|
|
@@ -722,21 +721,6 @@ var AWSLambdaSubscriber = class {
|
|
|
722
721
|
get logger() {
|
|
723
722
|
return this._logger;
|
|
724
723
|
}
|
|
725
|
-
async getServices() {
|
|
726
|
-
if (this._services) return this._services;
|
|
727
|
-
const serviceDiscovery = _geekmidas_services.ServiceDiscovery.getInstance(this.envParser);
|
|
728
|
-
if (this.subscriber.services.length > 0) {
|
|
729
|
-
const registered = await serviceDiscovery.register(this.subscriber.services);
|
|
730
|
-
this._services = registered;
|
|
731
|
-
} else this._services = {};
|
|
732
|
-
return this._services;
|
|
733
|
-
}
|
|
734
|
-
/** The subscriber's database, registered as functions register theirs. */
|
|
735
|
-
async getDatabase() {
|
|
736
|
-
const service = this.subscriber.databaseService;
|
|
737
|
-
if (!service) return void 0;
|
|
738
|
-
return (await _geekmidas_services.ServiceDiscovery.getInstance(this.envParser).register([service]))[service.serviceName];
|
|
739
|
-
}
|
|
740
724
|
error() {
|
|
741
725
|
return { onError: (req) => {
|
|
742
726
|
(req.event?.logger || this.subscriber.logger).error(req.error || {}, "Error processing subscriber");
|
|
@@ -758,112 +742,26 @@ var AWSLambdaSubscriber = class {
|
|
|
758
742
|
}
|
|
759
743
|
services() {
|
|
760
744
|
return { before: async (req) => {
|
|
761
|
-
|
|
762
|
-
req.event.
|
|
745
|
+
const { services, db } = await require_runSubscriber.subscriberContext(this.subscriber, this.envParser);
|
|
746
|
+
req.event.services = services;
|
|
747
|
+
req.event.db = db;
|
|
763
748
|
} };
|
|
764
749
|
}
|
|
765
750
|
parseEvents() {
|
|
766
751
|
return { before: async (req) => {
|
|
767
|
-
const { logger, ...
|
|
768
|
-
const rawEvent = e;
|
|
752
|
+
const { logger, ...rawEvent } = req.event;
|
|
769
753
|
logger.info({ rawEvent });
|
|
770
|
-
|
|
771
|
-
if ("Records" in rawEvent) {
|
|
772
|
-
if (this.isSQSEvent(rawEvent)) for (const record of rawEvent.Records) try {
|
|
773
|
-
const event = this.parseSQSRecord(record);
|
|
774
|
-
if (this.shouldIncludeEvent(event)) events.push(event);
|
|
775
|
-
} catch (error) {
|
|
776
|
-
this.logger.error({
|
|
777
|
-
error,
|
|
778
|
-
record
|
|
779
|
-
}, "Failed to parse SQS record");
|
|
780
|
-
}
|
|
781
|
-
else if (this.isSNSEvent(rawEvent)) for (const record of rawEvent.Records) try {
|
|
782
|
-
const event = this.parseSNSRecord(record);
|
|
783
|
-
if (this.shouldIncludeEvent(event)) events.push(event);
|
|
784
|
-
} catch (error) {
|
|
785
|
-
this.logger.error({
|
|
786
|
-
error,
|
|
787
|
-
record
|
|
788
|
-
}, "Failed to parse SNS record");
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
req.event.events = events;
|
|
754
|
+
req.event.events = require_runSubscriber.subscribedEvents(this.subscriber, rawEvent, this.logger);
|
|
792
755
|
} };
|
|
793
756
|
}
|
|
794
|
-
isSQSEvent(event) {
|
|
795
|
-
const firstRecord = event.Records[0];
|
|
796
|
-
return "Records" in event && event.Records.length > 0 && firstRecord !== void 0 && "eventSource" in firstRecord && firstRecord.eventSource === "aws:sqs";
|
|
797
|
-
}
|
|
798
|
-
isSNSEvent(event) {
|
|
799
|
-
const firstRecord = event.Records[0];
|
|
800
|
-
return "Records" in event && event.Records.length > 0 && firstRecord !== void 0 && "EventSource" in firstRecord && firstRecord.EventSource === "aws:sns";
|
|
801
|
-
}
|
|
802
|
-
parseSNSRecord(record) {
|
|
803
|
-
const message = this.safeJsonParse(record.Sns.Message);
|
|
804
|
-
const messageType = record.Sns.MessageAttributes?.type?.Value;
|
|
805
|
-
if (message === null) return messageType ? {
|
|
806
|
-
type: messageType,
|
|
807
|
-
payload: record.Sns.Message
|
|
808
|
-
} : record.Sns.Message;
|
|
809
|
-
const resolvedType = messageType ?? message.type;
|
|
810
|
-
if (message.type) return message;
|
|
811
|
-
return resolvedType ? {
|
|
812
|
-
type: resolvedType,
|
|
813
|
-
payload: message
|
|
814
|
-
} : message;
|
|
815
|
-
}
|
|
816
|
-
parseSQSRecord(record) {
|
|
817
|
-
const body = this.safeJsonParse(record.body);
|
|
818
|
-
if (body === null) return record.body;
|
|
819
|
-
if (body.Type === "Notification" && body.Message) {
|
|
820
|
-
const snsMessage = this.safeJsonParse(body.Message);
|
|
821
|
-
const messageType = body.MessageAttributes?.type?.Value;
|
|
822
|
-
if (snsMessage === null) return messageType ? {
|
|
823
|
-
type: messageType,
|
|
824
|
-
payload: body.Message
|
|
825
|
-
} : body.Message;
|
|
826
|
-
if (snsMessage.type) return snsMessage;
|
|
827
|
-
const resolvedType = messageType ?? snsMessage.type;
|
|
828
|
-
return resolvedType ? {
|
|
829
|
-
type: resolvedType,
|
|
830
|
-
payload: snsMessage
|
|
831
|
-
} : snsMessage;
|
|
832
|
-
}
|
|
833
|
-
return body;
|
|
834
|
-
}
|
|
835
|
-
safeJsonParse(value) {
|
|
836
|
-
try {
|
|
837
|
-
return JSON.parse(value);
|
|
838
|
-
} catch {
|
|
839
|
-
return null;
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
shouldIncludeEvent(event) {
|
|
843
|
-
if (typeof event !== "object" || !event?.type) return true;
|
|
844
|
-
if (!this.subscriber.subscribedEvents) return true;
|
|
845
|
-
return this.subscriber.subscribedEvents.includes(event.type);
|
|
846
|
-
}
|
|
847
757
|
async _handler(event) {
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
services: event.services,
|
|
855
|
-
logger: event.logger,
|
|
856
|
-
db: event.db
|
|
758
|
+
const { events, services, logger, db } = event;
|
|
759
|
+
return require_runSubscriber.runSubscriber(this.subscriber, {
|
|
760
|
+
events,
|
|
761
|
+
services,
|
|
762
|
+
logger,
|
|
763
|
+
db
|
|
857
764
|
});
|
|
858
|
-
if (this.subscriber.outputSchema && result) {
|
|
859
|
-
const validationResult = await this.subscriber.outputSchema["~standard"].validate(result);
|
|
860
|
-
if (validationResult.issues) {
|
|
861
|
-
this.logger.error({ issues: validationResult.issues }, "Subscriber output validation failed");
|
|
862
|
-
throw new Error("Subscriber output validation failed");
|
|
863
|
-
}
|
|
864
|
-
return validationResult.value;
|
|
865
|
-
}
|
|
866
|
-
return result;
|
|
867
765
|
}
|
|
868
766
|
get handler() {
|
|
869
767
|
const handler = this._handler.bind(this);
|
|
@@ -882,92 +780,11 @@ var AWSLambdaSubscriber = class {
|
|
|
882
780
|
}
|
|
883
781
|
};
|
|
884
782
|
//#endregion
|
|
885
|
-
//#region src/subscribers/SnsPushSubscriberAdaptor.ts
|
|
886
|
-
/**
|
|
887
|
-
* A topic subscriber run by SNS pushing to an HTTP endpoint, rather than by a
|
|
888
|
-
* poller.
|
|
889
|
-
*
|
|
890
|
-
* The notification is handed to {@link AWSLambdaSubscriber} as the Lambda
|
|
891
|
-
* event SNS would have invoked a function with, so the parsing, the services
|
|
892
|
-
* and the error handling are the ones that run deployed on Lambda — the
|
|
893
|
-
* endpoint is only a different way for the event to arrive.
|
|
894
|
-
*/
|
|
895
|
-
var SnsPushSubscriberAdaptor = class {
|
|
896
|
-
subscriber;
|
|
897
|
-
options;
|
|
898
|
-
lambda;
|
|
899
|
-
constructor(envParser, subscriber, options) {
|
|
900
|
-
this.subscriber = subscriber;
|
|
901
|
-
this.options = options;
|
|
902
|
-
this.lambda = new AWSLambdaSubscriber(envParser, subscriber);
|
|
903
|
-
}
|
|
904
|
-
async handle(body) {
|
|
905
|
-
const message = body;
|
|
906
|
-
if (typeof message?.Type !== "string" || !message.TopicArn) return refuse(400, "not an SNS message");
|
|
907
|
-
if (message.TopicArn !== this.options.topicArn) return refuse(403, `not subscribed to ${message.TopicArn}`);
|
|
908
|
-
if (this.options.verify) try {
|
|
909
|
-
await (0, _geekmidas_events_sns.verifySnsMessage)(message);
|
|
910
|
-
} catch (error) {
|
|
911
|
-
this.subscriber.logger.warn({
|
|
912
|
-
error,
|
|
913
|
-
topicArn: message.TopicArn
|
|
914
|
-
}, "Refused an SNS message that did not verify");
|
|
915
|
-
return refuse(403, error.name);
|
|
916
|
-
}
|
|
917
|
-
switch (message.Type) {
|
|
918
|
-
case "SubscriptionConfirmation":
|
|
919
|
-
await (0, _geekmidas_events_sns.confirmSnsSubscription)(this.options.emulatorEndpoint && message.SubscribeURL ? {
|
|
920
|
-
...message,
|
|
921
|
-
SubscribeURL: throughEndpoint(message.SubscribeURL, this.options.emulatorEndpoint)
|
|
922
|
-
} : message);
|
|
923
|
-
this.subscriber.logger.info({ topicArn: message.TopicArn }, "Confirmed SNS subscription");
|
|
924
|
-
return {
|
|
925
|
-
status: 200,
|
|
926
|
-
body: { status: "confirmed" }
|
|
927
|
-
};
|
|
928
|
-
case "Notification": try {
|
|
929
|
-
await this.lambda.handler((0, _geekmidas_events_sns.toSnsEvent)(message), {
|
|
930
|
-
awsRequestId: message.MessageId,
|
|
931
|
-
functionName: this.subscriber.topicName ?? "subscriber"
|
|
932
|
-
}, () => {});
|
|
933
|
-
return {
|
|
934
|
-
status: 200,
|
|
935
|
-
body: { status: "ok" }
|
|
936
|
-
};
|
|
937
|
-
} catch (error) {
|
|
938
|
-
return refuse(500, error.name);
|
|
939
|
-
}
|
|
940
|
-
default: return {
|
|
941
|
-
status: 200,
|
|
942
|
-
body: { status: "ignored" }
|
|
943
|
-
};
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
};
|
|
947
|
-
/** `url`, sent to `endpoint`'s origin instead of its own. */
|
|
948
|
-
function throughEndpoint(url, endpoint) {
|
|
949
|
-
const target = new URL(url);
|
|
950
|
-
const origin = new URL(endpoint);
|
|
951
|
-
target.protocol = origin.protocol;
|
|
952
|
-
target.host = origin.host;
|
|
953
|
-
return target.toString();
|
|
954
|
-
}
|
|
955
|
-
function refuse(status, error) {
|
|
956
|
-
return {
|
|
957
|
-
status,
|
|
958
|
-
body: {
|
|
959
|
-
status: "refused",
|
|
960
|
-
error
|
|
961
|
-
}
|
|
962
|
-
};
|
|
963
|
-
}
|
|
964
|
-
//#endregion
|
|
965
783
|
exports.AWSLambdaFunction = AWSLambdaFunction;
|
|
966
784
|
exports.AWSLambdaQueue = AWSLambdaQueue;
|
|
967
785
|
exports.AWSLambdaSubscriber = AWSLambdaSubscriber;
|
|
968
786
|
exports.AWSScheduledFunction = AWSScheduledFunction;
|
|
969
787
|
exports.AmazonApiGatewayV1Endpoint = AmazonApiGatewayV1Endpoint;
|
|
970
788
|
exports.AmazonApiGatewayV2Endpoint = AmazonApiGatewayV2Endpoint;
|
|
971
|
-
exports.SnsPushSubscriberAdaptor = SnsPushSubscriberAdaptor;
|
|
972
789
|
|
|
973
790
|
//# sourceMappingURL=aws.cjs.map
|