Merge pull request #824 from snake-eaterr/fix/date-objects-ipc
Fix: Date objects serializing/deserializing in IPC response from stor…
This commit is contained in:
commit
0031bf0b3c
3 changed files with 56 additions and 5 deletions
|
|
@ -85,3 +85,48 @@ export function deserializeRequest<T>(r: object): T {
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function serializeResponseData<T>(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<T>(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ import {
|
||||||
PingOperation,
|
PingOperation,
|
||||||
} from './storageProcessor.js';
|
} from './storageProcessor.js';
|
||||||
import { PickKeysByType } from 'typeorm/common/PickKeysByType.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 { Utils } from '../../helpers/utilsWrapper.js';
|
||||||
import { ProcessMetrics } from '../tlv/processMetricsCollector.js';
|
import { ProcessMetrics } from '../tlv/processMetricsCollector.js';
|
||||||
|
|
||||||
|
|
@ -174,7 +174,7 @@ export class StorageInterface extends EventEmitter {
|
||||||
reject(new Error('Invalid storage response type'));
|
reject(new Error('Invalid storage response type'));
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resolve(response.data);
|
resolve(deserializeResponseData(response.data));
|
||||||
}
|
}
|
||||||
this.once(op.opId, responseHandler)
|
this.once(op.opId, responseHandler)
|
||||||
this.process.send(this.serializeOperation(op))
|
this.process.send(this.serializeOperation(op))
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { PubLogger, getLogger } from '../../helpers/logger.js';
|
||||||
import { allMetricsMigrations, allMigrations } from '../migrations/runner.js';
|
import { allMetricsMigrations, allMigrations } from '../migrations/runner.js';
|
||||||
import transactionsQueue from './transactionsQueue.js';
|
import transactionsQueue from './transactionsQueue.js';
|
||||||
import { PickKeysByType } from 'typeorm/common/PickKeysByType';
|
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';
|
import { ProcessMetricsCollector } from '../tlv/processMetricsCollector.js';
|
||||||
|
|
||||||
export type DBNames = MainDbNames | MetricsDbNames
|
export type DBNames = MainDbNames | MetricsDbNames
|
||||||
|
|
@ -510,9 +510,15 @@ class StorageProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private sendResponse(response: OperationResponse<any>) {
|
private sendResponse(response: OperationResponse<any>) {
|
||||||
|
let finalResponse = response
|
||||||
|
if (finalResponse.success && finalResponse.data) {
|
||||||
|
const datesFlaggedData = serializeResponseData(finalResponse.data)
|
||||||
|
finalResponse = { ...finalResponse, data: datesFlaggedData }
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (process.send) {
|
if (process.send) {
|
||||||
process.send(response, undefined, undefined, err => {
|
process.send(finalResponse, undefined, undefined, err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error("failed to send response to main process from storage processor, killing sub process")
|
console.error("failed to send response to main process from storage processor, killing sub process")
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue