galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.14k stars 169 forks source link

Unable to recrypt files error when running in AWS Lambda #238

Open vinaynb opened 6 years ago

vinaynb commented 6 years ago

Hello I am using hummusjs in AWS Lambda environment (320mb ram) to protect pdf with password. For some of the pdf files i am getting below errors and lambda execution halts

"errorMessage": "Unable to recrypt files, check that input and output files are clear and arguments are coool",
   "errorType": "TypeError",
"stackTrace": [
       "TypeError (native)",
       "/var/task/index.js:77:16",
       "Promise._execute (/var/task/node_modules/bluebird/js/release/debuggability.js:303:9)",
       "Promise._resolveFromExecutor (/var/task/node_modules/bluebird/js/release/promise.js:483:18)",
       "new Promise (/var/task/node_modules/bluebird/js/release/promise.js:79:10)",
       "Object.exports.encryptPDF (/var/task/index.js:74:12)",
       "exports.getABBarcode.then.then (/var/task/index.js:35:28)",
       "tryCatcher (/var/task/node_modules/bluebird/js/release/util.js:16:23)",
       "Promise._settlePromiseFromHandler (/var/task/node_modules/bluebird/js/release/promise.js:512:31)",
       "Promise._settlePromise (/var/task/node_modules/bluebird/js/release/promise.js:569:18)",
       "Promise._settlePromise0 (/var/task/node_modules/bluebird/js/release/promise.js:614:10)",
       "Promise._settlePromises (/var/task/node_modules/bluebird/js/release/promise.js:693:18)",
       "Async._drainQueue (/var/task/node_modules/bluebird/js/release/async.js:133:16)",
       "Async._drainQueues (/var/task/node_modules/bluebird/js/release/async.js:143:10)",
       "Immediate.Async.drainQueues (/var/task/node_modules/bluebird/js/release/async.js:17:14)",
       "runCallback (timers.js:672:20)"
   ]

Below is the piece of code that uses hummus

return new Promise(function(resolve, reject) {
       let newPassword = crypto.createHash('sha256')
           .update('xxxxxx').digest('hex');
       hummus.recrypt(
           '/tmp/' + fileName + '_raw.pdf',
           '/tmp/' + fileName + '.pdf', {
               userPassword: newPassword,
               ownerPassword: newPassword
           });
       resolve(newPassword);
   });

I also checked the input and output arguments that are being passed to recrypt function and they have valid values. What's worth noticing is that this error occurs only in few files say out of 500 which indicates that ram availability is not an issue here.

I am having a hard time debugging the exact cause of this issue so maybe somehow can show me some pointers as to how should i go ahead fixing this.

Thanks !

chunyenHuang commented 6 years ago

How do you retrieve your pdf files for Lamdba? S3.get or payload? If your files are from payload, you may have to take a look about Lambda Limits. Your files may not exist in the /tmp.

You can check the files first~

if(!fs.existsSync(INPUT)) throw new Error('input does not exit');
if(fs.existsSync(OUTPUT)) fs.unlinkSync(OUTPUT)

// your codes here