Skip to content

Commit

Permalink
fix: improved API and options
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Sep 16, 2019
1 parent a64a8b6 commit f5ab096
Show file tree
Hide file tree
Showing 6 changed files with 2,753 additions and 2,259 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ Accepts the following arguments and returns a [Nodemailer plugin][nodemailer-plu
* `options` (Object) - configuration options for `base64ToS3`
* `maxAge` (Number) - `Cache-Control` headers `max-age` value in milliseconds (defaults to 1 year = `31557600000`)
* `dir` (String) - Amazon S3 directory inside of `aws.params.Bucket` to upload assets to (defaults to `/` (root) - must end with a trailing forward slash `/`) – if you want to upload to a particular folder in a bucket, then set it here
* `cloudFrontDomainName` (String) - Amazon CloudFront domain name (e.g. `gzpnk2i1spnlm.cloudfront.net`)
* `cloudFrontDomainName` (String) - Amazon CloudFront domain name (e.g. `gzpnk2i1spnlm.cloudfront.net`) (will use `process.env.AWS_CLOUDFRONT_DOMAIN` environment variable by default if available)
* `aws` (Object) **Required** - configuration options for Amazon Web Services
* `accessKeyId` (String) **Required** - AWS IAM Access Key ID
* `secretAccessKey` (String) **Required** - AWS IAM Access Key ID
* `params` (Object) **Required**
* `Bucket` (String) **Required** - AWS Bucket Name

Expand Down
4 changes: 1 addition & 3 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ transport.use(
dir: 'base64-to-s3-test/',
cloudFrontDomainName: process.env.AWS_CLOUDFRONT_DOMAIN,
aws: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
params: {
Bucket: process.env.AWS_BUCKET
Bucket: process.env.AWS_S3_BUCKET
}
}
})
Expand Down
47 changes: 24 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { promisify } = require('util');
const zlib = require('zlib');
const isSANB = require('is-string-and-not-blank');
const mime = require('mime-types');
const _ = require('lodash');
const ms = require('ms');
Expand All @@ -11,11 +12,15 @@ const regexp = new RegExp(
/(<img[\s\S]*? src=")data:(image\/(?:png|jpe?g|gif|svg\+xml));base64,([\s\S]*?)("[\s\S]*?>)/g
);

const gzip = promisify(zlib.gzip).bind(zlib);

const base64ToS3 = opts => {
// set defaults
opts = _.defaults(opts, {
aws: {},
maxAge: ms('1yr'),
dir: '/'
dir: '/',
cloudFrontDomainName: process.env.AWS_CLOUDFRONT_DOMAIN || ''
});

if (!_.isNumber(opts.maxAge))
Expand All @@ -27,45 +32,41 @@ const base64ToS3 = opts => {

if (_.startsWith(opts.dir, '/')) opts.dir = opts.dir.substring(1);

if (!_.isObject(opts.aws))
throw new Error(
'AWS config object `aws` missing (e.g. `base64ToS3({ aws: { ... } })`'
);

// prepare AWS upload using config
const s3 = new AWS.S3(opts.aws);
const upload = promisify(s3.upload).bind(s3);

async function compile(mail, fn) {
try {
// get the html content from the mail
let html = await mail.resolveContent(mail.data, 'html');

// create a transformation array of promises
const promises = [];
const arr = [];
let result;
do {
result = regexp.exec(html);
if (result) {
const [original, start, mimeType, base64, end] = result;
promises.push(
transformImage({
original,
start,
mimeType,
base64,
end
})
);
arr.push({
original,
start,
mimeType,
base64,
end
});
}
} while (result);

// fulfill promises
const replacements = await Promise.all(promises);
const replacements = await Promise.all(
arr.map(obj => transformImage(obj))
);

// go through each replacement and replace original with new
_.each(replacements, replacement => {
html = html.replace(...replacement);
});
for (let i = 0; i < replacements.length; i++) {
html = html.replace(...replacements[i]);
}

// update the HTML of the email
mail.data.html = html;
Expand Down Expand Up @@ -95,7 +96,7 @@ const base64ToS3 = opts => {
}

// apply transformation and gzip file
const Body = await promisify(zlib.gzip).bind(zlib)(buffer);
const Body = await gzip(buffer);

// generate random filename
// get the file extension based on mimeType
Expand All @@ -115,9 +116,9 @@ const base64ToS3 = opts => {
// await s3obj.upload({ Body }).promise();
//
// so instead we use promisify to convert it to a promise
const data = await promisify(s3.upload).bind(s3)(obj);
const data = await upload(obj);

const replacement = _.isString(opts.cloudFrontDomainName)
const replacement = isSANB(opts.cloudFrontDomainName)
? `${start}https://${opts.cloudFrontDomainName}/${data.key}${end}`
: `${start}${data.Location}${end}`;

Expand Down
69 changes: 34 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,35 @@
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
],
"dependencies": {
"aws-sdk": "^2.422.0",
"aws-sdk": "^2.528.0",
"is-string-and-not-blank": "^0.0.2",
"lipo": "^0.0.10",
"lodash": "^4.17.11",
"mime-types": "^2.1.22",
"rev-hash": "^2.0.0"
"lodash": "^4.17.15",
"mime-types": "^2.1.24",
"rev-hash": "^3.0.0"
},
"devDependencies": {
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"auto-bind": "^2.0.0",
"ava": "^1.3.1",
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"auto-bind": "^2.1.0",
"ava": "^2.4.0",
"cheerio": "^1.0.0-rc.2",
"codecov": "^3.2.0",
"cross-env": "^5.2.0",
"dotenv": "^7.0.0",
"eslint": "^5.15.2",
"codecov": "^3.5.0",
"cross-env": "^5.2.1",
"dotenv": "^8.1.0",
"eslint": "^6.4.0",
"eslint-config-xo-lass": "^1.0.3",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-node": "^10.0.0",
"fixpack": "^2.3.1",
"husky": "^1.3.1",
"husky": "^3.0.5",
"image-to-uri": "^1.0.0",
"lint-staged": "^8.1.5",
"ms": "^2.1.1",
"nodemailer": "^5.1.1",
"nyc": "^13.3.0",
"remark-cli": "^6.0.1",
"remark-preset-github": "^0.0.13",
"validator": "^10.11.0",
"lint-staged": "^9.2.5",
"ms": "^2.1.2",
"nodemailer": "^6.3.0",
"nyc": "^14.1.1",
"remark-cli": "^7.0.0",
"remark-preset-github": "^0.0.16",
"validator": "^11.1.0",
"xo": "^0.24.0"
},
"engines": {
Expand All @@ -66,20 +67,18 @@
],
"license": "MIT",
"lint-staged": {
"linters": {
"*.js": [
"xo --fix",
"git add"
],
"*.md": [
"remark . -qfo",
"git add"
],
"package.json": [
"fixpack",
"git add"
]
}
"*.js": [
"xo --fix",
"git add"
],
"*.md": [
"remark . -qfo",
"git add"
],
"package.json": [
"fixpack",
"git add"
]
},
"main": "index.js",
"prettier": {
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ transport.use(
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
params: {
Bucket: process.env.AWS_BUCKET
Bucket: process.env.AWS_S3_BUCKET
}
}
})
Expand Down
Loading

0 comments on commit f5ab096

Please sign in to comment.