Skip to content

Commit 6d7c758

Browse files
authored
fix(sparql-qlever): filter unsupported MIME types before downloading (#214)
- Check supportedFormats before download loop to avoid wasting bandwidth on distributions that will fail after download - Extract MIME-to-format mapping into a reusable Map - Simplify fileFormatFromMimeType to use map lookup
1 parent d6950ce commit 6d7c758

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

packages/sparql-qlever/src/importer.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import { basename, dirname } from 'path';
1313
import { writeFile } from 'node:fs/promises';
1414
import { TaskRunner } from '@lde/task-runner';
1515

16+
type fileFormat = 'nt' | 'nq' | 'ttl';
17+
18+
const supportedFormats = new Map<string, fileFormat>([
19+
['application/n-triples', 'nt'],
20+
['application/n-quads', 'nq'],
21+
['text/turtle', 'ttl'],
22+
]);
23+
1624
export interface Options {
1725
taskRunner: TaskRunner<unknown>;
1826
indexName?: string;
@@ -52,7 +60,8 @@ export class Importer implements ImporterInterface {
5260
.getDownloadDistributions()
5361
.filter(
5462
(distribution): distribution is Distribution & { mimeType: string } =>
55-
distribution.mimeType !== undefined,
63+
distribution.mimeType !== undefined &&
64+
supportedFormats.has(distribution.mimeType),
5665
);
5766
if (downloadDistributions.length === 0) {
5867
return new NotSupported();
@@ -92,16 +101,11 @@ export class Importer implements ImporterInterface {
92101
}
93102

94103
private fileFormatFromMimeType(mimeType: string): fileFormat {
95-
switch (mimeType) {
96-
case 'application/n-triples':
97-
return 'nt';
98-
case 'application/n-quads':
99-
return 'nq';
100-
case 'text/turtle':
101-
return 'ttl';
102-
default:
103-
throw new Error(`Unsupported media type: ${mimeType}`);
104+
const format = supportedFormats.get(mimeType);
105+
if (format === undefined) {
106+
throw new Error(`Unsupported media type: ${mimeType}`);
104107
}
108+
return format;
105109
}
106110

107111
private async index(file: string, format: fileFormat): Promise<void> {
@@ -127,5 +131,3 @@ export class Importer implements ImporterInterface {
127131
await this.taskRunner.wait(indexTask);
128132
}
129133
}
130-
131-
type fileFormat = 'nt' | 'nq' | 'ttl';

packages/sparql-qlever/vite.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ export default mergeConfig(
1212
},
1313
coverage: {
1414
thresholds: {
15-
lines: 78.04,
15+
lines: 82.92,
1616
functions: 100,
17-
branches: 45.45,
18-
statements: 78.04,
17+
branches: 54.54,
18+
statements: 82.92,
1919
},
2020
},
2121
},
22-
})
22+
}),
2323
);

0 commit comments

Comments
 (0)