@crvouga/mockingbird-runtime 0.0.0-reserved → 0.0.1
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/README.md +130 -1
- package/dist/adapter-node.d.ts +2521 -0
- package/dist/adapter-node.js +6920 -0
- package/dist/adapter-node.js.map +7 -0
- package/dist/admin-ui.js +25 -0
- package/dist/admin-ui.js.map +7 -0
- package/dist/chunk-2KDYLZXC.js +118 -0
- package/dist/chunk-2KDYLZXC.js.map +7 -0
- package/dist/chunk-2Y25NPCV.js +1289 -0
- package/dist/chunk-2Y25NPCV.js.map +7 -0
- package/dist/chunk-4SAK6MQT.js +246 -0
- package/dist/chunk-4SAK6MQT.js.map +7 -0
- package/dist/chunk-ANCCZFF2.js +778 -0
- package/dist/chunk-ANCCZFF2.js.map +7 -0
- package/dist/chunk-EBGYPQPA.js +198 -0
- package/dist/chunk-EBGYPQPA.js.map +7 -0
- package/dist/chunk-EQLY2YOA.js +274 -0
- package/dist/chunk-EQLY2YOA.js.map +7 -0
- package/dist/chunk-G62YDIIJ.js +7664 -0
- package/dist/chunk-G62YDIIJ.js.map +7 -0
- package/dist/chunk-HU6IOAFC.js +437 -0
- package/dist/chunk-HU6IOAFC.js.map +7 -0
- package/dist/chunk-RNAHPNUJ.js +97 -0
- package/dist/chunk-RNAHPNUJ.js.map +7 -0
- package/dist/chunk-UP4QOSFD.js +87 -0
- package/dist/chunk-UP4QOSFD.js.map +7 -0
- package/dist/chunk-XNS34BPF.js +2489 -0
- package/dist/chunk-XNS34BPF.js.map +7 -0
- package/dist/core.d.ts +356 -0
- package/dist/core.js +51 -0
- package/dist/core.js.map +7 -0
- package/dist/crypto-XS5XCMRJ.js +21 -0
- package/dist/crypto-XS5XCMRJ.js.map +7 -0
- package/dist/helpers-UD7CG4WQ.js +18 -0
- package/dist/helpers-UD7CG4WQ.js.map +7 -0
- package/dist/http-codec.d.ts +72 -0
- package/dist/http-codec.js +25 -0
- package/dist/http-codec.js.map +7 -0
- package/dist/http-provider.d.ts +3243 -0
- package/dist/http-provider.js +37920 -0
- package/dist/http-provider.js.map +7 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +7 -0
- package/dist/mqtt-broker/node.d.ts +632 -0
- package/dist/mqtt-broker/node.js +355 -0
- package/dist/mqtt-broker/node.js.map +7 -0
- package/dist/mqtt-broker.d.ts +990 -0
- package/dist/mqtt-broker.js +913 -0
- package/dist/mqtt-broker.js.map +7 -0
- package/dist/openapi-metadata.d.ts +290 -0
- package/dist/openapi-metadata.js +28 -0
- package/dist/openapi-metadata.js.map +7 -0
- package/dist/openapi.d.ts +252 -0
- package/dist/openapi.js +45 -0
- package/dist/openapi.js.map +7 -0
- package/dist/preview/artifacts.d.ts +19 -0
- package/dist/preview/artifacts.js +40 -0
- package/dist/preview/artifacts.js.map +7 -0
- package/dist/preview/bridge.d.ts +21 -0
- package/dist/preview/bridge.js +9 -0
- package/dist/preview/bridge.js.map +7 -0
- package/dist/preview/harness.d.ts +12 -0
- package/dist/preview/harness.js +65 -0
- package/dist/preview/harness.js.map +7 -0
- package/dist/preview/host.d.ts +30 -0
- package/dist/preview/host.js +70 -0
- package/dist/preview/host.js.map +7 -0
- package/dist/service/admin.d.ts +106 -0
- package/dist/service/admin.js +340 -0
- package/dist/service/admin.js.map +7 -0
- package/dist/service.d.ts +2665 -0
- package/dist/service.js +264 -0
- package/dist/service.js.map +7 -0
- package/dist/sqlite.d.ts +69 -0
- package/dist/sqlite.js +19 -0
- package/dist/sqlite.js.map +7 -0
- package/dist/ui.d.ts +37 -0
- package/dist/ui.js +702 -0
- package/dist/ui.js.map +7 -0
- package/package.json +130 -6
|
@@ -0,0 +1,913 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LEGACY_UNSUPPORTED_VERSION_CONNACK,
|
|
3
|
+
MAX_VARIABLE_BYTE_INTEGER,
|
|
4
|
+
MQTT_CONNECT_OPERATION,
|
|
5
|
+
MQTT_WEBSOCKET_PATH,
|
|
6
|
+
MqttProtocolError,
|
|
7
|
+
PacketReader,
|
|
8
|
+
ReasonCode,
|
|
9
|
+
brokerAdminRoutes,
|
|
10
|
+
connackPreset,
|
|
11
|
+
connectNamespace,
|
|
12
|
+
decodePacket,
|
|
13
|
+
encodePacket,
|
|
14
|
+
observeClock,
|
|
15
|
+
takeConnackFault,
|
|
16
|
+
webSocketNamespace
|
|
17
|
+
} from "./chunk-ANCCZFF2.js";
|
|
18
|
+
import {
|
|
19
|
+
Collection,
|
|
20
|
+
IdSequence
|
|
21
|
+
} from "./chunk-G62YDIIJ.js";
|
|
22
|
+
import "./chunk-2KDYLZXC.js";
|
|
23
|
+
import {
|
|
24
|
+
fromBase64,
|
|
25
|
+
toBase64
|
|
26
|
+
} from "./chunk-XNS34BPF.js";
|
|
27
|
+
import "./chunk-2Y25NPCV.js";
|
|
28
|
+
import "./chunk-EQLY2YOA.js";
|
|
29
|
+
import "./chunk-HU6IOAFC.js";
|
|
30
|
+
import "./chunk-4SAK6MQT.js";
|
|
31
|
+
|
|
32
|
+
// ../mqtt/broker/dist/topics.js
|
|
33
|
+
var encoder = new TextEncoder();
|
|
34
|
+
var utf8Length = (value) => encoder.encode(value).length;
|
|
35
|
+
var wellFormed = (value) => value.length > 0 && !value.includes("\0") && utf8Length(value) <= 65535;
|
|
36
|
+
var isValidTopicName = (topic) => wellFormed(topic) && !topic.includes("+") && !topic.includes("#");
|
|
37
|
+
var isValidTopicFilter = (filter) => {
|
|
38
|
+
if (!wellFormed(filter))
|
|
39
|
+
return false;
|
|
40
|
+
const levels = filter.split("/");
|
|
41
|
+
return levels.every((level, index) => {
|
|
42
|
+
if (level === "#")
|
|
43
|
+
return index === levels.length - 1;
|
|
44
|
+
if (level === "+")
|
|
45
|
+
return true;
|
|
46
|
+
return !level.includes("#") && !level.includes("+");
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
var isSharedSubscription = (filter) => filter.startsWith("$share/");
|
|
50
|
+
var topicMatches = (filter, topic, options = {}) => {
|
|
51
|
+
const first = filter[0];
|
|
52
|
+
if (topic.startsWith("$") && (first === "+" || first === "#"))
|
|
53
|
+
return false;
|
|
54
|
+
const filterLevels = filter.split("/");
|
|
55
|
+
const topicLevels = topic.split("/");
|
|
56
|
+
for (let index = 0; index < filterLevels.length; index++) {
|
|
57
|
+
const level = filterLevels[index];
|
|
58
|
+
if (level === "#")
|
|
59
|
+
return index < topicLevels.length || options.parentLevel !== false;
|
|
60
|
+
if (index >= topicLevels.length)
|
|
61
|
+
return false;
|
|
62
|
+
if (level !== "+" && level !== topicLevels[index])
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
return filterLevels.length === topicLevels.length;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// ../mqtt/broker/dist/broker.js
|
|
69
|
+
var SESSION_NEVER_EXPIRES = 4294967295;
|
|
70
|
+
var PUBLISH_LOG_SIZE = 1e3;
|
|
71
|
+
var EVERY_CLIENT = "*";
|
|
72
|
+
var subscriptionKey = (clientId, topicFilter) => JSON.stringify([clientId, topicFilter]);
|
|
73
|
+
var forwarded = (properties) => ({
|
|
74
|
+
...properties.payloadFormatIndicator !== void 0 ? { payloadFormatIndicator: properties.payloadFormatIndicator } : {},
|
|
75
|
+
...properties.messageExpiryInterval !== void 0 ? { messageExpiryInterval: properties.messageExpiryInterval } : {},
|
|
76
|
+
...properties.contentType !== void 0 ? { contentType: properties.contentType } : {},
|
|
77
|
+
...properties.responseTopic !== void 0 ? { responseTopic: properties.responseTopic } : {},
|
|
78
|
+
...properties.correlationData !== void 0 ? { correlationData: toBase64(properties.correlationData) } : {},
|
|
79
|
+
...properties.userProperties !== void 0 && properties.userProperties.length > 0 ? { userProperties: properties.userProperties } : {}
|
|
80
|
+
});
|
|
81
|
+
var restored = (properties, identifiers) => {
|
|
82
|
+
const { correlationData, ...rest } = properties;
|
|
83
|
+
return {
|
|
84
|
+
...rest,
|
|
85
|
+
...correlationData !== void 0 ? { correlationData: fromBase64(correlationData) } : {},
|
|
86
|
+
...identifiers.length > 0 ? { subscriptionIdentifiers: identifiers } : {}
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
var BrokerConnection = class {
|
|
90
|
+
id;
|
|
91
|
+
transport;
|
|
92
|
+
info;
|
|
93
|
+
broker;
|
|
94
|
+
state = "connecting";
|
|
95
|
+
clientId = "";
|
|
96
|
+
aliases = /* @__PURE__ */ new Map();
|
|
97
|
+
backlog = [];
|
|
98
|
+
reader;
|
|
99
|
+
lastPacketId = 0;
|
|
100
|
+
constructor(id, transport, info, broker, maxPacketSize) {
|
|
101
|
+
this.id = id;
|
|
102
|
+
this.transport = transport;
|
|
103
|
+
this.info = info;
|
|
104
|
+
this.broker = broker;
|
|
105
|
+
this.reader = new PacketReader(maxPacketSize);
|
|
106
|
+
}
|
|
107
|
+
/** Read through a getter so a handler closing the connection mid-loop is seen. */
|
|
108
|
+
get gone() {
|
|
109
|
+
return this.state === "closed";
|
|
110
|
+
}
|
|
111
|
+
receive(chunk) {
|
|
112
|
+
if (this.gone)
|
|
113
|
+
return;
|
|
114
|
+
const { packets, error } = this.reader.push(chunk);
|
|
115
|
+
for (const packet of packets) {
|
|
116
|
+
if (this.gone)
|
|
117
|
+
return;
|
|
118
|
+
this.broker.accept(this, packet);
|
|
119
|
+
}
|
|
120
|
+
if (error && !this.gone)
|
|
121
|
+
this.broker.fail(this, error.reasonCode);
|
|
122
|
+
}
|
|
123
|
+
closed() {
|
|
124
|
+
this.broker.detach(this);
|
|
125
|
+
}
|
|
126
|
+
send(packet) {
|
|
127
|
+
if (this.gone)
|
|
128
|
+
return;
|
|
129
|
+
this.transport.send(encodePacket(packet));
|
|
130
|
+
}
|
|
131
|
+
/** The next Packet Identifier not in `used` (2.2.1). */
|
|
132
|
+
nextPacketId(used) {
|
|
133
|
+
for (let tries = 0; tries < 65535; tries++) {
|
|
134
|
+
this.lastPacketId = this.lastPacketId >= 65535 ? 1 : this.lastPacketId + 1;
|
|
135
|
+
if (!used.has(this.lastPacketId))
|
|
136
|
+
return this.lastPacketId;
|
|
137
|
+
}
|
|
138
|
+
throw new RangeError("no free packet identifier");
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
var Broker = class {
|
|
142
|
+
sessions;
|
|
143
|
+
subscriptions;
|
|
144
|
+
queue;
|
|
145
|
+
publishes;
|
|
146
|
+
cuts;
|
|
147
|
+
counters;
|
|
148
|
+
ids;
|
|
149
|
+
now;
|
|
150
|
+
policy;
|
|
151
|
+
/** Connections holding a session, by client identifier. */
|
|
152
|
+
live = /* @__PURE__ */ new Map();
|
|
153
|
+
/** Every transport not yet closed, including ones still before their CONNACK. */
|
|
154
|
+
open = /* @__PURE__ */ new Set();
|
|
155
|
+
connectionCount = 0;
|
|
156
|
+
sweeping = false;
|
|
157
|
+
constructor(options) {
|
|
158
|
+
const { sqlite, namespace } = options;
|
|
159
|
+
this.sessions = new Collection(sqlite, namespace, "mqtt_sessions");
|
|
160
|
+
this.subscriptions = new Collection(sqlite, namespace, "mqtt_subscriptions");
|
|
161
|
+
this.queue = new Collection(sqlite, namespace, "mqtt_queue");
|
|
162
|
+
this.publishes = new Collection(sqlite, namespace, "mqtt_publishes");
|
|
163
|
+
this.cuts = new Collection(sqlite, namespace, "mqtt_transport_cuts");
|
|
164
|
+
this.counters = new Collection(sqlite, namespace, "mqtt_counters");
|
|
165
|
+
this.ids = new IdSequence(sqlite, namespace, "mqtt");
|
|
166
|
+
this.now = options.now;
|
|
167
|
+
this.policy = options.policy;
|
|
168
|
+
}
|
|
169
|
+
// ── transports ───────────────────────────────────────────────────────────
|
|
170
|
+
/** Attach a new client transport. Feed the returned {@link Connection} what it receives. */
|
|
171
|
+
connect(transport, info = { kind: "memory" }) {
|
|
172
|
+
this.connectionCount += 1;
|
|
173
|
+
const connection = new BrokerConnection(`c${this.connectionCount}`, transport, info, this, this.policy.connackProperties().maximumPacketSize);
|
|
174
|
+
this.open.add(connection);
|
|
175
|
+
return connection;
|
|
176
|
+
}
|
|
177
|
+
/** Open transports right now, including ones that have not sent CONNECT. */
|
|
178
|
+
get connections() {
|
|
179
|
+
return this.open.size;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Drop every transport without touching stored sessions: the process is going away, or the
|
|
183
|
+
* stored state is about to be replaced. A session whose connection vanished this way starts
|
|
184
|
+
* its expiry at the next {@link sweep}.
|
|
185
|
+
*/
|
|
186
|
+
dropConnections() {
|
|
187
|
+
for (const connection of [...this.open]) {
|
|
188
|
+
connection.state = "closed";
|
|
189
|
+
connection.transport.destroy();
|
|
190
|
+
}
|
|
191
|
+
this.open.clear();
|
|
192
|
+
this.live.clear();
|
|
193
|
+
}
|
|
194
|
+
// ── time ─────────────────────────────────────────────────────────────────
|
|
195
|
+
/**
|
|
196
|
+
* Settle everything the clock decides: discard expired sessions, start the expiry of
|
|
197
|
+
* sessions whose connection was lost, and drop connections whose credential expired.
|
|
198
|
+
*/
|
|
199
|
+
sweep() {
|
|
200
|
+
if (this.sweeping)
|
|
201
|
+
return;
|
|
202
|
+
this.sweeping = true;
|
|
203
|
+
try {
|
|
204
|
+
const now = this.now();
|
|
205
|
+
for (const { value: session } of this.sessions.list({ order: "oldest" })) {
|
|
206
|
+
const connection = this.live.get(session.clientId);
|
|
207
|
+
if (connection && connection.id === session.connectionId) {
|
|
208
|
+
if (session.connectionExpiresAt !== null && now >= session.connectionExpiresAt) {
|
|
209
|
+
this.shutdown(connection, this.policy.connectionExpiredReason);
|
|
210
|
+
}
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (session.disconnectedAt === null) {
|
|
214
|
+
this.endConnection(session, now);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (session.expiresAt !== null && now >= session.expiresAt) {
|
|
218
|
+
this.destroySession(session.clientId);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
} finally {
|
|
222
|
+
this.sweeping = false;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// ── server-side operations ───────────────────────────────────────────────
|
|
226
|
+
/**
|
|
227
|
+
* Publish from the server side (an HTTP API, an admin injection). Returns how many sessions
|
|
228
|
+
* the message was delivered or queued to. No client ACL applies; `receive` authorization does.
|
|
229
|
+
*/
|
|
230
|
+
publish(input, source = "http") {
|
|
231
|
+
this.sweep();
|
|
232
|
+
return this.route(input, source, null);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* End a client's session as an administrator does: disconnect it with `reasonCode` and
|
|
236
|
+
* discard the session, its subscriptions and its queue. False when no such session exists.
|
|
237
|
+
*/
|
|
238
|
+
kick(clientId, reasonCode = ReasonCode.AdministrativeAction) {
|
|
239
|
+
this.sweep();
|
|
240
|
+
if (!this.sessions.has(clientId))
|
|
241
|
+
return false;
|
|
242
|
+
const connection = this.live.get(clientId);
|
|
243
|
+
if (connection)
|
|
244
|
+
this.shutdown(connection, reasonCode, true);
|
|
245
|
+
this.destroySession(clientId);
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Send a connected client a DISCONNECT and close its connection. The session stays, under
|
|
250
|
+
* its own expiry. False when the client is not connected.
|
|
251
|
+
*/
|
|
252
|
+
disconnect(clientId, reasonCode = ReasonCode.AdministrativeAction) {
|
|
253
|
+
this.sweep();
|
|
254
|
+
const connection = this.live.get(clientId);
|
|
255
|
+
if (!connection)
|
|
256
|
+
return false;
|
|
257
|
+
this.shutdown(connection, reasonCode);
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Cut the transport of one client, or of every client: live connections die without a
|
|
262
|
+
* DISCONNECT and new CONNECTs are dropped unanswered until {@link restoreTransport}.
|
|
263
|
+
* Sessions are left to their own expiry. Returns how many connections were dropped.
|
|
264
|
+
*/
|
|
265
|
+
cutTransport(clientId) {
|
|
266
|
+
this.sweep();
|
|
267
|
+
const target = clientId ?? EVERY_CLIENT;
|
|
268
|
+
this.cuts.insert(target, { target, since: this.now() });
|
|
269
|
+
let dropped = 0;
|
|
270
|
+
for (const connection of [...this.open]) {
|
|
271
|
+
if (clientId !== void 0 && connection.clientId !== clientId)
|
|
272
|
+
continue;
|
|
273
|
+
connection.transport.destroy();
|
|
274
|
+
this.detach(connection);
|
|
275
|
+
dropped += 1;
|
|
276
|
+
}
|
|
277
|
+
return dropped;
|
|
278
|
+
}
|
|
279
|
+
/** Let one client, or with no argument every client, connect again. */
|
|
280
|
+
restoreTransport(clientId) {
|
|
281
|
+
if (clientId !== void 0) {
|
|
282
|
+
this.cuts.delete(clientId);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
for (const row of this.cuts.list())
|
|
286
|
+
this.cuts.delete(row.id);
|
|
287
|
+
}
|
|
288
|
+
/** Targets currently cut: client identifiers, or `*` for every client. */
|
|
289
|
+
transportCuts() {
|
|
290
|
+
return this.cuts.list({ order: "oldest" }).map((row) => row.value.target).sort();
|
|
291
|
+
}
|
|
292
|
+
clients() {
|
|
293
|
+
this.sweep();
|
|
294
|
+
const subscriptions = this.subscriptions.list();
|
|
295
|
+
const queue = this.queue.list();
|
|
296
|
+
const iso = (value) => value === null ? null : new Date(value).toISOString();
|
|
297
|
+
return this.sessions.list({ order: "oldest" }).map(({ value: session }) => ({
|
|
298
|
+
clientId: session.clientId,
|
|
299
|
+
username: session.username,
|
|
300
|
+
connected: this.live.has(session.clientId),
|
|
301
|
+
transport: session.transport,
|
|
302
|
+
sessionExpiryInterval: session.sessionExpiryInterval,
|
|
303
|
+
connectedAt: new Date(session.connectedAt).toISOString(),
|
|
304
|
+
disconnectedAt: iso(session.disconnectedAt),
|
|
305
|
+
expiresAt: iso(session.expiresAt),
|
|
306
|
+
subscriptions: subscriptions.filter((row) => row.value.clientId === session.clientId).length,
|
|
307
|
+
queued: queue.filter((row) => row.value.clientId === session.clientId && row.value.state === "queued").length,
|
|
308
|
+
inflight: queue.filter((row) => row.value.clientId === session.clientId && row.value.state === "inflight").length
|
|
309
|
+
}));
|
|
310
|
+
}
|
|
311
|
+
subscriptionList(clientId) {
|
|
312
|
+
this.sweep();
|
|
313
|
+
return this.subscriptions.list({
|
|
314
|
+
order: "oldest",
|
|
315
|
+
...clientId !== void 0 ? { where: (row) => row.clientId === clientId } : {}
|
|
316
|
+
}).map((row) => row.value);
|
|
317
|
+
}
|
|
318
|
+
/** The last {@link PUBLISH_LOG_SIZE} publishes, oldest first. */
|
|
319
|
+
publishLog() {
|
|
320
|
+
return this.publishes.list({ order: "oldest" }).map((row) => row.value);
|
|
321
|
+
}
|
|
322
|
+
/** Publishes since the last reset, including ones the log no longer holds. */
|
|
323
|
+
publishCount() {
|
|
324
|
+
return this.counters.get("publishes")?.value ?? 0;
|
|
325
|
+
}
|
|
326
|
+
// ── packets ──────────────────────────────────────────────────────────────
|
|
327
|
+
/** @internal Called by a connection for each decoded packet. */
|
|
328
|
+
accept(connection, packet) {
|
|
329
|
+
if (connection.state === "authenticating") {
|
|
330
|
+
connection.backlog.push(packet);
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (connection.state === "connecting") {
|
|
334
|
+
if (packet.type !== "connect") {
|
|
335
|
+
connection.transport.destroy();
|
|
336
|
+
this.detach(connection);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
this.onConnect(connection, packet);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (connection.state !== "open")
|
|
343
|
+
return;
|
|
344
|
+
this.sweep();
|
|
345
|
+
if (!this.open.has(connection))
|
|
346
|
+
return;
|
|
347
|
+
switch (packet.type) {
|
|
348
|
+
case "publish":
|
|
349
|
+
this.onPublish(connection, packet);
|
|
350
|
+
return;
|
|
351
|
+
case "puback":
|
|
352
|
+
this.onPuback(connection, packet.packetId);
|
|
353
|
+
return;
|
|
354
|
+
case "subscribe":
|
|
355
|
+
this.onSubscribe(connection, packet);
|
|
356
|
+
return;
|
|
357
|
+
case "unsubscribe":
|
|
358
|
+
this.onUnsubscribe(connection, packet);
|
|
359
|
+
return;
|
|
360
|
+
case "pingreq":
|
|
361
|
+
connection.send({ type: "pingresp" });
|
|
362
|
+
return;
|
|
363
|
+
case "disconnect":
|
|
364
|
+
this.onDisconnect(connection, packet);
|
|
365
|
+
return;
|
|
366
|
+
default:
|
|
367
|
+
this.fail(connection, ReasonCode.ProtocolError);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
/** @internal A protocol violation: answer with `reasonCode` and close. */
|
|
371
|
+
fail(connection, reasonCode) {
|
|
372
|
+
if (connection.state === "open") {
|
|
373
|
+
this.shutdown(connection, reasonCode);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
connection.transport.destroy();
|
|
377
|
+
this.detach(connection);
|
|
378
|
+
}
|
|
379
|
+
/** @internal The transport is gone, or was just closed by the broker. */
|
|
380
|
+
detach(connection, keepSession = false) {
|
|
381
|
+
if (connection.state === "closed")
|
|
382
|
+
return;
|
|
383
|
+
const wasOpen = connection.state === "open";
|
|
384
|
+
connection.state = "closed";
|
|
385
|
+
this.open.delete(connection);
|
|
386
|
+
if (!wasOpen || this.live.get(connection.clientId) !== connection)
|
|
387
|
+
return;
|
|
388
|
+
this.live.delete(connection.clientId);
|
|
389
|
+
if (keepSession)
|
|
390
|
+
return;
|
|
391
|
+
const session = this.sessions.get(connection.clientId);
|
|
392
|
+
if (session)
|
|
393
|
+
this.endConnection(session, this.now());
|
|
394
|
+
}
|
|
395
|
+
/** The session's connection ended at `at`: discard it, or start its expiry (4.1). */
|
|
396
|
+
endConnection(session, at) {
|
|
397
|
+
if (session.sessionExpiryInterval === 0) {
|
|
398
|
+
this.destroySession(session.clientId);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
this.sessions.update(session.clientId, {
|
|
402
|
+
...session,
|
|
403
|
+
connectionId: null,
|
|
404
|
+
connectionExpiresAt: null,
|
|
405
|
+
disconnectedAt: at,
|
|
406
|
+
expiresAt: session.sessionExpiryInterval === SESSION_NEVER_EXPIRES ? null : at + session.sessionExpiryInterval * 1e3
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
destroySession(clientId) {
|
|
410
|
+
this.sessions.delete(clientId);
|
|
411
|
+
for (const row of this.subscriptions.list({ where: (each) => each.clientId === clientId })) {
|
|
412
|
+
this.subscriptions.delete(row.id);
|
|
413
|
+
}
|
|
414
|
+
for (const row of this.queue.list({ where: (each) => each.clientId === clientId })) {
|
|
415
|
+
this.queue.delete(row.id);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
/** Send DISCONNECT with `reasonCode`, close the transport, and settle the session. */
|
|
419
|
+
shutdown(connection, reasonCode, keepSession = false) {
|
|
420
|
+
connection.send({ type: "disconnect", reasonCode, properties: {} });
|
|
421
|
+
connection.transport.close();
|
|
422
|
+
this.detach(connection, keepSession);
|
|
423
|
+
}
|
|
424
|
+
isCut(clientId) {
|
|
425
|
+
return this.cuts.has(EVERY_CLIENT) || this.cuts.has(clientId);
|
|
426
|
+
}
|
|
427
|
+
onConnect(connection, packet) {
|
|
428
|
+
if (packet.protocolName !== "MQTT" || packet.protocolVersion !== 5) {
|
|
429
|
+
if (packet.protocolVersion === 3 || packet.protocolVersion === 4) {
|
|
430
|
+
connection.transport.send(LEGACY_UNSUPPORTED_VERSION_CONNACK);
|
|
431
|
+
}
|
|
432
|
+
connection.transport.close();
|
|
433
|
+
this.detach(connection);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
const assigned = packet.clientId === "";
|
|
437
|
+
const clientId = assigned ? this.policy.assignClientId(this.ids.next("", 16)) : packet.clientId;
|
|
438
|
+
connection.clientId = clientId;
|
|
439
|
+
if (this.isCut(clientId)) {
|
|
440
|
+
connection.transport.destroy();
|
|
441
|
+
this.detach(connection);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const refuse = (reasonCode) => {
|
|
445
|
+
if (connection.state === "closed")
|
|
446
|
+
return;
|
|
447
|
+
connection.send({ type: "connack", sessionPresent: false, reasonCode, properties: {} });
|
|
448
|
+
connection.transport.close();
|
|
449
|
+
this.detach(connection);
|
|
450
|
+
};
|
|
451
|
+
const invalid = this.policy.checkClientId?.(clientId);
|
|
452
|
+
if (invalid !== void 0) {
|
|
453
|
+
refuse(invalid);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
connection.state = "authenticating";
|
|
457
|
+
const settle = (decision2) => {
|
|
458
|
+
if (connection.state !== "authenticating")
|
|
459
|
+
return;
|
|
460
|
+
if (!decision2.ok) {
|
|
461
|
+
refuse(decision2.reasonCode);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
this.openSession(connection, packet, clientId, assigned, decision2);
|
|
465
|
+
for (const queued of connection.backlog.splice(0))
|
|
466
|
+
this.accept(connection, queued);
|
|
467
|
+
};
|
|
468
|
+
let decision;
|
|
469
|
+
try {
|
|
470
|
+
decision = this.policy.authenticate({
|
|
471
|
+
clientId,
|
|
472
|
+
assigned,
|
|
473
|
+
username: packet.username,
|
|
474
|
+
password: packet.password,
|
|
475
|
+
cleanStart: packet.cleanStart,
|
|
476
|
+
keepAlive: packet.keepAlive,
|
|
477
|
+
properties: packet.properties,
|
|
478
|
+
transport: connection.info
|
|
479
|
+
});
|
|
480
|
+
} catch {
|
|
481
|
+
refuse(ReasonCode.UnspecifiedError);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
if (decision instanceof Promise) {
|
|
485
|
+
decision.then(settle, () => refuse(ReasonCode.UnspecifiedError));
|
|
486
|
+
} else
|
|
487
|
+
settle(decision);
|
|
488
|
+
}
|
|
489
|
+
openSession(connection, packet, clientId, assigned, decision) {
|
|
490
|
+
this.sweep();
|
|
491
|
+
const now = this.now();
|
|
492
|
+
const previous = this.live.get(clientId);
|
|
493
|
+
if (previous)
|
|
494
|
+
this.shutdown(previous, ReasonCode.SessionTakenOver, true);
|
|
495
|
+
const existing = this.sessions.get(clientId);
|
|
496
|
+
const sessionPresent = existing !== void 0 && !packet.cleanStart;
|
|
497
|
+
if (existing && !sessionPresent)
|
|
498
|
+
this.destroySession(clientId);
|
|
499
|
+
const requested = packet.properties.sessionExpiryInterval ?? 0;
|
|
500
|
+
const granted = this.policy.sessionExpiry(requested);
|
|
501
|
+
const record = {
|
|
502
|
+
clientId,
|
|
503
|
+
username: packet.username ?? null,
|
|
504
|
+
principal: decision.principal ?? null,
|
|
505
|
+
sessionExpiryInterval: granted,
|
|
506
|
+
createdAt: sessionPresent ? existing.createdAt : now,
|
|
507
|
+
connectedAt: now,
|
|
508
|
+
disconnectedAt: null,
|
|
509
|
+
expiresAt: null,
|
|
510
|
+
connectionExpiresAt: decision.connectionExpiresAt ?? null,
|
|
511
|
+
connectionId: connection.id,
|
|
512
|
+
// 3.1.2.11.3: 65,535 when the client names no Receive Maximum.
|
|
513
|
+
receiveMaximum: packet.properties.receiveMaximum ?? 65535,
|
|
514
|
+
maximumPacketSize: packet.properties.maximumPacketSize ?? null,
|
|
515
|
+
transport: connection.info.kind
|
|
516
|
+
};
|
|
517
|
+
if (sessionPresent)
|
|
518
|
+
this.sessions.update(clientId, record);
|
|
519
|
+
else
|
|
520
|
+
this.sessions.insert(clientId, record);
|
|
521
|
+
this.live.set(clientId, connection);
|
|
522
|
+
connection.state = "open";
|
|
523
|
+
const keepAlive = this.policy.serverKeepAlive?.(packet.keepAlive);
|
|
524
|
+
connection.send({
|
|
525
|
+
type: "connack",
|
|
526
|
+
sessionPresent,
|
|
527
|
+
reasonCode: ReasonCode.Success,
|
|
528
|
+
properties: {
|
|
529
|
+
...this.policy.connackProperties(),
|
|
530
|
+
// What this broker does not do, said the way the protocol provides for (3.2.2.3).
|
|
531
|
+
maximumQoS: 1,
|
|
532
|
+
retainAvailable: 0,
|
|
533
|
+
sharedSubscriptionAvailable: 0,
|
|
534
|
+
...assigned ? { assignedClientIdentifier: clientId } : {},
|
|
535
|
+
...granted !== requested ? { sessionExpiryInterval: granted } : {},
|
|
536
|
+
...keepAlive !== void 0 && keepAlive !== packet.keepAlive ? { serverKeepAlive: keepAlive } : {}
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
if (sessionPresent)
|
|
540
|
+
this.resume(connection, record);
|
|
541
|
+
}
|
|
542
|
+
/** 4.4: resend what was sent and never acknowledged, then whatever queued up meanwhile. */
|
|
543
|
+
resume(connection, session) {
|
|
544
|
+
const rows = this.queue.list({
|
|
545
|
+
where: (each) => each.clientId === session.clientId && each.state === "inflight",
|
|
546
|
+
order: "oldest"
|
|
547
|
+
});
|
|
548
|
+
for (const row of rows) {
|
|
549
|
+
this.sendMessage(connection, session, row.id, row.value, row.value.packetId, true);
|
|
550
|
+
}
|
|
551
|
+
this.pump(session.clientId);
|
|
552
|
+
}
|
|
553
|
+
onDisconnect(connection, packet) {
|
|
554
|
+
const interval = packet.properties.sessionExpiryInterval;
|
|
555
|
+
const session = this.sessions.get(connection.clientId);
|
|
556
|
+
if (interval !== void 0 && session) {
|
|
557
|
+
if (session.sessionExpiryInterval === 0 && interval !== 0) {
|
|
558
|
+
this.fail(connection, ReasonCode.ProtocolError);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
this.sessions.update(connection.clientId, {
|
|
562
|
+
...session,
|
|
563
|
+
sessionExpiryInterval: this.policy.sessionExpiry(interval)
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
connection.transport.close();
|
|
567
|
+
this.detach(connection);
|
|
568
|
+
}
|
|
569
|
+
onPublish(connection, packet) {
|
|
570
|
+
if (packet.properties.subscriptionIdentifiers !== void 0) {
|
|
571
|
+
this.fail(connection, ReasonCode.ProtocolError);
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
let topic = packet.topic;
|
|
575
|
+
const alias = packet.properties.topicAlias;
|
|
576
|
+
if (alias !== void 0) {
|
|
577
|
+
const limit2 = this.policy.connackProperties().topicAliasMaximum ?? 0;
|
|
578
|
+
if (alias === 0 || alias > limit2) {
|
|
579
|
+
this.fail(connection, ReasonCode.TopicAliasInvalid);
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
if (topic !== "")
|
|
583
|
+
connection.aliases.set(alias, topic);
|
|
584
|
+
else
|
|
585
|
+
topic = connection.aliases.get(alias) ?? "";
|
|
586
|
+
}
|
|
587
|
+
if (topic === "") {
|
|
588
|
+
this.fail(connection, ReasonCode.ProtocolError);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
if (packet.qos === 2) {
|
|
592
|
+
this.fail(connection, ReasonCode.QoSNotSupported);
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
if (packet.retain) {
|
|
596
|
+
this.fail(connection, ReasonCode.RetainNotSupported);
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
if (!isValidTopicName(topic)) {
|
|
600
|
+
this.fail(connection, ReasonCode.TopicNameInvalid);
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
const limit = this.policy.maxPayloadBytes;
|
|
604
|
+
if (limit !== void 0 && packet.payload.length > limit) {
|
|
605
|
+
this.fail(connection, ReasonCode.PacketTooLarge);
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
const acknowledge = (reasonCode) => {
|
|
609
|
+
if (packet.qos === 1 && packet.packetId !== void 0) {
|
|
610
|
+
connection.send({ type: "puback", packetId: packet.packetId, reasonCode, properties: {} });
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
const rejected = this.policy.checkTopicName?.(topic);
|
|
614
|
+
if (rejected !== void 0) {
|
|
615
|
+
acknowledge(rejected);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
const session = this.sessions.get(connection.clientId);
|
|
619
|
+
const allowed = this.policy.authorize({
|
|
620
|
+
action: "publish",
|
|
621
|
+
topic,
|
|
622
|
+
qos: packet.qos,
|
|
623
|
+
retain: packet.retain,
|
|
624
|
+
clientId: connection.clientId,
|
|
625
|
+
username: session?.username ?? null,
|
|
626
|
+
principal: session?.principal ?? null
|
|
627
|
+
});
|
|
628
|
+
if (!allowed) {
|
|
629
|
+
if (this.policy.denyAction() === "disconnect") {
|
|
630
|
+
this.fail(connection, ReasonCode.NotAuthorized);
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
acknowledge(ReasonCode.NotAuthorized);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
const matched = this.route({ topic, payload: packet.payload, qos: packet.qos, properties: packet.properties }, "mqtt", connection.clientId);
|
|
637
|
+
acknowledge(matched > 0 ? ReasonCode.Success : this.policy.pubackNoMatchingSubscribers);
|
|
638
|
+
}
|
|
639
|
+
onPuback(connection, packetId) {
|
|
640
|
+
const row = this.queue.list({
|
|
641
|
+
where: (each) => each.clientId === connection.clientId && each.state === "inflight" && each.packetId === packetId
|
|
642
|
+
}).at(0);
|
|
643
|
+
if (!row)
|
|
644
|
+
return;
|
|
645
|
+
this.queue.delete(row.id);
|
|
646
|
+
this.pump(connection.clientId);
|
|
647
|
+
}
|
|
648
|
+
onSubscribe(connection, packet) {
|
|
649
|
+
const identifier = packet.properties.subscriptionIdentifiers?.[0];
|
|
650
|
+
const identifiers = this.policy.connackProperties().subscriptionIdentifierAvailable ?? 1;
|
|
651
|
+
if (identifier !== void 0 && identifiers === 0) {
|
|
652
|
+
this.fail(connection, ReasonCode.SubscriptionIdentifiersNotSupported);
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
if (this.policy.qos2Subscribe === "ignore" && packet.subscriptions.some((each) => each.qos === 2)) {
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const session = this.sessions.get(connection.clientId);
|
|
659
|
+
const reasonCodes = [];
|
|
660
|
+
for (const each of packet.subscriptions) {
|
|
661
|
+
if (isSharedSubscription(each.topicFilter)) {
|
|
662
|
+
reasonCodes.push(ReasonCode.SharedSubscriptionsNotSupported);
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
if (!isValidTopicFilter(each.topicFilter)) {
|
|
666
|
+
reasonCodes.push(ReasonCode.TopicFilterInvalid);
|
|
667
|
+
continue;
|
|
668
|
+
}
|
|
669
|
+
const rejected = this.policy.checkTopicFilter?.(each.topicFilter);
|
|
670
|
+
if (rejected !== void 0) {
|
|
671
|
+
reasonCodes.push(rejected);
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
const allowed = this.policy.authorize({
|
|
675
|
+
action: "subscribe",
|
|
676
|
+
topic: each.topicFilter,
|
|
677
|
+
qos: each.qos,
|
|
678
|
+
retain: false,
|
|
679
|
+
clientId: connection.clientId,
|
|
680
|
+
username: session?.username ?? null,
|
|
681
|
+
principal: session?.principal ?? null
|
|
682
|
+
});
|
|
683
|
+
if (!allowed) {
|
|
684
|
+
if (this.policy.denyAction() === "disconnect") {
|
|
685
|
+
this.fail(connection, ReasonCode.NotAuthorized);
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
reasonCodes.push(ReasonCode.NotAuthorized);
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
const qos = each.qos === 0 ? 0 : 1;
|
|
692
|
+
this.subscriptions.insert(subscriptionKey(connection.clientId, each.topicFilter), {
|
|
693
|
+
clientId: connection.clientId,
|
|
694
|
+
topicFilter: each.topicFilter,
|
|
695
|
+
qos,
|
|
696
|
+
noLocal: each.noLocal,
|
|
697
|
+
retainAsPublished: each.retainAsPublished,
|
|
698
|
+
retainHandling: each.retainHandling,
|
|
699
|
+
subscriptionIdentifier: identifier ?? null
|
|
700
|
+
});
|
|
701
|
+
reasonCodes.push(qos);
|
|
702
|
+
}
|
|
703
|
+
connection.send({ type: "suback", packetId: packet.packetId, properties: {}, reasonCodes });
|
|
704
|
+
}
|
|
705
|
+
onUnsubscribe(connection, packet) {
|
|
706
|
+
const reasonCodes = packet.topicFilters.map((filter) => this.subscriptions.delete(subscriptionKey(connection.clientId, filter)) ? ReasonCode.Success : ReasonCode.NoSubscriptionExisted);
|
|
707
|
+
connection.send({ type: "unsuback", packetId: packet.packetId, properties: {}, reasonCodes });
|
|
708
|
+
}
|
|
709
|
+
// ── delivery ─────────────────────────────────────────────────────────────
|
|
710
|
+
route(input, source, publisher) {
|
|
711
|
+
const targets = /* @__PURE__ */ new Map();
|
|
712
|
+
for (const { value: subscription } of this.subscriptions.list({ order: "oldest" })) {
|
|
713
|
+
if (subscription.noLocal && subscription.clientId === publisher)
|
|
714
|
+
continue;
|
|
715
|
+
const matches = topicMatches(subscription.topicFilter, input.topic, {
|
|
716
|
+
parentLevel: this.policy.multiLevelWildcardMatchesParent
|
|
717
|
+
});
|
|
718
|
+
if (!matches)
|
|
719
|
+
continue;
|
|
720
|
+
const target = targets.get(subscription.clientId) ?? { qos: 0, identifiers: [] };
|
|
721
|
+
if (subscription.qos === 1)
|
|
722
|
+
target.qos = 1;
|
|
723
|
+
if (subscription.subscriptionIdentifier !== null) {
|
|
724
|
+
target.identifiers.push(subscription.subscriptionIdentifier);
|
|
725
|
+
}
|
|
726
|
+
targets.set(subscription.clientId, target);
|
|
727
|
+
}
|
|
728
|
+
let matched = 0;
|
|
729
|
+
for (const [clientId, target] of targets) {
|
|
730
|
+
const session = this.sessions.get(clientId);
|
|
731
|
+
if (!session)
|
|
732
|
+
continue;
|
|
733
|
+
const qos = input.qos === 0 ? 0 : target.qos;
|
|
734
|
+
const allowed = this.policy.authorize({
|
|
735
|
+
action: "receive",
|
|
736
|
+
topic: input.topic,
|
|
737
|
+
qos,
|
|
738
|
+
retain: false,
|
|
739
|
+
clientId,
|
|
740
|
+
username: session.username,
|
|
741
|
+
principal: session.principal
|
|
742
|
+
});
|
|
743
|
+
if (!allowed)
|
|
744
|
+
continue;
|
|
745
|
+
matched += 1;
|
|
746
|
+
this.deliver(session, {
|
|
747
|
+
clientId,
|
|
748
|
+
topic: input.topic,
|
|
749
|
+
payload: toBase64(input.payload),
|
|
750
|
+
qos,
|
|
751
|
+
properties: forwarded(input.properties ?? {}),
|
|
752
|
+
subscriptionIdentifiers: target.identifiers,
|
|
753
|
+
state: "queued",
|
|
754
|
+
packetId: null,
|
|
755
|
+
enqueuedAt: this.now()
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
this.record(input, source, publisher, matched);
|
|
759
|
+
return matched;
|
|
760
|
+
}
|
|
761
|
+
deliver(session, message) {
|
|
762
|
+
const connection = this.live.get(session.clientId);
|
|
763
|
+
if (message.qos === 0) {
|
|
764
|
+
if (connection) {
|
|
765
|
+
this.sendMessage(connection, session, void 0, message, null, false);
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
if (!this.policy.queue.storeQos0)
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
this.queue.insert(String(this.queue.nextSequence()).padStart(12, "0"), message);
|
|
772
|
+
const limit = this.policy.queue.maxLength;
|
|
773
|
+
if (limit !== void 0) {
|
|
774
|
+
const waiting = this.queue.list({
|
|
775
|
+
where: (each) => each.clientId === session.clientId && each.state === "queued",
|
|
776
|
+
order: "oldest"
|
|
777
|
+
});
|
|
778
|
+
for (const row of waiting.slice(0, Math.max(0, waiting.length - limit))) {
|
|
779
|
+
this.queue.delete(row.id);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
if (connection)
|
|
783
|
+
this.pump(session.clientId);
|
|
784
|
+
}
|
|
785
|
+
/** Send as much of a client's queue as its Receive Maximum allows (4.9). */
|
|
786
|
+
pump(clientId) {
|
|
787
|
+
const connection = this.live.get(clientId);
|
|
788
|
+
const session = this.sessions.get(clientId);
|
|
789
|
+
if (!connection || !session)
|
|
790
|
+
return;
|
|
791
|
+
const rows = this.queue.list({ where: (each) => each.clientId === clientId, order: "oldest" });
|
|
792
|
+
const used = /* @__PURE__ */ new Set();
|
|
793
|
+
for (const row of rows) {
|
|
794
|
+
if (row.value.state === "inflight" && row.value.packetId !== null) {
|
|
795
|
+
used.add(row.value.packetId);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
const window = Math.min(session.receiveMaximum, this.policy.maxInflight);
|
|
799
|
+
for (const row of rows) {
|
|
800
|
+
if (row.value.state !== "queued")
|
|
801
|
+
continue;
|
|
802
|
+
if (row.value.qos === 0) {
|
|
803
|
+
this.queue.delete(row.id);
|
|
804
|
+
this.sendMessage(connection, session, void 0, row.value, null, false);
|
|
805
|
+
continue;
|
|
806
|
+
}
|
|
807
|
+
if (used.size >= window)
|
|
808
|
+
break;
|
|
809
|
+
const packetId = connection.nextPacketId(used);
|
|
810
|
+
used.add(packetId);
|
|
811
|
+
const sending = { ...row.value, state: "inflight", packetId };
|
|
812
|
+
this.queue.update(row.id, sending);
|
|
813
|
+
this.sendMessage(connection, session, row.id, sending, packetId, false);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
sendMessage(connection, session, queueId, message, packetId, dup) {
|
|
817
|
+
const bytes = encodePacket({
|
|
818
|
+
type: "publish",
|
|
819
|
+
topic: message.topic,
|
|
820
|
+
payload: fromBase64(message.payload),
|
|
821
|
+
qos: message.qos,
|
|
822
|
+
// 3.3.1.3: a message forwarded to an established subscription has RETAIN 0.
|
|
823
|
+
retain: false,
|
|
824
|
+
dup,
|
|
825
|
+
...packetId !== null ? { packetId } : {},
|
|
826
|
+
properties: restored(message.properties, message.subscriptionIdentifiers)
|
|
827
|
+
});
|
|
828
|
+
if (session.maximumPacketSize !== null && bytes.length > session.maximumPacketSize) {
|
|
829
|
+
if (queueId !== void 0)
|
|
830
|
+
this.queue.delete(queueId);
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
if (connection.state === "open")
|
|
834
|
+
connection.transport.send(bytes);
|
|
835
|
+
}
|
|
836
|
+
record(input, source, publisher, matched) {
|
|
837
|
+
const sequence = this.publishCount() + 1;
|
|
838
|
+
this.counters.insert("publishes", { value: sequence });
|
|
839
|
+
this.publishes.insert(String(sequence).padStart(12, "0"), {
|
|
840
|
+
sequence,
|
|
841
|
+
topic: input.topic,
|
|
842
|
+
qos: input.qos,
|
|
843
|
+
bytes: input.payload.length,
|
|
844
|
+
source,
|
|
845
|
+
clientId: publisher,
|
|
846
|
+
matched,
|
|
847
|
+
at: new Date(this.now()).toISOString()
|
|
848
|
+
});
|
|
849
|
+
this.publishes.delete(String(sequence - PUBLISH_LOG_SIZE).padStart(12, "0"));
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
var MAX_ROUTED_CONNECT_BYTES = 1024 * 1024;
|
|
853
|
+
var routeConnection = (transport, select, info = { kind: "memory" }) => {
|
|
854
|
+
const reader = new PacketReader(MAX_ROUTED_CONNECT_BYTES);
|
|
855
|
+
const held = [];
|
|
856
|
+
let attached;
|
|
857
|
+
let done = false;
|
|
858
|
+
return {
|
|
859
|
+
receive(chunk) {
|
|
860
|
+
if (attached) {
|
|
861
|
+
attached.receive(chunk);
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
if (done)
|
|
865
|
+
return;
|
|
866
|
+
held.push(chunk);
|
|
867
|
+
const { packets, error } = reader.push(chunk);
|
|
868
|
+
const first = packets[0];
|
|
869
|
+
if (!first && !error)
|
|
870
|
+
return;
|
|
871
|
+
const broker = first?.type === "connect" ? select(first) : void 0;
|
|
872
|
+
if (!broker) {
|
|
873
|
+
done = true;
|
|
874
|
+
transport.destroy();
|
|
875
|
+
return;
|
|
876
|
+
}
|
|
877
|
+
attached = broker.connect(transport, info);
|
|
878
|
+
for (const each of held.splice(0))
|
|
879
|
+
attached.receive(each);
|
|
880
|
+
},
|
|
881
|
+
closed() {
|
|
882
|
+
done = true;
|
|
883
|
+
attached?.closed();
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
};
|
|
887
|
+
export {
|
|
888
|
+
Broker,
|
|
889
|
+
LEGACY_UNSUPPORTED_VERSION_CONNACK,
|
|
890
|
+
MAX_VARIABLE_BYTE_INTEGER,
|
|
891
|
+
MQTT_CONNECT_OPERATION,
|
|
892
|
+
MQTT_WEBSOCKET_PATH,
|
|
893
|
+
MqttProtocolError,
|
|
894
|
+
PUBLISH_LOG_SIZE,
|
|
895
|
+
PacketReader,
|
|
896
|
+
ReasonCode,
|
|
897
|
+
SESSION_NEVER_EXPIRES,
|
|
898
|
+
brokerAdminRoutes,
|
|
899
|
+
connackPreset,
|
|
900
|
+
connectNamespace,
|
|
901
|
+
decodePacket,
|
|
902
|
+
encodePacket,
|
|
903
|
+
isSharedSubscription,
|
|
904
|
+
isValidTopicFilter,
|
|
905
|
+
isValidTopicName,
|
|
906
|
+
observeClock,
|
|
907
|
+
routeConnection,
|
|
908
|
+
takeConnackFault,
|
|
909
|
+
topicMatches,
|
|
910
|
+
utf8Length,
|
|
911
|
+
webSocketNamespace
|
|
912
|
+
};
|
|
913
|
+
//# sourceMappingURL=mqtt-broker.js.map
|