Skip to content

Commit

Permalink
makemehapi streams is bad example...
Browse files Browse the repository at this point in the history
  • Loading branch information
nkamc committed Jul 4, 2014
1 parent 9de0c42 commit c156bc8
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
101 changes: 101 additions & 0 deletions makemehapi/08-STREAMS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
var Fs = require('fs');
var Hapi = require('hapi');
var Stream = require('stream');
var Util = require('util');
var server = Hapi.createServer('localhost', Number(process.argv[2] || 8081));

function ROT13Stream () {

Stream.Transform.call(this);
}
Util.inherits(ROT13Stream, Stream.Transform);

ROT13Stream.table = {
A: 'N',
B: 'O',
C: 'P',
D: 'Q',
E: 'R',
F: 'S',
G: 'T',
H: 'U',
I: 'V',
J: 'W',
K: 'X',
L: 'Y',
M: 'Z',
N: 'A',
O: 'B',
P: 'C',
Q: 'D',
R: 'E',
S: 'F',
T: 'G',
U: 'H',
V: 'I',
W: 'J',
X: 'K',
Y: 'L',
Z: 'M',
a: 'n',
b: 'o',
c: 'p',
d: 'q',
e: 'r',
f: 's',
g: 't',
h: 'u',
i: 'v',
j: 'w',
k: 'x',
l: 'y',
m: 'z',
n: 'a',
o: 'b',
p: 'c',
q: 'd',
r: 'e',
s: 'f',
t: 'g',
u: 'h',
v: 'i',
w: 'j',
x: 'k',
y: 'l',
z: 'm'
};

ROT13Stream.prototype._transform = function (data, encoding, callback) {

encoding = (encoding == 'buffer' ? 'utf8' : encoding);
data = data.toString(encoding);

var modified = "";
var l = data.length;
for(var i = 0;i<l;i++) {
if (ROT13Stream.table.hasOwnProperty(data[i])) {
modified += ROT13Stream.table[data[i]];
} else {
modified += data[i];
}
}
this.push(modified, encoding);

callback();
};


server.route({
method: "GET",
path: "/",
config: {
handler: function (request, reply) {
var thisfile = Fs.createReadStream(__dirname + '/input.txt');
var rot13 = new ROT13Stream();

reply(thisfile.pipe(rot13));
}
}
});

server.start();
2 changes: 2 additions & 0 deletions makemehapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ but you actually need
{{query.name}}
```
*but* most intelligent people can figure it out.

> http://en.wikipedia.org/wiki/ROT13
1 change: 1 addition & 0 deletions makemehapi/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The Pursuit of Hapi-ness
91 changes: 91 additions & 0 deletions makemehapi/simple-rot13-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var Stream = require('stream');
var Util = require('util');

/*
To Use:
var rot13 = new ROT13Stream();
streamOne.pipe(rot13).pipe(process.stdout); // takes readstream streamOne, modifies with rot13, pipes into stdout
*/


function ROT13Stream() {
Stream.Transform.call(this);
}
Util.inherits(ROT13Stream, Stream.Transform);

ROT13Stream.table = {
A: 'N',
B: 'O',
C: 'P',
D: 'Q',
E: 'R',
F: 'S',
G: 'T',
H: 'U',
I: 'V',
J: 'W',
K: 'X',
L: 'Y',
M: 'Z',
N: 'A',
O: 'B',
P: 'C',
Q: 'D',
R: 'E',
S: 'F',
T: 'G',
U: 'H',
V: 'I',
W: 'J',
X: 'K',
Y: 'L',
Z: 'M',
a: 'n',
b: 'o',
c: 'p',
d: 'q',
e: 'r',
f: 's',
g: 't',
h: 'u',
i: 'v',
j: 'w',
k: 'x',
l: 'y',
m: 'z',
n: 'a',
o: 'b',
p: 'c',
q: 'd',
r: 'e',
s: 'f',
t: 'g',
u: 'h',
v: 'i',
w: 'j',
x: 'k',
y: 'l',
z: 'm'
};

ROT13Stream.prototype._transform = function (data, encoding, callback) {

encoding = (encoding == 'buffer' ? 'utf8' : encoding);
data = data.toString(encoding);

var modified = "";
var l = data.length;
for(var i = 0;i<l;i++) {
if (ROT13Stream.table.hasOwnProperty(data[i])) {
modified += ROT13Stream.table[data[i]];
} else {
modified += data[i];
}
}
this.push(modified, encoding);

callback();
};

module.exports = ROT13Stream;

0 comments on commit c156bc8

Please sign in to comment.