Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
0.2.0. fix #44
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish committed Jan 20, 2016
1 parent 9e15d2b commit dc6cf75
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 442 deletions.
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ app.use(minify({
json_match: /json/,
uglifyJS: undefined,
cssmin: undefined,
cache: false
cache: false,
onerror: undefined,
}));
```

Expand Down Expand Up @@ -80,6 +81,10 @@ app.use(minify({

the directory for cache storage (must be writeable). Pass `false` to cache in the memory (not recommended).

- `onerror`: `Function`

handle compiling error or minifying errors. You can determine what to respond when facing such errors. See [Customize onError behavior](#customize-onrrror-behavior) for details.

## Per-Response Options

- `res._skip`
Expand Down Expand Up @@ -190,6 +195,54 @@ app.use(minify());

**Change since 0.1.6**: You need to manually install those modules to enable this feature.

## Customize onError behavior

Errors thrown by CoffeeScript/LESS/SASS/Stylus module are compiling errors and errors thrown by UglifyJS/cssmin/JSON module are minifying errors.

The default behavior is to return the error message for compiling errors and return the original content for minifying errors.

You can change this behavior or get notified about the error by providing `onerror` in options:

```javascript
var minify = require('express-minify');

/*
err: the Error object
stage: "compile" or "minify"
assetType: The type of the source code, can be one of
minify.Minifier.TYPE_TEXT
minify.Minifier.TYPE_JS
minify.Minifier.TYPE_CSS
minify.Minifier.TYPE_SASS
minify.Minifier.TYPE_LESS
minify.Minifier.TYPE_STYLUS
minify.Minifier.TYPE_COFFEE
minify.Minifier.TYPE_JSON
minifyOptions: Minification options
body: Source code (string)
callback: (err, body) return the final result as string in this callback function.
If `err` is null, the final result will be cached.
*/

var myErrorHandler = function (err, stage, assetType, minifyOptions, body, callback) {
console.log(err);
// below is the default implementation
if (stage === 'compile') {
callback(err, JSON.stringify(err));
return;
}
callback(err, body);
};

app.use(minify({
onerror: myErrorHandler
}));
```

You can also access the default implementation from `minify.Minifier.defaultErrorHandler`.

## Customize UglifyJS/cssmin instance

If you want to use your own UglifyJS/cssmin instance (for example, use a different branch to support ES6), you can pass them to the options.
Expand Down Expand Up @@ -303,6 +356,11 @@ If you are using `cluster`, it is strongly recommended to enable file cache. The

# Change log

0.2.0

- Support `onerror`
- Minimum required NodeJs version changed to `0.12.0` for `fs.access`.

0.1.7

- Support customizing UglifyJS/cssmin instance
Expand Down
71 changes: 71 additions & 0 deletions cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var fs = require('fs');
var path = require('path');

var FileCache = function (basePath) {
this.basePath = basePath;
};

FileCache.prototype.get = function (hash, callback) {
var destPath = this.basePath + hash;
fs.readFile(destPath, {encoding: 'utf8'}, function (err, data) {
if (err) {
callback(err);
return;
}
callback(null, data.toString());
});
};

FileCache.prototype.put = function (hash, minized, callback) {
var destPath = this.basePath + hash;
var tempPath = destPath + '.tmp';
// fix issue #3
fs.writeFile(tempPath, minized, {encoding: 'utf8'}, function (err) {
if (err) {
callback(err);
return;
}
fs.rename(tempPath, destPath, callback);
});
};

var MemoryCache = function () {
this.cache = {};
};

MemoryCache.prototype.get = function (hash, callback) {
if (!this.cache.hasOwnProperty(hash)) {
callback(new Error('miss'));
} else {
callback(null, this.cache[hash]);
}
};

MemoryCache.prototype.put = function (hash, minized, callback) {
this.cache[hash] = minized;
callback();
};

/**
* @param {String|false} cacheDirectory false == use memory cache
*/
var Cache = function (cacheDirectory) {
this.isFileCache = (cacheDirectory !== false);
if (this.isFileCache) {
// whether the directory is writeable
cacheDirectory = path.normalize(cacheDirectory + '/').toString();
try {
fs.accessSync(cacheDirectory, fs.W_OK);
} catch (ignore) {
console.log('WARNING: express-minify cache directory is not writeable, fallback to memory cache.');
this.isFileCache = false;
}
}
if (this.isFileCache) {
this.layer = new FileCache(cacheDirectory);
} else {
this.layer = new MemoryCache();
}
};

module.exports = Cache;
187 changes: 187 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
var crypto = require('crypto');
var onHeaders = require('on-headers');

var Factory = function (options) {
return createMiddleware(options);
};

Factory.Cache = require('./cache.js');
Factory.Minifier = require('./minifier.js');

module.exports = Factory;

var createMiddleware = function express_minify(options) {
options = options || {};

var js_match = options.js_match || /javascript/;
var css_match = options.css_match || /css/;
var sass_match = options.sass_match || /scss/;
var less_match = options.less_match || /less/;
var stylus_match = options.stylus_match || /stylus/;
var coffee_match = options.coffee_match || /coffeescript/;
var json_match = options.json_match || /json/;

var cache = new Factory.Cache(options.cache || false);
var minifier = new Factory.Minifier(options.uglifyJS, options.cssmin, options.onerror);

return function express_minify_middleware(req, res, next) {
var write = res.write;
var end = res.end;

var buf = null;
var type = Factory.Minifier.TYPE_TEXT;

onHeaders(res, function () {
if (req.method === 'HEAD') {
return;
}

if (res._skip) {
return;
}

var contentType = res.getHeader('Content-Type');
if (contentType === undefined) {
return;
}

// for sass, less, stylus, coffee module:
// null: module is found but not loaded
// false: module not found
// so we should not process false values, but allow null values
if (minifier.sass !== false && sass_match.test(contentType)) {
type = Factory.Minifier.TYPE_SASS;
res.setHeader('Content-Type', 'text/css');
} else if (minifier.less !== false && less_match.test(contentType)) {
type = Factory.Minifier.TYPE_LESS;
res.setHeader('Content-Type', 'text/css');
} else if (minifier.stylus !== false && stylus_match.test(contentType)) {
type = Factory.Minifier.TYPE_STYLUS;
res.setHeader('Content-Type', 'text/css');
} else if (minifier.coffee !== false && coffee_match.test(contentType)) {
type = Factory.Minifier.TYPE_COFFEE;
res.setHeader('Content-Type', 'text/javascript');
} else if (json_match.test(contentType)) {
type = Factory.Minifier.TYPE_JSON;
} else if (js_match.test(contentType)) {
type = Factory.Minifier.TYPE_JS;
} else if (css_match.test(contentType)) {
type = Factory.Minifier.TYPE_CSS;
}

if (type === Factory.Minifier.TYPE_TEXT) {
return;
}

if ((type === Factory.Minifier.TYPE_JS || type === Factory.Minifier.TYPE_CSS) && res._no_minify) {
return;
}

res.removeHeader('Content-Length');

// prepare the buffer
buf = [];
});

res.write = function (chunk, encoding) {
if (!this._header) {
this._implicitHeader();
}

if (buf === null) {
return write.call(this, chunk, encoding);
}

if (!this._hasBody) {
return true;
}

if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk)) {
throw new TypeError('first argument must be a string or Buffer');
}

if (chunk.length === 0) {
return true;
}

// no chunked_encoding here
if (typeof chunk === 'string') {
chunk = new Buffer(chunk, encoding);
}

buf.push(chunk);
};

res.end = function (data, encoding) {
if (this.finished) {
return false;
}

if (!this._header) {
this._implicitHeader();
}

if (data && !this._hasBody) {
data = false;
}

if (buf === null) {
return end.call(this, data, encoding);
}

// TODO: implement hot-path optimization
if (data) {
this.write(data, encoding);
}

var buffer = Buffer.concat(buf);

// prepare uglify options
var uglifyOptions = {};
if (this._no_mangle) {
uglifyOptions.mangle = false;
}
if (this._uglifyMangle !== undefined) {
uglifyOptions.mangle = this._uglifyMangle;
}
if (this._uglifyOutput !== undefined) {
uglifyOptions.output = this._uglifyOutput;
}
if (this._uglifyCompress !== undefined) {
uglifyOptions.compress = this._uglifyCompress;
}

var minifyOptions = {
uglifyOpt: uglifyOptions,
noMinify: this._no_minify
};

var cacheKey = crypto.createHash('sha1').update(JSON.stringify(minifyOptions) + buffer).digest('hex').toString();
var self = this;

cache.layer.get(cacheKey, function (err, minized) {
if (err) {
// cache miss
minifier.compileAndMinify(type, minifyOptions, buffer.toString(encoding), function (err, minized) {
if (self._no_cache || err) {
// do not cache the response body
write.call(self, minized, 'utf8');
end.call(self);
} else {
cache.layer.put(cacheKey, minized, function () {
write.call(self, minized, 'utf8');
end.call(self);
});
}
});
} else {
// cache hit
write.call(self, minized, 'utf8');
end.call(self);
}
});
};

next();
};
};
Loading

0 comments on commit dc6cf75

Please sign in to comment.