Skip to content

Commit 79cf867

Browse files
fix. infinite loading when installing plugin
1 parent bd0141a commit 79cf867

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

src/fileSystem/internalFs.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ const internalFs = {
3232
mtime: file.mtime,
3333
isFile: file.type === "file",
3434
isDirectory: file.type === "directory",
35-
// TODO: Link(symlink/hardlink) file detection if possible.
36-
//This function sometimes gets called even before the NativeLayer is registred so calling isSymlink is not possible
3735
isLink: false,
3836
})),
3937
);

src/lib/installPlugin.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ export default async function installPlugin(
6565
pluginUrl = id;
6666
}
6767

68-
console.log(`Plugin Url ${pluginUrl}`);
69-
7068
try {
7169
if (!isDependency) loaderDialog.show();
7270

71+
console.log("installing ...");
72+
7373
let plugin;
7474
if (
7575
pluginUrl.includes(constants.API_BASE) ||
@@ -97,9 +97,11 @@ export default async function installPlugin(
9797
(response) => {
9898
resolve(response.data);
9999
loaderDialog.setMessage(`${strings.loading} 100%`);
100+
console.log("Download complete");
100101
},
101102
(error) => {
102103
reject(error);
104+
console.log("Download failed");
103105
},
104106
);
105107
});
@@ -108,15 +110,19 @@ export default async function installPlugin(
108110
if (plugin) {
109111
const zip = new JSZip();
110112
await zip.loadAsync(plugin);
113+
console.log("Plugin zip loaded into memory");
111114

112115
if (!zip.files["plugin.json"]) {
113116
throw new Error(strings["invalid plugin"]);
114117
}
115118

119+
console.log("Plugin Json start");
120+
const jsonStr = await zip.files["plugin.json"].async("text");
121+
console.log(jsonStr);
122+
console.log("Plugin json end");
123+
116124
/** @type {{ dependencies: string[] }} */
117-
const pluginJson = JSON.parse(
118-
await zip.files["plugin.json"].async("text"),
119-
);
125+
const pluginJson = JSON.parse(jsonStr);
120126

121127
/** patch main in manifest */
122128
if (!zip.files[pluginJson.main]) {
@@ -171,20 +177,27 @@ export default async function installPlugin(
171177
pluginDir = Url.join(PLUGIN_DIR, id);
172178
}
173179

180+
console.log("Begin Install state");
174181
state = await InstallState.new(id);
182+
console.log("Install state end");
175183

176184
if (!(await fsOperation(pluginDir).exists())) {
177185
await fsOperation(PLUGIN_DIR).createDirectory(id);
178186
}
179187

188+
console.log(zip);
189+
180190
const promises = Object.keys(zip.files).map(async (file) => {
181191
try {
182192
let correctFile = file;
193+
183194
if (/\\/.test(correctFile)) {
184195
correctFile = correctFile.replace(/\\/g, "/");
185196
}
186197

198+
console.log(`Correct file ${correctFile}`);
187199
const fileUrl = Url.join(pluginDir, correctFile);
200+
console.log("file Url " + fileUrl);
188201

189202
if (!state.exists(correctFile)) {
190203
await createFileRecursive(pluginDir, correctFile);
@@ -200,7 +213,9 @@ export default async function installPlugin(
200213
}
201214

202215
if (!(await state.isUpdated(correctFile, data))) return;
216+
console.log("writing file");
203217
await fsOperation(fileUrl).writeFile(data);
218+
console.log("file written");
204219
return;
205220
} catch (error) {
206221
console.error(`Error processing file ${file}:`, error);
@@ -210,17 +225,27 @@ export default async function installPlugin(
210225
// Wait for all files to be processed
211226
await Promise.allSettled(promises);
212227

228+
console.log("done");
229+
213230
if (isDependency) {
214231
depsLoaders.push(async () => {
215232
await loadPlugin(id, true);
216233
});
217234
} else {
235+
console.log("loaders");
218236
for (const loader of depsLoaders) {
237+
console.log("loading loader");
238+
console.log(loader);
219239
await loader();
220240
}
241+
console.log("loader loading done");
242+
console.log("loading plugin");
221243
await loadPlugin(id, true);
244+
console.log("loading plugin done");
222245
}
223246

247+
console.log("successfully loaded");
248+
224249
await state.save();
225250
deleteRedundantFiles(pluginDir, state);
226251
}

src/lib/installState.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ export default class InstallState {
2525
}
2626

2727
state.storeUrl = Url.join(INSTALL_STATE_STORAGE, state.id);
28-
if (await fsOperation(state.storeUrl).exists()) {
28+
29+
console.log("store url");
30+
console.log(state.storeUrl);
31+
32+
if ((await fsOperation(state.storeUrl).exists()) && false) {
2933
state.store = JSON.parse(
3034
await fsOperation(state.storeUrl).readFile("utf-8"),
3135
);
@@ -41,6 +45,10 @@ export default class InstallState {
4145
await fsOperation(INSTALL_STATE_STORAGE).createFile(state.id);
4246
}
4347

48+
await fsOperation(state.storeUrl).delete();
49+
console.log("returned state");
50+
console.log(state);
51+
4452
return state;
4553
} catch (e) {
4654
console.error(e);

src/lib/loadPlugin.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,45 @@ export default async function loadPlugin(pluginId, justInstalled = false) {
2525
mainUrl = Url.join(baseUrl, "main.js");
2626
}
2727

28-
return new Promise((resolve, reject) => {
28+
console.log(`main url ${mainUrl}`);
29+
30+
return new Promise(async (resolve, reject) => {
2931
if (pluginId === undefined) {
3032
console.error("Skipping loading plugin with undefined id");
3133
reject("Skipping loading plugin with undefined id");
3234
return;
3335
}
3436

35-
const data = internalFs.readStringFile(mainUrl).data;
37+
const result = await internalFs.readStringFile(mainUrl);
38+
39+
console.log(`result ${result}`);
40+
41+
const data = result.data;
42+
43+
console.log(`data ${data}`);
44+
45+
const blob = new Blob([data], { type: "text/javascript" });
46+
const url = URL.createObjectURL(blob);
3647

37-
const $script = <script dangerouslySetInnerHTML={{ __html: data }} />;
48+
const $script = document.createElement("script");
49+
$script.src = url;
50+
$script.type = "text/javascript";
51+
52+
console.log("script created");
3853

3954
$script.onerror = (error) => {
55+
URL.revokeObjectURL(url);
4056
reject(
4157
new Error(
4258
`Failed to load script for plugin ${pluginId}: ${error.message || error}`,
4359
),
4460
);
4561
};
4662

63+
console.log("on error registered");
64+
4765
$script.onload = async () => {
66+
URL.revokeObjectURL(url);
4867
const $page = Page("Plugin");
4968
$page.show = () => {
5069
actionStack.push({
@@ -60,6 +79,7 @@ export default async function loadPlugin(pluginId, justInstalled = false) {
6079
};
6180

6281
try {
82+
console.log("trying");
6383
if (!(await fsOperation(cacheFile).exists())) {
6484
await fsOperation(CACHE_STORAGE).createFile(pluginId);
6585
}
@@ -70,12 +90,18 @@ export default async function loadPlugin(pluginId, justInstalled = false) {
7090
firstInit: justInstalled,
7191
});
7292

93+
console.log("done xx");
94+
7395
resolve();
7496
} catch (error) {
7597
reject(error);
7698
}
7799
};
78100

101+
console.log("on load registred");
102+
103+
console.log("attaching script");
79104
document.head.append($script);
105+
console.log("script attached");
80106
});
81107
}

0 commit comments

Comments
 (0)