Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

clipper(core): make fetch options optional everywhere

Abdullah Atta7b7fd6204701
diff --git a/packages/clipper/src/clone.ts b/packages/clipper/src/clone.ts
index 803f9d7d9..7921b1f3b 100644
--- a/packages/clipper/src/clone.ts
+++ b/packages/clipper/src/clone.ts
@@ -118,7 +118,7 @@ export async function cloneNode(node: HTMLElement, options: CloneProps) {
const { root, filter } = options;
if (!root && filter && !filter(node)) return null;
- let clone = await makeNodeCopy(node, options.fetchOptions || {});
+ let clone = await makeNodeCopy(node, options.fetchOptions);
if (!clone) return null;
clone = await cloneChildren(node, clone, options);
@@ -127,7 +127,7 @@ export async function cloneNode(node: HTMLElement, options: CloneProps) {
return processed;
}
-function makeNodeCopy(original: HTMLElement, options: FetchOptions) {
+function makeNodeCopy(original: HTMLElement, options?: FetchOptions) {
try {
if (original instanceof HTMLCanvasElement)
return createImage(original.toDataURL(), options);
diff --git a/packages/clipper/src/domtoimage.ts b/packages/clipper/src/domtoimage.ts
index 43d271d80..95b9b2884 100644
--- a/packages/clipper/src/domtoimage.ts
+++ b/packages/clipper/src/domtoimage.ts
@@ -26,14 +26,13 @@ import { cacheStylesheets, inlineStylesheets } from "./styles";
// Default impl options
const defaultOptions: Options = {
- fetchOptions: {},
inlineOptions: {}
};
async function getInlinedNode(node: HTMLElement, options: Options) {
const { fonts, images, stylesheets } = options.inlineOptions || {};
- if (stylesheets) await inlineStylesheets(options.fetchOptions || {});
+ if (stylesheets) await inlineStylesheets(options.fetchOptions);
const documentStyles = getComputedStyle(document.documentElement);
@@ -51,9 +50,9 @@ async function getInlinedNode(node: HTMLElement, options: Options) {
if (!clone || clone instanceof Text) return;
- if (fonts) clone = await embedFonts(clone, options.fetchOptions || {});
+ if (fonts) clone = await embedFonts(clone, options.fetchOptions);
- if (images) await inlineAllImages(clone, options.fetchOptions || {});
+ if (images) await inlineAllImages(clone, options.fetchOptions);
finalize(clone);
return clone;
@@ -125,7 +124,7 @@ function toCanvas(node: HTMLElement, options: Options) {
function draw(domNode: HTMLElement, options: Options) {
options = { ...defaultOptions, ...options };
return toSvg(domNode, options)
- .then((uri) => (uri ? createImage(uri, options.fetchOptions || {}) : null))
+ .then((uri) => (uri ? createImage(uri, options.fetchOptions) : null))
.then(delay(0))
.then(function (image) {
const scale = typeof options.scale !== "number" ? 1 : options.scale;
@@ -158,7 +157,7 @@ function newCanvas(node: HTMLElement, scale: number, options: Options) {
return canvas;
}
-function embedFonts(node: HTMLElement, options: FetchOptions) {
+function embedFonts(node: HTMLElement, options?: FetchOptions) {
return resolveAll(options).then(function (cssText) {
const styleNode = document.createElement("style");
node.appendChild(styleNode);
diff --git a/packages/clipper/src/fetch.ts b/packages/clipper/src/fetch.ts
index 0c66fa200..066b2af7f 100644
--- a/packages/clipper/src/fetch.ts
+++ b/packages/clipper/src/fetch.ts
@@ -23,7 +23,7 @@ export type FetchOptions = {
crossOrigin?: "anonymous" | "use-credentials" | null;
};
-export async function fetchResource(url: string, options: FetchOptions) {
+export async function fetchResource(url: string, options?: FetchOptions) {
if (!url) return null;
const response = await fetch(constructUrl(url, options));
@@ -39,11 +39,11 @@ export async function fetchResource(url: string, options: FetchOptions) {
});
}
-export function createImage(url: string, options: FetchOptions) {
+export function createImage(url: string, options?: FetchOptions) {
if (url === "data:,") return Promise.resolve(null);
return new Promise<HTMLImageElement>(function (resolve, reject) {
const image = new Image();
- image.crossOrigin = options.crossOrigin || null;
+ image.crossOrigin = options?.crossOrigin || null;
image.onload = function () {
resolve(image);
};
@@ -71,14 +71,14 @@ export function reloadImage(image: HTMLImageElement, options: FetchOptions) {
});
}
-export function constructUrl(url: string, options: FetchOptions) {
+export function constructUrl(url: string, options?: FetchOptions) {
if (!url.startsWith("http")) return url;
- if (options.noCache) {
+ if (options?.noCache) {
// Cache bypass so we dont have CORS issues with cached images
// Source: https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Bypassing_the_cache
url += (/\?/.test(url) ? "&" : "?") + Date.now();
}
- if (options.bypassCors && options.corsHost) {
+ if (options?.bypassCors && options?.corsHost) {
if (url.startsWith(options.corsHost)) return url;
url = `${options.corsHost}/${url}`;
diff --git a/packages/clipper/src/fontfaces.ts b/packages/clipper/src/fontfaces.ts
index 9b7f05f97..4c73280e0 100644
--- a/packages/clipper/src/fontfaces.ts
+++ b/packages/clipper/src/fontfaces.ts
@@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { FetchOptions } from "./fetch";
import { inlineAll, shouldProcess } from "./inliner";
-async function resolveAll(options: FetchOptions) {
+async function resolveAll(options?: FetchOptions) {
const fonts = readAll();
const cssStrings: string[] = [];
for (const font of fonts) {
@@ -54,7 +54,7 @@ function getWebFonts(styleSheets: StyleSheetList) {
function newWebFont(webFontRule: CSSFontFaceRule) {
return {
- resolve: function resolve(options: FetchOptions) {
+ resolve: function resolve(options?: FetchOptions) {
const baseUrl = (webFontRule.parentStyleSheet || {}).href || undefined;
return inlineAll(webFontRule.cssText, options, baseUrl);
},
diff --git a/packages/clipper/src/images.ts b/packages/clipper/src/images.ts
index 90121bd0d..07f4954b4 100644
--- a/packages/clipper/src/images.ts
+++ b/packages/clipper/src/images.ts
@@ -20,7 +20,7 @@ import { FetchOptions, fetchResource } from "./fetch";
import { inlineAll } from "./inliner";
import { isDataUrl } from "./utils";
-async function inlineAllImages(root: HTMLElement, options: FetchOptions) {
+async function inlineAllImages(root: HTMLElement, options?: FetchOptions) {
const imageNodes = root.querySelectorAll("img");
const promises: Promise<any>[] = [];
for (let i = 0; i < imageNodes.length; ++i) {
@@ -40,7 +40,7 @@ async function inlineAllImages(root: HTMLElement, options: FetchOptions) {
}
export { inlineAllImages };
-async function inlineImage(element: HTMLImageElement, options: FetchOptions) {
+async function inlineImage(element: HTMLImageElement, options?: FetchOptions) {
if (isDataUrl(element.currentSrc)) return Promise.resolve(null);
const dataURL = await fetchResource(
@@ -69,7 +69,7 @@ async function inlineImage(element: HTMLImageElement, options: FetchOptions) {
async function inlineBackground(
backgroundNode: HTMLElement,
- options: FetchOptions
+ options?: FetchOptions
) {
const background = backgroundNode.style.getPropertyValue("background-image");
if (!background) return backgroundNode;
diff --git a/packages/clipper/src/inliner.ts b/packages/clipper/src/inliner.ts
index f7b194bc0..9c860d89a 100644
--- a/packages/clipper/src/inliner.ts
+++ b/packages/clipper/src/inliner.ts
@@ -39,7 +39,7 @@ function readUrls(string: string) {
async function inline(
string: string,
url: string,
- options: FetchOptions,
+ options?: FetchOptions,
baseUrl?: string
) {
url = baseUrl ? resolveUrl(url, baseUrl) : url;
@@ -54,7 +54,7 @@ function urlAsRegex(urlValue: string) {
async function inlineAll(
string: string,
- options: FetchOptions,
+ options?: FetchOptions,
baseUrl?: string
) {
if (!shouldProcess(string)) return string;
diff --git a/packages/clipper/src/styles.ts b/packages/clipper/src/styles.ts
index cf4d8291e..78002cefa 100644
--- a/packages/clipper/src/styles.ts
+++ b/packages/clipper/src/styles.ts
@@ -68,7 +68,7 @@ const SHORTHANDS = [
"transition"
];
-export async function inlineStylesheets(options: FetchOptions) {
+export async function inlineStylesheets(options?: FetchOptions) {
for (const sheet of document.styleSheets) {
if (skipStyleSheet(sheet)) continue;
@@ -86,7 +86,7 @@ export async function inlineStylesheets(options: FetchOptions) {
await resolveImports(options);
}
-async function resolveImports(options: FetchOptions) {
+async function resolveImports(options?: FetchOptions) {
let index = 0;
for (const sheet of document.styleSheets) {
if (skipStyleSheet(sheet)) continue;
@@ -107,7 +107,7 @@ async function resolveImports(options: FetchOptions) {
}
}
-async function downloadStylesheet(href: string, options: FetchOptions) {
+async function downloadStylesheet(href: string, options?: FetchOptions) {
try {
const style = document.createElement("style");
const response = await fetch(constructUrl(href, options));