1
1
import type {
2
+ AttributeValue ,
2
3
Context ,
3
4
DynamoDBRecord ,
4
5
KinesisStreamRecord ,
@@ -8,60 +9,90 @@ import type {
8
9
const sqsRecordHandler = ( record : SQSRecord ) : string => {
9
10
const body = record . body ;
10
11
if ( body . includes ( 'fail' ) ) {
11
- throw Error ( 'Failed to process record.' ) ;
12
+ throw new Error ( 'Failed to process record.' ) ;
12
13
}
13
14
14
15
return body ;
15
16
} ;
16
17
17
- const asyncSqsRecordHandler = async ( record : SQSRecord ) : Promise < string > =>
18
- Promise . resolve ( sqsRecordHandler ( record ) ) ;
18
+ const asyncSqsRecordHandler = async ( record : SQSRecord ) : Promise < string > => {
19
+ const body = record . body ;
20
+ if ( body . includes ( 'fail' ) ) {
21
+ throw new Error ( 'Failed to process record.' ) ;
22
+ }
23
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ;
24
+ return body ;
25
+ } ;
19
26
20
27
const kinesisRecordHandler = ( record : KinesisStreamRecord ) : string => {
21
28
const body = record . kinesis . data ;
22
29
if ( body . includes ( 'fail' ) ) {
23
- throw Error ( 'Failed to process record.' ) ;
30
+ throw new Error ( 'Failed to process record.' ) ;
24
31
}
25
32
26
33
return body ;
27
34
} ;
28
35
29
36
const asyncKinesisRecordHandler = async (
30
37
record : KinesisStreamRecord
31
- ) : Promise < string > => Promise . resolve ( kinesisRecordHandler ( record ) ) ;
38
+ ) : Promise < string > => {
39
+ const body = record . kinesis . data ;
40
+ if ( body . includes ( 'fail' ) ) {
41
+ throw new Error ( 'Failed to process record.' ) ;
42
+ }
43
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ;
44
+ return body ;
45
+ } ;
32
46
33
- const dynamodbRecordHandler = ( record : DynamoDBRecord ) : object => {
47
+ const dynamodbRecordHandler = ( record : DynamoDBRecord ) : AttributeValue => {
34
48
const body = record . dynamodb ?. NewImage ?. Message || { S : 'fail' } ;
35
49
if ( body . S ?. includes ( 'fail' ) ) {
36
- throw Error ( 'Failed to process record.' ) ;
50
+ throw new Error ( 'Failed to process record.' ) ;
37
51
}
38
52
39
53
return body ;
40
54
} ;
41
55
42
- const asyncDynamodbRecordHandler = (
56
+ const asyncDynamodbRecordHandler = async (
43
57
record : DynamoDBRecord
44
- ) : Promise < object > => {
45
- return Promise . resolve ( dynamodbRecordHandler ( record ) ) ;
58
+ ) : Promise < AttributeValue > => {
59
+ const body = record . dynamodb ?. NewImage ?. Message || { S : 'fail' } ;
60
+ if ( body . S ?. includes ( 'fail' ) ) {
61
+ throw new Error ( 'Failed to process record.' ) ;
62
+ }
63
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ;
64
+ return body ;
46
65
} ;
47
66
48
67
const handlerWithContext = ( record : SQSRecord , context : Context ) : string => {
49
68
try {
50
69
if ( context . getRemainingTimeInMillis ( ) === 0 ) {
51
- throw Error ( 'No time remaining.' ) ;
70
+ throw new Error ( 'No time remaining.' ) ;
52
71
}
53
72
} catch {
54
- throw Error ( `Context possibly malformed. Displaying context:\n${ context } ` ) ;
73
+ throw new Error (
74
+ `Context possibly malformed. Displaying context:\n${ context } `
75
+ ) ;
55
76
}
56
77
57
78
return record . body ;
58
79
} ;
59
80
60
- const asyncHandlerWithContext = (
81
+ const asyncHandlerWithContext = async (
61
82
record : SQSRecord ,
62
83
context : Context
63
84
) : Promise < string > => {
64
- return Promise . resolve ( handlerWithContext ( record , context ) ) ;
85
+ try {
86
+ if ( context . getRemainingTimeInMillis ( ) === 0 ) {
87
+ throw new Error ( 'No time remaining.' ) ;
88
+ }
89
+ } catch {
90
+ throw new Error (
91
+ `Context possibly malformed. Displaying context:\n${ context } `
92
+ ) ;
93
+ }
94
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ;
95
+ return record . body ;
65
96
} ;
66
97
67
98
export {
0 commit comments