-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathentry-index.find.js
211 lines (199 loc) · 5.02 KB
/
entry-index.find.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
'use strict'
const CacheIndex = require('./fixtures/cache-index')
const path = require('path')
const t = require('tap')
const SIZE = 999
const contentPath = require('../lib/content/path')
const index = require('../lib/entry-index')
t.test('index.find cache hit', async t => {
const entry = {
key: 'whatever',
integrity: 'whatnot-deadbeef',
time: 12345,
metadata: 'omgsometa',
size: 5,
}
const CACHE = t.testdir(
CacheIndex({
whatever: entry,
})
)
const info = await index.find(CACHE, entry.key)
t.ok(info, 'cache hit')
t.equal(
info.path,
contentPath(CACHE, entry.integrity),
'path added to info'
)
delete info.path
t.same(info, entry, 'rest of info matches entry on disk')
})
t.test('index.find cache miss', async t => {
const CACHE = t.testdir(
CacheIndex({
foo: { key: 'foo' },
'w/e': { key: 'w/e' },
})
)
await t.resolveMatch(
index.find(CACHE, 'whatever'),
null,
'cache miss when specific key not present'
)
})
t.test('index.find no cache', async t => {
await t.resolveMatch(
index.find(path.resolve('adirectorythatdoesnotexit'), 'whatever'),
null,
'if there is no cache dir, behaves like a cache miss'
)
})
t.test('index.find key case-sensitivity', async t => {
const CACHE = t.testdir(
CacheIndex({
jsonstream: {
key: 'jsonstream',
integrity: 'sha1-lowercase',
time: 54321,
size: SIZE,
},
JSONStream: {
key: 'JSONStream',
integrity: 'sha1-capitalised',
time: 12345,
size: SIZE,
},
})
)
await t.resolveMatch(
index.find(CACHE, 'JSONStream'),
{ key: 'JSONStream' },
'fetched the correct entry'
)
await t.resolveMatch(
index.find(CACHE, 'jsonstream'),
{ key: 'jsonstream' },
'fetched the correct entry'
)
await t.resolveMatch(
index.find(CACHE, 'jsonStream'),
null,
'no entry for jsonStream'
)
})
t.test('index.find path-breaking characters', async t => {
const entry = {
key: ';;!registry\nhttps://registry.npmjs.org/back \\ slash@Cool™?',
integrity: 'sha1-deadbeef',
time: 12345,
metadata: 'omgsometa',
size: 9,
}
const CACHE = t.testdir(
CacheIndex({
[entry.key]: entry,
})
)
const info = await index.find(CACHE, entry.key)
t.ok(info, 'cache hit')
delete info.path
t.same(
info,
entry,
'info remains intact even with fs-unfriendly chars'
)
})
t.test('index.find extremely long keys', async t => {
let key = ''
for (let i = 0; i < 10000; i++) {
key += i
}
const entry = {
key: key,
integrity: 'sha1-deadbeef',
time: 12345,
metadata: 'woo',
size: 10,
}
const CACHE = t.testdir(
CacheIndex({
[entry.key]: entry,
})
)
const info = await index.find(CACHE, entry.key)
t.ok(info, 'cache hit')
delete info.path
t.same(info, entry, 'info remains intact even with absurdly long key')
})
t.test('index.find multiple index entries for key', async t => {
const key = 'whatever'
const CACHE = t.testdir(
CacheIndex({
whatever: [
{ key: key, integrity: 'sha1-deadbeef', time: 54321 },
{ key: key, integrity: 'sha1-bada55', time: 12345 },
],
})
)
const info = await index.find(CACHE, key)
t.ok(info, 'cache hit')
t.equal(info.integrity, 'sha1-bada55', 'most recent entry wins')
})
t.test('index.find garbled data in index file', async t => {
// Even though `index.insert()` is safe from direct
// race conditions, it's still possible for individual
// entries to become corrupted, or to be partially written,
// since `index.find` does not acquire a write-preventing lock.
//
// Because entries are newline-prepended and only one
// can be written at a time, the main possible corruption
// source is if an append fails mid-write (for example, due
// to the process crashing). In this case, the corrupt entry
// will simply be skipped.
const key = 'whatever'
const stringified = JSON.stringify({
key: key,
integrity: 'sha1-deadbeef',
time: 54321,
})
const CACHE = t.testdir(
CacheIndex({
whatever:
'\n' +
`${index.hashEntry(stringified)}\t${stringified}` +
'\n{"key": "' +
key +
'"\noway',
})
)
const info = await index.find(CACHE, key)
t.ok(info, 'cache hit in spite of crash-induced fail')
t.equal(info.integrity, 'sha1-deadbeef', ' recent entry wins')
})
t.test('index.find hash conflict in same bucket', async t => {
// This... is very unlikely to happen. But hey.
const entry = {
key: 'whatever',
integrity: 'sha1-deadbeef',
time: 12345,
metadata: 'yay',
size: 8,
}
const CACHE = t.testdir(
CacheIndex({
whatever: [
{ key: 'ohnoes', integrity: 'sha1-welp!' },
entry,
{ key: 'nope', integrity: 'sha1-bada55' },
],
})
)
const info = await index.find(CACHE, entry.key)
t.ok(info, 'cache hit')
delete info.path
t.same(
info,
entry,
'got the right one even though different keys exist in index'
)
})