Skip to content

Commit

Permalink
Fix logic for checking for files in non-nested and cleaned packages
Browse files Browse the repository at this point in the history
  • Loading branch information
jafeltra committed Dec 31, 2024
1 parent 2617104 commit 2ee37fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
26 changes: 17 additions & 9 deletions src/cache/BrowserBasedPackageCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,23 @@ export class BrowserBasedPackageCache implements PackageCache {
});
stream.on('end', () => {
try {
const resource = JSON.parse(resourceData);
// Split on '/' to ensure files that were in nested folders are not cached
if (resource.resourceType && resource.id && header.name.split('/').length <= 2) {
resources.push(resource);
} else if (header.name.endsWith('/package.json') || header.name === 'package.json') {
// Add a placeholder resourceType and id so we can uniquely store and retrieve package.json
resource.resourceType = 'packagejson';
resource.id = name; // This should be the same as resource.name
resources.push(resource);
// Only add:
// - files that are directly in 'packages' and not in nested folders to align with DiskBasedPackageCache, which reads directories non-recursively
// - files at the root-level to align with cleanCachedPackage in DiskBasedPackageCache
const fileParts = header.name.split('/');
const isInCleanedPackage =
(fileParts.length === 2 && fileParts[0] === 'package') || fileParts.length === 1;

if (isInCleanedPackage) {
const resource = JSON.parse(resourceData);
if (resource.resourceType && resource.id) {
resources.push(resource);
} else if (header.name.endsWith('/package.json') || header.name === 'package.json') {
// Add a placeholder resourceType and id so we can uniquely store and retrieve package.json
resource.resourceType = 'packagejson';
resource.id = name; // This should be the same as resource.name
resources.push(resource);
}
}
} catch {
// Right now, if the JSON doesn't parse, we just ignore it.
Expand Down
4 changes: 2 additions & 2 deletions test/cache/BrowserBasedPackageCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ describe('BrowserBasedPackageCache', () => {
);
expect(packageJSON.name).toEqual('fhir.small.wrong');
expect(packageJSON.version).toEqual('0.1.0');
// Check that resources in example and other folders are added if possible
// Check that resources in nested folders are not added
const patientExample = allSavedResources.find(
(r: any) => r.resourceType === 'Patient' && r.id === 'PatientExample'
);
expect(patientExample.name).toBeDefined();
expect(patientExample).toBeUndefined();
const bundle = allSavedResources.find((r: any) => r.resourceType === 'Bundle');
expect(bundle).toBeUndefined(); // Bundle resource doesn't have an id
});
Expand Down

0 comments on commit 2ee37fe

Please sign in to comment.