Skip to content

Commit 55d6c94

Browse files
committed
fix: only read the body of the response which worked
1 parent 4164cba commit 55d6c94

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

server/utils/readme-loaders.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ export async function fetchReadmeFromJsdelivr(
5555
return null
5656
}
5757

58-
return await response.text()
58+
return response
5959
} catch {
6060
return null
6161
}
6262
}),
6363
)
6464

65-
const matchedReadme = responses.find((response): response is string => response !== null)
65+
const matchedReadme = responses.find((response): response is Response => response !== null)
6666
if (matchedReadme) {
67-
return matchedReadme
67+
return await matchedReadme.text()
6868
}
6969
}
7070

test/unit/server/utils/readme-loaders.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ describe('fetchReadmeFromJsdelivr', () => {
135135
await expect(resultPromise).resolves.toBe('# Package')
136136
expect(fetchMock).toHaveBeenCalledTimes(3)
137137
})
138+
139+
it('reads only the matched successful response body', async () => {
140+
const firstTextMock = vi.fn().mockResolvedValue('# First')
141+
const secondTextMock = vi.fn().mockResolvedValue('# Second')
142+
const fetchMock = vi
143+
.fn()
144+
.mockResolvedValueOnce({
145+
ok: true,
146+
text: firstTextMock,
147+
})
148+
.mockResolvedValueOnce({
149+
ok: true,
150+
text: secondTextMock,
151+
})
152+
vi.stubGlobal('fetch', fetchMock)
153+
154+
const result = await fetchReadmeFromJsdelivr('pkg', ['README.md', 'readme.md'])
155+
156+
expect(result).toBe('# First')
157+
expect(firstTextMock).toHaveBeenCalledTimes(1)
158+
expect(secondTextMock).not.toHaveBeenCalled()
159+
})
138160
})
139161

140162
describe('resolvePackageReadmeSource', () => {

0 commit comments

Comments
 (0)