Skip to content

Commit

Permalink
Refactor to handle charsets in single func
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Dec 31, 2024
1 parent 109f0fb commit 5294a6d
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions services/feed-requests/src/feed-fetcher/feed-fetcher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ interface FetchResponse {
status: number;
headers: Map<'etag' | 'last-modified' | 'server' | 'content-type', string>;
text: () => Promise<string>;
arrayBuffer: () => Promise<ArrayBuffer>;
}

@Injectable()
Expand Down Expand Up @@ -251,10 +250,7 @@ export class FeedFetcherService {
let text: string | null = null;

try {
text =
res.status === HttpStatus.NOT_MODIFIED
? ''
: await this.maybeDecodeResponse(res);
text = res.status === HttpStatus.NOT_MODIFIED ? '' : await res.text();

if (request.status !== RequestStatus.OK) {
logger.debug(`Bad status code ${res.status} for url ${url}`, {
Expand Down Expand Up @@ -434,6 +430,11 @@ export class FeedFetcherService {
maxRedirections: 10,
});

const contentTypes =
typeof r.headers['content-type'] === 'string'
? r.headers['content-type'].split(';')
: r.headers['content-type'] || [];

const normalizedHeaders = Object.entries(r.headers).reduce(
(acc, [key, val]) => {
if (typeof val === 'string') {
Expand Down Expand Up @@ -464,28 +465,23 @@ export class FeedFetcherService {
headers,
ok: r.statusCode >= 200 && r.statusCode < 300,
status: r.statusCode,
text: () => r.body.text(),
arrayBuffer: () => r.body.arrayBuffer(),
};
}

private async maybeDecodeResponse(
res: Awaited<FetchResponse>,
): Promise<string> {
const charset = res.headers
.get('content-type')
?.split(';')
.find((s) => s.includes('charset'))
?.split('=')[1]
.trim();

if (!charset || /utf-*8/i.test(charset)) {
return res.text();
}
text: async () => {
const charset = contentTypes
.find((s) => s.includes('charset'))
?.split('=')[1]
.trim();

if (!charset || /utf-*8/i.test(charset)) {
return r.body.text();
}

const arrBuffer = await res.arrayBuffer();
const decoded = iconv.decode(Buffer.from(arrBuffer), charset).toString();
const arrBuffer = await r.body.arrayBuffer();
const decoded = iconv
.decode(Buffer.from(arrBuffer), charset)
.toString();

return decoded;
return decoded;
},
};
}
}

0 comments on commit 5294a6d

Please sign in to comment.