single-file-browser.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global
  21. DocUtilCore,
  22. crypto,
  23. cssTree,
  24. docHelper,
  25. fetch,
  26. setTimeout,
  27. superFetch,
  28. Blob,
  29. DOMParser,
  30. FileReader,
  31. FontFace
  32. SingleFileCore,
  33. TextDecoder,
  34. TextEncoder */
  35. this.SingleFileBrowser = this.SingleFileBrowser || (() => {
  36. const ONE_MB = 1024 * 1024;
  37. const DEBUG = false;
  38. const PREFIX_CONTENT_TYPE_TEXT = "text/";
  39. const FONT_FACE_TEST_MAX_DELAY = 1000;
  40. let fetchResource;
  41. return {
  42. getClass: () => {
  43. const DocUtil = DocUtilCore.getClass(getContent, parseDocContent, parseSVGContent, isValidFontUrl, getContentSize, digestText);
  44. return SingleFileCore.getClass(DocUtil, cssTree);
  45. }
  46. };
  47. async function getContent(resourceURL, options) {
  48. let resourceContent, startTime;
  49. if (DEBUG) {
  50. startTime = Date.now();
  51. log(" // STARTED download url =", resourceURL, "asDataURI =", options.asDataURI);
  52. }
  53. if (!fetchResource) {
  54. fetchResource = typeof superFetch == "undefined" ? fetch : superFetch.fetch;
  55. }
  56. try {
  57. resourceContent = await fetchResource(resourceURL);
  58. if (resourceContent.url) {
  59. resourceURL = resourceContent.url;
  60. }
  61. } catch (error) {
  62. return { data: options.asDataURI ? "data:base64," : "", resourceURL };
  63. }
  64. let contentType = resourceContent.headers && resourceContent.headers.get("content-type");
  65. let charset;
  66. if (contentType) {
  67. const matchContentType = contentType.toLowerCase().split(";");
  68. contentType = matchContentType[0].trim();
  69. if (!contentType.includes("/")) {
  70. contentType = null;
  71. }
  72. const charsetValue = matchContentType[1] && matchContentType[1].trim();
  73. if (charsetValue) {
  74. const matchCharset = charsetValue.match(/^charset=(.*)/);
  75. if (matchCharset && matchCharset[1]) {
  76. charset = docHelper.removeQuotes(matchCharset[1].trim());
  77. }
  78. }
  79. }
  80. if (!charset && options.charset) {
  81. charset = options.charset;
  82. }
  83. if (options.asDataURI) {
  84. try {
  85. if (DEBUG) {
  86. log(" // ENDED download url =", resourceURL, "delay =", Date.now() - startTime);
  87. }
  88. const buffer = await resourceContent.arrayBuffer();
  89. if (options.maxResourceSizeEnabled && buffer.byteLength > options.maxResourceSize * ONE_MB) {
  90. return { data: "data:base64,", resourceURL };
  91. } else {
  92. const reader = new FileReader();
  93. reader.readAsDataURL(new Blob([buffer], { type: contentType }));
  94. const dataURI = await new Promise((resolve, reject) => {
  95. reader.addEventListener("load", () => resolve(reader.result), false);
  96. reader.addEventListener("error", reject, false);
  97. });
  98. return { data: dataURI, resourceURL };
  99. }
  100. } catch (error) {
  101. return { data: "data:base64,", resourceURL };
  102. }
  103. } else {
  104. if (resourceContent.status >= 400 || (options.validateTextContentType && contentType && !contentType.startsWith(PREFIX_CONTENT_TYPE_TEXT))) {
  105. return { data: "", resourceURL };
  106. }
  107. if (!charset) {
  108. charset = "utf-8";
  109. }
  110. let buffer;
  111. try {
  112. buffer = await resourceContent.arrayBuffer();
  113. } catch (error) {
  114. return { data: "", resourceURL, charset };
  115. }
  116. if (DEBUG) {
  117. log(" // ENDED download url =", resourceURL, "delay =", Date.now() - startTime);
  118. }
  119. if (options.maxResourceSizeEnabled && buffer.byteLength > options.maxResourceSize * ONE_MB) {
  120. return { data: "", resourceURL, charset };
  121. } else {
  122. try {
  123. return { data: new TextDecoder(charset).decode(buffer), resourceURL, charset };
  124. } catch (error) {
  125. try {
  126. charset = "utf-8";
  127. return { data: new TextDecoder(charset).decode(buffer), resourceURL, charset };
  128. } catch (error) {
  129. return { data: "", resourceURL, charset };
  130. }
  131. }
  132. }
  133. }
  134. }
  135. // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
  136. function hex(buffer) {
  137. const hexCodes = [];
  138. const view = new DataView(buffer);
  139. for (let i = 0; i < view.byteLength; i += 4) {
  140. const value = view.getUint32(i);
  141. const stringValue = value.toString(16);
  142. const padding = "00000000";
  143. const paddedValue = (padding + stringValue).slice(-padding.length);
  144. hexCodes.push(paddedValue);
  145. }
  146. return hexCodes.join("");
  147. }
  148. function parseDocContent(content, baseURI) {
  149. const doc = (new DOMParser()).parseFromString(content, "text/html");
  150. let baseElement = doc.querySelector("base");
  151. if (!baseElement || !baseElement.getAttribute("href")) {
  152. if (baseElement) {
  153. baseElement.remove();
  154. }
  155. baseElement = doc.createElement("base");
  156. baseElement.setAttribute("href", baseURI);
  157. doc.head.insertBefore(baseElement, doc.head.firstChild);
  158. }
  159. return doc;
  160. }
  161. function parseSVGContent(content) {
  162. return (new DOMParser()).parseFromString(content, "image/svg+xml");
  163. }
  164. async function digestText(algo, text) {
  165. const hash = await crypto.subtle.digest(algo, new TextEncoder("utf-8").encode(text));
  166. return (hex(hash));
  167. }
  168. function getContentSize(content) {
  169. return new Blob([content]).size;
  170. }
  171. async function isValidFontUrl(urlFunction) {
  172. try {
  173. const font = new FontFace("font-test", urlFunction);
  174. await Promise.race([font.load(), new Promise(resolve => setTimeout(() => resolve(true), FONT_FACE_TEST_MAX_DELAY))]);
  175. return true;
  176. } catch (error) {
  177. return false;
  178. }
  179. }
  180. function log(...args) {
  181. console.log("S-File <browser>", ...args); // eslint-disable-line no-console
  182. }
  183. })();