MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / new

Method new

src/lib/installState.js:17–67  ·  view source on GitHub ↗

* * @param {string} id * @returns

(id)

Source from the content-addressed store, hash-verified

15 * @returns
16 */
17 static async new(id) {
18 try {
19 const state = new InstallState();
20 state.id = await checksumText(id);
21 state.updatedStore = {};
22
23 if (!(await fsOperation(INSTALL_STATE_STORAGE).exists())) {
24 await fsOperation(DATA_STORAGE).createDirectory(".install-state");
25 }
26
27 state.storeUrl = Url.join(INSTALL_STATE_STORAGE, state.id);
28 if (await fsOperation(state.storeUrl).exists()) {
29 let raw = "{}";
30 try {
31 raw = await fsOperation(state.storeUrl).readFile("utf-8");
32 state.store = JSON.parse(raw);
33 } catch (err) {
34 console.error(
35 "InstallState: Failed to parse state file, deleting:",
36 err,
37 );
38 // Delete corrupted state file to avoid parse errors such as 'Unexpected end of JSON'
39 state.store = {};
40 try {
41 await fsOperation(state.storeUrl).delete();
42 // Recreate a fresh empty file to keep invariant
43 await fsOperation(INSTALL_STATE_STORAGE).createFile(state.id);
44 } catch (writeErr) {
45 console.error(
46 "InstallState: Failed to recreate state file:",
47 writeErr,
48 );
49 }
50 }
51
52 const patchedStore = {};
53 for (const [key, value] of Object.entries(state.store)) {
54 patchedStore[key.toLowerCase()] = value;
55 }
56
57 state.store = patchedStore;
58 } else {
59 state.store = {};
60 await fsOperation(INSTALL_STATE_STORAGE).createFile(state.id);
61 }
62
63 return state;
64 } catch (e) {
65 console.error(e);
66 }
67 }
68
69 /**
70 *

Callers 3

uninstallFunction · 0.80
installPluginFunction · 0.80
index.jsFile · 0.80

Calls 8

checksumTextFunction · 0.85
fsOperationFunction · 0.85
existsMethod · 0.65
createDirectoryMethod · 0.65
deleteMethod · 0.65
createFileMethod · 0.65
readFileMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected