Skip to content

Commit d27b6c4

Browse files
committed
More perf tweaks
1 parent 29e1e93 commit d27b6c4

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/connection.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,20 @@ export class Connection {
286286
let parsedBody: any = {};
287287
if (contentType && contentType.match(MIME_JSON)) {
288288
try {
289-
if (res.body && expectBinary) {
290-
parsedBody = (res.body as Buffer).toString("utf-8");
291-
} else {
292-
parsedBody = (res.body as string) || "";
293-
}
289+
parsedBody = res.body || "";
294290
parsedBody = JSON.parse(parsedBody);
295291
} catch (e) {
296292
if (!expectBinary) {
293+
if (typeof parsedBody !== "string") {
294+
parsedBody = res.body.toString("utf-8");
295+
}
297296
e.response = res;
298297
cb(e);
299298
return;
300299
}
301300
}
301+
} else if (res.body && !expectBinary) {
302+
parsedBody = res.body.toString("utf-8");
302303
}
303304
if (
304305
parsedBody &&

src/util/request.node.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export function createRequest(
4646
else agent = new HttpAgent(agentOptions);
4747
}
4848
return function request(
49-
{ method, url, headers, body, expectBinary = false }: RequestOptions,
50-
cb: Errback<ArangojsResponse>
49+
{ method, url, headers, body }: RequestOptions,
50+
callback: Errback<ArangojsResponse>
5151
) {
5252
let path = baseUrlParts.pathname
5353
? url.pathname
@@ -64,10 +64,7 @@ export function createRequest(
6464
options.hostname = baseUrlParts.hostname;
6565
options.port = baseUrlParts.port;
6666
options.auth = baseUrlParts.auth;
67-
let callback: Errback<IncomingMessage> = (err, res) => {
68-
callback = () => undefined;
69-
cb(err, res);
70-
};
67+
let called = false;
7168
const req = (isTls ? httpsRequest : httpRequest)(
7269
options,
7370
(res: IncomingMessage) => {
@@ -76,17 +73,18 @@ export function createRequest(
7673
res.on("end", () => {
7774
const result = res as ArangojsResponse;
7875
result.body = Buffer.concat(data);
79-
if (!expectBinary) {
80-
result.body = result.body.toString("utf-8");
81-
}
76+
if (called) return;
77+
called = true;
8278
callback(null, result);
8379
});
8480
}
8581
);
8682
req.on("error", err => {
8783
const error = err as ArangojsError;
8884
error.request = req;
89-
callback(error);
85+
if (called) return;
86+
called = true;
87+
callback(err);
9088
});
9189
if (body) req.write(body);
9290
req.end();

0 commit comments

Comments
 (0)