Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma / lib / preprocessor.js
1 var path = require('path')
2 var fs = require('graceful-fs')
3 var crypto = require('crypto')
4 var mm = require('minimatch')
5 var extensions = require('./binary-extensions.json').extensions
6
7 var log = require('./logger').create('preprocess')
8
9 var sha1 = function (data) {
10   var hash = crypto.createHash('sha1')
11   hash.update(data)
12   return hash.digest('hex')
13 }
14
15 var isBinary = Object.create(null)
16 extensions.forEach(function (extension) {
17   isBinary['.' + extension] = true
18 })
19
20 // TODO(vojta): instantiate preprocessors at the start to show warnings immediately
21 var createPreprocessor = function (config, basePath, injector) {
22   var alreadyDisplayedWarnings = Object.create(null)
23
24   return function (file, done) {
25     var patterns = Object.keys(config)
26     var thisFileIsBinary = isBinary[path.extname(file.originalPath)]
27     var preprocessors = []
28     var nextPreprocessor = function (error, content) {
29       // normalize B-C
30       if (arguments.length === 1 && typeof error === 'string') {
31         content = error
32         error = null
33       }
34
35       if (error) {
36         file.content = null
37         file.contentPath = null
38         return done(error)
39       }
40
41       if (!preprocessors.length) {
42         file.contentPath = null
43         file.content = content
44         file.sha = sha1(content)
45         return done()
46       }
47
48       preprocessors.shift()(content, file, nextPreprocessor)
49     }
50     var instantiatePreprocessor = function (name) {
51       if (alreadyDisplayedWarnings[name]) {
52         return
53       }
54
55       try {
56         preprocessors.push(injector.get('preprocessor:' + name))
57       } catch (e) {
58         if (e.message.indexOf('No provider for "preprocessor:' + name + '"') !== -1) {
59           log.warn('Can not load "%s", it is not registered!\n  ' +
60             'Perhaps you are missing some plugin?', name)
61         } else {
62           log.warn('Can not load "%s"!\n  ' + e.stack, name)
63         }
64
65         alreadyDisplayedWarnings[name] = true
66       }
67     }
68
69     // collects matching preprocessors
70     // TODO(vojta): should we cache this ?
71     for (var i = 0; i < patterns.length; i++) {
72       if (mm(file.originalPath, patterns[i])) {
73         if (thisFileIsBinary) {
74           log.warn('Ignoring preprocessing (%s) %s because it is a binary file.',
75             config[patterns[i]].join(', '), file.originalPath)
76         } else {
77           config[patterns[i]].forEach(instantiatePreprocessor)
78         }
79       }
80     }
81
82     return fs.readFile(file.originalPath, function (err, buffer) {
83       if (err) {
84         throw err
85       }
86       nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
87     })
88   }
89 }
90 createPreprocessor.$inject = ['config.preprocessors', 'config.basePath', 'injector']
91
92 exports.createPreprocessor = createPreprocessor