1- import { S3Client , ListObjectsV2Command , GetObjectCommand } from '@aws-sdk/client-s3' ;
2- import { Readable } from 'stream' ;
1+ import {
2+ S3Client ,
3+ ListObjectsV2Command ,
4+ GetObjectCommand ,
5+ } from '@aws-sdk/client-s3' ;
6+ import { Readable } from 'node:stream' ;
37
48async function streamToString ( stream : Readable ) : Promise < string > {
59 return await new Promise ( ( resolve , reject ) => {
610 const chunks : Buffer [ ] = [ ] ;
7- stream . on ( " data" , ( chunk ) => chunks . push ( Buffer . from ( chunk ) ) ) ;
8- stream . on ( " error" , ( err ) => reject ( err ) ) ;
9- stream . on ( " end" , ( ) => resolve ( Buffer . concat ( chunks ) . toString ( "utf-8" ) ) ) ;
11+ stream . on ( ' data' , ( chunk ) => chunks . push ( Buffer . from ( chunk ) ) ) ;
12+ stream . on ( ' error' , ( err ) => reject ( err ) ) ;
13+ stream . on ( ' end' , ( ) => resolve ( Buffer . concat ( chunks ) . toString ( 'utf8' ) ) ) ;
1014 } ) ;
1115}
1216
@@ -22,38 +26,40 @@ export class EmailHelper {
2226 // This will stop working if one environment ever ends up with 1,000 email on it.
2327 // This seems very unlikely but if it happens we can revisit this and add pagination.
2428 async getEmailForTemplateId ( templateId : string , dateCutoff : Date ) {
25-
2629 const command = new ListObjectsV2Command ( {
27- Bucket : this . testEmailBucketName ,
28- Prefix : this . testEmailPrefix ,
30+ Bucket : this . testEmailBucketName ,
31+ Prefix : this . testEmailPrefix ,
2932 } ) ;
3033
3134 const { Contents = [ ] } = await this . s3Client . send ( command ) ;
3235
33- const sortedKeys = Contents
34- . filter ( ( { LastModified } ) => ( LastModified ?? 0 ) > dateCutoff )
35- . sort ( ( a , b ) => ( ( b . LastModified ?. getTime ( ) ?? 0 ) - ( a . LastModified ?. getTime ( ) ?? 0 ) ) ) ;
36+ const sortedKeys = Contents . filter (
37+ ( { LastModified } ) => ( LastModified ?? 0 ) > dateCutoff
38+ ) . sort (
39+ ( a , b ) =>
40+ ( b . LastModified ?. getTime ( ) ?? 0 ) - ( a . LastModified ?. getTime ( ) ?? 0 )
41+ ) ;
3642
3743 // SES does not tell us what the S3 keys are going to be for received emails,
3844 // so we have to search all recent ones to ensure we get the right one and
3945 // not one that was generated by a different test
4046 for ( const { Key } of sortedKeys ) {
41- const getCommand = new GetObjectCommand ( {
42- Bucket : this . testEmailBucketName ,
47+ const getCommand = new GetObjectCommand ( {
48+ Bucket : this . testEmailBucketName ,
4349 Key,
44- } ) ;
50+ } ) ;
4551
46- const { Body } = await this . s3Client . send ( getCommand ) ;
52+ const { Body } = await this . s3Client . send ( getCommand ) ;
4753
48- if ( ! Body || ! ( Body instanceof Readable ) ) {
49- throw new Error ( 'Unexpected response body type' ) ;
50- }
54+ if ( ! Body || ! ( Body instanceof Readable ) ) {
55+ throw new Error ( 'Unexpected response body type' ) ;
56+ }
5157
52- const content = await streamToString ( Body ) ;
58+ const content = await streamToString ( Body ) ;
5359
54- if ( content . includes ( templateId ) ) {
55- return content ;
56- }
60+ if ( content . includes ( templateId ) ) {
61+ return content ;
62+ }
5763 }
5864
5965 throw new Error ( 'Email not found' ) ;
0 commit comments