Socket
Socket
Sign inDemoInstall

mime-kind

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mime-kind - npm Package Compare versions

Comparing version 1.0.4 to 2.0.0

README.md

139

main.js
'use strict';
var fs = require('fs');
var normalize = require('path').normalize;
var fileType = require('file-type');
var mime = require('mime-types');
const fs = require('fs');
const normalize = require('path').normalize;
const fileType = require('file-type');
const mimeType = require('mime-types');
const BUFFER_LENGTH = 262;
function isString(val) {
return typeof val === 'string';
}
function isObject(val) {

@@ -12,4 +18,6 @@ return Object.prototype.toString.call(val) === '[object Object]';

function isStream(stream) {
return stream && typeof stream === 'object' && typeof stream.pipe === 'function' && stream.readable !== false && typeof stream._read === 'function' && typeof stream._readableState === 'object';
function isStream(val) {
return typeof val === 'object' && typeof val.pipe === 'function' &&
val.readable !== false && typeof val._read === 'function' &&
typeof val._readableState === 'object';
}

@@ -26,5 +34,3 @@

function chunkSync(data, length) {
var buf = new Buffer(length);
function chunkSync(data) {
if (!data.fd) {

@@ -39,3 +45,5 @@ data.path = normalize(data.path);

fs.readSync(data.fd, buf, 0, length);
const buf = new Buffer(BUFFER_LENGTH);
fs.readSync(data.fd, buf, 0, BUFFER_LENGTH);
fs.closeSync(data.fd);

@@ -46,74 +54,63 @@

function streamSync(stream, length) {
if (!stream.closed && !stream.destroyed && length > 0) {
try {
var data = {
path: stream.path,
flags: stream.flags,
mode: stream.mode,
fd: stream.fd
};
return chunkSync(data, length);
} catch (e) {
throw new Error('The file must be local and exists.');
}
} else {
return false;
function streamSync(stream) {
if (!stream.closed && !stream.destroyed) {
return chunkSync(stream);
}
return false;
}
module.exports = function (data, defaultValue) {
var type;
var ext;
if (typeof data === 'string') {
type = mime.lookup(data);
if (!type && isExists(data)) {
type = fileType(chunkSync({ path: data, flags: 'r' }, 262));
module.exports = (data, defaultValue) => {
if (data) {
if (isString(data)) {
const mime = mimeType.lookup(data);
if (mime) {
return { ext: mimeType.extension(mime), mime };
}
if (isExists(data)) {
return fileType(chunkSync({ path: data, flags: 'r' }));
}
} else if (Buffer.isBuffer(data)) {
return fileType(data);
} else if (isStream(data)) {
return fileType(streamSync(data));
}
} else if (Buffer.isBuffer(data)) {
type = fileType(data);
} else if (isStream(data)) {
type = fileType(streamSync(data, 262));
}
if (type) {
if (type.mime) {
return { ext: type.ext, mime: type.mime };
}
if (defaultValue) {
if (isObject(defaultValue)) {
let ext;
if (defaultValue.ext) {
ext = defaultValue.ext;
const mime = mimeType.lookup(ext);
if (mime) {
return { ext, mime };
}
}
return { ext: mime.extension(type), mime: type };
}
if (defaultValue.mime || defaultValue.type) {
const mime = defaultValue.mime || defaultValue.type;
if (!ext) {
ext = mimeType.extension(mime);
}
if (ext) {
return { ext, mime };
}
}
} else if (isString(defaultValue)) {
let ext = mimeType.extension(defaultValue);
let mime = mimeType.lookup(defaultValue);
if (defaultValue) {
if (isObject(defaultValue)) {
if (defaultValue.ext) {
ext || (ext = defaultValue.ext);
type || (type = mime.lookup(ext));
if (ext || mime) {
if (!mime) {
mime = mimeType.lookup(ext);
}
if (!ext) {
ext = mimeType.extension(mime);
}
return { ext, mime };
}
}
if (defaultValue.mime || defaultValue.type) {
type || (type = defaultValue.mime || defaultValue.type);
ext || (ext = mime.extension(type));
}
} else if (typeof defaultValue === 'string') {
ext = mime.extension(defaultValue);
type = mime.lookup(defaultValue);
if (ext) {
type || (type = mime.lookup(ext));
}
if (type) {
ext || (ext = mime.extension(type));
}
}
if (type && ext) {
return { ext: ext, mime: type };
}
}
return false;
return null;
};
{
"name": "mime-kind",
"author": "Alexey Bystrov <strikeentco@gmail.com>",
"version": "1.0.4",
"version": "2.0.0",
"description": "Detect the mime type of a Buffer, ReadStream, file path and file name.",
"engines": {
"node": ">=4.0.0"
},
"keywords": [

@@ -22,2 +25,5 @@ "file",

"test": "mocha test",
"lint": "eslint main.js",
"check": "npm run lint && npm run test",
"cover": "istanbul cover ./node_modules/mocha/bin/_mocha",
"test-on-travis": "istanbul cover --report lcovonly ./node_modules/mocha/bin/_mocha"

@@ -34,9 +40,14 @@ },

"file-type": "^3.8.0",
"mime-types": "^2.1.10"
"mime-types": "^2.1.11"
},
"devDependencies": {
"mocha": "^2.4.5",
"should": "^8.2.2"
"eslint": "^2.9.0",
"eslint-config-airbnb": "^9.0.1",
"eslint-plugin-import": "^1.14.0",
"eslint-plugin-jsx-a11y": "^1.2.0",
"eslint-plugin-react": "^5.1.1",
"mocha": "^3.0.2",
"should": "^11.1.0"
},
"license": "MIT"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc