Skip to content

Instantly share code, notes, and snippets.

@bsatrom
Created November 8, 2011 21:56

Revisions

  1. Brandon Satrom revised this gist Nov 8, 2011. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions spec.coffee
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,9 @@ hook = require '../js/hook_stdout'

    describe 'chat server', ->

    it 'should close the server after the server has started', ->
    server.listen '3001'
    unhook = hook.setup((string, encoding, fd) ->
    expect(string).toContain 'Server connection closed'
    )
    server.close()
    unhook()
    it 'should listen at localhost on a port I specify', ->
    unhook = hook.setup((string, encoding, fd) ->
    expect(string).toContain 'Server listening at http://127.0.0.1:3001/'
    )
    server.listen '3001'
    unhook()
  2. Brandon Satrom revised this gist Nov 8, 2011. 1 changed file with 9 additions and 7 deletions.
    16 changes: 9 additions & 7 deletions spec.coffee
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,12 @@
    server = require '../js/server'
    hook = require '../js/hook_stdout'

    it 'should close the server after the server has started', ->
    server.listen '3001'
    unhook = hook.setup((string, encoding, fd) ->
    expect(string).toContain 'Server connection closed'
    )
    server.close()
    unhook()
    describe 'chat server', ->

    it 'should close the server after the server has started', ->
    server.listen '3001'
    unhook = hook.setup((string, encoding, fd) ->
    expect(string).toContain 'Server connection closed'
    )
    server.close()
    unhook()
  3. Brandon Satrom revised this gist Nov 8, 2011. 1 changed file with 14 additions and 15 deletions.
    29 changes: 14 additions & 15 deletions hook_stdout.js
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,16 @@
    (function() {
    var exports;
    exports = module.exports;
    exports.setup = function(callback) {
    var write;
    write = process.stdout.write;
    process.stdout.write = (function(stub) {
    return function(string, encoding, fd) {
    stub.apply(process.stdout, arguments);
    return callback(string, encoding, fd);
    };
    })(process.stdout.write);
    return function() {
    return process.stdout.write = write;
    var exports = module.exports;

    exports.setup = function(callback) {
    var write = process.stdout.write;

    process.stdout.write = (function(stub) {
    function(string, encoding, fd) {
    stub.apply(process.stdout, arguments);
    callback(string, encoding, fd);
    };
    })(process.stdout.write);

    return function() {
    process.stdout.write = write;
    };
    }).call(this);
    };
  4. Brandon Satrom created this gist Nov 8, 2011.
    11 changes: 11 additions & 0 deletions hook_stdout.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    exports = module.exports

    exports.setup = (callback) ->
    write = process.stdout.write

    process.stdout.write = ((stub) ->
    (string, encoding, fd) ->
    stub.apply process.stdout, arguments
    callback string, encoding, fd)(process.stdout.write)

    return -> process.stdout.write = write
    17 changes: 17 additions & 0 deletions hook_stdout.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    (function() {
    var exports;
    exports = module.exports;
    exports.setup = function(callback) {
    var write;
    write = process.stdout.write;
    process.stdout.write = (function(stub) {
    return function(string, encoding, fd) {
    stub.apply(process.stdout, arguments);
    return callback(string, encoding, fd);
    };
    })(process.stdout.write);
    return function() {
    return process.stdout.write = write;
    };
    };
    }).call(this);
    10 changes: 10 additions & 0 deletions server.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    exports = module.exports

    # server logic omitted for brevity

    exports.listen = (port, host, callback) ->
    try
    server.listen port, host, callback
    util.log "Server listening at http://#{host or "127.0.0.1"}:#{port}/"
    catch err
    util.log err.message
    11 changes: 11 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    var exports;
    exports = module.exports;

    exports.listen = function(port, host, callback) {
    try {
    server.listen(port, host, callback);
    return util.log("Server listening at http://" + (host || "127.0.0.1") + ":" + port + "/");
    } catch (err) {
    return util.log(err.message);
    }
    };
    10 changes: 10 additions & 0 deletions spec.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    server = require '../js/server'
    hook = require '../js/hook_stdout'

    it 'should close the server after the server has started', ->
    server.listen '3001'
    unhook = hook.setup((string, encoding, fd) ->
    expect(string).toContain 'Server connection closed'
    )
    server.close()
    unhook()