BREAKING CHANGE(core): update
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartmime',
|
||||
version: '1.0.6',
|
||||
description: 'a module to detect mime types'
|
||||
version: '2.0.0',
|
||||
description: 'A module for detecting MIME types with support for binary and text file distinctions.'
|
||||
}
|
||||
|
59
ts/binary.ts
Normal file
59
ts/binary.ts
Normal file
@ -0,0 +1,59 @@
|
||||
export const binaryMimeTypes = [
|
||||
'application/octet-stream',
|
||||
'application/pdf',
|
||||
'application/vnd.ms-excel',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'application/vnd.ms-powerpoint',
|
||||
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||
'application/msword',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'application/x-rar-compressed',
|
||||
'application/x-tar',
|
||||
'application/zip',
|
||||
'application/x-7z-compressed',
|
||||
'application/x-bzip',
|
||||
'application/x-bzip2',
|
||||
'application/x-gzip',
|
||||
'application/x-lzh-compressed',
|
||||
'application/x-xz',
|
||||
'application/vnd.android.package-archive',
|
||||
'application/x-iso9660-image',
|
||||
'application/x-shockwave-flash',
|
||||
'application/x-silverlight-app',
|
||||
'application/vnd.apple.mpegurl',
|
||||
'application/vnd.rn-realmedia',
|
||||
'application/x-mobipocket-ebook',
|
||||
'application/x-msdownload',
|
||||
'application/vnd.amazon.ebook',
|
||||
'application/x-bittorrent',
|
||||
'audio/mpeg',
|
||||
'audio/x-wav',
|
||||
'audio/x-flac',
|
||||
'audio/x-aiff',
|
||||
'audio/x-matroska',
|
||||
'audio/ogg',
|
||||
'audio/x-ms-wma',
|
||||
'audio/x-pn-realaudio',
|
||||
'image/bmp',
|
||||
'image/gif',
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
'image/tiff',
|
||||
'image/x-icon',
|
||||
'image/x-xcf',
|
||||
'image/x-cmu-raster',
|
||||
'image/x-portable-bitmap',
|
||||
'image/x-portable-graymap',
|
||||
'image/x-portable-pixmap',
|
||||
'image/x-rgb',
|
||||
'video/mp4',
|
||||
'video/x-msvideo',
|
||||
'video/mpeg',
|
||||
'video/quicktime',
|
||||
'video/x-matroska',
|
||||
'video/x-ms-wmv',
|
||||
'video/x-flv',
|
||||
'video/webm',
|
||||
'video/3gpp',
|
||||
'video/3gpp2'
|
||||
];
|
49
ts/index.ts
49
ts/index.ts
@ -1,26 +1,41 @@
|
||||
import type { Readable } from 'stream';
|
||||
import * as plugins from './smartmime.plugins.js';
|
||||
|
||||
export const binaryFileTypes = ['jpg', 'png', 'pdf'];
|
||||
import { binaryMimeTypes } from './binary.js';
|
||||
|
||||
// TODO: evaluate where this is actually used
|
||||
export const supportedFileTypes = ['json', 'html', 'svg', 'jpg', 'ts', 'js'];
|
||||
|
||||
export type TMimeTypes = 'image/jpeg' | 'image/svg+xml' | 'application/json' | 'text/html';
|
||||
|
||||
export const detectMimeType = (pathArg: string) => {
|
||||
return plugins.mimeTypes.lookup(pathArg) as TMimeTypes;
|
||||
};
|
||||
|
||||
export const isBinary = (pathArg: string) => {
|
||||
let isBinary = false;
|
||||
for (const binaryFileType of binaryFileTypes) {
|
||||
if (isBinary) {
|
||||
continue;
|
||||
}
|
||||
isBinary = pathArg.endsWith(`.${binaryFileType}`);
|
||||
export const detectMimeType = async (optionsArg: {
|
||||
path?: string;
|
||||
buffer?: Uint8Array;
|
||||
stream?: Readable;
|
||||
}): Promise<plugins.fileType.FileTypeResult> => {
|
||||
if (optionsArg.path) {
|
||||
return {
|
||||
mime: plugins.mime.getType(optionsArg.path),
|
||||
ext: plugins.path.extname(optionsArg.path),
|
||||
} as plugins.fileType.FileTypeResult;
|
||||
} else if (optionsArg.buffer) {
|
||||
return plugins.fileType.fileTypeFromBuffer(optionsArg.buffer);
|
||||
} else if (optionsArg.stream) {
|
||||
return plugins.fileType.fileTypeFromStream(optionsArg.stream);
|
||||
}
|
||||
return isBinary;
|
||||
};
|
||||
|
||||
export const getEncoding = (pathArg: string) => {
|
||||
return isBinary(pathArg) ? 'binary' : 'utf8';
|
||||
export const isBinary = async (optionsArg: {
|
||||
path?: string;
|
||||
buffer?: Uint8Array;
|
||||
stream?: Readable;
|
||||
}) => {
|
||||
const mimeType = await detectMimeType(optionsArg);
|
||||
return binaryMimeTypes.includes(mimeType.mime);
|
||||
};
|
||||
|
||||
export const getEncoding = async (optionsArg: {
|
||||
path?: string;
|
||||
buffer?: Uint8Array;
|
||||
stream?: Readable;
|
||||
}) => {
|
||||
return (await isBinary(optionsArg)) ? 'binary' : 'utf8';
|
||||
};
|
||||
|
@ -4,6 +4,7 @@ import * as path from 'path';
|
||||
export { path };
|
||||
|
||||
// thirdparty scope
|
||||
import mimeTypes from 'mime-types';
|
||||
import * as fileType from 'file-type';
|
||||
import mime from 'mime';
|
||||
|
||||
export { mimeTypes };
|
||||
export { fileType, mime, };
|
||||
|
Reference in New Issue
Block a user