Skip to content

Commit 1a649da

Browse files
authored
chore(dev-deps): Bump jimp from 0.22.10 to 1.6.0 (#577)
This bumps `jimp` from `0.22.10` to `1.6.0` and handles the breaking changes. This removes the dependency on `phin`, which resolves this Dependabot alert: https://github.com/MetaMask/snaps-directory/security/dependabot/18
1 parent a33c9a3 commit 1a649da

8 files changed

Lines changed: 381 additions & 447 deletions

File tree

.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ module.exports = {
5555
'@metamask/eslint-config-nodejs',
5656
'plugin:jest-dom/recommended',
5757
],
58+
59+
rules: {
60+
'jsdoc/check-tag-names': [
61+
'error',
62+
{
63+
definedTags: ['jest-environment'],
64+
},
65+
],
66+
},
5867
},
5968

6069
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
diff --git a/dist/commonjs/load-font.js b/dist/commonjs/load-font.js
2+
index 7c158d32db4965d7b6c739c6ed2382251232b808..dad1ff91af23a661eefdc5d074cb61791c98e512 100644
3+
--- a/dist/commonjs/load-font.js
4+
+++ b/dist/commonjs/load-font.js
5+
@@ -2,6 +2,7 @@
6+
Object.defineProperty(exports, "__esModule", { value: true });
7+
exports.loadFont = loadFont;
8+
const load_bitmap_font_js_1 = require("./load-bitmap-font.js");
9+
+const _fs = require("fs");
10+
/**
11+
* Loads a Bitmap Font from a file.
12+
* @param file A path or URL to a font file
13+
@@ -20,7 +21,7 @@ const load_bitmap_font_js_1 = require("./load-bitmap-font.js");
14+
async function loadFont(file) {
15+
let fileOrBuffer = file;
16+
if (typeof window === "undefined" && !load_bitmap_font_js_1.isWebWorker) {
17+
- const { existsSync, promises: fs } = await import("fs");
18+
+ const { existsSync, promises: fs } = _fs;
19+
if (existsSync(file)) {
20+
fileOrBuffer = await fs.readFile(file);
21+
}

jest.setup.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ beforeEach(() => {
3232
originalConsoleError(error, ...args);
3333
});
3434

35-
const realAddEventListener = window?.addEventListener?.bind?.(window);
35+
const realAddEventListener =
36+
typeof window !== 'undefined' && window?.addEventListener?.bind?.(window);
3637

3738
// Return window.ethereum for EIP6963 when applicable.
3839
Object.assign(globalThis, 'window', {
@@ -54,7 +55,7 @@ beforeEach(() => {
5455
},
5556
}),
5657
);
57-
} else {
58+
} else if (realAddEventListener) {
5859
realAddEventListener(type, listener);
5960
}
6061
}),

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
},
4343
"resolutions": {
4444
"@chakra-ui/focus-lock@2.1.0": "patch:@chakra-ui/focus-lock@npm%3A2.1.0#./.yarn/patches/@chakra-ui-focus-lock-npm-2.1.0-46e41f49ee.patch",
45+
"@jimp/plugin-print@1.6.0": "patch:@jimp/plugin-print@npm%3A1.6.0#./.yarn/patches/@jimp-plugin-print-npm-1.6.0-66dc34fcee.patch",
4546
"@noble/hashes@1.3.1": "patch:@noble/hashes@npm%3A1.3.2#./.yarn/patches/@noble-hashes-npm-1.3.2-1e619f9da0.patch",
4647
"@noble/hashes@^1.0.0": "patch:@noble/hashes@npm%3A1.3.2#./.yarn/patches/@noble-hashes-npm-1.3.2-1e619f9da0.patch",
4748
"@noble/hashes@^1.3.1": "patch:@noble/hashes@npm%3A1.3.2#./.yarn/patches/@noble-hashes-npm-1.3.2-1e619f9da0.patch",
@@ -141,7 +142,7 @@
141142
"jest-image-snapshot": "^6.2.0",
142143
"jest-it-up": "^2.0.2",
143144
"jest-when": "^3.6.0",
144-
"jimp": "^0.22.10",
145+
"jimp": "^1.6.0",
145146
"lint-staged": "^16.1.2",
146147
"nanoid": "^3.3.7",
147148
"node-fetch-cache": "^3.1.3",

src/utils/seo/images.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
15
import { configureToMatchImageSnapshot } from 'jest-image-snapshot';
26
import { resolve } from 'path';
37

src/utils/seo/images.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable import/no-nodejs-modules, no-restricted-globals */
22

33
import assert from 'assert';
4-
import Jimp from 'jimp';
4+
import { Jimp, loadFont, measureText, measureTextHeight } from 'jimp';
55
import shuffle from 'lodash/shuffle';
66
import sharp, { type OverlayOptions } from 'sharp';
77

@@ -41,16 +41,21 @@ export async function createFallbackIcon(
4141
size = 256,
4242
fontPath = 'fonts/icon/grey.fnt',
4343
) {
44-
const image = await Jimp.create(size, size);
45-
const font = await Jimp.loadFont(getAssetPath(fontPath));
44+
const image = new Jimp({ width: size, height: size });
45+
const font = await loadFont(getAssetPath(fontPath));
4646

47-
const letterX = (size - Jimp.measureText(font, text)) / 2;
48-
const letterY = (size - Jimp.measureTextHeight(font, text, size)) / 2;
47+
const letterX = (size - measureText(font, text)) / 2;
48+
const letterY = (size - measureTextHeight(font, text, size)) / 2;
4949

50-
image.print(font, letterX, letterY, text);
50+
image.print({
51+
font,
52+
text,
53+
x: letterX,
54+
y: letterY,
55+
});
5156

5257
const icon = getImage('icon');
53-
const input = await image.getBufferAsync(Jimp.MIME_PNG);
58+
const input = await image.getBuffer('image/png');
5459

5560
return icon
5661
.resize(size, size)
@@ -182,7 +187,7 @@ export async function createSnapImage(
182187

183188
// The name may take two lines, so we need to calculate the height of the
184189
// name to determine the top of the author text.
185-
const nameHeight = Jimp.measureTextHeight(black, name, FONT_MAX_WIDTH);
190+
const nameHeight = measureTextHeight(black, name, FONT_MAX_WIDTH);
186191
const authorTop = 85 + nameHeight;
187192

188193
background.composite([

src/utils/seo/utils.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable import/no-nodejs-modules, no-restricted-globals */
22

3-
import Jimp from 'jimp';
3+
import { Jimp, JimpMime, loadFont, measureText } from 'jimp';
44
import { resolve } from 'path';
55
import sharp from 'sharp';
66

77
import { BASE_SEO_DIRECTORY, CANVAS_WIDTH, JIMP_FONTS } from './constants';
88
import type { Fields } from '../snaps';
99

10-
export type Font = Awaited<ReturnType<typeof Jimp.loadFont>>;
10+
export type Font = Awaited<ReturnType<typeof loadFont>>;
1111

1212
export type Snap = Fields<Queries.Snap, 'name'> & { icon?: string };
1313
export type SnapWithIcon = Snap & { icon: string };
@@ -21,14 +21,11 @@ export type SnapWithIcon = Snap & { icon: string };
2121
*/
2222
export async function getFonts(name: string, author = '') {
2323
for (const { maxWidth, black, grey } of JIMP_FONTS) {
24-
const font = await Jimp.loadFont(black);
25-
const width = Math.max(
26-
Jimp.measureText(font, name),
27-
Jimp.measureText(font, author),
28-
);
24+
const font = await loadFont(black);
25+
const width = Math.max(measureText(font, name), measureText(font, author));
2926

3027
if (width <= maxWidth) {
31-
return Promise.all([Jimp.loadFont(black), Jimp.loadFont(grey)]);
28+
return Promise.all([loadFont(black), loadFont(grey)]);
3229
}
3330
}
3431

@@ -76,10 +73,16 @@ export function normalizeName(name: string) {
7673
* @returns The buffer of the rendered image.
7774
*/
7875
export async function getText(value: string, font: Font) {
79-
const image = await Jimp.create(700, 300);
80-
image.print(font, 0, 0, normalizeName(value), 700);
76+
const image = new Jimp({ width: 700, height: 300 });
77+
image.print({
78+
font,
79+
x: 0,
80+
y: 0,
81+
text: normalizeName(value),
82+
maxWidth: 700,
83+
});
8184

82-
return image.getBufferAsync(Jimp.MIME_PNG);
85+
return image.getBuffer(JimpMime.png);
8386
}
8487

8588
/**

0 commit comments

Comments
 (0)