mqtt stuff added

This commit is contained in:
2018-05-16 10:44:10 +02:00
parent 74584cdbbe
commit c7eb46b346
499 changed files with 55775 additions and 19 deletions

8
node_modules/websocket-stream/LICENSE generated vendored Normal file
View File

@ -0,0 +1,8 @@
Copyright (c) 2013, Max Ogden
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

11
node_modules/websocket-stream/collaborators.md generated vendored Normal file
View File

@ -0,0 +1,11 @@
## Collaborators
websocket-stream is only possible due to the excellent work of the following collaborators:
<table><tbody><tr><th align="left">gst</th><td><a href="https://github.com/gst">GitHub/gst</a></td></tr>
<tr><th align="left">maxogden</th><td><a href="https://github.com/maxogden">GitHub/maxogden</a></td></tr>
<tr><th align="left">deanlandolt</th><td><a href="https://github.com/deanlandolt">GitHub/deanlandolt</a></td></tr>
<tr><th align="left">mcollina</th><td><a href="https://github.com/mcollina">GitHub/mcollina</a></td></tr>
<tr><th align="left">jnordberg</th><td><a href="https://github.com/jnordberg">GitHub/jnordberg</a></td></tr>
<tr><th align="left">mafintosh</th><td><a href="https://github.com/mafintosh">GitHub/mafintosh</a></td></tr>
</tbody></table>

51
node_modules/websocket-stream/echo-server.js generated vendored Normal file
View File

@ -0,0 +1,51 @@
'use strict'
var http = require('http')
var websocket = require('./')
var server = null
var port = module.exports.port = 8343
var url = module.exports.url = 'ws://localhost:' + module.exports.port
module.exports.start = function(opts, cb) {
if (server) {
cb(new Error('already started'));
return;
}
if (typeof opts == 'function') {
cb = opts;
opts = {};
}
server = http.createServer()
opts.server = server
websocket.createServer(opts, echo)
server.listen(port, cb)
function echo(stream) {
stream.pipe(stream)
}
}
module.exports.stop = function(cb) {
if (!server) {
cb(new Error('not started'))
return
}
server.close(cb)
server = null
}
if (!module.parent) {
module.exports.start(function(err) {
if (err) {
console.error(err);
return;
}
console.log('Echo server started on port ' + port);
});
}

6
node_modules/websocket-stream/index.js generated vendored Normal file
View File

@ -0,0 +1,6 @@
var server = require('./server.js')
module.exports = require('./stream.js')
module.exports.Server = server.Server
module.exports.createServer = server.createServer

76
node_modules/websocket-stream/package.json generated vendored Normal file
View File

@ -0,0 +1,76 @@
{
"_from": "websocket-stream@^5.1.2",
"_id": "websocket-stream@5.1.2",
"_inBundle": false,
"_integrity": "sha512-lchLOk435iDWs0jNuL+hiU14i3ERSrMA0IKSiJh7z6X/i4XNsutBZrtqu2CPOZuA4G/zabiqVAos0vW+S7GEVw==",
"_location": "/websocket-stream",
"_npmUser": {
"name": "maxogden",
"email": "max@maxogden.com"
},
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "websocket-stream@^5.1.2",
"name": "websocket-stream",
"escapedName": "websocket-stream",
"rawSpec": "^5.1.2",
"saveSpec": null,
"fetchSpec": "^5.1.2"
},
"_requiredBy": [
"/mqtt"
],
"_resolved": "https://registry.npmjs.org/websocket-stream/-/websocket-stream-5.1.2.tgz",
"_shasum": "1c31c627bcdf34f1a9bdacc9daa15bfa4816d9ad",
"_spec": "websocket-stream@^5.1.2",
"_where": "/home/wn/workspace-node/PiAlive/node_modules/mqtt",
"author": "",
"browser": {
"./echo-server.js": "./fake-server.js",
"./index.js": "./stream.js",
"ws": "./ws-fallback.js"
},
"bugs": {
"url": "https://github.com/maxogden/websocket-stream/issues"
},
"bundleDependencies": false,
"dependencies": {
"duplexify": "^3.5.1",
"inherits": "^2.0.1",
"readable-stream": "^2.3.3",
"safe-buffer": "^5.1.1",
"ws": "^3.2.0",
"xtend": "^4.0.0"
},
"deprecated": false,
"description": "Use websockets with the node streams API. Works in browser and node",
"devDependencies": {
"beefy": "^2.1.1",
"browserify": "^14.5.0",
"concat-stream": "^1.4.7",
"tape": "^4.8.0"
},
"homepage": "https://github.com/maxogden/websocket-stream#readme",
"keywords": [
"websocket",
"websockets",
"stream",
"streams",
"realtime"
],
"license": "BSD-2-Clause",
"main": "index.js",
"name": "websocket-stream",
"optionalDependencies": {},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/maxogden/websocket-stream.git"
},
"scripts": {
"start": "beefy test-client.js",
"test": "node test.js"
},
"version": "5.1.2"
}

148
node_modules/websocket-stream/readme.md generated vendored Normal file
View File

@ -0,0 +1,148 @@
# websocket-stream
[![NPM](https://nodei.co/npm/websocket-stream.png?global=true)](https://nodei.co/npm/websocket-stream/)
Use HTML5 [websockets](https://developer.mozilla.org/en-US/docs/WebSockets) using the Node Streams API.
### Usage
This module works in Node or in Browsers that support WebSockets. You can use [browserify](http://github.com/substack/node-browserify) to package this module for browser use.
```javascript
var websocket = require('websocket-stream')
var ws = websocket('ws://echo.websocket.org')
process.stdin.pipe(ws)
ws.pipe(process.stdout)
```
In the example above `ws` is a duplex stream. That means you can pipe output to anything that accepts streams. You can also pipe data into streams (such as a webcam feed or audio data).
The underlying `WebSocket` instance is available as `ws.socket`.
#### Options
The available options differs depending on if you use this module in the browser or with node.js. Options can be passed in as the third or second argument - `WebSocket(address, [protocols], [options])`.
##### `options.browserBufferSize`
How much to allow the [socket.bufferedAmount](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Attributes) to grow before starting to throttle writes. This option has no effect in node.js.
Default: `1024 * 512` (512KiB)
##### `options.browserBufferTimeout`
How long to wait before checking if the socket buffer has drained sufficently for another write. This option has no effect in node.js.
Default: `1000` (1 second)
##### `options.objectMode`
Send each chunk on its own, and do not try to pack them in a single
websocket frame.
Default: `false`
##### `options.binary`
Always convert to `Buffer` in Node.js before sending.
Forces `options.objectMode` to `false`.
Default: `true`
##### `options.perMessageDeflate`
We recommend disabling the [per message deflate
extension](https://tools.ietf.org/html/rfc7692) to achieve the best
throughput.
Default: `true` on the client, `false` on the server.
Example:
```js
var websocket = require('websocket-stream')
var ws = websocket('ws://realtimecats.com', {
perMessageDeflate: false
})
```
Beware that this option is ignored by browser clients. To make sure that permessage-deflate is never used, disable it on the server.
##### Other options
When used in node.js see the [ws.WebSocket documentation](https://github.com/websockets/ws/blob/master/doc/ws.md#class-wswebsocket)
### On the server
Using the [`ws`](http://npmjs.org/ws) module you can make a websocket server and use this module to get websocket streams on the server:
```javascript
var websocket = require('websocket-stream')
var wss = websocket.createServer({server: someHTTPServer}, handle)
function handle(stream, request) {
// `request` is the upgrade request sent by the client.
fs.createReadStream('bigdata.json').pipe(stream)
}
```
We recommend disabling the [per message deflate
extension](https://tools.ietf.org/html/rfc7692) to achieve the best
throughput:
```javascript
var websocket = require('websocket-stream')
var wss = websocket.createServer({
perMessageDeflate: false,
server: someHTTPServer
}, handle)
function handle(stream) {
fs.createReadStream('bigdata.json').pipe(stream)
}
```
You can even use it on express.js with the [express-ws](https://www.npmjs.com/package/express-ws) library:
```js
const express = require('express');
const expressWebSocket = require('express-ws');
const websocketStream = require('websocket-stream/stream');
const app = express();
// extend express app with app.ws()
expressWebSocket(app, null, {
// ws options here
perMessageDeflate: false,
});
app.ws('/bigdata.json', function(ws, req) {
// convert ws instance to stream
const stream = websocketStream(ws, {
// websocket-stream options here
binary: true,
});
fs.createReadStream('bigdata.json').pipe(stream);
});
app.listen(3000);
```
## Run the tests
### Server-side tests
```
npm test
```
### Client-side tests
First start the echo server by running `node test-server.js`
Then run `npm start` and open `localhost:9966` in your browser and open the Dev Tools console to see test output.
## license
BSD LICENSE

29
node_modules/websocket-stream/server.js generated vendored Normal file
View File

@ -0,0 +1,29 @@
'use strict'
var WebSocketServer = require('ws').Server
var stream = require('./stream')
class Server extends WebSocketServer{
constructor(opts, cb) {
super(opts)
var proxied = false
this.on('newListener', function(event) {
if (!proxied && event === 'stream') {
proxied = true
this.on('connection', function(conn, req) {
this.emit('stream', stream(conn, opts), req)
})
}
})
if (cb) {
this.on('stream', cb)
}
}
}
module.exports.Server = Server
module.exports.createServer = function(opts, cb) {
return new Server(opts, cb)
}

169
node_modules/websocket-stream/stream.js generated vendored Normal file
View File

@ -0,0 +1,169 @@
'use strict'
var Transform = require('readable-stream').Transform
var duplexify = require('duplexify')
var WS = require('ws')
var Buffer = require('safe-buffer').Buffer
module.exports = WebSocketStream
function buildProxy (options, socketWrite, socketEnd) {
var proxy = new Transform({
objectMode: options.objectMode
})
proxy._write = socketWrite
proxy._flush = socketEnd
return proxy
}
function WebSocketStream(target, protocols, options) {
var stream, socket
var isBrowser = process.title === 'browser'
var isNative = !!global.WebSocket
var socketWrite = isBrowser ? socketWriteBrowser : socketWriteNode
if (protocols && !Array.isArray(protocols) && 'object' === typeof protocols) {
// accept the "options" Object as the 2nd argument
options = protocols
protocols = null
if (typeof options.protocol === 'string' || Array.isArray(options.protocol)) {
protocols = options.protocol;
}
}
if (!options) options = {}
if (options.objectMode === undefined) {
options.objectMode = !(options.binary === true || options.binary === undefined)
}
var proxy = buildProxy(options, socketWrite, socketEnd)
if (!options.objectMode) {
proxy._writev = writev
}
// browser only: sets the maximum socket buffer size before throttling
var bufferSize = options.browserBufferSize || 1024 * 512
// browser only: how long to wait when throttling
var bufferTimeout = options.browserBufferTimeout || 1000
// use existing WebSocket object that was passed in
if (typeof target === 'object') {
socket = target
// otherwise make a new one
} else {
// special constructor treatment for native websockets in browsers, see
// https://github.com/maxogden/websocket-stream/issues/82
if (isNative && isBrowser) {
socket = new WS(target, protocols)
} else {
socket = new WS(target, protocols, options)
}
socket.binaryType = 'arraybuffer'
}
// was already open when passed in
if (socket.readyState === socket.OPEN) {
stream = proxy
} else {
stream = duplexify.obj()
socket.onopen = onopen
}
stream.socket = socket
socket.onclose = onclose
socket.onerror = onerror
socket.onmessage = onmessage
proxy.on('close', destroy)
var coerceToBuffer = !options.objectMode
function socketWriteNode(chunk, enc, next) {
// avoid errors, this never happens unless
// destroy() is called
if (socket.readyState !== socket.OPEN) {
next()
return
}
if (coerceToBuffer && typeof chunk === 'string') {
chunk = Buffer.from(chunk, 'utf8')
}
socket.send(chunk, next)
}
function socketWriteBrowser(chunk, enc, next) {
if (socket.bufferedAmount > bufferSize) {
setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
return
}
if (coerceToBuffer && typeof chunk === 'string') {
chunk = Buffer.from(chunk, 'utf8')
}
try {
socket.send(chunk)
} catch(err) {
return next(err)
}
next()
}
function socketEnd(done) {
socket.close()
done()
}
function onopen() {
stream.setReadable(proxy)
stream.setWritable(proxy)
stream.emit('connect')
}
function onclose() {
stream.end()
stream.destroy()
}
function onerror(err) {
stream.destroy(err)
}
function onmessage(event) {
var data = event.data
if (data instanceof ArrayBuffer) data = Buffer.from(data)
else data = Buffer.from(data, 'utf8')
proxy.push(data)
}
function destroy() {
socket.close()
}
// this is to be enabled only if objectMode is false
function writev (chunks, cb) {
var buffers = new Array(chunks.length)
for (var i = 0; i < chunks.length; i++) {
if (typeof chunks[i].chunk === 'string') {
buffers[i] = Buffer.from(chunks[i], 'utf8')
} else {
buffers[i] = chunks[i].chunk
}
}
this._write(Buffer.concat(buffers), 'binary', cb)
}
return stream
}

61
node_modules/websocket-stream/test-client.js generated vendored Normal file
View File

@ -0,0 +1,61 @@
var ws = require('./')
var test = require('tape')
var Buffer = require('safe-buffer').Buffer
test('echo works', function(t) {
var stream = ws('ws://localhost:8343')
stream.on('data', function(o) {
t.ok(Buffer.isBuffer(o), 'is buffer')
t.equal(o.toString(), 'hello', 'got hello back')
stream.destroy()
t.end()
})
stream.write(Buffer.from('hello'))
})
test('echo works two times', function(t) {
var stream = ws('ws://localhost:8343')
stream.once('data', function(o) {
t.equal(o.toString(), 'hello', 'got first hello back')
stream.write(Buffer.from('hello'))
stream.once('data', function(o) {
t.equal(o.toString(), 'hello', 'got second hello back')
stream.destroy()
t.end()
})
})
stream.write(Buffer.from('hello'))
})
test('with bare WebSocket, strings as strings', function (t) {
var socket = new WebSocket('ws://localhost:8344')
socket.onmessage = function (e) {
var data = e.data
t.ok(typeof data === 'string', 'data must be a string')
socket.close()
t.end()
}
})
test('with bare WebSocket, binary only', function (t) {
var socket = new WebSocket('ws://localhost:8345')
socket.onmessage = function (e) {
var data = e.data
t.notOk(typeof data === 'string', 'data must not be a string')
socket.close()
t.end()
}
})
test('coerce client data as binary', function(t) {
var stream = ws('ws://localhost:8346', { binary: true })
stream.on('data', function(o) {
t.ok(Buffer.isBuffer(o), 'is buffer')
t.equal(o.toString(), 'success', 'success!')
stream.destroy()
t.end()
})
stream.write('hello')
})

56
node_modules/websocket-stream/test-server.js generated vendored Normal file
View File

@ -0,0 +1,56 @@
var http = require('http')
var websocket = require('./')
var echo = require('./echo-server.js')
var WebSocketServer = require('ws').Server
var Buffer = require('safe-buffer').Buffer
echo.start(function(){
console.log('echo server is running')
})
function forBare (opts) {
var server = http.createServer()
websocket.createServer({
server: server,
binary: opts.binary
}, sendString)
server.listen(opts.port)
function sendString (stream) {
stream.write('hello world')
}
}
forBare({
port: 8344,
binary: false
})
forBare({
port: 8345
})
function checkIfDataIsBinary () {
var server = http.createServer()
var wss = new WebSocketServer({
server: server
})
server.listen(8346)
wss.on('connection', waitFor)
function waitFor (ws) {
ws.on('message', function (data) {
if (!Buffer.isBuffer(data)) {
ws.send(Buffer.from('fail'))
} else {
ws.send(Buffer.from('success'))
}
})
}
}
checkIfDataIsBinary()

307
node_modules/websocket-stream/test.js generated vendored Normal file
View File

@ -0,0 +1,307 @@
var test = require('tape')
var websocket = require('./')
var echo = require("./echo-server")
var WebSocketServer = require('ws').Server
var http = require('http')
var concat = require('concat-stream')
var Buffer = require('safe-buffer').Buffer
test('echo server', function(t) {
echo.start(function() {
var client = websocket(echo.url)
client.on('error', console.error)
client.on('data', function(data) {
t.ok(Buffer.isBuffer(data), 'is a buffer')
t.equal(data.toString(), 'hello world')
client.end()
echo.stop(function() {
t.end()
})
})
client.write('hello world')
})
})
test('emitting not connected errors', function(t) {
echo.start(function() {
var client = websocket(echo.url)
client.on('error', function() {
echo.stop(function() {
t.true(true, 'should emit error')
t.end()
})
})
client.once('data', function(data) {
client.end()
client.write('abcde')
})
client.write('hello world')
})
})
test('passes options to websocket constructor', function(t) {
t.plan(3)
opts = {
verifyClient: function verifyClient(info) {
t.equal(info.req.headers['x-custom-header'], 'Custom Value')
return true
}
}
echo.start(opts, function() {
var options = {headers: {'x-custom-header': 'Custom Value'}}
var client = websocket(echo.url, options)
client.on('error', console.error)
client.on('data', function(data) {
t.ok(Buffer.isBuffer(data), 'is a buffer')
t.equal(data.toString(), 'hello world')
client.end()
echo.stop(function() {})
})
client.write('hello world')
})
})
test('destroy', function(t) {
t.plan(1)
echo.start(function() {
var client = websocket(echo.url, echo.options)
client.on('close', function() {
echo.stop(function() {
t.pass('destroyed')
})
})
setTimeout(function() {
client.destroy()
}, 200)
})
})
test('drain', function(t) {
t.plan(1)
echo.start(function() {
var client = websocket(echo.url, echo.options)
client.on('drain', function() {
client.destroy()
echo.stop(function() {
t.pass('drained')
})
})
// write until buffer is full
while (client.write('foobar')) {}
})
})
test('emit sending errors if the socket is closed by the other party', function(t) {
var server = http.createServer()
var wss = new WebSocketServer({ server: server })
server.listen(8344, function() {
var client = websocket('ws://localhost:8344')
wss.on('connection', function(ws) {
var stream = websocket(ws)
client.destroy()
setTimeout(function() {
stream.write('hello world')
}, 50)
stream.on('error', function(err) {
t.ok(err, 'client errors')
server.close(t.end.bind(t))
})
})
})
})
test('destroy client pipe should close server pipe', function(t) {
t.plan(1)
var clientDestroy = function() {
var client = websocket(echo.url, echo.options)
client.on('data', function(o) {
client.destroy()
})
client.write(Buffer.from('hello'))
}
var opts = {}
var server = http.createServer()
opts.server = server
var wss = new WebSocketServer(opts)
wss.on('connection', function(ws) {
var stream = websocket(ws)
stream.on('close', function() {
server.close(function() {
t.pass('close is called')
})
})
stream.pipe(stream)
})
server.listen(echo.port, clientDestroy)
})
test('error on socket should forward it to pipe', function(t) {
t.plan(1)
var clientConnect = function() {
websocket(echo.url, echo.options)
}
var opts = {}
var server = http.createServer()
opts.server = server
var wss = new WebSocketServer(opts)
wss.on('connection', function(ws) {
var stream = websocket(ws)
stream.on('error', function() {
server.close(function() {
t.pass('error is called')
})
})
stream.socket.emit('error', new Error('Fake error'))
})
server.listen(echo.port, clientConnect)
})
test('stream end', function(t) {
t.plan(1)
var server = http.createServer()
websocket.createServer({ server: server }, handle)
function handle (stream) {
stream.pipe(concat(function (body) {
t.equal(body.toString(), 'pizza cats\n')
server.close()
}))
}
server.listen(0, function () {
var w = websocket('ws://localhost:' + server.address().port)
w.end('pizza cats\n')
})
})
test('stream handlers should fire once per connection', function(t) {
t.plan(2)
var server = http.createServer()
var wss = websocket.createServer({ server: server }, function() {
server.close(function() {
t.equal(m, 1)
})
})
var m = 0
wss.on('stream', function(stream, request) {
t.ok(request instanceof http.IncomingMessage)
m++
})
server.listen(0, function() {
var w = websocket('ws://localhost:' + server.address().port)
w.end('pizza cats\n')
})
})
test('client with writev', function(t) {
var server = http.createServer()
var str = ''
var wss = websocket.createServer({
server: server
}, function (stream) {
stream.once('data', function(data) {
t.ok(Buffer.isBuffer(data), 'is a buffer')
t.equal(data.toString(), 'hello world')
stream.once('data', function(data) {
t.ok(Buffer.isBuffer(data), 'is a buffer')
t.equal(data.toString(), str)
stream.end()
server.close()
t.end()
})
})
})
server.listen(8352, function () {
var client = websocket('ws://localhost:8352', {
objectMode: false
})
client.on('error', console.error)
client.once('connect', function () {
client.cork()
do {
str += 'foobar'
} while (client.write('foobar'))
client.uncork()
})
client.write('hello world')
})
})
test('server with writev', function(t) {
var server = http.createServer()
var str = ''
var wss = websocket.createServer({
server: server,
objectMode: false
}, function (stream) {
stream.cork()
do {
str += 'foobar'
} while (stream.write('foobar'))
stream.uncork()
})
server.listen(8352, function () {
var client = websocket('ws://localhost:8352')
client.on('error', console.error)
client.once('data', function(data) {
t.ok(Buffer.isBuffer(data), 'is a buffer')
t.equal(data.toString(), str)
client.end()
server.close()
t.end()
})
})
})
test('stop echo', function(t) {
echo.stop(function() {
t.end()
})
})

12
node_modules/websocket-stream/ws-fallback.js generated vendored Normal file
View File

@ -0,0 +1,12 @@
var ws = null
if (typeof WebSocket !== 'undefined') {
ws = WebSocket
} else if (typeof MozWebSocket !== 'undefined') {
ws = MozWebSocket
} else if (typeof window !== 'undefined') {
ws = window.WebSocket || window.MozWebSocket
}
module.exports = ws