jshttp/mime-db
The canonical MIME type database from jshttp, published as JSON. It maps file extensions to media types and includes metadata such as compression and charset, powering Node.js HTTP libraries like mime-types and Express.
jshttp/mime-db package provides a structured, up-to-date database of MIME types (media types) for HTTP responses, file handling, and content negotiation. It is a lightweight, dependency-free solution ideal for:
Content-Type headers.Storage, Response facades).Accept headers in requests).Response, File, and Request components.mime-types package (if used) or replaces it for stricter control..htaccess MIME mappings).MimeType class or custom logic.require and optional facade/class wrapper.Illuminate\Http\Response to auto-detect MIME types.storing event in HasFile models).Content-Type headers inbound/outbound.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Data Staleness | Medium | Schedule periodic updates via Laravel Tasks or CI/CD. |
| Incomplete Coverage | Low | Fallback to Laravel’s default mime-types or IANA API. |
| Performance Overhead | Low | Cache the DB in memory (e.g., static property). |
| Breaking Changes | Low | MIT license allows forks; monitor upstream. |
| Testing Gaps | Medium | Unit test edge cases (e.g., application/octet-stream fallbacks). |
application/octet-stream?)mime-types or spatie/mime-types (if active maintenance is preferred).require jshttp/mime-db.application/javascript).Content-Type.Response::make(), Storage::disk()).mime-db.finfo_file or mime-types).Response::header('Content-Type')).File model observers).mime-db for known types, fall back to finfo for unknowns.| Component | Compatibility Notes |
|---|---|
| Laravel Core | No conflicts; replaces or augments existing MIME logic. |
| Third-Party Packages | May need updates if they rely on mime-types (e.g., intervention/image). |
| Legacy Code | Hardcoded MIME types may need refactoring (e.g., application/json → DB lookup). |
| Cloud Storage | Works with S3, GCS, etc., but MIME detection must be handled client-side if needed. |
composer require jshttp/mime-db
config/mime.php (if extending).// app/Providers/MimeServiceProvider.php
public function register()
{
$this->app->singleton('mime-db', function () {
return require __DIR__.'/../../vendor/jshttp/mime-db/mime.json';
});
}
// app/Facades/Mime.php
public static function getType(string $extension): string
{
return data_get(self::db(), $extension, 'application/octet-stream');
}
return response()->make($content, 200, [
'Content-Type' => Mime::getType($file->extension),
]);
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'file' => 'required|mimes:' . implode(',', array_keys(Mime::db())),
]);
composer update jshttp/mime-db in CI/CD (e.g., GitHub Actions).php artisan mime:validate).config/mime.php or a migration:
// config/mime.php
'extensions' => [
'js' => 'application/javascript',
'custom' => 'application/x-custom',
],
if (!array_key_exists($extension, Mime::db())) {
Log::warning("Unsupported MIME type for extension: {$extension}");
}
php artisan mime:dump
jshttp/mime-db (e.g., missing types like application/merge-patch).static or Redis (TTL: 1 day):
if (!app()->bound('cached-mime-db')) {
Cache::remember('mime-db', now()->addDay(), function () {
return require __DIR__.'/../../vendor/jshttp/mime-db/mime.json';
});
}
10K requests/sec to ensure <1ms lookup time.How can I help you explore Laravel packages today?