UNPKG

@mindconnect/mindconnect-nodejs

Version:

NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)

65 lines 2.66 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultStorage = exports.IsConfigurationStorage = void 0; const AsyncLock = require("async-lock"); const debug = require("debug"); const fs = require("fs"); const _ = require("lodash"); const path = require("path"); const log = debug("mindconnect-storage"); function IsConfigurationStorage(arg) { return arg.GetConfig !== undefined && arg.SaveConfig !== undefined; } exports.IsConfigurationStorage = IsConfigurationStorage; /** * DefaultStorage, stores the config files in the _basePath folder * * @export * @class DefaultStorage */ class DefaultStorage { GetConfig(configuration) { try { const jsonString = fs.readFileSync(path.resolve(`${this._basePath}/${configuration.content.clientId}.json`)); const json = JSON.parse(jsonString.toString()); if (_.isEqual(json.content, configuration.content)) { return json; } else { log("The configuration has changed we will onboard again."); } } catch (err) { log(`There is no configuration stored yet for agent with id ${configuration.content.clientId}`); } return configuration; } SaveConfig(config) { return __awaiter(this, void 0, void 0, function* () { const fileName = `${this._basePath}/${config.content.clientId}.json`; return yield this.lock.acquire(fileName, () => { const data = JSON.stringify(config); fs.writeFileSync(fileName, data); return config; }); }); } constructor(_basePath) { this._basePath = _basePath; if (!fs.existsSync(this._basePath)) { fs.mkdirSync(this._basePath); } this.lock = new AsyncLock({}); } } exports.DefaultStorage = DefaultStorage; //# sourceMappingURL=mindconnect-storage.js.map