Skip to content

Commit fe638b6

Browse files
committed
feat(read): add sync support to other internal read.js fns
1 parent 4b28ae3 commit fe638b6

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

Diff for: lib/content/read.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,29 @@ function readStream (cache, integrity, opts) {
7373
let copyFileAsync
7474
if (fs.copyFile) {
7575
module.exports.copy = copy
76+
module.exports.copy.sync = copySync
7677
copyFileAsync = BB.promisify(fs.copyFile)
7778
}
79+
7880
function copy (cache, integrity, dest, opts) {
7981
opts = ReadOpts(opts)
8082
return withContentSri(cache, integrity, (cpath, sri) => {
8183
return copyFileAsync(cpath, dest)
8284
})
8385
}
8486

87+
function copySync (cache, integrity, dest, opts) {
88+
opts = ReadOpts(opts)
89+
return withContentSriSync(cache, integrity, (cpath, sri) => {
90+
return fs.copyFileSync(cpath, dest)
91+
})
92+
}
93+
8594
module.exports.hasContent = hasContent
8695
function hasContent (cache, integrity) {
8796
if (!integrity) { return BB.resolve(false) }
8897
return withContentSri(cache, integrity, (cpath, sri) => {
89-
return lstatAsync(cpath).then(stat => ({size: stat.size, sri}))
98+
return lstatAsync(cpath).then(stat => ({size: stat.size, sri, stat}))
9099
}).catch(err => {
91100
if (err.code === 'ENOENT') { return false }
92101
if (err.code === 'EPERM') {
@@ -99,6 +108,26 @@ function hasContent (cache, integrity) {
99108
})
100109
}
101110

111+
module.exports.hasContent.sync = hasContentSync
112+
function hasContentSync (cache, integrity) {
113+
if (!integrity) { return false }
114+
return withContentSriSync(cache, integrity, (cpath, sri) => {
115+
try {
116+
const stat = fs.lstatSync(cpath)
117+
return {size: stat.size, sri, stat}
118+
} catch (err) {
119+
if (err.code === 'ENOENT') { return false }
120+
if (err.code === 'EPERM') {
121+
if (process.platform !== 'win32') {
122+
throw err
123+
} else {
124+
return false
125+
}
126+
}
127+
}
128+
})
129+
}
130+
102131
function withContentSri (cache, integrity, fn) {
103132
return BB.try(() => {
104133
const sri = ssri.parse(integrity)

Diff for: test/content.read.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ test('read: errors if content size does not match size option', function (t) {
146146
)
147147
})
148148

149-
test('hasContent: returns { sri, size } when a cache file exists', function (t) {
149+
test('hasContent: tests content existence', t => {
150150
const fixture = new Tacks(CacheContent({
151151
'sha1-deadbeef': ''
152152
}))
@@ -156,6 +156,7 @@ test('hasContent: returns { sri, size } when a cache file exists', function (t)
156156
.then(content => {
157157
t.ok(content.sri, 'returned sri for this content')
158158
t.equal(content.size, 0, 'returned the right size for this content')
159+
t.ok(content.stat.isFile(), 'returned actual stat object')
159160
}),
160161
read.hasContent(CACHE, 'sha1-not-there')
161162
.then(content => {
@@ -168,6 +169,28 @@ test('hasContent: returns { sri, size } when a cache file exists', function (t)
168169
)
169170
})
170171

172+
test('hasContent.sync: checks content existence synchronously', t => {
173+
const fixture = new Tacks(CacheContent({
174+
'sha1-deadbeef': ''
175+
}))
176+
fixture.create(CACHE)
177+
const content = read.hasContent.sync(CACHE, 'sha1-deadbeef')
178+
t.ok(content.sri, 'returned sri for this content')
179+
t.equal(content.size, 0, 'returned the right size for this content')
180+
t.ok(content.stat.isFile(), 'returned actual stat object')
181+
t.equal(
182+
read.hasContent.sync(CACHE, 'sha1-not-there'),
183+
false,
184+
'returned false for missing content'
185+
)
186+
t.equal(
187+
read.hasContent.sync(CACHE, 'sha1-not-here sha1-also-not-here'),
188+
false,
189+
'multi-content hash failures work ok'
190+
)
191+
t.done()
192+
})
193+
171194
test('copy: copies content to a destination path', {
172195
skip: !fs.copyFile && 'Not supported on node versions without fs.copyFile'
173196
}, t => {
@@ -184,3 +207,22 @@ test('copy: copies content to a destination path', {
184207
t.deepEqual(data, CONTENT, 'file successfully copied')
185208
})
186209
})
210+
211+
test('copy.sync: copies content to a destination path synchronously', {
212+
skip: !fs.copyFile && 'Not supported on node versions without fs.copyFile'
213+
}, t => {
214+
const CONTENT = Buffer.from('foobarbaz')
215+
const INTEGRITY = ssri.fromData(CONTENT)
216+
const DEST = path.join(CACHE, 'foobar-file')
217+
const fixture = new Tacks(CacheContent({
218+
[INTEGRITY]: CONTENT
219+
}))
220+
fixture.create(CACHE)
221+
read.copy.sync(CACHE, INTEGRITY, DEST)
222+
t.deepEqual(
223+
fs.readFileSync(DEST),
224+
CONTENT,
225+
'file successfully copied'
226+
)
227+
t.done()
228+
})

0 commit comments

Comments
 (0)