From af967275386d9d2bc923d1063e0d2bb5f3a6c355 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik Date: Sat, 27 Jan 2024 14:10:59 +0100 Subject: [PATCH] fix: throw error if URL is provided but fetch is not available --- PDFMergerBase.js | 3 +++ test/PDFMerger.test.js | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/PDFMergerBase.js b/PDFMergerBase.js index 6285dfa..9df364b 100644 --- a/PDFMergerBase.js +++ b/PDFMergerBase.js @@ -162,6 +162,9 @@ export default class PDFMergerBase { // see https://developer.mozilla.org/en-US/docs/Web/API/URL if (input instanceof URL) { + if (typeof fetch === 'undefined') { + throw new Error('fetch is not defined. You need to use a polyfill for this to work in Node.js.') + } const res = await fetch(input) const aBuffer = await res.arrayBuffer() return new Uint8Array(aBuffer) diff --git a/test/PDFMerger.test.js b/test/PDFMerger.test.js index 91ef80d..ccb7262 100644 --- a/test/PDFMerger.test.js +++ b/test/PDFMerger.test.js @@ -121,7 +121,8 @@ describe('PDFMerger', () => { expect(diff).toBeFalsy() }) - test('provide input as URL', async () => { + const testIfFetch = typeof fetch !== 'undefined' ? test : test.skip + testIfFetch('provide input as URL', async () => { const merger = new PDFMerger() const TestfileAURL = new URL('https://raw.githubusercontent.com/nbesli/pdf-merger-js/master/test/fixtures/Testfile_A.pdf') expect(TestfileAURL).toBeInstanceOf(URL)