From 5bdb8210ef455c0d5552e67b8ce86aa89cb779c8 Mon Sep 17 00:00:00 2001 From: oxdev03 <140103378+oxdev03@users.noreply.github.com> Date: Mon, 5 Aug 2024 18:57:21 +0200 Subject: [PATCH] =?UTF-8?q?chore(utils):=20extend=20fs=20utils=20with=20cr?= =?UTF-8?q?eateCacheReadStream=20and=20statCach=E2=80=A6=20(#30600)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/util/fs/index.spec.ts | 36 ++++++++++++++++++++++++++++++++++++ lib/util/fs/index.ts | 16 ++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/util/fs/index.spec.ts b/lib/util/fs/index.spec.ts index 7a7fcb8f0b..4a4f114602 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 64512d6257..5ac59f8027 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 }, -- GitLab