blitline-resizer
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -30,2 +30,4 @@ var Blitline = require('simple_blitline_node'); | ||
options.retina = (typeof options.retina === 'boolean') ? options.retina : false; | ||
options.images.forEach(function(imageUrl) { | ||
@@ -78,3 +80,3 @@ if (!validator.isURL(imageUrl)) | ||
var filename = renamer(imageUrl, size); | ||
job.functions = job.functions.concat(resizeJson(filename, size, true)); | ||
job.functions = job.functions.concat(resizeJson(filename, size, options.retina)); | ||
}); | ||
@@ -81,0 +83,0 @@ |
{ | ||
"name": "blitline-resizer", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Resizes images using Blitline", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "node test.js | tap-spec" | ||
}, | ||
@@ -29,5 +29,11 @@ "keywords": [ | ||
"body-parser": "^1.10.1", | ||
"dotenv": "^0.5.1", | ||
"image-size-stream": "^1.0.0", | ||
"knox": "^0.9.2", | ||
"lodash": "^3.3.1", | ||
"ngrok": "^0.1.98", | ||
"request": "^2.53.0", | ||
"tap-spec": "^2.2.2", | ||
"tape": "^3.0.3" | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/digidem/blitline-resizer.svg)](https://travis-ci.org/digidem/blitline-resizer) | ||
Blitline Image Resizer | ||
@@ -53,4 +55,10 @@ ====================== | ||
- [ ] Better test coverage | ||
- [x] Better test coverage | ||
- [ ] Support [polling](https://www.blitline.com/docs/polling) to avoid needing postback server | ||
- [ ] Allow resizing to fit both height and width | ||
## Changelog | ||
### v0.1.0 | ||
Add `options.retina` to also create retina versions of images. Defaults to false. **NB. Breaking change** previously it created retina versions by default. |
239
test.js
var test = require('tape'); | ||
var ngrok = require('ngrok'); | ||
var http = require('http'); | ||
var knox = require('knox'); | ||
var dotenv = require('dotenv'); | ||
var request = require('request'); | ||
var _ = require('lodash'); | ||
var createImageSizeStream = require('image-size-stream'); | ||
var Resizer = require('./'); | ||
var bodyParser = require('body-parser').json(); | ||
var config = require('./test_config.json'); | ||
var config; | ||
// Try to load env variables from .env | ||
dotenv.load(); | ||
var client = knox.createClient({ | ||
key: process.env.S3_KEY, | ||
secret: process.env.S3_SECRET, | ||
bucket: process.env.S3_BUCKET | ||
}); | ||
var port = 8080; | ||
var server, resize, postbackUrl, postbackCallback; | ||
var timeout = 30000; | ||
var images = ["https://farm6.staticflickr.com/5595/15103964698_67fae4c535_k_d.jpg", "https://farm4.staticflickr.com/3907/14459663821_329233b70e_k_d.jpg"]; | ||
var imagesMeta = {}; | ||
var s3files = []; | ||
var server, resizerDefaults, postbackUrl, postbackCallback; | ||
function setup(t) { | ||
@@ -27,9 +49,48 @@ test('Test setup', function(t) { | ||
t.error(err, 'successfully set up test server'); | ||
postbackUrl = url; | ||
resizerDefaults = { | ||
blitlineAppId: process.env.BLITLINE_APP_ID, | ||
postbackUrl: url, | ||
s3Bucket: process.env.S3_BUCKET | ||
}; | ||
t.end(); | ||
}); | ||
}); | ||
test('Get sizes of input images', function(t) { | ||
t.plan(images.length); | ||
images.forEach(function(image) { | ||
var imageSizeStream = createImageSizeStream(); | ||
imageSizeStream.on('size', function(dimensions) { | ||
imagesMeta[image] = (dimensions); | ||
// imageSizeStream.destroy(); | ||
t.pass('got image dimensions for ' + image); | ||
}); | ||
imageSizeStream.on('error', function(err) { | ||
t.fail(err); | ||
}); | ||
request(image).pipe(imageSizeStream); | ||
}); | ||
}); | ||
} | ||
function teardown(cb) { | ||
test('Cleaup tmp files from s3', function(t) { | ||
s3files = _.uniq(s3files).concat(images); | ||
t.plan(s3files.length); | ||
s3files.forEach(function(file) { | ||
file = file.split('/').pop(); | ||
client.deleteFile(file, function(err, res) { | ||
t.error(err, 'deleted file ' + file); | ||
}); | ||
}); | ||
}); | ||
test('Test teardown', function(t) { | ||
@@ -46,57 +107,151 @@ ngrok.disconnect(); | ||
function checkResizeReponse(t, resizeTask) { | ||
return function(err, data) { | ||
t.error(err && data.error, 'successfully sent job to Blitline'); | ||
t.ok(data.results, 'got response with job id from Blitline'); | ||
resizeTask.jobs = data.results.map(function(value) { return value.job_id; }); | ||
t.equal(resizeTask.jobs.length, resizeTask.images.length, 'all jobs submitted'); | ||
}; | ||
} | ||
function checkPostback(t, results) { | ||
t.equal(typeof results, 'object', 'got postback results from Blitline'); | ||
t.error(results.errors, 'Blitline processing complete without errors'); | ||
// store s3_url so we can delete later | ||
results.images.forEach(function(image) { | ||
s3files.push(image.s3_url); | ||
}); | ||
} | ||
function createTimeout(t) { | ||
return setTimeout(function() { | ||
t.fail('Timeout waiting for blitline to process jobs'); | ||
t.end(); | ||
}, timeout); | ||
} | ||
test('Resizes a single image', function(t) { | ||
var resize = Resizer({ | ||
blitlineAppId: config.blitline_app_id, | ||
postbackUrl: postbackUrl, | ||
s3Bucket: config.s3_bucket | ||
}), | ||
jobs; | ||
var resize = Resizer(resizerDefaults), | ||
timer; | ||
t.plan(6); | ||
var resizeTask = { | ||
images: [ images[0] ], | ||
sizes: [500, 1000] | ||
}; | ||
resize({ | ||
images: ["https://farm6.staticflickr.com/5595/15103964698_67fae4c535_k_d.jpg"], | ||
resize(resizeTask, checkResizeReponse(t, resizeTask)); | ||
resizeTask.timer = createTimeout(t); | ||
postbackCallback = function(err, req, res) { | ||
var results = req.body.results; | ||
checkPostback(t, results); | ||
var resultsSizes = results.images.map(function(image) { | ||
return image.meta.width; | ||
}); | ||
t.deepEqual(resultsSizes.sort(), resizeTask.sizes.sort(), 'got the correct sizes back'); | ||
clearTimeout(resizeTask.timer); | ||
t.end(); | ||
}; | ||
}); | ||
test('Resizes multiple images, returning a separate job for each image', function(t) { | ||
var resize = Resizer(resizerDefaults), | ||
timer; | ||
var resizeTask = { | ||
images: images, | ||
sizes: [500] | ||
}, function(err, data) { | ||
t.error(err && data.error, 'successfully sent job to Blitline'); | ||
t.ok(data.results, 'got response from Blitline'); | ||
jobs = data.results.map(function(value) { return value.job_id; }); | ||
}); | ||
}; | ||
resize(resizeTask, checkResizeReponse(t, resizeTask)); | ||
resizeTask.timer = createTimeout(t); | ||
postbackCallback = function(err, req, res) { | ||
var results = req.body.results; | ||
t.equal(typeof results, 'object', 'got postback results from Blitline'); | ||
t.error(results.errors, 'Blitline processing complete without errors'); | ||
t.notEqual(jobs.indexOf(results.job_id), -1, 'got results for test job'); | ||
// The resizer also copies the original to the destination | ||
t.equal(results.images.length, 2, 'got the correct number of images back'); | ||
checkPostback(t, results); | ||
var jobIndex = resizeTask.jobs.indexOf(results.job_id); | ||
t.notEqual(jobIndex, -1, 'got results for test job ' + results.job_id); | ||
resizeTask.jobs.splice(jobIndex, 1); | ||
if (!resizeTask.jobs.length) { | ||
t.pass('all jobs returned'); | ||
clearTimeout(resizeTask.timer); | ||
t.end(); | ||
} | ||
}; | ||
}); | ||
test('Resizes a multiple images', function(t) { | ||
var resize = Resizer({ | ||
blitlineAppId: config.blitline_app_id, | ||
postbackUrl: postbackUrl, | ||
s3Bucket: config.s3_bucket | ||
}), | ||
jobs; | ||
test('Creates retina versions', function(t) { | ||
var resize = Resizer(resizerDefaults), | ||
timer; | ||
t.plan(10); | ||
var resizeTask = { | ||
images: [ images[0] ], | ||
sizes: [400, 1000], | ||
retina: true | ||
}; | ||
resize({ | ||
images: ["https://farm6.staticflickr.com/5595/15103964698_67fae4c535_k_d.jpg", "https://farm4.staticflickr.com/3907/14459663821_329233b70e_k_d.jpg"], | ||
resize(resizeTask, checkResizeReponse(t, resizeTask)); | ||
resizeTask.timer = createTimeout(t); | ||
postbackCallback = function(err, req, res) { | ||
var results = req.body.results; | ||
checkPostback(t, results); | ||
var resultsSizes = results.images.map(function(image) { | ||
return image.meta.width; | ||
}).sort(); | ||
var expectedSizes = resizeTask.sizes.concat(resizeTask.sizes.map(function(size) { | ||
return size * 2; | ||
})).sort(); | ||
t.deepEqual(resultsSizes, expectedSizes, 'got the correct sizes back'); | ||
clearTimeout(resizeTask.timer); | ||
t.end(); | ||
}; | ||
}); | ||
test('Copies original to s3', function(t) { | ||
var resize = Resizer(resizerDefaults), | ||
timer; | ||
var resizeTask = { | ||
images: [ images[0] ], | ||
sizes: [500] | ||
}, function(err, data) { | ||
t.error(err && data.error, 'successfully sent job to Blitline'); | ||
t.ok(data.results, 'got response from Blitline'); | ||
jobs = data.results.map(function(value) { return value.job_id; }); | ||
}); | ||
}; | ||
resize(resizeTask, checkResizeReponse(t, resizeTask)); | ||
resizeTask.timer = createTimeout(t); | ||
postbackCallback = function(err, req, res) { | ||
var results = req.body.results; | ||
t.equal(typeof results, 'object', 'got postback results from Blitline'); | ||
t.error(results.errors, 'Blitline processing complete without errors'); | ||
t.notEqual(jobs.indexOf(results.job_id), -1, 'got results for test job ' + results.job_id); | ||
// The resizer also copies the original to the destination | ||
t.equal(results.images.length, 2, 'got the correct number of images back for job ' + results.job_id); | ||
checkPostback(t, results); | ||
var imageSizeStream = createImageSizeStream(); | ||
imageSizeStream.on('size', function(dimensions) { | ||
// imageSizeStream.destroy(); | ||
t.equal(dimensions.width, imagesMeta[images[0]].width, 'original present on s3'); | ||
clearTimeout(resizeTask.timer); | ||
t.end(); | ||
}); | ||
imageSizeStream.on('error', function(err) { | ||
clearTimeout(resizeTask.timer); | ||
t.end(err); | ||
}); | ||
var url = 'http://' + process.env.S3_BUCKET + '.s3.amazonaws.com/' + images[0].split('/').pop(); | ||
request(url).pipe(imageSizeStream); | ||
}; | ||
@@ -103,0 +258,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14381
7
312
64
9
6