public
Created

  • Download Gist
stdout.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
Index: /Users/trentm/tm/json/lib/jsontool.js
index 734729c..d2764f9 100755
--- a/lib/jsontool.js
+++ b/lib/jsontool.js
@@ -399,36 +399,140 @@ function parseArgv(argv) {
* Get input from either given file paths or stdin.
*
* @param opts {Object} Parsed options.
- * @param callback {Function} `function (err, callback)` where err is an
- * error string if there was a problem.
+ * @param callback {Function} `function (err, chunk)` where err is an
+ * error string if there was a problem. This is called once for each
+ * "chunk". XXX START HERE: splain chunks
*/
-function getInput(opts, callback) {
- if (opts.inputFiles.length === 0) {
- // Read from stdin.
- var chunks = [];
-
- var stdin = process.openStdin();
- stdin.setEncoding('utf8');
- stdin.on('data', function (chunk) {
- chunks.push(chunk);
- });
+function jsonChunksFromInput(opts, callback) {
+ // If returned from `stripHeaders()` and `finishedHeaders` is still false,
+ // then we've process a chunk with an incomplete set of headers:
+ // `stripHeaders()` should be called again with the next chunk.
+ var finishedHeaders = false;
+ function stripHeaders(s) {
+ // Take off a leading HTTP header if any and pass it through.
+ while (true) {
+ if (s.slice(0,5) === "HTTP/") {
+ var index = s.indexOf('\r\n\r\n');
+ var sepLen = 4;
+ if (index == -1) {
+ index = s.indexOf('\n\n');
+ sepLen = 2;
+ }
+ if (index != -1) {
+ if (! opts.dropHeaders) {
+ emit(s.slice(0, index+sepLen));
+ }
+ var is100Continue = (s.slice(0, 21) === "HTTP/1.1 100 Continue");
+ s = s.slice(index+sepLen);
+ if (is100Continue) {
+ continue;
+ }
+ finishedHeaders = true;
+ }
+ } else {
+ finishedHeaders = true;
+ }
+ break;
+ }
+ //console.warn("XXX stripHeaders done, finishedHeaders=%s", finishedHeaders)
+ return s;
+ }
- stdin.on('end', function () {
- callback(null, chunks.join(''));
- });
- } else {
+ if (opts.inputFiles.length > 0) {
// Read input files.
+ // TODO: Improve streaming here: read files async in chunks and stream
+ // as above if `-ga`.
var i = 0;
var chunks = [];
try {
- for (; i < opts.inputFiles.length; i++) {
- chunks.push(fs.readFileSync(opts.inputFiles[i], 'utf8'));
+ var first = fs.readFileSync(opts.inputFiles[i], 'utf8');
+ first = stripHeaders(first);
+ callback(null, first);
+ for (i++; i < opts.inputFiles.length; i++) {
+ callback(null, fs.readFileSync(opts.inputFiles[i], 'utf8'));
}
} catch (e) {
return callback(
format('could not read "%s": %s', opts.inputFiles[i], e));
}
- callback(null, chunks.join(''));
+ } else if (opts.group && opts.array && opts.outputMode !== OM_JSON) {
+ // Streaming from stdin.
+ //console.warn("XXX streaming");
+ var streaming = true;
+ var leftover = '';
+ var chunks = [];
+ var splitter = /(})(\s*\n\s*)?({\s*")/;
+ function callbackJsonChunks(chunk) {
+ if (chunk[0] !== '{') { // Only support streaming consecutive *objects*.
+ streaming = false;
+ chunks.push(chunk);
+ return;
+ }
+ /* Example:
+ * > '{"a":"b"}\n{"a":"b"}\n{"a":"b"}'.split(/(})(\s*\n\s*)?({\s*")/)
+ * [ '{"a":"b"',
+ * '}',
+ * '\n',
+ * '{"',
+ * 'a":"b"',
+ * '}',
+ * '\n',
+ * '{"',
+ * 'a":"b"}' ]
+ */
+ var bits = chunk.split(splitter);
+ //console.warn("XXX bits: ", bits)
+ if (bits.length === 1) {
+ leftover = chunk;
+ } else {
+ var n = bits.length - 2;
+ callback(null, bits[0] + bits[1]);
+ for (var i = 3; i < n; i += 4) {
+ callback(null, bits[i] + bits[i+1] + bits[i+2]);
+ }
+ leftover = bits[n] + bits[n+1];
+ }
+ }
+
+ var stdin = process.openStdin();
+ stdin.setEncoding('utf8');
+ stdin.on('data', function (chunk) {
+ //console.warn("XXX process chunk: %s", JSON.stringify(chunk))
+ if (!streaming) {
+ chunks.push(chunk);
+ return;
+ }
+ var s = leftover + chunk;
+ if (!finishedHeaders) {
+ s = stripHeaders(s);
+ }
+ if (!finishedHeaders) {
+ leftover = s;
+ } else {
+ callbackJsonChunks(s);
+ }
+ });
+ stdin.on('end', function () {
+ if (!streaming) {
+ callback(null, chunks.join(''));
+ } else if (leftover) {
+ callbackJsonChunks(leftover);
+ callback(null, leftover);
+ }
+ });
+ } else {
+ // Read stdin in one big chunk.
+ var stdin = process.openStdin();
+ stdin.setEncoding('utf8');
+ var chunks = [];
+ stdin.on('data', function (chunk) {
+ chunks.push(chunk);
+ });
+ stdin.on('end', function () {
+ var chunk = chunks.join('');
+ chunk = stripHeaders(chunk);
+ callback(null, chunks.join(''));
+ });
}
}
@@ -590,6 +694,7 @@ function parseInput(buffer, group, merge) {
// This condition should be fine for typical use cases and ensures
// no false matches inside JS strings.
var newBuffer = buffer;
+ //XXX START HERE
[/(})\s*\n\s*({)/g, /(})({")/g].forEach(function (pat) {
newBuffer = newBuffer.replace(pat, "$1,\n$2");
});
@@ -820,55 +925,33 @@ function main(argv) {
}
var lookupStrs = opts.args;
- getInput(opts, function (err, buffer) {
+ jsonChunksFromInput(opts, function (err, chunk) {
+ //console.warn("XXX chunk: '%s'", chunk)
if (err) {
warn("json: error: %s", err)
return drainStdoutAndExit(1);
}
- // Take off a leading HTTP header if any and pass it through.
- while (true) {
- 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) {
- if (! opts.dropHeaders) {
- emit(buffer.slice(0, index+sepLen));
- }
- var is100Continue = (buffer.slice(0, 21) === "HTTP/1.1 100 Continue");
- buffer = buffer.slice(index+sepLen);
- if (is100Continue) {
- continue;
- }
- }
- }
- break;
- }
-
// Expect the remainder to be JSON.
- if (! buffer.length) {
+ if (! chunk.length) {
return;
}
// parseInput() -> {datum: <input object>, error: <error object>}
- var input = parseInput(buffer, opts.group, opts.merge);
+ var input = parseInput(chunk, opts.group, opts.merge);
if (input.error) {
// Doesn't look like JSON. Just print it out and move on.
if (! opts.quiet) {
// Use JSON-js' "json_parse" parser to get more detail on the
// syntax error.
var details = "";
- var normBuffer = buffer.replace(/\r\n|\n|\r/, '\n');
+ var normBuffer = chunk.replace(/\r\n|\n|\r/, '\n');
try {
json_parse(normBuffer);
details = input.error;
} catch(err) {
// err.at has the position. Get line/column from that.
var at = err.at - 1; // `err.at` looks to be 1-based.
- var lines = buffer.split('\n');
+ var lines = chunk.split('\n');
var line, col, pos = 0;
for (line = 0; line < lines.length; line++) {
pos += lines[line].length + 1;
@@ -887,8 +970,8 @@ function main(argv) {
warn("json: error: input is not JSON: %s", details);
}
if (!opts.validate) {
- emit(buffer);
- if (buffer.length && buffer[buffer.length-1] !== "\n") {
+ emit(chunk);
+ if (chunk.length && chunk[chunk.length-1] !== "\n") {
emit('\n');
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.