This repository has been archived by the owner on Oct 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
144 lines (120 loc) · 3.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"use strict";
var etag = require("etag");
var fresh = require("fresh");
var fs = require("fs");
var path = require("path");
var zlib = require("zlib");
var minifiedScript = path.join(__dirname, "/dist/index.min.js");
var unminifiedScript = path.join(__dirname, "/dist/index.js");
/**
* Does the current request support compressed encoding?
* @param {Object} req
* @returns {boolean}
*/
function supportsGzip (req) {
var accept = req.headers['accept-encoding'];
return accept && accept.indexOf('gzip') > -1;
}
/**
* Set headers on the response
* @param {Object} res
* @param {String} body
*/
function setHeaders(res, body) {
res.setHeader("Cache-Control", "public, max-age=0");
res.setHeader("Content-Type", "text/javascript");
res.setHeader("ETag", etag(body));
}
/**
* @param {Object} options
* @param {String} connector
* @returns {String}
*/
function getScriptBody(options, connector) {
var script = minifiedScript;
if (options && !options.minify) {
script = unminifiedScript;
}
return connector + fs.readFileSync(script);
}
/**
* @param {Object} req
* @returns {String}
*/
function isConditionalGet(req) {
return req.headers["if-none-match"] || req.headers["if-modified-since"];
}
/**
* Return a not-modified response
* @param {Object} res
*/
function notModified(res) {
res.removeHeader("Content-Type");
res.statusCode = 304;
res.end();
}
/**
* Public method for returning either a middleware fn
* or the content as a string
* @param {Object} options
* @param {String} connector - content to be prepended
* @param {String} type - either `file` or `middleware`
* @returns {*}
*/
function init(options, connector, type) {
var gzipCached;
/**
* Combine string to create the final version
* @type {String}
*/
var requestBody = getScriptBody(options, connector);
/**
* If the user asked for a file, simply return the string.
*/
if (type && type === "file") {
return requestBody;
}
/**
* Otherwise return a function to be used a middleware
*/
return function (req, res) {
/**
* default to using the uncompressed string
* @type {String}
*/
var output = requestBody;
/**
* Set the appropriate headers for caching
*/
setHeaders(res, output);
if (isConditionalGet(req) && fresh(req.headers, res._headers)) {
return notModified(res);
}
/**
* If gzip is supported, compress the string once
* and save for future requests
*/
if (supportsGzip(req)) {
res.setHeader("Content-Encoding", "gzip");
if (!gzipCached) {
var buf = new Buffer(output, "utf-8");
zlib.gzip(buf, function (_, result) {
gzipCached = result;
res.end(result);
});
} else {
res.end(gzipCached);
}
} else {
res.end(output);
}
};
}
module.exports.middleware = init;
module.exports.plugin = init;
module.exports.minified = function () {
return fs.readFileSync(minifiedScript, 'utf8');
};
module.exports.unminified = function () {
return fs.readFileSync(unminifiedScript, 'utf8');
}