Skip to content

Commit

Permalink
Switch to AWS SDK v3
Browse files Browse the repository at this point in the history
Fixes #144.
  • Loading branch information
lpsinger committed Jun 6, 2023
1 parent 6aa3fa4 commit 876f801
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
37 changes: 22 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
"use strict";

var AWS = require('aws-sdk');
var {
CopyObjectCommand,
GetObjectCommand,
S3Client
} = require('@aws-sdk/client-s3');
var {
SendEmailCommand,
SESv2Client
} = require('@aws-sdk/client-sesv2');

console.log("AWS Lambda SES Forwarder // @arithmetric // Version 5.0.0");

Expand Down Expand Up @@ -160,15 +168,15 @@ exports.fetchMessage = function(data) {
data.config.emailKeyPrefix + data.email.messageId
});
return new Promise(function(resolve, reject) {
data.s3.copyObject({
data.s3.send(new CopyObjectCommand({
Bucket: data.config.emailBucket,
CopySource: data.config.emailBucket + '/' + data.config.emailKeyPrefix +
data.email.messageId,
Key: data.config.emailKeyPrefix + data.email.messageId,
ACL: 'private',
ContentType: 'text/plain',
StorageClass: 'STANDARD'
}, function(err) {
}), function(err) {
if (err) {
data.log({
level: "error",
Expand All @@ -181,10 +189,10 @@ exports.fetchMessage = function(data) {
}

// Load the raw email from S3
data.s3.getObject({
data.s3.send(new GetObjectCommand({
Bucket: data.config.emailBucket,
Key: data.config.emailKeyPrefix + data.email.messageId
}, function(err, result) {
}), function(err, result) {
if (err) {
data.log({
level: "error",
Expand Down Expand Up @@ -292,21 +300,20 @@ exports.processMessage = function(data) {
* @return {object} - Promise resolved with data.
*/
exports.sendMessage = function(data) {
var params = {
Destinations: data.recipients,
Source: data.originalRecipient,
RawMessage: {
Data: data.emailData
}
};
data.log({
level: "info",
message: "sendMessage: Sending email via SES. Original recipients: " +
data.originalRecipients.join(", ") + ". Transformed recipients: " +
data.recipients.join(", ") + "."
});
return new Promise(function(resolve, reject) {
data.ses.sendRawEmail(params, function(err, result) {
data.ses.send(new SendEmailCommand({
Content: {
Raw: {
Data: data.emailData
}
}
}), function(err, result) {
if (err) {
data.log({
level: "error",
Expand Down Expand Up @@ -351,9 +358,9 @@ exports.handler = function(event, context, callback, overrides) {
context: context,
config: overrides && overrides.config ? overrides.config : defaultConfig,
log: overrides && overrides.log ? overrides.log : console.log,
ses: overrides && overrides.ses ? overrides.ses : new AWS.SES(),
ses: overrides && overrides.ses ? overrides.ses : new SESv2Client(),
s3: overrides && overrides.s3 ?
overrides.s3 : new AWS.S3({signatureVersion: 'v4'})
overrides.s3 : new S3Client({signatureVersion: 'v4'})
};
Promise.series(steps, data)
.then(function(data) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"test": "nyc --statements 100 -- mocha -- --check-leaks --timeout 3000"
},
"dependencies": {
"aws-sdk": "~2.1083.0"
"@aws-sdk/client-sesv2": "^3.188.0",
"@aws-sdk/client-s3": "^3.188.0"
},
"devDependencies": {
"coveralls": "^3.0.7",
Expand Down
27 changes: 13 additions & 14 deletions test/fetchMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

var assert = require("assert");

var {GetObjectCommand, CopyObjectCommand} = require('@aws-sdk/client-s3');

var index = require("../index");

describe('index.js', function() {
Expand All @@ -25,11 +27,11 @@ describe('index.js', function() {
},
log: console.log,
s3: {
copyObject: function(options, callback) {
callback(null);
},
getObject: function(options, callback) {
callback(null, {Body: "email data"});
send: function(options, callback) {
if (options instanceof CopyObjectCommand)
callback(null);
else if (options instanceof GetObjectCommand)
callback(null, {Body: "email data"});
}
}
};
Expand All @@ -55,10 +57,7 @@ describe('index.js', function() {
},
log: console.log,
s3: {
copyObject: function(options, callback) {
callback(true);
},
getObject: function(options, callback) {
send: function(options, callback) {
callback(true);
}
}
Expand All @@ -83,11 +82,11 @@ describe('index.js', function() {
},
log: console.log,
s3: {
copyObject: function(options, callback) {
callback(null);
},
getObject: function(options, callback) {
callback(true);
send: function(options, callback) {
if (options instanceof CopyObjectCommand)
callback(null);
else if (options instanceof GetObjectCommand)
callback(true);
}
}
};
Expand Down
14 changes: 8 additions & 6 deletions test/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
var assert = require("assert");
var fs = require("fs");

var {GetObjectCommand, CopyObjectCommand} = require('@aws-sdk/client-s3');

var index = require("../index");

describe('index.js', function() {
Expand All @@ -16,15 +18,15 @@ describe('index.js', function() {
};
var overrides = {
s3: {
copyObject: function(options, callback) {
callback(null);
},
getObject: function(options, callback) {
callback(null, {Body: "email data"});
send: function(options, callback) {
if (options instanceof CopyObjectCommand)
callback(null);
else if (options instanceof GetObjectCommand)
callback(null, {Body: "email data"});
}
},
ses: {
sendRawEmail: function(options, callback) {
send: function(options, callback) {
callback(null, {status: "ok"});
}
},
Expand Down
4 changes: 2 additions & 2 deletions test/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('index.js', function() {
context: {},
log: console.log,
ses: {
sendRawEmail: function(options, callback) {
send: function(options, callback) {
callback(null, {status: "ok"});
}
}
Expand All @@ -45,7 +45,7 @@ describe('index.js', function() {
context: {},
log: console.log,
ses: {
sendRawEmail: function(options, callback) {
send: function(options, callback) {
callback(true);
}
}
Expand Down

0 comments on commit 876f801

Please sign in to comment.