Fix StackOverflow avoidance (#4)
This commit is contained in:
1 parent
df74989fdd
commit
bf4e69e2ce
3 files changed
+46
-24
No files matched your search
@@ -926,15 +926,13 @@ MQTT.prototype.packetHandler = function(data) {
|
||||
this.partData = data;
|
||||
return;
|
||||
}
|
||||
// Get the data for this packet
|
||||
var pData = data.slice(1 + dLen.lenBy, pLen);
|
||||
|
||||
// Avoid an infinite data emit loop
|
||||
if (pData.length < 1) {
|
||||
this.partData = data;
|
||||
// Allow zero-size pData, but detect if its indexes
|
||||
// will go out of bounds.
|
||||
if (1 + dLen.lenBy >= data.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the data for this packet
|
||||
var pData = data.slice(1 + dLen.lenBy, pLen);
|
||||
// more than one packet? re-emit it so we handle it later
|
||||
if (data.length > pLen) {
|
||||
this.client.emit("data", data.slice(pLen, data.length));
|
||||
@@ -1095,7 +1093,7 @@ MQTT.prototype.packetHandler = function(data) {
|
||||
retain: bool // the server should retain this message and send it out again to new subscribers
|
||||
dup : bool // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
|
||||
}
|
||||
*/ MQTT.prototype.publish = function(topic, message, opts) {
|
||||
*/ MQTT.prototype.publish = function(topic, message, opts) {
|
||||
if (!this.client) return;
|
||||
opts = opts || {};
|
||||
try {
|
||||
|
||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "flic2hass",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "flic2hass",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@swc/cli": "^0.1.62",
|
||||
|
||||
+38
-14
@@ -57,7 +57,7 @@ var pakId = Math.floor(Math.random() * 65534);
|
||||
|
||||
Uint8Array.prototype.charCodeAt = function (a, b) {
|
||||
return this.toString().charCodeAt(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
Return Codes
|
||||
@@ -136,7 +136,7 @@ Object.prototype.on = function (type, fn) {
|
||||
__listeners[type] = [];
|
||||
}
|
||||
__listeners[type].push(fn);
|
||||
}
|
||||
};
|
||||
|
||||
Object.prototype.emit = function (type, ...data) {
|
||||
if (__listeners[type]) {
|
||||
@@ -144,12 +144,12 @@ Object.prototype.emit = function (type, ...data) {
|
||||
fn(...data);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (!Buffer.from) {
|
||||
Buffer.from = function (a) {
|
||||
return new Buffer(a);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** 'public' constants here */
|
||||
@@ -181,9 +181,9 @@ export function mqttStr(s) {
|
||||
export function mqttInt2Str(arr: number[] | Uint8Array): string {
|
||||
let outStr = "";
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
outStr += createEscapedHex(arr[i])
|
||||
outStr += createEscapedHex(arr[i]);
|
||||
}
|
||||
return outStr
|
||||
return outStr;
|
||||
}
|
||||
|
||||
/** MQTT packet length formatter - algorithm from reference docs */
|
||||
@@ -211,8 +211,8 @@ function mqttPacketLengthDec(length) {
|
||||
do {
|
||||
lb = length[bytes];
|
||||
decL |= (lb & 127) << (bytes++ * 7);
|
||||
} while ((lb & 128) && (bytes < 4))
|
||||
return { "decLen": decL, "lenBy": bytes };
|
||||
} while ((lb & 128) && (bytes < 4));
|
||||
return {"decLen": decL, "lenBy": bytes};
|
||||
}
|
||||
|
||||
/** MQTT standard packet formatter */
|
||||
@@ -310,8 +310,16 @@ MQTT.prototype.packetHandler = function (data) {
|
||||
this.partData = data;
|
||||
return;
|
||||
}
|
||||
|
||||
// Allow zero-size pData, but detect if its indexes
|
||||
// will go out of bounds.
|
||||
if (1 + dLen.lenBy >= data.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the data for this packet
|
||||
var pData = data.slice(1 + dLen.lenBy, pLen);
|
||||
|
||||
// more than one packet? re-emit it so we handle it later
|
||||
if (data.length > pLen) {
|
||||
this.client.emit('data', data.slice(pLen, data.length));
|
||||
@@ -402,7 +410,7 @@ MQTT.prototype.connect = function (client) {
|
||||
var onConnect = function () {
|
||||
mqo.client = client;
|
||||
// write connection message
|
||||
var teststring = mqo.mqttConnect(mqo.client_id)
|
||||
var teststring = mqo.mqttConnect(mqo.client_id);
|
||||
client.write(teststring);
|
||||
// handle connection timeout if too slow
|
||||
mqo.ctimo = setTimeout(function () {
|
||||
@@ -422,7 +430,7 @@ MQTT.prototype.connect = function (client) {
|
||||
} else {
|
||||
try {
|
||||
var self = this;
|
||||
client = require("net").Socket().connect({ host: mqo.server, port: mqo.port }, onConnect);
|
||||
client = require("net").Socket().connect({host: mqo.server, port: mqo.port}, onConnect);
|
||||
client.on('error', function (err) {
|
||||
self.emit('error', err.message);
|
||||
});
|
||||
@@ -463,7 +471,7 @@ MQTT.prototype.disconnect = function () {
|
||||
retain: bool // the server should retain this message and send it out again to new subscribers
|
||||
dup : bool // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
|
||||
}
|
||||
*/
|
||||
*/
|
||||
MQTT.prototype.publish = function (topic, message, opts) {
|
||||
if (!this.client) return;
|
||||
opts = opts || {};
|
||||
@@ -577,34 +585,50 @@ MQTT.prototype.mqttConnect = function (clean) {
|
||||
/** This is 'exported' so it can be used with `require('MQTT.js').create(server, options)` */
|
||||
export function create(server, options): MQTT {
|
||||
return new MQTT(server, options);
|
||||
};
|
||||
}
|
||||
|
||||
export function connect(options): MQTT {
|
||||
var mqtt = new MQTT(options.host, options);
|
||||
mqtt.connect();
|
||||
return mqtt;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export interface MQTT {
|
||||
connect(): void;
|
||||
|
||||
publish(topic: string, message: string, opts: {
|
||||
retain?: boolean, // the server should retain this message and send it out again to new subscribers
|
||||
dup?: boolean, // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
|
||||
} | undefined): void;
|
||||
|
||||
subscribe(topics: string[]): void;
|
||||
|
||||
unsubscribe(topic: string): void;
|
||||
|
||||
on(ev: 'publish', cb: (data: { topic: Buffer, message: Buffer }) => void): void;
|
||||
|
||||
on(ev: 'message', cb: (topic: string, message: string) => void): void;
|
||||
|
||||
on(ev: 'puback', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'pubcomp', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'subscribed_fail', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'subscribed', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'unsubscribed', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'ping_reply', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'connected', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'connect', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'error', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'disconnected', cb: (data) => void): void;
|
||||
|
||||
on(ev: 'close', cb: (data) => void): void;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user