Skip to content

Commit

Permalink
fix(transaction-logger): fix circular dep issue with util.inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
jrainville authored and iurimatias committed Jan 16, 2020
1 parent b607763 commit 6f239f4
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions packages/plugins/transaction-logger/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ const LISTENED_METHODS = [
blockchainConstants.transactionMethods.eth_sendRawTransaction
];

const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};

export default class TransactionLogger {
constructor(embark, _options) {
this.embark = embark;
Expand Down Expand Up @@ -50,7 +63,7 @@ export default class TransactionLogger {
this.writeLogFile = async.cargo((tasks, callback) => {
let appendThis = '';
tasks.forEach(task => {
appendThis += `"${new Date().getTime()}":${JSON.stringify(task)},\n`;
appendThis += `"${new Date().getTime()}":${JSON.stringify(task, getCircularReplacer())},\n`;
});
this.fs.appendFile(this.logFile, appendThis, (err) => {
if (err) {
Expand Down Expand Up @@ -262,28 +275,24 @@ export default class TransactionLogger {
}

async _readLogs() {
return new Promise(async resolve => {
try {
await this.fs.ensureFile(this.logFile);
this.fs.readFile(this.logFile, (err, data) => {
if (err) {
throw (new Error(err));
}
data = data.toString();
if (!data) {
return resolve({});
}

// remove last comma and add braces around
data = `{${data.substring(0, data.length - 2)}}`;

resolve(JSON.parse(data));
});
} catch (error) {
this.logger.error('Error reading contract log file', error.message);
this.logger.trace(error);
resolve({});
try {
await this.fs.ensureFile(this.logFile);
let data = await this.fs.readFile(this.logFile);

data = data.toString();

if (!data) {
return {};
}
});

// remove last comma and add braces around
data = `{${data.substring(0, data.length - 2)}}`;

return JSON.parse(data);
} catch (error) {
this.logger.error('Error reading contract log file', error.message);
this.logger.trace(error.trace);
return {};
}
}
}

0 comments on commit 6f239f4

Please sign in to comment.