BREAKING CHANGE(core): update

This commit is contained in:
Philipp Kunz 2024-05-21 00:57:24 +02:00
parent 70dc0ef68f
commit 139b31be07
9 changed files with 5234 additions and 3207 deletions

View File

@ -119,6 +119,6 @@ jobs:
run: | run: |
npmci node install stable npmci node install stable
npmci npm install npmci npm install
pnpm install -g @gitzone/tsdoc pnpm install -g @git.zone/tsdoc
npmci command tsdoc npmci command tsdoc
continue-on-error: true continue-on-error: true

View File

@ -121,7 +121,7 @@ pages:
stage: metadata stage: metadata
script: script:
- npmci node install lts - npmci node install lts
- npmci command npm install -g @gitzone/tsdoc - npmci command npm install -g @git.zone/tsdoc
- npmci npm prepare - npmci npm prepare
- npmci npm install - npmci npm install
- npmci command tsdoc - npmci command tsdoc

View File

@ -13,17 +13,16 @@
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.66", "@git.zone/tsbuild": "^2.1.66",
"@gitzone/tsrun": "^1.2.42", "@git.zone/tsrun": "^1.2.44",
"@gitzone/tstest": "^1.0.74", "@git.zone/tstest": "^1.0.77",
"@pushrocks/tapbundle": "^5.0.8", "@push.rocks/tapbundle": "^5.0.8",
"@types/node": "^20.4.1", "@types/node": "^20.12.12"
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
}, },
"dependencies": { "dependencies": {
"@types/mime-types": "^2.1.1", "@types/mime-types": "^2.1.4",
"mime-types": "^2.1.35" "file-type": "^19.0.0",
"mime": "^4.0.3"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

File diff suppressed because it is too large Load Diff

View File

@ -1,45 +1,63 @@
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@push.rocks/tapbundle';
import * as smartmime from '../ts/index.js'; import * as smartmime from '../ts/index.js';
tap.test('should detect image', async () => { tap.test('should detect image', async () => {
const filePath = 'file.jpg'; const filePath = 'file.jpg';
const fileType = smartmime.detectMimeType(filePath); const fileType = await smartmime.detectMimeType({
path: filePath,
});
console.log(fileType); console.log(fileType);
expect(smartmime.isBinary(filePath)).toBeTrue(); expect(await smartmime.isBinary({
path: filePath,
})).toBeTrue();
}); });
tap.test('should detect text', async () => { tap.test('should detect text', async () => {
const filePath = 'file.svg'; const filePath = 'file.svg';
const fileType = smartmime.detectMimeType(filePath); const fileType = await smartmime.detectMimeType({
path: filePath,
});
console.log(fileType); console.log(fileType);
expect(smartmime.isBinary(filePath)).toBeFalse(); expect(await smartmime.isBinary({
path: filePath,
})).toBeFalse();
}); });
tap.test('should detect json', async () => { tap.test('should detect json', async () => {
const filePath = 'file.json'; const filePath = 'file.json';
const fileType = smartmime.detectMimeType(filePath); const fileType = await smartmime.detectMimeType({
path: filePath,
});
console.log(fileType); console.log(fileType);
expect(smartmime.isBinary(filePath)).toBeFalse(); expect(await smartmime.isBinary({
path: filePath,
})).toBeFalse();
}); });
tap.test('should detect html', async () => { tap.test('should detect html', async () => {
const filePath = 'file.html'; const filePath = 'file.html';
const fileType = smartmime.detectMimeType(filePath); const fileType = await smartmime.detectMimeType({
expect(fileType).toEqual('text/html'); path: filePath,
});
expect(fileType.mime).toEqual('text/html');
console.log(fileType); console.log(fileType);
}); });
tap.test('should detect woff', async () => { tap.test('should detect woff', async () => {
const filePath = 'file.woff'; const filePath = 'file.woff';
const fileType = smartmime.detectMimeType(filePath); const fileType = await smartmime.detectMimeType({
expect(fileType).toEqual('font/woff'); path: filePath,
});
expect(fileType.mime).toEqual('font/woff');
console.log(fileType); console.log(fileType);
}); });
tap.test('should detect woff2', async () => { tap.test('should detect woff2', async () => {
const filePath = 'file.woff2'; const filePath = 'file.woff2';
const fileType = smartmime.detectMimeType(filePath); const fileType = await smartmime.detectMimeType({
expect(fileType).toEqual('font/woff2'); path: filePath,
});
expect(fileType.mime).toEqual('font/woff2');
console.log(fileType); console.log(fileType);
}); });

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartmime', name: '@push.rocks/smartmime',
version: '1.0.6', version: '2.0.0',
description: 'a module to detect mime types' description: 'A module for detecting MIME types with support for binary and text file distinctions.'
} }

59
ts/binary.ts Normal file
View 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'
];

View File

@ -1,26 +1,41 @@
import type { Readable } from 'stream';
import * as plugins from './smartmime.plugins.js'; 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 const supportedFileTypes = ['json', 'html', 'svg', 'jpg', 'ts', 'js'];
export type TMimeTypes = 'image/jpeg' | 'image/svg+xml' | 'application/json' | 'text/html'; export const detectMimeType = async (optionsArg: {
path?: string;
export const detectMimeType = (pathArg: string) => { buffer?: Uint8Array;
return plugins.mimeTypes.lookup(pathArg) as TMimeTypes; stream?: Readable;
}; }): Promise<plugins.fileType.FileTypeResult> => {
if (optionsArg.path) {
export const isBinary = (pathArg: string) => { return {
let isBinary = false; mime: plugins.mime.getType(optionsArg.path),
for (const binaryFileType of binaryFileTypes) { ext: plugins.path.extname(optionsArg.path),
if (isBinary) { } as plugins.fileType.FileTypeResult;
continue; } else if (optionsArg.buffer) {
} return plugins.fileType.fileTypeFromBuffer(optionsArg.buffer);
isBinary = pathArg.endsWith(`.${binaryFileType}`); } else if (optionsArg.stream) {
return plugins.fileType.fileTypeFromStream(optionsArg.stream);
} }
return isBinary;
}; };
export const getEncoding = (pathArg: string) => { export const isBinary = async (optionsArg: {
return isBinary(pathArg) ? 'binary' : 'utf8'; 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';
}; };

View File

@ -4,6 +4,7 @@ import * as path from 'path';
export { path }; export { path };
// thirdparty scope // thirdparty scope
import mimeTypes from 'mime-types'; import * as fileType from 'file-type';
import mime from 'mime';
export { mimeTypes }; export { fileType, mime, };