This repository has been archived by the owner on Mar 8, 2019. It is now read-only.
forked from evs-chris/xml-object-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (198 loc) · 6.59 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
var sax = require('sax'),
when = require('when'),
http = require('http'),
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();
stream._write = function(chunk, enc, next) {
var pos = 0;
if (chunk.length > size) {
var go;
go = function() {
to.write(chunk.slice(pos, pos + size > chunk.length ? chunk.length - 1 : pos + size));
pos += size;
if (pos >= chunk.length) {
chunkDone(next);
} else {
chunkDone(go);
}
}
go();
} else {
to.write(chunk);
chunkDone(next);
}
};
stream.end = function() {
to.end();
}
from.pipe(stream);
}
module.exports = function(config) {
var cfg = config || {},
strict = cfg.strict || true,
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 childMatcher = [];
var defer;
var after;
var collection = [];
if (typeof pattern === 'string') {
matcher.push(new RegExp('^' + pattern.replace(/\/\//, '\\/.*?') + '$', icase || !strict ? 'i' : ''));
childMatcher.push(new RegExp('^' + pattern.replace(/\/\//, '\\/.*?') + '/', icase || !strict ? 'i' : ''));
} else if (Array.isArray(pattern)) {
for (var i = 0; i < pattern.length; i++) {
var pat = pattern[i];
matcher.push(new RegExp('^' + pat.replace(/\/\//, '\\/.*?') + '$', icase || !strict ? 'i' : ''));
childMatcher.push(new RegExp('^' + pat.replace(/\/\//, '\\/.*?') + '/', icase || !strict ? 'i' : ''));
}
} else throw 'Invalid pattern.';
// if callback has a done function, wait until it is called to proceeds
var waiting = 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) { defer = when.defer(); }
else after = { onEnd: function(fn) { after.callback = fn; } };
function current() {
if (stack.length > 0) return stack[stack.length - 1];
else return null;
}
stream.on('error', function(e) {
this._parser.error = null;
this._parser.resume();
});
stream.on('opentag', function(node) {
// keep path current
path.push(node.name);
stack.push(node);
});
stream.on('closetag', function(name) {
var loc = '/' + path.join('/');
path.pop();
var n = pojo ? { object: buildPojo(stack.pop()), name: name } : build(stack.pop());
var p = current();
// is this a child of a node we're looking for?
for (var i = 0; i < childMatcher.length; i++) {
if (!!loc.match(childMatcher[i]) && !!p) {
if (!pojo) {
if (safe(name) && !p.hasOwnProperty(name)) p[name] = n;
}
if (!!!p.children) p.children = [n];
else p.children.push(n);
// make sure we don't match more than one pattern
break;
}
}
// is this a node we're looking for?
for (var i = 0; i < matcher.length; i++) {
if (!!loc.match(matcher[i])) {
if (!!cb) {
if (shouldWait) {
waiting++;
cb(pojo ? n.object : n, resume);
} else cb(pojo ? n.object : n);
}
else collection.push(pojo ? n.object : n);
// make sure we don't match more than one pattern
break;
}
}
});
stream.on('text', function(txt) {
// add text to current
var n = current();
if (!!n) {
if (!!!n.text) n.text = txt;
else n.text += txt;
}
});
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);
}
resume();
});
if (typeof xml === 'string') {
if (xml.indexOf('http://') === 0 || xml.indexOf('https://') === 0) {
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();
read._read = function() { read.push(xml); read.push(null); }
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 "Unknown input format."
}
if (!!!cb) return defer.promise;
else return after;
};
};