node-pureimage
node-pureimage copied to clipboard
work in cloudflare workers
Hi,
I'm working on a image placeholder that run in cloudflare workers.
I made it works with 2 modifications.
One in index.js: replace import {PNG} from "pngjs"; by import {PNG} from "pngjs/browser.js;"
One in bitmap.js: replace this.data = Buffer.alloc(w*h*4); by this.data = new Uint8Array(w*h*4);
You can find my project here https://github.com/peernohell/worker-imageholder
Also, I had to import pureimage/src otherwise it import the browser version that didn't do anything.
I'm open to make a pull request but before I wanted to know what you want todo. because all test seems to work se we can just add them into the code.
But if you don't want to import the browser version of png an other solution could be to improve browser.js to detect that we are in a workers and then load png/browser and maybe split the code in index to not depend of the png and get it in parameters.
Why do you need the browser version of pngjs to have it work in a cloud flare worker. Aren't they running node?
No, they are running on V8 and implement web standard. They want to be close to webworker. As they say in this article
Workers is also built on V8 Isolates and empowers developers in a similar way by allowing you to create entire applications with only JavaScript — except your code runs across Cloudflare’s data centers in over 100 countries.
I've also done some work on my branch to get things compatible with Cloudflare Workers. Primarily adding more synchrnous APIs for working with in-memory data structures. These are inherently CPU bound operations so async/await doesn't yield any noticeable benefits and working with streams is a bit awkward in workers.
Official Worker support would be fantastic, been looking for a way to render simple og:images for social media right in our Pages project instead of needing to call another api. "Simplest" and hopefully quickest would be having a template image I load in, then dynamically adding text like the pages' title on top
So if PureImage worked inside of a WebWorker, would that meet your needs?
I think this should make it usable in Cloudflare Workers yes 👍🏼
Uh though CF Workers don't support canvas nor offscreen canvas unfortunately 😬