blob: bb05eb7e0fb97653106f31b798715a714e628f34 (
plain)
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
|
/*!
* etag
* Copyright(c) 2014 Douglas Christopher Wilson
* MIT Licensed
*/
/**
* Module exports.
*/
module.exports = etag
/**
* Module dependencies.
*/
var crc = require('crc').crc32
var crypto = require('crypto')
var Stats = require('fs').Stats
/**
* Module variables.
*/
var crc32threshold = 1000 // 1KB
var NULL = new Buffer([0])
var toString = Object.prototype.toString
/**
* Create a simple ETag.
*
* @param {string|Buffer|Stats} entity
* @param {object} [options]
* @param {boolean} [options.weak]
* @return {String}
* @api public
*/
function etag(entity, options) {
if (entity == null) {
throw new TypeError('argument entity is required')
}
var isStats = isstats(entity)
var weak = options && typeof options.weak === 'boolean'
? options.weak
: isStats
// support fs.Stats object
if (isStats) {
return stattag(entity, weak)
}
if (typeof entity !== 'string' && !Buffer.isBuffer(entity)) {
throw new TypeError('argument entity must be string, Buffer, or fs.Stats')
}
var hash = weak
? weakhash(entity)
: stronghash(entity)
return weak
? 'W/"' + hash + '"'
: '"' + hash + '"'
}
/**
* Determine if object is a Stats object.
*
* @param {object} obj
* @return {boolean}
* @api private
*/
function isstats(obj) {
// not even an object
if (obj === null || typeof obj !== 'object') {
return false
}
// genuine fs.Stats
if (obj instanceof Stats) {
return true
}
// quack quack
return 'atime' in obj && toString.call(obj.atime) === '[object Date]'
&& 'ctime' in obj && toString.call(obj.ctime) === '[object Date]'
&& 'mtime' in obj && toString.call(obj.mtime) === '[object Date]'
&& 'ino' in obj && typeof obj.ino === 'number'
&& 'size' in obj && typeof obj.size === 'number'
}
/**
* Generate a tag for a stat.
*
* @param {Buffer} entity
* @return {String}
* @api private
*/
function stattag(stat, weak) {
var mtime = stat.mtime.toISOString()
var size = stat.size.toString(16)
if (weak) {
return 'W/"' + size + '-' + crc(mtime) + '"'
}
var hash = crypto
.createHash('md5')
.update('file', 'utf8')
.update(NULL)
.update(size, 'utf8')
.update(NULL)
.update(mtime, 'utf8')
.digest('base64')
return '"' + hash + '"'
}
/**
* Generate a strong hash.
*
* @param {Buffer} entity
* @return {String}
* @api private
*/
function stronghash(entity) {
if (entity.length === 0) {
// fast-path empty
return '1B2M2Y8AsgTpgAmY7PhCfg=='
}
return crypto
.createHash('md5')
.update(entity, 'utf8')
.digest('base64')
}
/**
* Generate a weak hash.
*
* @param {Buffer} entity
* @return {String}
* @api private
*/
function weakhash(entity) {
if (entity.length === 0) {
// fast-path empty
return '0-0'
}
var len = typeof entity === 'string'
? Buffer.byteLength(entity, 'utf8')
: entity.length
if (len <= crc32threshold) {
// crc32 plus length when it's fast
// crc(str) only accepts utf-8 encoding
return len.toString(16) + '-' + crc(entity).toString(16)
}
// use md4 for long strings
return crypto
.createHash('md4')
.update(entity, 'utf8')
.digest('base64')
}
|