diff --git a/img.js b/img.js index e6bbd41..de4e8b3 100644 --- a/img.js +++ b/img.js @@ -93,11 +93,18 @@ class Util { return obj; } - static isFullUrl(url) { + static isRemoteUrl(url) { try { - new URL(url); - return true; - } catch(e) { + const validUrl = new URL(url); + + if (validUrl.protocol.startsWith("https:") || validUrl.protocol.startsWith("http:")) { + return true; + } + + return false; + } catch(e) + + { // invalid url OR local path return false; } @@ -111,7 +118,7 @@ class Image { } this.src = src; - this.isRemoteUrl = typeof src === "string" && Util.isFullUrl(src); + this.isRemoteUrl = typeof src === "string" && Util.isRemoteUrl(src); this.options = Object.assign({}, globalOptions, options); if(this.isRemoteUrl) { @@ -506,8 +513,8 @@ class Image { * any files. */ static statsSync(src, opts) { - if(typeof src === "string" && Util.isFullUrl(src)) { - throw new Error("`statsSync` is not supported with full URL sources. Use `statsByDimensionsSync` instead."); + if(typeof src === "string" && Util.isRemoteUrl(src)) { + throw new Error("`statsSync` is not supported with remote sources. Use `statsByDimensionsSync` instead."); } let dimensions = getImageSize(src); @@ -586,7 +593,7 @@ function queueImage(src, opts) { let promise = processingQueue.add(async () => { if(typeof src === "string" && opts && opts.statsOnly) { - if(Util.isFullUrl(src)) { + if(Util.isRemoteUrl(src)) { if(!opts.remoteImageMetadata || !opts.remoteImageMetadata.width || !opts.remoteImageMetadata.height) { throw new Error("When using `statsOnly` and remote images, you must supply a `remoteImageMetadata` object with { width, height, format? }"); } diff --git a/test/test.js b/test/test.js index b23c5d4..2fda1bc 100644 --- a/test/test.js +++ b/test/test.js @@ -805,3 +805,26 @@ test("statsOnly using remote image, no urlFormat", async t => { }); }); +test("src is recognized as local when using absolute path on Windows", t => { + let image = new eleventyImage.Image("C:\\image.jpg"); + + t.is(image.isRemoteUrl, false); +}); + +test("src is recognized as local when using absolute path on POSIX", t => { + let image = new eleventyImage.Image("/home/user/image.jpg"); + + t.is(image.isRemoteUrl, false); +}); + +test("src is recognized as remote when using https scheme", t => { + let image = new eleventyImage.Image("https://example.com/image.jpg"); + + t.is(image.isRemoteUrl, true); +}); + +test("src is recognized as remote when using http scheme", t => { + let image = new eleventyImage.Image("http://example.com/image.jpg"); + + t.is(image.isRemoteUrl, true); +});