From e8b0ca79191d2c1fdd24f70e561488db01c1e04d Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Tue, 14 Nov 2023 09:02:54 +0100 Subject: [PATCH 1/3] fix: add default export This allows to also import mqtt on NodeJS like on browsers: `import mqtt from 'mqtt'` --- esbuild.js | 2 +- example.js | 12 ------------ example.ts | 25 +++++++++++++++++++++++++ package.json | 10 +++++----- src/index.ts | 4 ++++ 5 files changed, 35 insertions(+), 18 deletions(-) delete mode 100644 example.js create mode 100644 example.ts create mode 100644 src/index.ts diff --git a/esbuild.js b/esbuild.js index 0ed9b48f6..e2651fe25 100644 --- a/esbuild.js +++ b/esbuild.js @@ -9,7 +9,7 @@ const outdir = 'dist' * @type {import('esbuild').BuildOptions} */ const options = { - entryPoints: ['build/mqtt.js'], + entryPoints: ['build/index.js'], bundle: true, outfile: `${outdir}/mqtt.js`, format: 'iife', diff --git a/example.js b/example.js deleted file mode 100644 index 7aa831230..000000000 --- a/example.js +++ /dev/null @@ -1,12 +0,0 @@ -const mqtt = require('.') - -const client = mqtt.connect('mqtt://test.mosquitto.org') - -client.subscribe('presence') -client.publish('presence', 'Hello mqtt') - -client.on('message', (topic, message) => { - console.log(message.toString()) -}) - -client.end() diff --git a/example.ts b/example.ts new file mode 100644 index 000000000..78142070f --- /dev/null +++ b/example.ts @@ -0,0 +1,25 @@ +import mqtt from '.' + +const client = mqtt.connect('mqtt://test.mosquitto.org') + +const topic = 'presence' + +client.subscribe(topic, (err) => { + if (!err) { + console.log('subscribed to', topic) + client.publish(topic, 'Hello mqtt', (err) => { + if (!err) { + console.log('message published') + } else { + console.error(err) + } + }) + } else { + console.error(err) + } +}) + +client.on('message', (topic, message) => { + console.log('received message "%s" from topic "%s"', message, topic) + client.end() +}) diff --git a/package.json b/package.json index 41c4ff11d..a9bb6f606 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "type": "git", "url": "git://github.com/mqttjs/MQTT.js.git" }, - "main": "./build/mqtt.js", + "main": "./build/index.js", "module": "./dist/mqtt.esm.js", "bin": { "mqtt_pub": "./build/bin/pub.js", @@ -39,20 +39,20 @@ ".": { "browser": { "import": "./dist/mqtt.esm.js", - "default": "./dist/mqtt.js" + "default": "./dist/index.js" }, - "default": "./build/mqtt.js" + "default": "./build/index.js" }, "./package.json": "./package.json", "./*.map": "./build/*.js.map", "./dist/*": "./dist/*.js", "./*": "./build/*.js" }, - "types": "build/mqtt.d.ts", + "types": "build/index.d.ts", "typesVersions": { "*": { "*": [ - "./build/mqtt.d.ts" + "./build/index.d.ts" ] } }, diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 000000000..22f29ba81 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,4 @@ +import * as mqtt from './mqtt' + +export default mqtt +export * from './mqtt' From 609debf68b4ccced93c7c0315e78e862fc39d768 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Tue, 14 Nov 2023 09:10:57 +0100 Subject: [PATCH 2/3] fix: use default export in tests --- test/abstract_client.ts | 5 ++--- test/abstract_store.ts | 6 +----- test/browser/test.js | 2 +- test/client.ts | 2 +- test/client_mqtt5.ts | 3 +-- test/message-id-provider.ts | 3 +-- test/mqtt.ts | 3 +-- test/mqtt_store.ts | 2 +- test/secure_client.ts | 2 +- test/unique_message_id_provider_client.ts | 3 +-- test/websocket_client.ts | 3 +-- 11 files changed, 12 insertions(+), 22 deletions(-) diff --git a/test/abstract_client.ts b/test/abstract_client.ts index 7d876b79b..d51063548 100644 --- a/test/abstract_client.ts +++ b/test/abstract_client.ts @@ -5,19 +5,18 @@ import { assert } from 'chai' import sinon from 'sinon' import fs from 'fs' import levelStore from 'mqtt-level-store' -import * as mqtt from '../src/mqtt' import Store from '../src/lib/store' import serverBuilder from './server_helpers_for_client_tests' import handlePubrel from '../src/lib/handlers/pubrel' import handle from '../src/lib/handlers/index' import handlePublish from '../src/lib/handlers/publish' -import { +import mqtt, { IClientOptions, IClientPublishOptions, IClientSubscribeOptions, ISubscriptionMap, ISubscriptionRequest, -} from '../src/lib/client' +} from '../src' import { IPublishPacket, IPubrelPacket, ISubackPacket, QoS } from 'mqtt-packet' import { DoneCallback, ErrorWithReasonCode } from 'src/lib/shared' import { fail } from 'assert' diff --git a/test/abstract_store.ts b/test/abstract_store.ts index 3a1ea4a45..dc3ab81da 100644 --- a/test/abstract_store.ts +++ b/test/abstract_store.ts @@ -1,5 +1,5 @@ import { IPublishPacket, IPubrelPacket } from 'mqtt-packet' -import { IStore } from '../src/lib/store' +import { IStore } from '../src' import 'should' import { it, beforeEach, afterEach } from 'node:test' @@ -8,8 +8,6 @@ export default function abstractStoreTest( ) { let store: IStore - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error beforeEach((_ctx, done) => { build((err, _store) => { store = _store @@ -17,8 +15,6 @@ export default function abstractStoreTest( }) }) - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error afterEach((_ctx, done) => { store.close(done) }) diff --git a/test/browser/test.js b/test/browser/test.js index ce73eb0ed..623c33e7e 100644 --- a/test/browser/test.js +++ b/test/browser/test.js @@ -2,7 +2,7 @@ import { expect } from '@esm-bundle/chai'; import mqtt from '../../'; // this will resolve to mqtt/dist/mqtt.esm.js // needed to test no-esm version /dist/mqtt.js -/** @type { import('../../src/mqtt').MqttClient }*/ +/** @type { import('../../src').MqttClient }*/ const mqtt2 = window.mqtt function run(proto, port, cb) { diff --git a/test/client.ts b/test/client.ts index 06696bc16..706219be8 100644 --- a/test/client.ts +++ b/test/client.ts @@ -1,4 +1,4 @@ -import * as mqtt from '../src/mqtt' +import mqtt from '../src' import { assert } from 'chai' import { fork } from 'child_process' import path from 'path' diff --git a/test/client_mqtt5.ts b/test/client_mqtt5.ts index 6e1d709ca..1aab5cd96 100644 --- a/test/client_mqtt5.ts +++ b/test/client_mqtt5.ts @@ -1,10 +1,9 @@ import { assert } from 'chai' -import * as mqtt from '../src/mqtt' import abstractClientTests from './abstract_client' import { MqttServer } from './server' import serverBuilder from './server_helpers_for_client_tests' import getPorts from './helpers/port_list' -import { ErrorWithReasonCode } from '../src/lib/shared' +import mqtt, { ErrorWithReasonCode } from '../src' import { after, describe, it } from 'node:test' const ports = getPorts(1) diff --git a/test/message-id-provider.ts b/test/message-id-provider.ts index c45265b06..897b5f269 100644 --- a/test/message-id-provider.ts +++ b/test/message-id-provider.ts @@ -1,6 +1,5 @@ import { assert } from 'chai' -import DefaultMessageIdProvider from '../src/lib/default-message-id-provider' -import UniqueMessageIdProvider from '../src/lib/unique-message-id-provider' +import { DefaultMessageIdProvider, UniqueMessageIdProvider } from '../src' import { describe, it } from 'node:test' describe('message id provider', () => { diff --git a/test/mqtt.ts b/test/mqtt.ts index 77df013e6..6ba21a350 100644 --- a/test/mqtt.ts +++ b/test/mqtt.ts @@ -1,7 +1,6 @@ import fs from 'fs' import path from 'path' -import * as mqtt from '../src/mqtt' -import { IClientOptions } from '../src/lib/client' +import mqtt, { IClientOptions } from '../src' import { describe, it } from 'node:test' import 'should' diff --git a/test/mqtt_store.ts b/test/mqtt_store.ts index 62eeab752..157158fad 100644 --- a/test/mqtt_store.ts +++ b/test/mqtt_store.ts @@ -1,4 +1,4 @@ -import Store from '../src/lib/store' +import { Store } from '../src' import { describe, it } from 'node:test' import 'should' diff --git a/test/secure_client.ts b/test/secure_client.ts index eec731561..ba81b5413 100644 --- a/test/secure_client.ts +++ b/test/secure_client.ts @@ -1,6 +1,6 @@ import path from 'path' import fs from 'fs' -import * as mqtt from '../src/mqtt' +import mqtt from '../src' import abstractClientTests from './abstract_client' import { MqttSecureServer, MqttServerListener } from './server' import { assert } from 'chai' diff --git a/test/unique_message_id_provider_client.ts b/test/unique_message_id_provider_client.ts index eaedfb351..3d027b49f 100644 --- a/test/unique_message_id_provider_client.ts +++ b/test/unique_message_id_provider_client.ts @@ -1,8 +1,7 @@ import abstractClientTests from './abstract_client' import serverBuilder from './server_helpers_for_client_tests' -import UniqueMessageIdProvider from '../src/lib/unique-message-id-provider' +import { UniqueMessageIdProvider, IClientOptions } from '../src' import getPorts from './helpers/port_list' -import { IClientOptions } from 'src/mqtt' import { describe, after } from 'node:test' const ports = getPorts(3) diff --git a/test/websocket_client.ts b/test/websocket_client.ts index af37c5839..87c6866ab 100644 --- a/test/websocket_client.ts +++ b/test/websocket_client.ts @@ -5,8 +5,7 @@ import assert from 'assert' import abstractClientTests from './abstract_client' import getPorts from './helpers/port_list' import { MqttServerNoWait } from './server' -import * as mqtt from '../src/mqtt' -import { IClientOptions } from '../src/lib/client' +import mqtt, { IClientOptions } from '../src' import { after, describe, it } from 'node:test' const ports = getPorts(4) From fe2a995e7ea21a640292783f8f7975d6a4158186 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Tue, 14 Nov 2023 09:16:29 +0100 Subject: [PATCH 3/3] fix: build --- example.ts | 12 ++++++------ tsconfig.build.json | 2 +- tsconfig.json | 1 + 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/example.ts b/example.ts index 78142070f..6800abad4 100644 --- a/example.ts +++ b/example.ts @@ -2,16 +2,16 @@ import mqtt from '.' const client = mqtt.connect('mqtt://test.mosquitto.org') -const topic = 'presence' +const testTopic = 'presence' -client.subscribe(topic, (err) => { +client.subscribe(testTopic, (err) => { if (!err) { - console.log('subscribed to', topic) - client.publish(topic, 'Hello mqtt', (err) => { - if (!err) { + console.log('subscribed to', testTopic) + client.publish(testTopic, 'Hello mqtt', (err2) => { + if (!err2) { console.log('message published') } else { - console.error(err) + console.error(err2) } }) } else { diff --git a/tsconfig.build.json b/tsconfig.build.json index a042068ba..c97188e8a 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,5 +1,5 @@ { "extends": "./tsconfig.json", - "exclude": ["node_modules", "test", "dist", "build"], + "exclude": ["node_modules", "test", "dist", "build", "example.ts"], } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 5633fc730..ad7da5735 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,6 +25,7 @@ "include": [ "src", "test", + "example.ts" ], "exclude": [ "dist",