Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/utils/govinfo-bulk-listing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ export function parseGovInfoBulkListing(xml: string, baseUrl: string): GovInfoBu
name,
href,
url: url.toString(),
kind: classifyListingEntry({ name, href, url: url.toString() }),
kind: classifyListingEntry({ name, href, url: url.toString() }, entry),
});
}

return dedupeEntries(entries);
}

export function classifyListingEntry(entry: Pick<GovInfoBulkListingEntry, 'name' | 'href' | 'url'>): 'directory' | 'file' {
export function classifyListingEntry(entry: Pick<GovInfoBulkListingEntry, 'name' | 'href' | 'url'>, rawEntry?: Record<string, unknown>): 'directory' | 'file' {
if (rawEntry?.folder === true || rawEntry?.folder === 'true') {
return 'directory';
}
if (entry.href.endsWith('/') || entry.name.endsWith('/') || new URL(entry.url).pathname.endsWith('/')) {
return 'directory';
}
Expand All @@ -84,6 +87,12 @@ function findEntriesRoot(parsed: unknown): unknown {
throw new Error('invalid_listing_payload: listing XML did not parse into an object');
}

// GovInfo XML: <data><files><file>...</file></files></data>
const data = parsed.data;
if (isRecord(data) && data.files) {
return data.files;
}

return parsed.directory ?? parsed.listing ?? parsed.files ?? parsed;
}

Expand Down Expand Up @@ -120,6 +129,9 @@ function readStringField(entry: Record<string, unknown>, fields: string[]): stri
if (typeof value === 'string' && value.trim().length > 0) {
return value.trim();
}
if (typeof value === 'number') {
return String(value);
}
}
return null;
}
Expand Down
Loading