8000 no more swallowing errors · ORESoftware/json-parser@a4aa6b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit a4aa6b1

Browse files
author
Alexander Mills
committed
no more swallowing errors
1 parent a10e410 commit a4aa6b1

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

.r2g/tests/smoke-test.3.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
const assert = require('assert');
5+
const path = require('path');
6+
const cp = require('child_process');
7+
const os = require('os');
8+
const fs = require('fs');
9+
const EE = require('events');
10+
11+
process.on('uncaughtException', err => {
12+
console.error('uncaughtException:', {err});
13+
process.exit(1);
14+
});
15+
16+
process.on('unhandledRejection', (reason, p) => {
17+
// note: unless we force process to exit with 1, process may exit with 0 upon an unhandledRejection
18+
console.error(reason);
19+
process.exit(1);
20+
});
21+
22+
const {JSONParser} = require('@oresoftware/json-stream-parser');
23+
24+
console.log('Running test', __filename);
25+
26+
const k = cp.spawn('bash');
27+
const foo = 'medicine';
28+
29+
k.stdin.end(`
30+
31+
foo="${foo}"
32+
33+
cat <<EOF
34+
35+
{"foo":"$foo"} ∆∆
36+
{"foo":"$foo"} ∆
37+
∆{"foo":"$foo"}∆
38+
39+
EOF
40+
41+
`);
42+
43+
const to = setTimeout(() => {
44+
console.error('did not receive parsed JSON object within alloted time.');
45+
process.exit(1);
46+
}, 2000);
47+
48+
49+
let count = 0;
50+
51+
k.stdout.pipe(new JSONParser({delimiter:'∆'})).on('data', d => {
52+
53+
count++;
54+
55+
if(count > 3){
56+
throw new Error('too many json blobs.');
57+
}
58+
59+
if(count === 3){
60+
61+
setTimeout(() => {
62+
63+
clearTimeout(to);
64+
try {
65+
assert.deepStrictEqual(d, {foo: foo});
66+
process.exit(0);
67+
}
68+
catch (err) {
69+
console.error(err.message);
70+
process.exit(1);
71+
}
72+
73+
},1000);
74+
75+
}
76+
77+
78+
});
79+

readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const k = cp.spawn('bash');
7575
k.stdin.end(`echo '{"foo":"bar"}\n'`); // make sure to include the newline char when you write
7676

7777
k.stdout.pipe(new JSONParser()).on('data', d => {
78-
// => {foo:'bar'}
78+
// => {foo: 'bar'}
7979
});
8080

8181
```
@@ -94,9 +94,7 @@ k.stdin.end(`
9494
`);
9595

9696
k.stdout.pipe(new JSONParser()).on('data', d => {
97-
98-
assert.deepStrictEqual(d, {foo: 'medicine'});
99-
97+
assert.deepStrictEqual(d, {foo: 'medicine'}); // should pass
10098
});
10199

102100

@@ -111,4 +109,7 @@ new JSONParser({delimiter: '∆∆∆'}); // use 3 alt-j's to separate json chu
111109

112110
```
113111

112+
For other solutions to parsing JSON from CLIs, see:
113+
https://stackoverflow.com/questions/56014438/get-single-line-json-from-aws-cli
114+
114115

src/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,22 @@ export class JSONParser extends stream.Transform {
5151
continue;
5252
}
5353

54+
let json = null;
55+
5456
try {
5557
l = String(l).trim();
5658
// l might be an empty string; ignore if so
57-
l && this.push(JSON.parse(String(l).trim()));
59+
l && (json = JSON.parse(String(l).trim()));
5860
} catch (err) {
5961
if (this.debug) {
6062
console.error('json-parser:', 'error parsing line:', l);
6163
console.error('json-parser:', err.message);
6264
}
65+
continue;
6366
// noop
6467
}
68+
69+
json && this.push(json);
6570
}
6671

6772
cb();
@@ -70,11 +75,20 @@ export class JSONParser extends stream.Transform {
7075

7176
_flush(cb: Function) {
7277
if (this.lastLineData) {
78+
79+
let json = null, l = String(this.lastLineData).trim();
80+
7381
try {
74-
this.push(JSON.parse(this.lastLineData));
82+
json = JSON.parse(l);
7583
} catch (err) {
76-
// noop
84+
if (this.debug) {
85+
console.error('json-parser:', 'error parsing line:', l);
86+
console.error('json-parser:', err.message);
87+
}
7788
}
89+
90+
json && this.push(json);
91+
7892
}
7993
this.lastLineData = '';
8094
cb();

0 commit comments

Comments
 (0)
0