Comparing version 3.0.0 to 4.0.0
55
index.js
'use strict'; | ||
const fileType = require('file-type'); | ||
const { fromBuffer, fromFile } = require('file-type'); | ||
const { lookup, extension } = require('mime-types'); | ||
const { | ||
isString, isObject, isBuffer, isStream, isExists, isExistsSync, | ||
chunkAsync, chunkSync, streamChunk, BUFFER_LENGTH | ||
isString, isObject, isBuffer, isStream, isExists, | ||
streamChunk, BUFFER_LENGTH | ||
} = require('./utils'); | ||
@@ -50,41 +50,2 @@ | ||
/** | ||
* Synchronously determines MIME type of input | ||
* @param {String|Buffer|Uint8Array|ArrayBuffer|ReadableStream} input | ||
* @param {Object} [defaultValue] | ||
* @param {String} [defaultValue.ext] | ||
* @param {String} [defaultValue.mime] | ||
* @param {String} [defaultValue.type] | ||
* @returns {{ ext: String, mime: String }|null} | ||
*/ | ||
function sync(input, defaultValue) { | ||
if (input) { | ||
let file; | ||
if (isString(input)) { | ||
const mime = lookup(input); | ||
if (mime) { | ||
return { ext: extension(mime), mime }; | ||
} | ||
if (isExistsSync(input)) { | ||
const { bytesRead, buffer } = chunkSync({ path: input, flags: 'r' }); | ||
if (bytesRead && buffer) { | ||
file = fileType(buffer); | ||
} | ||
} | ||
} else if (isBuffer(input)) { | ||
file = fileType(input); | ||
} else if (isStream(input)) { | ||
const { bytesRead, buffer } = chunkSync(input); | ||
if (bytesRead && buffer) { | ||
file = fileType(buffer); | ||
} | ||
} | ||
if (file !== undefined && file !== null) { | ||
return file; | ||
} | ||
} | ||
return defaultData(defaultValue); | ||
} | ||
/** | ||
* Asynchronously determines MIME type of input | ||
@@ -108,13 +69,10 @@ * @param {String|Buffer|Uint8Array|ArrayBuffer|ReadableStream} input | ||
if (await isExists(input)) { | ||
const { bytesRead, buffer } = await chunkAsync({ path: input, flags: 'r' }); | ||
if (bytesRead && buffer) { | ||
file = fileType(buffer); | ||
} | ||
file = await fromFile(input); | ||
} | ||
} else if (isBuffer(input)) { | ||
file = fileType(input); | ||
file = await fromBuffer(input); | ||
} else if (isStream(input)) { | ||
const chunk = await streamChunk(input); | ||
if (chunk) { | ||
file = fileType(chunk); | ||
file = await fromBuffer(chunk); | ||
} | ||
@@ -131,4 +89,3 @@ } | ||
module.exports = async; | ||
module.exports.sync = sync; | ||
module.exports.async = async; | ||
module.exports.BUFFER_LENGTH = BUFFER_LENGTH; |
{ | ||
"name": "mime-kind", | ||
"author": "Alexey Bystrov <strikeentco@gmail.com>", | ||
"version": "3.0.0", | ||
"description": "Detect the MIME type of a Buffer, Uint8Array, ArrayBuffer, ReadableStream, file path and file name, with sync and async methods.", | ||
"version": "4.0.0", | ||
"description": "Detect the MIME type of a Buffer, Uint8Array, ArrayBuffer, ReadableStream, file path and file name, with async method.", | ||
"engines": { | ||
"node": ">=8.3.0" | ||
"node": ">=10" | ||
}, | ||
@@ -31,4 +31,3 @@ "keywords": [ | ||
"check": "npm run lint && npm run test", | ||
"cover": "nyc ./node_modules/mocha/bin/_mocha && nyc report --reporter=html", | ||
"test-on-travis": "nyc ./node_modules/mocha/bin/_mocha && nyc report --reporter=lcovonly" | ||
"cover": "nyc ./node_modules/mocha/bin/_mocha && nyc report --reporter=html" | ||
}, | ||
@@ -43,12 +42,10 @@ "repository": { | ||
"dependencies": { | ||
"file-type": "^12.1.0", | ||
"file-type": "^16.5.4", | ||
"mime-types": "^2.1.24" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^5.16.0", | ||
"eslint-config-airbnb": "^17.1.1", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-plugin-react": "^7.14.3", | ||
"mocha": "^6.2.0", | ||
"eslint": "^8.24.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"mocha": "^6.2.3", | ||
"nyc": "^14.1.1", | ||
@@ -55,0 +52,0 @@ "should": "^13.2.3" |
@@ -5,3 +5,3 @@ mime-kind [![License](https://img.shields.io/npm/l/mime-kind.svg)](https://github.com/strikeentco/mime-kind/blob/master/LICENSE) [![npm](https://img.shields.io/npm/v/mime-kind.svg)](https://www.npmjs.com/package/mime-kind) | ||
Detect the MIME type of a `Buffer`, `Uint8Array`, `ArrayBuffer`, `ReadableStream`, file path and file name, with `sync` and `async` methods. | ||
Detect the MIME type of a `Buffer`, `Uint8Array`, `ArrayBuffer`, `ReadableStream`, file path and file name, with `async` method. | ||
@@ -17,8 +17,7 @@ ## Install | ||
const { createReadStream } = require('fs'); | ||
const { sync: mime, async: mimeAsync } = require('mime-kind'); | ||
const mime = require('mime-kind'); | ||
mime('c:/anonim.jpeg'); // -> { ext: 'jpeg', mime: 'image/jpeg' } | ||
mime('.fakeext'); // -> null | ||
mime(createReadStream('./anonim.jpg')); // -> { ext: 'jpeg', mime: 'image/jpeg' } | ||
await mimeAsync('c:/anonim.jpeg'); // -> { ext: 'jpeg', mime: 'image/jpeg' } | ||
await mime('.fakeext'); // -> null | ||
await mime(createReadStream('./anonim.jpg')); // -> { ext: 'jpeg', mime: 'image/jpeg' } | ||
await mime('c:/anonim.jpeg'); // -> { ext: 'jpeg', mime: 'image/jpeg' } | ||
``` | ||
@@ -65,30 +64,5 @@ | ||
### mime.sync(input, [defaultValue]) | ||
Returns an object (or `null` when no match) with: | ||
* `ext` - file type | ||
* `mime` - the [MIME type](http://en.wikipedia.org/wiki/Internet_media_type) | ||
This methods supports only `fs.ReadStream` as `ReadableStream`. | ||
### Params: | ||
* **input** (*String|Buffer|Uint8Array|ArrayBuffer|ReadableStream*) - `Buffer`, `Uint8Array`, `ArrayBuffer`, `ReadableStream`, file path or file name. | ||
* **[defaultValue]** (*String|Object*) - `String` or `Object` with value which will be returned if no match will be found. If `defaultValue` is incorrect returns `null`. | ||
```js | ||
const mime = require('mime-kind'); | ||
mime.sync('.fakeext', 'application/octet-stream'); // -> { ext: 'bin', mime: 'application/octet-stream' } | ||
mime.sync('.fakeext', { ext: 'mp4', mime: 'video/mp4' }); // -> { ext: 'mp4', mime: 'video/mp4' } | ||
mime.sync('.fakeext', 'ogg'); // -> { ext: 'ogg', mime: 'audio/ogg' } | ||
// but | ||
mime.sync('.fakeext', 'ogg3'); // -> null | ||
mime.sync('.fakeext', { ext: 'fake', mime: 'fake/fake' }); // -> null | ||
``` | ||
## License | ||
The MIT License (MIT)<br/> | ||
Copyright (c) 2015-2019 Alexey Bystrov | ||
Copyright (c) 2015-2022 Alexey Bystrov |
70
utils.js
'use strict'; | ||
const { promisify } = require('util'); | ||
const { | ||
accessSync, openSync, readSync, closeSync, | ||
access, open, read, close | ||
} = require('fs'); | ||
const { access } = require('fs'); | ||
const { normalize } = require('path'); | ||
@@ -13,5 +10,2 @@ | ||
const accessAsync = promisify(access); | ||
const openAsync = promisify(open); | ||
const readAsync = promisify(read); | ||
const closeAsync = promisify(close); | ||
@@ -58,16 +52,2 @@ /** | ||
* @param {String} path | ||
* @returns {Boolean} | ||
*/ | ||
function isExistsSync(path) { | ||
try { | ||
accessSync(normalize(path)); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
/** | ||
* Tests whether or not the given path exists | ||
* @param {String} path | ||
* @returns {Promise<Boolean>} | ||
@@ -86,47 +66,2 @@ * @async | ||
/** | ||
* Read a chunk from a file | ||
* @param {Object} data | ||
* @param {String} [data.path] | ||
* @param {String} [data.flags] | ||
* @param {String} [data.mode] | ||
* @returns {{bytesRead: Number, buffer: Buffer}} | ||
*/ | ||
function chunkSync(data) { | ||
if (!data.fd && data.path) { | ||
data.path = normalize(data.path); | ||
if (isExistsSync(data.path)) { | ||
data.fd = openSync(data.path, data.flags, data.mode); | ||
} | ||
} | ||
if (data.fd) { | ||
try { | ||
const buffer = Buffer.alloc(BUFFER_LENGTH); | ||
const bytesRead = readSync(data.fd, buffer, 0, BUFFER_LENGTH); | ||
return { bytesRead, buffer }; | ||
} finally { | ||
closeSync(data.fd); | ||
} | ||
} | ||
throw new Error('The file must be local and exists.'); | ||
} | ||
/** | ||
* Read a chunk from a file | ||
* @param {Object} data | ||
* @param {String} data.path | ||
* @param {String} data.flags | ||
* @param {String} [data.mode] | ||
* @returns {Promise<{bytesRead: Number, buffer: Buffer}>} | ||
* @async | ||
*/ | ||
async function chunkAsync(data) { | ||
const fd = await openAsync(normalize(data.path), data.flags, data.mode); | ||
try { | ||
return await readAsync(fd, Buffer.alloc(BUFFER_LENGTH), 0, BUFFER_LENGTH, 0); | ||
} finally { | ||
await closeAsync(fd); | ||
} | ||
} | ||
/** | ||
* Read a chunk from a stream | ||
@@ -156,5 +91,2 @@ * @param {ReadableStream} stream | ||
isExists, | ||
isExistsSync, | ||
chunkAsync, | ||
chunkSync, | ||
streamChunk, | ||
@@ -161,0 +93,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
6
0
8875
163
66
+ Added@tokenizer/token@0.3.0(transitive)
+ Addedfile-type@16.5.4(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedpeek-readable@4.1.0(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedreadable-web-to-node-stream@3.0.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstrtok3@6.3.0(transitive)
+ Addedtoken-types@4.2.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedfile-type@12.4.2(transitive)
Updatedfile-type@^16.5.4