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