Automattic / Automattic/node-canvas
registerFont is not reliable inside AWS Lambda
- Dominant language
- JavaScript
- Stars
- 10.7k
- Forks
- 1.2k
- Avg merge
- 4d 8h
- Merged PRs (30d)
- 1
Description
## Issue or Feature
I'm noticing that `registerFont` is not reliable inside AWS Lambda. What I've observed that if I call Lambda function half the time it would work and the other half it would not. I'm using the `/tmp` folder to temporally write the font files there so `registerFont` can load them. The error I'm saying is `Error: Could not parse font file at Object.registerFont`
## Steps to Reproduce
Here is the piece of code loading the font
```ts
import { createWriteStream } from 'fs';
import { join, resolve } from 'path';
import { registerFont } from 'canvas';
export default function nodeFontLoader(fonts: Font[], configs: Configs) {
const promises: Promise[] = [];
fonts.forEach(({ family, fontFileUrl }) => {
const fontFileName = `${family}.ttf`;
const fontFilePath = resolve(join(configs.baseWritePath, `/${fontFileName}`));
const promise = fetch(fontFileUrl)
.then((response) => {
if (!response.ok) {
throw new Error('failed to fetch font');
}
return new Promise((resolve, reject) => {
const dest = createWriteStream(fontFilePath, {});
response.body.pipe(dest);
response.body.on('end', () => resolve('it worked'));
dest.on('error', (error) => {
console.log('FAILED TO WRITE FONTS TO FOLDER');
reject(error);
});
});
})
.then(() => {
console.log('REGISTERING FONT.....');
registerFont(fontFilePath, { family });
console.log('REGISTERED FONT.....');
})
.catch((error) => {
console.log(error);
// THIS WHERE THE ERROR MESSAGE FIRE
throw new Error('failed to fetch font ' + family);
});
promises.push(promise);
});
return Promise.all(promises);
}
```
## Your Environment
* Version of node-canvas (output of `npm list canvas` or `yarn list canvas`): canvas@2.8.0
* Environment (e.g. node 4.2.0 on Mac OS X 10.8): Node 14.x , Lambda runtime
apologies in advance if the above is not enough to go on, this is really at the edge of my expertises.
Edit:
one more point I want to clarify is that I've already checked that if error happens because the preceding write operation has failed and turns out that is not the case. I checked by using `fs.existsSync` and listing the content of the `/tmp` folder and in both cases I can say the font file each time, before the call to `registerFont`
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.