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/daemon/www/js/webfuse/client.js

151 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-02-21 23:35:59 +00:00
import { BadState } from "./bad_state.js";
export class Client {
static get _PROTOCOL() { return "fs"; }
constructor(provider) {
this._provider = provider;
this._ws = null;
this.onopen = () => { };
this.onclose = () => { };
this.onerror = () => { };
2019-02-21 23:35:59 +00:00
}
connectTo(url) {
this.disconnect();
this._ws = new WebSocket(url, Client._PROTOCOL);
this._ws.onopen = this.onopen;
this._ws.onclose = this.onclose;
this._ws.onerror = this.onerror;
2019-02-21 23:35:59 +00:00
this._ws.onmessage = (message) => {
this._onmessage(message);
};
}
authenticate(type, credentials) {
const request = {
"method": "authenticate",
"params": [type, credentials],
"id": 42
};
this._ws.send(JSON.stringify(request));
}
2019-02-21 23:35:59 +00:00
disconnect() {
if (this._ws) {
this._ws.close();
this._ws = null;
2019-02-21 23:35:59 +00:00
}
}
isConnected() {
return ((this._ws) && (this._ws.readyState === WebSocket.OPEN));
}
2019-02-21 23:35:59 +00:00
_onmessage(message) {
try {
const request = JSON.parse(message.data);
const method = request.method;
const id = request.id;
const params = request.params;
if ("string" !== typeof(method)) {
throw new Error("parse error: missing field: \"method\"");
}
if (!params) {
throw new Error("parse error: missing field: \"params\"");
}
if ("number" === typeof(request.id)) {
this._invoke(method, params, id);
}
else {
this._notify(method, params);
}
}
catch (ex) {
// swallow
}
}
_invoke(method, params, id) {
this._invokeAsync(method, params).
then((result) => {
2019-02-21 23:35:59 +00:00
const response = { result, id };
this._ws.send(JSON.stringify(response));
}).
catch((ex) => {
const code = ex.code || BadState.BAD;
const response = {error: {code}, id};
this._ws.send(JSON.stringify(response));
});
}
async _invokeAsync(method, params) {
switch(method)
{
case "lookup":
return this._lookup(params);
case "getattr":
return this._getattr(params);
case "readdir":
return this._readdir(params);
case "open":
return this._open(params);
case "read":
return this._read(params);
default:
throw new BadState(BadState.NOT_IMPLEMENTED);
}
}
_notify(method, params) {
switch(method) {
case 'close':
this._close(params);
break;
default:
throw new Error(`Invalid method: "${method}"`);
}
}
async _lookup([parent, name]) {
2019-02-21 23:35:59 +00:00
return this._provider.lookup(parent, name);
}
async _getattr([inode]) {
2019-02-21 23:35:59 +00:00
return this._provider.getattr(inode);
}
async _readdir([inode]) {
2019-02-21 23:35:59 +00:00
return this._provider.readdir(inode);
}
async _open([inode, mode]) {
2019-02-21 23:35:59 +00:00
return this._provider.open(inode, mode);
}
_close([inode, handle, mode]) {
2019-02-21 23:35:59 +00:00
this._provider.close(inode, handle, mode);
}
async _read([inode, handle, offset, length]) {
2019-02-21 23:35:59 +00:00
const data = await this._provider.read(inode, handle, offset, length);
if ("string" === typeof(data)) {
return {
data: btoa(data),
format: "base64",
count: data.length
};
}
else {
throw new BadState(BadState.BAD);
}
}
}