apitally
Version:
Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, and Koa.
86 lines (85 loc) • 2.16 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/common/response.ts
async function measureResponseSize(response, tee = true) {
const [newResponse1, newResponse2] = tee ? teeResponse(response) : [
response,
response
];
let size = 0;
if (newResponse2.body) {
let done = false;
const reader = newResponse2.body.getReader();
while (!done) {
const result = await reader.read();
done = result.done;
if (!done && result.value) {
size += result.value.byteLength;
}
}
}
return [
size,
newResponse1
];
}
__name(measureResponseSize, "measureResponseSize");
async function getResponseBody(response, tee = true) {
const [newResponse1, newResponse2] = tee ? teeResponse(response) : [
response,
response
];
const responseBuffer = Buffer.from(await newResponse2.arrayBuffer());
return [
responseBuffer,
newResponse1
];
}
__name(getResponseBody, "getResponseBody");
async function getResponseJson(response) {
const contentType = response.headers.get("content-type");
if (contentType == null ? void 0 : contentType.includes("application/json")) {
const [newResponse1, newResponse2] = teeResponse(response);
const responseJson = await newResponse2.json();
return [
responseJson,
newResponse1
];
}
return [
null,
response
];
}
__name(getResponseJson, "getResponseJson");
function teeResponse(response) {
if (!response.body) {
return [
response,
response
];
}
const [stream1, stream2] = response.body.tee();
const newResponse1 = new Response(stream1, {
status: response.status,
statusText: response.statusText,
headers: response.headers
});
const newResponse2 = new Response(stream2, {
status: response.status,
statusText: response.statusText,
headers: response.headers
});
return [
newResponse1,
newResponse2
];
}
__name(teeResponse, "teeResponse");
export {
getResponseBody,
getResponseJson,
measureResponseSize,
teeResponse
};
//# sourceMappingURL=response.js.map