apitally
Version:
Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, Hapi, and Koa.
125 lines • 4.08 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);
var tempGzipFile_exports = {};
__export(tempGzipFile_exports, {
checkWritableFs: () => checkWritableFs,
default: () => TempGzipFile
});
module.exports = __toCommonJS(tempGzipFile_exports);
var import_node_buffer = require("node:buffer");
var import_node_crypto = require("node:crypto");
var import_node_fs = require("node:fs");
var import_promises = require("node:fs/promises");
var import_node_os = require("node:os");
var import_node_path = require("node:path");
var import_node_zlib = require("node:zlib");
const TEMP_DIR = (0, import_node_path.join)((0, import_node_os.tmpdir)(), "apitally");
function checkWritableFs() {
try {
(0, import_node_fs.mkdirSync)(TEMP_DIR, {
recursive: true
});
const testPath = (0, import_node_path.join)(TEMP_DIR, `test_${(0, import_node_crypto.randomUUID)()}`);
(0, import_node_fs.writeFileSync)(testPath, "test");
(0, import_node_fs.unlinkSync)(testPath);
return true;
} catch (error) {
return false;
}
}
__name(checkWritableFs, "checkWritableFs");
const _TempGzipFile = class _TempGzipFile {
uuid;
filePath;
gzip;
writeStream;
readyPromise;
closedPromise;
constructor(name) {
(0, import_node_fs.mkdirSync)(TEMP_DIR, {
recursive: true
});
this.uuid = (0, import_node_crypto.randomUUID)();
this.filePath = (0, import_node_path.join)(TEMP_DIR, `${name}_${this.uuid}.gz`);
this.writeStream = (0, import_node_fs.createWriteStream)(this.filePath);
this.readyPromise = new Promise((resolve, reject) => {
this.writeStream.once("ready", resolve);
this.writeStream.once("error", reject);
});
this.closedPromise = new Promise((resolve, reject) => {
this.writeStream.once("close", resolve);
this.writeStream.once("error", reject);
});
this.gzip = (0, import_node_zlib.createGzip)();
this.gzip.pipe(this.writeStream);
}
get size() {
return this.writeStream.bytesWritten;
}
async writeLines(lines) {
if (lines.length === 0) return;
await this.readyPromise;
const parts = [];
const newline = import_node_buffer.Buffer.from("\n");
for (const line of lines) {
parts.push(line, newline);
}
const combined = import_node_buffer.Buffer.concat(parts);
return new Promise((resolve, reject) => {
this.gzip.write(combined, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
async getContent() {
return new Promise((resolve, reject) => {
(0, import_node_fs.readFile)(this.filePath, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
}
async close() {
await new Promise((resolve) => {
this.gzip.end(() => {
resolve();
});
});
await this.closedPromise;
}
async delete() {
await this.close();
await (0, import_promises.unlink)(this.filePath);
}
};
__name(_TempGzipFile, "TempGzipFile");
let TempGzipFile = _TempGzipFile;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
checkWritableFs
});
//# sourceMappingURL=tempGzipFile.cjs.map