diff --git a/src/services/storage/db/serializationHelpers.ts b/src/services/storage/db/serializationHelpers.ts index 446d1878..95b6c0f6 100644 --- a/src/services/storage/db/serializationHelpers.ts +++ b/src/services/storage/db/serializationHelpers.ts @@ -84,4 +84,49 @@ export function deserializeRequest(r: object): T { result[key] = deserializeRequest(value); } return result; -} \ No newline at end of file +} + +export function serializeResponseData(r: T) { + // flag Date objects + function replaceDates(val: any): any { + if (val instanceof Date) { + return { __type: 'Date', value: val.toISOString() }; + } else if (Array.isArray(val)) { + return val.map(replaceDates); + } else if (val && typeof val === 'object') { + const result: any = {}; + for (const key in val) { + result[key] = replaceDates(val[key]); + } + return result; + } + return val; + } + + return replaceDates(r); +} + +export function deserializeResponseData(r: T) { + + // reconstruct flagged Date objects + function reviveFlaggedDates(value: any): any { + if ( + value && + typeof value === 'object' && + value.__type === 'Date' && + typeof value.value === 'string' + ) { + return new Date(value.value); + } else if (Array.isArray(value)) { + return value.map(reviveFlaggedDates); + } else if (value && typeof value === 'object') { + for (const key in value) { + value[key] = reviveFlaggedDates(value[key]); + } + } + return value; + } + + return reviveFlaggedDates(r); +} + diff --git a/src/services/storage/db/storageInterface.ts b/src/services/storage/db/storageInterface.ts index 81da2270..0169efa9 100644 --- a/src/services/storage/db/storageInterface.ts +++ b/src/services/storage/db/storageInterface.ts @@ -15,7 +15,7 @@ import { PingOperation, } from './storageProcessor.js'; import { PickKeysByType } from 'typeorm/common/PickKeysByType.js'; -import { serializeRequest, WhereCondition } from './serializationHelpers.js'; +import { deserializeResponseData, serializeRequest, WhereCondition } from './serializationHelpers.js'; import { Utils } from '../../helpers/utilsWrapper.js'; import { ProcessMetrics } from '../tlv/processMetricsCollector.js'; @@ -174,7 +174,7 @@ export class StorageInterface extends EventEmitter { reject(new Error('Invalid storage response type')); return } - resolve(response.data); + resolve(deserializeResponseData(response.data)); } this.once(op.opId, responseHandler) this.process.send(this.serializeOperation(op)) diff --git a/src/services/storage/db/storageProcessor.ts b/src/services/storage/db/storageProcessor.ts index 05940629..449277b1 100644 --- a/src/services/storage/db/storageProcessor.ts +++ b/src/services/storage/db/storageProcessor.ts @@ -4,7 +4,7 @@ import { PubLogger, getLogger } from '../../helpers/logger.js'; import { allMetricsMigrations, allMigrations } from '../migrations/runner.js'; import transactionsQueue from './transactionsQueue.js'; import { PickKeysByType } from 'typeorm/common/PickKeysByType'; -import { deserializeRequest, WhereCondition } from './serializationHelpers.js'; +import { deserializeRequest, serializeResponseData, WhereCondition } from './serializationHelpers.js'; import { ProcessMetricsCollector } from '../tlv/processMetricsCollector.js'; export type DBNames = MainDbNames | MetricsDbNames @@ -510,9 +510,15 @@ class StorageProcessor { } private sendResponse(response: OperationResponse) { + let finalResponse = response + if (finalResponse.success && finalResponse.data) { + const datesFlaggedData = serializeResponseData(finalResponse.data) + finalResponse = { ...finalResponse, data: datesFlaggedData } + } + try { if (process.send) { - process.send(response, undefined, undefined, err => { + process.send(finalResponse, undefined, undefined, err => { if (err) { console.error("failed to send response to main process from storage processor, killing sub process") process.exit(1)