|
| 1 | +const { Writable } = require('stream'); |
1 | 2 | const clarinet = require('clarinet'); |
2 | 3 |
|
3 | 4 | /** |
@@ -30,129 +31,137 @@ const cleanObject = (obj) => { |
30 | 31 | * @param {boolean} buffer If true, will buffer entire object. |
31 | 32 | * @return {clarinet.Stream} |
32 | 33 | */ |
33 | | -module.exports = (buffer) => { |
34 | | - const parser = clarinet.createStream(); |
35 | | - const stack = []; |
36 | | - let currObj = {}; |
37 | | - let currKey = 'feed'; |
38 | | - let inArray = false; |
39 | | - let feedJustFound = false; |
40 | | - |
41 | | - // Look feed object in case this is a json encoded atom feed. |
42 | | - // Place these underlying keys onto the root object. |
43 | | - const findfeed = (key) => { |
44 | | - if (key === 'feed') { |
45 | | - feedJustFound = true; |
46 | | - parser.removeListener('openobject', findfeed); |
47 | | - parser.removeListener('key', findfeed); |
48 | | - } |
49 | | - }; |
50 | | - |
51 | | - const onvalue = (value) => { |
52 | | - feedJustFound = false; |
53 | | - currObj[currKey] = value; |
54 | | - if (stack.length === 1) { |
55 | | - parser.emit(currKey, value); |
56 | | - if (!buffer) { delete currObj[currKey]; } |
57 | | - } |
58 | | - if (inArray) { |
59 | | - currKey++; |
60 | | - } |
61 | | - }; |
| 34 | +module.exports = class JSONFeedParser extends Writable { |
| 35 | + constructor(buffer) { |
| 36 | + super(); |
| 37 | + this._buffer = buffer; |
| 38 | + const parser = this.parser = clarinet.createStream(); |
| 39 | + const stack = []; |
| 40 | + this._currObj = {}; |
| 41 | + this._currKey = 'feed'; |
| 42 | + let inArray = false; |
| 43 | + let feedJustFound = false; |
| 44 | + |
| 45 | + // Look feed object in case this is a json encoded atom feed. |
| 46 | + // Place these underlying keys onto the root object. |
| 47 | + const findfeed = (key) => { |
| 48 | + if (key === 'feed') { |
| 49 | + feedJustFound = true; |
| 50 | + parser.removeListener('openobject', findfeed); |
| 51 | + parser.removeListener('key', findfeed); |
| 52 | + } |
| 53 | + }; |
62 | 54 |
|
63 | | - const onopenobject = (key) => { |
64 | | - if (feedJustFound) { |
| 55 | + const onvalue = (value) => { |
65 | 56 | feedJustFound = false; |
66 | | - currKey = key; |
67 | | - return; |
68 | | - } |
69 | | - let obj = currObj[currKey] = {}; |
70 | | - stack.push({ |
71 | | - obj: currObj, |
72 | | - key: currKey, |
73 | | - arr: inArray, |
74 | | - }); |
75 | | - currObj = obj; |
76 | | - currKey = key; |
77 | | - inArray = false; |
78 | | - }; |
79 | | - |
80 | | - const onkey = (key) => { currKey = key; }; |
81 | | - |
82 | | - const oncloseobject = () => { |
83 | | - let parent = stack.pop(); |
84 | | - if (!parent) { return; } |
85 | | - currObj = parent.obj; |
86 | | - currKey = parent.key; |
87 | | - inArray = parent.arr; |
88 | | - |
89 | | - // Clean object. |
90 | | - currObj[currKey] = cleanObject(currObj[currKey]); |
91 | | - |
92 | | - // Emit key in feed if curr is parent. |
93 | | - if (stack.length === 1) { |
94 | | - parser.emit(currKey, currObj[currKey]); |
95 | | - if (!buffer) { delete currObj[currKey]; } |
96 | | - |
97 | | - // Or parent is array. |
98 | | - } else if (inArray) { |
99 | | - if (stack.length === 2) { |
100 | | - let key = stack[1].key; |
101 | | - let event = key === 'entry' || key === 'items' ? |
102 | | - 'item' : stack[1].key; |
103 | | - parser.emit(event, currObj[currKey]); |
104 | | - if (!buffer) { currObj.splice(currKey, 1); } |
| 57 | + this._currObj[this._currKey] = value; |
| 58 | + if (stack.length === 1) { |
| 59 | + parser.emit(this._currKey, value); |
| 60 | + if (!buffer) { delete this._currObj[this._currKey]; } |
105 | 61 | } |
| 62 | + if (inArray) { |
| 63 | + this._currKey++; |
| 64 | + } |
| 65 | + }; |
106 | 66 |
|
107 | | - if (stack.length > 2 || buffer) { currKey++; } |
108 | | - } |
109 | | - }; |
110 | | - |
111 | | - const onopenarray = () => { |
112 | | - feedJustFound = false; |
113 | | - let obj = currObj[currKey] = []; |
114 | | - stack.push({ |
115 | | - obj: currObj, |
116 | | - key: currKey, |
117 | | - arr: inArray, |
118 | | - }); |
119 | | - currObj = obj; |
120 | | - currKey = 0; |
121 | | - inArray = true; |
122 | | - }; |
123 | | - |
124 | | - const onclosearray = () => { |
125 | | - let parent = stack.pop(); |
126 | | - currObj = parent.obj; |
127 | | - currKey = parent.key; |
128 | | - inArray = parent.arr; |
129 | | - |
130 | | - if (stack.length === 1) { |
131 | | - if (!buffer) { delete currObj[currKey]; } |
132 | | - } else if (inArray) { |
133 | | - currKey++; |
134 | | - } |
135 | | - }; |
136 | | - |
137 | | - parser.on('openobject', findfeed); |
138 | | - parser.on('key', findfeed); |
139 | | - parser.on('value', onvalue); |
140 | | - parser.on('openobject', onopenobject); |
141 | | - parser.on('key', onkey); |
142 | | - parser.on('closeobject', oncloseobject); |
143 | | - parser.on('openarray', onopenarray); |
144 | | - parser.on('closearray', onclosearray); |
145 | | - |
146 | | - parser.done = () => { |
147 | | - if (!buffer) { return; } |
148 | | - let root = currObj[currKey]; |
| 67 | + const onopenobject = (key) => { |
| 68 | + if (feedJustFound) { |
| 69 | + feedJustFound = false; |
| 70 | + this._currKey = key; |
| 71 | + return; |
| 72 | + } |
| 73 | + let obj = this._currObj[this._currKey] = {}; |
| 74 | + stack.push({ |
| 75 | + obj: this._currObj, |
| 76 | + key: this._currKey, |
| 77 | + arr: inArray, |
| 78 | + }); |
| 79 | + this._currObj = obj; |
| 80 | + this._currKey = key; |
| 81 | + inArray = false; |
| 82 | + }; |
| 83 | + |
| 84 | + const onkey = (key) => { this._currKey = key; }; |
| 85 | + |
| 86 | + const oncloseobject = () => { |
| 87 | + let parent = stack.pop(); |
| 88 | + if (!parent) { return; } |
| 89 | + this._currObj = parent.obj; |
| 90 | + this._currKey = parent.key; |
| 91 | + inArray = parent.arr; |
| 92 | + |
| 93 | + // Clean object. |
| 94 | + this._currObj[this._currKey] = cleanObject(this._currObj[this._currKey]); |
| 95 | + |
| 96 | + // Emit key in feed if curr is parent. |
| 97 | + if (stack.length === 1) { |
| 98 | + parser.emit(this._currKey, this._currObj[this._currKey]); |
| 99 | + if (!buffer) { delete this._currObj[this._currKey]; } |
| 100 | + |
| 101 | + // Or parent is array. |
| 102 | + } else if (inArray) { |
| 103 | + if (stack.length === 2) { |
| 104 | + let key = stack[1].key; |
| 105 | + let event = key === 'entry' || key === 'items' ? |
| 106 | + 'item' : stack[1].key; |
| 107 | + let data = this._currObj[this._currKey]; |
| 108 | + parser.emit(event, data); |
| 109 | + if (!buffer) { this._currObj.splice(this._currKey, 1); } |
| 110 | + } |
| 111 | + |
| 112 | + if (stack.length > 2 || buffer) { this._currKey++; } |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + const onopenarray = () => { |
| 117 | + feedJustFound = false; |
| 118 | + let obj = this._currObj[this._currKey] = []; |
| 119 | + stack.push({ |
| 120 | + obj: this._currObj, |
| 121 | + key: this._currKey, |
| 122 | + arr: inArray, |
| 123 | + }); |
| 124 | + this._currObj = obj; |
| 125 | + this._currKey = 0; |
| 126 | + inArray = true; |
| 127 | + }; |
| 128 | + |
| 129 | + const onclosearray = () => { |
| 130 | + let parent = stack.pop(); |
| 131 | + this._currObj = parent.obj; |
| 132 | + this._currKey = parent.key; |
| 133 | + inArray = parent.arr; |
| 134 | + |
| 135 | + if (stack.length === 1) { |
| 136 | + if (!buffer) { delete this._currObj[this._currKey]; } |
| 137 | + } else if (inArray) { |
| 138 | + this._currKey++; |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + parser.on('openobject', findfeed); |
| 143 | + parser.on('key', findfeed); |
| 144 | + parser.on('value', onvalue); |
| 145 | + parser.on('openobject', onopenobject); |
| 146 | + parser.on('key', onkey); |
| 147 | + parser.on('closeobject', oncloseobject); |
| 148 | + parser.on('openarray', onopenarray); |
| 149 | + parser.on('closearray', onclosearray); |
| 150 | + } |
| 151 | + |
| 152 | + _write(chunk, encoding, callback) { |
| 153 | + this.parser.write(chunk, encoding); |
| 154 | + callback(null); |
| 155 | + } |
| 156 | + |
| 157 | + done() { |
| 158 | + if (!this._buffer) { return; } |
| 159 | + let root = this._currObj[this._currKey]; |
149 | 160 | root.type = 'json'; |
150 | 161 | if (Array.isArray(root.entry)) { |
151 | 162 | root.items = root.entry; |
152 | 163 | } |
153 | 164 | delete root.entry; |
154 | 165 | return root; |
155 | | - }; |
156 | | - |
157 | | - return parser; |
| 166 | + } |
158 | 167 | }; |
0 commit comments