Skip to content

Commit

Permalink
feat: allow WebSource to have options
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Jan 20, 2024
1 parent 695f638 commit 1b1a436
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/WebSourceFile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Options } from './Options';

export interface WebSourceFile {
relativePath: string;
lastModified?: number;
size?: number;
baseURL?: string;
options?: Options;
}

export interface WebSource {
Expand Down
Binary file added src/__tests__/data.zp
Binary file not shown.
71 changes: 66 additions & 5 deletions src/__tests__/fileCollectionFromWebSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { readdir, stat, readFile } from 'fs/promises';
import { join } from 'path';

import fetch from 'cross-fetch';
import { rest } from 'msw';
import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node';

import { fileCollectionFromWebSource } from '../fileCollectionFromWebSource';

let fileRequestedCounter = 0;
const server = setupServer(
rest.get('http://localhost/data*', async (req, res, ctx) => {
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}`);
}
Expand Down Expand Up @@ -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);
});
5 changes: 4 additions & 1 deletion src/fileCollectionFromWebSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 1b1a436

Please sign in to comment.