Skip to content

Commit

Permalink
refactor(deps): update dependencies
Browse files Browse the repository at this point in the history
- Require Node.js 18+
  • Loading branch information
azu committed Oct 22, 2023
1 parent b05a528 commit ab8a032
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 323 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 14, 16, 18 ]
node-version: [ 14, 16, 18, 20 ]
steps:
- name: checkout
uses: actions/checkout@v3
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@
"trailingComma": "none"
},
"dependencies": {
"file-type": "^16.5.4",
"is-svg": "^4.2.1",
"read-chunk": "^3.2.0"
"file-type": "^18.5.0",
"is-svg": "^5.0.0",
"read-chunk": "^4.0.3"
},
"devDependencies": {
"@types/mocha": "^9.1.1",
"@types/node": "^18.0.6",
"lint-staged": "^13.0.3",
"mocha": "^10.0.0",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"@types/mocha": "^10.0.3",
"@types/node": "^20.8.7",
"lint-staged": "^15.0.2",
"mocha": "^10.2.0",
"prettier": "^3.0.3",
"rimraf": "^5.0.5",
"ts-node": "^10.9.1",
"ts-node-test-register": "^10.0.0",
"typescript": "^4.7.4"
"typescript": "^5.2.2"
}
}
15 changes: 8 additions & 7 deletions src/image-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { fromBuffer } from "file-type";

const imageExts = new Set([
const supportedImageTypes = [
"jpg",
"png",
"gif",
Expand All @@ -19,12 +17,15 @@ const imageExts = new Set([
"heic",
"cur",
"dcm"
]);
];
export type SupportedImageTypes = (typeof supportedImageTypes)[number];
const imageExts = new Set(supportedImageTypes);

export const imageType = async (buffer: Buffer | Uint8Array | ArrayBuffer) => {
const ret = await fromBuffer(buffer);
export const imageType = async (buffer: Buffer | Uint8Array | ArrayBuffer): Promise<SupportedImageTypes | null> => {
const { fileTypeFromBuffer } = await import("file-type");
const ret = await fileTypeFromBuffer(buffer);
if (!ret) {
return null;
}
return imageExts.has(ret.ext) ? ret : null;
return imageExts.has(ret.ext) ? ret.mime : null;
};
33 changes: 20 additions & 13 deletions src/validate-image-type.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import assert from "assert";
import readChunk from "read-chunk";
import isSvg from "is-svg";
import * as fs from "fs/promises";
import { imageType } from "./image-type";
import { BINARY_READ_LENGTH, isBinary } from "./isBinary";

export type ValidateImageTypeOptions = {
/**
* Original file name
Expand Down Expand Up @@ -36,14 +35,17 @@ export async function validateBufferMIMEType(
`Should be set an array of mimeType. e.g.) ['image/jpeg']`
);
const allowSVG = mimeTypes.includes("image/svg+xml");
if (allowSVG && isSvg(buffer)) {
return {
ok: true,
error: undefined
};
if (allowSVG) {
const { default: isSvg } = await import("is-svg");
if (isSvg(String(buffer))) {
return {
ok: true,
error: undefined
};
}
}
const imageTypeResult = await imageType(buffer);
if (!imageTypeResult) {
const imageTypeMime = await imageType(buffer);
if (!imageTypeMime) {
return {
ok: false,
error: new Error(
Expand All @@ -52,12 +54,12 @@ export async function validateBufferMIMEType(
)
};
}
const isAllowed = mimeTypes.includes(imageTypeResult.mime);
const isAllowed = mimeTypes.includes(imageTypeMime);
if (!isAllowed) {
return {
ok: false,
error: new Error(
`This buffer is disallowed image MimeType: ${imageTypeResult.mime}, allowMimeTypes: ${JSON.stringify(
`This buffer is disallowed image MimeType: ${imageTypeMime}, allowMimeTypes: ${JSON.stringify(
mimeTypes
)}` + (options.originalFilename ? `,filename: ${options.originalFilename}` : "")
)
Expand All @@ -70,7 +72,7 @@ export async function validateBufferMIMEType(
}

/**
* Detect the image type of a filePath
* Detect the image type of filePath
* @example
*
* ```
Expand All @@ -86,14 +88,19 @@ export async function validateMIMEType(
filePath: string,
options: ValidateImageTypeOptions
): Promise<ValidateImageResult> {
const { readChunk } = await import("read-chunk");
// Use head buffer for performance reason
const buffer = await readChunk(filePath, 0, BINARY_READ_LENGTH);
const buffer = await readChunk(filePath, {
startPosition: 0,
length: BINARY_READ_LENGTH
});
if (!isBinary(buffer)) {
const mimeTypes = options.allowMimeTypes;
// Handle SVG as special case
// https://github.com/sindresorhus/is-svg
const allowSVG = mimeTypes.includes("image/svg+xml");
if (allowSVG) {
const { default: isSvg } = await import("is-svg");
// if the content is not binary, read all content and check it
// Note: Require 128 bytes at least one
const content = await fs.readFile(filePath, "utf-8");
Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"compilerOptions": {
/* Basic Options */
"module": "commonjs",
"moduleResolution": "node",
"module": "nodenext",
"moduleResolution": "nodenext",
"esModuleInterop": true,
"newLine": "LF",
"outDir": "./lib/",
"target": "ES2015",
"target": "ES2022",
"sourceMap": true,
"declaration": true,
"jsx": "preserve",
Expand Down
Loading

0 comments on commit ab8a032

Please sign in to comment.