aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOmar Rizwan <omar@omar.website>2020-12-29 04:27:12 -0800
committerOmar Rizwan <omar@omar.website>2020-12-29 04:27:12 -0800
commit8315efcb604fcf6002af99f7dd4303e2ac149047 (patch)
treea284f76c9c60fec4712125ba3c0e0cd0f5af10b8
parent705b245be5ea7e69f04c1ab370962de054800bb0 (diff)
improve scripts dir -- checking scriptId more reliably. add tests
-rw-r--r--extension/background.js27
-rw-r--r--test/test.c10
2 files changed, 28 insertions, 9 deletions
diff --git a/extension/background.js b/extension/background.js
index 51b4043..5cb3a8c 100644
--- a/extension/background.js
+++ b/extension/background.js
@@ -58,11 +58,11 @@ const TabManager = (function() {
console.log(source, method, params);
if (method === "Page.frameStartedLoading") {
// we're gonna assume we're always plugged into both Page and Debugger.
- TabManager.scriptsForTab[source.tabId] = [];
+ TabManager.scriptsForTab[source.tabId] = {};
} else if (method === "Debugger.scriptParsed") {
- TabManager.scriptsForTab[source.tabId] = TabManager.scriptsForTab[source.tabId] || [];
- TabManager.scriptsForTab[source.tabId].push(params);
+ TabManager.scriptsForTab[source.tabId] = TabManager.scriptsForTab[source.tabId] || {};
+ TabManager.scriptsForTab[source.tabId][params.scriptId] = params;
}
});
@@ -82,7 +82,7 @@ const TabManager = (function() {
},
enableDomainForTab: async function(tabId, domain) {
// TODO: could we remember if we're already enabled? idk if it's worth it
- if (domain === 'Debugger') { TabManager.scriptsForTab[tabId] = []; }
+ if (domain === 'Debugger') { TabManager.scriptsForTab[tabId] = {}; }
await sendDebuggerCommand(tabId, `${domain}.enable`, {});
}
};
@@ -288,18 +288,24 @@ router["/tabs/by-id/*/control"] = {
},
async readdir({path}) {
const tabId = parseInt(pathComponent(path, -3));
- return { entries: [".", "..", ...TabManager.scriptsForTab[tabId].map(params => sanitize(params.url).slice(0, 200) + "_" + params.scriptId)] };
+ // it's useful to put the ID first so:
+ // 1. the .js extension stays on the end
+ // 2. file prefixes like Emacs temp files, Emacs looking for file with SCCS s. prefix, etc all are clearly invalid automatically
+ const scriptFileNames = Object.values(TabManager.scriptsForTab[tabId])
+ .map(params => params.scriptId + "_" + sanitize(params.url).slice(0, 200));
+ return { entries: [".", "..", ...scriptFileNames] };
}
};
router["/tabs/by-id/*/debugger/scripts/*"] = defineFile(async path => {
const [tabId, suffix] = [parseInt(pathComponent(path, -4)), pathComponent(path, -1)];
await TabManager.debugTab(tabId);
- console.log('BEFORE', TabManager.scriptsForTab[tabId].length);
await TabManager.enableDomainForTab(tabId, "Page");
await TabManager.enableDomainForTab(tabId, "Debugger");
- console.log('AFTER', TabManager.scriptsForTab[tabId].length)
- const parts = path.split("_"); const scriptId = parts[parts.length - 1];
+ const parts = pathComponent(path, -1).split("_"); const scriptId = parts[0];
+ // TODO: check the script title in path vs the actual script title
+ if (!TabManager.scriptsForTab[tabId][scriptId]) { throw new UnixError(unix.ENOENT); }
+
const {scriptSource} = await sendDebuggerCommand(tabId, "Debugger.getScriptSource", {scriptId});
return scriptSource;
@@ -307,7 +313,10 @@ router["/tabs/by-id/*/control"] = {
const [tabId, suffix] = [parseInt(pathComponent(path, -4)), pathComponent(path, -1)];
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Debugger");
- const parts = path.split("_"); const scriptId = parts[parts.length - 1];
+ const parts = path.split("_"); const scriptId = parts[0];
+ // TODO: check the script title in path vs the actual script title
+ if (!TabManager.scriptsForTab[tabId][scriptId]) { throw new UnixError(unix.ENOENT); }
+
await sendDebuggerCommand(tabId, "Debugger.setScriptSource", {scriptId, scriptSource: buf});
});
})();
diff --git a/test/test.c b/test/test.c
index edb1c99..b5d0900 100644
--- a/test/test.c
+++ b/test/test.c
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
+#include <dirent.h>
+#include <string.h>
#include <unistd.h>
#include <assert.h>
@@ -44,6 +46,14 @@ int main() {
assert(system("ls ../fs/mnt/tabs/last-focused/debugger/scripts") == 0);
+ {
+ DIR* scripts = opendir("../fs/mnt/tabs/last-focused/debugger/scripts");
+ assert(strcmp(readdir(scripts)->d_name, ".") == 0);
+ assert(strcmp(readdir(scripts)->d_name, "..") == 0);
+ assert(strcmp(readdir(scripts)->d_name, "7_file____Users_osnr_Code_tabfs_test_test-script.js") == 0);
+ closedir(scripts);
+ }
+
assert(system("echo remove > ../fs/mnt/tabs/last-focused/control") == 0);
}