single-file-jsdom.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global require, exports, Buffer */
  24. const { URL } = require("url");
  25. const fs = require("fs");
  26. const crypto = require("crypto");
  27. const { JSDOM, VirtualConsole } = require("jsdom");
  28. const dataUri = require("strong-data-uri");
  29. const iconv = require("iconv-lite");
  30. const request = require("request-promise-native");
  31. const SCRIPTS = [
  32. "./lib/single-file/util/doc-util.js",
  33. "./lib/single-file/util/doc-helper.js",
  34. "./lib/single-file/vendor/css-tree.js",
  35. "./lib/single-file/vendor/html-srcset-parser.js",
  36. "./lib/single-file/vendor/css-minifier.js",
  37. "./lib/single-file/vendor/css-font-property-parser.js",
  38. "./lib/single-file/vendor/css-media-query-parser.js",
  39. "./lib/single-file/modules/html-minifier.js",
  40. "./lib/single-file/modules/css-fonts-minifier.js",
  41. "./lib/single-file/modules/css-fonts-alt-minifier.js",
  42. "./lib/single-file/modules/css-matched-rules.js",
  43. "./lib/single-file/modules/css-medias-alt-minifier.js",
  44. "./lib/single-file/modules/css-rules-minifier.js",
  45. "./lib/single-file/modules/html-images-alt-minifier.js",
  46. "./lib/single-file/modules/html-serializer.js",
  47. "./lib/single-file/single-file-core.js"
  48. ];
  49. SCRIPTS.forEach(scriptPath => eval(fs.readFileSync(scriptPath).toString()));
  50. const docHelper = this.docHelper;
  51. const modules = {
  52. docHelper: docHelper,
  53. srcsetParser: this.srcsetParser,
  54. cssMinifier: this.cssMinifier,
  55. htmlMinifier: this.htmlMinifier,
  56. serializer: this.serializer,
  57. fontsMinifier: this.fontsMinifier.getInstance(this.cssTree, this.fontPropertyParser, docHelper),
  58. fontsAltMinifier: this.fontsAltMinifier.getInstance(this.cssTree),
  59. cssRulesMinifier: this.cssRulesMinifier.getInstance(this.cssTree),
  60. matchedRules: this.matchedRules.getInstance(this.cssTree),
  61. mediasMinifier: this.mediasMinifier.getInstance(this.cssTree, this.mediaQueryParser),
  62. imagesAltMinifier: this.imagesAltMinifier.getInstance(this.srcsetParser)
  63. };
  64. const domUtil = {
  65. getResourceContent,
  66. parseDocContent,
  67. parseSVGContent,
  68. isValidFontUrl,
  69. getContentSize,
  70. digestText,
  71. parseURL
  72. };
  73. const SingleFile = this.SingleFileCore.getClass(this.docUtil.getInstance(modules, domUtil), this.cssTree);
  74. exports.getClass = () => SingleFile;
  75. exports.getPageData = async options => {
  76. const pageContent = (await request({
  77. method: "GET",
  78. uri: options.url,
  79. resolveWithFullResponse: true,
  80. encoding: null,
  81. headers: {
  82. "User-Agent": options.userAgent
  83. }
  84. })).body.toString();
  85. const dom = new JSDOM(pageContent, { url: options.url, virtualConsole: new VirtualConsole(), userAgent: options.userAgent });
  86. options.win = dom.window;
  87. options.doc = dom.window.document;
  88. options.saveRawPage = true;
  89. options.loadDeferredImages = false;
  90. const processor = new (this.getClass())(options);
  91. await processor.initialize();
  92. await processor.run();
  93. return processor.getPageData();
  94. };
  95. function parseDocContent(content) {
  96. return (new JSDOM(content, {
  97. contentType: "text/html"
  98. })).window.document;
  99. }
  100. function parseSVGContent(content) {
  101. return (new JSDOM(content, {
  102. contentType: "image/svg+xml"
  103. })).window.document;
  104. }
  105. async function digestText(algo, text) {
  106. const hash = crypto.createHash(algo.replace("-", "").toLowerCase());
  107. hash.update(text, "utf-8");
  108. return hash.digest("hex");
  109. }
  110. function getContentSize(content) {
  111. return Buffer.byteLength(content, "utf-8");
  112. }
  113. function isValidFontUrl(/* urlFunction */) {
  114. // TODO?
  115. return true;
  116. }
  117. async function getResourceContent(resourceURL, options) {
  118. const resourceContent = await request({
  119. method: "GET",
  120. uri: resourceURL,
  121. resolveWithFullResponse: true,
  122. encoding: null,
  123. headers: {
  124. "User-Agent": options.userAgent
  125. }
  126. });
  127. return {
  128. getUrl() {
  129. return resourceContent.request.href;
  130. },
  131. getContentType() {
  132. return resourceContent.headers["content-type"];
  133. },
  134. getStatusCode() {
  135. return resourceContent.statusCode;
  136. },
  137. getSize() {
  138. return resourceContent.body.byteLength;
  139. },
  140. getText(charset) {
  141. return iconv.decode(resourceContent.body, charset);
  142. },
  143. async getDataUri(contentType) {
  144. return dataUri.encode(resourceContent.body, contentType || this.getContentType());
  145. }
  146. };
  147. }
  148. function parseURL(resourceURL, baseURI) {
  149. return new URL(resourceURL, baseURI);
  150. }