@@ -30,19 +30,55 @@ class S3 {
30
30
} )
31
31
) ;
32
32
33
+ this . events = [ ] ;
33
34
this . listeners = [ ] ;
34
35
}
35
36
36
37
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
+ ) ;
38
45
}
39
46
40
47
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
+ ) ;
42
77
}
43
78
44
79
stop ( timeout ) {
45
80
this . listeners . forEach ( listener => listener . stop ( ) ) ;
81
+ this . listeners = [ ] ;
46
82
}
47
83
48
84
_create ( functionKey , rawS3EventDefinition ) {
0 commit comments