UNPKG

@sourceregistry/node-ovsdb

Version:

Open vSwitch Database Management Protocol typescript client

5 lines (4 loc) 4.13 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("net"),u=require("fs");class l{constructor(e={}){this.socket=null,this.requestId=1,this.pendingRequests=new Map,this.isConnected=!1,this.socketPath=e.socketPath||"/var/run/openvswitch/db.sock",this.timeout=e.timeout||5e3}connect(){return new Promise((e,r)=>{if(this.isConnected){e(this);return}if(!u.existsSync(this.socketPath)){r(new Error(`OVSDB socket not found: ${this.socketPath}`));return}this.socket=a.createConnection(this.socketPath);const t=setTimeout(()=>{this._cleanup(),r(new Error(`Connection timeout after ${this.timeout}ms`))},this.timeout);this.socket.on("connect",()=>{clearTimeout(t),this.isConnected=!0,e(this)}),this.socket.on("error",o=>{clearTimeout(t),this._cleanup(),r(o)}),this.socket.on("close",()=>{this._cleanup()}),this.socket.on("data",o=>{this._handleData(o)})})}request(e,r){return new Promise((t,o)=>{if(!this.isConnected){o(new Error("Not connected to OVSDB"));return}const s=this.requestId++,c=JSON.stringify({method:e,params:r,id:s})+` `,n=setTimeout(()=>{this.pendingRequests.delete(s),o(new Error(`Request timeout for method: ${e}`))},this.timeout);this.pendingRequests.set(s,{resolve:t,reject:o,timeoutId:n}),this.socket.write(c,i=>{i&&(clearTimeout(n),this.pendingRequests.delete(s),o(i))})})}async listDbs(){const e=await this.request("list_dbs",[]);if("error"in e&&e.error!==null)throw new Error(`RPC Error: ${e.error}`);const r=e.result;if("error"in r)throw new Error(`OVSDB Error: ${r.error}`);return r}async getSchema(e="Open_vSwitch"){const r=await this.request("get_schema",[e]);if("error"in r&&r.error!==null)throw new Error(`RPC Error: ${r.error}`);const t=r.result;if("error"in t)throw new Error(`OVSDB Error: ${t.error}`);return t}async transact(e,r){const t=await this.request("transact",[e,...r]);if("error"in t&&t.error!==null)throw new Error(`RPC Error: ${t.error}`);return t.result}async monitor(e,r,t){const o=await this.request("monitor",[e,r,t]);if("error"in o&&o.error!==null)throw new Error(`RPC Error: ${o.error}`);const s=o.result;if("error"in s)throw new Error(`OVSDB Error: ${s.error}`);return s}async monitorCancel(e){const r=await this.request("monitor_cancel",[e]);if("error"in r&&r.error!==null)throw new Error(`RPC Error: ${r.error}`);const t=r.result;if("error"in t)throw new Error(`OVSDB Error: ${t.error}`);return t}async echo(e="ping"){const r=await this.request("echo",Array.isArray(e)?e:[e]);if("error"in r&&r.error!==null)throw new Error(`RPC Error: ${r.error}`);const t=r.result;if("error"in t)throw new Error(`OVSDB Error: ${t.error}`);return t}async close(){await this[Symbol.asyncDispose]()}async[Symbol.asyncDispose](){if(this.isConnected){const e=new Promise(r=>{if(this.socket){if(this.socket.destroyed||this.socket.readyState==="closed"){r();return}this.socket.once("close",()=>{r()})}else r()});this._cleanup(),await e}}_cleanup(){this.isConnected=!1,this.socket&&(this.socket.destroy(),this.socket=null),this.pendingRequests.forEach(({reject:e,timeoutId:r})=>{clearTimeout(r),e(new Error("Connection closed"))}),this.pendingRequests.clear()}_handleData(e){e.toString().trim().split(` `).forEach(t=>{if(t)try{const o=JSON.parse(t);this._handleResponse(o)}catch(o){console.error("Failed to parse JSON:",o)}})}_handleResponse(e){if(typeof e!="object"||e===null||!("id"in e)){console.error("Invalid response format:",e);return}const r=e.id,t=this.pendingRequests.get(r);if(!t){this._handleNotification(e);return}this.pendingRequests.delete(r),clearTimeout(t.timeoutId),"error"in e&&e.error!==null?t.reject(new Error(`RPC Error: ${e.error}`)):t.resolve(e)}_handleNotification(e){if(typeof e!="object"||e===null||!("method"in e)){console.error("Invalid notification format:",e);return}const r=e.method;switch(r){case"update":console.log("Received update notification:",e.params[0]);break;case"locked":console.log("Received locked notification:",e.params[0]);break;case"stolen":console.log("Received stolen notification:",e.params[0]);break;default:console.log("Received notification:",r)}}}exports.OVSDBClient=l; //# sourceMappingURL=index.cjs.js.map