TabFS/extension/background.js

119 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-11-11 14:32:44 +01:00
const ws = new WebSocket("ws://localhost:8888");
2018-11-12 01:18:21 +01:00
const ops = {
NONE: 0,
GETATTR: 1,
READDIR: 2
};
const unix = {
EPERM: 1,
ENOENT: 2,
ESRCH: 3,
EINTR: 4,
EIO: 5,
ENXIO: 6,
// Unix file types
S_IFMT: 0170000, // type of file mask
S_IFIFO: 010000, // named pipe (fifo)
S_IFCHR: 020000, // character special
S_IFDIR: 040000, // directory
S_IFBLK: 060000, // block special
S_IFREG: 0100000, // regular
S_IFLNK: 0120000, // symbolic link
S_IFSOCK: 0140000, // socket
}
2018-11-15 08:52:26 +01:00
function queryTabs() {
return new Promise((resolve, reject) => chrome.tabs.query({}, resolve));
}
const router = {
"tabs": {
"by-id": {
async readdir() {
const tabs = await queryTabs();
return tabs.map(tab => String(tab.id));
},
"*": {
async getattr() {
return {
st_mode: unix.S_IFREG | 0444,
st_nlink: 1,
st_size: 10 // FIXME
};
}
}
}
}
};
2018-11-12 01:31:02 +01:00
2018-11-15 08:52:26 +01:00
function findRoute(path) {
let route = router;
for (let segment of path.split("/")) {
if (segment === "") continue;
route = route[segment] || route["*"];
2018-11-12 01:31:02 +01:00
}
2018-11-15 08:52:26 +01:00
return route;
2018-11-12 01:31:02 +01:00
}
2018-11-15 08:52:26 +01:00
async function readdir(path) {
let route = findRoute(path);
2018-11-12 01:31:02 +01:00
2018-11-15 08:52:26 +01:00
if (route.readdir) {
return route.readdir();
}
return Object.keys(route);
}
async function getattr(path) {
let route = findRoute(path);
2018-11-12 01:31:02 +01:00
2018-11-15 08:52:26 +01:00
if (route.getattr) {
return route.getattr();
2018-11-12 01:31:02 +01:00
} else {
2018-11-15 08:52:26 +01:00
return {
st_mode: unix.S_IFDIR | 0755,
st_nlink: 3
};
2018-11-12 01:31:02 +01:00
}
2018-11-15 08:52:26 +01:00
/*
* 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;*/
2018-11-12 01:31:02 +01:00
}
2018-11-15 08:52:26 +01:00
ws.onmessage = async function(event) {
2018-11-11 14:32:44 +01:00
const req = JSON.parse(event.data);
2018-11-12 01:18:21 +01:00
console.log('req', Object.entries(ops).find(([op, opcode]) => opcode === req.op)[0], req);
2018-11-11 14:32:44 +01:00
2018-11-11 20:44:36 +01:00
let response;
2018-11-12 01:18:21 +01:00
if (req.op === ops.READDIR) {
response = {
op: ops.READDIR,
2018-11-15 08:52:26 +01:00
entries: [".", "..", ...(await readdir(req.path))]
2018-11-12 01:18:21 +01:00
};
2018-11-12 01:31:02 +01:00
2018-11-12 01:18:21 +01:00
} else if (req.op === ops.GETATTR) {
2018-11-11 20:44:36 +01:00
response = {
2018-11-12 01:18:21 +01:00
op: ops.GETATTR,
st_mode: 0,
st_nlink: 0,
2018-11-12 01:31:02 +01:00
st_size: 0,
2018-11-15 08:52:26 +01:00
...(await getattr(req.path))
2018-11-11 20:44:36 +01:00
};
2018-11-12 01:18:21 +01:00
}
2018-11-11 14:32:44 +01:00
2018-11-12 01:18:21 +01:00
console.log('response', Object.entries(ops).find(([op, opcode]) => opcode === response.op)[0], response);
2018-11-11 14:32:44 +01:00
ws.send(JSON.stringify(response));
};