From 1b1a436988aec57c925ab55a09da2e9dcbc946c4 Mon Sep 17 00:00:00 2001 From: Luc Patiny Date: Sat, 20 Jan 2024 07:57:19 +0100 Subject: [PATCH] feat: allow WebSource to have options --- src/WebSourceFile.ts | 3 + src/__tests__/data.zp | Bin 0 -> 1816 bytes .../fileCollectionFromWebSource.test.ts | 71 ++++++++++++++++-- src/fileCollectionFromWebSource.ts | 5 +- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 src/__tests__/data.zp diff --git a/src/WebSourceFile.ts b/src/WebSourceFile.ts index e87b359..d291bc2 100644 --- a/src/WebSourceFile.ts +++ b/src/WebSourceFile.ts @@ -1,8 +1,11 @@ +import { Options } from './Options'; + export interface WebSourceFile { relativePath: string; lastModified?: number; size?: number; baseURL?: string; + options?: Options; } export interface WebSource { diff --git a/src/__tests__/data.zp b/src/__tests__/data.zp new file mode 100644 index 0000000000000000000000000000000000000000..43467260152e8cff3b479b6c320a29a084987432 GIT binary patch literal 1816 zcmb`HuTR536vwYW*NhMZ{R8kMT)H_F%^Woqo56Jy5-I#@>#=*ymcEd^raw$S?|bk1X&hO$&!Fu$5g8{h3u z#0mG?!4@;@Z-X)UYP=TFREYHArnMnXK;(GLLrW5A*$&EcdXNpWzs0?;UR>m|IBIxT zvsc#=1fVR|ThC8Pd_(dl$DdD_54%v^5 { - const pathname = join(__dirname, req.url.pathname); + http.get('http://localhost/data*', async ({ request }) => { + const pathname = join(__dirname, new URL(request.url).pathname); const pathnameStat = await stat(pathname); if (pathnameStat.isDirectory()) { const source = await getJSON(join(__dirname, 'dataUnzip')); - return res(ctx.json(source)); + return HttpResponse.json(source); } else if (pathnameStat.isFile()) { fileRequestedCounter++; const data = await readFile(pathname); - return res(ctx.body(data)); + return HttpResponse.arrayBuffer(data); } else { throw new Error(`unknown path: ${pathname}`); } @@ -216,3 +216,64 @@ async function appendFiles(files: any, currentDir: string) { } } } + +test('zip file should be unzipped', async () => { + const source = { + entries: [ + { + relativePath: 'data.zip', + }, + ], + baseURL: 'http://localhost/', + }; + + const fileCollection = await fileCollectionFromWebSource(source, { + cache: true, + }); + expect(fileCollection.files).toHaveLength(6); + const first = await fileCollection.files[0].text(); + expect(first).toBe('a'); + await fileCollection.files[0].text(); + // cached it is loaded only once + expect(fileRequestedCounter).toBe(1); +}); + +test('zip file has strange extension and should not be unzipped', async () => { + const source = { + entries: [ + { + relativePath: 'data.zp', + }, + ], + baseURL: 'http://localhost/', + }; + + const fileCollection = await fileCollectionFromWebSource(source, { + cache: true, + }); + expect(fileCollection.files).toHaveLength(1); +}); + +test('zip file should be unzipped because we add options', async () => { + const source = { + entries: [ + { + relativePath: 'data.zp', + options: { + unzip: { zipExtensions: ['zp'] }, + }, + }, + ], + baseURL: 'http://localhost/', + }; + + const fileCollection = await fileCollectionFromWebSource(source, { + cache: true, + }); + expect(fileCollection.files).toHaveLength(6); + const first = await fileCollection.files[0].text(); + expect(first).toBe('a'); + await fileCollection.files[0].text(); + // cached it is loaded only once + expect(fileRequestedCounter).toBe(1); +}); diff --git a/src/fileCollectionFromWebSource.ts b/src/fileCollectionFromWebSource.ts index e9bd635..d36a68a 100644 --- a/src/fileCollectionFromWebSource.ts +++ b/src/fileCollectionFromWebSource.ts @@ -77,7 +77,10 @@ export async function fileCollectionFromWebSource( throw new Error('stream not yet implemented'); }, }; - const expanded = await expandAndFilter(item, options); + const expanded = await expandAndFilter(item, { + ...options, + ...entry.options, + }); // we should be aware that we don't cache the zip file itself for (const item of expanded) { fileCollectionItems.push(