Query the MIME DB on systems that support it.
Query the MIME DB on systems that support it.
Essentially equivalent to file --mime-type, but faster, as we don't spawn an additional process.
This will fail when:
1. the file does not have any known filetype;
2. the system doesn't have a mime db, or;
3. libmagic is not installed.
See JSDocs for more detailed documentation. We recommend falling back to a traditional filepath-based solution, such as mime-types, when this package errors.
We depend on CMake to be installed system-wide to build the node bindings. Additionally, on most systems, a package like g++ will be required aswell.
If ninja is installed, cmake-js will use it, however you can build without it.
Both at compile and runtime, the magic library is required, however to run on systems without it, we won't error if it isn't present at installation time. Instead, you will get a ERR_NOT_BUILD_WITH_LIBMAGIC (when the compiler couldn't link against it) or ERR_NO_LIBMAGIC (when it's not loadable at runtime) when calling query(). You can programmatically check if you're encounting one of these via the QueryException.code property.
Assuming the above section has been fulfilled, pnpm i querymimedb
``ts`
import fs from 'fs';
import query from 'querymimedb';
fs.writeFileSync('test.txt', '');
query('./test.txt'); // => inode/x-empty
fs.writeFileSync('test.txt', 'Some Contents');
query('./test.txt'); // => text/plain
`ts``
const fs = require('fs');
const { query } = require('querymimedb');
fs.writeFileSync('test.txt', '');
query('./test.txt'); // => inode/x-empty
fs.writeFileSync('test.txt', 'Some Contents');
query('./test.txt'); // => text/plain