Skip to content

Commit

Permalink
fix: throw error if URL is provided but fetch is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
mojoaxel committed Jan 27, 2024
1 parent 8bca3b7 commit af96727
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions PDFMergerBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/PDFMerger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit af96727

Please sign in to comment.