From 5f34b2af8818fc58e10fb9a821d307573abcc524 Mon Sep 17 00:00:00 2001 From: Jeffrey Sun Date: Mon, 12 Jun 2017 18:16:26 -0700 Subject: [PATCH 1/7] americanize fox colors --- js/constants/colors.js | 16 ++++++++++++++++ js/fox.js | 22 +++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 js/constants/colors.js diff --git a/js/constants/colors.js b/js/constants/colors.js new file mode 100644 index 0000000..41eda43 --- /dev/null +++ b/js/constants/colors.js @@ -0,0 +1,16 @@ +var head = { + brick: [0, 62, 53], + yellow: [48, 100, 64] +} +var bg = { + forest: '#006375', + green: '#63D6A3', + blue: '#358EFF', + salmon: '#FF9B7A', + coral: '#F96854', +} + +module.exports = { + head: head, + bg: bg +}; diff --git a/js/fox.js b/js/fox.js index 7ff2ae9..a249124 100644 --- a/js/fox.js +++ b/js/fox.js @@ -1,4 +1,5 @@ const Chance = require('chance'); +const colors = require('./constants/colors.js'); const hsl = function (h, s, l) { return "hsl(" + h + "," + s + "%, " + l + "%)"; @@ -14,15 +15,22 @@ const Fox = function (IMG_WIDTH, IMG_HEIGHT, seed) { // origin: head top left corner const kappa = chance.floating({min: 0.2, max: 0.45}) - const hue = chance.integer({min: 5, max: 50}); - const saturation = chance.integer({min: 70, max: 90}); - const lightness = chance.integer({min: 40, max: 60}); + const headColor = (function () { + const level = chance.floating({min: 0, max: 1}); + const result = []; + const min = colors.head.brick; + const max = colors.head.yellow; + for (let i=0; i Date: Mon, 12 Jun 2017 18:25:34 -0700 Subject: [PATCH 2/7] var -> const --- js/constants/colors.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/constants/colors.js b/js/constants/colors.js index 41eda43..53b6da1 100644 --- a/js/constants/colors.js +++ b/js/constants/colors.js @@ -1,8 +1,8 @@ -var head = { +const head = { brick: [0, 62, 53], yellow: [48, 100, 64] } -var bg = { +const bg = { forest: '#006375', green: '#63D6A3', blue: '#358EFF', From e32a19b954e639a53ddc206881c525e7d4ce6fdc Mon Sep 17 00:00:00 2001 From: joshbuddy Date: Mon, 12 Jun 2017 18:34:02 -0700 Subject: [PATCH 3/7] linting --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3fed837..c218e6a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Makes Fox Faces", "main": "server.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "lint": "standard" }, "repository": { "type": "git", @@ -25,5 +25,8 @@ "node-gyp": "^3.4.0", "sanitize-filename": "^1.6.1", "uuid": "^3.0.1" + }, + "devDependencies": { + "standard": "^8.6.0" } } From c5a8acc12db01ab75884229bac2e914cc62e7bb5 Mon Sep 17 00:00:00 2001 From: joshbuddy Date: Mon, 12 Jun 2017 18:39:14 -0700 Subject: [PATCH 4/7] Linting with "test" --- circle.yml | 11 ++ js/constants/colors.js | 20 ++-- js/fox.js | 78 +++++++------ js/render-fox.js | 243 ++++++++++++++++++++--------------------- run.js | 18 +-- server.js | 60 +++++----- 6 files changed, 219 insertions(+), 211 deletions(-) create mode 100644 circle.yml diff --git a/circle.yml b/circle.yml new file mode 100644 index 0000000..9cf2b94 --- /dev/null +++ b/circle.yml @@ -0,0 +1,11 @@ +dependencies: + pre: + - sudo apt-get install libgif-dev + +machine: + node: + version: 6.7.0 + +test: + override: + - npm run lint diff --git a/js/constants/colors.js b/js/constants/colors.js index 53b6da1..1c22588 100644 --- a/js/constants/colors.js +++ b/js/constants/colors.js @@ -1,16 +1,16 @@ const head = { - brick: [0, 62, 53], - yellow: [48, 100, 64] + brick: [0, 62, 53], + yellow: [48, 100, 64] } const bg = { - forest: '#006375', - green: '#63D6A3', - blue: '#358EFF', - salmon: '#FF9B7A', - coral: '#F96854', + forest: '#006375', + green: '#63D6A3', + blue: '#358EFF', + salmon: '#FF9B7A', + coral: '#F96854' } module.exports = { - head: head, - bg: bg -}; + head: head, + bg: bg +} diff --git a/js/fox.js b/js/fox.js index a249124..e0d63b5 100644 --- a/js/fox.js +++ b/js/fox.js @@ -1,95 +1,91 @@ -const Chance = require('chance'); -const colors = require('./constants/colors.js'); +const Chance = require('chance') +const colors = require('./constants/colors.js') const hsl = function (h, s, l) { - return "hsl(" + h + "," + s + "%, " + l + "%)"; + return 'hsl(' + h + ',' + s + '%, ' + l + '%)' } const Fox = function (IMG_WIDTH, IMG_HEIGHT, seed) { - if (seed) { - chance = new Chance(seed); - } else { - chance = new Chance(); - } + const chance = seed ? new Chance(seed) : new Chance() // origin: head top left corner const kappa = chance.floating({min: 0.2, max: 0.45}) const headColor = (function () { - const level = chance.floating({min: 0, max: 1}); - const result = []; - const min = colors.head.brick; - const max = colors.head.yellow; - for (let i=0; i { - app.listen(activePort, () => { - console.log('worker ' + worker.id + ' is listening on port ' + activePort); - }); + app.listen(activePort, () => { + console.log('worker ' + worker.id + ' is listening on port ' + activePort) + }) }, { - 'respawn': true, // workers will restart on failure - 'verbose': true, // logs what happens to console -}); + 'respawn': true, // workers will restart on failure + 'verbose': true // logs what happens to console +}) diff --git a/server.js b/server.js index 912edb4..53bed90 100644 --- a/server.js +++ b/server.js @@ -1,42 +1,44 @@ +/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "newrelic" }] */ + try { - const newrelic = require('newrelic'); + const newrelic = require('newrelic') } catch (e) { - console.error("WARNING unable to load newrelic") + console.error('WARNING unable to load newrelic') } -const express = require('express'); -const uuid = require('uuid/v4'); -const sanitize = require('sanitize-filename'); -const Canvas = require('canvas'); +const express = require('express') +const uuid = require('uuid/v4') +const sanitize = require('sanitize-filename') +const Canvas = require('canvas') -const Fox = require('./js/fox.js'); -const renderFox = require('./js/render-fox.js'); +const Fox = require('./js/fox.js') +const renderFox = require('./js/render-fox.js') -function composeImage(width, height, seed) { - seed = seed || uuid(); - const fox = Fox(width, height, seed); - const canvas = new Canvas(width, height); - renderFox(canvas, fox); - return canvas; +function composeImage (width, height, seed) { + seed = seed || uuid() + const fox = Fox(width, height, seed) + const canvas = new Canvas(width, height) + renderFox(canvas, fox) + return canvas }; -const cacheTimeout = 60 * 60 * 24 * 30; -const app = express(); +const cacheTimeout = 60 * 60 * 24 * 30 +const app = express() app.get('/healthcheck', (req, res) => { - res.status(200).end(); -}); + res.status(200).end() +}) app.get('/:width/:seed', (req, res) => { - let width = parseInt(req.params.width) || 400; - if (width > 400) width = 400; - const seed = sanitize(req.params.seed) || uuid(); - const canvas = composeImage(width, width, seed); - const buffer = canvas.toBuffer(); - res.set('Cache-Control', 'max-age=' + cacheTimeout); - res.set('Content-length', buffer.length); - res.type('png'); - res.end(buffer, 'binary'); -}); + let width = parseInt(req.params.width) || 400 + if (width > 400) width = 400 + const seed = sanitize(req.params.seed) || uuid() + const canvas = composeImage(width, width, seed) + const buffer = canvas.toBuffer() + res.set('Cache-Control', 'max-age=' + cacheTimeout) + res.set('Content-length', buffer.length) + res.type('png') + res.end(buffer, 'binary') +}) -module.exports = app; +module.exports = app From 37590f21fbb675dbde3bb30a458b51e67e189c30 Mon Sep 17 00:00:00 2001 From: Jeffrey Sun Date: Tue, 13 Jun 2017 15:27:24 -0700 Subject: [PATCH 5/7] two tests --- package.json | 5 ++++- test/test.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/test.js diff --git a/package.json b/package.json index 3fed837..2386d1c 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Makes Fox Faces", "main": "server.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha" }, "repository": { "type": "git", @@ -21,9 +21,12 @@ "chance": "^1.0.4", "express": "^4.14.0", "express-cluster": "0.0.4", + "mocha": "^3.4.2", "newrelic": "^1.35.1", "node-gyp": "^3.4.0", "sanitize-filename": "^1.6.1", + "sharp": "^0.18.1", + "supertest": "^3.0.0", "uuid": "^3.0.1" } } diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..5523a7a --- /dev/null +++ b/test/test.js @@ -0,0 +1,47 @@ +const request = require('supertest') +const assert = require('assert') +const sharp = require('sharp') + +const app = require('../server') + +const testUID = 4125370 + +describe('Foxy-moxy', () => { + describe('fox generation', () => { + it('should respect widths < 400', (done) => { + const width = 158 + request(app) + .get(`/${width}/${testUID}`) + .expect('Content-Type', 'image/png') + .expect(200) + .end(function(err, res) { + assert(!err, String(err)) + sharp(res.body).metadata((err, metadata) => { + assert(!err, String(err)) + assert.equal(metadata.format, 'png') + assert.equal(metadata.height, width) + assert.equal(metadata.width, width) + done() + }) + }) + }) + + it('should allow max width of 400', (done) => { + const width = 510 + request(app) + .get(`/${width}/${testUID}`) + .expect('Content-Type', 'image/png') + .expect(200) + .end(function(err, res) { + assert(!err, String(err)) + sharp(res.body).metadata((err, metadata) => { + assert(!err, String(err)) + assert.equal(metadata.format, 'png') + assert.equal(metadata.height, 400) + assert.equal(metadata.width, 400) + done() + }) + }) + }) + }) +}) From a55f0f454956c24bbe490946a1dadb38eb422a5d Mon Sep 17 00:00:00 2001 From: Jeffrey Sun Date: Tue, 13 Jun 2017 16:00:21 -0700 Subject: [PATCH 6/7] linting tests --- package.json | 1 + test/test.js | 64 +++++++++++++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index d66944a..f1044c1 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "node-gyp": "^3.4.0", "sanitize-filename": "^1.6.1", "sharp": "^0.18.1", + "standard": "^8.6.0", "supertest": "^3.0.0", "uuid": "^3.0.1" }, diff --git a/test/test.js b/test/test.js index 5523a7a..5d809d6 100644 --- a/test/test.js +++ b/test/test.js @@ -1,3 +1,5 @@ +const describe = require('mocha').describe +const it = require('mocha').it const request = require('supertest') const assert = require('assert') const sharp = require('sharp') @@ -7,41 +9,41 @@ const app = require('../server') const testUID = 4125370 describe('Foxy-moxy', () => { - describe('fox generation', () => { - it('should respect widths < 400', (done) => { - const width = 158 - request(app) + describe('fox generation', () => { + it('should respect widths < 400', (done) => { + const width = 158 + request(app) .get(`/${width}/${testUID}`) .expect('Content-Type', 'image/png') .expect(200) - .end(function(err, res) { + .end(function (err, res) { + assert(!err, String(err)) + sharp(res.body).metadata((err, metadata) => { assert(!err, String(err)) - sharp(res.body).metadata((err, metadata) => { - assert(!err, String(err)) - assert.equal(metadata.format, 'png') - assert.equal(metadata.height, width) - assert.equal(metadata.width, width) - done() - }) + assert.equal(metadata.format, 'png') + assert.equal(metadata.height, width) + assert.equal(metadata.width, width) + done() + }) }) - }) - - it('should allow max width of 400', (done) => { - const width = 510 - request(app) - .get(`/${width}/${testUID}`) - .expect('Content-Type', 'image/png') - .expect(200) - .end(function(err, res) { - assert(!err, String(err)) - sharp(res.body).metadata((err, metadata) => { - assert(!err, String(err)) - assert.equal(metadata.format, 'png') - assert.equal(metadata.height, 400) - assert.equal(metadata.width, 400) - done() - }) - }) - }) }) + + it('should allow max width of 400', (done) => { + const width = 510 + request(app) + .get(`/${width}/${testUID}`) + .expect('Content-Type', 'image/png') + .expect(200) + .end(function (err, res) { + assert(!err, String(err)) + sharp(res.body).metadata((err, metadata) => { + assert(!err, String(err)) + assert.equal(metadata.format, 'png') + assert.equal(metadata.height, 400) + assert.equal(metadata.width, 400) + done() + }) + }) + }) + }) }) From 7fe438aab36f2542473220ebde0c7b7469a04d5b Mon Sep 17 00:00:00 2001 From: Jeffrey Sun Date: Tue, 13 Jun 2017 16:42:51 -0700 Subject: [PATCH 7/7] move dev deps to devDependencies --- package.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index f1044c1..e90d137 100644 --- a/package.json +++ b/package.json @@ -22,16 +22,14 @@ "chance": "^1.0.4", "express": "^4.14.0", "express-cluster": "0.0.4", - "mocha": "^3.4.2", "newrelic": "^1.35.1", "node-gyp": "^3.4.0", "sanitize-filename": "^1.6.1", - "sharp": "^0.18.1", - "standard": "^8.6.0", - "supertest": "^3.0.0", "uuid": "^3.0.1" }, "devDependencies": { - "standard": "^8.6.0" + "mocha": "^3.4.2", + "sharp": "^0.18.1", + "supertest": "^3.0.0" } }