From 7af44d4a2cf8412cb0cb113ea0eb7d6c5b26b349 Mon Sep 17 00:00:00 2001 From: fabio286 Date: Wed, 5 Feb 2025 15:34:01 +0100 Subject: [PATCH] refactor: add ciaplu for pattern matching in language detection and MIME type resolution --- package-lock.json | 12 ++++++ package.json | 1 + src/common/libs/langDetector.ts | 24 ++++++------ src/common/libs/mimeFromHex.ts | 66 ++++++++++++--------------------- 4 files changed, 47 insertions(+), 56 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7f6a9f44..b8bf4a16 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "babel-loader": "~8.2.3", "better-sqlite3": "~10.0.0", "chalk": "~4.1.2", + "ciaplu": "^2.2.0", "cpu-features": "^0.0.10", "cross-env": "~7.0.2", "css-loader": "~6.5.0", @@ -5608,6 +5609,12 @@ "node": ">=8" } }, + "node_modules/ciaplu": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/ciaplu/-/ciaplu-2.2.0.tgz", + "integrity": "sha512-7y8s0GMFpIKqX2kwiOEYbaX3P9tPIbX4x41uw8GZjkZ+y0QZrpY3PFjE2Ed6BOeFxcCWi7b85MYHeiRrVrlSOQ==", + "license": "MIT" + }, "node_modules/clean-css": { "version": "5.3.2", "license": "MIT", @@ -20345,6 +20352,11 @@ "version": "3.9.0", "dev": true }, + "ciaplu": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/ciaplu/-/ciaplu-2.2.0.tgz", + "integrity": "sha512-7y8s0GMFpIKqX2kwiOEYbaX3P9tPIbX4x41uw8GZjkZ+y0QZrpY3PFjE2Ed6BOeFxcCWi7b85MYHeiRrVrlSOQ==" + }, "clean-css": { "version": "5.3.2", "requires": { diff --git a/package.json b/package.json index ccc3d90d..21d4613e 100644 --- a/package.json +++ b/package.json @@ -131,6 +131,7 @@ "babel-loader": "~8.2.3", "better-sqlite3": "~10.0.0", "chalk": "~4.1.2", + "ciaplu": "^2.2.0", "cpu-features": "^0.0.10", "cross-env": "~7.0.2", "css-loader": "~6.5.0", diff --git a/src/common/libs/langDetector.ts b/src/common/libs/langDetector.ts index e300a73b..80265ff8 100644 --- a/src/common/libs/langDetector.ts +++ b/src/common/libs/langDetector.ts @@ -1,3 +1,5 @@ +import { match } from 'ciaplu'; + function isJSON (str: string) { try { if (!['{', '['].includes(str.trim()[0])) @@ -176,17 +178,13 @@ function isMD (str: string) { } export function langDetector (str: string) { - if (!str || !str.trim().length) - return 'text'; - if (isJSON(str)) - return 'json'; - if (isHTML(str)) - return 'html'; - if (isSVG(str)) - return 'svg'; - if (isXML(str)) - return 'xml'; - if (isMD(str)) - return 'markdown'; - return 'text'; + return match(str) + .when(() => !str || !str.trim().length, () => 'text') + .when(isJSON, () => 'json') + .when(isHTML, () => 'html') + .when(isSVG, () => 'svg') + .when(isXML, () => 'xml') + .when(isMD, () => 'markdown') + .otherwise(() => 'text') + .return(); } diff --git a/src/common/libs/mimeFromHex.ts b/src/common/libs/mimeFromHex.ts index 9e4ecbff..040ceab4 100644 --- a/src/common/libs/mimeFromHex.ts +++ b/src/common/libs/mimeFromHex.ts @@ -1,45 +1,25 @@ +import { match } from 'ciaplu'; + export function mimeFromHex (hex: string) { - switch (hex.substring(0, 4)) { // 2 bytes - case '424D': - return { ext: 'bmp', mime: 'image/bmp' }; - case '1F8B': - return { ext: 'tar.gz', mime: 'application/gzip' }; - case '0B77': - return { ext: 'ac3', mime: 'audio/vnd.dolby.dd-raw' }; - case '7801': - return { ext: 'dmg', mime: 'application/x-apple-diskimage' }; - case '4D5A': - return { ext: 'exe', mime: 'application/x-msdownload' }; - case '1FA0': - case '1F9D': - return { ext: 'Z', mime: 'application/x-compress' }; - default: - switch (hex.substring(0, 6)) { // 3 bytes - case 'FFD8FF': - return { ext: 'jpg', mime: 'image/jpeg' }; - case '4949BC': - return { ext: 'jxr', mime: 'image/vnd.ms-photo' }; - case '425A68': - return { ext: 'bz2', mime: 'application/x-bzip2' }; - default: - switch (hex) { // 4 bites - case '89504E47': - return { ext: 'png', mime: 'image/png' }; - case '47494638': - return { ext: 'gif', mime: 'image/gif' }; - case '25504446': - return { ext: 'pdf', mime: 'application/pdf' }; - case '504B0304': - return { ext: 'zip', mime: 'application/zip' }; - case '425047FB': - return { ext: 'bpg', mime: 'image/bpg' }; - case '4D4D002A': - return { ext: 'tif', mime: 'image/tiff' }; - case '00000100': - return { ext: 'ico', mime: 'image/x-icon' }; - default: - return { ext: '', mime: 'unknown ' + hex }; - } - } - } + return match(hex.substring(0, 4)) // 2 bytes + .with('424D', () => ({ ext: 'bmp', mime: 'image/bmp' })) + .with('1F8B', () => ({ ext: 'tar.gz', mime: 'application/gzip' })) + .with('0B77', () => ({ ext: 'ac3', mime: 'audio/vnd.dolby.dd-raw' })) + .with('7801', () => ({ ext: 'dmg', mime: 'application/x-apple-diskimage' })) + .with('4D5A', () => ({ ext: 'exe', mime: 'application/x-msdownload' })) + .when((val) => ['1FA0', '1F9D'].includes(val), () => ({ ext: 'Z', mime: 'application/x-compress' })) + .extracting(() => hex.substring(0, 6)) // 3 bytes + .with('FFD8FF', () => ({ ext: 'jpg', mime: 'image/jpeg' })) + .with('4949BC', () => ({ ext: 'jxr', mime: 'image/vnd.ms-photo' })) + .with('425A68', () => ({ ext: 'bz2', mime: 'application/x-bzip2' })) + .extracting(() => hex) // 4 bytes + .with('89504E47', () => ({ ext: 'png', mime: 'image/png' })) + .with('47494638', () => ({ ext: 'gif', mime: 'image/gif' })) + .with('25504446', () => ({ ext: 'pdf', mime: 'application/pdf' })) + .with('504B0304', () => ({ ext: 'zip', mime: 'application/zip' })) + .with('425047FB', () => ({ ext: 'bpg', mime: 'image/bpg' })) + .with('4D4D002A', () => ({ ext: 'tif', mime: 'image/tiff' })) + .with('00000100', () => ({ ext: 'ico', mime: 'image/x-icon' })) + .otherwise(() => ({ ext: '', mime: 'unknown ' + hex })) + .return(); }