Skip to content

Commit

Permalink
Merge branch 'main' into fix-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BertKleewein authored Jul 6, 2023
2 parents dfee75d + b544a47 commit c08c7c3
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 36 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/mqttjs-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- name: Node Tests

- name: Install Dependencies
run: npm ci

- name: Test NodeJS
run: npm run test:node
env:
CI: true
DEBUG: "mqttjs"

- name: Test Typescript
run: npm run test:typescript
env:
CI: true
DEBUG: "mqttjs"


4 changes: 2 additions & 2 deletions .github/workflows/release-it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
with:
ref: 'main'
fetch-depth: 0 # fetch all commits history to create the changelog
token: ${{ secrets.GITHUB_TOKEN }}
token: ${{ secrets.GH_TOKEN }}

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/[email protected]
Expand All @@ -47,7 +47,7 @@ jobs:

- name: Make the release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
# When all commits since the latest major tag should be added to the changelog, use --git.tagExclude='*[-]*'
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ test/typescript/*.js
test/typescript/*.map
# VS Code stuff
**/typings/**
**/.vscode/**

.npmrc
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"standard.enable": true,
"standard.autoFixOnSave": true,
"editor.defaultFormatter": "standard.vscode-standard"
}
53 changes: 49 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@


# [5.0.0-beta.2](https://github.com/mqttjs/MQTT.js/compare/v4.3.7...v5.0.0-beta.2) (2023-07-03)


### Bug Fixes

* browser tests not working ([#1628](https://github.com/mqttjs/MQTT.js/issues/1628)) ([8775fcd](https://github.com/mqttjs/MQTT.js/commit/8775fcdad952b39fa4b79dbe912ca42033be030a))
* setImmediate polyfill ([#1626](https://github.com/mqttjs/MQTT.js/issues/1626)) ([0ed0754](https://github.com/mqttjs/MQTT.js/commit/0ed0754b95b92df51ed49ae63058b31fdba1d415))


### Features

* option to disable `writeCache` and fix leak in subscriptions ([#1622](https://github.com/mqttjs/MQTT.js/issues/1622)) ([c8aa654](https://github.com/mqttjs/MQTT.js/commit/c8aa6540dbf68ffb0d88c287e2c862b28d3fb6e6)), closes [#1535](https://github.com/mqttjs/MQTT.js/issues/1535) [#1151](https://github.com/mqttjs/MQTT.js/issues/1151)



# [5.0.0-beta.1](https://github.com/mqttjs/MQTT.js/compare/v4.3.7...v5.0.0-beta.2) (2023-06-29)


### Bug Fixes

* `_storeProcessing` staying true after outStore got emptied ([#1492](https://github.com/mqttjs/MQTT.js/issues/1492)) ([f3f7be7](https://github.com/mqttjs/MQTT.js/commit/f3f7be76199115a622fde2590d44b1bb0cf57d41))
* consistency, used `this` instead of `that` ([#1618](https://github.com/mqttjs/MQTT.js/issues/1618)) ([800825b](https://github.com/mqttjs/MQTT.js/commit/800825bf619d83ef713a5b2fa1533bbf6ccac872))
* prevent store message on store when it's restored ([#1255](https://github.com/mqttjs/MQTT.js/issues/1255)) ([8d68c8c](https://github.com/mqttjs/MQTT.js/commit/8d68c8c3e38aede52741a06838933011a6fccc43))



# [5.0.0-beta.0](https://github.com/mqttjs/MQTT.js/compare/v4.3.7...v5.0.0-beta.2) (2023-06-27)


### Bug Fixes

* add missing export of UniqueMessageIdProvider and DefaultMessageIdProvider ([#1572](https://github.com/mqttjs/MQTT.js/issues/1572)) ([aa2e0ad](https://github.com/mqttjs/MQTT.js/commit/aa2e0ad49aadf333141f18cb85d2582abb8e19fc))
* IS_BROWSER check is now safer and more agnostic about the bundler ([#1571](https://github.com/mqttjs/MQTT.js/issues/1571)) ([b48b4b4](https://github.com/mqttjs/MQTT.js/commit/b48b4b4e79690c96033ea2df387c11f3bc26bf6a))
* **test:** `topicAliasMaximum` tests ([#1612](https://github.com/mqttjs/MQTT.js/issues/1612)) ([f1e5518](https://github.com/mqttjs/MQTT.js/commit/f1e5518150ea45067b87104abd9fed64ec13a48c))
* topicAliasMaximum under must be under Connect properties ([#1519](https://github.com/mqttjs/MQTT.js/issues/1519)) ([3b2e1cb](https://github.com/mqttjs/MQTT.js/commit/3b2e1cb7c4bf33ff66bcd1cc3091790a9635f19a))
* **types:** missing null declaration for error in subscription callback ([#1589](https://github.com/mqttjs/MQTT.js/issues/1589)) ([afc067b](https://github.com/mqttjs/MQTT.js/commit/afc067be2ca83990209b6176adec06f9a4c76a2c))
* **types:** topic alias controls and password ([#1509](https://github.com/mqttjs/MQTT.js/issues/1509)) ([85c9341](https://github.com/mqttjs/MQTT.js/commit/85c9341bba2676cfd069ec38a1a7cfda71647b68))


* chore!: drop support for node 12-14 (#1615) ([a2cbf61](https://github.com/mqttjs/MQTT.js/commit/a2cbf61c2a051a5ee69a50e00688e8ace79e7ef5)), closes [#1615](https://github.com/mqttjs/MQTT.js/issues/1615)


### BREAKING CHANGES

* Dropped support for NodeJS 12-14

# [5.0.0-beta.1](https://github.com/mqttjs/MQTT.js/compare/v5.0.0-beta.0...v5.0.0-beta.1) (2023-06-29)


Expand Down Expand Up @@ -739,7 +787,4 @@



# 0.1.0 (2012-01-17)



# 0.1.0 (2012-01-17)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ to use MQTT.js in the browser see the [browserify](#browserify) section
### CommonJS (Require)

```js
const mqtt = require("mqtt"); // require mqtt
const client = mqtt.connect("est.mosquitto.org"); // create a client
const mqtt = require("mqtt") // require mqtt
const client = mqtt.connect("test.mosquitto.org") // create a client
```

### ES6 Modules (Import)
Expand Down
30 changes: 19 additions & 11 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ const validations = require('./validations')
const xtend = require('xtend')
const debug = require('debug')('mqttjs:client')
const nextTick = process ? process.nextTick : function (callback) { setTimeout(callback, 0) }
const setImmediate = global.setImmediate || function (callback) {
// works in node v0.8
nextTick(callback)
const setImmediate = global.setImmediate || function (...args) {
const callback = args.shift()
nextTick(callback.bind(null, ...args))
}

const defaultConnectOptions = {
keepalive: 60,
reschedulePings: true,
Expand All @@ -29,7 +30,8 @@ const defaultConnectOptions = {
reconnectPeriod: 1000,
connectTimeout: 30 * 1000,
clean: true,
resubscribe: true
resubscribe: true,
writeCache: true
}

const socketErrors = [
Expand Down Expand Up @@ -278,6 +280,11 @@ function MqttClient (streamBuilder, options) {

this.options.customHandleAcks = (options.protocolVersion === 5 && options.customHandleAcks) ? options.customHandleAcks : function () { arguments[3](0) }

// Disable pre-generated write cache if requested. Will allocate buffers on-the-fly instead. WARNING: This can affect write performance
if (!this.options.writeCache) {
mqttPacket.writeToStream.cacheNumbers = false
}

this.streamBuilder = streamBuilder

this.messageIdProvider = (typeof this.options.messageIdProvider === 'undefined') ? new DefaultMessageIdProvider() : this.options.messageIdProvider
Expand Down Expand Up @@ -741,7 +748,7 @@ MqttClient.prototype.subscribe = function () {
debug('subscribe: array topic %s', topic)
if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, topic) ||
that._resubscribeTopics[topic].qos < opts.qos ||
resubscribe) {
resubscribe) {
const currentOpts = {
topic: topic,
qos: opts.qos
Expand All @@ -763,7 +770,7 @@ MqttClient.prototype.subscribe = function () {
debug('subscribe: object topic %s', k)
if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, k) ||
that._resubscribeTopics[k].qos < obj[k].qos ||
resubscribe) {
resubscribe) {
const currentOpts = {
topic: k,
qos: obj[k].qos
Expand Down Expand Up @@ -1257,7 +1264,7 @@ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut, noStore) {
* anyway it will result in -1 evaluation
*/
case 0:
/* falls through */
/* falls through */
default:
sendPacket(this, packet, cb)
break
Expand Down Expand Up @@ -1616,7 +1623,7 @@ MqttClient.prototype._handleAck = function (packet) {
debug('_handleAck :: packet type', type)
switch (type) {
case 'pubcomp':
// same thing as puback for QoS 2
// same thing as puback for QoS 2
case 'puback': {
const pubackRC = packet.reasonCode
// Callback - we're done
Expand Down Expand Up @@ -1662,6 +1669,7 @@ MqttClient.prototype._handleAck = function (packet) {
}
}
}
delete this.messageIdToTopic[messageId]
this._invokeStoreProcessingQueue()
cb(null, packet)
break
Expand All @@ -1678,7 +1686,7 @@ MqttClient.prototype._handleAck = function (packet) {
}

if (this.disconnecting &&
Object.keys(this.outgoing).length === 0) {
Object.keys(this.outgoing).length === 0) {
this.emit('outgoingEmpty')
}
}
Expand Down Expand Up @@ -1747,8 +1755,8 @@ MqttClient.prototype._resubscribe = function () {
debug('_resubscribe')
const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics)
if (!this._firstConnection &&
(this.options.clean || (this.options.protocolVersion === 5 && !this.connackPacket.sessionPresent)) &&
_resubscribeTopicsKeys.length > 0) {
(this.options.clean || (this.options.protocolVersion === 5 && !this.connackPacket.sessionPresent)) &&
_resubscribeTopicsKeys.length > 0) {
if (this.options.resubscribe) {
if (this.options.protocolVersion === 5) {
debug('_resubscribe: protocolVersion 5')
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mqtt",
"description": "A library for the MQTT protocol",
"version": "5.0.0-beta.1",
"version": "5.0.0-beta.2",
"contributors": [
"Adam Rudd <[email protected]>",
"Matteo Collina <[email protected]> (https://github.com/mcollina)",
Expand Down Expand Up @@ -123,6 +123,7 @@
"mkdirp": "^0.5.1",
"mocha": "^9.2.0",
"mqtt-connection": "^4.0.0",
"mqtt-level-store": "^3.1.0",
"nyc": "^15.0.1",
"pre-commit": "^1.2.2",
"release-it": "^15.11.0",
Expand All @@ -133,8 +134,7 @@
"standard": "^16.0.4",
"tape": "^5.5.2",
"terser": "^5.14.2",
"typescript": "^4.5.5",
"mqtt-level-store": "^3.1.0"
"typescript": "^4.5.5"
},
"standard": {
"env": [
Expand Down
39 changes: 39 additions & 0 deletions test/abstract_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3313,4 +3313,43 @@ module.exports = function (server, config) {
})
})
})

describe('message id to subscription topic mapping', () => {
it('should not create a mapping if resubscribe is disabled', function (done) {
const client = connect({ resubscribe: false })
client.subscribe('test1')
client.subscribe('test2')
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 0)
client.end(true, done)
})

it('should create a mapping for each subscribe call', function (done) {
const client = connect()
client.subscribe('test1')
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 1)
client.subscribe('test2')
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 2)

client.subscribe(['test3', 'test4'])
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 3)
client.subscribe(['test5', 'test6'])
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 4)

client.end(true, done)
})

it('should remove the mapping after suback', function (done) {
const client = connect()
client.once('connect', function () {
client.subscribe('test1', { qos: 2 }, function () {
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 0)

client.subscribe(['test2', 'test3'], { qos: 2 }, function () {
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 0)
client.end(done)
})
})
})
})
})
}
6 changes: 4 additions & 2 deletions test/browser/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ function start (startPort, done) {
return server
}

const port = process.env.PORT || process.env.AIRTAP_SUPPORT_PORT

if (require.main === module) {
start(process.env.PORT || process.env.AIRTAP_PORT, function (err) {
start(port, function (err) {
if (err) {
console.error(err)
return
}
console.log('tunnelled server started on port', process.env.PORT || process.env.AIRTAP_PORT)
console.log('tunnelled server started on port', port)
})
}
12 changes: 8 additions & 4 deletions test/browser/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ client.on('reconnect', function () {
})

test('MQTT.js browser test', function (t) {
t.plan(2)
t.plan(4)
client.on('connect', function () {
client.on('message', function (msg) {
t.equal(msg, 'Hello World!')
client.on('message', function (topic, msg) {
t.equal(topic, 'hello', 'should match topic')
t.equal(msg.toString(), 'Hello World!', 'should match payload')
client.end(() => {
t.pass('client should close')
})
})
client.subscribe('hello', function () {
}).publish('hello', 'Hello World!')
})
client.once('close', function () {
t.true(true)
t.pass('should emit close')
})
})
Loading

0 comments on commit c08c7c3

Please sign in to comment.