@curium.rocks/data-emitter-base
Advanced tools
Comparing version 0.1.1-alpha.31 to 0.1.1-alpha.32
@@ -116,15 +116,2 @@ import { ICommand, IDataEmitter, IDataEvent, IDataEventListener, IDataEventListenerFunc, IDisposable, IEmitterDescription, IExecutionResult, IFormatSettings, ISettings, IStatusChangeListener, IStatusChangeListenerFunc, IStatusEvent, ITraceableAction } from "./dataEmitter"; | ||
/** | ||
* | ||
* @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 | ||
@@ -131,0 +118,0 @@ * @return {Record<string, unknown>} |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -9,4 +6,4 @@ exports.BaseEmitter = exports.BaseDataEvent = exports.BaseStatusEvent = void 0; | ||
const loggerFacade_1 = require("./loggerFacade"); | ||
const crypto_1 = __importDefault(require("crypto")); | ||
const provider_1 = require("./provider"); | ||
const common_1 = require("./common"); | ||
/** | ||
@@ -145,49 +142,6 @@ * | ||
else { | ||
return BaseEmitter.encrypt(JSON.stringify(this.getEmitterDescription()), settings); | ||
return common_1.encrypt(JSON.stringify(this.getEmitterDescription()), settings); | ||
} | ||
} | ||
/** | ||
* | ||
* @param {string} json | ||
* @param {IFormatSettings} settings | ||
*/ | ||
static async encrypt(json, settings) { | ||
if (settings.algorithm.toLowerCase().indexOf('gcm') != -1) { | ||
const gcmCipher = crypto_1.default.createCipheriv(settings.algorithm, Buffer.from(settings.key, 'base64'), Buffer.from(settings.iv, 'base64'), { | ||
authTagLength: 16 | ||
}); | ||
const cipherBuffer = Buffer.concat([gcmCipher.update(json, 'utf-8'), gcmCipher.final(), gcmCipher.getAuthTag()]); | ||
return Promise.resolve(cipherBuffer.toString('base64')); | ||
} | ||
else { | ||
const cipher = crypto_1.default.createCipheriv(settings.algorithm, Buffer.from(settings.key, 'base64'), Buffer.from(settings.iv, 'base64')); | ||
const cipherBuffer = Buffer.concat([cipher.update(json, 'utf-8'), cipher.final()]); | ||
return Promise.resolve(cipherBuffer.toString('base64')); | ||
} | ||
} | ||
/** | ||
* | ||
* @param {string} base64CipherText | ||
* @param {IFormatSettings} settings | ||
* @return {Promise<void>} | ||
*/ | ||
static async decrypt(base64CipherText, settings) { | ||
if (settings.algorithm.toLowerCase().indexOf('gcm') != -1) { | ||
const gcmCipher = crypto_1.default.createDecipheriv(settings.algorithm, Buffer.from(settings.key, 'base64'), Buffer.from(settings.iv, 'base64')); | ||
const buffer = Buffer.from(base64CipherText, 'base64'); | ||
const authTag = buffer.slice(buffer.length - 16); | ||
const cipher = buffer.slice(0, buffer.length - 16); | ||
gcmCipher.setAuthTag(authTag); | ||
let plainText = gcmCipher.update(cipher, undefined, 'utf-8'); | ||
plainText += gcmCipher.final('utf-8'); | ||
return Promise.resolve(plainText); | ||
} | ||
else { | ||
const decipher = crypto_1.default.createDecipheriv(settings.algorithm, Buffer.from(settings.key, 'base64'), Buffer.from(settings.iv, 'base64')); | ||
let plainText = decipher.update(base64CipherText, 'base64', 'utf-8'); | ||
plainText += decipher.final('utf-8'); | ||
return Promise.resolve(plainText); | ||
} | ||
} | ||
/** | ||
* Control serialized properties when JSON.stringify is called | ||
@@ -463,3 +417,3 @@ * @return {Record<string, unknown>} | ||
if (settings.encrypted) { | ||
description = await BaseEmitter.decrypt(stateData, settings); | ||
description = await common_1.decrypt(stateData, settings); | ||
} | ||
@@ -466,0 +420,0 @@ else { |
@@ -1,2 +0,3 @@ | ||
import { IClassifier, IDisposable } from "./dataEmitter"; | ||
import { IClassifier, IDisposable, IFormatSettings } from "./dataEmitter"; | ||
import { IChroniclerDescription, ISerializableState } from "./lib"; | ||
/** | ||
@@ -18,3 +19,3 @@ * Enforce the object providing controlled | ||
*/ | ||
export interface IChronicler extends IDisposable, IClassifier { | ||
export interface IChronicler extends IDisposable, IClassifier, ISerializableState { | ||
/** | ||
@@ -55,1 +56,55 @@ * Save the provided object into a persistent store, | ||
} | ||
/** | ||
* Base chronicler | ||
*/ | ||
export declare abstract class BaseChronicler implements IChronicler { | ||
protected _id: string; | ||
protected _name: string; | ||
protected _description: string; | ||
/** | ||
* Unique id of the chronicler | ||
*/ | ||
get id(): string; | ||
/** | ||
* Name of the chronicler | ||
*/ | ||
get name(): string; | ||
/** | ||
* Description of the chronicler | ||
*/ | ||
get description(): string; | ||
/** | ||
* | ||
* @param {IChroniclerDescription} desc | ||
*/ | ||
constructor(desc: IChroniclerDescription); | ||
/** | ||
* | ||
* @param {IJsonSerializable} record | ||
* @return {Promise<void>} | ||
*/ | ||
abstract saveRecord(record: IJsonSerializable): Promise<void>; | ||
/** | ||
* Get the chronicler properties for this instance | ||
*/ | ||
abstract getChroniclerProperties(): unknown; | ||
/** | ||
* Get the chronicler type for this instance | ||
*/ | ||
abstract getType(): string; | ||
/** | ||
* Dispose any resources that will not automatically be cleaned up | ||
*/ | ||
abstract dispose(): void; | ||
/** | ||
* Gets the chronicler description which is used to recreate the chronicler | ||
* @return {IChroniclerDescription} | ||
*/ | ||
protected getChronicerDescription(): IChroniclerDescription; | ||
/** | ||
* | ||
* @param {IFormatSettings} settings | ||
* @return {Promise<string>} | ||
*/ | ||
serializeState(settings: IFormatSettings): Promise<string>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BaseChronicler = void 0; | ||
const common_1 = require("./common"); | ||
/** | ||
* Base chronicler | ||
*/ | ||
class BaseChronicler { | ||
/** | ||
* | ||
* @param {IChroniclerDescription} desc | ||
*/ | ||
constructor(desc) { | ||
this._id = desc.id; | ||
this._name = desc.name; | ||
this._description = desc.description; | ||
} | ||
/** | ||
* Unique id of the chronicler | ||
*/ | ||
get id() { | ||
return this._id; | ||
} | ||
/** | ||
* Name of the chronicler | ||
*/ | ||
get name() { | ||
return this._name; | ||
} | ||
/** | ||
* Description of the chronicler | ||
*/ | ||
get description() { | ||
return this._description; | ||
} | ||
/** | ||
* Gets the chronicler description which is used to recreate the chronicler | ||
* @return {IChroniclerDescription} | ||
*/ | ||
getChronicerDescription() { | ||
return { | ||
name: this.name, | ||
description: this.description, | ||
id: this.id, | ||
type: this.getType(), | ||
chroniclerProperties: this.getChroniclerProperties() | ||
}; | ||
} | ||
/** | ||
* | ||
* @param {IFormatSettings} settings | ||
* @return {Promise<string>} | ||
*/ | ||
async serializeState(settings) { | ||
if (settings.encrypted) { | ||
return Promise.resolve(await common_1.encrypt(JSON.stringify(this.getChronicerDescription()), settings)); | ||
} | ||
return Promise.resolve(JSON.stringify(this.getChronicerDescription())); | ||
} | ||
} | ||
exports.BaseChronicler = BaseChronicler; | ||
//# sourceMappingURL=chronicler.js.map |
import { IChronicler, IJsonSerializable } from "./chronicler"; | ||
import { ISerializableState } from "./common"; | ||
import { LoggerFacade } from "./loggerFacade"; | ||
@@ -177,3 +178,3 @@ /** | ||
*/ | ||
export interface IDataEmitter extends IJsonSerializable, IClassifier { | ||
export interface IDataEmitter extends IJsonSerializable, IClassifier, ISerializableState { | ||
/** | ||
@@ -207,14 +208,31 @@ * Register a data listener that will receive events on new data | ||
probeCurrentData(): Promise<IDataEvent>; | ||
} | ||
export interface IFormatSettings { | ||
/** | ||
* Serialize the state of the emitter in a base64 string | ||
* Whether the state data is encrypted or not | ||
*/ | ||
serializeState(settings: IFormatSettings): Promise<string>; | ||
} | ||
export interface IFormatSettings { | ||
encrypted: boolean; | ||
/** | ||
* Meta data about the encrypted data, such as an emitter, chronicler type | ||
*/ | ||
type: string; | ||
/** | ||
* The algorithm of encryption, such as aes-256-gcm | ||
*/ | ||
algorithm?: string; | ||
/** | ||
* Initialization vector to use if available | ||
*/ | ||
iv?: string; | ||
/** | ||
* Tag to use if available/applicable | ||
*/ | ||
tag?: string; | ||
/** | ||
* key to use | ||
*/ | ||
key?: string; | ||
/** | ||
* When using a key store, the name of the key to use | ||
*/ | ||
keyName?: string; | ||
@@ -254,2 +272,3 @@ } | ||
buildChronicler(description: IChroniclerDescription): Promise<IChronicler>; | ||
recreateChronicler(base64StateData: string, formatSettings: IFormatSettings): Promise<IChronicler>; | ||
setLoggerFacade(loggerFacade: LoggerFacade): void; | ||
@@ -256,0 +275,0 @@ } |
@@ -27,7 +27,7 @@ import { IChronicler } from "./chronicler"; | ||
* | ||
* @param {string} base64StateData | ||
* @param {string} stateData either base64 encoded ciphertext if encrypted, or a json string | ||
* @param {IFormatSettings} formatSettings | ||
* @return {Promise<IDataEmitter>} | ||
*/ | ||
recreateEmitter(base64StateData: string, formatSettings: IFormatSettings): Promise<IDataEmitter>; | ||
recreateEmitter(stateData: string, formatSettings: IFormatSettings): Promise<IDataEmitter>; | ||
} | ||
@@ -43,2 +43,9 @@ /** | ||
abstract buildChronicler(description: IChroniclerDescription): Promise<IChronicler>; | ||
/** | ||
* | ||
* @param {string} stateData either base64 encoded ciphertext if encrypted, or a json string | ||
* @param {IFormatSettings} formatSettings | ||
* @return {Promise<IChronicler>} | ||
*/ | ||
recreateChronicler(stateData: string, formatSettings: IFormatSettings): Promise<IChronicler>; | ||
} |
@@ -5,2 +5,3 @@ "use strict"; | ||
const baseEmitter_1 = require("./baseEmitter"); | ||
const common_1 = require("./common"); | ||
/** | ||
@@ -25,8 +26,8 @@ * | ||
* | ||
* @param {string} base64StateData | ||
* @param {string} stateData either base64 encoded ciphertext if encrypted, or a json string | ||
* @param {IFormatSettings} formatSettings | ||
* @return {Promise<IDataEmitter>} | ||
*/ | ||
recreateEmitter(base64StateData, formatSettings) { | ||
return baseEmitter_1.BaseEmitter.recreateEmitter(base64StateData, formatSettings); | ||
recreateEmitter(stateData, formatSettings) { | ||
return baseEmitter_1.BaseEmitter.recreateEmitter(stateData, formatSettings); | ||
} | ||
@@ -39,4 +40,21 @@ } | ||
class BaseChroniclerFactory extends BaseFactory { | ||
/** | ||
* | ||
* @param {string} stateData either base64 encoded ciphertext if encrypted, or a json string | ||
* @param {IFormatSettings} formatSettings | ||
* @return {Promise<IChronicler>} | ||
*/ | ||
async recreateChronicler(stateData, formatSettings) { | ||
let jsonStr; | ||
if (formatSettings.encrypted) { | ||
jsonStr = await common_1.decrypt(stateData, formatSettings); | ||
} | ||
else { | ||
jsonStr = stateData; | ||
} | ||
const description = JSON.parse(jsonStr); | ||
return this.buildChronicler(description); | ||
} | ||
} | ||
exports.BaseChroniclerFactory = BaseChroniclerFactory; | ||
//# sourceMappingURL=factory.js.map |
@@ -9,1 +9,2 @@ export { IDataEmitter, ICompoundDataEmitter, ICommand, IDataEvent, IExecutionResult, ISettings, ITraceableAction, IEmitterFactory, IDisposableAsync, IDisposable, IClassifier, IDataEventListener, IDataEventListenerFunc, IStatusChangeListener, IStatusChangeListenerFunc, IStatusEvent, IEmitterProvider, IChroniclerFactory, IChroniclerProvider, IEmitterDescription, IChroniclerDescription, IFormatSettings, isJsonSerializable, isDisposable, isDisposableAsync, hasMethod } from './dataEmitter'; | ||
export { BaseFactory, BaseEmitterFactory, BaseChroniclerFactory } from './factory'; | ||
export { ISerializableState } from './common'; |
@@ -34,2 +34,10 @@ import { IChronicler } from "./chronicler"; | ||
* | ||
* @param {string} stateData if encrypted, base64 encoded cipher text, otherwise a json string | ||
* @param {IFormatSettings} formatSettings the format settings describing the state data is encrypted | ||
* and if so what cipher type etc. | ||
* @return {IChronicler} | ||
*/ | ||
recreateChronicler(stateData: string, formatSettings: IFormatSettings): Promise<IChronicler>; | ||
/** | ||
* | ||
* @param {string} type | ||
@@ -36,0 +44,0 @@ * @param {IChroniclerFactory} factory |
@@ -56,2 +56,19 @@ "use strict"; | ||
* | ||
* @param {string} stateData if encrypted, base64 encoded cipher text, otherwise a json string | ||
* @param {IFormatSettings} formatSettings the format settings describing the state data is encrypted | ||
* and if so what cipher type etc. | ||
* @return {IChronicler} | ||
*/ | ||
recreateChronicler(stateData, formatSettings) { | ||
const key = formatSettings.type.toLowerCase(); | ||
const factory = this._chroncilerFactories.get(key); | ||
if (factory != null) { | ||
return Promise.resolve(factory.recreateChronicler(stateData, formatSettings)); | ||
} | ||
else { | ||
return Promise.reject(new Error(`No chronicler factory found for ${key}`)); | ||
} | ||
} | ||
/** | ||
* | ||
* @param {string} type | ||
@@ -58,0 +75,0 @@ * @param {IChroniclerFactory} factory |
{ | ||
"name": "@curium.rocks/data-emitter-base", | ||
"version": "0.1.1-alpha.31", | ||
"version": "0.1.1-alpha.32", | ||
"description": "A collection of typescript class interfaces and base classes that specify generic contracts with things that emit data", | ||
@@ -48,6 +48,11 @@ "main": "build/src/lib.js", | ||
"exclude": [ | ||
"**/*.d.ts" | ||
".eslintrc.js", | ||
"docs/**", | ||
"coverage/**", | ||
"**/*.d.ts", | ||
"test/**" | ||
], | ||
"reporter": [ | ||
"lcov" | ||
"lcov", | ||
"text" | ||
], | ||
@@ -54,0 +59,0 @@ "all": true |
# data-emitter-base | ||
[](https://sonarcloud.io/dashboard?id=curium-rocks_data-emitter-base)[](https://sonarcloud.io/dashboard?id=curium-rocks_data-emitter-base) | ||
[](https://sonarcloud.io/dashboard?id=curium-rocks_data-emitter-base) [](https://sonarcloud.io/dashboard?id=curium-rocks_data-emitter-base)  | ||
@@ -4,0 +4,0 @@ Contains a set of base classes and interfaces to minimize |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
95681
33
2091