Permalink
Browse files

Merge pull request #18 from terencehuynh/master

Add a `flip` option
  • Loading branch information...
hugomd committed Jun 22, 2018
2 parents f7ceac5 + 02fa850 commit b219378417cc25a83348ef3f22986f7acf2e0787
Showing with 53 additions and 16 deletions.
  1. +3 −0 .prettierrc
  2. +50 −16 index.js
@@ -0,0 +1,3 @@
semi: true
useTabs: false
singleQuote: true
@@ -1,60 +1,94 @@
const fs = require('mz/fs');
const http = require('http');
const {Readable} = require('stream');
const url = require('url');
const { Readable } = require('stream');
const colors = require('colors/safe');
const frames = [];
const flipped = [];
// Setup frames in memory
fs.readdir('./frames').then(data => {
fs.readdir('./frames').then(data => {
data.forEach(async frame => {
const f = await fs.readFile(`./frames/${frame}`);
frames.push(f.toString());
})
flipped.push(
f
.toString()
.split('')
.reverse()
.join('')
);
});
});
const colorsOptions = ['red', 'yellow', 'green', 'blue', 'magenta', 'cyan', 'white'];
const colorsOptions = [
'red',
'yellow',
'green',
'blue',
'magenta',
'cyan',
'white'
];
const numColors = colorsOptions.length;
const streamer = stream => {
const streamer = (stream, opts) => {
const { flip } = opts;
let index = 0;
let lastColor = -1;
let newColor = 0;
let frame = null;
return setInterval(() => {
if (index >= frames.length) index = 0; stream.push('\033[2J\033[H');
if (index >= frames.length) index = 0;
if (index < 0) index = frames.length - 1;
stream.push('\033[2J\033[H');
newColor = Math.floor(Math.random() * numColors);
// Reroll for a new color if it was the same as last frame
if(newColor == lastColor) {
newColor += (1 + Math.floor(Math.random() * (numColors - 1)));
if (newColor == lastColor) {
newColor += 1 + Math.floor(Math.random() * (numColors - 1));
newColor %= numColors;
}
lastColor = newColor;
stream.push(colors[colorsOptions[newColor]](frames[index]));
frame = flip ? flipped[index] : frames[index];
stream.push(colors[colorsOptions[newColor]](frame));
index++;
}, 70);
}
};
const validateQuery = query => {
const { flip } = query;
return {
flip: typeof flip === 'string' && flip.toLowerCase() == 'true'
};
};
const server = http.createServer((req, res) => {
if (req.headers && req.headers['user-agent'] && !req.headers['user-agent'].includes('curl')) {
res.writeHead(302, {'Location': 'https://github.com/hugomd/parrot.live'});
if (
req.headers &&
req.headers['user-agent'] &&
!req.headers['user-agent'].includes('curl')
) {
res.writeHead(302, { Location: 'https://github.com/hugomd/parrot.live' });
return res.end();
}
const { query } = url.parse(req.url, true);
const stream = new Readable();
stream._read = function noop () {};
stream._read = function noop() {};
stream.pipe(res);
const interval = streamer(stream);
const interval = streamer(stream, validateQuery(query));
req.on('close', () => {
stream.destroy();
clearInterval(interval);
});
});
const port = process.env.PARROT_PORT || 3000;
server.listen(port, err => {
if (err) throw err;

0 comments on commit b219378

Please sign in to comment.