Skip to content

Commit

Permalink
feat: add code
Browse files Browse the repository at this point in the history
  • Loading branch information
csimi committed Dec 15, 2019
0 parents commit d9fda65
Show file tree
Hide file tree
Showing 24 changed files with 3,376 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = tab
charset = utf-8

[{package.json,*.yml}]
indent_style = space
indent_size = 2
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
coverage
dist
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "script"
},
"extends": [
"@csimi/eslint-config"
],
"rules": {
"no-bitwise": "off"
}
}
26 changes: 26 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: build

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [8.x, 10.x, 12.x]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm test
env:
CI: true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log*
coverage
.nyc_output
4 changes: 4 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"recursive": true,
"file": ["test/setup.js"]
}
7 changes: 7 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"all": true,
"produce-source-map": true,
"include": [
"lib/**"
]
}
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { EventEmitter } = require('events');
const { inherits } = require('util');
const nanoMQTT = require('./lib');

inherits(nanoMQTT, EventEmitter);

function nanoEmitterMQTT (...args) {
const nano = new nanoMQTT(...args);
EventEmitter.call(nano);
return nano;
}

module.exports = nanoEmitterMQTT;
12 changes: 12 additions & 0 deletions lib/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"env": {
"es6": false
},
"parserOptions": {
"ecmaVersion": 5
},
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
}
54 changes: 54 additions & 0 deletions lib/adapter/espruino-tcp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* global E */

var net = require('net');

function createConnection (nano, address, port) {
var connection = net.connect({
'host': address,
'port': port,
}, nano.connect.bind(nano));

var parse = function (message) {
var data = E.toUint8Array(message);
nano.parse(data);
};
var write = function (data) {
var message = E.toString(data);
connection.write(message);
};

connection.on('data', parse);
nano.on('data', write);

connection.on('end', function onClose () {
connection.removeListener('data', parse);
nano.removeListener('data', write);
});

return connection;
}

module.exports = function espruinoTcpAdapter (nano, address, port, timeout, retry) {
function setupConnection () {
try {
var connection = createConnection(nano, address, port);

var closeConnection = connection.end.bind(connection);
var removeTimeout = clearTimeout.bind(void 0, setTimeout(closeConnection, timeout || 10000));

nano.on('connected', removeTimeout);
connection.on('end', function onConnectionClosed () {
nano.removeListener('connected', removeTimeout);
nano.setConnected(false);
setTimeout(setupConnection, retry || 5000);
});
}
catch (err) {
setTimeout(setupConnection, retry || 5000);
}
}

setupConnection();

return nano;
};
28 changes: 28 additions & 0 deletions lib/adapter/espruino-udp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* global E */

var dgram = require('dgram');

module.exports = function espruinoUdpAdapter (nano, address, port, isBroadcast) {
var server = dgram.createSocket('udp4');
server.bind(function onBound () {
server.setBroadcast(Boolean(isBroadcast));
});

server.on('message', function onMessage (message) {
var data = E.toUint8Array(message);
nano.parse(data);
});
nano.on('data', function onData (data) {
var message = E.toString(data);
server.send(message, 0, message.length, port, address);
});

if (isBroadcast) {
setTimeout(nano.setConnected.bind(nano, true), 0);
}
else {
setTimeout(nano.connect.bind(nano));
}

return nano;
};
106 changes: 106 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* global Uint8Array */

var FCC = String.fromCharCode;

function createField (value) {
return FCC(value.length >> 8, value.length & 255) + value;
}

function createPacket (cmd, variable, payload) {
var value = variable + payload;
var length = value.length;

var buf = new Uint8Array(2 + length);

buf[0] = cmd;
buf[1] = length;

for (var i = 0; i < length; i++) {
buf[i + 2] = value.charCodeAt(i);
}

return buf;
}

function nano (uid) {
this.uid = uid;
this.isConnected = false;
}

nano.prototype.CONNECT = 1 << 4;
nano.prototype.CONNACK = 2 << 4;
nano.prototype.PUBLISH = 3 << 4;
nano.prototype.SUBSCRIBE = 8 << 4;
nano.prototype.PINGREQ = 12 << 4;
nano.prototype.DISCONNECT = 14 << 4;

nano.prototype.setConnected = function setConnected (isConnected) {
this.isConnected = isConnected;
this.emit(isConnected ? 'connected' : 'disconnected');
};

nano.prototype.connect = function connect () {
this.emit('data', createPacket(
nano.prototype.CONNECT,
createField('MQTT') + FCC(
4,
0,
0,
0
),
createField(this.uid)
), nano.prototype.CONNECT);
};

nano.prototype.disconnect = function disconnect () {
this.emit('data', createPacket(
nano.prototype.DISCONNECT,
'',
''
), nano.prototype.DISCONNECT);
};

nano.prototype.publish = function publish (topic, message, retain) {
var flags = retain ? 1 : 0;
this.emit('data', createPacket(
nano.prototype.PUBLISH | flags,
createField(topic),
message
), nano.prototype.PUBLISH);
};

nano.prototype.subscribe = function subscribe (topic) {
this.emit('data', createPacket(
nano.prototype.SUBSCRIBE | 2,
FCC(1 << 8, 1 & 255),
createField(topic) + FCC(0)
), nano.prototype.SUBSCRIBE);
};

nano.prototype.parse = function parse (data) {
var cmd = data[0];
var message = data.toString();
if (cmd === nano.prototype.CONNACK) {
this.setConnected(true);
return [nano.prototype.CONNACK];
}
else if (cmd === nano.prototype.PUBLISH) {
var index = 4 + ((data[2] << 8) | data[3]);
var topic = message.slice(4, index);
var payload = message.slice(index);

this.emit('message', topic, payload);
return [nano.prototype.PUBLISH, topic, payload];
}
else if (cmd === nano.prototype.PINGREQ) {
this.emit('ping');
return [nano.prototype.PINGREQ];
}
else {
this.emit('error', data);
}

return [];
};

module.exports = nano;
5 changes: 5 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Copyright (c) 2019, David Csirmaz

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Loading

0 comments on commit d9fda65

Please sign in to comment.