-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
270 lines (230 loc) · 7.07 KB
/
index.js
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
var sax = require('sax'),
http = require('http'),
https = require('https'),
fs = require('fs'),
writable = require('stream').Writable;
var reserved = ['attributes', 'children', 'tagName', 'text'];
function safe(name) { return reserved.indexOf(name) < 0; }
function build(from) {
var res = {};
var children = from.children || [];
res.children = children;
res.attributes = from.attributes;
res.tagName = from.name;
res.text = from.text;
var attrs = res.attributes;
for (var attr in attrs) if (safe(attr)) res[attr] = attrs[attr];
for (var i in children) {
var child = children[i];
if (!res.hasOwnProperty(child.tagName) && safe(child.tagName)) res[child.tagName] = child;
}
return res;
}
function setProp(dest, name, value) {
if (dest.hasOwnProperty(name)) {
if (Array.isArray(dest[name])) dest[name].push(value);
else dest[name] = [dest[name], value];
} else dest[name] = value;
}
function buildPojo(from) {
var res = {};
var attrs = from.attributes || {};
var children = from.children || [];
// if this has no children or attributes, just return text if it's available
if (JSON.stringify(attrs) === '{}' && children.length === 0 && !!from.text)
return from.text;
else
if (!!from.text) res.text = from.text;
for (var a in attrs) setProp(res, a, attrs[a]);
for (var c = 0; c < children.length; c++) setProp(res, children[c].name, children[c].object);
return res;
}
function chunkStream(size, from, to, chunkDone) {
var stream = writable();
var pending = 0;
var ended = false;
stream._write = function(chunk, _enc, next) {
pending++;
function done() {
pending--;
next();
if (ended) stream.end();
}
var pos = 0;
if (chunk.length > size) {
var go;
go = function() {
var sub = chunk.slice(pos, pos + size);
to.write(sub);
pos += sub.length;
if (pos >= chunk.length) {
chunkDone(done);
} else {
chunkDone(go);
}
};
go();
} else {
to.write(chunk);
chunkDone(done);
}
};
stream.end = function() {
ended = true;
if (!pending) to.end();
};
from.pipe(stream);
}
module.exports = function(config) {
var cfg = config || {},
strict = cfg.strict === undefined ? true : cfg.strict,
icase = cfg.icase || true,
pojo = cfg.pojo || false,
chunkSize = cfg.chunk || 8196;
return function(xml, pattern, cb) {
var stream = sax.createStream(strict, cfg);
var stack = [];
var path = [];
var matcher = [];
var levels = [];
var defer;
var promise;
var after;
var collection = [];
var cdata;
if (typeof pattern === 'string') {
matcher.push(new RegExp('^' + pattern.replace(/\/\//, '\\/.*?') + '$', icase || !strict ? 'i' : ''));
} else if (Array.isArray(pattern)) {
for (var i = 0; i < pattern.length; i++) {
matcher.push(new RegExp('^' + pattern[i].replace(/\/\//, '\\/.*?') + '$', icase || !strict ? 'i' : ''));
}
} else throw 'Invalid pattern.';
// if callback has a done function, wait until it is called to proceeds
var waiting = 0, fired = 0;
var shouldWait = !cb ? false : cb.length > 1;
var waitingTo;
function waitToDo(fn) {
if (shouldWait && fired < waiting) {
if (!!waitingTo) console.error('overwriting waitFn!!!');
waitingTo = fn;
} else {
setImmediate(fn);
}
}
function resume() {
fired++;
if (waiting <= fired) {
if (!!waitingTo) setImmediate(waitingTo);
waitingTo = null;
}
}
if (!cb) {
promise = new Promise(function(ok, fail) {
defer = { resolve: ok, reject: fail };
});
} else after = { onEnd: function(fn) { after.callback = fn; } };
stream.on('error', function() {
this._parser.error = null;
this._parser.resume();
});
stream.on('opentag', function(node) {
// keep path current
path.push(node.name);
stack.unshift(node);
var loc = '/' + path.join('/');
// is this a child of a node we're looking for?
if (levels[0]) levels.unshift(2);
else levels.unshift(0);
// is this a node we're looking for?
for (i = 0; i < matcher.length; i++) {
if (!!loc.match(matcher[i])) {
levels[0] = 1;
break;
}
}
});
stream.on('closetag', function(name) {
path.pop();
var n = pojo ? { object: buildPojo(stack.shift()), name: name } : build(stack.shift());
var p = stack[0];
var level = levels.shift();
// is this a child of a node we're looking for?
if (level === 2 && !!p) {
if (!pojo) {
if (safe(name) && !p.hasOwnProperty(name)) p[name] = n;
}
if (!p.children) p.children = [n];
else p.children.push(n);
}
// is this a node we're looking for?
if (level === 1) {
if (!!cb) {
if (shouldWait) {
waiting++;
cb(pojo ? n.object : n, resume);
} else cb(pojo ? n.object : n);
} else collection.push(pojo ? n.object : n);
}
});
stream.on('text', function(txt) {
if (!levels[0]) return;
// add text to current
var n = stack[0];
if (!!n) {
if (!n.text) n.text = txt;
else n.text += txt;
}
});
stream.on('opencdata', function() {
cdata = '';
});
stream.on('cdata', function(txt) {
cdata += txt;
});
stream.on('closecdata', function() {
if (!levels[0]) return;
// add text to current
var n = stack[0];
if (!!n) {
if (!n.text) n.text = cdata;
else n.text += cdata;
}
cdata = '';
});
stream.on('end', function() {
if (shouldWait) {
if (!!after.callback && typeof after.callback === 'function') waitingTo = after.callback;
} else {
if (!cb) defer.resolve(collection);
else if (!!after.callback && typeof after.callback === 'function') setImmediate(after.callback);
}
});
if (typeof xml === 'string') {
if (xml.indexOf('http://') === 0 || xml.indexOf('https://') === 0) {
(xml.indexOf('https://') === 0 ? https : http).get(xml, function(res) { chunkStream(chunkSize, res, stream, waitToDo); }).on('error', function(err) {
if (!cb) defer.reject(err);
});
} else if (xml.indexOf('file://') === 0) {
var pth = xml.substring(7);
chunkStream(chunkSize, fs.createReadStream(pth), stream, waitToDo);
} else {
var read = require('stream').Readable();
var done = false;
read._read = function() {
if (done) return read.push(null);
read.push(xml);
done = true;
return;
};
chunkStream(chunkSize, read, stream, waitToDo);
}
} else if (!!xml && typeof xml === 'object' && typeof xml.pipe === 'function') {
chunkStream(chunkSize, xml, stream, waitToDo);
} else {
stream._parser.close();
throw new Error("Unknown input format.");
}
if (!cb) return promise;
else return after;
};
};