"use strict";
/**
 * Copyright: (c) Myia SAS 2026.
 * This file and its contents are licensed under the AGPLv3 License.
 * Please see the LICENSE file at the root of this repository
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.NdjsonExporter = void 0;
// NdjsonExporter — HealTraceExporter adapter that appends one JSON record
// per line to a file on disk.
//
// Designed to be crash-safe: every `write()` calls `fs.writeSync()`
// so the record is on disk before the call returns. If the test
// process is SIGKILL'd one instruction later the file still
// contains every record written up to that point. This is the
// durable record of truth; the live agent path (AgentHttpExporter) is
// best-effort and may lose its tail on crash.
//
// The file descriptor is opened in the constructor and closed by
// `close()`. Callers MUST call `close()` at test teardown; omitting
// it leaks the fd but does not lose data (writes were already
// flushed line-by-line).
const fs = __importStar(require("fs"));
class NdjsonExporter {
    constructor(filePath) {
        this.closed = false;
        // O_APPEND so concurrent appenders (not expected, but cheap) are
        // safe and so truncation on reopen never happens.
        this.fd = fs.openSync(filePath, 'a');
    }
    write(record) {
        if (this.closed)
            return;
        fs.writeSync(this.fd, JSON.stringify(record) + '\n');
    }
    close() {
        if (this.closed)
            return Promise.resolve();
        this.closed = true;
        fs.closeSync(this.fd);
        return Promise.resolve();
    }
}
exports.NdjsonExporter = NdjsonExporter;
