forked from mtharrison/hapi-etags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (94 loc) · 3.28 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
var Package = require('./package');
var Crypto = require('crypto');
var Hoek = require('hoek');
var Boom = require('boom');
var Joi = require('joi');
var internals = {};
internals.getOptions = function (options) {
var defaults = {
encoding: 'base64',
algo: 'sha1',
varieties: ['plain', 'buffer'],
etagOptions: {}
};
options = Hoek.applyToDefaults(defaults, options);
var schema = {
encoding: Joi.string().required().valid(['hex', 'base64']),
algo: Joi.string().required().valid(Crypto.getHashes()),
varieties: Joi.array().required().items(Joi.string().valid(['plain', 'buffer', 'view', 'stream'])),
etagOptions: Joi.object().required()
};
Joi.assert(options, schema);
return options;
};
internals.marshal = function (request, next) {
var options = request.server.plugins[Package.name].options;
var response = request.response;
var source = response.source;
switch (response.variety) {
case 'plain':
if (typeof source === 'object') {
return next(null, JSON.stringify(source));
}
// Should we allow numbers?
if (typeof source !== 'string') {
return next(Boom.badImplementation('Plain variety responses must be objects or strings'));
}
return next(null, source);
break;
case 'buffer':
return next(null, source);
break;
case 'view':
return request.render(source.template, source.context, source.options, function (err, rendered) {
if (err) {
throw err;
}
return next(null, rendered);
});
break;
case 'stream':
// We have to read all of the data off the stream to calculate the ETag
var pass = new (require('stream').PassThrough);
var data = new Buffer('');
source.on('data', function (d) {
pass.push(d);
data = Buffer.concat([data, d]);
});
source.on('end', function () {
pass.push(null);
request.response.source = pass;
next(null, data);
});
break;
default:
next(Boom.badImplementation('Unknown variety'));
break;
}
};
internals.onPreResponse = function (request, reply) {
var options = request.server.plugins[Package.name].options;
var response = request.response;
if (options.varieties.indexOf(response.variety) === -1) {
return reply.continue();
}
internals.marshal(request, function (err, contents) {
if (err) {
throw err;
}
var hash = Crypto.createHash(options.algo);
hash.update(contents);
response.etag(hash.digest(options.encoding), options.etagOptions);
reply.continue();
});
};
exports.register = function (server, options, next) {
server.plugins[Package.name] = server.plugins[Package.name] || {};
server.plugins[Package.name].options = internals.getOptions(options);
server.ext('onPreResponse', internals.onPreResponse);
next();
};
exports.register.attributes = {
name: Package.name,
version: Package.version
};