Initial import

This commit is contained in:
Garrett Mills 2020-11-26 20:03:19 -06:00
commit 1459d5293b
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
2 changed files with 126 additions and 0 deletions

32
Errors.js Normal file
View File

@ -0,0 +1,32 @@
const Fuse = require('fuse-native')
module.exports = exports = {
InvalidReplyUUID: 'INVALID_REPLY_UUID',
InvalidMessageRoute: 'INVALID_MESSAGE_ROUTE',
NodeDoesNotExist: 'NODE_DOES_NOT_EXIST',
NodeAlreadyExists: 'NODE_ALREADY_EXISTS',
NodePermissionFail: 'NODE_PERMISSION_FAIL',
NodeNotEmpty: 'NODE_NOT_EMPTY',
NotDirectoryDescriptor: 'NOT_DIRECTORY_DESCRIPTOR',
IsDirectoryDescriptor: 'IS_DIRECTORY_DESCRIPTOR',
NoSuchDescriptor: 'NO_SUCH_DESCRIPTOR',
toCallback(cb, error) {
if ( error === module.exports.NodeDoesNotExist ) {
return process.nextTick(cb, Fuse.ENOENT)
} else if ( error === module.exports.NodeAlreadyExists ) {
return process.nextTick(cb, Fuse.EEXIST)
} else if ( error === module.exports.NodePermissionFail ) {
return process.nextTick(cb, Fuse.EACCES)
} else if ( error === module.exports.NodeNotEmpty ) {
return process.nextTick(cb, Fuse.ENOTEMPTY)
} else if ( error === module.exports.NotDirectoryDescriptor ) {
return process.nextTick(cb, Fuse.ENOTDIR)
} else if ( error === module.exports.IsDirectoryDescriptor ) {
return process.nextTick(cb, Fuse.EISDIR)
} else {
return process.nextTick(cb, Fuse.EBADMSG)
}
}
}

94
Message.js Normal file
View File

@ -0,0 +1,94 @@
const uuid = require('uuid').v4;
class Message {
needs_response = false;
has_response = false;
socket;
sender;
get socket_send() {
if ( this.sender ) {
return this.sender;
} else {
return (...args) => this.socket.send(...args);
}
}
static route(route) {
const msg = new this();
return msg.route(route);
}
constructor(message = '{}') {
this._data = JSON.parse(message)
this._uuid = this._data.uuid || uuid()
}
fresh() {
const inst = new this.constructor()
inst.socket = this.socket
inst.sender = this.sender
return inst
}
error(set = undefined) {
if ( set ) {
this._data.error = set;
return this;
}
return this._data.error;
}
response_to(req_uuid = undefined) {
if ( req_uuid ) {
this._data.in_response_to = req_uuid;
return this;
}
return this._data.in_response_to;
}
is_response() {
return !!this._data.in_response_to;
}
route(set = undefined) {
if ( set ) {
this._data.route = set;
return this;
}
return this._data.route;
}
data(set = undefined) {
if ( set ) {
this._data.data = set;
return this;
}
return this._data.data;
}
uuid() {
return this._uuid;
}
serialize() {
return JSON.stringify({...this._data, uuid: this._uuid});
}
expect_response(callback = () => {}) {
this.needs_response = true;
this._response_callback = callback;
return this;
}
send_response(other_message) {
other_message.response_to(this.uuid())
this.socket_send(other_message.serialize())
}
}
module.exports = exports = Message