"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isPDF = exports.getMetaRegexMatch = exports.preparePDF = exports.getByteRange = exports.checkForSubFilter = void 0; const errors_1 = require("./errors"); const DEFAULT_BYTE_RANGE_PLACEHOLDER = "**********"; const checkForSubFilter = (pdfBuffer) => { const matches = pdfBuffer.toString().match(/\/SubFilter\s*\/([\w.]*)/); const subFilter = Array.isArray(matches) && matches[1]; if (!subFilter) { //SubFilter is only available if there is a sig in the pdf return; } const supportedTypes = [ "adbe.pkcs7.detached", "etsi.cades.detached", "etsi.rfc3161", ]; if (!supportedTypes.includes(subFilter.trim().toLowerCase())) { throw new errors_1.AppError(`Subfilter not supported - ${subFilter.trim().toLowerCase()}`); } }; exports.checkForSubFilter = checkForSubFilter; const getByteRange = (pdfBuffer) => { const byteRangeStrings = pdfBuffer .toString() .match(/\/ByteRange\s*\[{1}\s*(?:(?:\d*|\/\*{10})\s+){3}(?:\d+|\/\*{10}){1}\s*\]{1}/g); const byteRangePlaceholder = byteRangeStrings.find((s) => s.includes(`/${DEFAULT_BYTE_RANGE_PLACEHOLDER}`)); const strByteRanges = byteRangeStrings.map((brs) => brs.match(/[^[\s]*(?:\d|\/\*{10})/g)); const byteRanges = strByteRanges.map((n) => n.map(Number)); return { byteRangePlaceholder, byteRanges, }; }; exports.getByteRange = getByteRange; const preparePDF = (pdf) => { try { if (Buffer.isBuffer(pdf)) return pdf; return Buffer.from(pdf); } catch (error) { throw new errors_1.AppError(error); } }; exports.preparePDF = preparePDF; const getMetaRegexMatch = (keyName) => (str) => { const regex = new RegExp(`/${keyName}\\s*\\(([\\w.\\s@+~_\\-://,]*)`, "g"); const matches = [...str.matchAll(regex)]; const meta = matches.length ? matches[matches.length - 1][1] : null; return meta; }; exports.getMetaRegexMatch = getMetaRegexMatch; const isPDF = (buf) => { return (Buffer.isBuffer(buf) && buf.lastIndexOf("%PDF-") === 0 && buf.lastIndexOf("%%EOF") > -1); }; exports.isPDF = isPDF;