querymimedb
v0.1.16
Published
Query the MIME DB on systems that support it.
Downloads
29
Readme
querymimedb
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:
- the file does not have any known filetype;
- the system doesn't have a mime db, or;
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.
System Dependencies
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.
Installation
Assuming the above section has been fulfilled, pnpm i querymimedb
Example Usage
ESM
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
CJS
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