From d562383873d54a9ec29142d65441fc19df33aff1 Mon Sep 17 00:00:00 2001 From: comdiv Date: Wed, 2 Dec 2015 00:04:49 +0500 Subject: [PATCH] feat(config): mime config option support Allows to setup required mime-type to extensions mapping (for ex. fix default *.ts to video/mp2t mapping) Closes #1735 --- docs/config/01-configuration-file.md | 17 +++++++++++++++++ lib/middleware/common.js | 13 +++++++++++++ lib/web-server.js | 1 + test/unit/web-server.spec.js | 12 +++++++++++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/config/01-configuration-file.md b/docs/config/01-configuration-file.md index 2888cb30d..9b40cb280 100644 --- a/docs/config/01-configuration-file.md +++ b/docs/config/01-configuration-file.md @@ -441,6 +441,23 @@ Note: Just about all additional reporters in Karma (other than progress) require If `true`, Karma will start and capture all configured browsers, run tests and then exit with an exit code of `0` or `1` depending on whether all tests passed or any tests failed. +## mime +**Type:** Object + +**Default:** `{}` + +**Description:** Redefine default mapping from file extensions to MIME-type + +Set property name to required MIME, provide Array of extensions (without dots) as it's value + +**Example:** +```javascript +mime: { + 'text/x-typescript': ['ts','tsx'] + 'text/plain' : ['mytxt'] +} +``` + ## transports **Type:** Array diff --git a/lib/middleware/common.js b/lib/middleware/common.js index b13433e64..510819e1b 100644 --- a/lib/middleware/common.js +++ b/lib/middleware/common.js @@ -4,6 +4,8 @@ var mime = require('mime') var log = require('../logger').create('web-server') +var helper = require('../helper') +var _ = helper._ var PromiseContainer = function () { var promise @@ -92,9 +94,20 @@ var setHeavyCacheHeaders = function (response) { response.setHeader('Cache-Control', 'public, max-age=31536000') } +var initializeMimeTypes = function (config) { + if (config && config.mime) { + _.forEach(config.mime, function (value, key) { + var map = {} + map[key] = value + mime.define(map) + }) + } +} + // PUBLIC API exports.PromiseContainer = PromiseContainer exports.createServeFile = createServeFile exports.setNoCacheHeaders = setNoCacheHeaders exports.setHeavyCacheHeaders = setHeavyCacheHeaders +exports.initializeMimeTypes = initializeMimeTypes exports.serve404 = serve404 diff --git a/lib/web-server.js b/lib/web-server.js index fbb9c6eb4..89b5165bd 100644 --- a/lib/web-server.js +++ b/lib/web-server.js @@ -31,6 +31,7 @@ createCustomHandler.$inject = ['customFileHandlers', 'config.basePath'] var createWebServer = function (injector, emitter, fileList) { var config = injector.get('config') + common.initializeMimeTypes(config) var serveStaticFile = common.createServeFile(fs, path.normalize(__dirname + '/../static'), config) var serveFile = common.createServeFile(fs, null, config) var filesPromise = new common.PromiseContainer() diff --git a/test/unit/web-server.spec.js b/test/unit/web-server.spec.js index 4592f5b1e..7d311ba1e 100644 --- a/test/unit/web-server.spec.js +++ b/test/unit/web-server.spec.js @@ -4,6 +4,7 @@ import request from 'supertest-as-promised' import di from 'di' import mocks from 'mocks' import fs from 'fs' +import mime from 'mime' describe('web-server', () => { var server @@ -45,7 +46,8 @@ describe('web-server', () => { basePath: '/base/path', urlRoot: '/', middleware: ['custom'], - middlewareResponse: 'hello middleware!' + middlewareResponse: 'hello middleware!', + mime: {'custom/custom': ['custom']} } var injector = new di.Injector([{ @@ -71,6 +73,14 @@ describe('web-server', () => { server = injector.invoke(m.createWebServer) }) + it('should setup mime', () => { + expect(mime.lookup('/my.custom')).to.equal('custom/custom') + }) + + it('should keep default mimes', () => { + expect(mime.lookup('/my.html')).to.equal('text/html') + }) + it('should serve client.html', () => { servedFiles(new Set())