@miso.ai/server-commons 0.6.4 → 0.6.5-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -16,9 +16,10 @@
16
16
  "denque": "^2.1.0",
17
17
  "js-yaml": "^4.1.0",
18
18
  "log-update": "^5.0.1",
19
+ "saxes": "^6.0.0",
19
20
  "toml": "^3.0.0",
20
21
  "uuid": "^9.0.0",
21
22
  "yargs": "^17.5.1"
22
23
  },
23
- "version": "0.6.4"
24
+ "version": "0.6.5-beta.0"
24
25
  }
@@ -6,3 +6,4 @@ export { default as OutputStream } from './output.js';
6
6
  export { default as LogUpdateStream } from './log-update.js';
7
7
  export { default as DiffStream } from './diff.js';
8
8
  export { default as ParallelTransform } from './parallel-transform.js';
9
+ export { default as XmlParseStream } from './xml.js';
@@ -0,0 +1,51 @@
1
+ import { Transform } from 'stream';
2
+ import { SaxesParser } from 'saxes';
3
+
4
+ function normalizeSaxesOptions({
5
+ ...options
6
+ } = {}) {
7
+ return {
8
+ ...options,
9
+ };
10
+ }
11
+
12
+ function bindHandler(context, saxes, parser, event, method) {
13
+ parser[method] && saxes.on(event, data => parser[method](data, context));
14
+ }
15
+
16
+ export default class XmlParseStream extends Transform {
17
+
18
+ constructor(parser, { saxes, ...options } = {}) {
19
+ super({
20
+ ...options,
21
+ writableObjectMode: false,
22
+ readableObjectMode: true,
23
+ });
24
+ this._options = options;
25
+ const saxesParser = this._saxes = new SaxesParser(normalizeSaxesOptions(saxes));
26
+ const context = Object.freeze({
27
+ stream: this,
28
+ push: this.push.bind(this),
29
+ emit: this.emit.bind(this),
30
+ });
31
+
32
+ bindHandler(context, saxesParser, parser, 'opentag', 'onOpenTag');
33
+ bindHandler(context, saxesParser, parser, 'closetag', 'onCloseTag');
34
+ bindHandler(context, saxesParser, parser, 'text', 'onText');
35
+ bindHandler(context, saxesParser, parser, 'cdata', 'onCData');
36
+ bindHandler(context, saxesParser, parser, 'error', 'onError');
37
+
38
+ if (!this._options.surpressErrors) {
39
+ bindHandler(context, saxesParser, 'error', this._onError);
40
+ }
41
+ }
42
+
43
+ _transform(chunk, encoding, next) {
44
+ this._saxes.write(chunk);
45
+ next();
46
+ }
47
+
48
+ _onError(err) {
49
+ this.emit('error', err);
50
+ }
51
+ }