Skip to content

Commit

Permalink
fix: replace window for globalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
davbrito authored and mojoaxel committed Apr 11, 2023
1 parent 742cf3e commit 6736492
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const { PDFDocument } = require('pdf-lib')

const globalObject =
typeof globalThis === 'object'
? globalThis
: typeof window === 'object'
? window // Browser
: typeof self === 'object'
? self // Worker
: this

class PDFMerger {
constructor () {
this.reset()
Expand Down Expand Up @@ -54,20 +63,20 @@ class PDFMerger {
} catch (e) {
throw new Error(`This is not a valid url: ${input}`)
}
const res = await window.fetch(input)
const res = await globalObject.fetch(input)
const aBuffer = await res.arrayBuffer()
return new Uint8Array(aBuffer)
}

if (input instanceof window.File) {
const fileReader = new window.FileReader()
if (input instanceof globalObject.File) {
const fileReader = new globalObject.FileReader()
fileReader.onload = function (evt) {
return fileReader.result
}
fileReader.readAsArrayBuffer(input)
}

if (input instanceof window.Blob) {
if (input instanceof globalObject.Blob) {
const aBuffer = await input.arrayBuffer()
return new Uint8Array(aBuffer)
}
Expand Down Expand Up @@ -139,7 +148,7 @@ class PDFMerger {
async saveAsBlob () {
const buffer = await this.saveAsBuffer()

return new window.Blob([buffer], {
return new globalObject.Blob([buffer], {
type: 'application/pdf'
})
}
Expand Down

0 comments on commit 6736492

Please sign in to comment.