-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.js
167 lines (141 loc) · 3.99 KB
/
stream.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
const Saxophone = require('@pirxpilot/saxophone');
const stack = require('./stack');
const debug = require('debug')('sax-super-stream');
/* global TransformStream, EventTarget */
module.exports = initParser;
// used to mark all tags that we want to skip
const IGNORE = Symbol('ignore');
function stripPrefix(name) {
const prefixed = name.split(':', 2);
return prefixed[1] ? prefixed : [undefined, prefixed[0]];
}
function makeHandlers(parserConfig, emit) {
const ns = Object.create(null);
let defaultNS;
const items = stack();
const parsers = stack(parserConfig);
const context = {};
const target = new EventTarget();
target.addEventListener('tagopen', ontagopen);
target.addEventListener('tagclose', ontagclose);
target.addEventListener('text', ontext);
target.addEventListener('cdata', ontext);
return target;
function ontagopen({ detail: { name, attrs, isSelfClosing } }) {
const [prefix, tag] = stripPrefix(name);
const attrsObj = Saxophone.parseAttrs(attrs);
updateNamespaces(attrsObj);
const uri = (prefix && ns[prefix]) ?? defaultNS;
const tagParser = getParser(tag, uri);
debug('tagopen', tag, attrs);
if (!tagParser) {
parsers.push(IGNORE, tag);
} else if (typeof tagParser === 'function') {
parseWith(tagParser);
} else {
if (typeof tagParser.$ === 'function') {
parseWith(tagParser.$);
}
parsers.push(tagParser, tag);
}
if (isSelfClosing) {
ontagclose({ detail: { name } });
}
function parseWith(tp) {
const attributes = Object.fromEntries(
Object.entries(attrsObj).map(
([name, value]) => [
name,
{
name,
value: Saxophone.parseEntities(value)
}
]
)
);
const node = { prefix, tag, attributes };
const elem = tp.call(items, node, items.top(), context);
if (elem) {
items.push(elem, tag);
}
}
}
function ontagclose({ detail: { name } }) {
const [, tag] = stripPrefix(name);
debug('tagclose', tag);
const parser = parsers.pop(tag);
if (typeof parser?.$after === 'function') {
debug('$after', tag);
parser.$after.call(items, items.top(), context);
}
if (parser !== IGNORE) {
const top = items.pop(tag);
// if nothing on the stack emit result
if (top !== undefined && items.empty()) {
emit(top);
}
}
}
function ontext({ type, detail: { contents } }) {
debug('text', contents);
const textParser = parsers.top().$text;
textParser?.call(
items,
type === 'cdata' ? contents : Saxophone.parseEntities(contents),
items.top(),
context
);
}
function updateNamespaces(attrsObj) {
Object.entries(attrsObj).forEach(xmlns);
function xmlns([name, value]) {
if (name === 'xmlns') {
defaultNS = value;
} else if (name.startsWith('xmlns:')) {
const prefix = name.slice(6); // 'xmlns:'.length === 6
ns[prefix] = value;
}
}
}
function getParser(tag, uri) {
const top = parsers.top();
if (top === IGNORE) {
return;
}
const tp = top[tag];
if (!tp) {
return;
}
if (!tp.$uri) {
return tp;
}
// if parser specifies namespace, it has to match node namespace
if (tp.$uri === uri) {
return tp;
}
}
}
function initParser(config) {
const results = [];
const target = makeHandlers(config, (...objs) => results.push(...objs));
const sax = new Saxophone(target);
const writer = sax.getWriter();
return new TransformStream({
async flush(controller) {
await writer.ready;
await writer.close();
flush(controller);
controller.terminate();
},
async transform(chunk, controller) {
debug('writing', chunk);
await writer.ready;
await writer.write(chunk);
flush(controller);
}
});
function flush(controller) {
results.forEach(r => controller.enqueue(r));
results.length = 0;
}
}