Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to AWS SDK v3 #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 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 All @@ -195,8 +203,12 @@ exports.fetchMessage = function(data) {
return reject(
new Error("Error: Failed to load message body from S3."));
}
data.emailData = result.Body.toString();
return resolve(data);
result.Body.transformToString().then(
body => {
data.emailData = body;
resolve(data);
}
);
});
});
});
Expand Down Expand Up @@ -292,21 +304,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: Buffer.from(data.emailData)
}
}
}), function(err, result) {
if (err) {
data.log({
level: "error",
Expand Down Expand Up @@ -351,9 +362,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