So im using canvas-constructor (dependent on Skia canvas) to do some image processing (instead of node-canvas due to incompatibility issues with sharp library)
Update : getting the same error while using canvas as well
Whenever i run my code locally, it runs flawlessly.
When i deploy it to AWS and run my lambda, I get the following error :
My Lambda code ( Running Nodejs14.x Not Working) :
let SERIALIZED = frameDom.serialize() //returns a string
SERIALIZED = SERIALIZED.replace('<html>','')
SERIALIZED = SERIALIZED.replace('</html>','')
SERIALIZED = SERIALIZED.replace('<head>','')
SERIALIZED = SERIALIZED.replace('</head>','')
SERIALIZED = SERIALIZED.replace('<body>','')
SERIALIZED = SERIALIZED.replace('</body>','')
sharp(Buffer.from(SERIALIZED,'utf-8')).toFormat("png").toBuffer(async (err, data, info) => {
//data is output file buffer
const image = await resolveImage(data);
let _canvas = new Canvas(97, 96).printImage(image, 0, 0, 97, 96)
const FINAL = convertImage(_canvas.canvas.getContext("2d").getImageData(0, 0, 97, 96))
})
My Code Locally (Windows 10 env Working) :
Note : test.svg was downloaded from my s3 bucket where frameDom.serialize() (in my lambda code) was the image body when it was stored
fs.readFile('./test.svg', 'utf8' , (err, image) => {
if (err)
throw new Error("Error openning frame")
image = image.replace('<html>','')
image = image.replace('</html>','')
image = image.replace('<head>','')
image = image.replace('</head>','')
image = image.replace('<body>','')
image = image.replace('</body>','')
sharp(Buffer.from(image,'utf-8')).toFormat("png").toBuffer(async (err, data, info) => {
//data is output file buffer
const image = await resolveImage(data);
let canvas = new Canvas(97, 96).printImage(image, 0, 0, 97, 96)
fs.writeFile('./new.svg', Ass.convertImage(canvas.canvas.getContext("2d").getImageData(0, 0, 97, 96)), err => {
if (err) {
console.error(err)
return
}
})
})
})
Seems like the modules need to be installed using an amazon linux OS. They still come out to be large. My only solution was to dockerize my lambda code , host it on ECR, then have my serverless application point to the image to use when a request is made to it