Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export function Serializable(target: any) { | ||
target.prototype.toJSON = function() { | ||
const props = Object.getOwnPropertyDescriptors(this); | ||
const map = {}; | ||
Object.entries(props).map(([name, prop]) => { | ||
if (Serialization.isIgnored(target.prototype, name)) { | ||
return; | ||
} | ||
map[name] = prop.value; | ||
}); | ||
return map; | ||
}; | ||
} | ||
|
||
export function Ignore(target: any, propertyKey: string) { | ||
Serialization.registerIgnore(target, propertyKey); | ||
} | ||
|
||
class Serialization { | ||
private static ignoreMap: Map<any, string[]> = new Map(); | ||
static registerIgnore(target: any, property: any): void { | ||
let keys = this.ignoreMap.get(target); | ||
if (!keys) { | ||
keys = []; | ||
this.ignoreMap.set(target, keys); | ||
} | ||
keys.push(property); | ||
} | ||
|
||
static isIgnored(target: any, property: any): boolean { | ||
const keys = this.ignoreMap.get(target); | ||
if (!keys) { | ||
return false; | ||
} | ||
return keys.includes(property); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters