Skip to content

Commit

Permalink
feat(png): polyfill ImageData api for cloudflare worker env
Browse files Browse the repository at this point in the history
  • Loading branch information
jamsinclair committed Jul 8, 2023
1 parent 52ea8b8 commit 2ab483a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/png/codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"squoosh_png.js",
"squoosh_png.d.ts"
],
"scripts": {
"patch-pre-script": "cat pre.js >> squoosh_png.js"
},
"module": "squoosh_png.js",
"types": "squoosh_png.d.ts",
"sideEffects": false
}
}
15 changes: 15 additions & 0 deletions packages/png/codec/pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function () {
const isRunningInCloudFlareWorkers = caches.default !== undefined;
if (isRunningInCloudFlareWorkers) {
if (!globalThis.ImageData) {
// Simple Polyfill for ImageData Object
globalThis.ImageData = class ImageData {
constructor(data, width, height) {
this.data = data;
this.width = width;
this.height = height;
}
};
}
}
})();
15 changes: 15 additions & 0 deletions packages/png/codec/squoosh_png.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,18 @@ async function init(input) {

export default init;

(function () {
const isRunningInCloudFlareWorkers = caches.default !== undefined;
if (isRunningInCloudFlareWorkers) {
if (!globalThis.ImageData) {
// Simple Polyfill for ImageData Object
globalThis.ImageData = class ImageData {
constructor(data, width, height) {
this.data = data;
this.width = width;
this.height = height;
}
};
}
}
})();

0 comments on commit 2ab483a

Please sign in to comment.