What is @hapi/mimos?
@hapi/mimos is a utility library for handling and managing MIME types. It provides a way to look up MIME types based on file extensions, and vice versa, and allows for custom MIME type definitions.
What are @hapi/mimos's main functionalities?
Lookup MIME type by file extension
This feature allows you to look up the MIME type based on a file extension. In this example, the MIME type for a .txt file is retrieved.
const Mimos = require('@hapi/mimos');
const mimos = new Mimos();
const mimeType = mimos.path('file.txt');
console.log(mimeType); // { source: 'iana', compressible: true, charset: 'UTF-8', type: 'text/plain' }
Lookup file extension by MIME type
This feature allows you to look up the file extension based on a MIME type. In this example, the file extensions for the MIME type 'text/html' are retrieved.
const Mimos = require('@hapi/mimos');
const mimos = new Mimos();
const extension = mimos.type('text/html');
console.log(extension); // { source: 'iana', compressible: true, charset: 'UTF-8', type: 'text/html', extensions: [ 'html', 'htm' ] }
Custom MIME type definitions
This feature allows you to define custom MIME types. In this example, a custom MIME type 'application/x-custom' is defined and then retrieved based on the file extension.
const Mimos = require('@hapi/mimos');
const mimos = new Mimos({
override: {
'application/x-custom': {
source: 'custom',
compressible: true,
extensions: ['custom']
}
}
});
const customType = mimos.path('file.custom');
console.log(customType); // { source: 'custom', compressible: true, type: 'application/x-custom', extensions: [ 'custom' ] }
Other packages similar to @hapi/mimos
mime
The 'mime' package is a popular library for working with MIME types. It provides similar functionality to @hapi/mimos, such as looking up MIME types based on file extensions and vice versa. However, it does not support custom MIME type definitions as flexibly as @hapi/mimos.
mime-types
The 'mime-types' package is another library for handling MIME types. It offers a comprehensive list of MIME types and allows for looking up MIME types by extension and vice versa. It is similar to @hapi/mimos but does not provide the same level of customization for MIME type definitions.
mimos
Mime database interface.
Mimos is a convenience class for retrieving mime information objects.
Usage
new Mimos([options])
Creates a new Mimos object where:
[options]
- an option object the following keys
[override]
- an object hash that is merged into the built in mime information specified here. Each key value pair represents a single mime object. Each override value should follow this schema:
key
- the key is the lower-cased correct mime-type. (Ex. "application/javascript").value
- the value should an object following the specifications outlined here. Additional values include:
type
- specify the type
value of result objects, defaults to key
. See the example below for more clarification.predicate
- method with signature function(mime)
when this mime type is found in the database, this function will run. This allows you make customizations to mime
based on developer criteria.
mimos.path(path)
Returns mime object where:
path
path to file including the file extension. Uses the extension
values of the mime objects for lookup.
const mimos = new Mimos();
const mime = mimos.path('/static/public/app.js');
mimos.type(type)
Returns mime object where:
type
the content-type to find mime information about. Uses the type
values of the mime objects for lookup.
const mimos = new Mimos();
const mime = mimos.type('text/plain');
Override Example
In certain situations, it can be helpful to override the built in mime type information. The optional argument to the Mimos constructor is used to override and add mime information. Below is an example to help understand how this works.
const options = {
override: {
'node/module': {
source: 'iana',
compressible: true,
extensions: ['node', 'module', 'npm'],
type: 'node/module'
},
'application/javascript': {
source: 'iana',
charset: 'UTF-8',
compressible: true,
extensions: ['js', 'javascript'],
type: 'text/javascript'
},
'text/html': {
predicate: function(mime) {
if (someCondition) {
mime.foo = 'test';
}
else {
mime.foo = 'bar';
}
return mime;
}
}
}
}
const mimos = new Mimos(options);
console.dir(mimos.path('./node_modules/mimos.module'));
console.dir(mimos.type('application/javascript'));
console.dir(mimos.type('text/html'));