parse5-parser-stream 5.1.1 → 7.0.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/README.md CHANGED
@@ -15,9 +15,8 @@
15
15
  </div>
16
16
  <br>
17
17
 
18
-
19
18
  <p align="center">
20
- 📖 <a href="https://github.com/inikulin/parse5/tree/master/packages/parse5-parser-stream/docs/index.md"><b>Documentation</b></a> 📖
19
+ 📖 <a href="https://parse5.js.org/modules/parse5_parser_stream.html"><b>Documentation</b></a> 📖
21
20
  </p>
22
21
 
23
22
  ---
@@ -31,5 +30,5 @@
31
30
  </p>
32
31
 
33
32
  <p align="center">
34
- <a href="https://github.com/inikulin/parse5/tree/master/docs/version-history.md">Version history</a>
33
+ <a href="https://github.com/inikulin/parse5/releases">Changelog</a>
35
34
  </p>
@@ -0,0 +1,85 @@
1
+ /// <reference types="node" />
2
+ import { Writable } from 'node:stream';
3
+ import { Parser, type ParserOptions, type TreeAdapterTypeMap, type DefaultTreeAdapterMap } from 'parse5';
4
+ /**
5
+ * Streaming HTML parser with scripting support.
6
+ * A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
7
+ *
8
+ * @example
9
+ *
10
+ * ```js
11
+ * const ParserStream = require('parse5-parser-stream');
12
+ * const http = require('http');
13
+ * const { finished } = require('node:stream');
14
+ *
15
+ * // Fetch the page content and obtain it's <head> node
16
+ * http.get('http://inikulin.github.io/parse5/', res => {
17
+ * const parser = new ParserStream();
18
+ *
19
+ * finished(parser, () => {
20
+ * console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
21
+ * });
22
+ *
23
+ * res.pipe(parser);
24
+ * });
25
+ * ```
26
+ *
27
+ */
28
+ export declare class ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> extends Writable {
29
+ parser: Parser<T>;
30
+ static getFragmentStream<T extends TreeAdapterTypeMap>(fragmentContext?: T['parentNode'] | null, options?: ParserOptions<T>): ParserStream<T>;
31
+ private lastChunkWritten;
32
+ private writeCallback;
33
+ private pendingHtmlInsertions;
34
+ /** The resulting document node. */
35
+ get document(): T['document'];
36
+ getFragment(): T['documentFragment'];
37
+ /**
38
+ * @param options Parsing options.
39
+ */
40
+ constructor(options?: ParserOptions<T>, parser?: Parser<T>);
41
+ _write(chunk: string, _encoding: string, callback: () => void): void;
42
+ end(chunk?: any, encoding?: any, callback?: any): any;
43
+ }
44
+ export interface ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> {
45
+ /**
46
+ * Raised when parser encounters a `<script>` element. If this event has listeners, parsing will be suspended once
47
+ * it is emitted. So, if `<script>` has the `src` attribute, you can fetch it, execute and then resume parsing just
48
+ * like browsers do.
49
+ *
50
+ * @example
51
+ *
52
+ * ```js
53
+ * const ParserStream = require('parse5-parser-stream');
54
+ * const http = require('http');
55
+ *
56
+ * const parser = new ParserStream();
57
+ *
58
+ * parser.on('script', (scriptElement, documentWrite, resume) => {
59
+ * const src = scriptElement.attrs.find(({ name }) => name === 'src').value;
60
+ *
61
+ * http.get(src, res => {
62
+ * // Fetch the script content, execute it with DOM built around `parser.document` and
63
+ * // `document.write` implemented using `documentWrite`.
64
+ * ...
65
+ * // Then resume parsing.
66
+ * resume();
67
+ * });
68
+ * });
69
+ *
70
+ * parser.end('<script src="example.com/script.js"></script>');
71
+ * ```
72
+ *
73
+ * @param event Name of the event
74
+ * @param handler
75
+ */
76
+ on(event: 'script', handler: (scriptElement: T['element'], documentWrite: (html: string) => void, resume: () => void) => void): void;
77
+ /**
78
+ * Base event handler.
79
+ *
80
+ * @param event Name of the event
81
+ * @param handler Event handler
82
+ */
83
+ on(event: string, handler: (...args: any[]) => void): this;
84
+ }
85
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,86 @@
1
+ import { Writable } from 'node:stream';
2
+ import { Parser } from 'parse5';
3
+ /* eslint-disable unicorn/consistent-function-scoping -- The rule seems to be broken here. */
4
+ /**
5
+ * Streaming HTML parser with scripting support.
6
+ * A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
7
+ *
8
+ * @example
9
+ *
10
+ * ```js
11
+ * const ParserStream = require('parse5-parser-stream');
12
+ * const http = require('http');
13
+ * const { finished } = require('node:stream');
14
+ *
15
+ * // Fetch the page content and obtain it's <head> node
16
+ * http.get('http://inikulin.github.io/parse5/', res => {
17
+ * const parser = new ParserStream();
18
+ *
19
+ * finished(parser, () => {
20
+ * console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
21
+ * });
22
+ *
23
+ * res.pipe(parser);
24
+ * });
25
+ * ```
26
+ *
27
+ */
28
+ export class ParserStream extends Writable {
29
+ /**
30
+ * @param options Parsing options.
31
+ */
32
+ constructor(options, parser = new Parser(options)) {
33
+ super({ decodeStrings: false });
34
+ this.parser = parser;
35
+ this.lastChunkWritten = false;
36
+ this.writeCallback = undefined;
37
+ this.pendingHtmlInsertions = [];
38
+ const resume = () => {
39
+ for (let i = this.pendingHtmlInsertions.length - 1; i >= 0; i--) {
40
+ this.parser.tokenizer.insertHtmlAtCurrentPos(this.pendingHtmlInsertions[i]);
41
+ }
42
+ this.pendingHtmlInsertions.length = 0;
43
+ //NOTE: keep parsing if we don't wait for the next input chunk
44
+ this.parser.tokenizer.resume(this.writeCallback);
45
+ };
46
+ const documentWrite = (html) => {
47
+ if (!this.parser.stopped) {
48
+ this.pendingHtmlInsertions.push(html);
49
+ }
50
+ };
51
+ const scriptHandler = (scriptElement) => {
52
+ if (this.listenerCount('script') > 0) {
53
+ this.parser.tokenizer.pause();
54
+ this.emit('script', scriptElement, documentWrite, resume);
55
+ }
56
+ };
57
+ this.parser.scriptHandler = scriptHandler;
58
+ }
59
+ static getFragmentStream(fragmentContext, options) {
60
+ const parser = Parser.getFragmentParser(fragmentContext, options);
61
+ const stream = new ParserStream(options, parser);
62
+ return stream;
63
+ }
64
+ /** The resulting document node. */
65
+ get document() {
66
+ return this.parser.document;
67
+ }
68
+ getFragment() {
69
+ return this.parser.getFragment();
70
+ }
71
+ //WritableStream implementation
72
+ _write(chunk, _encoding, callback) {
73
+ if (typeof chunk !== 'string') {
74
+ throw new TypeError('Parser can work only with string streams.');
75
+ }
76
+ this.writeCallback = callback;
77
+ this.parser.tokenizer.write(chunk, this.lastChunkWritten, this.writeCallback);
78
+ }
79
+ // TODO [engine:node@>=16]: Due to issues with Node < 16, we are overriding `end` instead of `_final`.
80
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
81
+ end(chunk, encoding, callback) {
82
+ this.lastChunkWritten = true;
83
+ super.end(chunk || '', encoding, callback);
84
+ }
85
+ }
86
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "parse5-parser-stream",
3
+ "type": "module",
3
4
  "description": "Streaming HTML parser with scripting support.",
4
- "version": "5.1.1",
5
+ "version": "7.0.0",
5
6
  "author": "Ivan Nikulin <ifaaan@gmail.com> (https://github.com/inikulin)",
6
7
  "contributors": "https://github.com/inikulin/parse5/graphs/contributors",
7
8
  "homepage": "https://github.com/inikulin/parse5",
9
+ "funding": "https://github.com/inikulin/parse5?sponsor=1",
8
10
  "keywords": [
9
11
  "parse5",
10
12
  "parser",
@@ -12,16 +14,19 @@
12
14
  "streaming"
13
15
  ],
14
16
  "license": "MIT",
15
- "main": "./lib/index.js",
17
+ "main": "dist/index.js",
18
+ "module": "dist/index.js",
19
+ "types": "dist/index.d.ts",
20
+ "exports": "./dist/index.js",
16
21
  "dependencies": {
17
- "parse5": "^5.1.1"
22
+ "parse5": "^7.0.0"
18
23
  },
19
24
  "repository": {
20
25
  "type": "git",
21
26
  "url": "git://github.com/inikulin/parse5.git"
22
27
  },
23
28
  "files": [
24
- "lib"
25
- ],
26
- "gitHead": "9c7556ed05e4ff4d884ab2447e27ce3817c42e79"
29
+ "dist",
30
+ "!*.map"
31
+ ]
27
32
  }
package/lib/index.js DELETED
@@ -1,83 +0,0 @@
1
- 'use strict';
2
-
3
- const { Writable } = require('stream');
4
- const Parser = require('parse5/lib/parser');
5
-
6
- class ParserStream extends Writable {
7
- constructor(options) {
8
- super({ decodeStrings: false });
9
-
10
- this.parser = new Parser(options);
11
-
12
- this.lastChunkWritten = false;
13
- this.writeCallback = null;
14
- this.pausedByScript = false;
15
-
16
- this.document = this.parser.treeAdapter.createDocument();
17
-
18
- this.pendingHtmlInsertions = [];
19
-
20
- this._resume = this._resume.bind(this);
21
- this._documentWrite = this._documentWrite.bind(this);
22
- this._scriptHandler = this._scriptHandler.bind(this);
23
-
24
- this.parser._bootstrap(this.document, null);
25
- }
26
-
27
- //WritableStream implementation
28
- _write(chunk, encoding, callback) {
29
- if (typeof chunk !== 'string') {
30
- throw new TypeError('Parser can work only with string streams.');
31
- }
32
-
33
- this.writeCallback = callback;
34
- this.parser.tokenizer.write(chunk, this.lastChunkWritten);
35
- this._runParsingLoop();
36
- }
37
-
38
- end(chunk, encoding, callback) {
39
- this.lastChunkWritten = true;
40
- super.end(chunk || '', encoding, callback);
41
- }
42
-
43
- //Scriptable parser implementation
44
- _runParsingLoop() {
45
- this.parser.runParsingLoopForCurrentChunk(this.writeCallback, this._scriptHandler);
46
- }
47
-
48
- _resume() {
49
- if (!this.pausedByScript) {
50
- throw new Error('Parser was already resumed');
51
- }
52
-
53
- while (this.pendingHtmlInsertions.length) {
54
- const html = this.pendingHtmlInsertions.pop();
55
-
56
- this.parser.tokenizer.insertHtmlAtCurrentPos(html);
57
- }
58
-
59
- this.pausedByScript = false;
60
-
61
- //NOTE: keep parsing if we don't wait for the next input chunk
62
- if (this.parser.tokenizer.active) {
63
- this._runParsingLoop();
64
- }
65
- }
66
-
67
- _documentWrite(html) {
68
- if (!this.parser.stopped) {
69
- this.pendingHtmlInsertions.push(html);
70
- }
71
- }
72
-
73
- _scriptHandler(scriptElement) {
74
- if (this.listenerCount('script') > 0) {
75
- this.pausedByScript = true;
76
- this.emit('script', scriptElement, this._documentWrite, this._resume);
77
- } else {
78
- this._runParsingLoop();
79
- }
80
- }
81
- }
82
-
83
- module.exports = ParserStream;