-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
requires ESM modules and dropping support for nodejs v16
- Loading branch information
Showing
11 changed files
with
87 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,31 @@ | ||
import { promises as fs } from "node:fs"; | ||
import { readFileSync } from "node:fs"; | ||
import { arrayBuffer } from "node:stream/consumers"; | ||
|
||
const PREFIX = "data:application/pdf;base64,"; | ||
|
||
async function streamToBuffer( | ||
readableStream: NodeJS.ReadableStream | ||
): Promise<Buffer> { | ||
const chunks: Buffer[] = []; | ||
for await (const chunk of readableStream) { | ||
chunks.push(chunk as Buffer); | ||
} | ||
return Buffer.concat(chunks); | ||
} | ||
|
||
export async function parseInput( | ||
input: string | Buffer | NodeJS.ReadableStream | ||
): Promise<Buffer | Uint8Array> { | ||
input: string | Uint8Array | Buffer | NodeJS.ReadableStream | ||
): Promise<Uint8Array> { | ||
// Buffer is a subclass of Uint8Array, but it's not actually | ||
// compatible: https://github.com/sindresorhus/uint8array-extras/issues/4 | ||
if (Buffer.isBuffer(input)) return Uint8Array.from(input); | ||
|
||
if (input instanceof Uint8Array) return input; | ||
|
||
// provided with a data url or a path to a file on disk | ||
if (typeof input === "string") { | ||
return input.startsWith(PREFIX) | ||
? Buffer.from(input.slice(PREFIX.length), "base64") | ||
: new Uint8Array(await fs.readFile(input)); | ||
if (input.startsWith(PREFIX)) { | ||
return Uint8Array.from(Buffer.from(input.slice(PREFIX.length), "base64")); | ||
} | ||
return new Uint8Array(readFileSync(input)); | ||
} | ||
|
||
// provided a buffer | ||
if (Buffer.isBuffer(input)) return input; | ||
|
||
// provided a ReadableStream (or any object with an asyncIterator that yields buffer chunks) | ||
if (typeof input === "object" && input && Symbol.asyncIterator in input) { | ||
return streamToBuffer(input); | ||
return new Uint8Array(await arrayBuffer(input)); | ||
} | ||
|
||
throw new Error( | ||
"pdf-to-img received an unexpected input. Provide a path to file, a data URL, a Buffer, or a ReadableStream." | ||
"pdf-to-img received an unexpected input. Provide a path to file, a data URL, a Uint8Array, a Buffer, or a ReadableStream." | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters