UNPKG

apitally

Version:

Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, Hapi, and Koa.

102 lines 2.75 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { Buffer as Buffer2 } from "node:buffer"; import { randomUUID } from "node:crypto"; import { createWriteStream, mkdirSync, readFile, unlinkSync, writeFileSync } from "node:fs"; import { unlink } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createGzip } from "node:zlib"; const TEMP_DIR = join(tmpdir(), "apitally"); function checkWritableFs() { try { mkdirSync(TEMP_DIR, { recursive: true }); const testPath = join(TEMP_DIR, `test_${randomUUID()}`); writeFileSync(testPath, "test"); unlinkSync(testPath); return true; } catch (error) { return false; } } __name(checkWritableFs, "checkWritableFs"); const _TempGzipFile = class _TempGzipFile { uuid; filePath; gzip; writeStream; readyPromise; closedPromise; constructor(name) { mkdirSync(TEMP_DIR, { recursive: true }); this.uuid = randomUUID(); this.filePath = join(TEMP_DIR, `${name}_${this.uuid}.gz`); this.writeStream = 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 = 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 = Buffer2.from("\n"); for (const line of lines) { parts.push(line, newline); } const combined = Buffer2.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) => { 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 unlink(this.filePath); } }; __name(_TempGzipFile, "TempGzipFile"); let TempGzipFile = _TempGzipFile; export { checkWritableFs, TempGzipFile as default }; //# sourceMappingURL=tempGzipFile.js.map