Unable to convert pdf to image on AWS Lambda - node.js

I have a Nodejs 8.10 lambda which converts pdf to png which was working fine untill few days ago.
This is the error which seems to be caused because of AWS update
https://aws.amazon.com/blogs/compute/upcoming-updates-to-the-aws-lambda-execution-environment/
{
Error: Command failed: convert -density 200 -quality 75 "/tmp/temp_file.pdf[0]" "/tmp/temp_file-0.png"
convert: unable to load module `/usr/lib64/ImageMagick-6.7.8/modules-Q16/coders/pdf.la': file not found # error/module.c/OpenModule/1278.
convert: no decode delegate for this image format `/tmp/temp_file.pdf' # error/constitute.c/ReadImage/544.
convert: no images defined `/tmp/temp_file-0.png' # error/convert.c/ConvertImageCommand/3046.
}
The error was resolved by adding this public layer which will stop working after July 22:
arn:aws:lambda:::awslayer:AmazonLinux1703
I tried creating ImageMagick Static Binaries for AWS Lambda.
I followed these instructions for ImageMagic version 6.9.10-5
https://gist.github.com/bensie/56f51bc33d4a55e2fc9a
https://imagemagick.org/download/ImageMagick-6.9.10-55.tar.gz
Folder structure for lambda:
I tried basic resize of png image:
var IM_PATH = "var/task/imagemagick/bin/";
process.env['LD_LIBRARY_PATH'] = 'var/task/imagemagick/lib/';
process.env['PATH'] = process.env['PATH'] + ':' + IM_PATH + ':' + process.env['LD_LIBRARY_PATH'];
var gm = require('gm').subClass({
imageMagick: true,
appPath: 'var/task/imagemagick/bin/',
});
gm('image.png')
.resize(240, 240, '!')
.write('/tmp/resize.png', function (err) {
if (!err) console.log('done');
else {
console.log(err);
}
});
Tried running on Node 8.10 and node 10.x lambdas and here the the errors:
1) Node 8.10
{ Error: Command failed: convert: UnableToOpenConfigureFile `delegates.xml' # warning/configure.c/GetConfigureOptions/677.
convert: NoDecodeDelegateForThisImageFormat `PNG' # error/constitute.c/ReadImage/560.
convert: NoImagesDefined `/tmp/resize.png' # error/convert.c/ConvertImageCommand/3235.
at ChildProcess.onExit (/var/task/node_modules/gm/lib/command.js:301:17)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:925:16)
at Socket.stream.socket.on (internal/child_process.js:346:11)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at Pipe._handle.close [as _onclose] (net.js:567:12) code: 1, signal: null }
2) Node 10.x
{ Error: Command failed: var/task/imagemagick/bin/convert: error while loading shared libraries: libgomp.so.1: cannot open shared object file: No such file or directory
at ChildProcess.onExit (/var/task/node_modules/gm/lib/command.js:301:17)
at ChildProcess.emit (events.js:189:13)
at ChildProcess.EventEmitter.emit (domain.js:441:20)
at maybeClose (internal/child_process.js:970:16)
at Socket.stream.socket.on (internal/child_process.js:389:11)
at Socket.emit (events.js:189:13)
at Socket.EventEmitter.emit (domain.js:441:20)
at Pipe._handle.close (net.js:597:12) code: 127, signal: null }

Related

Error when i try to convert file using imagemagick

I try to make a PDF file cover in JPG for this I use Node JS and the following libraries ImageMagick Graphicsmagick and Ghostcript.
Code:
root/controller/convertorController.js
const path = require('path');
const {exec} = require('child_process')
let baseDir = path.join(__dirname,'../tmp');
exports.customExecution = async(req,res,next)=>{
console.log(testDir)
exec(`magick convert ${testDir} -quality 100 awbtest.jpg`,(err,stdout,stderr)=>{
if(err){
console.log(err)
}else{
console.log('success')
next();
}
})
}
Project Structure
I checked the file path is correct if when I execute the function does nothing:
Error: Command failed: magick convert D:\Project\pdf-jpg convertor\tmp/test.pdf -quality 100 awbtest.jpg
convert: unable to open image 'D:\Project\pdf-jpg convertor\tmp/test.pdf': No such file
or directory # error/blob.c/OpenBlob/3570.
convert: no images defined `awbtest.jpg' # error/convert.c/ConvertImageCommand/3342.
at ChildProcess.exithandler (node:child_process:397:12)
at ChildProcess.emit (node:events:390:28)
at maybeClose (node:internal/child_process:1064:16)
at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) {
killed: false,
code: 1,
signal: null,
cmd: 'magick convert D:\Project\pdf-jpg convertor\tmp/test.pdf -quality 100
awbtest.jpg'
}

How to get PostgreSQL dump database file using Node JS

I tried to generate postgresql dump file using Node JS. SO, I used secure-backup library to generate pg-dump file encrypted. I add this package in my project and I configured .pem file also. When I execute this file it's throwing some error. but I couldn't understand what type error is throwing and how to fix it I don't know. Any one give some suggestion how to resolve this issue to generate pg dump file using Node JS otherwise different library used to generate pg dump file.
CODE
let secureBackup = require('secure-backup');
let pgHandler = require('secure-backup/lib/handlers/pg')
console.log('hi')
let backup = secureBackup({
pubKeyPath: 'C:/Users/DELL/Desktop/Twilio_Phone/cert.pem', // path to your public key
outputPath: 'C:/Users/DELL/Desktop/DB_Dump', // where to output your encrypted backup
compress: true, // enable compression (gzip)
handler: pgHandler({
user: 'postgres',
password: '*****',
database: '****'
})
})
backup() // can be invoked directly
// or used with a cron-job module like node-schedule
Error
events.js:292
throw er; // Unhandled 'error' event
^
Error: spawn pg_dump ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
at onErrorNT (internal/child_process.js:465:16)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
at onErrorNT (internal/child_process.js:465:16)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -4058,
code: 'ENOENT',
syscall: 'spawn pg_dump',
path: 'pg_dump',
spawnargs: [ '-U', 'postgres', 'HP', 'hummapi' ]
}

Firebase Auth testing with jest and emulator calling the login throws Error: Headers X-Client-Version forbidden

I try to login a dummy user with the firebase js sdk localy. I have the default firebase emulator running. After calling the function i get following exception:
Error: Headers X-Client-Version forbidden
at dispatchError (C:\Users\user\Documents\Projekte\Backend\functions\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:62:19)
at validCORSPreflightHeaders (C:\Users\user\Documents\Projekte\Backend\functions\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:99:5)
at Request.<anonymous> (C:\Users\user\Documents\Projekte\Backend\functions\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:367:12)
at Request.emit (events.js:315:20)
at Request.onRequestResponse (C:\Users\user\Documents\Projekte\Backend\functions\node_modules\request\request.js:1059:10)
at ClientRequest.emit (events.js:315:20)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:641:27)
at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
at Socket.socketOnData (_http_client.js:509:22)
at Socket.emit (events.js:315:20) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:65:53)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:99:5)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:367:12)
at Request.onRequestResponse (node_modules/request/request.js:1059:10)
console.log
t {
code: 'auth/network-request-failed',
message: 'A network error (such as timeout, interrupted connection or unreachable host) has occurred.',
a: null
}
at Object.<anonymous> (test/test.ts:275:12)
If i try to connect to my online project it works fine, but i want to perform my testing localy with the emulator.
Example Code:
const app = firebase.initializeApp(firebaseConfig);
app.auth().useEmulator("http://localhost:9099");
app.firestore().settings({
host: "localhost:8080",
ssl: false,
});
test('Example test case', async () => {
try {
const cred: UserCredential = await app.auth().signInWithEmailAndPassword("foo#bar.de", "bla2377");
expect(cred).toBeTruthy();
expect(cred.user).toBeTruthy();
} catch (e) {
console.log(e);
expect(true).toBeFalsy();
}
});
Enviorment Informations:
Operating System version: Windows 10 Home 10.0.18363 Build 18363
Firebase SDK version: 8.2.3
Jest Version: 26.6.3
Node Version: 14
jsdom doesn't support wildcard access-control-allow-headers, which are used by firebase. This answer fixes the exception.

fs.watch auto reload commands on discord bot

I have been working on my Discord Bot Rxiqi and I've been wanting auto-reload commands when the file has changed... my code is
var folder = "./commands/";
fs.watch(folder, { encoding: 'UTF-8' }, (eventType, filename) => {
if (eventType ==="change") {
console.log(`Updating Command: ${folder+filename}`)
delete require.cache[require.resolve(folder+filename)];
client.commands.delete(folder+filename);
const props = require(filename);
client.commands.set(folder+filename,props);
}
});
But I keep getting this console output:
Updating Command: ./commands/adverts.js
Error: Cannot find module './commands/adverts.js'
at Function.Module._resolveFilename (module.js:547:15)
at Function.resolve (internal/module.js:18:19)
at FSWatcher.fs.watch (E:\BotDevelopment\Rxiqi\status\ready.js:62:38)
at emitTwo (events.js:126:13)
at FSWatcher.emit (events.js:214:7)
at FSEvent.FSWatcher._handle.onchange (fs.js:1364:12)
Updating Command: ./commands/adverts.js
Error: Cannot find module './commands/adverts.js'
at Function.Module._resolveFilename (module.js:547:15)
at Function.resolve (internal/module.js:18:19)
at FSWatcher.fs.watch (E:\BotDevelopment\Rxiqi\status\ready.js:62:38)
at emitTwo (events.js:126:13)
at FSWatcher.emit (events.js:214:7)
at FSEvent.FSWatcher._handle.onchange (fs.js:1364:12)
Any help is very much appreciated :)
What OS are you on? If you're on windows there is a problem as your '/' will not work

Creating a child process of a bash file in node

Below I have a string with newlines line, this string is the contents of a bash file. I was wondering if there was a way to run this contents without creating the bash file. I would like to do something like below, where I just pipe the contents to the sh command. But this blew up with the error below.
let string = "\necho \'hello-world\'\n"
execAsync(`${string} | sh`)
I know I can create a temp file / exec it with the path equal to the current working dir / delete the file. But I'd rather not go through the trouble if there's a way to run the script without creating a file.
Error:
{ Error: Command failed:
echo 'hello-world'
| sh
/bin/sh: -c: line 2: syntax error near unexpected token `|'
/bin/sh: -c: line 2: ` | sh'
at ChildProcess.exithandler (child_process.js:207:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:852:16)
at Socket.<anonymous> (internal/child_process.js:323:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:492:12)
cause:
{ Error: Command failed:
echo 'hello-world'
| sh
/bin/sh: -c: line 2: syntax error near unexpected token `|'
/bin/sh: -c: line 2: ` | sh'
at ChildProcess.exithandler (child_process.js:207:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:852:16)
at Socket.<anonymous> (internal/child_process.js:323:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:492:12)
killed: false,
code: 2,
signal: null,
cmd: '\necho \'hello-world\'\n | sh' },
isOperational: true,
killed: false,
code: 2,
signal: null,
cmd: '\necho \'hello-world\'\n | sh' }
Pass the script contents to the shell through the -c option. It is supported by most shells.
Example:
var execAsync = require('exec-async');
let script = "echo \'hello\' $(whoami)\nls -l\necho 'bye'"
execAsync('sh', ['-c', script]).then(console.log).catch(console.log);

Resources