1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/example/www/js/filesystem_handler.js

88 lines
3.0 KiB
JavaScript
Raw Normal View History

2019-01-28 21:08:37 +00:00
class FileSystemHandler {
constructor(filesystem, connection) {
this._fs = filesystem;
this._connection = connection;
this._connection.onmessage = (message) => {
this._onmessage(message);
};
}
_onmessage(message) {
try {
let request = JSON.parse(message.data);
let result = -42;
let response;
console.log(request);
if (("string" === typeof(request.method)) &&
("number" === typeof(request.id)) &&
(request.params)) {
switch(request.method)
{
case "lookup":
{
const parent = request.params[0];
const name = request.params[1];
result = this._fs.lookup(parent, name);
}
break;
2019-01-28 21:08:37 +00:00
case "getattr":
2019-02-02 13:25:57 +00:00
{
const inode = request.params[0];
result = this._fs.getattr(inode);
}
break;
2019-01-28 21:08:37 +00:00
case "readdir":
2019-01-29 22:47:08 +00:00
{
2019-02-03 18:10:05 +00:00
const inode = request.params[0];
result = this._fs.readdir(inode);
2019-01-29 22:47:08 +00:00
}
break;
2019-01-28 21:08:37 +00:00
case "open":
2019-01-29 22:47:08 +00:00
{
2019-02-03 18:10:05 +00:00
const inode = request.params[0];
2019-01-29 22:47:08 +00:00
const mode = request.params[1];
2019-02-03 18:10:05 +00:00
result = this._fs.open(inode, mode);
2019-01-29 22:47:08 +00:00
}
break;
case "close":
{
2019-02-03 18:10:05 +00:00
const inode = request.params[0];
2019-01-29 22:47:08 +00:00
const handle = request.params[1];
const mode = request.params[2];
this._fs.close(inode, handle, mode);
2019-01-29 22:47:08 +00:00
}
break;
2019-01-28 21:08:37 +00:00
case "read":
2019-01-29 22:47:08 +00:00
{
2019-02-03 18:10:05 +00:00
const inode = request.params[0];
2019-01-29 22:47:08 +00:00
const handle = request.params[1];
const offset = request.params[2];
const length = request.params[3];
2019-02-03 18:10:05 +00:00
result = this._fs.read(inode, handle, offset, length);
2019-01-29 22:47:08 +00:00
}
break;
2019-01-28 21:08:37 +00:00
default:
break;
}
if ("number" == typeof(request.id))
{
if ("number" !== typeof(result)) {
response = {result: result, id: request.id};
}
else {
response = {error: {code: result}, id: request.id};
}
console.log(response);
this._connection.send(JSON.stringify(response));
2019-01-28 21:08:37 +00:00
}
}
}
catch (ex) { console.log(ex, message); }
};
}