From 755319130e6d3135a286cab86bd12de9d4c6ff41 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 24 Jan 2023 10:40:45 +0100 Subject: [PATCH] Tweak the internal handling of the `url`-parameter in `getDocument` (PR 13166 follow-up) - Use a `URL`-instance directly, since it's by definition an absolute URL. - Actually limit the "raw" url-string handling to Node.js environments, as intended. - Skip the warning, since we're already throwing an Error if the `url`-parameter is invalid. --- src/display/api.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index b960a16d3..10c1703f8 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -289,18 +289,24 @@ function getDocument(src) { switch (key) { case "url": - if (typeof window !== "undefined") { - try { - // The full path is required in the 'url' field. - params[key] = new URL(val, window.location).href; - continue; - } catch (ex) { - warn(`Cannot create valid URL: "${ex}".`); - } - } else if (typeof val === "string" || val instanceof URL) { - params[key] = val.toString(); // Support Node.js environments. + if (val instanceof URL) { + params[key] = val.href; continue; } + try { + // The full path is required in the 'url' field. + params[key] = new URL(val, window.location).href; + continue; + } catch (ex) { + if ( + typeof PDFJSDev !== "undefined" && + PDFJSDev.test("GENERIC") && + isNodeJS && + typeof val === "string" + ) { + break; // Use the url as-is in Node.js environments. + } + } throw new Error( "Invalid PDF url data: " + "either string or URL-object is expected in the url property."