Browse Source

moved code into single-file-util.js

Former-commit-id: 66ed5654425609ca8c16be0fe335ed69ce31dcbf
Gildas 6 years ago
parent
commit
a3e44f481f
2 changed files with 57 additions and 69 deletions
  1. 54 4
      lib/single-file/single-file-util.js
  2. 3 65
      lib/single-file/single-file.js

+ 54 - 4
lib/single-file/single-file-util.js

@@ -33,9 +33,13 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 	const DOMParser = window.DOMParser;
 	const Blob = window.Blob;
 	const FileReader = window.FileReader;
+	const fetch = window.fetch;
+	const crypto = window.crypto;
+	const TextDecoder = window.TextDecoder;
+	const TextEncoder = window.TextEncoder;
 
 	return {
-		getInstance: (modules, util) => {
+		getInstance: (modules, utilOptions) => {
 			if (modules.serializer === undefined) {
 				modules.serializer = {
 					process(doc) {
@@ -58,6 +62,8 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 				};
 			}
 
+			utilOptions = utilOptions || {};
+			utilOptions.fetch = utilOptions.fetch || fetch;
 			return {
 				getContent,
 				parseURL(resourceURL, baseURI) {
@@ -106,7 +112,8 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 					return (new DOMParser()).parseFromString(content, "image/svg+xml");
 				},
 				async digest(algo, text) {
-					return util.digestText(algo, text);
+					const hash = await crypto.subtle.digest(algo, new TextEncoder("utf-8").encode(text));
+					return hex(hash);
 				},
 				getContentSize(content) {
 					return new Blob([content]).size;
@@ -187,7 +194,7 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 					log("  // STARTED download url =", resourceURL, "asBinary =", options.asBinary);
 				}
 				try {
-					resourceContent = await util.getResourceContent(resourceURL);
+					resourceContent = await getResourceContent(resourceURL, utilOptions.fetch);
 				} catch (error) {
 					return { data: options.asBinary ? "data:base64," : "", resourceURL };
 				}
@@ -251,10 +258,53 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 					}
 				}
 			}
-
 		}
 	};
 
+	async function getResourceContent(resourceURL, fetchResource) {
+		const resourceContent = await fetchResource(resourceURL);
+		const buffer = await resourceContent.arrayBuffer();
+		return {
+			getUrl() {
+				return resourceContent.url || resourceURL;
+			},
+			getContentType() {
+				return resourceContent.headers && resourceContent.headers.get("content-type");
+			},
+			getStatusCode() {
+				return resourceContent.status;
+			},
+			getSize() {
+				return buffer.byteLength;
+			},
+			getText(charset) {
+				return new TextDecoder(charset).decode(buffer);
+			},
+			async getDataUri(contentType) {
+				const reader = new FileReader();
+				reader.readAsDataURL(new Blob([buffer], { type: contentType || this.getContentType() }));
+				return new Promise((resolve, reject) => {
+					reader.addEventListener("load", () => resolve(reader.result), false);
+					reader.addEventListener("error", reject, false);
+				});
+			}
+		};
+	}
+
+	// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
+	function hex(buffer) {
+		const hexCodes = [];
+		const view = new DataView(buffer);
+		for (let i = 0; i < view.byteLength; i += 4) {
+			const value = view.getUint32(i);
+			const stringValue = value.toString(16);
+			const padding = "00000000";
+			const paddedValue = (padding + stringValue).slice(-padding.length);
+			hexCodes.push(paddedValue);
+		}
+		return hexCodes.join("");
+	}
+
 	function log(...args) {
 		console.log("S-File <browser>", ...args); // eslint-disable-line no-console
 	}

+ 3 - 65
lib/single-file/single-file.js

@@ -21,17 +21,9 @@
  *   Source.
  */
 
-/* global window */
-
 this.singlefile.lib.SingleFile = this.singlefile.lib.SingleFile || (() => {
 
 	const singlefile = this.singlefile;
-	const crypto = window.crypto;
-	const fetch = window.fetch;
-	const Blob = window.Blob;
-	const FileReader = window.FileReader;
-	const TextDecoder = window.TextDecoder;
-	const TextEncoder = window.TextEncoder;
 
 	const modules = {
 		helper: singlefile.lib.helper,
@@ -46,66 +38,12 @@ this.singlefile.lib.SingleFile = this.singlefile.lib.SingleFile || (() => {
 		mediasAltMinifier: singlefile.lib.modules.mediasAltMinifier,
 		imagesAltMinifier: singlefile.lib.modules.imagesAltMinifier
 	};
-	const util = {
-		getResourceContent,
-		digestText
-	};
-	const SingleFile = singlefile.lib.core.getClass(singlefile.lib.util.getInstance(modules, util), singlefile.lib.vendor.cssTree);
-	let fetchResource;
+
 	return {
-		getClass: options => {
-			fetchResource = (options && options.fetch) || fetch;
+		getClass: classOptions => {
+			const SingleFile = singlefile.lib.core.getClass(singlefile.lib.util.getInstance(modules, classOptions), singlefile.lib.vendor.cssTree);
 			return SingleFile;
 		}
 	};
 
-	async function getResourceContent(resourceURL) {
-		const resourceContent = await fetchResource(resourceURL);
-		const buffer = await resourceContent.arrayBuffer();
-		return {
-			getUrl() {
-				return resourceContent.url || resourceURL;
-			},
-			getContentType() {
-				return resourceContent.headers && resourceContent.headers.get("content-type");
-			},
-			getStatusCode() {
-				return resourceContent.status;
-			},
-			getSize() {
-				return buffer.byteLength;
-			},
-			getText(charset) {
-				return new TextDecoder(charset).decode(buffer);
-			},
-			async getDataUri(contentType) {
-				const reader = new FileReader();
-				reader.readAsDataURL(new Blob([buffer], { type: contentType || this.getContentType() }));
-				return new Promise((resolve, reject) => {
-					reader.addEventListener("load", () => resolve(reader.result), false);
-					reader.addEventListener("error", reject, false);
-				});
-			}
-		};
-	}
-
-	async function digestText(algo, text) {
-		const hash = await crypto.subtle.digest(algo, new TextEncoder("utf-8").encode(text));
-		return hex(hash);
-	}
-
-	// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
-	function hex(buffer) {
-		const hexCodes = [];
-		const view = new DataView(buffer);
-		for (let i = 0; i < view.byteLength; i += 4) {
-			const value = view.getUint32(i);
-			const stringValue = value.toString(16);
-			const padding = "00000000";
-			const paddedValue = (padding + stringValue).slice(-padding.length);
-			hexCodes.push(paddedValue);
-		}
-		return hexCodes.join("");
-	}
-
 })();