"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; 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/index.ts var src_exports = {}; __export(src_exports, { KeyvEtcd: () => KeyvEtcd, default: () => src_default }); module.exports = __toCommonJS(src_exports); var import_events = require("events"); var import_etcd3 = require("etcd3"); var KeyvEtcd = class extends import_events.EventEmitter { ttlSupport; opts; client; lease; namespace; constructor(url, options) { super(); this.ttlSupport = typeof options?.ttl === "number"; url ??= {}; if (typeof url === "string") { url = { url }; } if (url.uri) { url = { url: url.uri, ...url }; } if (url.ttl) { this.ttlSupport = typeof url.ttl === "number"; } this.opts = { url: "127.0.0.1:2379", ...url, ...options }; this.opts.url = this.opts.url.replace(/^etcd:\/\//, ""); this.client = new import_etcd3.Etcd3({ hosts: this.opts.url }); this.client.getRoles().catch((error) => this.emit("error", error)); if (this.ttlSupport) { this.lease = this.client.lease(this.opts.ttl / 1e3, { autoKeepAlive: false }); } } async get(key) { return this.client.get(key); } async getMany(keys) { const promises = []; for (const key of keys) { promises.push(this.get(key)); } return Promise.allSettled(promises).then((values) => { const data = []; for (const value of values) { if (value.value === null) { data.push(void 0); } else { data.push(value.value); } } return data; }); } async set(key, value) { let client = "client"; if (this.opts.ttl) { client = "lease"; } await this[client].put(key).value(value); } async delete(key) { if (typeof key !== "string") { return false; } return this.client.delete().key(key).then((key2) => key2.deleted !== "0"); } async deleteMany(keys) { const promises = []; for (const key of keys) { promises.push(this.delete(key)); } return Promise.allSettled(promises).then((values) => values.every((x) => x.value === true)); } async clear() { const promise = this.namespace ? this.client.delete().prefix(this.namespace) : this.client.delete().all(); return promise.then(() => void 0); } async *iterator(namespace) { const iterator = await this.client.getAll().prefix(namespace ? namespace + ":" : "").keys(); for await (const key of iterator) { const value = await this.get(key); yield [key, value]; } } async has(key) { return this.client.get(key).exists(); } async disconnect() { return this.client.close(); } }; var src_default = KeyvEtcd; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { KeyvEtcd });