BREAKING CHANGE(core): update
This commit is contained in:
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';
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user