-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.js
349 lines (305 loc) · 9.23 KB
/
file.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
'use strict';
var debug = require('diagnostics')('fever:file')
, EventEmitter = require('eventemitter3')
, hash = require('crypto').createHash
, destroy = require('demolish')
, Supply = require('supply')
, sm = require('source-map')
, async = require('async')
, path = require('path')
, zlib = require('zlib')
, mime = require('mime');
/**
* Representation of a single file.
*
* @TODO: Add the path to the contents.
*
* @constructor
* @param {Fever} fever The fever that creates and manages the files.
* @param {String} file Location of the file.
* @param {Object} options File configuration.
* @api public
*/
function File(fever, file, options) {
if (!this) return new File(path, options);
options = options || {};
Supply.call(this);
this.type = 'text/javascript'; // The content type of the file.
this.directory = fever.dir; // Directory in which our compiled assets live.
this.requested = 0; // The amount of x this file has been requested.
this.contents = []; // Various of file that we should read.
this.fever = fever; // Reference to the fever instance.
this.alias = ''; // Alias of the file, also known as fingerprint.
this.smg = null; // Placeholder for the source file generator.
this.size = {
deflate: 0, // Total deflate size.
gzip: 0, // Total gzip size.
raw: 0 // Total binary size.
};
fever.emit('add', this);
if (file) this.push(file);
}
//
// File inherits from the EventEmitter so people can hook in to these changes.
//
File.prototype.__proto__ = Supply.prototype;
File.prototype.emits = require('emits');
Object.keys(EventEmitter.prototype).forEach(function each(key) {
File.prototype[key] = EventEmitter.prototype[key];
});
//
// Generate API methods which allows the adding and removing of file's.
//
['push', 'shift', 'pop', 'unshift'].forEach(function generate(method) {
File.prototype[method] = function compiled(path, fn) {
this.contents[method](path);
this.emit(method);
return this.modified(fn);
};
});
/**
* Generate a fingerprint based on all the contents.
*
* @param {Array} contents Array of read file contents.
* @returns {String} The newly generated alias/fingerprint.
* @api private
*/
File.prototype.fingerprinter = function fingerprinter(contents) {
var md5 = hash('md5');
//
// As crypto is actually a stream we can do multiple update calls with each of
// the files that we've stored in our contents array.
//
(contents || this.contents).forEach(function each(content) {
md5.update(content.toString());
});
return this.alias = md5.digest('hex');
};
/**
* Concatenate multiple files together.
*
* @returns {File}
* @api public
*/
File.prototype.concat = function concat() {
var file = new File(this.fever)
, files = Array.prototype.slice.call(arguments, 0);
//
// Add all the file contents to our new file instances.
//
Array.prototype.push.apply(file.contents, this.contents);
Array.prototype.push.apply(file.contents, files);
//
// Nuke all old file instances as they are now concatenated in to a new
// instance.
//
files.forEach(function each(old) {
old.destroy();
});
this.destroy();
return file;
};
/**
* Read out the compiled contents and callback with the resulting buffer.
*
* @param {Function} fn Error first callback.
* @returns {File}
* @api public
*/
File.prototype.buffer = function buffer(fn) {
this.fever.fs.readFile(path.join(this.directory, this.alias), fn);
return this;
};
/**
* Check if the our internal contents array contains a given file.
*
* @param {String} file Full path of the file we should search for.
* @returns {Boolean}
* @api public
*/
File.prototype.contains = function contains(file) {
return this.contents.some(function some(name) {
return name === file;
});
};
/**
* Something in this file has been modified, we need to re-calculate all the
* things:
*
* - Source map.
* - New alias based on the content.
*
* @returns {File}
* @api public
*/
File.prototype.modified = function modified(fn) {
var alias = this.alias
, fs = this.fever.fs
, selfie = this
, buffers = []
, local;
async.map(this.contents, function map(file, next) {
fs.readFile(file, function read(err, content) {
buffers.push(content);
next(err, {
type: mime.lookup(file),
content: content,
path: file
});
});
}, function mapped(err, contents) {
if (err) return fn(err);
//
// Generate a new alias, as it only works file actual file content we need to
// return an array which only holds these contents. Once we have the new
// alias we can generate a new source map.
//
selfie.smg = new sm.SourceMapGenerator({
file: selfie.fingerprinter(buffers)
});
async.series([
function fever(next) {
async.eachSeries(contents, function iterator(content, next) {
selfie.fever.each(content, selfie, next);
}, next);
},
function local(next) {
async.eachSeries(contents, function iterator(content, next) {
selfie.each(content, selfie, next);
}, next);
}
], function transformed(err) {
if (err) return fn(err);
//
// Generate the total size of the file so we can use it for the
// Content-Length header.
//
selfie.size.deflate = 0;
selfie.size.gzip = 0;
selfie.size.raw = 0;
contents.forEach(function each(file) {
selfie.size.raw += file.content.length;
// @TODO check if we need to add an exiting file map.
// @TODO check for a possible existing .map on the contents object.
selfie.smg.setSourceContent(file.path, file.content.toString());
});
//
// Assume that the content types in this file are all the same so we can
// get the first one and roll with that.
//
selfie.type = contents[0].type;
//
// Concat the buffers so it's easier to pass around and write.
//
buffers = Buffer.concat(buffers);
async.parallel([
function gzip(next) {
zlib.gzip(buffers, function compressed(err, buffer) {
if (err) return next(err);
selfie.size.gzip = buffer.length;
fs.writeFile(
path.join(selfie.directory, selfie.alias +'.gzip'),
buffer,
next
);
});
},
function normal(next) {
fs.writeFile(
path.join(selfie.directory, selfie.alias),
buffers,
next
);
},
function deflate(next) {
zlib.deflate(buffers, function compressed(err, buffer) {
if (err) return next(err);
selfie.size.deflate = buffer.length;
fs.writeFile(
path.join(selfie.directory, selfie.alias +'.deflate'),
buffer,
next
);
});
},
], function (err) {
fn(err);
//
// Attempt to remove the old stored buffer, it doesn't really matter if we
// cannot remove it.
//
fs.unlink(path.join(selfie.directory, alias), function rm(err) {
if (err) debug('Failed to destroy old %s due to error', alias, err);
});
});
});
debug('Updated %s to %s as content is changed', alias || '(empty)', selfie.alias);
});
return this;
};
/**
* Write the response headers for this cached item.
*
* @param {Request} req Incoming HTTP request.
* @param {Response} res Outgoing HTTP response.
* @returns {File}
* @api public
*/
File.prototype.setHeader = function setHeader(req, res) {
var age = 84029840280;
res.statusCode = 200;
res.setHeader('Expires', new Date(Date.now() + age).toUTCString());
res.setHeader('Cache-Control', 'max-age='+ age +', public');
res.setHeader('Content-Type', this.type);
// @TODO make this gzip selection aware.
res.setHeader('Content-Length', this.size.raw);
this.requested++;
return this;
};
/**
* Forward the file contents to different streams, services and API's.
*
* Options:
*
* - encoding: The accepted content encoding
*
* @param {Stream} where Where do we need to write to.
* @param {Object} options Write options.
* @returns {File}
* @api public
*/
File.prototype.forward = function forward(where, options) {
options = options || {};
var stream = this.stream(options.encoding || []);
return this;
};
/**
* Get the file stream.
*
* @param {Array} encoding Array with possible allowed encodings.
* @returns {ReadableStream}
* @api public
*/
File.prototype.stream = function stream(encoding) {
var alias = this.alias + (
~encoding.indexOf('gzip') && this.size.gzip ? '.gzip' : (
~encoding.indexOf('deflate') && this.size.deflate ? '.deflate' : '')
);
return this.fever.fs.createReadstream(path.join(this.directory, alias));
};
/**
* Destroy the file instance and un-register it in the fever.
*
* @TODO also clean up locally stored file.
* @returns {File}
* @api public
*/
File.prototype.destroy = destroy('requested, contents, fever, alias, smg, size', {
before: function before() {
this.fever.emit('remove', this);
}
});
//
// Expose the module.
//
module.exports = File;