apitally
Version:
Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, and Koa.
112 lines (111 loc) • 3.28 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/common/response.ts
var response_exports = {};
__export(response_exports, {
getResponseBody: () => getResponseBody,
getResponseJson: () => getResponseJson,
measureResponseSize: () => measureResponseSize,
teeResponse: () => teeResponse
});
module.exports = __toCommonJS(response_exports);
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");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getResponseBody,
getResponseJson,
measureResponseSize,
teeResponse
});
//# sourceMappingURL=response.cjs.map