Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ describe('followDownload', () => {
).rejects.toThrow(/Columns not in this file: nope/);
});

it('filters a tab-separated report that quotes every field', async () => {
// The FBA inventory ledger wraps each field in double quotes.
const quoted =
'"Date"\t"FNSKU"\t"Title"\t"Quantity"\n' +
'"12/01/2026"\t"X001"\t"Mug ""XL"""\t"4"\n' +
'"12/01/2026"\t"X002"\t"Cup"\t"2"\n';
const out = await followDownload(
{ url: LINK },
config,
{ filterColumn: 'FNSKU', filterValue: 'X001' },
fakeFetch(Buffer.from(quoted)) as any,
);
expect(out).toMatchObject({
format: 'tsv',
columns: ['Date', 'FNSKU', 'Title', 'Quantity'],
matchedRows: 1,
rows: [{ Date: '12/01/2026', FNSKU: 'X001', Title: 'Mug "XL"', Quantity: '4' }],
});
});

it('caps the page size', async () => {
const out = await followDownload({ url: LINK }, config, { limit: 100000 }, fakeFetch(Buffer.from(TSV)) as any);
expect(out.returned).toBe(3);
Expand Down Expand Up @@ -126,6 +146,11 @@ describe('download helpers', () => {
]);
});

it('strips the quotes around tab-separated fields only when a field is wrapped', () => {
expect(parseDelimited('"a"\tb\t"say ""hi"""\t"\t""\n', '\t')).toEqual([['a', 'b', 'say "hi"', '"', '']]);
expect(parseDelimited('12" pipe\tx\n', '\t')).toEqual([['12" pipe', 'x']]);
});

it('validates the stored block', () => {
expect(validateDownloadConfig(config)).toBeNull();
expect(validateDownloadConfig({ urlField: 'url' })).toMatch(/allowedHosts/);
Expand Down
18 changes: 15 additions & 3 deletions packages/backend/src/connectors/engines/download-follow.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,27 @@ export function decodeText(buf: Buffer, contentType: string | undefined): string
}

/**
* Split delimited text into records. Tabs: Amazon flat files, no quoting.
* Commas: RFC 4180 quoting ("" inside quotes, line breaks in quoted fields).
* A tab-separated field wrapped in double quotes loses them, and "" inside
* becomes ". Some Amazon reports (FBA inventory ledger) quote every field.
*/
function unquoteTabField(field: string): string {
if (field.length >= 2 && field.startsWith('"') && field.endsWith('"')) {
return field.slice(1, -1).replace(/""/g, '"');
}
return field;
}

/**
* Split delimited text into records. Tabs: Amazon flat files, one record per
* line; a field may be wrapped in double quotes. Commas: RFC 4180 quoting
* ("" inside quotes, line breaks in quoted fields).
*/
export function parseDelimited(text: string, delimiter: '\t' | ','): string[][] {
const out: string[][] = [];
if (delimiter === '\t') {
for (const line of text.split(/\r?\n/)) {
if (line.length === 0) continue;
out.push(line.split('\t'));
out.push(line.split('\t').map(unquoteTabField));
}
return out;
}
Expand Down
Loading