@curium.rocks/data-emitter-base
Advanced tools
Comparing version 0.1.1-alpha.21 to 0.1.1-alpha.22
@@ -1,2 +0,2 @@ | ||
import { ICommand, IDataEmitter, IDataEvent, IDataEventListener, IDataEventListenerFunc, IDisposable, IExecutionResult, ISettings, IStatusChangeListener, IStatusChangeListenerFunc, IStatusEvent, ITraceableAction } from "./dataEmitter"; | ||
import { ICommand, IDataEmitter, IDataEvent, IDataEventListener, IDataEventListenerFunc, IDisposable, IEmitterDescription, IExecutionResult, IFormatSettings, ISettings, IStatusChangeListener, IStatusChangeListenerFunc, IStatusEvent, ITraceableAction } from "./dataEmitter"; | ||
import { LoggerFacade, LogLevel } from "./loggerFacade"; | ||
@@ -70,3 +70,27 @@ /** | ||
/** | ||
* serialize the important data that is needed to be able to recreate this emitter in it's current state | ||
* @param {IFormatSettings} settings used to specify expected format of the serialization | ||
* @return {Promise<string>} | ||
*/ | ||
serializeState(settings: IFormatSettings): Promise<string>; | ||
/** | ||
* | ||
* @param {string} json | ||
* @param {IFormatSettings} settings | ||
*/ | ||
protected static encrypt(json: string, settings: IFormatSettings): Promise<string>; | ||
/** | ||
* | ||
* @param {string} base64CipherText | ||
* @param {IFormatSettings} settings | ||
* @return {Promise<void>} | ||
*/ | ||
protected static decrypt(base64CipherText: string, settings: IFormatSettings): Promise<string>; | ||
/** | ||
* Control serialized properties when JSON.stringify is called | ||
* @return {unknown} | ||
*/ | ||
toJSON(): unknown; | ||
/** | ||
* | ||
* @param {IDataEvent} evt | ||
@@ -213,2 +237,18 @@ */ | ||
/** | ||
* Gets the emitter description which is used to recreate the emitter | ||
* @return {IEmitterDescription} | ||
*/ | ||
protected getEmitterDescription(): IEmitterDescription; | ||
/** | ||
* | ||
* @return {unknown} | ||
*/ | ||
protected getEmitterProperties(): unknown; | ||
/** | ||
* | ||
* @param {string} stateData | ||
* @param {IFormatSettings} settings | ||
*/ | ||
static recreateEmitter(stateData: string, settings: IFormatSettings): Promise<IDataEmitter>; | ||
/** | ||
* Cleanup any resources/timers managed by the | ||
@@ -215,0 +255,0 @@ * emitter |
"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()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BaseEmitter = void 0; | ||
var loggerFacade_1 = require("./loggerFacade"); | ||
var crypto_1 = __importDefault(require("crypto")); | ||
var provider_1 = require("./provider"); | ||
/** | ||
@@ -78,3 +119,82 @@ * Abstract base class emitter that takes care of managing registrations of | ||
/** | ||
* serialize the important data that is needed to be able to recreate this emitter in it's current state | ||
* @param {IFormatSettings} settings used to specify expected format of the serialization | ||
* @return {Promise<string>} | ||
*/ | ||
BaseEmitter.prototype.serializeState = function (settings) { | ||
if (!settings.encrypted) { | ||
return Promise.resolve(JSON.stringify(this.getEmitterDescription())); | ||
} | ||
else { | ||
return BaseEmitter.encrypt(JSON.stringify(this.getEmitterDescription()), settings); | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {string} json | ||
* @param {IFormatSettings} settings | ||
*/ | ||
BaseEmitter.encrypt = function (json, settings) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var gcmCipher, cipherBuffer, cipher, cipherBuffer; | ||
return __generator(this, function (_a) { | ||
if (settings.algorithm.toLowerCase().indexOf('gcm') != -1) { | ||
gcmCipher = crypto_1.default.createCipheriv(settings.algorithm, Buffer.from(settings.key, 'base64'), Buffer.from(settings.iv, 'base64'), { | ||
authTagLength: 16 | ||
}); | ||
cipherBuffer = Buffer.concat([gcmCipher.update(json, 'utf-8'), gcmCipher.final(), gcmCipher.getAuthTag()]); | ||
return [2 /*return*/, Promise.resolve(cipherBuffer.toString('base64'))]; | ||
} | ||
else { | ||
cipher = crypto_1.default.createCipheriv(settings.algorithm, Buffer.from(settings.key, 'base64'), Buffer.from(settings.iv, 'base64')); | ||
cipherBuffer = Buffer.concat([cipher.update(json, 'utf-8'), cipher.final()]); | ||
return [2 /*return*/, Promise.resolve(cipherBuffer.toString('base64'))]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* | ||
* @param {string} base64CipherText | ||
* @param {IFormatSettings} settings | ||
* @return {Promise<void>} | ||
*/ | ||
BaseEmitter.decrypt = function (base64CipherText, settings) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var gcmCipher, buffer, authTag, cipher, plainText, decipher, plainText; | ||
return __generator(this, function (_a) { | ||
if (settings.algorithm.toLowerCase().indexOf('gcm') != -1) { | ||
gcmCipher = crypto_1.default.createDecipheriv(settings.algorithm, Buffer.from(settings.key, 'base64'), Buffer.from(settings.iv, 'base64')); | ||
buffer = Buffer.from(base64CipherText, 'base64'); | ||
authTag = buffer.slice(buffer.length - 16); | ||
cipher = buffer.slice(0, buffer.length - 16); | ||
gcmCipher.setAuthTag(authTag); | ||
plainText = gcmCipher.update(cipher, undefined, 'utf-8'); | ||
plainText += gcmCipher.final('utf-8'); | ||
return [2 /*return*/, Promise.resolve(plainText)]; | ||
} | ||
else { | ||
decipher = crypto_1.default.createDecipheriv(settings.algorithm, Buffer.from(settings.key, 'base64'), Buffer.from(settings.iv, 'base64')); | ||
plainText = decipher.update(base64CipherText, 'base64', 'utf-8'); | ||
plainText += decipher.final('utf-8'); | ||
return [2 /*return*/, Promise.resolve(plainText)]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Control serialized properties when JSON.stringify is called | ||
* @return {unknown} | ||
*/ | ||
BaseEmitter.prototype.toJSON = function () { | ||
return { | ||
name: this.name, | ||
description: this.description, | ||
id: this.id | ||
}; | ||
}; | ||
/** | ||
* | ||
* @param {IDataEvent} evt | ||
@@ -323,2 +443,48 @@ */ | ||
/** | ||
* Gets the emitter description which is used to recreate the emitter | ||
* @return {IEmitterDescription} | ||
*/ | ||
BaseEmitter.prototype.getEmitterDescription = function () { | ||
return { | ||
type: this.getType(), | ||
name: this.name, | ||
description: this.description, | ||
id: this.id, | ||
emitterProperties: this.getEmitterProperties() | ||
}; | ||
}; | ||
/** | ||
* | ||
* @return {unknown} | ||
*/ | ||
BaseEmitter.prototype.getEmitterProperties = function () { | ||
return {}; | ||
}; | ||
/** | ||
* | ||
* @param {string} stateData | ||
* @param {IFormatSettings} settings | ||
*/ | ||
BaseEmitter.recreateEmitter = function (stateData, settings) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var description, emitterDescription; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!settings.encrypted) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, BaseEmitter.decrypt(stateData, settings)]; | ||
case 1: | ||
description = _a.sent(); | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
description = stateData; | ||
_a.label = 3; | ||
case 3: | ||
emitterDescription = JSON.parse(description); | ||
return [2 /*return*/, provider_1.ProviderSingleton.getInstance().buildEmitter(emitterDescription)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Cleanup any resources/timers managed by the | ||
@@ -325,0 +491,0 @@ * emitter |
@@ -0,1 +1,3 @@ | ||
import { IChronicler } from "./chronicler"; | ||
import { LoggerFacade } from "./loggerFacade"; | ||
/** | ||
@@ -172,3 +174,16 @@ * A action with a unique identifier, the identifier | ||
probeCurrentData(): Promise<IDataEvent>; | ||
/** | ||
* Serialize the state of the emitter in a base64 string | ||
*/ | ||
serializeState(settings: IFormatSettings): Promise<string>; | ||
} | ||
export interface IFormatSettings { | ||
encrypted: boolean; | ||
type: string; | ||
algorithm?: string; | ||
iv?: string; | ||
tag?: string; | ||
key?: string; | ||
keyName?: string; | ||
} | ||
/** | ||
@@ -189,1 +204,34 @@ * A interface that describes a composite data source that takes several related | ||
} | ||
export interface IEmitterProvider extends IEmitterFactory { | ||
registerEmitterFactory(type: string, factory: IEmitterFactory): void; | ||
hasEmitterFactory(type: string): boolean; | ||
getEmitterFactoryTypes(): Array<string>; | ||
} | ||
export interface IEmitterFactory { | ||
buildEmitter(description: IEmitterDescription): Promise<IDataEmitter>; | ||
recreateEmitter(base64StateData: string, formatSettings: IFormatSettings): Promise<IDataEmitter>; | ||
setLoggerFacade(loggerFacade: LoggerFacade): void; | ||
} | ||
export interface IChroniclerProvider extends IChroniclerFactory { | ||
registerChroniclerFactory(type: string, factory: IChroniclerFactory): void; | ||
hasChroniclerFactory(type: string): boolean; | ||
getChroniclerFactoryTypes(): Array<string>; | ||
} | ||
export interface IChroniclerFactory { | ||
buildChronicler(description: IChroniclerDescription): Promise<IChronicler>; | ||
setLoggerFacade(loggerFacade: LoggerFacade): void; | ||
} | ||
export interface IEmitterDescription { | ||
emitterProperties: unknown; | ||
type: string; | ||
name: string; | ||
id: string; | ||
description: string; | ||
} | ||
export interface IChroniclerDescription { | ||
chroniclerProperties: unknown; | ||
type: string; | ||
name: string; | ||
id: string; | ||
description: string; | ||
} |
{ | ||
"name": "@curium.rocks/data-emitter-base", | ||
"version": "0.1.1-alpha.21", | ||
"version": "0.1.1-alpha.22", | ||
"description": "A collection of typescript class interfaces and base classes that specify generic contracts with things that emit data", | ||
@@ -5,0 +5,0 @@ "main": "build/src/lib.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
89739
29
1722