Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

examples

  • Loading branch information...
commit 2305da7fb151c9581546eaa1d19129afca28b4a2 0 parents
@substack authored
Showing with 13,640 additions and 0 deletions.
  1. +5 −0 crdt/000_title.js
  2. +9 −0 crdt/010.js
  3. +7 −0 crdt/020.js
  4. +1 −0  crdt/100_server.js
  5. +8 −0 crdt/101_server.js
  6. +9 −0 crdt/102_server.js
  7. +12 −0 crdt/103_server.js
  8. +17 −0 crdt/104_server.js
  9. +18 −0 crdt/105_server.js
  10. +24 −0 crdt/106_server.js
  11. +24 −0 crdt/107_server.js
  12. +1 −0  crdt/200_browser.js
  13. +2 −0  crdt/201_browser.js
  14. +6 −0 crdt/202_browser.js
  15. +10 −0 crdt/203_browser.js
  16. +10 −0 crdt/204_browser.js
  17. +22 −0 crdt/205_browser.js
  18. +23 −0 crdt/206_browser.js
  19. +22 −0 crdt/207_browser.js
  20. +23 −0 crdt/208_browser.js
  21. +60 −0 crdt/209_browser.js
  22. +2 −0  crdt/300_seed.js
  23. +6 −0 crdt/301_seed.js
  24. +11 −0 crdt/302_seed.js
  25. +60 −0 crdt/src/browser.js
  26. +12 −0 crdt/src/seed.js
  27. +24 −0 crdt/src/server.js
  28. +354 −0 crdt/src/static/bundle.js
  29. +18 −0 crdt/src/static/index.html
  30. +5 −0 dnode/000_title.js
  31. +5 −0 dnode/001_title.js
  32. +5 −0 dnode/002_title.js
  33. +5 −0 dnode/003_title.js
  34. +5 −0 dnode/004_title.js
  35. +5 −0 dnode/005_server.js
  36. +11 −0 dnode/010_server.js
  37. +11 −0 dnode/020_server.js
  38. +12 −0 dnode/030_server.js
  39. +4 −0 dnode/040_client.js
  40. +8 −0 dnode/050_client.js
  41. +10 −0 dnode/060_client.js
  42. +12 −0 dnode/070_client.js
  43. +7 −0 dnode/run1.sh
  44. +2 −0  mux-demux/00.js
  45. +5 −0 mux-demux/01.js
  46. +6 −0 mux-demux/02.js
  47. +10 −0 mux-demux/03.js
  48. +16 −0 mux-demux/04.js
  49. +21 −0 mux-demux/05.js
  50. +26 −0 mux-demux/06.js
  51. +26 −0 mux-demux/07.js
  52. +33 −0 mux-demux/08.js
  53. +36 −0 mux-demux/09.js
  54. +40 −0 mux-demux/10.js
  55. +23 −0 package.json
  56. +7 −0 roll_your_own_socketio/000_title.js
  57. +1 −0  roll_your_own_socketio/001_server.js
  58. +4 −0 roll_your_own_socketio/004_server.js
  59. +6 −0 roll_your_own_socketio/010_server.js
  60. +12 −0 roll_your_own_socketio/020_server.js
  61. +15 −0 roll_your_own_socketio/030_server.js
  62. +1 −0  roll_your_own_socketio/040_browser.js
  63. +2 −0  roll_your_own_socketio/050_browser.js
  64. +7 −0 roll_your_own_socketio/060_browser.js
  65. +16 −0 roll_your_own_socketio/070_server.js
  66. +18 −0 roll_your_own_socketio/080_server.js
  67. +18 −0 roll_your_own_socketio/090_server.js
  68. +20 −0 roll_your_own_socketio/100_server.js
  69. +22 −0 roll_your_own_socketio/110_server.js
  70. +24 −0 roll_your_own_socketio/120_server.js
  71. +33 −0 roll_your_own_socketio/130_server.js
  72. +7 −0 roll_your_own_socketio/140_browser.js
  73. +9 −0 roll_your_own_socketio/150_browser.js
  74. +10 −0 roll_your_own_socketio/160_browser.js
  75. +10 −0 roll_your_own_socketio/170_browser.js
  76. +15 −0 roll_your_own_socketio/180_browser.js
  77. +20 −0 roll_your_own_socketio/190_browser.js
  78. +7 −0 roll_your_own_socketio/run1.sh
  79. +7 −0 roll_your_own_socketio/run2.sh
  80. +4,152 −0 roll_your_own_socketio/static/bundle.js
  81. +5 −0 roll_your_own_socketio/static/index.html
  82. +7 −0 scuttlebutt/000.js
  83. +3 −0  scuttlebutt/005.js
  84. +6 −0 scuttlebutt/010.js
  85. +8 −0 scuttlebutt/020.js
  86. +12 −0 scuttlebutt/030.js
  87. +15 −0 scuttlebutt/040.js
  88. +9 −0 scuttlebutt/050.js
  89. +11 −0 scuttlebutt/060.js
  90. +14 −0 scuttlebutt/070.js
  91. +18 −0 scuttlebutt/080.js
  92. +23 −0 scuttlebutt/090.js
  93. +31 −0 scuttlebutt/100.js
  94. +18 −0 scuttlebutt/110.js
  95. +18 −0 scuttlebutt/120.js
  96. +19 −0 scuttlebutt/130.js
  97. +46 −0 scuttlebutt/140.js
  98. +2 −0  scuttlebutt/150_browser.js
  99. +5 −0 scuttlebutt/160_browser.js
  100. +7 −0 scuttlebutt/170_browser.js
  101. +11 −0 scuttlebutt/180_browser.js
  102. +14 −0 scuttlebutt/190_browser.js
  103. +21 −0 scuttlebutt/200_browser.js
  104. +24 −0 scuttlebutt/210_browser.js
  105. +25 −0 scuttlebutt/220_browser.js
  106. +32 −0 scuttlebutt/230_browser.js
  107. +2 −0  scuttlebutt/peer.sh
  108. +3 −0  scuttlebutt/run1.sh
  109. +7 −0 scuttlebutt/run2.sh
  110. +10 −0 scuttlebutt/run3.sh
  111. +7,146 −0 scuttlebutt/static/bundle.js
  112. +5 −0 scuttlebutt/static/index.html
  113. +5 −0 seaport/000_title.js
  114. +12 −0 seaport/001.js
  115. +16 −0 seaport/002.js
  116. +20 −0 seaport/003.js
  117. +1 −0  seaport/110_worker.js
  118. +5 −0 seaport/120_worker.js
  119. +8 −0 seaport/130_worker.js
  120. +9 −0 seaport/140_worker.js
  121. +12 −0 seaport/150_worker.js
  122. +12 −0 seaport/160_worker.js
  123. +13 −0 seaport/170_worker.js
  124. +13 −0 seaport/180_worker.js
  125. +16 −0 seaport/190_worker.js
  126. +17 −0 seaport/200_worker.js
  127. +19 −0 seaport/210_worker.js
  128. +19 −0 seaport/220_worker.js
  129. +17 −0 seaport/230_worker.js
  130. +19 −0 seaport/240_worker.js
  131. +21 −0 seaport/250_worker.js
  132. +1 −0  seaport/300_web.js
  133. +2 −0  seaport/310_web.js
  134. +5 −0 seaport/320_web.js
  135. +7 −0 seaport/330_web.js
  136. +9 −0 seaport/340_web.js
  137. +14 −0 seaport/350_web.js
  138. +16 −0 seaport/360_web.js
  139. +18 −0 seaport/370_web.js
  140. +19 −0 seaport/380_web.js
  141. +22 −0 seaport/390_web.js
  142. +30 −0 seaport/400_web.js
  143. +31 −0 seaport/410_web.js
  144. +23 −0 seaport/500_worker.js
  145. +10 −0 seaport/run1.sh
  146. +10 −0 seaport/run2.sh
  147. 0  seaport/static/index.html
5 crdt/000_title.js
@@ -0,0 +1,5 @@
+/*
+
+ crdt - commutative replicated data types
+
+*/
9 crdt/010.js
@@ -0,0 +1,9 @@
+/*
+
+ commutativity:
+
+ 1 + 2 === 3
+
+ 2 + 1 === 3
+
+*/
7 crdt/020.js
@@ -0,0 +1,7 @@
+/*
+
+ commutativity:
+
+ insert(before, node, after);
+
+*/
1  crdt/100_server.js
@@ -0,0 +1 @@
+var doc = new(require('crdt').Doc);
8 crdt/101_server.js
@@ -0,0 +1,8 @@
+var doc = new(require('crdt').Doc);
+
+var http = require('http');
+
+var server = http.createServer(function (req, res) {
+ // ...
+});
+server.listen(Number(process.argv[2]));
9 crdt/102_server.js
@@ -0,0 +1,9 @@
+var doc = new(require('crdt').Doc);
+
+var http = require('http');
+var ecstatic = require('ecstatic')(__dirname + '/static');
+
+var server = http.createServer(function (req, res) {
+ ecstatic(req, res);
+});
+server.listen(Number(process.argv[2]));
12 crdt/103_server.js
@@ -0,0 +1,12 @@
+var doc = new(require('crdt').Doc);
+
+var http = require('http');
+var ecstatic = require('ecstatic')(__dirname + '/static');
+
+var server = http.createServer(function (req, res) {
+ if (req.url === '/_replicate') {
+ req.pipe(doc.createStream()).pipe(res);
+ }
+ else ecstatic(req, res);
+});
+server.listen(Number(process.argv[2]));
17 crdt/104_server.js
@@ -0,0 +1,17 @@
+var doc = new(require('crdt').Doc);
+
+var http = require('http');
+var ecstatic = require('ecstatic')(__dirname + '/static');
+
+var server = http.createServer(function (req, res) {
+ if (req.url === '/_replicate') {
+ req.pipe(doc.createStream()).pipe(res);
+ }
+ else ecstatic(req, res);
+});
+server.listen(Number(process.argv[2]));
+
+var request = require('request');
+process.argv.slice(3).forEach(function (port) {
+ // ...
+});
18 crdt/105_server.js
@@ -0,0 +1,18 @@
+var doc = new(require('crdt').Doc);
+
+var http = require('http');
+var ecstatic = require('ecstatic')(__dirname + '/static');
+
+var server = http.createServer(function (req, res) {
+ if (req.url === '/_replicate') {
+ req.pipe(doc.createStream()).pipe(res);
+ }
+ else ecstatic(req, res);
+});
+server.listen(Number(process.argv[2]));
+
+var request = require('request');
+process.argv.slice(3).forEach(function (port) {
+ var s = doc.createStream();
+ s.pipe(request.put('http://localhost:' + port + '/_replicate')).pipe(s);
+});
24 crdt/106_server.js
@@ -0,0 +1,24 @@
+var doc = new(require('crdt').Doc);
+
+var http = require('http');
+var ecstatic = require('ecstatic')(__dirname + '/static');
+
+var server = http.createServer(function (req, res) {
+ if (req.url === '/_replicate') {
+ req.pipe(doc.createStream()).pipe(res);
+ }
+ else ecstatic(req, res);
+});
+server.listen(Number(process.argv[2]));
+
+var shoe = require('shoe');
+var sock = shoe(function (stream) {
+ // ...
+});
+sock.install(server, '/sock');
+
+var request = require('request');
+process.argv.slice(3).forEach(function (port) {
+ var s = doc.createStream();
+ s.pipe(request.put('http://localhost:' + port + '/_replicate')).pipe(s);
+});
24 crdt/107_server.js
@@ -0,0 +1,24 @@
+var doc = new(require('crdt').Doc);
+
+var http = require('http');
+var ecstatic = require('ecstatic')(__dirname + '/static');
+
+var server = http.createServer(function (req, res) {
+ if (req.url === '/_replicate') {
+ req.pipe(doc.createStream()).pipe(res);
+ }
+ else ecstatic(req, res);
+});
+server.listen(Number(process.argv[2]));
+
+var shoe = require('shoe');
+var sock = shoe(function (stream) {
+ stream.pipe(doc.createStream()).pipe(stream);
+});
+sock.install(server, '/sock');
+
+var request = require('request');
+process.argv.slice(3).forEach(function (port) {
+ var s = doc.createStream();
+ s.pipe(request.put('http://localhost:' + port + '/_replicate')).pipe(s);
+});
1  crdt/200_browser.js
@@ -0,0 +1 @@
+var doc = new(require('crdt').Doc);
2  crdt/201_browser.js
@@ -0,0 +1,2 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
6 crdt/202_browser.js
@@ -0,0 +1,6 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+var shoe = require('shoe');
+var c = shoe('/sock');
+c.pipe(doc.createStream()).pipe(c);
10 crdt/203_browser.js
@@ -0,0 +1,10 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+doc.on('row_update', function (row) {
+ // ...
+});
+
+var shoe = require('shoe');
+var c = shoe('/sock');
+c.pipe(doc.createStream()).pipe(c);
10 crdt/204_browser.js
@@ -0,0 +1,10 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+doc.on('row_update', function (row) {
+ if (row.state.type !== 'rank') return;
+});
+
+var shoe = require('shoe');
+var c = shoe('/sock');
+c.pipe(doc.createStream()).pipe(c);
22 crdt/205_browser.js
@@ -0,0 +1,22 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+doc.on('row_update', function (row) {
+ // ...
+
+ var up = document.createElement('input');
+ up.setAttribute('type', 'button');
+ up.value = '+';
+ div.appendChild(up);
+
+ up.addEventListener('click', function () {
+ var ix = seq.indexOf(row);
+ if (ix === 0) return;
+
+ // ...
+ });
+});
+
+var shoe = require('shoe');
+var c = shoe('/sock');
+c.pipe(doc.createStream()).pipe(c);
23 crdt/206_browser.js
@@ -0,0 +1,23 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+doc.on('row_update', function (row) {
+ // ...
+
+ var up = document.createElement('input');
+ up.setAttribute('type', 'button');
+ up.value = '+';
+ div.appendChild(up);
+
+ up.addEventListener('click', function () {
+ var ix = seq.indexOf(row);
+ if (ix === 0) return;
+
+ seq.remove(row);
+ seq.before(row, seq.at(ix - 1));
+ });
+});
+
+var shoe = require('shoe');
+var c = shoe('/sock');
+c.pipe(doc.createStream()).pipe(c);
22 crdt/207_browser.js
@@ -0,0 +1,22 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+doc.on('row_update', function (row) {
+ // ...
+
+ var down = document.createElement('input');
+ down.setAttribute('type', 'button');
+ down.value = '-';
+ div.appendChild(down);
+
+ down.addEventListener('click', function () {
+ var ix = seq.indexOf(row);
+ if (ix === seq.length() - 1) return;
+
+ // ...
+ });
+});
+
+var shoe = require('shoe');
+var c = shoe('/sock');
+c.pipe(doc.createStream()).pipe(c);
23 crdt/208_browser.js
@@ -0,0 +1,23 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+doc.on('row_update', function (row) {
+ // ...
+
+ var down = document.createElement('input');
+ down.setAttribute('type', 'button');
+ down.value = '-';
+ div.appendChild(down);
+
+ down.addEventListener('click', function () {
+ var ix = seq.indexOf(row);
+ if (ix === seq.length() - 1) return;
+
+ seq.remove(row);
+ seq.after(row, seq.at(ix));
+ });
+});
+
+var shoe = require('shoe');
+var c = shoe('/sock');
+c.pipe(doc.createStream()).pipe(c);
60 crdt/209_browser.js
@@ -0,0 +1,60 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+doc.on('row_update', function (row) {
+ if (row.state.type !== 'rank') return;
+
+ var spans = document.querySelectorAll('.row span');
+
+ [].forEach.call(spans, function (span, ix) {
+ if (span.textContent === row.state.value) {
+ document.body.removeChild(span.parentNode);
+ }
+ });
+
+ var div = document.createElement('div');
+ div.className = 'row';
+
+ var ix = seq.indexOf(row);
+ if (ix === seq.length() - 1) {
+ document.body.appendChild(div);
+ }
+ else {
+ var e = document.querySelectorAll('.row')[ix];
+ document.body.insertBefore(div, e);
+ }
+
+ var span = document.createElement('span');
+ span.textContent = row.state.value;
+ div.appendChild(span);
+
+ var up = document.createElement('input');
+ up.setAttribute('type', 'button');
+ up.value = '+';
+ div.appendChild(up);
+
+ var down = document.createElement('input');
+ down.setAttribute('type', 'button');
+ down.value = '-';
+ div.appendChild(down);
+
+ up.addEventListener('click', function () {
+ var ix = seq.indexOf(row);
+ if (ix === 0) return;
+
+ seq.remove(row);
+ seq.before(row, seq.at(ix - 1));
+ });
+
+ down.addEventListener('click', function () {
+ var ix = seq.indexOf(row);
+ if (ix === seq.length() - 1) return;
+
+ seq.remove(row);
+ seq.after(row, seq.at(ix));
+ });
+});
+
+var shoe = require('shoe');
+var c = shoe('/sock');
+c.pipe(doc.createStream()).pipe(c);
2  crdt/300_seed.js
@@ -0,0 +1,2 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
6 crdt/301_seed.js
@@ -0,0 +1,6 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+var request = require('request');
+var r = request.put('http://localhost:' + process.argv[2] + '/_replicate');
+r.pipe(doc.createStream()).pipe(r);
11 crdt/302_seed.js
@@ -0,0 +1,11 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+var request = require('request');
+var r = request.put('http://localhost:' + process.argv[2] + '/_replicate');
+r.pipe(doc.createStream()).pipe(r);
+
+var shuffle = require('deck').shuffle;
+shuffle('abcdefg'.split('')).forEach(function (letter) {
+ seq.push({ value : letter });
+});
60 crdt/src/browser.js
@@ -0,0 +1,60 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+doc.on('row_update', function (row) {
+ if (row.state.type !== 'rank') return;
+
+ var spans = document.querySelectorAll('.row span');
+
+ [].forEach.call(spans, function (span, ix) {
+ if (span.textContent === row.state.value) {
+ document.body.removeChild(span.parentNode);
+ }
+ });
+
+ var div = document.createElement('div');
+ div.className = 'row';
+
+ var ix = seq.indexOf(row);
+ if (ix === seq.length() - 1) {
+ document.body.appendChild(div);
+ }
+ else {
+ var e = document.querySelectorAll('.row')[ix];
+ document.body.insertBefore(div, e);
+ }
+
+ var span = document.createElement('span');
+ span.textContent = row.state.value;
+ div.appendChild(span);
+
+ var up = document.createElement('input');
+ up.setAttribute('type', 'button');
+ up.value = '+';
+ div.appendChild(up);
+
+ var down = document.createElement('input');
+ down.setAttribute('type', 'button');
+ down.value = '-';
+ div.appendChild(down);
+
+ up.addEventListener('click', function () {
+ var ix = seq.indexOf(row);
+ if (ix === 0) return;
+
+ seq.remove(row);
+ seq.before(row, seq.at(ix - 1));
+ });
+
+ down.addEventListener('click', function () {
+ var ix = seq.indexOf(row);
+ if (ix === seq.length() - 1) return;
+
+ seq.remove(row);
+ seq.after(row, seq.at(ix));
+ });
+});
+
+var shoe = require('shoe');
+var c = shoe('/sock');
+c.pipe(doc.createStream()).pipe(c);
12 crdt/src/seed.js
@@ -0,0 +1,12 @@
+var doc = new(require('crdt').Doc);
+var seq = doc.createSeq('type', 'rank');
+
+var request = require('request');
+var r = request.put('http://localhost:' + process.argv[2] + '/_replicate');
+r.pipe(doc.createStream()).pipe(r);
+
+var shuffle = require('deck').shuffle;
+
+shuffle('abcdefg'.split('')).forEach(function (letter) {
+ seq.push({ value : letter });
+});
24 crdt/src/server.js
@@ -0,0 +1,24 @@
+var doc = new(require('crdt').Doc);
+
+var http = require('http');
+var ecstatic = require('ecstatic')(__dirname + '/static');
+
+var server = http.createServer(function (req, res) {
+ if (req.url === '/_replicate') {
+ req.pipe(doc.createStream()).pipe(res);
+ }
+ else ecstatic(req, res);
+});
+server.listen(Number(process.argv[2]));
+
+var shoe = require('shoe');
+var sock = shoe(function (stream) {
+ stream.pipe(doc.createStream()).pipe(stream);
+});
+sock.install(server, '/sock');
+
+var request = require('request');
+process.argv.slice(3).forEach(function (port) {
+ var s = doc.createStream();
+ s.pipe(request.put('http://localhost:' + port + '/_replicate')).pipe(s);
+});
354 crdt/src/static/bundle.js
354 additions, 0 deletions not shown
18 crdt/src/static/index.html
@@ -0,0 +1,18 @@
+<html>
+<head>
+<style>
+ div.row {
+ width: 200px;
+ margin: auto;
+ margin-bottom: 1em;
+ font-size: 2em;
+ }
+ div.row span {
+ padding-right: 1em;
+ }
+</style>
+</head>
+<body>
+<script src="bundle.js"></script>
+</body>
+</html>
5 dnode/000_title.js
@@ -0,0 +1,5 @@
+/*
+
+ previously at realtime conf
+
+*/
5 dnode/001_title.js
@@ -0,0 +1,5 @@
+/*
+
+ previously (2011) at realtime conf
+
+*/
5 dnode/002_title.js
@@ -0,0 +1,5 @@
+/*
+
+ previously (2011) at (keepin' it)-realtime conf
+
+*/
5 dnode/003_title.js
@@ -0,0 +1,5 @@
+/*
+
+ dnode
+
+*/
5 dnode/004_title.js
@@ -0,0 +1,5 @@
+/*
+
+ dnode - turtles all the way down rpc
+
+*/
5 dnode/005_server.js
@@ -0,0 +1,5 @@
+var net = require('net');
+var server = net.createServer(function (stream) {
+
+});
+server.listen(8000);
11 dnode/010_server.js
@@ -0,0 +1,11 @@
+var net = require('net');
+var dnode = require('dnode');
+
+var server = net.createServer(function (stream) {
+ var d = dnode({
+ louder : function (s, cb) {
+
+ }
+ });
+});
+server.listen(8000);
11 dnode/020_server.js
@@ -0,0 +1,11 @@
+var net = require('net');
+var dnode = require('dnode');
+
+var server = net.createServer(function (stream) {
+ var d = dnode({
+ louder : function (s, cb) {
+ cb(s.toUpperCase())
+ }
+ });
+});
+server.listen(8000);
12 dnode/030_server.js
@@ -0,0 +1,12 @@
+var net = require('net');
+var dnode = require('dnode');
+
+var server = net.createServer(function (stream) {
+ var d = dnode({
+ louder : function (s, cb) {
+ cb(s.toUpperCase())
+ }
+ });
+ d.pipe(stream).pipe(d);
+});
+server.listen(8000);
4 dnode/040_client.js
@@ -0,0 +1,4 @@
+var net = require('net');
+var dnode = require('dnode');
+
+var stream = net.connect(8000);
8 dnode/050_client.js
@@ -0,0 +1,8 @@
+var net = require('net');
+var dnode = require('dnode');
+
+var stream = net.connect(8000);
+var d = dnode();
+d.on('remote', function (remote) {
+ // ...
+});
10 dnode/060_client.js
@@ -0,0 +1,10 @@
+var net = require('net');
+var dnode = require('dnode');
+
+var stream = net.connect(8000);
+var d = dnode();
+d.on('remote', function (remote) {
+ remote.louder('beep boop', function (s) {
+ console.log(s);
+ });
+});
12 dnode/070_client.js
@@ -0,0 +1,12 @@
+//!run1.sh
+var net = require('net');
+var dnode = require('dnode');
+
+var stream = net.connect(8000);
+var d = dnode();
+d.on('remote', function (remote) {
+ remote.louder('beep boop', function (s) {
+ console.log(s);
+ });
+});
+d.pipe(stream).pipe(d);
7 dnode/run1.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+node 030_server.js &
+echo 'node 030_server.js &'
+sleep 0.5
+
+echo 'node 070_client.js'
+node 070_client.js
2  mux-demux/00.js
@@ -0,0 +1,2 @@
+var MuxDemux = require('mux-demux');
+var md = new MuxDemux;
5 mux-demux/01.js
@@ -0,0 +1,5 @@
+var MuxDemux = require('mux-demux');
+var md = new MuxDemux;
+
+var a = md.createWriteStream();
+var b = md.createWriteStream();
6 mux-demux/02.js
@@ -0,0 +1,6 @@
+var MuxDemux = require('mux-demux');
+var md = new MuxDemux;
+md.pipe(process.stdout);
+
+var a = md.createWriteStream();
+var b = md.createWriteStream();
10 mux-demux/03.js
@@ -0,0 +1,10 @@
+var MuxDemux = require('mux-demux');
+var md = new MuxDemux;
+md.pipe(process.stdout);
+
+var a = md.createWriteStream();
+var b = md.createWriteStream();
+
+var fs = require('fs');
+fs.createReadStream('/usr/share/dict/words', { encoding : 'utf8' }).pipe(a);
+fs.createReadStream('/etc/passwd', { encoding : 'utf8' }).pipe(b);
16 mux-demux/04.js
@@ -0,0 +1,16 @@
+var MuxDemux = require('mux-demux');
+var net = require('net');
+
+var server = net.createServer(function (stream) {
+ var md = new MuxDemux;
+ md.pipe(process.stdout);
+
+ var a = md.createWriteStream();
+ var b = md.createWriteStream();
+
+ var fs = require('fs');
+ fs.createReadStream('/usr/share/dict/words', { encoding : 'utf8' }).pipe(a);
+ fs.createReadStream('/etc/passwd', { encoding : 'utf8' }).pipe(b);
+});
+
+server.listen(8000);
21 mux-demux/05.js
@@ -0,0 +1,21 @@
+var MuxDemux = require('mux-demux');
+var net = require('net');
+
+var server = net.createServer(function (stream) {
+ var md = new MuxDemux;
+ md.pipe(stream).pipe(md);
+
+ var a = md.createWriteStream();
+ var b = md.createWriteStream();
+
+ var fs = require('fs');
+ fs.createReadStream('/usr/share/dict/words', { encoding : 'utf8' }).pipe(a);
+ fs.createReadStream('/etc/passwd', { encoding : 'utf8' }).pipe(b);
+});
+
+server.on('listening', function () {
+ var stream = net.connect(8000);
+
+});
+
+server.listen(8000);
26 mux-demux/06.js
@@ -0,0 +1,26 @@
+var MuxDemux = require('mux-demux');
+var net = require('net');
+
+var server = net.createServer(function (stream) {
+ var md = new MuxDemux;
+ md.pipe(stream).pipe(md);
+
+ var a = md.createWriteStream();
+ var b = md.createWriteStream();
+
+ var fs = require('fs');
+ fs.createReadStream('/usr/share/dict/words', { encoding : 'utf8' }).pipe(a);
+ fs.createReadStream('/etc/passwd', { encoding : 'utf8' }).pipe(b);
+});
+
+server.on('listening', function () {
+ var stream = net.connect(8000);
+ var md = new MuxDemux;
+
+ md.on('connection', function (s) {
+ console.dir(s);
+ });
+ md.pipe(stream).pipe(md);
+});
+
+server.listen(8000);
26 mux-demux/07.js
@@ -0,0 +1,26 @@
+var MuxDemux = require('mux-demux');
+var net = require('net');
+
+var server = net.createServer(function (stream) {
+ var md = new MuxDemux;
+ md.pipe(stream).pipe(md);
+
+ var a = md.createWriteStream('a');
+ var b = md.createWriteStream('b');
+
+ var fs = require('fs');
+ fs.createReadStream('/usr/share/dict/words', { encoding : 'utf8' }).pipe(a);
+ fs.createReadStream('/etc/passwd', { encoding : 'utf8' }).pipe(b);
+});
+
+server.on('listening', function () {
+ var stream = net.connect(8000);
+ var md = new MuxDemux;
+
+ md.on('connection', function (s) {
+ console.dir(s.meta);
+ });
+ md.pipe(stream).pipe(md);
+});
+
+server.listen(8000);
33 mux-demux/08.js
@@ -0,0 +1,33 @@
+var MuxDemux = require('mux-demux');
+var net = require('net');
+
+var server = net.createServer(function (stream) {
+ var md = new MuxDemux;
+ md.pipe(stream).pipe(md);
+
+ var a = md.createWriteStream('a');
+ var b = md.createWriteStream('b');
+
+ var fs = require('fs');
+ fs.createReadStream('/usr/share/dict/words', { encoding : 'utf8' }).pipe(a);
+ fs.createReadStream('/etc/passwd', { encoding : 'utf8' }).pipe(b);
+});
+
+server.on('listening', function () {
+ var stream = net.connect(8000);
+ var md = new MuxDemux;
+
+ md.on('connection', function (s) {
+ var size = 0;
+ s.on('data', function (buf) {
+ size += buf.length;
+ });
+
+ s.on('end', function () {
+ console.log(s.meta + ': ' + size + ' bytes');
+ });
+ });
+ md.pipe(stream).pipe(md);
+});
+
+server.listen(8000);
36 mux-demux/09.js
@@ -0,0 +1,36 @@
+var MuxDemux = require('mux-demux');
+var net = require('net');
+var through = require('through');
+
+var server = net.createServer(function (stream) {
+ var md = new MuxDemux;
+ md.pipe(stream).pipe(md);
+
+ var a = md.createWriteStream('a');
+ var b = md.createWriteStream('b');
+
+ var fs = require('fs');
+ fs.createReadStream('/usr/share/dict/words', { encoding : 'utf8' }).pipe(a);
+ fs.createReadStream('/etc/passwd', { encoding : 'utf8' }).pipe(b);
+});
+
+server.on('listening', function () {
+ var stream = net.connect(8000);
+ var md = new MuxDemux;
+
+ md.on('connection', function (s) {
+ var size = 0;
+ function write (buf) {
+ size += buf.length;
+ }
+ function end () {
+ this.emit('data', s.meta + ': ' + size + ' bytes\n');
+ }
+ s.pipe(through(write, end))
+ .pipe(md.createWriteStream('sum'))
+ ;
+ });
+ md.pipe(stream).pipe(md);
+});
+
+server.listen(8000);
40 mux-demux/10.js
@@ -0,0 +1,40 @@
+var MuxDemux = require('mux-demux');
+var net = require('net');
+var through = require('through');
+
+var server = net.createServer(function (stream) {
+ var md = new MuxDemux;
+ md.on('connection', function (s) {
+ s.pipe(process.stdout);
+ });
+
+ md.pipe(stream).pipe(md);
+
+ var a = md.createWriteStream('a');
+ var b = md.createWriteStream('b');
+
+ var fs = require('fs');
+ fs.createReadStream('/usr/share/dict/words', { encoding : 'utf8' }).pipe(a);
+ fs.createReadStream('/etc/passwd', { encoding : 'utf8' }).pipe(b);
+});
+
+server.on('listening', function () {
+ var stream = net.connect(8000);
+ var md = new MuxDemux;
+
+ md.on('connection', function (s) {
+ var size = 0;
+ function write (buf) {
+ size += buf.length;
+ }
+ function end () {
+ this.emit('data', s.meta + ': ' + size + ' bytes\n');
+ }
+ s.pipe(through(write, end))
+ .pipe(md.createWriteStream(s.meta + '.sum'))
+ ;
+ });
+ md.pipe(stream).pipe(md);
+});
+
+server.listen(8000);
23 package.json
@@ -0,0 +1,23 @@
+{
+ "name": "realtimeconf-2012",
+ "version": "0.0.0",
+ "dependencies": {
+ "browserify": "~1.16.1",
+ "crdt": "~3.1.0",
+ "deck": "~0.0.4",
+ "dnode": "~1.0.1",
+ "ecstatic": "~0.1.6",
+ "emit-stream": "~0.1.0",
+ "event-stream": "~0.7.0",
+ "falafel": "~0.1.3",
+ "inherits": "~1.0.0",
+ "JSONStream": "~0.2.3",
+ "mkdirp": "~0.3.4",
+ "mux-demux": "~3.3.2",
+ "request": "~2.11.4",
+ "scuttlebutt": "~3.2.4",
+ "seaport": "~0.8.1",
+ "shoe": "~0.0.7",
+ "through": "~0.1.4"
+ }
+}
7 roll_your_own_socketio/000_title.js
@@ -0,0 +1,7 @@
+/*
+
+ roll your own socket.io
+
+ using shoe and emit-stream
+
+*/
1  roll_your_own_socketio/001_server.js
@@ -0,0 +1 @@
+var http = require('http');
4 roll_your_own_socketio/004_server.js
@@ -0,0 +1,4 @@
+var http = require('http');
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
6 roll_your_own_socketio/010_server.js
@@ -0,0 +1,6 @@
+var http = require('http');
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+var shoe = require('shoe');
12 roll_your_own_socketio/020_server.js
@@ -0,0 +1,12 @@
+var http = require('http');
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+var shoe = require('shoe');
+
+var sock = shoe(function (stream) {
+ // ...
+});
+
+sock.install(server, '/sock');
15 roll_your_own_socketio/030_server.js
@@ -0,0 +1,15 @@
+var http = require('http');
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+var shoe = require('shoe');
+
+var sock = shoe(function (stream) {
+ var i = 0;
+ setInterval(function () {
+ stream.write(String(i++ % 2));
+ }, 100);
+});
+
+sock.install(server, '/sock');
1  roll_your_own_socketio/040_browser.js
@@ -0,0 +1 @@
+var shoe = require('shoe');
2  roll_your_own_socketio/050_browser.js
@@ -0,0 +1,2 @@
+var shoe = require('shoe');
+var stream = shoe('/sock');
7 roll_your_own_socketio/060_browser.js
@@ -0,0 +1,7 @@
+//!run1.sh
+var shoe = require('shoe');
+var stream = shoe('/sock');
+
+stream.on('data', function (buf) {
+ document.body.textContent += buf;
+});
16 roll_your_own_socketio/070_server.js
@@ -0,0 +1,16 @@
+var http = require('http');
+var EventEmitter = require('events').EventEmitter;
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+var shoe = require('shoe');
+
+var sock = shoe(function (stream) {
+ var i = 0;
+ setInterval(function () {
+ stream.write(String(i++ % 2));
+ }, 100);
+});
+
+sock.install(server, '/sock');
18 roll_your_own_socketio/080_server.js
@@ -0,0 +1,18 @@
+var http = require('http');
+var EventEmitter = require('events').EventEmitter;
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+var shoe = require('shoe');
+
+var sock = shoe(function (stream) {
+ var ev = new EventEmitter;
+
+ var i = 0;
+ setInterval(function () {
+ stream.write(String(i++ % 2));
+ }, 100);
+});
+
+sock.install(server, '/sock');
18 roll_your_own_socketio/090_server.js
@@ -0,0 +1,18 @@
+var http = require('http');
+var EventEmitter = require('events').EventEmitter;
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+var shoe = require('shoe');
+
+var sock = shoe(function (stream) {
+ var ev = new EventEmitter;
+
+ var i = 0;
+ setInterval(function () {
+ ev.emit('upper', i++ % 2);
+ }, 100);
+});
+
+sock.install(server, '/sock');
20 roll_your_own_socketio/100_server.js
@@ -0,0 +1,20 @@
+var http = require('http');
+var EventEmitter = require('events').EventEmitter;
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+var shoe = require('shoe');
+var emitStream = require('emit-stream');
+var JSONStream = require('JSONStream');
+
+var sock = shoe(function (stream) {
+ var ev = new EventEmitter;
+
+ var i = 0;
+ setInterval(function () {
+ ev.emit('upper', i++ % 2);
+ }, 100);
+});
+
+sock.install(server, '/sock');
22 roll_your_own_socketio/110_server.js
@@ -0,0 +1,22 @@
+var http = require('http');
+var EventEmitter = require('events').EventEmitter;
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+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())
+
+ var i = 0;
+ setInterval(function () {
+ ev.emit('upper', i++ % 2);
+ }, 100);
+});
+
+sock.install(server, '/sock');
24 roll_your_own_socketio/120_server.js
@@ -0,0 +1,24 @@
+var http = require('http');
+var EventEmitter = require('events').EventEmitter;
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+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 i = 0;
+ setInterval(function () {
+ ev.emit('upper', i++ % 2);
+ }, 100);
+});
+
+sock.install(server, '/sock');
33 roll_your_own_socketio/130_server.js
@@ -0,0 +1,33 @@
+var http = require('http');
+var EventEmitter = require('events').EventEmitter;
+
+var server = http.createServer(require('ecstatic')(__dirname + '/static'));
+server.listen(8005);
+
+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');
7 roll_your_own_socketio/140_browser.js
@@ -0,0 +1,7 @@
+var shoe = require('shoe');
+
+var stream = shoe('/sock');
+
+stream.on('data', function (buf) {
+ document.body.textContent += buf;
+});
9 roll_your_own_socketio/150_browser.js
@@ -0,0 +1,9 @@
+var shoe = require('shoe');
+var emitStream = require('emit-stream');
+var JSONStream = require('JSONStream');
+
+var stream = shoe('/sock');
+
+stream.on('data', function (buf) {
+ document.body.textContent += buf;
+});
10 roll_your_own_socketio/160_browser.js
@@ -0,0 +1,10 @@
+var shoe = require('shoe');
+var emitStream = require('emit-stream');
+var JSONStream = require('JSONStream');
+
+var parser = JSONStream.parse([true]);
+var stream = shoe('/sock');
+
+stream.on('data', function (buf) {
+ document.body.textContent += buf;
+});
10 roll_your_own_socketio/170_browser.js
@@ -0,0 +1,10 @@
+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);
+
+stream.on('data', function (buf) {
+ document.body.textContent += buf;
+});
15 roll_your_own_socketio/180_browser.js
@@ -0,0 +1,15 @@
+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) {
+ // ...
+});
+
+ev.on('upper', function (msg) {
+ // ...
+});
20 roll_your_own_socketio/190_browser.js
@@ -0,0 +1,20 @@
+//!run2.sh
+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);
+});
7 roll_your_own_socketio/run1.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+echo '#' browserify 060_browser.js -o static/bundle.js
+browserify 060_browser.js -o static/bundle.js
+
+echo http://localhost:8005
+node 030_server.js
7 roll_your_own_socketio/run2.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+echo '#' browserify 190_browser.js -o static/bundle.js
+browserify 190_browser.js -o static/bundle.js
+
+echo http://localhost:8005
+node 130_server.js
4,152 roll_your_own_socketio/static/bundle.js
4,152 additions, 0 deletions not shown
5 roll_your_own_socketio/static/index.html
@@ -0,0 +1,5 @@
+<html>
+<body>
+<script src="bundle.js"></script>
+</body>
+</html>
7 scuttlebutt/000.js
@@ -0,0 +1,7 @@
+/*
+
+ peer to peer state replication
+
+ with scuttlebutt
+
+*/
3  scuttlebutt/005.js
@@ -0,0 +1,3 @@
+var Model = require('scuttlebutt/model');
+var am = new Model;
+var bm = new Model;
6 scuttlebutt/010.js
@@ -0,0 +1,6 @@
+var Model = require('scuttlebutt/model');
+var am = new Model;
+var bm = new Model;
+
+var as = am.createStream();
+var bs = bm.createStream();
8 scuttlebutt/020.js
@@ -0,0 +1,8 @@
+var Model = require('scuttlebutt/model');
+var am = new Model;
+var bm = new Model;
+
+var as = am.createStream();
+var bs = bm.createStream();
+
+as.pipe(bs).pipe(as);
12 scuttlebutt/030.js
@@ -0,0 +1,12 @@
+var Model = require('scuttlebutt/model');
+var am = new Model;
+var bm = new Model;
+
+var as = am.createStream();
+var bs = bm.createStream();
+
+as.pipe(bs).pipe(as);
+
+bm.on('update', function (key, value, source) {
+ console.log(key + ' => ' + value + ' from ' + source);
+});
15 scuttlebutt/040.js
@@ -0,0 +1,15 @@
+//!run1.sh
+var Model = require('scuttlebutt/model');
+var am = new Model;
+var bm = new Model;
+
+var as = am.createStream();
+var bs = bm.createStream();
+
+as.pipe(bs).pipe(as);
+
+bm.on('update', function (key, value, source) {
+ console.log(key + ' => ' + value + ' from ' + source);
+});
+
+am.set('beep', 789);
9 scuttlebutt/050.js
@@ -0,0 +1,9 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var http = require('http');
+var server = http.createServer(function (req, res) {
+ // ...
+});
+
+server.listen(Number(process.argv[2]));
11 scuttlebutt/060.js
@@ -0,0 +1,11 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var http = require('http');
+var server = http.createServer(function (req, res) {
+ var key = req.url.split('=')[0];
+ var value = req.url.split('=')[1];
+
+});
+
+server.listen(Number(process.argv[2]));
14 scuttlebutt/070.js
@@ -0,0 +1,14 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var http = require('http');
+var server = http.createServer(function (req, res) {
+ var key = req.url.split('=')[0];
+ var value = req.url.split('=')[1];
+
+ if (value === undefined) {
+ res.end(model.get(key) + '\n');
+ }
+});
+
+server.listen(Number(process.argv[2]));
18 scuttlebutt/080.js
@@ -0,0 +1,18 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var http = require('http');
+var server = http.createServer(function (req, res) {
+ var key = req.url.split('=')[0];
+ var value = req.url.split('=')[1];
+
+ if (value === undefined) {
+ res.end(model.get(key) + '\n');
+ }
+ else {
+ model.set(key, value);
+ res.end(key + '=' + value + '\n');
+ }
+});
+
+server.listen(Number(process.argv[2]));
23 scuttlebutt/090.js
@@ -0,0 +1,23 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var http = require('http');
+var server = http.createServer(function (req, res) {
+ if (req.url === '/_replicate') {
+ req.pipe(model.createStream()).pipe(res);
+ return;
+ }
+
+ var key = req.url.split('=')[0];
+ var value = req.url.split('=')[1];
+
+ if (value === undefined) {
+ res.end(model.get(key) + '\n');
+ }
+ else {
+ model.set(key, value);
+ res.end(key + '=' + value + '\n');
+ }
+});
+
+server.listen(Number(process.argv[2]));
31 scuttlebutt/100.js
@@ -0,0 +1,31 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var request = require('request');
+process.argv.slice(3).map(Number).forEach(function (port) {
+ var r = request.put('http://localhost:' + port + '/_replicate');
+ r.pipe(model.createStream()).pipe(r);
+});
+
+// ./a.s
+
+var http = require('http');
+var server = http.createServer(function (req, res) {
+ if (req.url === '/_replicate') {
+ req.pipe(model.createStream()).pipe(res);
+ return;
+ }
+
+ var key = req.url.split('=')[0];
+ var value = req.url.split('=')[1];
+
+ if (value === undefined) {
+ res.end(model.get(key) + '\n');
+ }
+ else {
+ model.set(key, value);
+ res.end(key + '=' + value + '\n');
+ }
+});
+
+server.listen(Number(process.argv[2]));
18 scuttlebutt/110.js
@@ -0,0 +1,18 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var sock = shoe(function (stream) {
+ // ...
+});
+
+var http = require('http');
+var server = http.createServer(/* ... */);
+
+server.listen(Number(process.argv[2]));
+
+var request = require('request');
+process.argv.slice(3).map(Number).forEach(function (port) {
+ var r = request.put('http://localhost:' + port + '/_replicate');
+ r.pipe(model.createStream()).pipe(r);
+});
18 scuttlebutt/120.js
@@ -0,0 +1,18 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var sock = shoe(function (stream) {
+ stream.pipe(model.createStream()).pipe(stream);
+});
+
+var http = require('http');
+var server = http.createServer(/* ... */);
+
+server.listen(Number(process.argv[2]));
+
+var request = require('request');
+process.argv.slice(3).map(Number).forEach(function (port) {
+ var r = request.put('http://localhost:' + port + '/_replicate');
+ r.pipe(model.createStream()).pipe(r);
+});
19 scuttlebutt/130.js
@@ -0,0 +1,19 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var sock = shoe(function (stream) {
+ stream.pipe(model.createStream()).pipe(stream);
+});
+
+var http = require('http');
+var server = http.createServer(/* ... */);
+sock.install(server, '/sock');
+
+server.listen(Number(process.argv[2]));
+
+var request = require('request');
+process.argv.slice(3).map(Number).forEach(function (port) {
+ var r = request.put('http://localhost:' + port + '/_replicate');
+ r.pipe(model.createStream()).pipe(r);
+});
46 scuttlebutt/140.js
@@ -0,0 +1,46 @@
+//!peer.sh
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var sock = shoe(function (stream) {
+ stream.pipe(model.createStream()).pipe(stream);
+});
+
+var http = require('http');
+var fs = require('fs');
+
+var server = http.createServer(function (req, res) {
+ if (req.url === '/_replicate') {
+ req.pipe(model.createStream()).pipe(res);
+ return;
+ }
+ else if (req.url === '/') {
+ res.setHeader('content-type', 'text/html');
+ return fs.createReadStream(__dirname + '/static/index.html').pipe(res);
+ }
+ else if (req.url === '/bundle.js') {
+ res.setHeader('content-type', 'text/javascript');
+ return fs.createReadStream(__dirname + '/static/bundle.js').pipe(res);
+ }
+
+ var key = req.url.split('=')[0];
+ var value = req.url.split('=')[1];
+
+ if (value === undefined) {
+ res.end(model.get(key) + '\n');
+ }
+ else {
+ model.set(key, value);
+ res.end(key + '=' + value + '\n');
+ }
+});
+sock.install(server, '/sock');
+
+server.listen(Number(process.argv[2]));
+
+var request = require('request');
+process.argv.slice(3).map(Number).forEach(function (port) {
+ var r = request.put('http://localhost:' + port + '/_replicate');
+ r.pipe(model.createStream()).pipe(r);
+});
2  scuttlebutt/150_browser.js
@@ -0,0 +1,2 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
5 scuttlebutt/160_browser.js
@@ -0,0 +1,5 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var stream = shoe('/sock');
7 scuttlebutt/170_browser.js
@@ -0,0 +1,7 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var stream = shoe('/sock');
+
+stream.pipe(model.createStream()).pipe(stream);
11 scuttlebutt/180_browser.js
@@ -0,0 +1,11 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var stream = shoe('/sock');
+
+stream.pipe(model.createStream()).pipe(stream);
+
+model.on('update', function (key, value, source) {
+ // ...
+});
14 scuttlebutt/190_browser.js
@@ -0,0 +1,14 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var stream = shoe('/sock');
+
+stream.pipe(model.createStream()).pipe(stream);
+
+var keys = {};
+model.on('update', function (key, value, source) {
+ if (!keys[key]) {
+ // ...
+ }
+});
21 scuttlebutt/200_browser.js
@@ -0,0 +1,21 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var stream = shoe('/sock');
+
+stream.pipe(model.createStream()).pipe(stream);
+
+var keys = {};
+model.on('update', function (key, value, source) {
+ if (!keys[key]) {
+ var div = document.createElement('div');
+ document.body.appendChild(div);
+
+ var label = document.createTextNode(key);
+ div.appendChild(label);
+
+ keys[key] = document.createElement('input');
+ div.appendChild(keys[key]);
+ }
+});
24 scuttlebutt/210_browser.js
@@ -0,0 +1,24 @@
+//!run2.sh
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var stream = shoe('/sock');
+
+stream.pipe(model.createStream()).pipe(stream);
+
+var keys = {};
+model.on('update', function (key, value, source) {
+ if (!keys[key]) {
+ var div = document.createElement('div');
+ document.body.appendChild(div);
+
+ var label = document.createTextNode(key);
+ div.appendChild(label);
+
+ keys[key] = document.createElement('input');
+ div.appendChild(keys[key]);
+ }
+
+ keys[key].value = value;
+});
25 scuttlebutt/220_browser.js
@@ -0,0 +1,25 @@
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var stream = shoe('/sock');
+
+stream.pipe(model.createStream()).pipe(stream);
+
+var keys = {};
+model.on('update', function (key, value, source) {
+ if (!keys[key]) {
+ var div = document.createElement('div');
+ document.body.appendChild(div);
+
+ var label = document.createTextNode(key);
+ div.appendChild(label);
+
+ keys[key] = document.createElement('input');
+ keys[key].addEventListener('keydown', onkeydown);
+
+ div.appendChild(keys[key]);
+ }
+
+ keys[key].value = value;
+});
32 scuttlebutt/230_browser.js
@@ -0,0 +1,32 @@
+//!run3.sh
+var Model = require('scuttlebutt/model');
+var model = new Model;
+
+var shoe = require('shoe');
+var stream = shoe('/sock');
+
+stream.pipe(model.createStream()).pipe(stream);
+
+var keys = {};
+model.on('update', function (key, value, source) {
+ function onkeydown (ev) {
+ if (ev.keyCode === 13) {
+ model.set(key, this.value);
+ }
+ }
+
+ if (!keys[key]) {
+ var div = document.createElement('div');
+ document.body.appendChild(div);
+
+ var label = document.createTextNode(key);
+ div.appendChild(label);
+
+ keys[key] = document.createElement('input');
+ keys[key].addEventListener('keydown', onkeydown);
+
+ div.appendChild(keys[key]);
+ }
+
+ keys[key].value = value;
+});
2  scuttlebutt/peer.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+node 100.js $*
3  scuttlebutt/run1.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+node 040.js
7 scuttlebutt/run2.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+echo '#' browserify 210_browser.js -o static/bundle.js
+browserify 210_browser.js -o static/bundle.js
+
+echo http://localhost:8005
+node 140.js 8005
10 scuttlebutt/run3.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+echo '#' browserify 230_browser.js -o static/bundle.js
+browserify 230_browser.js -o static/bundle.js
+
+echo http://localhost:8005
+echo http://localhost:8006
+
+node 140.js 8005 &
+node 140.js 8006 8005
7,146 scuttlebutt/static/bundle.js
7,146 additions, 0 deletions not shown
5 scuttlebutt/static/index.html
@@ -0,0 +1,5 @@
+<html>
+<body>
+<script src="bundle.js"></script>
+</body>
+</html>
5 seaport/000_title.js
@@ -0,0 +1,5 @@
+/*
+
+ delete all your config files with seaport
+
+*/
12 seaport/001.js
@@ -0,0 +1,12 @@
+/*
+
+ delete all your config files with seaport
+
+ {
+ "auth" : {
+ "host" : "192.168.3.4",
+ "port" : 5555
+ }
+ }
+
+*/
16 seaport/002.js
@@ -0,0 +1,16 @@
+/*
+
+ delete all your config files with seaport
+
+ {
+ "auth" : {
+ "host" : "192.168.3.4",
+ "port" : 5555
+ },
+ "payments" : {
+ "host" : "192.168.2.100",
+ "port" : 5400
+ }
+ }
+
+*/
20 seaport/003.js
@@ -0,0 +1,20 @@
+/*
+
+ delete all your config files with seaport
+
+ {
+ "auth" : {
+ "host" : "192.168.3.4",
+ "port" : 5555
+ },
+ "payments" : {
+ "host" : "192.168.2.100",
+ "port" : 5400
+ },
+ "batch" : {
+ "host" : "192.168.6.50",
+ "port" : 443
+ }
+ }
+
+*/
1  seaport/110_worker.js
@@ -0,0 +1 @@
+var http = require('http');
5 seaport/120_worker.js
@@ -0,0 +1,5 @@
+var http = require('http');
+var server = http.createServer(function (req, res) {
+ // ...
+});
+server.listen(8000);
8 seaport/130_worker.js
@@ -0,0 +1,8 @@
+var http = require('http');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ // ...
+});
+server.listen(8000);
9 seaport/140_worker.js
@@ -0,0 +1,9 @@
+var http = require('http');
+var split = require('event-stream').split;
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n'))
+});
+server.listen(8000);
12 seaport/150_worker.js
@@ -0,0 +1,12 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ // ...
+ }));
+});
+server.listen(8000);
12 seaport/160_worker.js
@@ -0,0 +1,12 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ }));
+});
+server.listen(8000);
13 seaport/170_worker.js
@@ -0,0 +1,13 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ this.emit('data', n + '\n');
+ }));
+});
+server.listen(8000);
13 seaport/180_worker.js
@@ -0,0 +1,13 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ this.emit('data', n + '\n');
+ })).pipe(res);
+});
+server.listen(8000);
16 seaport/190_worker.js
@@ -0,0 +1,16 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ this.emit('data', n + '\n');
+ })).pipe(res);
+});
+
+var seaport = require('seaport');
+
+server.listen(8000);
17 seaport/200_worker.js
@@ -0,0 +1,17 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ this.emit('data', n + '\n');
+ })).pipe(res);
+});
+
+var seaport = require('seaport');
+var ports = seaport.connect(7000);
+
+server.listen(8000);
19 seaport/210_worker.js
@@ -0,0 +1,19 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ this.emit('data', n + '\n');
+ })).pipe(res);
+});
+
+var seaport = require('seaport');
+var ports = seaport.connect(7000);
+
+ports.service('business@0.5.2', function (port, ready) {
+ // ...
+});
19 seaport/220_worker.js
@@ -0,0 +1,19 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ this.emit('data', n + '\n');
+ })).pipe(res);
+});
+
+var seaport = require('seaport');
+var ports = seaport.connect(7000);
+
+ports.service('business@0.5.2', function (port, ready) {
+ server.listen(port, ready);
+});
17 seaport/230_worker.js
@@ -0,0 +1,17 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ this.emit('data', n + '\n');
+ })).pipe(res);
+});
+
+var seaport = require('seaport');
+var ports = seaport.connect(7000);
+
+ports.service('business@0.5.2', server.listen.bind(server));
19 seaport/240_worker.js
@@ -0,0 +1,19 @@
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ this.emit('data', n + '\n');
+ })).pipe(res);
+});
+
+var seaport = require('seaport');
+var ports = seaport.connect(7000);
+
+ports.service('business@0.5.2', {
+ enterprise : true
+}, server.listen.bind(server));
21 seaport/250_worker.js
@@ -0,0 +1,21 @@
+//!run1.sh
+var http = require('http');
+var split = require('event-stream').split;
+var through = require('through');
+
+var server = http.createServer(function (req, res) {
+ if (req.method !== 'POST') res.end('serious business logic\n');
+
+ req.pipe(split('\n')).pipe(through(function (line) {
+ var n = (Math.pow(1.102, Number(line)) + 8.92) * 1.05;
+ this.emit('data', n + '\n');
+ })).pipe(res);
+});
+
+var seaport = require('seaport');
+var ports = seaport.connect(7000);
+
+ports.service('business@0.5.2', {
+ enterprise : true,
+ webscale : 38
+}, server.listen.bind(server));
1  seaport/300_web.js
@@ -0,0 +1 @@
+var seaport = require('seaport');
2  seaport/310_web.js
@@ -0,0 +1,2 @@
+var seaport = require('seaport');
+var ports = seaport.connect(7000);
5 seaport/320_web.js