UNPKG

@nova-slides/pdf-to-img

Version:

📃📸 Converts PDFs to images in nodejs

64 lines (63 loc) • 3.35 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseInput = void 0; const node_fs_1 = require("node:fs"); const PREFIX = "data:application/pdf;base64,"; function streamToBuffer(readableStream) { var _a, readableStream_1, readableStream_1_1; var _b, e_1, _c, _d; return __awaiter(this, void 0, void 0, function* () { const chunks = []; try { for (_a = true, readableStream_1 = __asyncValues(readableStream); readableStream_1_1 = yield readableStream_1.next(), _b = readableStream_1_1.done, !_b; _a = true) { _d = readableStream_1_1.value; _a = false; const chunk = _d; chunks.push(chunk); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_a && !_b && (_c = readableStream_1.return)) yield _c.call(readableStream_1); } finally { if (e_1) throw e_1.error; } } return Buffer.concat(chunks); }); } function parseInput(input) { return __awaiter(this, void 0, void 0, function* () { // 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(yield node_fs_1.promises.readFile(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); } throw new Error("pdf-to-img received an unexpected input. Provide a path to file, a data URL, a Buffer, or a ReadableStream."); }); } exports.parseInput = parseInput;