aboutsummaryrefslogtreecommitdiffstats
path: root/extension
diff options
context:
space:
mode:
authorOmar Rizwan <omar.rizwan@gmail.com>2018-11-22 02:52:54 -0800
committerOmar Rizwan <omar.rizwan@gmail.com>2018-11-22 02:52:54 -0800
commitb6f46e166da28e67adc0bf6f1e9a0a380434e7ce (patch)
tree7510b4ba4c8407e25b32134558fc3affa75b2292 /extension
parent66041e333c10387a18990f9af20d9c4daec5320e (diff)
Checkpoint. Just access JSON directly in FUSE handlers.
Diffstat (limited to 'extension')
-rw-r--r--extension/background.js86
1 files changed, 50 insertions, 36 deletions
diff --git a/extension/background.js b/extension/background.js
index b08311c..912075d 100644
--- a/extension/background.js
+++ b/extension/background.js
@@ -2,9 +2,13 @@ const ws = new WebSocket("ws://localhost:8888");
const ops = {
NONE: 0,
+
GETATTR: 1,
- READDIR: 2
+ OPEN: 2,
+ READDIR: 3,
+ READ: 4
};
+
const unix = {
EPERM: 1,
ENOENT: 2,
@@ -24,6 +28,12 @@ const unix = {
S_IFSOCK: 0140000, // socket
}
+function UnixError(error) {
+ this.name = "UnixError";
+ this.error = error;
+}
+UnixError.prototype = Error.prototype;
+
function queryTabs() {
return new Promise((resolve, reject) => chrome.tabs.query({}, resolve));
}
@@ -54,22 +64,14 @@ function findRoute(path) {
for (let segment of path.split("/")) {
if (segment === "") continue;
route = route[segment] || route["*"];
- }
- return route;
-}
-async function readdir(path) {
- let route = findRoute(path);
-
- if (route.readdir) {
- return route.readdir();
+ if (!route) throw new UnixError(unix.ENOENT);
}
- return Object.keys(route);
+ return route;
}
async function getattr(path) {
let route = findRoute(path);
-
if (route.getattr) {
return route.getattr();
} else {
@@ -78,39 +80,51 @@ async function getattr(path) {
st_nlink: 3
};
}
- /*
- * const response = {};
- * if (path === "/" || path === "/tabs" || path === "/tabs/by-title" || path === "/tabs/by-id") {
- * response.st_mode = unix.S_IFDIR | 0755;
- * response.st_nlink = 3;
-
- * } else if (path === "/tabs/hello.txt") {
- *
- * } else {
- * response.error = unix.ENOENT;
- * }
- * return response;*/
+}
+
+async function readdir(path) {
+ let route = findRoute(path);
+ if (route.readdir) {
+ return route.readdir();
+ }
+ return Object.keys(route);
}
ws.onmessage = async function(event) {
const req = JSON.parse(event.data);
console.log('req', Object.entries(ops).find(([op, opcode]) => opcode === req.op)[0], req);
- let response;
- if (req.op === ops.READDIR) {
- response = {
- op: ops.READDIR,
- entries: [".", "..", ...(await readdir(req.path))]
- };
+ let response = { op: req.op, error: unix.EIO };
+ try {
+ if (req.op === ops.GETATTR) {
+ response = {
+ op: ops.GETATTR,
+ st_mode: 0,
+ st_nlink: 0,
+ st_size: 0,
+ ...(await getattr(req.path))
+ };
+ } else if (req.op === ops.OPEN) {
+ throw new UnixError(unix.EIO);
- } else if (req.op === ops.GETATTR) {
+ } else if (req.op === ops.READDIR) {
+ response = {
+ op: ops.READDIR,
+ entries: [".", "..", ...(await readdir(req.path))]
+ };
+
+ } else if (req.op === ops.READ) {
+ response = {
+ op: ops.READ,
+ buf: await read(req.path)
+ };
+ }
+
+ } catch (e) {
response = {
- op: ops.GETATTR,
- st_mode: 0,
- st_nlink: 0,
- st_size: 0,
- ...(await getattr(req.path))
- };
+ op: req.op,
+ error: e instanceof UnixError ? e.error : unix.EIO
+ }
}
console.log('response', Object.entries(ops).find(([op, opcode]) => opcode === response.op)[0], response);