Skip to content

Commit

Permalink
fix: update file-type to disable eval dynamic code errors during build
Browse files Browse the repository at this point in the history
  • Loading branch information
rxliuli committed Feb 19, 2025
1 parent b6b0e41 commit afa085e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 67 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@jimp/utils": "workspace:*",
"await-to-js": "^3.0.0",
"exif-parser": "^0.1.12",
"file-type": "^16.0.0",
"file-type": "^19.6.0",
"mime": "3"
},
"devDependencies": {
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bitmap, Format, JimpClass, Edge } from "@jimp/types";
import { cssColorToHex, scan, scanIterator } from "@jimp/utils";
import fileType from "file-type/core.js";
import { fileTypeFromBuffer } from "./utils/fileTypeFromBuffer.js";
import { to } from "await-to-js";
import { existsSync, readFile, writeFile } from "@jimp/file-ops";
import mime from "mime/lite.js";
Expand Down Expand Up @@ -90,7 +90,7 @@ export interface JimpPlugin {
}

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I
k: infer I,
) => void
? I
: never;
Expand Down Expand Up @@ -225,7 +225,7 @@ export function createJimp<
*/
static async read(
url: string | Buffer | ArrayBuffer,
options?: MimeTypeToDecodeOptions
options?: MimeTypeToDecodeOptions,
) {
if (Buffer.isBuffer(url) || url instanceof ArrayBuffer) {
return this.fromBuffer(url);
Expand Down Expand Up @@ -292,8 +292,8 @@ export function createJimp<
if (Array.isArray(bitmap.data)) {
data = Buffer.concat(
bitmap.data.map((hex) =>
Buffer.from(hex.toString(16).padStart(8, "0"), "hex")
)
Buffer.from(hex.toString(16).padStart(8, "0"), "hex"),
),
);
}

Expand Down Expand Up @@ -329,12 +329,12 @@ export function createJimp<
*/
static async fromBuffer(
buffer: Buffer | ArrayBuffer,
options?: MimeTypeToDecodeOptions
options?: MimeTypeToDecodeOptions,
) {
const actualBuffer =
buffer instanceof ArrayBuffer ? bufferFromArrayBuffer(buffer) : buffer;

const mime = await fileType.fromBuffer(actualBuffer);
const mime = await fileTypeFromBuffer(actualBuffer);

if (!mime || !mime.mime) {
throw new Error("Could not find MIME for Buffer");
Expand All @@ -347,7 +347,7 @@ export function createJimp<
}

const image = new CustomJimp(
await format.decode(actualBuffer, options?.[format.mime])
await format.decode(actualBuffer, options?.[format.mime]),
) as InstanceType<typeof CustomJimp> & ExtraMethodMap;

image.mime = mime.mime;
Expand Down Expand Up @@ -500,7 +500,7 @@ export function createJimp<
const mimeType = mime.getType(path);
await writeFile(
path,
await this.getBuffer(mimeType as SupportedMimeTypes, options)
await this.getBuffer(mimeType as SupportedMimeTypes, options),
);
}

Expand Down Expand Up @@ -701,7 +701,7 @@ export function createJimp<
mode?: BlendMode;
opacitySource?: number;
opacityDest?: number;
} = {}
} = {},
) {
return composite(this, src, x, y, options);
}
Expand Down Expand Up @@ -731,14 +731,14 @@ export function createJimp<
y: number,
w: number,
h: number,
cb: (x: number, y: number, idx: number) => any
cb: (x: number, y: number, idx: number) => any,
): this;
scan(
x: number | ((x: number, y: number, idx: number) => any),
y?: number,
w?: number,
h?: number,
f?: (x: number, y: number, idx: number) => any
f?: (x: number, y: number, idx: number) => any,
): this {
return scan(this, x as any, y as any, w as any, h as any, f as any);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function composite<I extends JimpClass>(
mode?: BlendMode;
opacitySource?: number;
opacityDest?: number;
} = {}
} = {},
) {
if (!(src instanceof baseImage.constructor)) {
throw new Error("The source must be a Jimp image");
Expand Down Expand Up @@ -72,7 +72,7 @@ export function composite<I extends JimpClass>(
b: baseImage.bitmap.data[dstIdx + 2]! / 255,
a: baseImage.bitmap.data[dstIdx + 3]! / 255,
},
opacitySource
opacitySource,
);

baseImage.bitmap.data[dstIdx + 0] = limit255(blended.r * 255);
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/utils/fileTypeFromBuffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const fileTypeFromBuffer = async (
buffer: Uint8Array | ArrayBuffer
) => {
const { fileTypeFromBuffer } = await import("file-type/core");
return await fileTypeFromBuffer(buffer);
};
4 changes: 2 additions & 2 deletions packages/core/src/utils/image-bitmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function transformBitmap<I extends JimpClass>(
img: I,
width: number,
height: number,
transformation: (x: number, y: number) => readonly [number, number]
transformation: (x: number, y: number) => readonly [number, number],
) {
// Underscore-prefixed values are related to the source bitmap
// Their counterparts with no prefix are related to the target bitmap
Expand Down Expand Up @@ -140,7 +140,7 @@ function exifRotate<I extends JimpClass>(img: I) {

export async function attemptExifRotate<I extends JimpClass>(
image: I,
buffer: Buffer
buffer: Buffer,
) {
try {
(image as unknown as { _exif: ExifData })._exif =
Expand Down
Loading

0 comments on commit afa085e

Please sign in to comment.