Guest User

Untitled

a guest
Feb 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. const { createReadStream } = require("fs");
  2. const { Parser } = require("htmlparser2");
  3. const { pipeline, Transform } = require("stream");
  4. const { isArray, last } = require("lodash");
  5.  
  6. let i = 0;
  7.  
  8. class XML extends Transform {
  9. /**
  10. * @constructor
  11. * @param {string} tagName Target tag name
  12. * @param {number} tagDepth Target tag depath
  13. * @param {object} options All transform stream options
  14. */
  15. constructor(tagName, tagDepth, options = {}) {
  16. super({
  17. ...options,
  18. objectMode: true
  19. });
  20.  
  21. let depth = 0;
  22. let tree;
  23.  
  24. this._parser = new Parser(
  25. {
  26. onopentag: (name, attrs) => {
  27. if (!tree && name === tagName && depth === tagDepth) {
  28. tree = [];
  29. }
  30. if (tree) {
  31. const parent = last(tree);
  32. const child = { _attributes: attrs };
  33. if (parent) {
  34. if (!parent[name]) {
  35. parent[name] = child;
  36. } else if (isArray(parent[name])) {
  37. parent[name].push(child);
  38. } else {
  39. parent[name] = [parent[name], child];
  40. }
  41. }
  42. tree.push(child);
  43. }
  44. depth++;
  45. },
  46. ontext: text => {
  47. if (tree) {
  48. last(tree)._text = text;
  49. }
  50. },
  51. onclosetag: () => {
  52. if (tree) {
  53. const node = tree.pop();
  54. if (tree.length <= 0) {
  55. tree = undefined;
  56. this.push(node);
  57. }
  58. }
  59. depth--;
  60. },
  61. onend: () => {
  62. this._close();
  63. },
  64. onerror: err => {
  65. this.emit("error", err);
  66. }
  67. },
  68. { decodeEntities: true }
  69. );
  70. }
  71.  
  72. _transform(chunk, encoding, callback) {
  73. this._parser.write(chunk.toString("utf8"));
  74. callback();
  75. }
  76.  
  77. _final(callback) {
  78. this._close = callback;
  79. this._parser.end();
  80. }
  81. }
  82.  
  83. const file = "big.xml";
  84. const encoding = "utf8";
  85.  
  86. async function foo() {
  87. await new Promise((resolve, reject) => {
  88. pipeline(
  89. //
  90. createReadStream(file, { encoding }),
  91. //
  92. new XML("page", 1),
  93. //
  94. err => (err ? reject(err) : resolve())
  95. )
  96. // .on("data", data => console.log(data))
  97. .on("data", () => i++);
  98. });
  99. }
  100.  
  101. console.log(new Date());
  102. foo()
  103. .catch(err => console.error(err))
  104. .then(() => console.log(i))
  105. .then(() => console.log(new Date()));
Add Comment
Please, Sign In to add comment