From 52becb9702c2341f89c0259beaf6fe75c1b58055 Mon Sep 17 00:00:00 2001 From: YuShifan <894402575bt@gmail.com> Date: Thu, 14 Nov 2024 23:40:20 +0800 Subject: [PATCH] fix(cli): support more biarny type files --- cli/src/__tests__/utils/binaryFormats.test.ts | 102 +++++++++++++++++- cli/src/utils/binaryFormats.ts | 23 ++++ 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/cli/src/__tests__/utils/binaryFormats.test.ts b/cli/src/__tests__/utils/binaryFormats.test.ts index 1af1c43f8..0a699bae5 100644 --- a/cli/src/__tests__/utils/binaryFormats.test.ts +++ b/cli/src/__tests__/utils/binaryFormats.test.ts @@ -3,22 +3,96 @@ import { expect, describe, it } from '@jest/globals' describe('isSupportedBinaryFormatForMQTT', () => { it('should return true for supported binary formats', () => { - const supportedFormats = ['.png', '.mp4', '.mp3', '.zip', '.exe', '.pdf'] + const supportedFormats = [ + // Common formats + '.png', + '.mp4', + '.mp3', + '.zip', + '.exe', + '.pdf', + // Network & Protocol formats + '.netp', + '.pcap', + '.pcapng', + '.cap', + '.dmp', + '.trace', + // Database formats + '.db', + '.sqlite', + '.mdb', + '.frm', + '.myd', + '.myi', + // IoT & Device formats + '.dat', + '.raw', + '.log', + '.blob', + '.hex', + '.rom', + '.fw', + // Custom protocol & data formats + '.pkt', + '.mpack', + '.parquet', + '.orc', + ] supportedFormats.forEach((format) => { expect(isSupportedBinaryFormatForMQTT(format)).toBe(true) }) }) it('should return false for unsupported binary formats', () => { - const unsupportedFormats = ['.txt', '.doc', '.xls', '.ppt', '.html', '.css'] + const unsupportedFormats = [ + // Text formats + '.txt', + '.md', + '.rtf', + // Document formats + '.doc', + '.docx', + '.xls', + '.xlsx', + '.ppt', + '.pptx', + // Web formats + '.html', + '.css', + '.js', + '.json', + '.xml', + // Source code formats + '.c', + '.cpp', + '.java', + '.py', + '.ts', + // Config formats + '.yml', + '.ini', + '.conf', + '.env', + ] unsupportedFormats.forEach((format) => { expect(isSupportedBinaryFormatForMQTT(format)).toBe(false) }) }) it('should be case-sensitive', () => { - expect(isSupportedBinaryFormatForMQTT('.PNG')).toBe(false) - expect(isSupportedBinaryFormatForMQTT('.png')).toBe(true) + const testCases = [ + ['.PNG', '.png'], + ['.DB', '.db'], + ['.NETP', '.netp'], + ['.PCAP', '.pcap'], + ['.HEX', '.hex'], + ] + + testCases.forEach(([upper, lower]) => { + expect(isSupportedBinaryFormatForMQTT(upper)).toBe(false) + expect(isSupportedBinaryFormatForMQTT(lower)).toBe(true) + }) }) it('should include all supported formats', () => { @@ -26,4 +100,24 @@ describe('isSupportedBinaryFormatForMQTT', () => { expect(isSupportedBinaryFormatForMQTT(format)).toBe(true) }) }) + + it('should handle format categories correctly', () => { + // Test by categories + const categories = { + image: ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.ico', '.tif', '.tiff'], + video: ['.mp4', '.avi', '.mov', '.mkv', '.flv', '.wmv', '.mpeg', '.3gp'], + audio: ['.mp3', '.wav', '.flac', '.aac', '.ogg', '.wma', '.m4a', '.m4p'], + compressed: ['.zip', '.gz', '.rar', '.tar', '.7z', '.bz2', '.xz', '.jar'], + network: ['.netp', '.pcap', '.pcapng', '.cap', '.dmp', '.trace'], + database: ['.db', '.sqlite', '.mdb', '.frm', '.myd', '.myi'], + device: ['.dat', '.raw', '.log', '.blob', '.hex', '.rom', '.fw'], + protocol: ['.pkt', '.mpack', '.parquet', '.orc'], + } + + Object.values(categories) + .flat() + .forEach((format) => { + expect(isSupportedBinaryFormatForMQTT(format)).toBe(true) + }) + }) }) diff --git a/cli/src/utils/binaryFormats.ts b/cli/src/utils/binaryFormats.ts index c74d5ae63..3b75b4784 100644 --- a/cli/src/utils/binaryFormats.ts +++ b/cli/src/utils/binaryFormats.ts @@ -40,6 +40,29 @@ export const supportedBinaryFormatsForMQTT = [ '.img', // Binary file formats '.pdf', '.epub', // Binary document file formats + '.netp', // Network protocol binary format + '.pcap', // Packet capture + '.pcapng', // Next generation packet capture + '.cap', // Network capture + '.dmp', // Memory dump + '.trace', // Network trace + '.db', // Generic database + '.sqlite', // SQLite database + '.mdb', // Microsoft Access database + '.frm', // MySQL table definition + '.myd', // MySQL data + '.myi', // MySQL index + '.dat', // Generic data file + '.raw', // Raw data + '.log', // Binary log + '.blob', // Binary large object + '.hex', // Hex dump + '.rom', // ROM image + '.fw', // Firmware + '.pkt', // Packet data + '.mpack', // MessagePack data + '.parquet', // Parquet columnar storage + '.orc', // Optimized Row Columnar ] const isSupportedBinaryFormatForMQTT = (fileExtname: string) => {