Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser: Add fetch for urls #54

Merged
merged 6 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class PDFMerger {
this._resetDoc()
}

add (inputFile, pages) {
async add (inputFile, pages) {
if (typeof pages === 'undefined' || pages === null) {
return this._addEntireDocument(inputFile, pages)
} else if (Array.isArray(pages)) {
Expand Down Expand Up @@ -39,19 +39,29 @@ class PDFMerger {
}

async _getInputFile (inputFile) {
return new Promise((resolve) => {
if (inputFile instanceof Buffer) {
resolve(inputFile)
} else {
const fileReader = new window.FileReader()

fileReader.onload = function (evt) {
resolve(fileReader.result)
}
if (inputFile instanceof Buffer || inputFile instanceof ArrayBuffer) {
return inputFile
}
if (typeof inputFile === 'string' || inputFile instanceof String) {
const res = await window.fetch(inputFile)
const ab = await res.arrayBuffer()
return ab
}
if (inputFile instanceof window.File) {
const fileReader = new window.FileReader()

fileReader.readAsArrayBuffer(inputFile)
fileReader.onload = function (evt) {
return fileReader.result
Copy link
Contributor

@Prinzhorn Prinzhorn Sep 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR broke the FileReader implementation, at least from looking at the code. This return doesn't do anything, because it's an anonymous function. The outer _getInputFile never gets the result and I think it will reach the throw at the bottom?

You can return new Promise(...) that was originally wrapping the whole thing just for this branch

Maybe also reject onerror, currently silently ignored

}
})

fileReader.readAsArrayBuffer(inputFile)
}

if (inputFile instanceof window.Blob) {
return await inputFile.arrayBuffer()
}

throw new Error('pdf must be represented as an ArrayBuffer, Blob, Buffer, File, or URL')
}

async _addEntireDocument (inputFile) {
Expand Down
14 changes: 10 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"devDependencies": {
"fs-extra": "^10.0.0",
"jest": "^27.0.6",
"jsdom": "^16.7.0",
"node-fetch": "^2.6.1",
"pdf-diff": "^0.1.1",
"standard": "^16.0.3"
}
Expand Down
Loading