Skip to content

Commit

Permalink
fix: IPFS gateway should return only json (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e authored Sep 7, 2023
1 parent 531ca44 commit b5a7507
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,20 @@ router.get('/ipfs/*', async (req, res) => {
if (!response.ok) {
return Promise.reject(response.status);
}
status = 1;

return { gateway, json: await response.json() };
if (!['text/plain', 'application/json'].includes(response.headers.get('content-type'))) {
return Promise.reject('');
}

let json;
try {
json = await response.json();
} catch {
return Promise.reject('');
}

status = 1;
return { gateway, json };
} finally {
end({ status });
countOpenGatewaysRequest.dec({ name: gateway });
Expand Down
10 changes: 9 additions & 1 deletion test/e2e/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ const HOST = `http://localhost:${process.env.PORT || 3003}`;

describe('GET /ipfs/*', () => {
describe('when the IPFS cid exists', () => {
it('returns the JSON file', async () => {
it('returns a JSON file', async () => {
const response = await request(HOST).get(
'/ipfs/bafkreib5epjzumf3omr7rth5mtcsz4ugcoh3ut4d46hx5xhwm4b3pqr2vi'
);

expect(response.statusCode).toBe(200);
expect(response.body).toEqual({ status: 'OK' });
});

it('returns a 400 error when not a JSON file', async () => {
const response = await request(HOST).get(
'/ipfs/bafybeie2x4ptheqskiauhfz4w4pbq7o6742oupitganczhjanvffp2spti'
);

expect(response.statusCode).toBe(400);
});
});

describe('when the IPFS cid does not exist', () => {
Expand Down

0 comments on commit b5a7507

Please sign in to comment.