node-pureimage icon indicating copy to clipboard operation
node-pureimage copied to clipboard

work in cloudflare workers

Open peernohell opened this issue 3 years ago • 7 comments

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.

peernohell avatar Feb 13 '22 19:02 peernohell

Why do you need the browser version of pngjs to have it work in a cloud flare worker. Aren't they running node?

joshmarinacci avatar Jun 26 '22 17:06 joshmarinacci

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.

peernohell avatar Jun 28 '22 10:06 peernohell

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.

andrewmd5 avatar Sep 18 '22 11:09 andrewmd5

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

CanRau avatar Jan 28 '23 03:01 CanRau

So if PureImage worked inside of a WebWorker, would that meet your needs?

joshmarinacci avatar Feb 06 '23 22:02 joshmarinacci

I think this should make it usable in Cloudflare Workers yes 👍🏼

CanRau avatar Feb 07 '23 02:02 CanRau

Uh though CF Workers don't support canvas nor offscreen canvas unfortunately 😬

CanRau avatar Feb 11 '23 16:02 CanRau