Skip to content

Commit 2354916

Browse files
authored
Prevent double trigger (#237)
1 parent bd6d244 commit 2354916

File tree

1 file changed

+38
-2
lines changed
  • packages/serverless-offline-s3/src

1 file changed

+38
-2
lines changed

packages/serverless-offline-s3/src/s3.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,55 @@ class S3 {
3030
})
3131
);
3232

33+
this.events = [];
3334
this.listeners = [];
3435
}
3536

3637
create(events) {
37-
return Promise.all(events.map(({functionKey, s3}) => this._create(functionKey, s3)));
38+
this.events = events;
39+
return Promise.all(
40+
this.events.map(async ({s3}) => {
41+
const {bucket} = s3;
42+
await this._waitFor(bucket);
43+
})
44+
);
3845
}
3946

4047
start() {
41-
this.listeners.forEach(listener => listener.start());
48+
return Promise.all(
49+
this.events.map(async ({functionKey, s3}) => {
50+
const {event, bucket, rules} = s3;
51+
await this._waitFor(bucket);
52+
53+
const eventRules = rules || [];
54+
const prefix = (eventRules.find(rule => rule.prefix) || {prefix: '*'}).prefix;
55+
const suffix = (eventRules.find(rule => rule.suffix) || {suffix: '*'}).suffix;
56+
57+
const listener = this.client.listenBucketNotification(bucket, prefix, suffix, [event]);
58+
59+
listener.on('notification', async record => {
60+
if (record) {
61+
try {
62+
const lambdaFunction = this.lambda.get(functionKey);
63+
64+
const s3Notification = new S3Event(record);
65+
lambdaFunction.setEvent(s3Notification);
66+
67+
await lambdaFunction.runHandler();
68+
} catch (err) {
69+
log.warn(err.stack);
70+
}
71+
}
72+
});
73+
74+
this.listeners = [...this.listeners, listener];
75+
})
76+
);
4277
}
4378

4479
stop(timeout) {
4580
this.listeners.forEach(listener => listener.stop());
81+
this.listeners = [];
4682
}
4783

4884
_create(functionKey, rawS3EventDefinition) {

0 commit comments

Comments
 (0)