Skip to content

Commit d529420

Browse files
emizzleiurimatias
authored andcommitted
fix(@embark/contracts-manager): Remove logger from serialized contract
For all instances where a `Contract` instance is serialized using `JSON.stringify`, the `logger` property was being stringified and written to logs and contract artifact files. Add Serializer class that allows ignoring of class properties during serialization when using `JSON.stringify`. NOTE: The `Serializer` relies on TypeScript’s decorators which are still listed as experimental (requiring the necessary compiler flag) despite being around for several years. Decorators are a stage 2 proposal for JavaScript.
1 parent c7eb274 commit d529420

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

packages/core/utils/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const https = require('follow-redirects').https;
33
const shelljs = require('shelljs');
44
const clipboardy = require('clipboardy');
55

6+
import * as Serialize from './serialize';
7+
export { Serialize };
68
import { canonicalHost } from './host';
79
export { canonicalHost, defaultCorsHost, defaultHost, dockerHostSwap, isDocker } from './host';
810
export { downloadFile, findNextPort, getJson, httpGet, httpsGet, httpGetJson, httpsGetJson, pingEndpoint } from './network';

packages/core/utils/src/serialize.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export function Serializable(target: any) {
2+
target.prototype.toJSON = function() {
3+
const props = Object.getOwnPropertyDescriptors(this);
4+
const map = {};
5+
Object.entries(props).map(([name, prop]) => {
6+
if (Serialization.isIgnored(target.prototype, name)) {
7+
return;
8+
}
9+
map[name] = prop.value;
10+
});
11+
return map;
12+
};
13+
}
14+
15+
export function Ignore(target: any, propertyKey: string) {
16+
Serialization.registerIgnore(target, propertyKey);
17+
}
18+
19+
class Serialization {
20+
private static ignoreMap: Map<any, string[]> = new Map();
21+
static registerIgnore(target: any, property: any): void {
22+
let keys = this.ignoreMap.get(target);
23+
if (!keys) {
24+
keys = [];
25+
this.ignoreMap.set(target, keys);
26+
}
27+
keys.push(property);
28+
}
29+
30+
static isIgnored(target: any, property: any): boolean {
31+
const keys = this.ignoreMap.get(target);
32+
if (!keys) {
33+
return false;
34+
}
35+
return keys.includes(property);
36+
}
37+
}

packages/stack/contracts-manager/src/contract.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { ContractConfig } from "embark-core";
22
import { Logger } from 'embark-logger';
3-
import { sha3 } from "embark-utils";
3+
import { sha3, Serialize } from "embark-utils";
44
import { AbiItem } from "web3-utils";
55

6+
@Serialize.Serializable
67
export default class Contract {
8+
@Serialize.Ignore
79
private logger: Logger;
810
public abiDefinition?: AbiItem[];
911
public deployedAddress?: string;

tsconfig.base.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"declarationMap": true,
77
"emitDeclarationOnly": true,
88
"esModuleInterop": true,
9+
"experimentalDecorators": true,
910
"isolatedModules": true,
1011
"moduleResolution": "Node",
1112
"noImplicitAny": false,
@@ -14,4 +15,4 @@
1415
"strict": true,
1516
"target": "ESNext"
1617
}
17-
}
18+
}

0 commit comments

Comments
 (0)