mqtt 5.13.1 → 5.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mqtt",
3
3
  "description": "A library for the MQTT protocol",
4
- "version": "5.13.1",
4
+ "version": "5.13.2",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
7
  "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
@@ -32,8 +32,7 @@
32
32
  "CONTRIBUTING.md",
33
33
  "LICENSE.md",
34
34
  "help/",
35
- "build/",
36
- "src/"
35
+ "build/"
37
36
  ],
38
37
  "exports": {
39
38
  ".": {
@@ -112,6 +111,8 @@
112
111
  "net": false
113
112
  },
114
113
  "dependencies": {
114
+ "@types/readable-stream": "^4.0.18",
115
+ "@types/ws": "^8.18.1",
115
116
  "commist": "^3.2.0",
116
117
  "concat-stream": "^2.0.0",
117
118
  "debug": "^4.4.0",
@@ -132,10 +133,8 @@
132
133
  "@release-it/conventional-changelog": "^7.0.2",
133
134
  "@types/chai": "^4.3.20",
134
135
  "@types/node": "^20.17.16",
135
- "@types/readable-stream": "^4.0.18",
136
136
  "@types/sinon": "^17.0.3",
137
137
  "@types/tape": "^5.8.1",
138
- "@types/ws": "^8.18.1",
139
138
  "@typescript-eslint/eslint-plugin": "^6.21.0",
140
139
  "@typescript-eslint/parser": "^6.21.0",
141
140
  "@web/test-runner": "^0.19.0",
package/src/bin/mqtt.ts DELETED
@@ -1,39 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /*
4
- * Copyright (c) 2015-2015 MQTT.js contributors.
5
- * Copyright (c) 2011-2014 Adam Rudd.
6
- *
7
- * See LICENSE for more information
8
- */
9
- import path from 'path'
10
- import Commist from 'commist'
11
- import help from 'help-me'
12
- import publish from './pub'
13
- import subscribe from './sub'
14
-
15
- // eslint-disable-next-line @typescript-eslint/no-var-requires
16
- const version = require('../../package.json').version
17
-
18
- const helpMe = help({
19
- dir: path.join(__dirname, '../../', 'help'),
20
- ext: '.txt',
21
- })
22
-
23
- const commist = Commist()
24
-
25
- commist.register('publish', publish)
26
- commist.register('pub', publish)
27
-
28
- commist.register('subscribe', subscribe)
29
- commist.register('sub', subscribe)
30
-
31
- commist.register('version', () => {
32
- console.log('MQTT.js version:', version)
33
- })
34
- commist.register('help', helpMe.toStdout)
35
-
36
- if (commist.parse(process.argv.slice(2)) !== null) {
37
- console.log('No such command:', process.argv[2], '\n')
38
- helpMe.toStdout()
39
- }
package/src/bin/pub.ts DELETED
@@ -1,174 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import { Writable } from 'readable-stream'
4
- import path from 'path'
5
- import fs from 'fs'
6
- import concat from 'concat-stream'
7
- import help from 'help-me'
8
-
9
- import minimist, { type ParsedArgs } from 'minimist'
10
- import split2 from 'split2'
11
- import { connect } from '../mqtt'
12
- import { type IClientOptions, type IClientPublishOptions } from 'src/lib/client'
13
- import { pipeline } from 'stream'
14
-
15
- const helpMe = help({
16
- dir: path.join(__dirname, '../../', 'help'),
17
- })
18
-
19
- function send(args: ParsedArgs) {
20
- const client = connect(args as IClientOptions)
21
- client.on('connect', () => {
22
- client.publish(
23
- args.topic,
24
- args.message,
25
- args as IClientPublishOptions,
26
- (err) => {
27
- if (err) {
28
- console.warn(err)
29
- }
30
- client.end()
31
- },
32
- )
33
- })
34
- client.on('error', (err) => {
35
- console.warn(err)
36
- client.end()
37
- })
38
- }
39
-
40
- function multisend(args: ParsedArgs) {
41
- const client = connect(args as IClientOptions)
42
- const sender = new Writable({
43
- objectMode: true,
44
- })
45
- sender._write = (line, enc, cb) => {
46
- client.publish(
47
- args.topic,
48
- line.trim(),
49
- args as IClientPublishOptions,
50
- cb,
51
- )
52
- }
53
-
54
- client.on('connect', () => {
55
- pipeline(process.stdin, split2(), sender, (err) => {
56
- client.end()
57
- if (err) {
58
- throw err
59
- }
60
- })
61
- })
62
- }
63
-
64
- export default function start(args: string[]) {
65
- const parsedArgs = minimist(args, {
66
- string: [
67
- 'hostname',
68
- 'username',
69
- 'password',
70
- 'key',
71
- 'cert',
72
- 'ca',
73
- 'message',
74
- 'clientId',
75
- 'i',
76
- 'id',
77
- ],
78
- boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
79
- alias: {
80
- port: 'p',
81
- hostname: ['h', 'host'],
82
- topic: 't',
83
- message: 'm',
84
- qos: 'q',
85
- clientId: ['i', 'id'],
86
- retain: 'r',
87
- username: 'u',
88
- password: 'P',
89
- stdin: 's',
90
- multiline: 'M',
91
- protocol: ['C', 'l'],
92
- help: 'H',
93
- ca: 'cafile',
94
- },
95
- default: {
96
- host: 'localhost',
97
- qos: 0,
98
- retain: false,
99
- topic: '',
100
- message: '',
101
- },
102
- })
103
-
104
- if (parsedArgs.help) {
105
- return helpMe.toStdout('publish')
106
- }
107
-
108
- if (parsedArgs.key) {
109
- parsedArgs.key = fs.readFileSync(parsedArgs.key)
110
- }
111
-
112
- if (parsedArgs.cert) {
113
- parsedArgs.cert = fs.readFileSync(parsedArgs.cert)
114
- }
115
-
116
- if (parsedArgs.ca) {
117
- parsedArgs.ca = fs.readFileSync(parsedArgs.ca)
118
- }
119
-
120
- if (parsedArgs.key && parsedArgs.cert && !parsedArgs.protocol) {
121
- parsedArgs.protocol = 'mqtts'
122
- }
123
-
124
- if (parsedArgs.port) {
125
- if (typeof parsedArgs.port !== 'number') {
126
- console.warn(
127
- "# Port: number expected, '%s' was given.",
128
- typeof parsedArgs.port,
129
- )
130
- return
131
- }
132
- }
133
-
134
- if (parsedArgs['will-topic']) {
135
- parsedArgs.will = {}
136
- parsedArgs.will.topic = parsedArgs['will-topic']
137
- parsedArgs.will.payload = parsedArgs['will-message']
138
- parsedArgs.will.qos = parsedArgs['will-qos']
139
- parsedArgs.will.retain = parsedArgs['will-retain']
140
- }
141
-
142
- if (parsedArgs.insecure) {
143
- parsedArgs.rejectUnauthorized = false
144
- }
145
-
146
- parsedArgs.topic = (parsedArgs.topic || parsedArgs._.shift())?.toString()
147
- parsedArgs.message = (
148
- parsedArgs.message || parsedArgs._.shift()
149
- )?.toString()
150
-
151
- if (!parsedArgs.topic) {
152
- console.error('missing topic\n')
153
- return helpMe.toStdout('publish')
154
- }
155
-
156
- if (parsedArgs.stdin) {
157
- if (parsedArgs.multiline) {
158
- multisend(parsedArgs)
159
- } else {
160
- process.stdin.pipe(
161
- concat((data) => {
162
- parsedArgs.message = data
163
- send(parsedArgs)
164
- }),
165
- )
166
- }
167
- } else {
168
- send(parsedArgs)
169
- }
170
- }
171
-
172
- if (require.main === module) {
173
- start(process.argv.slice(2))
174
- }
package/src/bin/sub.ts DELETED
@@ -1,146 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import path from 'path'
4
- import fs from 'fs'
5
- import minimist from 'minimist'
6
- import help from 'help-me'
7
- import { connect } from '../mqtt'
8
- import { type IClientOptions } from 'src/lib/client'
9
-
10
- const helpMe = help({
11
- dir: path.join(__dirname, '../../', 'help'),
12
- })
13
-
14
- export default function start(args: string[]) {
15
- const parsedArgs = minimist(args, {
16
- string: [
17
- 'hostname',
18
- 'username',
19
- 'password',
20
- 'key',
21
- 'cert',
22
- 'ca',
23
- 'clientId',
24
- 'i',
25
- 'id',
26
- ],
27
- boolean: ['stdin', 'help', 'clean', 'insecure'],
28
- alias: {
29
- port: 'p',
30
- hostname: ['h', 'host'],
31
- topic: 't',
32
- qos: 'q',
33
- clean: 'c',
34
- keepalive: 'k',
35
- clientId: ['i', 'id'],
36
- username: 'u',
37
- password: 'P',
38
- protocol: ['C', 'l'],
39
- verbose: 'v',
40
- help: '-H',
41
- ca: 'cafile',
42
- },
43
- default: {
44
- host: 'localhost',
45
- qos: 0,
46
- retain: false,
47
- clean: true,
48
- keepAlive: 30, // 30 sec
49
- },
50
- })
51
-
52
- if (parsedArgs.help) {
53
- return helpMe.toStdout('subscribe')
54
- }
55
-
56
- parsedArgs.topic = parsedArgs.topic || parsedArgs._.shift()
57
-
58
- if (!parsedArgs.topic) {
59
- console.error('missing topic\n')
60
- return helpMe.toStdout('subscribe')
61
- }
62
-
63
- if (parsedArgs.key) {
64
- parsedArgs.key = fs.readFileSync(parsedArgs.key)
65
- }
66
-
67
- if (parsedArgs.cert) {
68
- parsedArgs.cert = fs.readFileSync(parsedArgs.cert)
69
- }
70
-
71
- if (parsedArgs.ca) {
72
- parsedArgs.ca = fs.readFileSync(parsedArgs.ca)
73
- }
74
-
75
- if (parsedArgs.key && parsedArgs.cert && !parsedArgs.protocol) {
76
- parsedArgs.protocol = 'mqtts'
77
- }
78
-
79
- if (parsedArgs.insecure) {
80
- parsedArgs.rejectUnauthorized = false
81
- }
82
-
83
- if (parsedArgs.port) {
84
- if (typeof parsedArgs.port !== 'number') {
85
- console.warn(
86
- "# Port: number expected, '%s' was given.",
87
- typeof parsedArgs.port,
88
- )
89
- return
90
- }
91
- }
92
-
93
- if (parsedArgs['will-topic']) {
94
- parsedArgs.will = {}
95
- parsedArgs.will.topic = parsedArgs['will-topic']
96
- parsedArgs.will.payload = parsedArgs['will-message']
97
- parsedArgs.will.qos = parsedArgs['will-qos']
98
- parsedArgs.will.retain = parsedArgs['will-retain']
99
- }
100
-
101
- parsedArgs.keepAlive = parsedArgs['keep-alive']
102
-
103
- const client = connect(parsedArgs as IClientOptions)
104
-
105
- client.on('connect', () => {
106
- client.subscribe(
107
- parsedArgs.topic,
108
- { qos: parsedArgs.qos },
109
- (err, result) => {
110
- if (err) {
111
- console.error(err)
112
- process.exit(1)
113
- }
114
-
115
- result.forEach((sub) => {
116
- if (sub.qos > 2) {
117
- console.error(
118
- 'subscription negated to',
119
- sub.topic,
120
- 'with code',
121
- sub.qos,
122
- )
123
- process.exit(1)
124
- }
125
- })
126
- },
127
- )
128
- })
129
-
130
- client.on('message', (topic, payload) => {
131
- if (parsedArgs.verbose) {
132
- console.log(topic, payload.toString())
133
- } else {
134
- console.log(payload.toString())
135
- }
136
- })
137
-
138
- client.on('error', (err) => {
139
- console.warn(err)
140
- client.end()
141
- })
142
- }
143
-
144
- if (require.main === module) {
145
- start(process.argv.slice(2))
146
- }
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- import * as mqtt from './mqtt'
2
-
3
- export default mqtt
4
- export * from './mqtt'
@@ -1,115 +0,0 @@
1
- import { Duplex, type Transform } from 'readable-stream'
2
- import { Buffer } from 'buffer'
3
- import { type IClientOptions } from './client'
4
-
5
- /**
6
- * Utils writev function for browser, ensure to write Buffers to socket (convert strings).
7
- */
8
- export function writev(
9
- chunks: { chunk: any; encoding: string }[],
10
- cb: (err?: Error) => void,
11
- ) {
12
- const buffers = new Array(chunks.length)
13
- for (let i = 0; i < chunks.length; i++) {
14
- if (typeof chunks[i].chunk === 'string') {
15
- buffers[i] = Buffer.from(chunks[i].chunk, 'utf8')
16
- } else {
17
- buffers[i] = chunks[i].chunk
18
- }
19
- }
20
-
21
- this._write(Buffer.concat(buffers), 'binary', cb)
22
- }
23
-
24
- /**
25
- * How this works:
26
- * - `socket` is the `WebSocket` instance, the connection to our broker.
27
- * - `proxy` is a `Transform`, it ensure data written to the `socket` is a `Buffer`.
28
- * This class buffers the data written to the `proxy` (so then to `socket`) until the `socket` is ready.
29
- * The stream returned from this class, will be passed to the `MqttClient`.
30
- */
31
- export class BufferedDuplex extends Duplex {
32
- public socket: WebSocket
33
-
34
- private proxy: Transform
35
-
36
- private isSocketOpen: boolean
37
-
38
- private writeQueue: Array<{
39
- chunk: any
40
- encoding: string
41
- cb: (err?: Error) => void
42
- }>
43
-
44
- constructor(opts: IClientOptions, proxy: Transform, socket: WebSocket) {
45
- super({
46
- objectMode: true,
47
- })
48
- this.proxy = proxy
49
- this.socket = socket
50
- this.writeQueue = []
51
-
52
- if (!opts.objectMode) {
53
- this._writev = writev.bind(this)
54
- }
55
-
56
- this.isSocketOpen = false
57
-
58
- this.proxy.on('data', (chunk) => {
59
- if (!this.destroyed && this.readable) {
60
- this.push(chunk)
61
- }
62
- })
63
- }
64
-
65
- _read(size?: number): void {
66
- this.proxy.read(size)
67
- }
68
-
69
- _write(chunk: any, encoding: string, cb: (err?: Error) => void) {
70
- if (!this.isSocketOpen) {
71
- // Buffer the data in a queue
72
- this.writeQueue.push({ chunk, encoding, cb })
73
- } else {
74
- this.writeToProxy(chunk, encoding, cb)
75
- }
76
- }
77
-
78
- _final(callback: (error?: Error) => void): void {
79
- this.writeQueue = []
80
- this.proxy.end(callback)
81
- }
82
-
83
- _destroy(err: Error, callback: (error: Error) => void): void {
84
- this.writeQueue = []
85
- // do not pass error here otherwise we should listen for `error` event on proxy to prevent uncaught exception
86
- this.proxy.destroy()
87
- callback(err)
88
- }
89
-
90
- /** Method to call when socket is ready to stop buffering writes */
91
- socketReady() {
92
- this.emit('connect')
93
- this.isSocketOpen = true
94
- this.processWriteQueue()
95
- }
96
-
97
- private writeToProxy(
98
- chunk: any,
99
- encoding: string,
100
- cb: (err?: Error) => void,
101
- ) {
102
- if (this.proxy.write(chunk, encoding) === false) {
103
- this.proxy.once('drain', cb)
104
- } else {
105
- cb()
106
- }
107
- }
108
-
109
- private processWriteQueue() {
110
- while (this.writeQueue.length > 0) {
111
- const { chunk, encoding, cb } = this.writeQueue.shift()!
112
- this.writeToProxy(chunk, encoding, cb)
113
- }
114
- }
115
- }
@@ -1,112 +0,0 @@
1
- import type MqttClient from './client'
2
- import getTimer, { type Timer } from './get-timer'
3
- import type { TimerVariant } from './shared'
4
-
5
- export default class KeepaliveManager {
6
- private _keepalive: number
7
-
8
- private timerId: number
9
-
10
- private timer: Timer
11
-
12
- private destroyed = false
13
-
14
- private counter: number
15
-
16
- private client: MqttClient
17
-
18
- private _keepaliveTimeoutTimestamp: number
19
-
20
- private _intervalEvery: number
21
-
22
- /** Timestamp of next keepalive timeout */
23
- get keepaliveTimeoutTimestamp() {
24
- return this._keepaliveTimeoutTimestamp
25
- }
26
-
27
- /** Milliseconds of the actual interval */
28
- get intervalEvery() {
29
- return this._intervalEvery
30
- }
31
-
32
- get keepalive() {
33
- return this._keepalive
34
- }
35
-
36
- constructor(client: MqttClient, variant: TimerVariant | Timer) {
37
- this.client = client
38
- this.timer =
39
- typeof variant === 'object' &&
40
- 'set' in variant &&
41
- 'clear' in variant
42
- ? variant
43
- : getTimer(variant)
44
- this.setKeepalive(client.options.keepalive)
45
- }
46
-
47
- private clear() {
48
- if (this.timerId) {
49
- this.timer.clear(this.timerId)
50
- this.timerId = null
51
- }
52
- }
53
-
54
- /** Change the keepalive */
55
- setKeepalive(value: number) {
56
- // keepalive is in seconds
57
- value *= 1000
58
-
59
- if (
60
- // eslint-disable-next-line no-restricted-globals
61
- isNaN(value) ||
62
- value <= 0 ||
63
- value > 2147483647
64
- ) {
65
- throw new Error(
66
- `Keepalive value must be an integer between 0 and 2147483647. Provided value is ${value}`,
67
- )
68
- }
69
-
70
- this._keepalive = value
71
-
72
- this.reschedule()
73
-
74
- this.client['log'](`KeepaliveManager: set keepalive to ${value}ms`)
75
- }
76
-
77
- destroy() {
78
- this.clear()
79
- this.destroyed = true
80
- }
81
-
82
- reschedule() {
83
- if (this.destroyed) {
84
- return
85
- }
86
-
87
- this.clear()
88
- this.counter = 0
89
-
90
- // https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Figure_3.5_Keep
91
- const keepAliveTimeout = Math.ceil(this._keepalive * 1.5)
92
-
93
- this._keepaliveTimeoutTimestamp = Date.now() + keepAliveTimeout
94
- this._intervalEvery = Math.ceil(this._keepalive / 2)
95
-
96
- this.timerId = this.timer.set(() => {
97
- // this should never happen, but just in case
98
- if (this.destroyed) {
99
- return
100
- }
101
-
102
- this.counter += 1
103
-
104
- // after keepalive seconds, send a pingreq
105
- if (this.counter === 2) {
106
- this.client.sendPing()
107
- } else if (this.counter > 2) {
108
- this.client.onKeepaliveTimeout()
109
- }
110
- }, this._intervalEvery)
111
- }
112
- }
@@ -1,71 +0,0 @@
1
- import EventEmitter from 'events'
2
- import { applyMixin } from './shared'
3
-
4
- export type EventHandler =
5
- // Add more overloads as necessary
6
- | ((arg1: any, arg2: any, arg3: any, arg4: any) => void)
7
- | ((arg1: any, arg2: any, arg3: any) => void)
8
- | ((arg1: any, arg2: any) => void)
9
- | ((arg1: any) => void)
10
- | ((...args: any[]) => void)
11
-
12
- export interface TypedEventEmitter<
13
- TEvents extends Record<keyof TEvents, EventHandler>,
14
- > {
15
- on<TEvent extends keyof TEvents>(
16
- event: TEvent,
17
- callback: TEvents[TEvent],
18
- ): this
19
- once<TEvent extends keyof TEvents>(
20
- event: TEvent,
21
- callback: TEvents[TEvent],
22
- ): this
23
- prependListener<TEvent extends keyof TEvents>(
24
- event: TEvent,
25
- callback: TEvents[TEvent],
26
- ): this
27
- prependOnceListener<TEvent extends keyof TEvents>(
28
- event: TEvent,
29
- callback: TEvents[TEvent],
30
- ): this
31
-
32
- removeListener<TEvent extends keyof TEvents>(
33
- event: TEvent,
34
- callback: TEvents[TEvent],
35
- ): this
36
- off<TEvent extends keyof TEvents>(
37
- event: TEvent,
38
- callback: TEvents[TEvent],
39
- ): this
40
-
41
- removeAllListeners(event?: keyof TEvents): this
42
-
43
- emit<TEvent extends keyof TEvents>(
44
- event: TEvent,
45
- ...args: Parameters<TEvents[TEvent]>
46
- ): boolean
47
-
48
- setMaxListeners(n: number): this
49
- getMaxListeners(): number
50
-
51
- listeners<TEvent extends keyof TEvents>(
52
- eventName: TEvent,
53
- ): TEvents[TEvent][]
54
- rawListeners<TEvent extends keyof TEvents>(
55
- eventName: TEvent,
56
- ): TEvents[TEvent][]
57
- listenerCount<TEvent extends keyof TEvents>(
58
- event: TEvent,
59
- listener?: TEvents[TEvent],
60
- ): number
61
-
62
- eventNames(): Array<keyof TEvents>
63
- }
64
-
65
- // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
66
- export class TypedEventEmitter<
67
- TEvents extends Record<keyof TEvents, EventHandler>,
68
- > {}
69
-
70
- // Make TypedEventEmitter inherit from EventEmitter without actually extending
71
- applyMixin(TypedEventEmitter, EventEmitter)