Skip to content

Commit 9ed0e43

Browse files
committed
fix: better manage StoreAdapterNode
1 parent 9b14843 commit 9ed0e43

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

  • packages/sdk-multichain/src/store/adapters

packages/sdk-multichain/src/store/adapters/node.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,33 @@ const CONFIG_FILE = path.resolve(process.cwd(), '.metamask.json');
66

77
export class StoreAdapterNode extends StoreAdapter {
88

9+
10+
private safeParse(contents: string): Record<string, string> {
11+
try {
12+
return JSON.parse(contents);
13+
} catch (e) {
14+
return {};
15+
}
16+
}
17+
918
async getItem(key: string): Promise<string | null> {
1019
if (!fs.existsSync(CONFIG_FILE)) {
1120
return null;
1221
}
1322
const contents = fs.readFileSync(CONFIG_FILE, 'utf8');
14-
const config = JSON.parse(contents);
15-
return config[key] || null;
23+
const config = this.safeParse(contents);
24+
if (config[key] !== undefined) {
25+
return config[key];
26+
}
27+
return null;
1628
}
1729

1830
async setItem(key: string, value: string): Promise<void> {
1931
if (!fs.existsSync(CONFIG_FILE)) {
2032
fs.writeFileSync(CONFIG_FILE, '{}');
2133
}
2234
const contents = fs.readFileSync(CONFIG_FILE, 'utf8');
23-
const config = JSON.parse(contents);
35+
const config = this.safeParse(contents);
2436
config[key] = value;
2537
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
2638
}
@@ -30,7 +42,7 @@ export class StoreAdapterNode extends StoreAdapter {
3042
return;
3143
}
3244
const contents = fs.readFileSync(CONFIG_FILE, 'utf8');
33-
const config = JSON.parse(contents);
45+
const config = this.safeParse(contents);
3446
delete config[key];
3547
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
3648
}

0 commit comments

Comments
 (0)