var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
/**
 * Creates a {@link DiscoveryFileFs} backed by Node.js / Bun built-in modules.
 * Returns `undefined` when those modules are unavailable.
 */
export function createNodeDiscoveryFileFs() {
    try {
        const nodeRequire = require;
        const fs = nodeRequire('node:fs/promises');
        const path = nodeRequire('node:path');
        const os = nodeRequire('node:os');
        return {
            readFile(filePath) {
                return __awaiter(this, void 0, void 0, function* () {
                    try {
                        return yield fs.readFile(filePath, 'utf-8');
                    }
                    catch (_a) {
                        return null;
                    }
                });
            },
            writeFile(filePath, contents) {
                return __awaiter(this, void 0, void 0, function* () {
                    yield fs.mkdir(path.dirname(filePath), { recursive: true });
                    // mode 0o600: owner read/write only; this file contains the endpoint and PID of a local DWN server.
                    yield fs.writeFile(filePath, contents, { encoding: 'utf-8', mode: 0o600 });
                });
            },
            removeFile(filePath) {
                return __awaiter(this, void 0, void 0, function* () {
                    try {
                        yield fs.unlink(filePath);
                    }
                    catch (_a) {
                        // Ignore ENOENT; the file was already gone.
                    }
                });
            },
            isProcessAlive(pid) {
                try {
                    process.kill(pid, 0);
                    return true;
                }
                catch (_a) {
                    return false;
                }
            },
            homedir() {
                return os.homedir();
            },
        };
    }
    catch (_a) {
        return undefined;
    }
}
//# sourceMappingURL=dwn-discovery-file-fs.js.map