Welcome, guest | Sign In | My Account | Store | Cart
#!/usr/bin/env node
//
// nicerest -- pipe your REST API `curl` calls through this for nicer output
//

var stdin = process.openStdin();
var EventEmitter = require('events').EventEmitter;

var buffer = "";

stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
    buffer += chunk;
});

stdin.on('end', function () {
    if (buffer.slice(0,5) === "HTTP/") {
        var index = buffer.indexOf('\r\n\r\n');
        var sepLen = 4;
        if (index == -1) {
            index = buffer.indexOf('\n\n');
            sepLen = 2;
        }
        if (index != -1) {
            process.stdout.write(buffer.slice(0, index+sepLen));
            buffer = buffer.slice(index+sepLen);
        }
    }
    if (buffer[0] === '{' || buffer[0] === '[') {
        try {
            process.stdout.write(JSON.stringify(JSON.parse(buffer), null, 4));
            process.stdout.write('\n');
        } catch(ex) {
            process.stdout.write(buffer);
            if (buffer[buffer.length-1] !== "\n") {
                process.stdout.write('\n');
            }
        }
    } else {
        process.stdout.write(buffer);
        if (buffer[buffer.length-1] !== "\n") {
            process.stdout.write('\n');
        }
    }
});

Diff to Previous Revision

--- revision 1 2011-01-18 22:54:42
+++ revision 2 2011-01-18 22:57:17
@@ -27,8 +27,15 @@
         }
     }
     if (buffer[0] === '{' || buffer[0] === '[') {
-        process.stdout.write(JSON.stringify(JSON.parse(buffer), null, 2));
-        process.stdout.write('\n');
+        try {
+            process.stdout.write(JSON.stringify(JSON.parse(buffer), null, 4));
+            process.stdout.write('\n');
+        } catch(ex) {
+            process.stdout.write(buffer);
+            if (buffer[buffer.length-1] !== "\n") {
+                process.stdout.write('\n');
+            }
+        }
     } else {
         process.stdout.write(buffer);
         if (buffer[buffer.length-1] !== "\n") {

History