@crvouga/mockingbird-runtime 0.0.0-stage → 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 +131 -2
- 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 +135 -4
|
@@ -0,0 +1,778 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_NAMESPACE
|
|
3
|
+
} from "./chunk-G62YDIIJ.js";
|
|
4
|
+
import {
|
|
5
|
+
fromBase64,
|
|
6
|
+
matchNamespacePath
|
|
7
|
+
} from "./chunk-XNS34BPF.js";
|
|
8
|
+
|
|
9
|
+
// ../mqtt/broker/dist/reason.js
|
|
10
|
+
var ReasonCode = {
|
|
11
|
+
/** Also "Normal disconnection" and "Granted QoS 0". */
|
|
12
|
+
Success: 0,
|
|
13
|
+
GrantedQoS1: 1,
|
|
14
|
+
GrantedQoS2: 2,
|
|
15
|
+
NoMatchingSubscribers: 16,
|
|
16
|
+
NoSubscriptionExisted: 17,
|
|
17
|
+
UnspecifiedError: 128,
|
|
18
|
+
MalformedPacket: 129,
|
|
19
|
+
ProtocolError: 130,
|
|
20
|
+
ImplementationSpecificError: 131,
|
|
21
|
+
UnsupportedProtocolVersion: 132,
|
|
22
|
+
ClientIdentifierNotValid: 133,
|
|
23
|
+
BadUserNameOrPassword: 134,
|
|
24
|
+
NotAuthorized: 135,
|
|
25
|
+
ServerUnavailable: 136,
|
|
26
|
+
ServerBusy: 137,
|
|
27
|
+
Banned: 138,
|
|
28
|
+
ServerShuttingDown: 139,
|
|
29
|
+
BadAuthenticationMethod: 140,
|
|
30
|
+
KeepAliveTimeout: 141,
|
|
31
|
+
SessionTakenOver: 142,
|
|
32
|
+
TopicFilterInvalid: 143,
|
|
33
|
+
TopicNameInvalid: 144,
|
|
34
|
+
PacketIdentifierInUse: 145,
|
|
35
|
+
PacketIdentifierNotFound: 146,
|
|
36
|
+
ReceiveMaximumExceeded: 147,
|
|
37
|
+
TopicAliasInvalid: 148,
|
|
38
|
+
PacketTooLarge: 149,
|
|
39
|
+
MessageRateTooHigh: 150,
|
|
40
|
+
QuotaExceeded: 151,
|
|
41
|
+
AdministrativeAction: 152,
|
|
42
|
+
PayloadFormatInvalid: 153,
|
|
43
|
+
RetainNotSupported: 154,
|
|
44
|
+
QoSNotSupported: 155,
|
|
45
|
+
UseAnotherServer: 156,
|
|
46
|
+
ServerMoved: 157,
|
|
47
|
+
SharedSubscriptionsNotSupported: 158,
|
|
48
|
+
ConnectionRateExceeded: 159,
|
|
49
|
+
MaximumConnectTime: 160,
|
|
50
|
+
SubscriptionIdentifiersNotSupported: 161,
|
|
51
|
+
WildcardSubscriptionsNotSupported: 162
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// ../mqtt/broker/dist/codec.js
|
|
55
|
+
var MqttProtocolError = class extends Error {
|
|
56
|
+
reasonCode;
|
|
57
|
+
constructor(reasonCode, message) {
|
|
58
|
+
super(message);
|
|
59
|
+
this.reasonCode = reasonCode;
|
|
60
|
+
this.name = "MqttProtocolError";
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var malformed = (message) => new MqttProtocolError(ReasonCode.MalformedPacket, message);
|
|
64
|
+
var protocolError = (message) => new MqttProtocolError(ReasonCode.ProtocolError, message);
|
|
65
|
+
var PROPERTY_TABLE = [
|
|
66
|
+
[1, "payloadFormatIndicator", "byte"],
|
|
67
|
+
[2, "messageExpiryInterval", "u32"],
|
|
68
|
+
[3, "contentType", "utf8"],
|
|
69
|
+
[8, "responseTopic", "utf8"],
|
|
70
|
+
[9, "correlationData", "binary"],
|
|
71
|
+
[11, "subscriptionIdentifiers", "varint"],
|
|
72
|
+
[17, "sessionExpiryInterval", "u32"],
|
|
73
|
+
[18, "assignedClientIdentifier", "utf8"],
|
|
74
|
+
[19, "serverKeepAlive", "u16"],
|
|
75
|
+
[21, "authenticationMethod", "utf8"],
|
|
76
|
+
[22, "authenticationData", "binary"],
|
|
77
|
+
[23, "requestProblemInformation", "byte"],
|
|
78
|
+
[24, "willDelayInterval", "u32"],
|
|
79
|
+
[25, "requestResponseInformation", "byte"],
|
|
80
|
+
[26, "responseInformation", "utf8"],
|
|
81
|
+
[28, "serverReference", "utf8"],
|
|
82
|
+
[31, "reasonString", "utf8"],
|
|
83
|
+
[33, "receiveMaximum", "u16"],
|
|
84
|
+
[34, "topicAliasMaximum", "u16"],
|
|
85
|
+
[35, "topicAlias", "u16"],
|
|
86
|
+
[36, "maximumQoS", "byte"],
|
|
87
|
+
[37, "retainAvailable", "byte"],
|
|
88
|
+
[38, "userProperties", "pair"],
|
|
89
|
+
[39, "maximumPacketSize", "u32"],
|
|
90
|
+
[40, "wildcardSubscriptionAvailable", "byte"],
|
|
91
|
+
[41, "subscriptionIdentifierAvailable", "byte"],
|
|
92
|
+
[42, "sharedSubscriptionAvailable", "byte"]
|
|
93
|
+
];
|
|
94
|
+
var PROPERTY_BY_ID = new Map(PROPERTY_TABLE.map((row) => [row[0], row]));
|
|
95
|
+
var TYPE_CODES = {
|
|
96
|
+
connect: 1,
|
|
97
|
+
connack: 2,
|
|
98
|
+
publish: 3,
|
|
99
|
+
puback: 4,
|
|
100
|
+
pubrec: 5,
|
|
101
|
+
pubrel: 6,
|
|
102
|
+
pubcomp: 7,
|
|
103
|
+
subscribe: 8,
|
|
104
|
+
suback: 9,
|
|
105
|
+
unsubscribe: 10,
|
|
106
|
+
unsuback: 11,
|
|
107
|
+
pingreq: 12,
|
|
108
|
+
pingresp: 13,
|
|
109
|
+
disconnect: 14,
|
|
110
|
+
auth: 15
|
|
111
|
+
};
|
|
112
|
+
var TYPE_NAMES = Object.fromEntries(Object.entries(TYPE_CODES).map(([name, code]) => [code, name]));
|
|
113
|
+
var MAX_VARIABLE_BYTE_INTEGER = 268435455;
|
|
114
|
+
var utf8Encoder = new TextEncoder();
|
|
115
|
+
var utf8Decoder = new TextDecoder("utf-8", { fatal: true });
|
|
116
|
+
var Writer = class {
|
|
117
|
+
chunks = [];
|
|
118
|
+
size = 0;
|
|
119
|
+
bytes(value) {
|
|
120
|
+
this.chunks.push(value);
|
|
121
|
+
this.size += value.length;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
byte(value) {
|
|
125
|
+
return this.bytes(Uint8Array.of(value & 255));
|
|
126
|
+
}
|
|
127
|
+
u16(value) {
|
|
128
|
+
return this.bytes(Uint8Array.of(value >>> 8 & 255, value & 255));
|
|
129
|
+
}
|
|
130
|
+
u32(value) {
|
|
131
|
+
return this.bytes(Uint8Array.of(value >>> 24 & 255, value >>> 16 & 255, value >>> 8 & 255, value & 255));
|
|
132
|
+
}
|
|
133
|
+
varint(value) {
|
|
134
|
+
if (!Number.isInteger(value) || value < 0 || value > MAX_VARIABLE_BYTE_INTEGER) {
|
|
135
|
+
throw new RangeError(`variable byte integer out of range: ${value}`);
|
|
136
|
+
}
|
|
137
|
+
const out = [];
|
|
138
|
+
let rest = value;
|
|
139
|
+
do {
|
|
140
|
+
let digit = rest % 128;
|
|
141
|
+
rest = Math.floor(rest / 128);
|
|
142
|
+
if (rest > 0)
|
|
143
|
+
digit |= 128;
|
|
144
|
+
out.push(digit);
|
|
145
|
+
} while (rest > 0);
|
|
146
|
+
return this.bytes(Uint8Array.from(out));
|
|
147
|
+
}
|
|
148
|
+
binary(value) {
|
|
149
|
+
if (value.length > 65535)
|
|
150
|
+
throw new RangeError("binary data longer than 65535 bytes");
|
|
151
|
+
return this.u16(value.length).bytes(value);
|
|
152
|
+
}
|
|
153
|
+
utf8(value) {
|
|
154
|
+
return this.binary(utf8Encoder.encode(value));
|
|
155
|
+
}
|
|
156
|
+
get length() {
|
|
157
|
+
return this.size;
|
|
158
|
+
}
|
|
159
|
+
concat() {
|
|
160
|
+
const out = new Uint8Array(this.size);
|
|
161
|
+
let offset = 0;
|
|
162
|
+
for (const chunk of this.chunks) {
|
|
163
|
+
out.set(chunk, offset);
|
|
164
|
+
offset += chunk.length;
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
var writeProperties = (writer, properties) => {
|
|
170
|
+
const body = new Writer();
|
|
171
|
+
for (const [id, name, kind] of PROPERTY_TABLE) {
|
|
172
|
+
const value = properties[name];
|
|
173
|
+
if (value === void 0)
|
|
174
|
+
continue;
|
|
175
|
+
if (kind === "pair") {
|
|
176
|
+
for (const [key, text] of value)
|
|
177
|
+
body.byte(id).utf8(key).utf8(text);
|
|
178
|
+
} else if (name === "subscriptionIdentifiers") {
|
|
179
|
+
for (const each of value)
|
|
180
|
+
body.byte(id).varint(each);
|
|
181
|
+
} else if (kind === "byte")
|
|
182
|
+
body.byte(id).byte(value);
|
|
183
|
+
else if (kind === "u16")
|
|
184
|
+
body.byte(id).u16(value);
|
|
185
|
+
else if (kind === "u32")
|
|
186
|
+
body.byte(id).u32(value);
|
|
187
|
+
else if (kind === "utf8")
|
|
188
|
+
body.byte(id).utf8(value);
|
|
189
|
+
else if (kind === "binary")
|
|
190
|
+
body.byte(id).binary(value);
|
|
191
|
+
}
|
|
192
|
+
writer.varint(body.length).bytes(body.concat());
|
|
193
|
+
};
|
|
194
|
+
var hasProperties = (properties) => Object.values(properties).some((value) => value !== void 0 && !(Array.isArray(value) && value.length === 0));
|
|
195
|
+
var frame = (type, flags, body) => new Writer().byte(TYPE_CODES[type] << 4 | flags).varint(body.length).bytes(body.concat()).concat();
|
|
196
|
+
var encodePacket = (packet) => {
|
|
197
|
+
const body = new Writer();
|
|
198
|
+
switch (packet.type) {
|
|
199
|
+
case "connect": {
|
|
200
|
+
body.utf8(packet.protocolName).byte(packet.protocolVersion);
|
|
201
|
+
const will = packet.will;
|
|
202
|
+
body.byte((packet.username !== void 0 ? 128 : 0) | (packet.password !== void 0 ? 64 : 0) | (will?.retain ? 32 : 0) | (will?.qos ?? 0) << 3 | (will ? 4 : 0) | (packet.cleanStart ? 2 : 0));
|
|
203
|
+
body.u16(packet.keepAlive);
|
|
204
|
+
writeProperties(body, packet.properties);
|
|
205
|
+
body.utf8(packet.clientId);
|
|
206
|
+
if (will) {
|
|
207
|
+
writeProperties(body, will.properties);
|
|
208
|
+
body.utf8(will.topic).binary(will.payload);
|
|
209
|
+
}
|
|
210
|
+
if (packet.username !== void 0)
|
|
211
|
+
body.utf8(packet.username);
|
|
212
|
+
if (packet.password !== void 0)
|
|
213
|
+
body.binary(packet.password);
|
|
214
|
+
return frame("connect", 0, body);
|
|
215
|
+
}
|
|
216
|
+
case "connack":
|
|
217
|
+
body.byte(packet.sessionPresent ? 1 : 0).byte(packet.reasonCode);
|
|
218
|
+
writeProperties(body, packet.properties);
|
|
219
|
+
return frame("connack", 0, body);
|
|
220
|
+
case "publish": {
|
|
221
|
+
body.utf8(packet.topic);
|
|
222
|
+
if (packet.qos > 0)
|
|
223
|
+
body.u16(packet.packetId ?? 0);
|
|
224
|
+
writeProperties(body, packet.properties);
|
|
225
|
+
body.bytes(packet.payload);
|
|
226
|
+
const flags = (packet.dup ? 8 : 0) | packet.qos << 1 | (packet.retain ? 1 : 0);
|
|
227
|
+
return frame("publish", flags, body);
|
|
228
|
+
}
|
|
229
|
+
case "puback":
|
|
230
|
+
case "pubrec":
|
|
231
|
+
case "pubrel":
|
|
232
|
+
case "pubcomp": {
|
|
233
|
+
body.u16(packet.packetId);
|
|
234
|
+
if (hasProperties(packet.properties)) {
|
|
235
|
+
body.byte(packet.reasonCode);
|
|
236
|
+
writeProperties(body, packet.properties);
|
|
237
|
+
} else if (packet.reasonCode !== ReasonCode.Success)
|
|
238
|
+
body.byte(packet.reasonCode);
|
|
239
|
+
return frame(packet.type, packet.type === "pubrel" ? 2 : 0, body);
|
|
240
|
+
}
|
|
241
|
+
case "subscribe":
|
|
242
|
+
body.u16(packet.packetId);
|
|
243
|
+
writeProperties(body, packet.properties);
|
|
244
|
+
for (const each of packet.subscriptions) {
|
|
245
|
+
body.utf8(each.topicFilter).byte(each.qos | (each.noLocal ? 4 : 0) | (each.retainAsPublished ? 8 : 0) | (each.retainHandling & 3) << 4);
|
|
246
|
+
}
|
|
247
|
+
return frame("subscribe", 2, body);
|
|
248
|
+
case "suback":
|
|
249
|
+
case "unsuback":
|
|
250
|
+
body.u16(packet.packetId);
|
|
251
|
+
writeProperties(body, packet.properties);
|
|
252
|
+
body.bytes(Uint8Array.from(packet.reasonCodes));
|
|
253
|
+
return frame(packet.type, 0, body);
|
|
254
|
+
case "unsubscribe":
|
|
255
|
+
body.u16(packet.packetId);
|
|
256
|
+
writeProperties(body, packet.properties);
|
|
257
|
+
for (const filter of packet.topicFilters)
|
|
258
|
+
body.utf8(filter);
|
|
259
|
+
return frame("unsubscribe", 2, body);
|
|
260
|
+
case "pingreq":
|
|
261
|
+
case "pingresp":
|
|
262
|
+
return frame(packet.type, 0, body);
|
|
263
|
+
case "disconnect":
|
|
264
|
+
case "auth":
|
|
265
|
+
if (hasProperties(packet.properties)) {
|
|
266
|
+
body.byte(packet.reasonCode);
|
|
267
|
+
writeProperties(body, packet.properties);
|
|
268
|
+
} else if (packet.reasonCode !== ReasonCode.Success)
|
|
269
|
+
body.byte(packet.reasonCode);
|
|
270
|
+
return frame(packet.type, 0, body);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
var LEGACY_UNSUPPORTED_VERSION_CONNACK = Uint8Array.of(32, 2, 0, 1);
|
|
274
|
+
var Reader = class _Reader {
|
|
275
|
+
data;
|
|
276
|
+
offset = 0;
|
|
277
|
+
constructor(data) {
|
|
278
|
+
this.data = data;
|
|
279
|
+
}
|
|
280
|
+
get remaining() {
|
|
281
|
+
return this.data.length - this.offset;
|
|
282
|
+
}
|
|
283
|
+
take(length) {
|
|
284
|
+
if (length > this.remaining)
|
|
285
|
+
throw malformed("packet is shorter than its fields");
|
|
286
|
+
const slice = this.data.subarray(this.offset, this.offset + length);
|
|
287
|
+
this.offset += length;
|
|
288
|
+
return slice;
|
|
289
|
+
}
|
|
290
|
+
byte() {
|
|
291
|
+
return this.take(1)[0];
|
|
292
|
+
}
|
|
293
|
+
u16() {
|
|
294
|
+
const bytes = this.take(2);
|
|
295
|
+
return bytes[0] << 8 | bytes[1];
|
|
296
|
+
}
|
|
297
|
+
u32() {
|
|
298
|
+
const bytes = this.take(4);
|
|
299
|
+
return bytes[0] * 16777216 + (bytes[1] << 16 | bytes[2] << 8 | bytes[3]);
|
|
300
|
+
}
|
|
301
|
+
varint() {
|
|
302
|
+
let value = 0;
|
|
303
|
+
let multiplier = 1;
|
|
304
|
+
for (let index = 0; index < 4; index++) {
|
|
305
|
+
const digit = this.byte();
|
|
306
|
+
value += (digit & 127) * multiplier;
|
|
307
|
+
if ((digit & 128) === 0)
|
|
308
|
+
return value;
|
|
309
|
+
multiplier *= 128;
|
|
310
|
+
}
|
|
311
|
+
throw malformed("variable byte integer longer than four bytes");
|
|
312
|
+
}
|
|
313
|
+
binary() {
|
|
314
|
+
return this.take(this.u16()).slice();
|
|
315
|
+
}
|
|
316
|
+
utf8() {
|
|
317
|
+
let text;
|
|
318
|
+
try {
|
|
319
|
+
text = utf8Decoder.decode(this.take(this.u16()));
|
|
320
|
+
} catch {
|
|
321
|
+
throw malformed("string is not well-formed UTF-8");
|
|
322
|
+
}
|
|
323
|
+
if (text.includes("\0"))
|
|
324
|
+
throw malformed("string contains U+0000");
|
|
325
|
+
return text;
|
|
326
|
+
}
|
|
327
|
+
rest() {
|
|
328
|
+
return this.take(this.remaining).slice();
|
|
329
|
+
}
|
|
330
|
+
sub(length) {
|
|
331
|
+
return new _Reader(this.take(length));
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
var readProperties = (reader) => {
|
|
335
|
+
const scope = reader.sub(reader.varint());
|
|
336
|
+
const properties = {};
|
|
337
|
+
while (scope.remaining > 0) {
|
|
338
|
+
const id = scope.varint();
|
|
339
|
+
const row = PROPERTY_BY_ID.get(id);
|
|
340
|
+
if (!row)
|
|
341
|
+
throw malformed(`unknown property 0x${id.toString(16)}`);
|
|
342
|
+
const [, name, kind] = row;
|
|
343
|
+
if (kind === "pair") {
|
|
344
|
+
const list = properties.userProperties ?? [];
|
|
345
|
+
list.push([scope.utf8(), scope.utf8()]);
|
|
346
|
+
properties.userProperties = list;
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (name === "subscriptionIdentifiers") {
|
|
350
|
+
const list = properties.subscriptionIdentifiers ?? [];
|
|
351
|
+
const value2 = scope.varint();
|
|
352
|
+
if (value2 === 0)
|
|
353
|
+
throw protocolError("subscription identifier 0");
|
|
354
|
+
list.push(value2);
|
|
355
|
+
properties.subscriptionIdentifiers = list;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
if (properties[name] !== void 0)
|
|
359
|
+
throw protocolError(`duplicate property ${name}`);
|
|
360
|
+
const value = kind === "byte" ? scope.byte() : kind === "u16" ? scope.u16() : kind === "u32" ? scope.u32() : kind === "utf8" ? scope.utf8() : scope.binary();
|
|
361
|
+
properties[name] = value;
|
|
362
|
+
}
|
|
363
|
+
return properties;
|
|
364
|
+
};
|
|
365
|
+
var requireFlags = (type, flags, expected) => {
|
|
366
|
+
if (flags !== expected)
|
|
367
|
+
throw malformed(`${type} fixed header flags must be ${expected}`);
|
|
368
|
+
};
|
|
369
|
+
var decodeBody = (type, flags, body) => {
|
|
370
|
+
switch (type) {
|
|
371
|
+
case "connect": {
|
|
372
|
+
requireFlags(type, flags, 0);
|
|
373
|
+
const protocolName = body.utf8();
|
|
374
|
+
const protocolVersion = body.byte();
|
|
375
|
+
if (protocolName !== "MQTT" || protocolVersion !== 5) {
|
|
376
|
+
return {
|
|
377
|
+
type,
|
|
378
|
+
protocolName,
|
|
379
|
+
protocolVersion,
|
|
380
|
+
cleanStart: false,
|
|
381
|
+
keepAlive: 0,
|
|
382
|
+
properties: {},
|
|
383
|
+
clientId: ""
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
const connectFlags = body.byte();
|
|
387
|
+
if (connectFlags & 1)
|
|
388
|
+
throw malformed("CONNECT reserved flag is set");
|
|
389
|
+
const willFlag = (connectFlags & 4) !== 0;
|
|
390
|
+
const willQos = connectFlags >> 3 & 3;
|
|
391
|
+
const willRetain = (connectFlags & 32) !== 0;
|
|
392
|
+
if (willQos === 3)
|
|
393
|
+
throw malformed("Will QoS 3");
|
|
394
|
+
if (!willFlag && (willQos !== 0 || willRetain))
|
|
395
|
+
throw malformed("Will flags without a Will");
|
|
396
|
+
const keepAlive = body.u16();
|
|
397
|
+
const properties = readProperties(body);
|
|
398
|
+
const clientId = body.utf8();
|
|
399
|
+
const packet = {
|
|
400
|
+
type,
|
|
401
|
+
protocolName,
|
|
402
|
+
protocolVersion,
|
|
403
|
+
cleanStart: (connectFlags & 2) !== 0,
|
|
404
|
+
keepAlive,
|
|
405
|
+
properties,
|
|
406
|
+
clientId
|
|
407
|
+
};
|
|
408
|
+
if (willFlag) {
|
|
409
|
+
const willProperties = readProperties(body);
|
|
410
|
+
const topic = body.utf8();
|
|
411
|
+
packet.will = {
|
|
412
|
+
topic,
|
|
413
|
+
payload: body.binary(),
|
|
414
|
+
qos: willQos,
|
|
415
|
+
retain: willRetain,
|
|
416
|
+
properties: willProperties
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
if (connectFlags & 128)
|
|
420
|
+
packet.username = body.utf8();
|
|
421
|
+
if (connectFlags & 64)
|
|
422
|
+
packet.password = body.binary();
|
|
423
|
+
if (body.remaining > 0)
|
|
424
|
+
throw malformed("CONNECT has trailing bytes");
|
|
425
|
+
return packet;
|
|
426
|
+
}
|
|
427
|
+
case "connack": {
|
|
428
|
+
requireFlags(type, flags, 0);
|
|
429
|
+
const acknowledgeFlags = body.byte();
|
|
430
|
+
if (acknowledgeFlags & 254)
|
|
431
|
+
throw malformed("CONNACK reserved flags are set");
|
|
432
|
+
const reasonCode = body.byte();
|
|
433
|
+
return {
|
|
434
|
+
type,
|
|
435
|
+
sessionPresent: acknowledgeFlags === 1,
|
|
436
|
+
reasonCode,
|
|
437
|
+
properties: readProperties(body)
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
case "publish": {
|
|
441
|
+
const qos = flags >> 1 & 3;
|
|
442
|
+
if (qos === 3)
|
|
443
|
+
throw malformed("PUBLISH QoS 3");
|
|
444
|
+
const dup = (flags & 8) !== 0;
|
|
445
|
+
if (qos === 0 && dup)
|
|
446
|
+
throw malformed("PUBLISH DUP set at QoS 0");
|
|
447
|
+
const topic = body.utf8();
|
|
448
|
+
const packetId = qos > 0 ? body.u16() : void 0;
|
|
449
|
+
if (packetId === 0)
|
|
450
|
+
throw protocolError("PUBLISH packet identifier 0");
|
|
451
|
+
const properties = readProperties(body);
|
|
452
|
+
return {
|
|
453
|
+
type,
|
|
454
|
+
topic,
|
|
455
|
+
payload: body.rest(),
|
|
456
|
+
qos,
|
|
457
|
+
retain: (flags & 1) !== 0,
|
|
458
|
+
dup,
|
|
459
|
+
...packetId !== void 0 ? { packetId } : {},
|
|
460
|
+
properties
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
case "puback":
|
|
464
|
+
case "pubrec":
|
|
465
|
+
case "pubrel":
|
|
466
|
+
case "pubcomp": {
|
|
467
|
+
requireFlags(type, flags, type === "pubrel" ? 2 : 0);
|
|
468
|
+
const packetId = body.u16();
|
|
469
|
+
const reasonCode = body.remaining > 0 ? body.byte() : ReasonCode.Success;
|
|
470
|
+
const properties = body.remaining > 0 ? readProperties(body) : {};
|
|
471
|
+
return { type, packetId, reasonCode, properties };
|
|
472
|
+
}
|
|
473
|
+
case "subscribe": {
|
|
474
|
+
requireFlags(type, flags, 2);
|
|
475
|
+
const packetId = body.u16();
|
|
476
|
+
if (packetId === 0)
|
|
477
|
+
throw protocolError("SUBSCRIBE packet identifier 0");
|
|
478
|
+
const properties = readProperties(body);
|
|
479
|
+
const subscriptions = [];
|
|
480
|
+
while (body.remaining > 0) {
|
|
481
|
+
const topicFilter = body.utf8();
|
|
482
|
+
const options = body.byte();
|
|
483
|
+
if (options & 192)
|
|
484
|
+
throw malformed("SUBSCRIBE reserved option bits are set");
|
|
485
|
+
if ((options & 3) === 3)
|
|
486
|
+
throw malformed("SUBSCRIBE QoS 3");
|
|
487
|
+
const retainHandling = options >> 4 & 3;
|
|
488
|
+
if (retainHandling === 3)
|
|
489
|
+
throw protocolError("SUBSCRIBE Retain Handling 3");
|
|
490
|
+
subscriptions.push({
|
|
491
|
+
topicFilter,
|
|
492
|
+
qos: options & 3,
|
|
493
|
+
noLocal: (options & 4) !== 0,
|
|
494
|
+
retainAsPublished: (options & 8) !== 0,
|
|
495
|
+
retainHandling
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
if (subscriptions.length === 0)
|
|
499
|
+
throw protocolError("SUBSCRIBE without a topic filter");
|
|
500
|
+
return { type, packetId, properties, subscriptions };
|
|
501
|
+
}
|
|
502
|
+
case "suback":
|
|
503
|
+
case "unsuback": {
|
|
504
|
+
requireFlags(type, flags, 0);
|
|
505
|
+
const packetId = body.u16();
|
|
506
|
+
const properties = readProperties(body);
|
|
507
|
+
return { type, packetId, properties, reasonCodes: [...body.rest()] };
|
|
508
|
+
}
|
|
509
|
+
case "unsubscribe": {
|
|
510
|
+
requireFlags(type, flags, 2);
|
|
511
|
+
const packetId = body.u16();
|
|
512
|
+
if (packetId === 0)
|
|
513
|
+
throw protocolError("UNSUBSCRIBE packet identifier 0");
|
|
514
|
+
const properties = readProperties(body);
|
|
515
|
+
const topicFilters = [];
|
|
516
|
+
while (body.remaining > 0)
|
|
517
|
+
topicFilters.push(body.utf8());
|
|
518
|
+
if (topicFilters.length === 0)
|
|
519
|
+
throw protocolError("UNSUBSCRIBE without a topic filter");
|
|
520
|
+
return { type, packetId, properties, topicFilters };
|
|
521
|
+
}
|
|
522
|
+
case "pingreq":
|
|
523
|
+
case "pingresp":
|
|
524
|
+
requireFlags(type, flags, 0);
|
|
525
|
+
if (body.remaining > 0)
|
|
526
|
+
throw malformed(`${type} has a body`);
|
|
527
|
+
return { type };
|
|
528
|
+
case "disconnect":
|
|
529
|
+
case "auth": {
|
|
530
|
+
requireFlags(type, flags, 0);
|
|
531
|
+
const reasonCode = body.remaining > 0 ? body.byte() : ReasonCode.Success;
|
|
532
|
+
const properties = body.remaining > 0 ? readProperties(body) : {};
|
|
533
|
+
return { type, reasonCode, properties };
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
var decodePacket = (bytes) => {
|
|
538
|
+
const reader = new Reader(bytes);
|
|
539
|
+
const first = reader.byte();
|
|
540
|
+
const type = TYPE_NAMES[first >> 4];
|
|
541
|
+
if (!type)
|
|
542
|
+
throw malformed("reserved control packet type 0");
|
|
543
|
+
const body = reader.sub(reader.varint());
|
|
544
|
+
if (reader.remaining > 0)
|
|
545
|
+
throw malformed("bytes after the packet");
|
|
546
|
+
return decodeBody(type, first & 15, body);
|
|
547
|
+
};
|
|
548
|
+
var packetLength = (buffer) => {
|
|
549
|
+
let value = 0;
|
|
550
|
+
let multiplier = 1;
|
|
551
|
+
for (let index = 1; index <= 4; index++) {
|
|
552
|
+
const digit = buffer[index];
|
|
553
|
+
if (digit === void 0)
|
|
554
|
+
return void 0;
|
|
555
|
+
value += (digit & 127) * multiplier;
|
|
556
|
+
if ((digit & 128) === 0)
|
|
557
|
+
return index + 1 + value;
|
|
558
|
+
multiplier *= 128;
|
|
559
|
+
}
|
|
560
|
+
throw malformed("remaining length longer than four bytes");
|
|
561
|
+
};
|
|
562
|
+
var PacketReader = class {
|
|
563
|
+
maxPacketSize;
|
|
564
|
+
chunks = [];
|
|
565
|
+
/** Bytes of `chunks[0]` already consumed. */
|
|
566
|
+
head = 0;
|
|
567
|
+
/** Bytes held and not yet consumed. */
|
|
568
|
+
buffered = 0;
|
|
569
|
+
/** `maxPacketSize` is the Maximum Packet Size this endpoint advertised (3.2.2.3.6). */
|
|
570
|
+
constructor(maxPacketSize = MAX_VARIABLE_BYTE_INTEGER + 5) {
|
|
571
|
+
this.maxPacketSize = maxPacketSize;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* The complete packets `chunk` finishes, as raw bytes and decoded. A packet that cannot be
|
|
575
|
+
* decoded stops the stream: its error is returned after the packets before it.
|
|
576
|
+
*/
|
|
577
|
+
push(chunk) {
|
|
578
|
+
if (chunk.length > 0) {
|
|
579
|
+
this.chunks.push(chunk);
|
|
580
|
+
this.buffered += chunk.length;
|
|
581
|
+
}
|
|
582
|
+
const packets = [];
|
|
583
|
+
try {
|
|
584
|
+
for (; ; ) {
|
|
585
|
+
const length = packetLength(this.peek(5));
|
|
586
|
+
if (length === void 0)
|
|
587
|
+
break;
|
|
588
|
+
if (length > this.maxPacketSize) {
|
|
589
|
+
throw new MqttProtocolError(ReasonCode.PacketTooLarge, `packet of ${length} bytes`);
|
|
590
|
+
}
|
|
591
|
+
if (this.buffered < length)
|
|
592
|
+
break;
|
|
593
|
+
packets.push(decodePacket(this.take(length)));
|
|
594
|
+
}
|
|
595
|
+
} catch (error) {
|
|
596
|
+
this.chunks = [];
|
|
597
|
+
this.head = 0;
|
|
598
|
+
this.buffered = 0;
|
|
599
|
+
if (error instanceof MqttProtocolError)
|
|
600
|
+
return { packets, error };
|
|
601
|
+
throw error;
|
|
602
|
+
}
|
|
603
|
+
return { packets };
|
|
604
|
+
}
|
|
605
|
+
/** Up to the first `count` held bytes, without consuming them. */
|
|
606
|
+
peek(count) {
|
|
607
|
+
const bytes = [];
|
|
608
|
+
let skip = this.head;
|
|
609
|
+
for (const chunk of this.chunks) {
|
|
610
|
+
for (let index = skip; index < chunk.length && bytes.length < count; index++) {
|
|
611
|
+
bytes.push(chunk[index] ?? 0);
|
|
612
|
+
}
|
|
613
|
+
if (bytes.length === count)
|
|
614
|
+
break;
|
|
615
|
+
skip = 0;
|
|
616
|
+
}
|
|
617
|
+
return Uint8Array.from(bytes);
|
|
618
|
+
}
|
|
619
|
+
/** Consume the first `length` held bytes into a fresh array (decoded packets keep views of it). */
|
|
620
|
+
take(length) {
|
|
621
|
+
const out = new Uint8Array(length);
|
|
622
|
+
let filled = 0;
|
|
623
|
+
let used = 0;
|
|
624
|
+
while (filled < length) {
|
|
625
|
+
const chunk = this.chunks[used];
|
|
626
|
+
if (!chunk)
|
|
627
|
+
break;
|
|
628
|
+
const count = Math.min(chunk.length - this.head, length - filled);
|
|
629
|
+
out.set(chunk.subarray(this.head, this.head + count), filled);
|
|
630
|
+
filled += count;
|
|
631
|
+
this.head += count;
|
|
632
|
+
if (this.head === chunk.length) {
|
|
633
|
+
used += 1;
|
|
634
|
+
this.head = 0;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
this.chunks.splice(0, used);
|
|
638
|
+
this.buffered -= length;
|
|
639
|
+
return out;
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
// ../mqtt/broker/dist/runtime.js
|
|
644
|
+
var MQTT_WEBSOCKET_PATH = "/mqtt";
|
|
645
|
+
var MQTT_CONNECT_OPERATION = "MqttConnect";
|
|
646
|
+
var connackPreset = (reasonCode, description) => ({
|
|
647
|
+
description,
|
|
648
|
+
rules: [{ operationId: MQTT_CONNECT_OPERATION, effect: "connack", params: { reasonCode } }]
|
|
649
|
+
});
|
|
650
|
+
var takeConnackFault = async (faults, namespace) => {
|
|
651
|
+
const hits = await faults.take({
|
|
652
|
+
operationId: MQTT_CONNECT_OPERATION,
|
|
653
|
+
method: "CONNECT",
|
|
654
|
+
path: MQTT_WEBSOCKET_PATH,
|
|
655
|
+
namespace
|
|
656
|
+
});
|
|
657
|
+
for (const hit of hits) {
|
|
658
|
+
const code = hit.effect?.name === "connack" ? hit.effect.params.reasonCode : void 0;
|
|
659
|
+
if (typeof code === "number")
|
|
660
|
+
return code;
|
|
661
|
+
}
|
|
662
|
+
return void 0;
|
|
663
|
+
};
|
|
664
|
+
var observeClock = (clock, moved) => ({
|
|
665
|
+
now: clock.now,
|
|
666
|
+
state: clock.state,
|
|
667
|
+
freeze: clock.freeze,
|
|
668
|
+
set: (epochMs) => {
|
|
669
|
+
clock.set(epochMs);
|
|
670
|
+
moved();
|
|
671
|
+
},
|
|
672
|
+
advance: (deltaMs) => {
|
|
673
|
+
clock.advance(deltaMs);
|
|
674
|
+
moved();
|
|
675
|
+
},
|
|
676
|
+
unfreeze: () => {
|
|
677
|
+
clock.unfreeze();
|
|
678
|
+
moved();
|
|
679
|
+
},
|
|
680
|
+
reset: () => {
|
|
681
|
+
clock.reset();
|
|
682
|
+
moved();
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
var webSocketNamespace = (pathname, adminPrefix) => {
|
|
686
|
+
const prefixed = matchNamespacePath(pathname, adminPrefix);
|
|
687
|
+
if ((prefixed ? prefixed[2] : pathname) !== MQTT_WEBSOCKET_PATH)
|
|
688
|
+
return void 0;
|
|
689
|
+
return { namespace: prefixed ? decodeURIComponent(prefixed[1]) : void 0 };
|
|
690
|
+
};
|
|
691
|
+
var connectNamespace = (credentials, connect, username = connect.username) => (username !== void 0 ? credentials.get(username) : void 0) ?? credentials.get(connect.clientId) ?? DEFAULT_NAMESPACE;
|
|
692
|
+
var json = (status, body) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
|
|
693
|
+
var adminError = (status, message) => json(status, { error: { type: "mockingbird_admin", message } });
|
|
694
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
695
|
+
var brokerAdminRoutes = (brokerOf) => {
|
|
696
|
+
const target = (body) => !isRecord(body) || body.clientId === void 0 ? void 0 : typeof body.clientId === "string" && body.clientId !== "" ? body.clientId : null;
|
|
697
|
+
return {
|
|
698
|
+
"GET /clients": ({ namespace }) => json(200, { clients: brokerOf(namespace).clients() }),
|
|
699
|
+
"GET /clients/:clientId": ({ params, namespace }) => {
|
|
700
|
+
const broker = brokerOf(namespace);
|
|
701
|
+
const client = broker.clients().find((each) => each.clientId === params.clientId);
|
|
702
|
+
if (!client)
|
|
703
|
+
return adminError(404, `no client ${params.clientId}`);
|
|
704
|
+
return json(200, { ...client, topics: broker.subscriptionList(client.clientId) });
|
|
705
|
+
},
|
|
706
|
+
"POST /clients/:clientId/disconnect": ({ params, body, namespace }) => {
|
|
707
|
+
const reasonCode = isRecord(body) ? body.reasonCode : void 0;
|
|
708
|
+
const valid = reasonCode === void 0 || typeof reasonCode === "number" && Number.isInteger(reasonCode) && reasonCode >= 0 && reasonCode <= 255;
|
|
709
|
+
if (!valid)
|
|
710
|
+
return adminError(400, "reasonCode: an MQTT reason code, 0 to 255");
|
|
711
|
+
return brokerOf(namespace).disconnect(params.clientId, reasonCode) ? json(200, { disconnected: params.clientId }) : adminError(404, `client ${params.clientId} is not connected`);
|
|
712
|
+
},
|
|
713
|
+
"GET /subscriptions": ({ url, namespace }) => json(200, {
|
|
714
|
+
subscriptions: brokerOf(namespace).subscriptionList(url.searchParams.get("clientId") ?? void 0)
|
|
715
|
+
}),
|
|
716
|
+
"GET /publishes": ({ namespace }) => {
|
|
717
|
+
const broker = brokerOf(namespace);
|
|
718
|
+
return json(200, { total: broker.publishCount(), publishes: broker.publishLog() });
|
|
719
|
+
},
|
|
720
|
+
"POST /inject": ({ body, namespace }) => {
|
|
721
|
+
if (!isRecord(body) || typeof body.topic !== "string" || body.topic === "") {
|
|
722
|
+
return adminError(400, 'expected {"topic", "payload" | "payloadBase64", "qos"?}');
|
|
723
|
+
}
|
|
724
|
+
let payload;
|
|
725
|
+
if (typeof body.payloadBase64 === "string") {
|
|
726
|
+
try {
|
|
727
|
+
payload = fromBase64(body.payloadBase64);
|
|
728
|
+
} catch {
|
|
729
|
+
return adminError(400, "payloadBase64: not base64");
|
|
730
|
+
}
|
|
731
|
+
} else if (typeof body.payload === "string")
|
|
732
|
+
payload = new TextEncoder().encode(body.payload);
|
|
733
|
+
else
|
|
734
|
+
return adminError(400, "payload: a string, or payloadBase64 for raw bytes");
|
|
735
|
+
const qos = body.qos ?? 1;
|
|
736
|
+
if (qos !== 0 && qos !== 1)
|
|
737
|
+
return adminError(400, "qos: 0 or 1");
|
|
738
|
+
const matched = brokerOf(namespace).publish({ topic: body.topic, payload, qos }, "admin");
|
|
739
|
+
return json(200, { matched });
|
|
740
|
+
},
|
|
741
|
+
"GET /transport": ({ namespace }) => json(200, { cut: brokerOf(namespace).transportCuts() }),
|
|
742
|
+
"POST /transport/cut": ({ body, namespace }) => {
|
|
743
|
+
const clientId = target(body);
|
|
744
|
+
if (clientId === null)
|
|
745
|
+
return adminError(400, "clientId: a client identifier, or omit it");
|
|
746
|
+
const broker = brokerOf(namespace);
|
|
747
|
+
const dropped = broker.cutTransport(clientId);
|
|
748
|
+
return json(200, { cut: broker.transportCuts(), dropped });
|
|
749
|
+
},
|
|
750
|
+
"POST /transport/restore": ({ body, namespace }) => {
|
|
751
|
+
const clientId = target(body);
|
|
752
|
+
if (clientId === null)
|
|
753
|
+
return adminError(400, "clientId: a client identifier, or omit it");
|
|
754
|
+
const broker = brokerOf(namespace);
|
|
755
|
+
broker.restoreTransport(clientId);
|
|
756
|
+
return json(200, { cut: broker.transportCuts() });
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
export {
|
|
762
|
+
ReasonCode,
|
|
763
|
+
MqttProtocolError,
|
|
764
|
+
MAX_VARIABLE_BYTE_INTEGER,
|
|
765
|
+
encodePacket,
|
|
766
|
+
LEGACY_UNSUPPORTED_VERSION_CONNACK,
|
|
767
|
+
decodePacket,
|
|
768
|
+
PacketReader,
|
|
769
|
+
MQTT_WEBSOCKET_PATH,
|
|
770
|
+
MQTT_CONNECT_OPERATION,
|
|
771
|
+
connackPreset,
|
|
772
|
+
takeConnackFault,
|
|
773
|
+
observeClock,
|
|
774
|
+
webSocketNamespace,
|
|
775
|
+
connectNamespace,
|
|
776
|
+
brokerAdminRoutes
|
|
777
|
+
};
|
|
778
|
+
//# sourceMappingURL=chunk-ANCCZFF2.js.map
|