From 1459d5293b2eac9399265edb77c2a3c017221f1a Mon Sep 17 00:00:00 2001 From: garrettmills Date: Thu, 26 Nov 2020 20:03:19 -0600 Subject: [PATCH] Initial import --- Errors.js | 32 +++++++++++++++++++ Message.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 Errors.js create mode 100644 Message.js diff --git a/Errors.js b/Errors.js new file mode 100644 index 0000000..2c3c1d8 --- /dev/null +++ b/Errors.js @@ -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) + } + } +} diff --git a/Message.js b/Message.js new file mode 100644 index 0000000..de5de3b --- /dev/null +++ b/Message.js @@ -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