diff --git a/lib/util/fs/index.spec.ts b/lib/util/fs/index.spec.ts index 7a7fcb8f0b13c8b990169f2ea7202e6612b13b73..4a4f114602728dfb84fe580dba7c0af9508bbb44 100644 --- a/lib/util/fs/index.spec.ts +++ b/lib/util/fs/index.spec.ts @@ -8,6 +8,7 @@ import { cachePathExists, cachePathIsFile, chmodLocalFile, + createCacheReadStream, createCacheWriteStream, deleteLocalFile, ensureCacheDir, @@ -32,6 +33,7 @@ import { readSystemFile, renameLocalFile, rmCache, + statCacheFile, statLocalFile, writeLocalFile, writeSystemFile, @@ -332,6 +334,29 @@ describe('util/fs/index', () => { }); }); + describe('createCacheReadStream', () => { + it('creates read stream', async () => { + const path = `${cacheDir}/file.txt`; + const fileContent = 'foo'; + await fs.outputFile(path, fileContent); + + const stream = createCacheReadStream('file.txt'); + expect(stream).toBeInstanceOf(fs.ReadStream); + + let data = ''; + stream.on('data', (chunk) => { + data += chunk.toString(); + }); + + await new Promise((resolve, reject) => { + stream.on('end', resolve); + stream.on('error', reject); + }); + + expect(data).toBe(fileContent); + }); + }); + describe('localPathIsFile', () => { it('returns true for file', async () => { const path = `${localDir}/file.txt`; @@ -431,6 +456,17 @@ describe('util/fs/index', () => { }); }); + describe('statCacheFile', () => { + it('returns stat object', async () => { + expect(await statCacheFile('foo')).toBeNull(); + + await fs.outputFile(`${cacheDir}/foo`, 'foobar'); + const stat = await statCacheFile('foo'); + expect(stat).toBeTruthy(); + expect(stat!.isFile()).toBeTrue(); + }); + }); + describe('listCacheDir', () => { it('lists directory', async () => { await fs.outputFile(`${cacheDir}/foo/bar.txt`, 'foobar'); diff --git a/lib/util/fs/index.ts b/lib/util/fs/index.ts index 64512d6257a546c8c7ade0a55ddd8120f25c9523..5ac59f80278f08ee284b4016282af66d6f0ed997 100644 --- a/lib/util/fs/index.ts +++ b/lib/util/fs/index.ts @@ -176,6 +176,11 @@ export function createCacheWriteStream(path: string): fs.WriteStream { return fs.createWriteStream(fullPath); } +export function createCacheReadStream(path: string): fs.ReadStream { + const fullPath = ensureCachePath(path); + return fs.createReadStream(fullPath); +} + export async function localPathIsFile(pathName: string): Promise<boolean> { const path = ensureLocalPath(pathName); try { @@ -249,6 +254,17 @@ export async function statLocalFile( } } +export async function statCacheFile( + pathName: string, +): Promise<fs.Stats | null> { + const path = ensureCachePath(pathName); + try { + return await fs.stat(path); + } catch (_) { + return null; + } +} + export function listCacheDir( path: string, options: { recursive: boolean } = { recursive: false },