diff --git a/src/services/storage/tlv/tlvFilesStorageFactory.ts b/src/services/storage/tlv/tlvFilesStorageFactory.ts index e1a05fcd..8d5ba554 100644 --- a/src/services/storage/tlv/tlvFilesStorageFactory.ts +++ b/src/services/storage/tlv/tlvFilesStorageFactory.ts @@ -1,6 +1,6 @@ import { ChildProcess, fork } from 'child_process'; import { EventEmitter } from 'events'; -import { AddTlvOperation, ITlvStorageOperation, LoadLatestTlvOperation, LoadTlvFileOperation, NewTlvStorageOperation, TlvOperationResponse, TlvStorageSettings } from './tlvFilesStorageProcessor'; +import { AddTlvOperation, ITlvStorageOperation, LoadLatestTlvOperation, LoadTlvFileOperation, NewTlvStorageOperation, SerializableLatestData, SerializableTlvFile, TlvOperationResponse, TlvStorageSettings } from './tlvFilesStorageProcessor'; import { LatestData, TlvFile } from './tlvFilesStorage'; export type TlvStorageInterface = { @@ -56,20 +56,29 @@ export class TlvStorageFactory extends EventEmitter { AddTlv(storageName: string, appId: string, dataName: string, tlv: Uint8Array): Promise { const opId = Math.random().toString() - const op: AddTlvOperation = { type: 'addTlv', opId, storageName, appId, dataName, tlv } + const op: AddTlvOperation = { type: 'addTlv', opId, storageName, appId, dataName, base64Tlv: Buffer.from(tlv).toString('base64') } return this.handleOp(op) } - LoadLatest(storageName: string, limit?: number): Promise { + async LoadLatest(storageName: string, limit?: number): Promise { const opId = Math.random().toString() const op: LoadLatestTlvOperation = { type: 'loadLatestTlv', opId, storageName, limit } - return this.handleOp(op) + const latestData = await this.handleOp(op) + const deserializedLatestData: LatestData = {} + for (const appId in latestData) { + deserializedLatestData[appId] = {} + for (const dataName in latestData[appId]) { + deserializedLatestData[appId][dataName] = { tlvs: latestData[appId][dataName].base64tlvs.map(tlv => new Uint8Array(Buffer.from(tlv, 'base64'))), current_chunk: latestData[appId][dataName].current_chunk, available_chunks: latestData[appId][dataName].available_chunks } + } + } + return deserializedLatestData } - LoadFile(storageName: string, appId: string, dataName: string, chunk: number): Promise { + async LoadFile(storageName: string, appId: string, dataName: string, chunk: number): Promise { const opId = Math.random().toString() const op: LoadTlvFileOperation = { type: 'loadTlvFile', opId, storageName, appId, dataName, chunk } - return this.handleOp(op) + const tlvFile = await this.handleOp(op) + return { fileData: Buffer.from(tlvFile.base64fileData, 'base64'), chunks: tlvFile.chunks } } private handleOp(op: ITlvStorageOperation): Promise { diff --git a/src/services/storage/tlv/tlvFilesStorageProcessor.ts b/src/services/storage/tlv/tlvFilesStorageProcessor.ts index dbb266c2..bef1d3e7 100644 --- a/src/services/storage/tlv/tlvFilesStorageProcessor.ts +++ b/src/services/storage/tlv/tlvFilesStorageProcessor.ts @@ -1,6 +1,7 @@ import { PubLogger, getLogger } from '../../helpers/logger.js'; import { TlvFilesStorage } from './tlvFilesStorage.js'; - +export type SerializableLatestData = Record> +export type SerializableTlvFile = { base64fileData: string, chunks: number[] } export type TlvStorageSettings = { path: string name: string @@ -19,7 +20,7 @@ export type AddTlvOperation = { storageName: string appId: string dataName: string - tlv: Uint8Array + base64Tlv: string debug?: boolean } @@ -73,7 +74,6 @@ class TlvFilesStorageProcessor { private async handleOperation(operation: TlvStorageOperation) { try { const opId = operation.opId; - if (operation.type === 'addTlv') operation.tlv = new Uint8Array(operation.tlv) if (operation.debug) console.log('handleOperation', operation) switch (operation.type) { case 'newStorage': @@ -132,8 +132,9 @@ class TlvFilesStorageProcessor { }) return } - this.storages[operation.storageName].AddTlv(operation.appId, operation.dataName, operation.tlv) - this.sendResponse({ + const tlv = new Uint8Array(Buffer.from(operation.base64Tlv, 'base64')) + this.storages[operation.storageName].AddTlv(operation.appId, operation.dataName, tlv) + this.sendResponse({ success: true, type: 'addTlv', data: null, @@ -151,10 +152,17 @@ class TlvFilesStorageProcessor { return } const data = this.storages[operation.storageName].LoadLatest(operation.limit) - this.sendResponse({ + const serializableData: SerializableLatestData = {} + for (const appId in data) { + serializableData[appId] = {} + for (const dataName in data[appId]) { + serializableData[appId][dataName] = { base64tlvs: data[appId][dataName].tlvs.map(tlv => Buffer.from(tlv).toString('base64')), current_chunk: data[appId][dataName].current_chunk, available_chunks: data[appId][dataName].available_chunks } + } + } + this.sendResponse({ success: true, type: 'loadLatest', - data: data, + data: serializableData, opId: operation.opId }); } @@ -169,15 +177,15 @@ class TlvFilesStorageProcessor { return } const data = this.storages[operation.storageName].LoadFile(operation.appId, operation.dataName, operation.chunk) - this.sendResponse({ + this.sendResponse({ success: true, type: 'loadFile', - data: data, + data: { base64fileData: Buffer.from(data.fileData).toString('base64'), chunks: data.chunks }, opId: operation.opId }); } - private sendResponse(response: TlvOperationResponse) { + private sendResponse(response: TlvOperationResponse) { if (process.send) { process.send(response); }