-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathread.js
317 lines (289 loc) · 7.96 KB
/
read.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
'use strict'
const fs = require('fs')
const path = require('path')
const ssri = require('ssri')
const t = require('tap')
const CacheContent = require('../fixtures/cache-content')
const read = require('../../lib/content/read')
// defines reusable errors
const genericError = new Error('ERR')
genericError.code = 'ERR'
const permissionError = new Error('EPERM')
permissionError.code = 'EPERM'
// helpers
const getRead = (t, opts) => t.mock('../../lib/content/read', opts)
const getReadStatFailure = (t, err) => getRead(t, {
fs: {
...fs,
statSync: () => {
throw err
},
},
'fs/promises': {
...fs.promises,
stat: async () => {
throw err
},
},
})
t.test('read: returns a Promise with cache content data', async t => {
const CONTENT = Buffer.from('foobarbaz')
const INTEGRITY = ssri.fromData(CONTENT)
const CACHE = t.testdir(
CacheContent({
[INTEGRITY]: CONTENT,
})
)
const data = await read(CACHE, INTEGRITY)
t.same(data, CONTENT, 'cache contents read correctly')
})
t.test('read.stream: returns a stream with cache content data', async t => {
const CONTENT = Buffer.from('foobarbaz')
const INTEGRITY = ssri.fromData(CONTENT)
const CACHE = t.testdir(
CacheContent({
[INTEGRITY]: CONTENT,
})
)
const stream = read.stream(CACHE, INTEGRITY)
const [fromStream, fromBulk] = await Promise.all([
stream.concat(),
read(CACHE, INTEGRITY, { size: CONTENT.length }),
])
t.same(fromStream, CONTENT, 'stream data checks out')
t.same(fromBulk, CONTENT, 'promise data checks out')
})
t.test('read: allows hashAlgorithm configuration', async t => {
const CONTENT = Buffer.from('foobarbaz')
const HASH = 'sha384'
const INTEGRITY = ssri.fromData(CONTENT, { algorithms: [HASH] })
const CACHE = t.testdir(
CacheContent({
[INTEGRITY]: CONTENT,
})
)
const stream = read.stream(CACHE, INTEGRITY)
const [fromStream, fromBulk] = await Promise.all([
stream.concat(),
read(CACHE, INTEGRITY),
])
t.same(fromStream, CONTENT, 'stream used algorithm')
t.same(fromBulk, CONTENT, 'promise used algorithm')
})
t.test('read: errors if content missing', async t => {
const CACHE = t.testdir({})
const stream = read.stream(CACHE, 'sha512-whatnot')
stream.on('data', function (data) {
throw new Error('unexpected data: ' + JSON.stringify(data))
})
stream.on('end', function () {
throw new Error('end was emitted even though stream errored')
})
await t.rejects(
stream.promise(),
{ code: 'ENOENT' },
'stream got the right error'
)
await t.rejects(
read(CACHE, 'sha512-whatnot'),
{ code: 'ENOENT' },
'bulk got the right error'
)
})
t.test('read: errors if content fails checksum', async t => {
const CONTENT = Buffer.from('foobarbaz')
const INTEGRITY = ssri.fromData(CONTENT)
const CACHE = t.testdir(
CacheContent({
[INTEGRITY]: CONTENT.slice(3), // invalid contents!
})
)
const stream = read.readStream(CACHE, INTEGRITY)
stream.on('end', function () {
throw new Error('end was emitted even though stream errored')
})
await t.rejects(
stream.promise(),
{ code: 'EINTEGRITY' },
'stream got the right error'
)
await t.rejects(
read(CACHE, INTEGRITY),
{ code: 'EINTEGRITY' },
'bulk got the right error'
)
})
t.test('read: errors if content size does not match size option', async t => {
const CONTENT = Buffer.from('foobarbaz')
const INTEGRITY = ssri.fromData(CONTENT)
const CACHE = t.testdir(
CacheContent({
[INTEGRITY]: CONTENT.slice(3), // invalid contents!
})
)
const stream = read.readStream(CACHE, INTEGRITY, { size: CONTENT.length })
stream.on('end', function () {
throw new Error('end was called even though stream errored')
})
await t.rejects(
stream.promise(),
{ code: 'EBADSIZE' },
'stream got the right error'
)
await t.rejects(
read(CACHE, INTEGRITY, { size: CONTENT.length }),
{ code: 'EBADSIZE' },
'bulk got the right error'
)
})
t.test('read: error while parsing provided integrity data', function (t) {
const CACHE = t.testdir()
const INTEGRITY = 'sha1-deadbeef'
const mockedRead = getRead(t, {
ssri: {
parse () {
throw genericError
},
},
})
t.plan(1)
return t.rejects(
mockedRead(CACHE, INTEGRITY),
genericError,
'should reject promise upon catching internal errors'
)
})
t.test('read: unknown error parsing nested integrity data', function (t) {
const CACHE = t.testdir()
const INTEGRITY = 'sha1-deadbeef sha1-13371337'
// patches method in order to force a last error scenario
const mockedRead = getRead(t, {
ssri: {
parse (sri) {
if (sri !== INTEGRITY) {
throw genericError
}
return ssri.parse(sri)
},
},
})
t.plan(1)
return t.rejects(
mockedRead(CACHE, INTEGRITY),
genericError,
'should throw unknown errors'
)
})
t.test('read: returns only first result if other hashes fails', function (t) {
// sets up a cache that has multiple entries under the
// same algorithm but then only one has real contents in the fs
const CONTENT = {
foo: Buffer.from('foo'),
bar: Buffer.from('bar'),
}
const INTEGRITY = ssri.fromData(CONTENT.foo).concat(
ssri.fromData(CONTENT.bar)
)
const CACHE = t.testdir(
CacheContent({
[INTEGRITY.sha512[1]]: CONTENT.bar,
})
)
t.plan(1)
return t.resolveMatch(
read(CACHE, INTEGRITY),
CONTENT.bar,
'should return only the first valid result'
)
})
t.test('read: opening large files', function (t) {
const CACHE = t.testdir()
const mockedRead = getRead(t, {
'fs/promises': {
...fs.promises,
stat: async () => {
return { size: Number.MAX_SAFE_INTEGER }
},
},
'fs-minipass': {
ReadStream: class {
constructor (path, opts) {
t.match(
opts,
{
readSize: 64 * 1024 * 1024,
size: Number.MAX_SAFE_INTEGER,
},
'should use fs-minipass interface'
)
}
},
},
'minipass-pipeline': Array,
})
t.plan(1)
mockedRead(CACHE, 'sha1-deadbeef')
})
t.test('hasContent: tests content existence', async t => {
const CACHE = t.testdir(
CacheContent({
'sha1-deadbeef': '',
})
)
const content = await read.hasContent(CACHE, 'sha1-deadbeef')
t.ok(content.sri, 'returned sri for this content')
t.equal(content.size, 0, 'returned the right size for this content')
t.ok(content.stat.isFile(), 'returned actual stat object')
await t.resolveMatch(
read.hasContent(CACHE, 'sha1-not-there'),
false,
'returned false for missing content'
)
await t.resolveMatch(
read.hasContent(CACHE, 'sha1-not-here sha1-also-not-here'),
false,
'multi-content hash failures work ok'
)
})
t.test('hasContent: permission error', (t) => {
const CACHE = t.testdir()
// setup a syntetic permission error
const mockedRead = getReadStatFailure(t, permissionError)
t.plan(1)
t.rejects(
mockedRead.hasContent(CACHE, 'sha1-deadbeef sha1-13371337'),
permissionError,
'should reject on permission errors'
)
})
t.test('hasContent: generic error', (t) => {
const CACHE = t.testdir()
const mockedRead = getReadStatFailure(t, genericError)
t.plan(1)
t.resolves(
mockedRead.hasContent(CACHE, 'sha1-deadbeef sha1-13371337'),
'should not reject on generic errors'
)
})
t.test('hasContent: no integrity provided', (t) => {
const CACHE = t.testdir()
t.resolveMatch(
read.hasContent(CACHE, ''),
false,
'should resolve with a value of false'
)
t.end()
})
t.test('copy: copies content to a destination path', async t => {
const CONTENT = Buffer.from('foobarbaz')
const INTEGRITY = ssri.fromData(CONTENT)
const CACHE = t.testdir(
CacheContent({
[INTEGRITY]: CONTENT,
})
)
const DEST = path.join(CACHE, 'foobar-file')
await read.copy(CACHE, INTEGRITY, DEST)
const data = await fs.promises.readFile(DEST)
t.same(data, CONTENT, 'file successfully copied')
})