mqtt-plus 1.3.0 → 1.4.0

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.
Files changed (64) hide show
  1. package/AGENTS.md +135 -0
  2. package/CHANGELOG.md +19 -0
  3. package/README.md +10 -961
  4. package/doc/mqtt-plus-api.md +682 -0
  5. package/doc/mqtt-plus-broker-setup.md +152 -0
  6. package/doc/mqtt-plus-communication.md +68 -0
  7. package/doc/mqtt-plus-internals.md +68 -0
  8. package/dst-stage1/mqtt-plus-auth.d.ts +1 -1
  9. package/dst-stage1/mqtt-plus-base.d.ts +3 -3
  10. package/dst-stage1/mqtt-plus-codec.d.ts +2 -2
  11. package/dst-stage1/mqtt-plus-encode.d.ts +1 -1
  12. package/dst-stage1/mqtt-plus-encode.js +4 -3
  13. package/dst-stage1/mqtt-plus-error.d.ts +37 -0
  14. package/dst-stage1/mqtt-plus-error.js +262 -0
  15. package/dst-stage1/mqtt-plus-event.d.ts +3 -3
  16. package/dst-stage1/mqtt-plus-event.js +41 -45
  17. package/dst-stage1/mqtt-plus-info.d.ts +1 -1
  18. package/dst-stage1/mqtt-plus-meta.d.ts +1 -1
  19. package/dst-stage1/mqtt-plus-msg.d.ts +1 -1
  20. package/dst-stage1/mqtt-plus-msg.js +2 -2
  21. package/dst-stage1/mqtt-plus-options.d.ts +1 -1
  22. package/dst-stage1/mqtt-plus-service.d.ts +4 -3
  23. package/dst-stage1/mqtt-plus-service.js +87 -111
  24. package/dst-stage1/mqtt-plus-sink.d.ts +10 -6
  25. package/dst-stage1/mqtt-plus-sink.js +262 -264
  26. package/dst-stage1/mqtt-plus-source.d.ts +11 -5
  27. package/dst-stage1/mqtt-plus-source.js +209 -209
  28. package/dst-stage1/mqtt-plus-trace.d.ts +1 -1
  29. package/dst-stage1/mqtt-plus-trace.js +2 -2
  30. package/dst-stage1/mqtt-plus-util.d.ts +8 -6
  31. package/dst-stage1/mqtt-plus-util.js +98 -56
  32. package/dst-stage1/mqtt-plus-version.js +1 -1
  33. package/dst-stage1/tsc.tsbuildinfo +1 -1
  34. package/dst-stage2/mqtt-plus.cjs.js +865 -634
  35. package/dst-stage2/mqtt-plus.esm.js +865 -634
  36. package/dst-stage2/mqtt-plus.umd.js +12 -11
  37. package/etc/knip.jsonc +8 -4
  38. package/etc/stx.conf +3 -4
  39. package/package.json +12 -8
  40. package/src/mqtt-plus-auth.ts +6 -6
  41. package/src/mqtt-plus-base.ts +7 -8
  42. package/src/mqtt-plus-codec.ts +4 -4
  43. package/src/mqtt-plus-encode.ts +7 -6
  44. package/src/mqtt-plus-error.ts +368 -0
  45. package/src/mqtt-plus-event.ts +54 -55
  46. package/src/mqtt-plus-info.ts +1 -1
  47. package/src/mqtt-plus-meta.ts +2 -2
  48. package/src/mqtt-plus-msg.ts +7 -7
  49. package/src/mqtt-plus-options.ts +4 -4
  50. package/src/mqtt-plus-service.ts +113 -131
  51. package/src/mqtt-plus-sink.ts +320 -317
  52. package/src/mqtt-plus-source.ts +249 -256
  53. package/src/mqtt-plus-trace.ts +9 -9
  54. package/src/mqtt-plus-util.ts +113 -71
  55. package/src/mqtt-plus-version.ts +1 -1
  56. package/tst/mqtt-plus-0-fixture.ts +122 -0
  57. package/tst/{mqtt-plus-mosquitto.ts → mqtt-plus-0-mosquitto.ts} +1 -0
  58. package/tst/mqtt-plus-1-api.spec.ts +138 -0
  59. package/tst/mqtt-plus-2-event.spec.ts +148 -0
  60. package/tst/mqtt-plus-3-service.spec.ts +222 -0
  61. package/tst/mqtt-plus-4-sink.spec.ts +174 -0
  62. package/tst/mqtt-plus-5-source.spec.ts +128 -0
  63. package/tst/mqtt-plus-6-misc.spec.ts +194 -0
  64. package/tst/mqtt-plus.spec.ts +0 -435
package/AGENTS.md ADDED
@@ -0,0 +1,135 @@
1
+
2
+ AGENTS
3
+ ======
4
+
5
+ This file provides guidance to Agentic AI Coding Tools when working with code in this repository.
6
+
7
+ Project Overview
8
+ ----------------
9
+
10
+ MQTT+ (`mqtt-plus`) is a TypeScript library implementing four MQTT communication patterns
11
+ with full type safety: Event Emission, Service Call (RPC), Source Fetch, and Sink Push.
12
+ It uses `mqtt` as a peer dependency and builds to ESM, CJS, and UMD formats.
13
+
14
+ Commands
15
+ --------
16
+
17
+ Build and development commands use STX (`@rse/stx`) as the task runner:
18
+
19
+ ```bash
20
+ npm start lint # type-check (tsc), lint (eslint), dead-code check (knip)
21
+ npm start build # lint + vite bundle (ESM, CJS, UMD into dst-stage2/)
22
+ npm start test # lint test file + run mocha test suite
23
+ npm start dev # watch mode (chokidar → rebuild on change)
24
+ npm start sample # run sample/sample.ts via tsx
25
+ npm start build-doc # generate SVG diagrams from D2 sources in doc/
26
+ npm start clean # remove dst-stage1/ and dst-stage2/
27
+ npm start distclean # clean + remove node_modules/ and package-lock.json
28
+ ```
29
+
30
+ Tests require a Mosquitto MQTT broker under run-time; the `mosquitto`
31
+ npm package provides one that the test suite starts/stops automatically.
32
+
33
+ Build Pipeline
34
+ --------------
35
+
36
+ Two-stage build:
37
+
38
+ 1. **Stage 1** — TypeScript → JavaScript + `.d.ts` declarations (output: `dst-stage1/`)
39
+ configured via `etc/tsc.json` (target ES2022, module ESNext, strict mode).
40
+
41
+ 2. **Stage 2** — Vite bundles stage-1 output into three formats (output: `dst-stage2/`):
42
+ `mqtt-plus.esm.js`, `mqtt-plus.cjs.js`, `mqtt-plus.umd.js`.
43
+ UMD build includes Node polyfills (events, stream, buffer).
44
+
45
+ Configuration lives in `etc/`: `tsc.json`, `vite.mts`, `eslint.mts`, `knip.jsonc`, `stx.conf`, `d2.mts`.
46
+
47
+ Architecture
48
+ ------------
49
+
50
+ ### Trait-Based Mixin Tower
51
+
52
+ The library is composed as a vertical chain of trait classes (mixins),
53
+ each extending the previous. The final exported class `MQTTp` sits at
54
+ the bottom of this chain:
55
+
56
+ ```
57
+ OptionsTrait — configuration (id, codec, timeout, chunkSize, chunkCredit, topicMake/topicMatch)
58
+ ↓ CodecTrait — CBOR/JSON codec handling
59
+ ↓ EncodeTrait — message encoding/validation (valibot schemas)
60
+ ↓ MsgTrait — message class definitions and parsing
61
+ ↓ TraceTrait — EventEmitter + structured logging
62
+ ↓ BaseTrait — MQTT client hookup, subscription management, message routing
63
+ ↓ MetaTrait — instance/per-request metadata
64
+ ↓ AuthTrait — JWT authentication (jose), role-based access
65
+ ↓ EventTrait — Event Emission pattern (event/emit)
66
+ ↓ ServiceTrait — Service Call / RPC pattern (service/call)
67
+ ↓ SourceTrait — Source Fetch pattern (source/fetch)
68
+ ↓ SinkTrait — Sink Push pattern (sink/push)
69
+ ↓ MQTTp — final exported class
70
+ ```
71
+
72
+ Each trait lives in its own file: `src/mqtt-plus-<trait>.ts`.
73
+
74
+ ### Key Source Files
75
+
76
+ | File | Role |
77
+ |----------------------------|------|
78
+ | `src/mqtt-plus.ts` | Main entry point, re-exports public API types and the final MQTTp class |
79
+ | `src/mqtt-plus-api.ts` | Branded endpoint type definitions (Event, Service, Source, Sink) and APISchema generic |
80
+ | `src/mqtt-plus-info.ts` | Info/context object types passed to pattern callbacks (sender metadata, etc.) |
81
+ | `src/mqtt-plus-error.ts` | Spool (resource cleanup) and run (error handling) utilities |
82
+ | `src/mqtt-plus-util.ts` | Stream/buffer conversion, RefCountedSubscription, and CreditGate flow control |
83
+ | `src/mqtt-plus-version.ts` | Version utility for converting version strings to numeric format |
84
+ | `src/mqtt-plus-options.ts` | OptionsTrait — configuration (id, codec, timeout, chunkSize, chunkCredit, topicMake/topicMatch) |
85
+ | `src/mqtt-plus-codec.ts` | CodecTrait — CBOR and JSON codec encoding/decoding |
86
+ | `src/mqtt-plus-encode.ts` | EncodeTrait — message validation and encoding via valibot schemas |
87
+ | `src/mqtt-plus-msg.ts` | MsgTrait — message class definitions and parsing logic |
88
+ | `src/mqtt-plus-trace.ts` | TraceTrait — EventEmitter and structured logging |
89
+ | `src/mqtt-plus-base.ts` | BaseTrait — MQTT client connection, subscription management, message routing |
90
+ | `src/mqtt-plus-meta.ts` | MetaTrait — instance and per-request metadata management |
91
+ | `src/mqtt-plus-auth.ts` | AuthTrait — JWT authentication (jose) and role-based access control |
92
+ | `src/mqtt-plus-event.ts` | EventTrait — Event Emission communication pattern (event/emit) |
93
+ | `src/mqtt-plus-service.ts` | ServiceTrait — Service Call / RPC communication pattern (service/call) |
94
+ | `src/mqtt-plus-source.ts` | SourceTrait — Source Fetch communication pattern (source/fetch) |
95
+ | `src/mqtt-plus-sink.ts` | SinkTrait — Sink Push communication pattern (sink/push) |
96
+
97
+ ### Documentation
98
+
99
+ The `doc/` directory contains D2 diagram sources and generated SVG files
100
+ illustrating the four communication patterns:
101
+
102
+ - `doc/mqtt-plus-1-event-emission.{d2,svg}`
103
+ - `doc/mqtt-plus-2-service-call.{d2,svg}`
104
+ - `doc/mqtt-plus-3-sink-push.{d2,svg}`
105
+ - `doc/mqtt-plus-4-source-fetch.{d2,svg}`
106
+ - `doc/theme.d2` — shared D2 theme
107
+
108
+ Regenerate with `npm start build-doc` (requires the `etc/d2.mts` helper script).
109
+
110
+ ### Tests
111
+
112
+ Test files live in `tst/`:
113
+
114
+ | File | Role |
115
+ |-------------------------------|------|
116
+ | `tst/mqtt-plus.spec.ts` | Main Mocha test suite covering all four communication patterns |
117
+ | `tst/mqtt-plus-mosquitto.ts` | Helper for starting/stopping the Mosquitto MQTT broker |
118
+ | `tst/tsc.json` | TypeScript configuration for the test directory |
119
+
120
+ ### Type System
121
+
122
+ The API uses branded types (`Event<...>`, `Service<...>`, `Source<...>`,
123
+ `Sink<...>`) to define typed endpoint schemas. A generic `APISchema`
124
+ type parameter threads through the trait tower, enabling full type
125
+ inference for pattern names and parameter types.
126
+
127
+ Coding Style
128
+ ------------
129
+
130
+ - 4-space indentation, double quotes, no semicolons
131
+ - Stroustrup brace style (`else`/`catch`/`finally` on new line after closing brace)
132
+ - Comments: `/* ... */` with two leading/trailing spaces; no `//` end-of-line comments
133
+ - Vertical alignment of similar operators on consecutive lines
134
+ - Private members prefixed with `_`
135
+
package/CHANGELOG.md CHANGED
@@ -2,6 +2,25 @@
2
2
  ChangeLog
3
3
  =========
4
4
 
5
+ 1.4.0 (2026-02-21)
6
+ ------------------
7
+
8
+ - BUGFIX: do not suppress errors in order to catch them
9
+ - BUGFIX: do not have lingering resources on destroy
10
+ - BUGFIX: fix cleanup of resources
11
+ - BUGFIX: avoid hangs in processing
12
+ - IMPROVEMENT: add spooling and runner for error handling
13
+ - IMPROVEMENT: add lingering unsubscribe to reduce contention on broker
14
+ - IMPROVEMENT: move code from _dispatchMessage into domain methods
15
+ - IMPROVEMENT: factor out similar code for reusability
16
+ - IMPROVEMENT: split test suite and add more tests
17
+ - CLEANUP: cleanup signatures of on/off methods
18
+ - CLEANUP: simplify typing and remove unused code
19
+ - CLEANUP: rename variables and align code formatting
20
+ - CLEANUP: various code cleanups
21
+ - CLEANUP: splitted documentation from README.md into doc/*.md
22
+ - UPDATE: upgrade NPM dependencies
23
+
5
24
  1.3.0 (2026-02-07)
6
25
  ------------------
7
26