fix bugs with error handling (Unix vs other Errors). add open stubs

now I actually will have to implement all the ops properly for every
route...
This commit is contained in:
Omar Rizwan 2020-12-02 11:58:37 -08:00
parent a7af875a65
commit 45518bce56

View file

@ -20,11 +20,14 @@ const unix = {
S_IFSOCK: 0140000, // socket S_IFSOCK: 0140000, // socket
} }
function UnixError(error) { class UnixError extends Error {
this.name = "UnixError"; constructor(error) {
this.error = error; super();
this.name = "UnixError";
this.error = error;
}
} }
UnixError.prototype = Error.prototype;
// tabs/by-id/ID/title // tabs/by-id/ID/title
// tabs/by-id/ID/url // tabs/by-id/ID/url
@ -58,11 +61,19 @@ function stringSize(str) {
const debugging = {}; const debugging = {};
async function debugTab(tabId) { async function debugTab(tabId) {
if (!debugging[tabId]) { if (debugging[tabId]) {
await new Promise(resolve => chrome.debugger.attach({tabId}, "1.3", resolve)); debugging[tabId] += 1;
debugging[tabId] = 0;
} else {
await new Promise((resolve, reject) => chrome.debugger.attach({tabId}, "1.3", () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
debugging[tabId] = 1;
resolve();
}
}));
} }
debugging[tabId] += 1;
} }
function sendDebuggerCommand(tabId, method, commandParams) { function sendDebuggerCommand(tabId, method, commandParams) {
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
@ -107,6 +118,7 @@ function withTab(handler) {
st_size: stringSize(handler(tab)) st_size: stringSize(handler(tab))
}; };
}, },
async open({path}) { return { fh: 0 }; },
async read({path, fh, size, offset}) { async read({path, fh, size, offset}) {
const tab = await browser.tabs.get(parseInt(pathComponent(path, -2))); const tab = await browser.tabs.get(parseInt(pathComponent(path, -2)));
return { buf: handler(tab).substr(offset, size) }; return { buf: handler(tab).substr(offset, size) };
@ -123,6 +135,7 @@ function fromScript(code) {
st_size: stringSize((await browser.tabs.executeScript(tabId, {code}))[0]) st_size: stringSize((await browser.tabs.executeScript(tabId, {code}))[0])
}; };
}, },
async open({path}) { return { fh: 0 }; },
async read({path, fh, size, offset}) { async read({path, fh, size, offset}) {
const tabId = parseInt(pathComponent(path, -2)); const tabId = parseInt(pathComponent(path, -2));
return { buf: (await browser.tabs.executeScript(tabId, {code}))[0] return { buf: (await browser.tabs.executeScript(tabId, {code}))[0]
@ -133,6 +146,7 @@ function fromScript(code) {
router["/tabs/by-id"] = { router["/tabs/by-id"] = {
getattr: directoryGetattr, getattr: directoryGetattr,
async opendir({path}) { return { fh: 0 }; },
async readdir() { async readdir() {
const tabs = await browser.tabs.query({}); const tabs = await browser.tabs.query({});
return { entries: tabs.map(tab => String(tab.id)) }; return { entries: tabs.map(tab => String(tab.id)) };
@ -146,7 +160,7 @@ router["/tabs/by-id/*/resources"] = {
async opendir({path}) { async opendir({path}) {
const tabId = parseInt(pathComponent(path, -2)); const tabId = parseInt(pathComponent(path, -2));
await debugTab(tabId); await debugTab(tabId);
return 0; return { fh: 0 };
}, },
async readdir({path}) { async readdir({path}) {
const tabId = parseInt(pathComponent(path, -2)); const tabId = parseInt(pathComponent(path, -2));
@ -184,6 +198,7 @@ router["/tabs/by-id/*/resources/*"] = {
}, },
async open({path}) { async open({path}) {
// FIXME: cache the file // FIXME: cache the file
return {fh: 3};
}, },
async read({path, fh, size, offset}) { async read({path, fh, size, offset}) {
const tabId = parseInt(pathComponent(path, -3)); const tabId = parseInt(pathComponent(path, -3));
@ -235,6 +250,7 @@ router["/tabs/by-id/*/control"] = {
router["/tabs/by-title"] = { router["/tabs/by-title"] = {
getattr: directoryGetattr, getattr: directoryGetattr,
async opendir({path}) { return { fh: 0 }; },
async readdir() { async readdir() {
const tabs = await browser.tabs.query({}); const tabs = await browser.tabs.query({});
return { entries: tabs.map(tab => sanitize(String(tab.title).slice(0, 200)) + "_" + String(tab.id)) }; return { entries: tabs.map(tab => sanitize(String(tab.title).slice(0, 200)) + "_" + String(tab.id)) };
@ -243,7 +259,7 @@ router["/tabs/by-title"] = {
router["/tabs/by-title/*"] = { router["/tabs/by-title/*"] = {
// a symbolic link to /tabs/by-id/[id for this tab] // a symbolic link to /tabs/by-id/[id for this tab]
async getattr({path}) { async getattr({path}) {
const st_size = (await this.readlink({path})).length + 1; const st_size = (await this.readlink({path})).buf.length + 1;
return { return {
st_mode: unix.S_IFLNK | 0444, st_mode: unix.S_IFLNK | 0444,
st_nlink: 1, st_nlink: 1,
@ -297,6 +313,7 @@ for (let key in router) {
router[path] = { router[path] = {
getattr: directoryGetattr, getattr: directoryGetattr,
opendir({path}) { return { fh: 0 }; },
readdir() { return { entries: children }; } readdir() { return { entries: children }; }
}; };
} }
@ -417,7 +434,6 @@ async function onMessage(req) {
let response = { op: req.op, error: unix.EIO }; let response = { op: req.op, error: unix.EIO };
/* console.time(req.op + ':' + req.path);*/ /* console.time(req.op + ':' + req.path);*/
try { try {
console.log(findRoute(req.path)[req.op]);
response = await findRoute(req.path)[req.op](req); response = await findRoute(req.path)[req.op](req);
response.op = req.op; response.op = req.op;