Skip to content

Commit 2ce61f9

Browse files
Uzlopakwolfy1339
andauthored
chore: improve getResponseData (#715)
* chore: improve getResponseData * use fast-content-type-parse * make more resilient --------- Co-authored-by: wolfy1339 <[email protected]>
1 parent fcc5b25 commit 2ce61f9

6 files changed

+53
-16
lines changed

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@octokit/endpoint": "^10.0.0",
2929
"@octokit/request-error": "^6.0.1",
3030
"@octokit/types": "^13.1.0",
31+
"fast-content-type-parse": "^2.0.0",
3132
"universal-user-agent": "^7.0.2"
3233
},
3334
"devDependencies": {

src/fetch-wrapper.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { safeParse } from "fast-content-type-parse";
12
import { isPlainObject } from "./is-plain-object.js";
23
import { RequestError } from "@octokit/request-error";
34
import type { EndpointInterface, OctokitResponse } from "@octokit/types";
@@ -147,25 +148,29 @@ export default async function fetchWrapper(
147148

148149
async function getResponseData(response: Response): Promise<any> {
149150
const contentType = response.headers.get("content-type");
150-
if (/application\/json/.test(contentType!)) {
151-
return (
152-
response
153-
.json()
154-
// In the event that we get an empty response body we fallback to
155-
// using .text(), but this should be investigated since if this were
156-
// to occur in the GitHub API it really should not return an empty body.
157-
.catch(() => response.text())
158-
// `node-fetch` is throwing a "body used already for" error if `.text()` is run
159-
// after a failed .json(). To account for that we fallback to an empty string
160-
.catch(() => "")
161-
);
162-
}
163151

164-
if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) {
165-
return response.text();
152+
if (!contentType) {
153+
return response.text().catch(() => "");
166154
}
167155

168-
return response.arrayBuffer();
156+
const mimetype = safeParse(contentType);
157+
158+
if (mimetype.type === "application/json") {
159+
let text = "";
160+
try {
161+
text = await response.text();
162+
return JSON.parse(text);
163+
} catch (err) {
164+
return text;
165+
}
166+
} else if (
167+
mimetype.type.startsWith("text/") ||
168+
mimetype.parameters.charset?.toLowerCase() === "utf-8"
169+
) {
170+
return response.text().catch(() => "");
171+
} else {
172+
return response.arrayBuffer().catch(() => new ArrayBuffer(0));
173+
}
169174
}
170175

171176
function toErrorMessage(data: string | ArrayBuffer | Record<string, unknown>) {

test/request-common.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ describe("request()", () => {
6565
json() {
6666
return Promise.resolve("funk");
6767
},
68+
text() {
69+
return Promise.resolve("funk");
70+
},
6871
}),
6972
},
7073
});

test/request-native-fetch.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -1397,4 +1397,23 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
13971397

13981398
request.closeMockServer();
13991399
});
1400+
1401+
it("invalid json as response data", async () => {
1402+
expect.assertions(4);
1403+
1404+
const request = await mockRequestHttpServer(async (req, res) => {
1405+
expect(req.method).toBe("GET");
1406+
expect(req.url).toBe("/");
1407+
1408+
res.writeHead(200, {
1409+
"content-type": "application/json",
1410+
});
1411+
res.end('"invalid');
1412+
});
1413+
1414+
const response = await request("GET /");
1415+
1416+
expect(response.status).toEqual(200);
1417+
expect(response.data).toEqual('"invalid');
1418+
});
14001419
});

test/request.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,9 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
702702
json() {
703703
return Promise.resolve("funk");
704704
},
705+
text() {
706+
return Promise.resolve("funk");
707+
},
705708
}),
706709
},
707710
});

0 commit comments

Comments
 (0)