-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
188 lines (165 loc) · 4.33 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
const Duplex = require('readable-stream').Duplex
const Wrapper = require('./lib/wrap.js')
const through2 = require('through2')
const duplexer = require('duplexer2')
const tokenize = require('html-tokenize')
const select = require('html-select2')
class Trumpet extends Duplex {
#writing = false;
#piping = false;
#tokenize = tokenize();
#selected = this.#tokenize.pipe(select());
constructor (options) {
super(options)
this.#selected.once('end', () => {
this.emit('_end')
this.push(null)
})
this.once('finish', () => {
this.#tokenize.end()
})
}
select (str, cb) {
let first = true
const res = this.selectAll(str, (elem) => {
if (!first) return
first = false
res.createReadStream = () => {}
res.createWriteStream = () => {}
res.createStream = () => {}
if (cb) cb(elem)
})
return res
}
// override stream
pipe () {
this.#piping = true
return super.pipe.apply(this, arguments)
}
selectAll (str, cb) {
const readers = []
const writers = []
const duplex = []
const gets = []
const getss = []
const sets = []
const removes = []
this.once('_end', () => {
readers.splice(0).forEach((r) => {
r.end()
r.resume()
})
duplex.splice(0).forEach((d) => {
d.input.end()
d.input.resume()
})
})
let welem
this.#selected.select(str, (elem) => {
welem = new Wrapper(elem)
if (cb) cb(welem)
elem.once('close', () => {
welem = null
})
readers.splice(0).forEach((r) => {
welem.createReadStream(r._options).pipe(r)
})
writers.splice(0).forEach((w) => {
w.pipe(welem.createWriteStream(w._options))
})
duplex.splice(0).forEach((d) => {
d.input.pipe(welem.createStream(d.options))
.pipe(d.output)
})
gets.splice(0).forEach((g) => {
welem.getAttribute(g[0], g[1])
})
getss.splice(0).forEach((cb) => {
welem.getAttributes(cb)
})
sets.splice(0).forEach((g) => {
welem.setAttribute(g[0], g[1])
})
removes.splice(0).forEach((key) => {
welem.removeAttribute(key)
})
})
return {
getAttribute: (key, cb) => {
if (welem) return welem.getAttribute(key, cb)
gets.push([key, cb])
return this
},
getAttributes: (cb) => {
getss.push(cb)
return this
},
setAttribute: (key, value) => {
if (welem) return welem.setAttribute(key, value)
sets.push([key, value])
return this
},
removeAttribute: (key) => {
if (welem) return welem.removeAttribute(key)
removes.push(key)
return this
},
createReadStream: (opts) => {
if (welem) return welem.createReadStream(opts)
const r = through2()
r._options = opts
readers.push(r)
return r
},
createWriteStream: (opts) => {
if (welem) return welem.createWriteStream(opts)
const w = through2()
w._options = opts
writers.push(w)
return w
},
createStream: (opts) => {
if (welem) return welem.createStream(opts)
const d = { input: through2(), output: through2() }
d.options = opts
duplex.push(d)
return duplexer(d.input, d.output)
}
}
}
// override stream
_read (n) {
let read = 0
let row
while ((row = this.#selected.read()) !== null) {
if (row[0] === 'END') {
this.push(row[1][1])
} else if (row[1] && row[1].length) {
this.push(row[1])
}
read++
}
if (read === 0) this.#selected.once('readable', () => { this._read(n) })
}
// override stream
_write (buf, enc, next) {
if (!this.#writing && !this.#piping) {
this.#piping = true
this.resume()
}
// _write is member of readable-stream.Transform
// noinspection JSUnresolvedFunction
return this.#tokenize._write(buf, enc, next)
}
createReadStream (sel, opts) {
return this.select(sel).createReadStream(opts)
}
createWriteStream (sel, opts) {
return this.select(sel).createWriteStream(opts)
}
createStream (sel, opts) {
return this.select(sel).createStream(opts)
}
}
module.exports = () => { return new Trumpet() }
module.exports.Trumpet = Trumpet