Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Remove redundant jimp fetches (#6333)" #6339

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions packages/web/craco.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ export default {
test: /\.wasm$/,
type: 'webassembly/async'
},
{
test: /\.workerscript$/,
use: ['raw-loader'],
type: 'javascript/auto'
},
{
test: /\.(glsl|vs|fs|vert|frag)$/,
exclude: /node_modules/,
Expand Down
4 changes: 4 additions & 0 deletions packages/web/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
onLoad="window.web3Loaded = true; window.dispatchEvent(new CustomEvent('WEB3_LOADED'))"
src="%PUBLIC_URL%/scripts/web3.min.js"
></script>
<script
defer
src="%PUBLIC_URL%/scripts/jimp.min.js"
></script>

<script async type="text/javascript">
// Account recovery
Expand Down
9 changes: 4 additions & 5 deletions packages/web/src/services/WebWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ export default class WebWorker {
* Initializes a web worker for performing async tasks.
* @param {file} workerFile The web worker code, which is turned into a string an exec'd.
* @param {?boolean} terminateOnResult Whether or not to terminate the worker on gathering the result.
* @param {?Array<file>} dependencies Optional array of file dependencies they worker needs
* Note: Workers are non-trivial to spin up, so leaving commonly used workers running can be useful.
*/
constructor(workerFile, terminateOnResult = true, dependencies = []) {
constructor(workerFile, terminateOnResult = true) {
const code = workerFile.toString()
const dependencyCode = dependencies.map((d) => `${d.toString()};`).join()
const blob = new Blob([
`
${dependencyCode};
const importWorkerScript = ${importWorkScriptCode}
const code = ${code}
code()
`
])
this.worker = new Worker(URL.createObjectURL(blob))
if (typeof Worker !== 'undefined') {
this.worker = new Worker(URL.createObjectURL(blob))
}
this.terminateOnResult = terminateOnResult
}

Expand Down
15 changes: 5 additions & 10 deletions packages/web/src/utils/imageProcessingUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ import dominantColorWorkerFile from 'workers/dominantColor.worker.js'
import generatePlaylistArtworkWorkerFile from 'workers/generatePlaylistArtwork.worker.js'
import gifPreviewWorkerFile from 'workers/gifPreview.worker.js'
import resizeImageWorkerFile from 'workers/resizeImage.worker.js'
// @ts-ignore - jimp is a raw-loaded to have workers called directly with it.
import jimp from 'workers/utils/jimp.min.workerscript'

const averageRgbWorker = new WebWorker(averageRgbWorkerFile, false, [jimp])
const dominantColorWorker = new WebWorker(dominantColorWorkerFile, false, [
jimp
])
const gifPreviewWorker = new WebWorker(gifPreviewWorkerFile, false, [jimp])
const averageRgbWorker = new WebWorker(averageRgbWorkerFile, false)
const dominantColorWorker = new WebWorker(dominantColorWorkerFile, false)
const gifPreviewWorker = new WebWorker(gifPreviewWorkerFile, false)
const generatePlaylistArtworkWorker = new WebWorker(
generatePlaylistArtworkWorkerFile,
false,
[jimp]
false
)

export const ALLOWED_IMAGE_FILE_TYPES = [
Expand All @@ -37,7 +32,7 @@ export const resizeImage = async (
throw new Error('invalid file type')
}
const imageUrlBlob = URL.createObjectURL(imageFile)
const worker = new WebWorker(resizeImageWorkerFile, true, [jimp])
const worker = new WebWorker(resizeImageWorkerFile)
worker.call({ imageUrl: imageUrlBlob, maxWidth, square }, key)
return worker.getResult()
}
Expand Down
8 changes: 6 additions & 2 deletions packages/web/src/workers/averageRgb.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
const DEFAULT_RGB = { r: 13, g: 16, b: 18 }

export default () => {
const script = '/scripts/jimp.min.js'
// eslint-disable-next-line
importWorkerScript(script)

/**
* Returns the average RGB of an image to be used for backgrounds/shading, etc.
* @param {string} key identifies this computation
Expand All @@ -25,12 +29,12 @@ export default () => {
rgb.g = Math.floor(rgb.g / count)
rgb.b = Math.floor(rgb.b / count)
// eslint-disable-next-line
postMessage({ key, result: rgb })
postMessage({key, result: rgb})
})
.catch((err) => {
console.error(imageUrl, err)
// eslint-disable-next-line
postMessage({ key, result: DEFAULT_RGB })
postMessage({key, result: DEFAULT_RGB})
})
}

Expand Down
4 changes: 4 additions & 0 deletions packages/web/src/workers/dominantColor.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default () => {
// #CDC8C8, which works out to a luminance of 201.
const LUMINANCE_THRESHOLD = 201

const script = `/scripts/jimp.min.js`
// eslint-disable-next-line
importWorkerScript(script)

const clampedRGBColor = (rgbString /* string of 'r,g,b' */) => {
const rgb = rgbString.split(',').map((x) => parseInt(x, 10))
const r = rgb[0]
Expand Down
4 changes: 4 additions & 0 deletions packages/web/src/workers/generatePlaylistArtwork.worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* globals Jimp */

export default () => {
const script = `/scripts/jimp.min.js`
// eslint-disable-next-line
importWorkerScript(script)

/**
* Given 4 images, return a new image with each image taking up the four corners
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/web/src/workers/gifPreview.worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* globals Jimp */

export default () => {
const script = '/scripts/jimp.min.js'
// eslint-disable-next-line
importWorkerScript(script)

/**
* Returns a jpeg of a gif
* @param {string} key identifies this computation
Expand Down
3 changes: 3 additions & 0 deletions packages/web/src/workers/resizeImage.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

export default () => {
// eslint-disable-next-line
const jimp = '/scripts/jimp.min.js'
const exifParser = '/scripts/exif-parser-0.1.12-min.js'
// eslint-disable-next-line
importWorkerScript(jimp)
// eslint-disable-next-line
importWorkerScript(exifParser)

// Copied directly from Jimp.
Expand Down
1 change: 0 additions & 1 deletion packages/web/src/workers/utils/jimp.min.workerscript

This file was deleted.