Route tree!

This commit is contained in:
Omar Rizwan 2018-11-14 23:52:26 -08:00
parent 828a1c33d0
commit 66041e333c
2 changed files with 66 additions and 24 deletions

View file

@ -24,33 +24,75 @@ const unix = {
S_IFSOCK: 0140000, // socket S_IFSOCK: 0140000, // socket
} }
function readdir(path) { function queryTabs() {
if (path === "/") { return new Promise((resolve, reject) => chrome.tabs.query({}, resolve));
return ["tabs"];
} else if (path === "/tabs") {
return ["hello.txt"];
}
} }
function getattr(path) { const router = {
const response = {}; "tabs": {
if (path === "/" || path === "/tabs") { "by-id": {
response.st_mode = unix.S_IFDIR | 0755; async readdir() {
response.st_nlink = 3; const tabs = await queryTabs();
return tabs.map(tab => String(tab.id));
},
} else if (path === "/tabs/hello.txt") { "*": {
response.st_mode = unix.S_IFREG | 0444; async getattr() {
response.st_nlink = 1; return {
response.st_size = 10; // FIXME st_mode: unix.S_IFREG | 0444,
st_nlink: 1,
st_size: 10 // FIXME
};
}
}
}
}
};
function findRoute(path) {
let route = router;
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();
}
return Object.keys(route);
}
async function getattr(path) {
let route = findRoute(path);
if (route.getattr) {
return route.getattr();
} else { } else {
response.error = unix.ENOENT; return {
st_mode: unix.S_IFDIR | 0755,
st_nlink: 3
};
} }
return response; /*
* 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;*/
} }
ws.onmessage = function(event) { ws.onmessage = async function(event) {
const req = JSON.parse(event.data); const req = JSON.parse(event.data);
console.log('req', Object.entries(ops).find(([op, opcode]) => opcode === req.op)[0], req); console.log('req', Object.entries(ops).find(([op, opcode]) => opcode === req.op)[0], req);
@ -58,7 +100,7 @@ ws.onmessage = function(event) {
if (req.op === ops.READDIR) { if (req.op === ops.READDIR) {
response = { response = {
op: ops.READDIR, op: ops.READDIR,
entries: [".", "..", ...readdir(req.path)] entries: [".", "..", ...(await readdir(req.path))]
}; };
} else if (req.op === ops.GETATTR) { } else if (req.op === ops.GETATTR) {
@ -67,7 +109,7 @@ ws.onmessage = function(event) {
st_mode: 0, st_mode: 0,
st_nlink: 0, st_nlink: 0,
st_size: 0, st_size: 0,
...getattr(req.path) ...(await getattr(req.path))
}; };
} }

View file

@ -1,11 +1,11 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "ChromeFS Extension", "name": "TabFS Extension",
"description": "Connects to ChromeFS filesystem", "description": "Connects to TabFS filesystem",
"version": "1.0", "version": "1.0",
"permissions": ["tabs"], "permissions": ["tabs", "debugger"],
"background": { "background": {
"scripts": ["background.js"], "scripts": ["background.js"],