Permalink
Browse files

Merge pull request #2 from hitsujiwool/catching-up-with-original

原文を2553a94c6fまで追従させ、和訳を追加
  • Loading branch information...
2 parents f088197 + 305ea47 commit 3e95411ae2219bd89f5b11c40639aee44c212f61 @meso meso committed Oct 23, 2012
@@ -0,0 +1,15 @@
+var dnode = require('dnode');
+var net = require('net');
+
+var d = dnode();
+d.on('remote', function (remote) {
+ remote.transform('beep', function (cb) {
+ cb(10, function (s) {
+ console.log('beep:10 => ' + s);
+ d.end();
+ });
+ });
+});
+
+var c = net.connect(5004);
+c.pipe(d).pipe(c);
@@ -0,0 +1,16 @@
+var dnode = require('dnode');
+var net = require('net');
+
+var server = net.createServer(function (c) {
+ var d = dnode({
+ transform : function (s, cb) {
+ cb(function (n, fn) {
+ var oo = Array(n+1).join('o');
+ fn(s.replace(/[aeiou]{2,}/, oo).toUpperCase());
+ });
+ }
+ });
+ c.pipe(d).pipe(c);
+});
+
+server.listen(5004);
@@ -0,0 +1,13 @@
+var dnode = require('dnode');
+var net = require('net');
+
+var d = dnode();
+d.on('remote', function (remote) {
+ remote.transform('beep', function (s) {
+ console.log('beep => ' + s);
+ d.end();
+ });
+});
+
+var c = net.connect(5004);
+c.pipe(d).pipe(c);
@@ -0,0 +1,13 @@
+var dnode = require('dnode');
+var net = require('net');
+
+var server = net.createServer(function (c) {
+ var d = dnode({
+ transform : function (s, cb) {
+ cb(s.replace(/[aeiou]{2,}/, 'oo').toUpperCase())
+ }
+ });
+ c.pipe(d).pipe(c);
+});
+
+server.listen(5004);
@@ -0,0 +1,5 @@
+<html>
+<body>
+<script src="bundle.js"></script>
+</body>
+</html>
@@ -0,0 +1,19 @@
+var shoe = require('shoe');
+var emitStream = require('emit-stream');
+var JSONStream = require('JSONStream');
+
+var parser = JSONStream.parse([true]);
+var stream = parser.pipe(shoe('/sock')).pipe(parser);
+var ev = emitStream(stream);
+
+ev.on('lower', function (msg) {
+ var div = document.createElement('div');
+ div.textContent = msg.toLowerCase();
+ document.body.appendChild(div);
+});
+
+ev.on('upper', function (msg) {
+ var div = document.createElement('div');
+ div.textContent = msg.toUpperCase();
+ document.body.appendChild(div);
+});
@@ -0,0 +1,33 @@
+var http = require('http');
+var EventEmitter = require('events').EventEmitter;
+
+var server = http.createServer(require('ecstatic')(__dirname));
+server.listen(8080);
+
+var shoe = require('shoe');
+var emitStream = require('emit-stream');
+var JSONStream = require('JSONStream');
+
+var sock = shoe(function (stream) {
+ var ev = new EventEmitter;
+ emitStream(ev)
+ .pipe(JSONStream.stringify())
+ .pipe(stream)
+ ;
+
+ var intervals = [];
+
+ intervals.push(setInterval(function () {
+ ev.emit('upper', 'abc');
+ }, 500));
+
+ intervals.push(setInterval(function () {
+ ev.emit('lower', 'def');
+ }, 300));
+
+ stream.on('end', function () {
+ intervals.forEach(clearInterval);
+ });
+
+});
+sock.install(server, '/sock');
@@ -0,0 +1,21 @@
+var Model = require('scuttlebutt/model');
+var net = require('net');
+
+var m = new Model;
+var s = m.createStream();
+
+s.pipe(net.connect(8888, 'localhost')).pipe(s);
+
+m.on('update', function cb (key) {
+ // wait until we've gotten at least one count value from the network
+ if (key !== 'count') return;
+ m.removeListener('update', cb);
+
+ setInterval(function () {
+ m.set('count', Number(m.get('count')) + 1);
+ }, 100);
+});
+
+m.on('update', function (key, value) {
+ console.log(key + ' = ' + value);
+});
@@ -0,0 +1,26 @@
+var Model = require('scuttlebutt/model');
+var am = new Model;
+var as = am.createStream();
+
+var bm = new Model;
+var bs = bm.createStream();
+
+var cm = new Model;
+var cs = cm.createStream();
+
+var dm = new Model;
+var ds = dm.createStream();
+
+var em = new Model;
+var es = em.createStream();
+
+as.pipe(bs).pipe(as);
+bs.pipe(cs).pipe(bs);
+bs.pipe(ds).pipe(bs);
+ds.pipe(es).pipe(ds);
+
+em.on('update', function (key, value, source) {
+ console.log(key + ' => ' + value + ' from ' + source);
+});
+
+am.set('x', 555);
@@ -0,0 +1,8 @@
+{
+ "name" : "stream-handbook-scuttlebutt-example",
+ "private" : true,
+ "version" : "0.0.0",
+ "dependencies" : {
+ "scuttlebutt" : "~3.2.3"
+ }
+}
@@ -0,0 +1,17 @@
+var Model = require('scuttlebutt/model');
+var net = require('net');
+
+var m = new Model;
+m.set('count', '0');
+m.on('update', function (key, value) {
+ console.log(key + ' = ' + m.get('count'));
+});
+
+var server = net.createServer(function (stream) {
+ stream.pipe(m.createStream()).pipe(stream);
+});
+server.listen(8888);
+
+setInterval(function () {
+ m.set('count', Number(m.get('count')) + 1);
+}, 320);
View
@@ -0,0 +1,23 @@
+var Stream = require('stream');
+var s = new Stream;
+s.writable = true;
+
+var bytes = 0;
+
+s.write = function (buf) {
+ bytes += buf.length;
+};
+
+s.end = function (buf) {
+ if (arguments.length) s.write(buf);
+
+ s.writable = false;
+ console.log(bytes + ' bytes written');
+};
+
+s.destroy = function () {
+ s.writable = false;
+};
+
+var fs = require('fs');
+fs.createReadStream('/etc/passwd').pipe(s);
Oops, something went wrong.

0 comments on commit 3e95411

Please sign in to comment.