From 68b4dc29a0c00547b9d034111ca9d8e2795841bd Mon Sep 17 00:00:00 2001 From: Josh McFarlin Date: Wed, 9 Feb 2022 15:15:52 -0500 Subject: [PATCH] refactor(server): replaced usage of Buffer with Uint8Array --- CHANGELOG.md | 7 ++ examples/basic/package-lock.json | 16 ++-- examples/cloudflare-wasm/package-lock.json | 16 ++-- examples/cloudflare/app/routes/index.tsx | 1 - examples/cloudflare/package-lock.json | 16 ++-- examples/gif-resizer/package-lock.json | 16 ++-- examples/sharp/package-lock.json | 16 ++-- package-lock.json | 83 ++++++++++++------- package.json | 6 +- rollup.config.js | 13 ++- src/server/caches/diskCache/diskCache.ts | 2 +- .../pureTransformer/handlers/bmp.ts | 38 ++++++--- .../pureTransformer/handlers/gif.ts | 6 +- .../pureTransformer/handlers/jpeg.ts | 4 +- tsconfig.json | 2 +- 15 files changed, 141 insertions(+), 101 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16c9e93..2459934 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Changed + +- Replaced `bmp-js` with `@wokwi/bmp-ts` for improved platform support. +- Replaced instances of `Buffer` with `Uint8Array` + ## [0.3.0] - 2022-02-09 ### Changed diff --git a/examples/basic/package-lock.json b/examples/basic/package-lock.json index b18569b..ea33dbe 100644 --- a/examples/basic/package-lock.json +++ b/examples/basic/package-lock.json @@ -116,11 +116,11 @@ } }, "../..": { - "version": "0.2.0", + "version": "0.3.0", "license": "MIT", "dependencies": { "@pdf-lib/upng": "^1.0.1", - "bmp-js": "^0.1.0", + "@wokwi/bmp-ts": "^3.0.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "get-rgba-palette": "^2.0.1", @@ -145,10 +145,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -200,8 +200,7 @@ "sass-loader": "^10.2.0", "style-loader": "^2.0.0", "ts-jest": "^27.1.0", - "typescript": "^4.5.2", - "yarn-or-npm": "^3.0.1" + "typescript": "^4.5.2" }, "peerDependencies": { "@cloudflare/kv-asset-handler": "^0.2.0", @@ -9548,10 +9547,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -9560,9 +9559,9 @@ "@types/utif": "^3.0.1", "@typescript-eslint/eslint-plugin": "^5.5.0", "@typescript-eslint/parser": "^5.5.0", + "@wokwi/bmp-ts": "^3.0.0", "autoprefixer": "^10.4.0", "babel-loader": "^8.2.3", - "bmp-js": "^0.1.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "commitizen": "^4.2.4", @@ -9612,8 +9611,7 @@ "style-loader": "^2.0.0", "ts-jest": "^27.1.0", "typescript": "^4.5.2", - "utif": "^3.1.0", - "yarn-or-npm": "^3.0.1" + "utif": "^3.1.0" } }, "resolve": { diff --git a/examples/cloudflare-wasm/package-lock.json b/examples/cloudflare-wasm/package-lock.json index 5ac50fc..d7b4e80 100644 --- a/examples/cloudflare-wasm/package-lock.json +++ b/examples/cloudflare-wasm/package-lock.json @@ -126,11 +126,11 @@ } }, "../..": { - "version": "0.2.0", + "version": "0.3.0", "license": "MIT", "dependencies": { "@pdf-lib/upng": "^1.0.1", - "bmp-js": "^0.1.0", + "@wokwi/bmp-ts": "^3.0.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "get-rgba-palette": "^2.0.1", @@ -155,10 +155,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -210,8 +210,7 @@ "sass-loader": "^10.2.0", "style-loader": "^2.0.0", "ts-jest": "^27.1.0", - "typescript": "^4.5.2", - "yarn-or-npm": "^3.0.1" + "typescript": "^4.5.2" }, "peerDependencies": { "@cloudflare/kv-asset-handler": "^0.2.0", @@ -8131,10 +8130,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -8143,9 +8142,9 @@ "@types/utif": "^3.0.1", "@typescript-eslint/eslint-plugin": "^5.5.0", "@typescript-eslint/parser": "^5.5.0", + "@wokwi/bmp-ts": "^3.0.0", "autoprefixer": "^10.4.0", "babel-loader": "^8.2.3", - "bmp-js": "^0.1.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "commitizen": "^4.2.4", @@ -8195,8 +8194,7 @@ "style-loader": "^2.0.0", "ts-jest": "^27.1.0", "typescript": "^4.5.2", - "utif": "^3.1.0", - "yarn-or-npm": "^3.0.1" + "utif": "^3.1.0" } }, "resolve": { diff --git a/examples/cloudflare/app/routes/index.tsx b/examples/cloudflare/app/routes/index.tsx index ea4ee9a..d2ab0ae 100644 --- a/examples/cloudflare/app/routes/index.tsx +++ b/examples/cloudflare/app/routes/index.tsx @@ -41,7 +41,6 @@ const IndexPage: React.FC = () => ( src={img.src} loaderUrl="/api/image" responsive={img.responsive} - //width={img.responsive[0].size.width} /> ))} diff --git a/examples/cloudflare/package-lock.json b/examples/cloudflare/package-lock.json index baf7e8f..7c370d8 100644 --- a/examples/cloudflare/package-lock.json +++ b/examples/cloudflare/package-lock.json @@ -119,11 +119,11 @@ } }, "../..": { - "version": "0.2.0", + "version": "0.3.0", "license": "MIT", "dependencies": { "@pdf-lib/upng": "^1.0.1", - "bmp-js": "^0.1.0", + "@wokwi/bmp-ts": "^3.0.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "get-rgba-palette": "^2.0.1", @@ -148,10 +148,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -203,8 +203,7 @@ "sass-loader": "^10.2.0", "style-loader": "^2.0.0", "ts-jest": "^27.1.0", - "typescript": "^4.5.2", - "yarn-or-npm": "^3.0.1" + "typescript": "^4.5.2" }, "peerDependencies": { "@cloudflare/kv-asset-handler": "^0.2.0", @@ -7593,10 +7592,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -7605,9 +7604,9 @@ "@types/utif": "^3.0.1", "@typescript-eslint/eslint-plugin": "^5.5.0", "@typescript-eslint/parser": "^5.5.0", + "@wokwi/bmp-ts": "^3.0.0", "autoprefixer": "^10.4.0", "babel-loader": "^8.2.3", - "bmp-js": "^0.1.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "commitizen": "^4.2.4", @@ -7657,8 +7656,7 @@ "style-loader": "^2.0.0", "ts-jest": "^27.1.0", "typescript": "^4.5.2", - "utif": "^3.1.0", - "yarn-or-npm": "^3.0.1" + "utif": "^3.1.0" } }, "resolve": { diff --git a/examples/gif-resizer/package-lock.json b/examples/gif-resizer/package-lock.json index afe713b..048e565 100644 --- a/examples/gif-resizer/package-lock.json +++ b/examples/gif-resizer/package-lock.json @@ -29,11 +29,11 @@ } }, "../..": { - "version": "0.2.0", + "version": "0.3.0", "license": "MIT", "dependencies": { "@pdf-lib/upng": "^1.0.1", - "bmp-js": "^0.1.0", + "@wokwi/bmp-ts": "^3.0.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "get-rgba-palette": "^2.0.1", @@ -58,10 +58,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -113,8 +113,7 @@ "sass-loader": "^10.2.0", "style-loader": "^2.0.0", "ts-jest": "^27.1.0", - "typescript": "^4.5.2", - "yarn-or-npm": "^3.0.1" + "typescript": "^4.5.2" }, "peerDependencies": { "@cloudflare/kv-asset-handler": "^0.2.0", @@ -9518,10 +9517,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -9530,9 +9529,9 @@ "@types/utif": "^3.0.1", "@typescript-eslint/eslint-plugin": "^5.5.0", "@typescript-eslint/parser": "^5.5.0", + "@wokwi/bmp-ts": "^3.0.0", "autoprefixer": "^10.4.0", "babel-loader": "^8.2.3", - "bmp-js": "^0.1.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "commitizen": "^4.2.4", @@ -9582,8 +9581,7 @@ "style-loader": "^2.0.0", "ts-jest": "^27.1.0", "typescript": "^4.5.2", - "utif": "^3.1.0", - "yarn-or-npm": "^3.0.1" + "utif": "^3.1.0" } }, "resolve": { diff --git a/examples/sharp/package-lock.json b/examples/sharp/package-lock.json index bbb6507..9335690 100644 --- a/examples/sharp/package-lock.json +++ b/examples/sharp/package-lock.json @@ -29,11 +29,11 @@ } }, "../..": { - "version": "0.2.0", + "version": "0.3.0", "license": "MIT", "dependencies": { "@pdf-lib/upng": "^1.0.1", - "bmp-js": "^0.1.0", + "@wokwi/bmp-ts": "^3.0.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "get-rgba-palette": "^2.0.1", @@ -58,10 +58,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -113,8 +113,7 @@ "sass-loader": "^10.2.0", "style-loader": "^2.0.0", "ts-jest": "^27.1.0", - "typescript": "^4.5.2", - "yarn-or-npm": "^3.0.1" + "typescript": "^4.5.2" }, "peerDependencies": { "@cloudflare/kv-asset-handler": "^0.2.0", @@ -9599,10 +9598,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -9611,9 +9610,9 @@ "@types/utif": "^3.0.1", "@typescript-eslint/eslint-plugin": "^5.5.0", "@typescript-eslint/parser": "^5.5.0", + "@wokwi/bmp-ts": "^3.0.0", "autoprefixer": "^10.4.0", "babel-loader": "^8.2.3", - "bmp-js": "^0.1.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "commitizen": "^4.2.4", @@ -9663,8 +9662,7 @@ "style-loader": "^2.0.0", "ts-jest": "^27.1.0", "typescript": "^4.5.2", - "utif": "^3.1.0", - "yarn-or-npm": "^3.0.1" + "utif": "^3.1.0" } }, "resolve": { diff --git a/package-lock.json b/package-lock.json index b6aac9a..c04767a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "@pdf-lib/upng": "^1.0.1", - "bmp-js": "^0.1.0", + "@wokwi/bmp-ts": "^3.0.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "get-rgba-palette": "^2.0.1", @@ -35,10 +35,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", @@ -3030,6 +3030,19 @@ "rollup": "^2.42.0" } }, + "node_modules/@rollup/plugin-replace": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@rollup/plugin-replace/-/plugin-replace-3.0.1.tgz", + "integrity": "sha512-989J5oRzf3mm0pO/0djTijdfEh9U3n63BIXN5X7T4U9BP+fN4oxQ6DvDuBvFaHA6scaHQRclqmKQEkBhB7k7Hg==", + "dev": true, + "dependencies": { + "@rollup/pluginutils": "^3.1.0", + "magic-string": "^0.25.7" + }, + "peerDependencies": { + "rollup": "^1.20.0 || ^2.0.0" + } + }, "node_modules/@rollup/pluginutils": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-3.1.0.tgz", @@ -3417,15 +3430,6 @@ "dev": true, "peer": true }, - "node_modules/@types/bmp-js": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/@types/bmp-js/-/bmp-js-0.1.0.tgz", - "integrity": "sha512-uMU85ROcmlY1f4mVPTlNodRXa6Z5f0AIxvv5b0pvjty3KNg7ljf5lNSspHgaF6iFDCiGpLQmJna+VwEpUC9TyA==", - "dev": true, - "dependencies": { - "@types/node": "*" - } - }, "node_modules/@types/busboy": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/@types/busboy/-/busboy-0.3.2.tgz", @@ -4168,6 +4172,19 @@ "@xtuc/long": "4.2.2" } }, + "node_modules/@wokwi/bmp-ts": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@wokwi/bmp-ts/-/bmp-ts-3.0.0.tgz", + "integrity": "sha512-fBoRZQEd8RHdFq4gaS6eU69fYZjqGIz8myYPLm0gJSosxqHWpge8/3+6yCyc8b8afcb/ptjSlnEVP93t00fWDQ==", + "dependencies": { + "@types/node": "^10.11.7" + } + }, + "node_modules/@wokwi/bmp-ts/node_modules/@types/node": { + "version": "10.17.60", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.60.tgz", + "integrity": "sha512-F0KIgDJfy2nA3zMLmWGKxcH2ZVEtCZXHHdOQs2gSaQ27+lNeEfGxzkIw90aXswATX7AZ33tahPbzy6KAfUreVw==" + }, "node_modules/@xtuc/ieee754": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz", @@ -5135,11 +5152,6 @@ "blob": "0.0.4" } }, - "node_modules/bmp-js": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/bmp-js/-/bmp-js-0.1.0.tgz", - "integrity": "sha1-4Fpj95amwf8l9Hcex62twUjAcjM=" - }, "node_modules/bn.js": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-5.2.0.tgz", @@ -22314,6 +22326,16 @@ "resolve": "^1.19.0" } }, + "@rollup/plugin-replace": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@rollup/plugin-replace/-/plugin-replace-3.0.1.tgz", + "integrity": "sha512-989J5oRzf3mm0pO/0djTijdfEh9U3n63BIXN5X7T4U9BP+fN4oxQ6DvDuBvFaHA6scaHQRclqmKQEkBhB7k7Hg==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^3.1.0", + "magic-string": "^0.25.7" + } + }, "@rollup/pluginutils": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-3.1.0.tgz", @@ -22620,15 +22642,6 @@ "dev": true, "peer": true }, - "@types/bmp-js": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/@types/bmp-js/-/bmp-js-0.1.0.tgz", - "integrity": "sha512-uMU85ROcmlY1f4mVPTlNodRXa6Z5f0AIxvv5b0pvjty3KNg7ljf5lNSspHgaF6iFDCiGpLQmJna+VwEpUC9TyA==", - "dev": true, - "requires": { - "@types/node": "*" - } - }, "@types/busboy": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/@types/busboy/-/busboy-0.3.2.tgz", @@ -23263,6 +23276,21 @@ "@xtuc/long": "4.2.2" } }, + "@wokwi/bmp-ts": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@wokwi/bmp-ts/-/bmp-ts-3.0.0.tgz", + "integrity": "sha512-fBoRZQEd8RHdFq4gaS6eU69fYZjqGIz8myYPLm0gJSosxqHWpge8/3+6yCyc8b8afcb/ptjSlnEVP93t00fWDQ==", + "requires": { + "@types/node": "^10.11.7" + }, + "dependencies": { + "@types/node": { + "version": "10.17.60", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.60.tgz", + "integrity": "sha512-F0KIgDJfy2nA3zMLmWGKxcH2ZVEtCZXHHdOQs2gSaQ27+lNeEfGxzkIw90aXswATX7AZ33tahPbzy6KAfUreVw==" + } + } + }, "@xtuc/ieee754": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz", @@ -23996,11 +24024,6 @@ "blob": "0.0.4" } }, - "bmp-js": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/bmp-js/-/bmp-js-0.1.0.tgz", - "integrity": "sha1-4Fpj95amwf8l9Hcex62twUjAcjM=" - }, "bn.js": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-5.2.0.tgz", diff --git a/package.json b/package.json index 314a875..67cd30d 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "url": "https://github.com/Josh-McFarlin/remix-image/issues" }, "main": "build/index.js", - "types": "build/index.d.ts", + "types": "build/src/index.d.ts", "style": "build/styles.css", "exports": { ".": "./build/index.js", @@ -48,7 +48,7 @@ }, "dependencies": { "@pdf-lib/upng": "^1.0.1", - "bmp-js": "^0.1.0", + "@wokwi/bmp-ts": "^3.0.0", "buffer": "^6.0.3", "clsx": "^1.1.1", "get-rgba-palette": "^2.0.1", @@ -73,10 +73,10 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.6", + "@rollup/plugin-replace": "^3.0.1", "@testing-library/jest-dom": "^5.16.0", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", - "@types/bmp-js": "^0.1.0", "@types/jest": "^27.0.3", "@types/omggif": "^1.0.2", "@types/pngjs": "^6.0.1", diff --git a/rollup.config.js b/rollup.config.js index d04384e..47a0dbe 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -5,7 +5,8 @@ import commonjs from "@rollup/plugin-commonjs"; import json from "@rollup/plugin-json"; import typescript from "rollup-plugin-typescript2"; import postcss from "rollup-plugin-postcss"; -import nodePolyfills from "rollup-plugin-polyfill-node"; +import { terser } from "rollup-plugin-terser"; +import replace from "@rollup/plugin-replace"; export default [ { @@ -49,6 +50,7 @@ export default [ }), resolve({ preferBuiltins: false }), commonjs(), + terser(), ], }, { @@ -68,9 +70,14 @@ export default [ }), resolve({ preferBuiltins: false }), commonjs(), - nodePolyfills({ - include: null, + replace({ + preventAssignment: true, + include: ["node_modules/jpeg-js/**/*.js"], + values: { + "Buffer.from": "new Uint8Array", + }, }), + terser(), ], }, ]; diff --git a/src/server/caches/diskCache/diskCache.ts b/src/server/caches/diskCache/diskCache.ts index e20a535..96d7557 100644 --- a/src/server/caches/diskCache/diskCache.ts +++ b/src/server/caches/diskCache/diskCache.ts @@ -46,7 +46,7 @@ export class DiskCache extends Cache { } async set(key: string, resultImg: Uint8Array): Promise { - await this.cache.set(key, Buffer.from(resultImg)); + await this.cache.set(key, resultImg as Buffer); } async clear(): Promise { diff --git a/src/server/transformers/pureTransformer/handlers/bmp.ts b/src/server/transformers/pureTransformer/handlers/bmp.ts index aad0094..18fa273 100644 --- a/src/server/transformers/pureTransformer/handlers/bmp.ts +++ b/src/server/transformers/pureTransformer/handlers/bmp.ts @@ -1,26 +1,38 @@ -import * as BMPJS from "bmp-js"; +import BMPJS from "@wokwi/bmp-ts"; import { ImageHandler } from "../types"; +const rgbaToABGR = (pixels: Uint8Array): Uint8Array => { + const len = pixels.length; + const newImage = new Uint8Array(len); + + for (let i = 0; i < len; i += 4) { + newImage[i] = pixels[i + 3]; + newImage[i + 1] = pixels[i + 2]; + newImage[i + 2] = pixels[i + 1]; + newImage[i + 3] = pixels[i]; + } + + return newImage; +}; + export const BmpHandler: ImageHandler = { async decode(buffer) { - const image = BMPJS.decode(Buffer.from(buffer)); + const image = BMPJS.decode(buffer as Buffer, { toRGBA: true }); return { width: image.width, height: image.height, - data: new Uint8Array(image.data), + data: image.data, }; }, - async encode(image, options) { - const bmpImageData = BMPJS.encode( - { - width: image.width, - height: image.height, - data: Buffer.from(image.data), - }, - options.quality - ); + async encode(image) { + const bmpImageData = BMPJS.encode({ + bitPP: 24, + width: image.width, + height: image.height, + data: rgbaToABGR(image.data), + }); - return new Uint8Array(bmpImageData.data); + return bmpImageData.data; }, }; diff --git a/src/server/transformers/pureTransformer/handlers/gif.ts b/src/server/transformers/pureTransformer/handlers/gif.ts index 16d7b23..dc56ceb 100644 --- a/src/server/transformers/pureTransformer/handlers/gif.ts +++ b/src/server/transformers/pureTransformer/handlers/gif.ts @@ -4,7 +4,7 @@ import { ImageHandler } from "../types"; export const GifHandler: ImageHandler = { async decode(buffer) { - const image = new GIFJS.GifReader(Buffer.from(buffer)); + const image = new GIFJS.GifReader(buffer as Buffer); const dest = { width: image.width, @@ -19,7 +19,9 @@ export const GifHandler: ImageHandler = { async encode(image, options) { const frames = [image.data]; - const buf = new Buffer(image.width * image.height * frames.length + 1024); + const buf = new Uint8Array( + image.width * image.height * frames.length + 1024 + ); const gifImageData = new GIFJS.GifWriter( buf as Buffer, diff --git a/src/server/transformers/pureTransformer/handlers/jpeg.ts b/src/server/transformers/pureTransformer/handlers/jpeg.ts index 0f7c850..1be8a4c 100644 --- a/src/server/transformers/pureTransformer/handlers/jpeg.ts +++ b/src/server/transformers/pureTransformer/handlers/jpeg.ts @@ -3,12 +3,12 @@ import { ImageHandler } from "../types"; export const JpegHandler: ImageHandler = { async decode(buffer) { - const image = jpeg.decode(buffer); + const image = jpeg.decode(buffer, { useTArray: true }); return { width: image.width, height: image.height, - data: new Uint8Array(image.data), + data: image.data, }; }, async encode(image, options) { diff --git a/tsconfig.json b/tsconfig.json index 238398a..175a9f0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,6 +19,6 @@ "experimentalDecorators": true, "forceConsistentCasingInFileNames": true }, - "include": ["src/**/*", "./jest.setup.ts"], + "include": ["src/**/*"], "exclude": ["node_modules", "build"] }