Telegram Web, preconfigured for usage in I2P. http://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.2 KiB

11 years ago
var fs = require('graceful-fs');
var crypto = require('crypto');
var mm = require('minimatch');
var log = require('./logger').create('preprocess');
var sha1 = function(data) {
var hash = crypto.createHash('sha1');
hash.update(data);
return hash.digest('hex');
};
// TODO(vojta): instantiate preprocessors at the start to show warnings immediately
var createPreprocessor = function(config, basePath, injector) {
var patterns = Object.keys(config);
var alreadyDisplayedWarnings = Object.create(null);
return function(file, done) {
var preprocessors = [];
var nextPreprocessor = function(content) {
if (!preprocessors.length) {
return fs.writeFile(file.contentPath, content, function() {
done();
});
}
preprocessors.shift()(content, file, nextPreprocessor);
};
var instantiatePreprocessor = function(name) {
if (alreadyDisplayedWarnings[name]) {
return;
}
try {
preprocessors.push(injector.get('preprocessor:' + name));
} catch (e) {
if (e.message.indexOf('No provider for "preprocessor:' + name + '"') !== -1) {
log.warn('Can not load "%s", it is not registered!\n ' +
'Perhaps you are missing some plugin?', name);
} else {
log.warn('Can not load "%s"!\n ' + e.stack, name);
}
alreadyDisplayedWarnings[name] = true;
}
};
// collects matching preprocessors
// TODO(vojta): should we cache this ?
for (var i = 0; i < patterns.length; i++) {
if (mm(file.originalPath, patterns[i])) {
config[patterns[i]].forEach(instantiatePreprocessor);
}
}
if (preprocessors.length) {
return fs.readFile(file.originalPath, function(err, buffer) {
// TODO(vojta): extract get/create temp dir somewhere else (use the same for launchers etc)
var env = process.env;
var tmp = env.TMPDIR || env.TMP || env.TEMP || '/tmp';
file.contentPath = tmp + '/' + sha1(file.originalPath) + '.js';
nextPreprocessor(buffer.toString());
});
}
return process.nextTick(done);
};
};
createPreprocessor.$inject = ['config.preprocessors', 'config.basePath', 'injector'];
exports.createPreprocessor = createPreprocessor;