Skip to content

Instantly share code, notes, and snippets.

View watson's full-sized avatar

Thomas Watson watson

View GitHub Profile

This sets up two HTTP servers on port 3000 and 3001. The one on port 3000 works as expected, the one on port 3001 doesn't.

Console log output when requesting server on port 3000:

------- NEW WORKING REQUEST -------
New listener added: end
adding 1st data litener
New listener added: data
New listener added: data
New listener added: readable
var stream = require('stream')
var rs = new stream.Readable()
rs.push('hello')
rs.push('world')
rs._read = function() {}
var sameTick = true
rs.on('data', function() {
@watson
watson / index.js
Created June 4, 2014 10:53
Trying to auto-resume a paused stream by piping (not working)
var http = require('http');
http.createServer(function (req, res) {
console.log('Got request');
req.pause();
var size = 0;
req.on('data', function (chunk) {
console.log('Counting...');
size += chunk.length;
@watson
watson / halt.js
Created August 8, 2014 16:43
Node.js 0.10.30 bug :(
// This will stop working after a few iterations on Node 0.10.30
// For details see: https://github.com/joyent/node/issues/8065
setInterval(function () {
console.log("I'm alive!");
}, 42.1);
@watson
watson / parallel.js
Created August 16, 2014 21:11
Thoughts on a new parallel callback API module
var foo = require('foo');
var next = foo(function (err, results) {
err // => the first error thrown
results // => array of all the results
});
doSomething(next());
doSomethingElse(next());
anotherThing(next());
@watson
watson / foo.js
Created October 13, 2014 16:21
JavaScript Array filtering madness
// the data
var arr = ['foo', 'bar', 'baz'];
// the formal way
arr.filter(function (elm) {
return /a/.test(elm);
});
// the one-liner
arr.filter(RegExp.prototype.test.bind(/a/));
@watson
watson / isArray.js
Created December 29, 2014 12:06
Node.js util.isArray implementation
function isArray(ar) {
return Array.isArray(ar) ||
(typeof ar === 'object' && objectToString(ar) === '[object Array]');
}
@watson
watson / console
Created June 18, 2015 17:01
standard fails to fail
~stackman% standard --version
4.3.1
~stackman% standard
~stackman% mkdir /tmp/stackman
~stackman% cp -pr . /tmp/stackman
~stackman% cd /tmp/stackman
~stackman% standard
standard: Use JavaScript Standard Style (https://github.com/feross/standard)
/private/tmp/stackman/err1.js:1:13: Extra semicolon.
/private/tmp/stackman/err1.js:3:29: Extra semicolon.
@watson
watson / server.js
Last active August 29, 2015 14:25
Example using the async-state module
var http = require('http')
var asyncState = require('async-state')
process.on('uncaughtException', function (err) {
if (asyncState.req) {
console.log('An error occurred while processing request for', asyncState.req.url)
} else {
console.log('An error occurred outside of an HTTP request')
}
})
@watson
watson / README.md
Last active August 29, 2015 14:25
A stash of useful unix commands that are too hard to remember, but too useful to forget

Find all files excluding a single directory:

find . -path ./exclude-this -prune -o -type f -name '*.js' -print

Notes:

  • The -path ./exclude-this -prune -o part ensures that the path ./exclude-this isn't included
  • The -print part ensures that the base directory of the excluded path isn't outputtet (this isn't needed if combining with -exec)