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: |
npmci node install stable
npmci npm install
pnpm install -g @gitzone/tsdoc
pnpm install -g @git.zone/tsdoc
npmci command tsdoc
continue-on-error: true

View File

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

View File

@ -13,17 +13,16 @@
"buildDocs": "tsdoc"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.66",
"@gitzone/tsrun": "^1.2.42",
"@gitzone/tstest": "^1.0.74",
"@pushrocks/tapbundle": "^5.0.8",
"@types/node": "^20.4.1",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
"@git.zone/tsbuild": "^2.1.66",
"@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.77",
"@push.rocks/tapbundle": "^5.0.8",
"@types/node": "^20.12.12"
},
"dependencies": {
"@types/mime-types": "^2.1.1",
"mime-types": "^2.1.35"
"@types/mime-types": "^2.1.4",
"file-type": "^19.0.0",
"mime": "^4.0.3"
},
"files": [
"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';
tap.test('should detect image', async () => {
const filePath = 'file.jpg';
const fileType = smartmime.detectMimeType(filePath);
const fileType = await smartmime.detectMimeType({
path: filePath,
});
console.log(fileType);
expect(smartmime.isBinary(filePath)).toBeTrue();
expect(await smartmime.isBinary({
path: filePath,
})).toBeTrue();
});
tap.test('should detect text', async () => {
const filePath = 'file.svg';
const fileType = smartmime.detectMimeType(filePath);
const fileType = await smartmime.detectMimeType({
path: filePath,
});
console.log(fileType);
expect(smartmime.isBinary(filePath)).toBeFalse();
expect(await smartmime.isBinary({
path: filePath,
})).toBeFalse();
});
tap.test('should detect json', async () => {
const filePath = 'file.json';
const fileType = smartmime.detectMimeType(filePath);
const fileType = await smartmime.detectMimeType({
path: filePath,
});
console.log(fileType);
expect(smartmime.isBinary(filePath)).toBeFalse();
expect(await smartmime.isBinary({
path: filePath,
})).toBeFalse();
});
tap.test('should detect html', async () => {
const filePath = 'file.html';
const fileType = smartmime.detectMimeType(filePath);
expect(fileType).toEqual('text/html');
const fileType = await smartmime.detectMimeType({
path: filePath,
});
expect(fileType.mime).toEqual('text/html');
console.log(fileType);
});
tap.test('should detect woff', async () => {
const filePath = 'file.woff';
const fileType = smartmime.detectMimeType(filePath);
expect(fileType).toEqual('font/woff');
const fileType = await smartmime.detectMimeType({
path: filePath,
});
expect(fileType.mime).toEqual('font/woff');
console.log(fileType);
});
tap.test('should detect woff2', async () => {
const filePath = 'file.woff2';
const fileType = smartmime.detectMimeType(filePath);
expect(fileType).toEqual('font/woff2');
const fileType = await smartmime.detectMimeType({
path: filePath,
});
expect(fileType.mime).toEqual('font/woff2');
console.log(fileType);
});

View File

@ -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
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';
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';
};

View File

@ -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, };