Skip to content

Commit bbc5fca

Browse files
committed
fix(index): change index filenames to sha1s of keys
Fixes: #41 BREAKING CHANGE: The index format has changed and previous caches are no longer compatible. Existing caches will need to be regenerated.
1 parent ea00ba6 commit bbc5fca

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/entry-index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const asyncMap = require('slide/lib/async-map')
44
const contentPath = require('./content/path')
5+
const crypto = require('crypto')
56
const fixOwner = require('./util/fix-owner')
67
const fs = require('graceful-fs')
78
const lockfile = require('lockfile')
@@ -170,8 +171,17 @@ function indexPath (cache, key) {
170171

171172
module.exports._hashKey = hashKey
172173
function hashKey (key) {
173-
// relatively readable key. Conflicts handled by buckets.
174-
return key.replace(/[^a-z0-9_-]+/ig, '_').toLowerCase().slice(0, 120)
174+
// sha1 conflicts can be generated, but it doesn't matter in this case,
175+
// since we intend for there to be regular conflicts anyway. You can have
176+
// the entire cache in a single bucket and all that'll do is just make a big
177+
// file with a lot of contention, if you can even pull it off in the `key`
178+
// string. So whatever. `sha1` is faster and it doesn't trigger the warnings
179+
// `md5` tends to (yet?...).
180+
return crypto
181+
.createHash('sha1')
182+
.update(key.toLowerCase()) // lump case-variant keys into same bucket.
183+
.digest('hex')
184+
.slice(0, 7)
175185
}
176186

177187
function formatEntry (cache, entry) {

test/index.insert.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ test('key case-sensitivity', function (t) {
141141
test('hash conflict in same bucket', function (t) {
142142
// NOTE - this test will break if `index._hashKey` changes its algorithm.
143143
// Adapt to it accordingly.
144-
const NEWKEY = KEY + '!'
145-
const CONFLICTING = KEY + '!!!'
144+
const NEWKEY = KEY.toUpperCase()
145+
const CONFLICTING = KEY.toLowerCase()
146146
return index.insert(
147147
CACHE, NEWKEY, DIGEST
148148
).then(() => (
@@ -157,7 +157,7 @@ test('hash conflict in same bucket', function (t) {
157157
key: NEWKEY,
158158
digest: DIGEST
159159
}, {
160-
key: KEY + '!!!',
160+
key: CONFLICTING,
161161
digest: DIGEST
162162
}], 'multiple entries for conflicting keys in the same bucket')
163163
})

0 commit comments

Comments
 (0)