Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make types.json loading optional? #129

Closed
Hypercubed opened this issue Aug 19, 2015 · 2 comments
Closed

Make types.json loading optional? #129

Hypercubed opened this issue Aug 19, 2015 · 2 comments

Comments

@Hypercubed
Copy link

I am using this module in a web application installed and bundled using JSPM. It works fine from what I can tell. However, I am actually only using a subset of the mime types. I can use mime.define but because mime.js requires types.json the entire file is bundled anyway when I build the application. This is probably not an issue since types.json is fairly small but for an optimal web app I see three options:

  1. Fork this module to be API only, doesn't load types.json at all, use mime.define in my app.
  2. Fork this module to create a smaller custom types.json file.
  3. Somehow modify this module to load types.json only when needed.

Thoughts?

@Hypercubed
Copy link
Author

Hello again. I would really like to use this library without loading the default database. Here is what I came up with: https://github.com/Hypercubed/node-mime .

With this fork I can do:

var MimeLookup = require('mime');
var mime = new MimeLookup(require('mime-db'));

The API is not not compatible and should likely be a separate npm module. I'd like to suggest that you or I publish a separate module (published to npm as mime-lookup) that your module (mime) requires. Then your mime.js can be simply:

var MimeLookup = require('mime-lookup');

// Default instance
var mime = new MimeLookup(require('./types.json'));

// Default type
mime.default_type = mime.lookup('bin');

//
// Additional API specific to the default instance
//

mime.Mime = MimeLookup;

/**
 * Lookup a charset based on mime type.
 */
mime.charsets = {
  lookup: function(mimeType, fallback) {
    // Assume text types are utf8
    return (/^text\//).test(mimeType) ? 'UTF-8' : fallback;
  }
};

module.exports = mime;

I'm willing to do this through PRs.

@broofa
Copy link
Owner

broofa commented Jun 15, 2017

Moving to #161

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants