UNPKG

node-red-contrib-home-assistant-websocket

Version:
2 lines (1 loc) 2.15 kB
"use strict";var w=Object.create;var n=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var p=Object.getPrototypeOf,f=Object.prototype.hasOwnProperty;var y=(a,t)=>{for(var e in t)n(a,e,{get:t[e],enumerable:!0})},d=(a,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of b(t))!f.call(a,o)&&o!==e&&n(a,o,{get:()=>t[o],enumerable:!(s=g(t,o))||s.enumerable});return a};var i=(a,t,e)=>(e=a!=null?w(p(a)):{},d(t||!a||!a.__esModule?n(e,"default",{value:a,enumerable:!0}):e,a)),D=a=>d(n({},"__esModule",{value:!0}),a);var E={};y(E,{PACKAGE_NAME:()=>l,default:()=>P});module.exports=D(E);var r=i(require("fs")),c=i(require("lowdb")),u=i(require("lowdb/adapters/FileAsync")),h=require("../../globals");const l="node-red-contrib-home-assistant-websocket",m=`${l}.json`;class N{#a;#t;#o=!1;#s;constructor({path:t,adapter:e}={}){this.#a=e,this.#s=t}checkPermissions(t){if(!t)return!0;try{r.default.accessSync(t,r.default.constants.R_OK|r.default.constants.W_OK)}catch{throw new Error("Cannot read/write to storage file for Home Assistant nodes")}return!0}async init(){if(this.#o)return;this.#o=!0;const t=this.#s??h.RED.settings.userDir,e=`${t}/${m}`;if(this.checkPermissions(t)){const s=this.#a??new u.default(e);this.#t=await(0,c.default)(s),await this.#t.defaults({nodes:{}}).write()}}#e(t){return t.replace(/\./g,"_")}async saveNodeData(t,e,s){if(!this.#t)throw new Error("cannot save node data without a db");const o=`nodes.${this.#e(t)}.${e}`;return this.#t.set(o,s).write()}getNodeData(t,e){if(!this.#t)throw new Error("cannot get node data without a db");const o=this.#t.get("nodes").value()[this.#e(t)];if(o)return e?o[e]:o}async removeNodeData(t){if(!this.#t)throw new Error("cannot remove node data without a db");return await this.#t.unset(`nodes.${this.#e(t)}`).write(),!0}async saveIssues(t){if(!this.#t)throw new Error("cannot save node data without a db");return this.#t.set("issues.hidden",t).write()}getIssues(){if(!this.#t)throw new Error("cannot get node data without a db");const t=this.#t.get("issues.hidden").value();return t||[]}}var P=new N;0&&(module.exports={PACKAGE_NAME});