-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetagres.js
36 lines (30 loc) · 902 Bytes
/
etagres.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
var crypto = require('crypto');
var stream = require('stream');
var util = require('util');
util.inherits(ETagRes, stream.Writable);
function ETagRes (req, res) {
stream.Writable.call(this);
this._h = crypto.createHash('sha1');
this._buffer = new Buffer(0);
// Listen on finish since writables have no _flush
// See https://github.com/joyent/node/issues/5315
this.on('finish', function () {
var etag = '"' + this._h.digest('base64') + '"';
if (req.headers['if-none-match'] === etag) {
res.statusCode = 304;
res.end();
} else {
res.statusCode = 200;
res.setHeader('ETag', etag);
res.end(this._buffer);
}
});
}
ETagRes.prototype._write = function(chunk, enc, cb) {
this._h.update(chunk);
this._buffer = Buffer.concat([this._buffer, chunk]);
cb();
};
module.exports = function etagres (req, res) {
return new ETagRes(req, res);
};