@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
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared Mockingbird runtime, published once so every emulator service depends on the same
|
|
3
|
+
* copy. Each subpath re-exports one workspace helper (`@crvouga/mockingbird-runtime/<subpath>` is
|
|
4
|
+
* `@crvouga/mockingbird-<subpath>`); a service's build imports these instead of inlining its own
|
|
5
|
+
* copy (scripts/bundle-service-runtime.ts).
|
|
6
|
+
*/
|
|
7
|
+
/** Every subpath this package exports, in `package.json` order. */
|
|
8
|
+
declare const RUNTIME_SUBPATHS: readonly ["core", "openapi", "openapi-metadata", "http-codec", "http-provider", "mqtt-broker", "mqtt-broker/node", "ui", "sqlite", "service", "service/admin", "adapter-node", "preview/bridge", "preview/host", "preview/artifacts", "preview/harness"];
|
|
9
|
+
|
|
10
|
+
export { RUNTIME_SUBPATHS };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var RUNTIME_SUBPATHS = [
|
|
3
|
+
"core",
|
|
4
|
+
"openapi",
|
|
5
|
+
"openapi-metadata",
|
|
6
|
+
"http-codec",
|
|
7
|
+
"http-provider",
|
|
8
|
+
"mqtt-broker",
|
|
9
|
+
"mqtt-broker/node",
|
|
10
|
+
"ui",
|
|
11
|
+
"sqlite",
|
|
12
|
+
"service",
|
|
13
|
+
"service/admin",
|
|
14
|
+
"adapter-node",
|
|
15
|
+
"preview/bridge",
|
|
16
|
+
"preview/host",
|
|
17
|
+
"preview/artifacts",
|
|
18
|
+
"preview/harness"
|
|
19
|
+
];
|
|
20
|
+
export {
|
|
21
|
+
RUNTIME_SUBPATHS
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * The shared Mockingbird runtime, published once so every emulator service depends on the same\n * copy. Each subpath re-exports one workspace helper (`@crvouga/mockingbird-runtime/<subpath>` is\n * `@crvouga/mockingbird-<subpath>`); a service's build imports these instead of inlining its own\n * copy (scripts/bundle-service-runtime.ts).\n */\n\n/** Every subpath this package exports, in `package.json` order. */\nexport const RUNTIME_SUBPATHS = [\n \"core\",\n \"openapi\",\n \"openapi-metadata\",\n \"http-codec\",\n \"http-provider\",\n \"mqtt-broker\",\n \"mqtt-broker/node\",\n \"ui\",\n \"sqlite\",\n \"service\",\n \"service/admin\",\n \"adapter-node\",\n \"preview/bridge\",\n \"preview/host\",\n \"preview/artifacts\",\n \"preview/harness\",\n] as const\n"],
|
|
5
|
+
"mappings": ";AAQO,IAAM,mBAAmB;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import { Server } from 'node:http';
|
|
2
|
+
import { Duplex } from 'node:stream';
|
|
3
|
+
|
|
4
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
5
|
+
type SqliteValue$1 = null | number | bigint | string | Uint8Array | boolean;
|
|
6
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
7
|
+
type SqliteRunResult$1 = {
|
|
8
|
+
changes: number;
|
|
9
|
+
lastInsertRowid: number | bigint;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
13
|
+
*
|
|
14
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
15
|
+
*/
|
|
16
|
+
interface SqliteStatement$1 {
|
|
17
|
+
run(...params: SqliteValue$1[]): SqliteRunResult$1;
|
|
18
|
+
all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
|
|
19
|
+
get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
23
|
+
*
|
|
24
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
25
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
26
|
+
*/
|
|
27
|
+
interface SqliteClient$1 {
|
|
28
|
+
exec(sql: string): void;
|
|
29
|
+
prepare(sql: string): SqliteStatement$1;
|
|
30
|
+
transaction<T>(fn: () => T): T;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
34
|
+
type Stored<T> = {
|
|
35
|
+
seq: number;
|
|
36
|
+
value: T;
|
|
37
|
+
};
|
|
38
|
+
type ListRecordsOptions<T> = {
|
|
39
|
+
/** Keep only records passing the predicate. */
|
|
40
|
+
where?: (value: T, seq: number) => boolean;
|
|
41
|
+
/** Sort order; default newest first. */
|
|
42
|
+
order?: "newest" | "oldest";
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
46
|
+
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
47
|
+
*/
|
|
48
|
+
declare class Collection<T> {
|
|
49
|
+
private readonly sqlite;
|
|
50
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
51
|
+
readonly namespace: string;
|
|
52
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
53
|
+
readonly collectionName: string;
|
|
54
|
+
private readonly statements;
|
|
55
|
+
private prepare;
|
|
56
|
+
constructor(sqlite: SqliteClient$1,
|
|
57
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
58
|
+
namespace: string,
|
|
59
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
60
|
+
collectionName: string);
|
|
61
|
+
private bumpCollectionSeq;
|
|
62
|
+
nextSequence(): number;
|
|
63
|
+
get(id: string): T | undefined;
|
|
64
|
+
has(id: string): boolean;
|
|
65
|
+
/** Insert a new record, assigning it the next sequence number. */
|
|
66
|
+
insert(id: string, value: T): Stored<T>;
|
|
67
|
+
/** Replace an existing record's value, keeping its position. */
|
|
68
|
+
update(id: string, value: T): Stored<T> | undefined;
|
|
69
|
+
delete(id: string): boolean;
|
|
70
|
+
/** How many records the collection holds, without reading them. */
|
|
71
|
+
count(): number;
|
|
72
|
+
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
73
|
+
id: string;
|
|
74
|
+
}>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
78
|
+
type SqliteValue = null | number | bigint | string | Uint8Array | boolean;
|
|
79
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
80
|
+
type SqliteRunResult = {
|
|
81
|
+
changes: number;
|
|
82
|
+
lastInsertRowid: number | bigint;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
86
|
+
*
|
|
87
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
88
|
+
*/
|
|
89
|
+
interface SqliteStatement {
|
|
90
|
+
run(...params: SqliteValue[]): SqliteRunResult;
|
|
91
|
+
all<T = Record<string, unknown>>(...params: SqliteValue[]): T[];
|
|
92
|
+
get<T = Record<string, unknown>>(...params: SqliteValue[]): T | undefined;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
96
|
+
*
|
|
97
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
98
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
99
|
+
*/
|
|
100
|
+
interface SqliteClient {
|
|
101
|
+
exec(sql: string): void;
|
|
102
|
+
prepare(sql: string): SqliteStatement;
|
|
103
|
+
transaction<T>(fn: () => T): T;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Properties (2.2.2.2), by the specification's names in camelCase. */
|
|
107
|
+
type Properties = {
|
|
108
|
+
payloadFormatIndicator?: number;
|
|
109
|
+
messageExpiryInterval?: number;
|
|
110
|
+
contentType?: string;
|
|
111
|
+
responseTopic?: string;
|
|
112
|
+
correlationData?: Uint8Array;
|
|
113
|
+
/** PUBLISH may carry several; SUBSCRIBE carries at most one. */
|
|
114
|
+
subscriptionIdentifiers?: number[];
|
|
115
|
+
sessionExpiryInterval?: number;
|
|
116
|
+
assignedClientIdentifier?: string;
|
|
117
|
+
serverKeepAlive?: number;
|
|
118
|
+
authenticationMethod?: string;
|
|
119
|
+
authenticationData?: Uint8Array;
|
|
120
|
+
requestProblemInformation?: number;
|
|
121
|
+
willDelayInterval?: number;
|
|
122
|
+
requestResponseInformation?: number;
|
|
123
|
+
responseInformation?: string;
|
|
124
|
+
serverReference?: string;
|
|
125
|
+
reasonString?: string;
|
|
126
|
+
receiveMaximum?: number;
|
|
127
|
+
topicAliasMaximum?: number;
|
|
128
|
+
topicAlias?: number;
|
|
129
|
+
maximumQoS?: number;
|
|
130
|
+
retainAvailable?: number;
|
|
131
|
+
userProperties?: [string, string][];
|
|
132
|
+
maximumPacketSize?: number;
|
|
133
|
+
wildcardSubscriptionAvailable?: number;
|
|
134
|
+
subscriptionIdentifierAvailable?: number;
|
|
135
|
+
sharedSubscriptionAvailable?: number;
|
|
136
|
+
};
|
|
137
|
+
type QoS = 0 | 1 | 2;
|
|
138
|
+
type Will = {
|
|
139
|
+
topic: string;
|
|
140
|
+
payload: Uint8Array;
|
|
141
|
+
qos: QoS;
|
|
142
|
+
retain: boolean;
|
|
143
|
+
properties: Properties;
|
|
144
|
+
};
|
|
145
|
+
type SubscriptionRequest = {
|
|
146
|
+
topicFilter: string;
|
|
147
|
+
qos: QoS;
|
|
148
|
+
noLocal: boolean;
|
|
149
|
+
retainAsPublished: boolean;
|
|
150
|
+
retainHandling: number;
|
|
151
|
+
};
|
|
152
|
+
type ConnectPacket = {
|
|
153
|
+
type: "connect";
|
|
154
|
+
protocolName: string;
|
|
155
|
+
protocolVersion: number;
|
|
156
|
+
cleanStart: boolean;
|
|
157
|
+
keepAlive: number;
|
|
158
|
+
properties: Properties;
|
|
159
|
+
clientId: string;
|
|
160
|
+
will?: Will;
|
|
161
|
+
username?: string;
|
|
162
|
+
password?: Uint8Array;
|
|
163
|
+
};
|
|
164
|
+
type ConnackPacket = {
|
|
165
|
+
type: "connack";
|
|
166
|
+
sessionPresent: boolean;
|
|
167
|
+
reasonCode: number;
|
|
168
|
+
properties: Properties;
|
|
169
|
+
};
|
|
170
|
+
type PublishPacket = {
|
|
171
|
+
type: "publish";
|
|
172
|
+
topic: string;
|
|
173
|
+
payload: Uint8Array;
|
|
174
|
+
qos: QoS;
|
|
175
|
+
retain: boolean;
|
|
176
|
+
dup: boolean;
|
|
177
|
+
/** Present when `qos` is 1 or 2. */
|
|
178
|
+
packetId?: number;
|
|
179
|
+
properties: Properties;
|
|
180
|
+
};
|
|
181
|
+
type AckPacket = {
|
|
182
|
+
type: "puback" | "pubrec" | "pubrel" | "pubcomp";
|
|
183
|
+
packetId: number;
|
|
184
|
+
reasonCode: number;
|
|
185
|
+
properties: Properties;
|
|
186
|
+
};
|
|
187
|
+
type SubscribePacket = {
|
|
188
|
+
type: "subscribe";
|
|
189
|
+
packetId: number;
|
|
190
|
+
properties: Properties;
|
|
191
|
+
subscriptions: SubscriptionRequest[];
|
|
192
|
+
};
|
|
193
|
+
type SubackPacket = {
|
|
194
|
+
type: "suback" | "unsuback";
|
|
195
|
+
packetId: number;
|
|
196
|
+
properties: Properties;
|
|
197
|
+
reasonCodes: number[];
|
|
198
|
+
};
|
|
199
|
+
type UnsubscribePacket = {
|
|
200
|
+
type: "unsubscribe";
|
|
201
|
+
packetId: number;
|
|
202
|
+
properties: Properties;
|
|
203
|
+
topicFilters: string[];
|
|
204
|
+
};
|
|
205
|
+
type PingPacket = {
|
|
206
|
+
type: "pingreq" | "pingresp";
|
|
207
|
+
};
|
|
208
|
+
type DisconnectPacket = {
|
|
209
|
+
type: "disconnect";
|
|
210
|
+
reasonCode: number;
|
|
211
|
+
properties: Properties;
|
|
212
|
+
};
|
|
213
|
+
type AuthPacket = {
|
|
214
|
+
type: "auth";
|
|
215
|
+
reasonCode: number;
|
|
216
|
+
properties: Properties;
|
|
217
|
+
};
|
|
218
|
+
type Packet = ConnectPacket | ConnackPacket | PublishPacket | AckPacket | SubscribePacket | SubackPacket | UnsubscribePacket | PingPacket | DisconnectPacket | AuthPacket;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The MQTT 5 broker engine: sessions, subscriptions, QoS 1 flows, offline queues and expiry.
|
|
222
|
+
*
|
|
223
|
+
* It is transport-free and timer-free. Bytes arrive through {@link Connection.receive} and
|
|
224
|
+
* leave through a {@link Transport}; time is whatever `now()` returns, and everything that
|
|
225
|
+
* depends on it (session expiry, connection expiry) is settled by {@link Broker.sweep}, which
|
|
226
|
+
* every entry point runs first. A suite therefore moves a virtual clock instead of sleeping.
|
|
227
|
+
*
|
|
228
|
+
* Everything durable lives in `Collection`s, so reset and Timeline checkpoints cover it. Only
|
|
229
|
+
* the live connections are held in memory.
|
|
230
|
+
*
|
|
231
|
+
* What a vendor decides differently (who may connect, which topics, limits, reason codes for
|
|
232
|
+
* a refusal) comes in through {@link BrokerPolicy}.
|
|
233
|
+
*/
|
|
234
|
+
|
|
235
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
236
|
+
[key: string]: JsonValue;
|
|
237
|
+
};
|
|
238
|
+
/** The byte pipe to one client. A listener implements it over a socket. */
|
|
239
|
+
type Transport = {
|
|
240
|
+
send(bytes: Uint8Array): void;
|
|
241
|
+
/** Flush what was sent, then close: the peer sees an orderly end. */
|
|
242
|
+
close(): void;
|
|
243
|
+
/** Drop the connection at once, as a network failure does. */
|
|
244
|
+
destroy(): void;
|
|
245
|
+
};
|
|
246
|
+
/** How a client reached the broker, for a policy that reads it. */
|
|
247
|
+
type TransportInfo = {
|
|
248
|
+
kind: "tcp" | "websocket" | "memory";
|
|
249
|
+
/** Request path of a WebSocket upgrade. */
|
|
250
|
+
path?: string;
|
|
251
|
+
};
|
|
252
|
+
/** What a listener feeds: the other half of a {@link Transport}. */
|
|
253
|
+
type Connection = {
|
|
254
|
+
receive(chunk: Uint8Array): void;
|
|
255
|
+
/** The transport is gone: the peer closed it or the network failed. */
|
|
256
|
+
closed(): void;
|
|
257
|
+
};
|
|
258
|
+
type ConnectRequest = {
|
|
259
|
+
clientId: string;
|
|
260
|
+
/** True when the client sent an empty identifier and `clientId` was assigned. */
|
|
261
|
+
assigned: boolean;
|
|
262
|
+
username: string | undefined;
|
|
263
|
+
password: Uint8Array | undefined;
|
|
264
|
+
cleanStart: boolean;
|
|
265
|
+
keepAlive: number;
|
|
266
|
+
properties: Properties;
|
|
267
|
+
transport: TransportInfo;
|
|
268
|
+
};
|
|
269
|
+
type ConnectDecision = {
|
|
270
|
+
ok: true;
|
|
271
|
+
/** Kept with the session and handed back to {@link BrokerPolicy.authorize}. */
|
|
272
|
+
principal?: JsonValue;
|
|
273
|
+
/** Epoch ms after which the server drops the connection (a token's expiry). */
|
|
274
|
+
connectionExpiresAt?: number;
|
|
275
|
+
} | {
|
|
276
|
+
ok: false;
|
|
277
|
+
reasonCode: number;
|
|
278
|
+
};
|
|
279
|
+
type AuthorizeRequest = {
|
|
280
|
+
/** `receive` is asked once per delivery, for vendors that authorize it separately. */
|
|
281
|
+
action: "publish" | "subscribe" | "receive";
|
|
282
|
+
/** The Topic Name, or the Topic Filter for `subscribe`. */
|
|
283
|
+
topic: string;
|
|
284
|
+
qos: QoS;
|
|
285
|
+
retain: boolean;
|
|
286
|
+
clientId: string;
|
|
287
|
+
username: string | null;
|
|
288
|
+
principal: JsonValue | null;
|
|
289
|
+
};
|
|
290
|
+
/** Where one vendor's broker differs from another's. Every member is read at use time. */
|
|
291
|
+
type BrokerPolicy = {
|
|
292
|
+
authenticate(request: ConnectRequest): ConnectDecision | Promise<ConnectDecision>;
|
|
293
|
+
authorize(request: AuthorizeRequest): boolean;
|
|
294
|
+
/** What a denied PUBLISH or SUBSCRIBE does: answer `Not authorized`, or also disconnect. */
|
|
295
|
+
denyAction(): "ignore" | "disconnect";
|
|
296
|
+
/** A reason code refuses a CONNECT for its client identifier. */
|
|
297
|
+
checkClientId?(clientId: string): number | undefined;
|
|
298
|
+
/** The identifier assigned to a client that sent none, from a deterministic 16-character token. */
|
|
299
|
+
assignClientId(token: string): string;
|
|
300
|
+
/** A reason code rejects a well-formed Topic Name the vendor does not accept. */
|
|
301
|
+
checkTopicName?(topic: string): number | undefined;
|
|
302
|
+
/** A reason code rejects a well-formed Topic Filter the vendor does not accept. */
|
|
303
|
+
checkTopicFilter?(filter: string): number | undefined;
|
|
304
|
+
/** The Session Expiry Interval (seconds) granted for the one a client asked for. */
|
|
305
|
+
sessionExpiry(requested: number): number;
|
|
306
|
+
/** The keep alive the server imposes, when it differs from the client's. */
|
|
307
|
+
serverKeepAlive?(requested: number): number | undefined;
|
|
308
|
+
/**
|
|
309
|
+
* Vendor values for the CONNACK: Maximum Packet Size, Topic Alias Maximum, Receive Maximum,
|
|
310
|
+
* Wildcard Subscription Available, Subscription Identifier Available.
|
|
311
|
+
*/
|
|
312
|
+
connackProperties(): Properties;
|
|
313
|
+
/** Largest PUBLISH payload accepted from a client; larger ones disconnect it. */
|
|
314
|
+
maxPayloadBytes?: number;
|
|
315
|
+
/** QoS 1 messages in flight to one client at a time. */
|
|
316
|
+
maxInflight: number;
|
|
317
|
+
/** What a session keeps while its client is away. */
|
|
318
|
+
queue: {
|
|
319
|
+
storeQos0: boolean;
|
|
320
|
+
maxLength?: number;
|
|
321
|
+
};
|
|
322
|
+
/** PUBACK reason code for a publish no subscription matched. */
|
|
323
|
+
pubackNoMatchingSubscribers: number;
|
|
324
|
+
/** A SUBSCRIBE asking for QoS 2: grant QoS 1, or send no SUBACK at all. */
|
|
325
|
+
qos2Subscribe: "downgrade" | "ignore";
|
|
326
|
+
/** Whether `a/#` matches `a` (MQTT 5.0 4.7.1.2 says it does). */
|
|
327
|
+
multiLevelWildcardMatchesParent: boolean;
|
|
328
|
+
/** DISCONNECT reason code when `connectionExpiresAt` passes. */
|
|
329
|
+
connectionExpiredReason: number;
|
|
330
|
+
};
|
|
331
|
+
type SessionRecord = {
|
|
332
|
+
clientId: string;
|
|
333
|
+
username: string | null;
|
|
334
|
+
principal: JsonValue | null;
|
|
335
|
+
/** Seconds the session outlives its connection. */
|
|
336
|
+
sessionExpiryInterval: number;
|
|
337
|
+
createdAt: number;
|
|
338
|
+
connectedAt: number;
|
|
339
|
+
/** `null` while a connection holds the session. */
|
|
340
|
+
disconnectedAt: number | null;
|
|
341
|
+
/** Epoch ms the session is discarded at; `null` while connected, or when it never expires. */
|
|
342
|
+
expiresAt: number | null;
|
|
343
|
+
connectionExpiresAt: number | null;
|
|
344
|
+
/** The live connection holding the session; a stale id marks a connection lost to a restore. */
|
|
345
|
+
connectionId: string | null;
|
|
346
|
+
receiveMaximum: number;
|
|
347
|
+
maximumPacketSize: number | null;
|
|
348
|
+
transport: TransportInfo["kind"];
|
|
349
|
+
};
|
|
350
|
+
type SubscriptionRecord = {
|
|
351
|
+
clientId: string;
|
|
352
|
+
topicFilter: string;
|
|
353
|
+
/** Granted QoS. */
|
|
354
|
+
qos: 0 | 1;
|
|
355
|
+
noLocal: boolean;
|
|
356
|
+
retainAsPublished: boolean;
|
|
357
|
+
retainHandling: number;
|
|
358
|
+
subscriptionIdentifier: number | null;
|
|
359
|
+
};
|
|
360
|
+
/** Application message properties a broker forwards unaltered (3.3.2.3). */
|
|
361
|
+
type ForwardedProperties = {
|
|
362
|
+
payloadFormatIndicator?: number;
|
|
363
|
+
messageExpiryInterval?: number;
|
|
364
|
+
contentType?: string;
|
|
365
|
+
responseTopic?: string;
|
|
366
|
+
/** base64. */
|
|
367
|
+
correlationData?: string;
|
|
368
|
+
userProperties?: [string, string][];
|
|
369
|
+
};
|
|
370
|
+
type QueuedMessage = {
|
|
371
|
+
clientId: string;
|
|
372
|
+
topic: string;
|
|
373
|
+
/** base64. */
|
|
374
|
+
payload: string;
|
|
375
|
+
qos: 0 | 1;
|
|
376
|
+
properties: ForwardedProperties;
|
|
377
|
+
subscriptionIdentifiers: number[];
|
|
378
|
+
/** `inflight` once sent at QoS 1 and not yet acknowledged. */
|
|
379
|
+
state: "queued" | "inflight";
|
|
380
|
+
packetId: number | null;
|
|
381
|
+
enqueuedAt: number;
|
|
382
|
+
};
|
|
383
|
+
/** Who published. */
|
|
384
|
+
type PublishSource = "mqtt" | "http" | "admin";
|
|
385
|
+
/** One publish as the log keeps it: metadata only, never the payload. */
|
|
386
|
+
type PublishRecord = {
|
|
387
|
+
/** 1 for the first publish since the last reset, counting ones the log has dropped. */
|
|
388
|
+
sequence: number;
|
|
389
|
+
topic: string;
|
|
390
|
+
qos: QoS;
|
|
391
|
+
bytes: number;
|
|
392
|
+
source: PublishSource;
|
|
393
|
+
/** The publishing client, for `mqtt`. */
|
|
394
|
+
clientId: string | null;
|
|
395
|
+
/** Sessions the message was delivered or queued to. */
|
|
396
|
+
matched: number;
|
|
397
|
+
at: string;
|
|
398
|
+
};
|
|
399
|
+
type TransportCut = {
|
|
400
|
+
target: string;
|
|
401
|
+
since: number;
|
|
402
|
+
};
|
|
403
|
+
type ClientInfo = {
|
|
404
|
+
clientId: string;
|
|
405
|
+
username: string | null;
|
|
406
|
+
connected: boolean;
|
|
407
|
+
transport: TransportInfo["kind"];
|
|
408
|
+
sessionExpiryInterval: number;
|
|
409
|
+
connectedAt: string;
|
|
410
|
+
disconnectedAt: string | null;
|
|
411
|
+
expiresAt: string | null;
|
|
412
|
+
subscriptions: number;
|
|
413
|
+
queued: number;
|
|
414
|
+
inflight: number;
|
|
415
|
+
};
|
|
416
|
+
type PublishInput = {
|
|
417
|
+
topic: string;
|
|
418
|
+
payload: Uint8Array;
|
|
419
|
+
qos: QoS;
|
|
420
|
+
properties?: Properties;
|
|
421
|
+
};
|
|
422
|
+
type BrokerOptions = {
|
|
423
|
+
sqlite: SqliteClient;
|
|
424
|
+
/** Storage namespace; brokers with different namespaces share nothing. */
|
|
425
|
+
namespace: string;
|
|
426
|
+
now: () => number;
|
|
427
|
+
policy: BrokerPolicy;
|
|
428
|
+
};
|
|
429
|
+
type ConnectionState = "connecting" | "authenticating" | "open" | "closed";
|
|
430
|
+
declare class BrokerConnection implements Connection {
|
|
431
|
+
readonly id: string;
|
|
432
|
+
readonly transport: Transport;
|
|
433
|
+
readonly info: TransportInfo;
|
|
434
|
+
private readonly broker;
|
|
435
|
+
state: ConnectionState;
|
|
436
|
+
clientId: string;
|
|
437
|
+
readonly aliases: Map<number, string>;
|
|
438
|
+
readonly backlog: Packet[];
|
|
439
|
+
private readonly reader;
|
|
440
|
+
private lastPacketId;
|
|
441
|
+
constructor(id: string, transport: Transport, info: TransportInfo, broker: Broker, maxPacketSize: number | undefined);
|
|
442
|
+
/** Read through a getter so a handler closing the connection mid-loop is seen. */
|
|
443
|
+
private get gone();
|
|
444
|
+
receive(chunk: Uint8Array): void;
|
|
445
|
+
closed(): void;
|
|
446
|
+
send(packet: Packet): void;
|
|
447
|
+
/** The next Packet Identifier not in `used` (2.2.1). */
|
|
448
|
+
nextPacketId(used: ReadonlySet<number>): number;
|
|
449
|
+
}
|
|
450
|
+
declare class Broker {
|
|
451
|
+
readonly sessions: Collection<SessionRecord>;
|
|
452
|
+
readonly subscriptions: Collection<SubscriptionRecord>;
|
|
453
|
+
readonly queue: Collection<QueuedMessage>;
|
|
454
|
+
readonly publishes: Collection<PublishRecord>;
|
|
455
|
+
readonly cuts: Collection<TransportCut>;
|
|
456
|
+
readonly counters: Collection<{
|
|
457
|
+
value: number;
|
|
458
|
+
}>;
|
|
459
|
+
private readonly ids;
|
|
460
|
+
private readonly now;
|
|
461
|
+
private readonly policy;
|
|
462
|
+
/** Connections holding a session, by client identifier. */
|
|
463
|
+
private readonly live;
|
|
464
|
+
/** Every transport not yet closed, including ones still before their CONNACK. */
|
|
465
|
+
private readonly open;
|
|
466
|
+
private connectionCount;
|
|
467
|
+
private sweeping;
|
|
468
|
+
constructor(options: BrokerOptions);
|
|
469
|
+
/** Attach a new client transport. Feed the returned {@link Connection} what it receives. */
|
|
470
|
+
connect(transport: Transport, info?: TransportInfo): Connection;
|
|
471
|
+
/** Open transports right now, including ones that have not sent CONNECT. */
|
|
472
|
+
get connections(): number;
|
|
473
|
+
/**
|
|
474
|
+
* Drop every transport without touching stored sessions: the process is going away, or the
|
|
475
|
+
* stored state is about to be replaced. A session whose connection vanished this way starts
|
|
476
|
+
* its expiry at the next {@link sweep}.
|
|
477
|
+
*/
|
|
478
|
+
dropConnections(): void;
|
|
479
|
+
/**
|
|
480
|
+
* Settle everything the clock decides: discard expired sessions, start the expiry of
|
|
481
|
+
* sessions whose connection was lost, and drop connections whose credential expired.
|
|
482
|
+
*/
|
|
483
|
+
sweep(): void;
|
|
484
|
+
/**
|
|
485
|
+
* Publish from the server side (an HTTP API, an admin injection). Returns how many sessions
|
|
486
|
+
* the message was delivered or queued to. No client ACL applies; `receive` authorization does.
|
|
487
|
+
*/
|
|
488
|
+
publish(input: PublishInput, source?: PublishSource): number;
|
|
489
|
+
/**
|
|
490
|
+
* End a client's session as an administrator does: disconnect it with `reasonCode` and
|
|
491
|
+
* discard the session, its subscriptions and its queue. False when no such session exists.
|
|
492
|
+
*/
|
|
493
|
+
kick(clientId: string, reasonCode?: number): boolean;
|
|
494
|
+
/**
|
|
495
|
+
* Send a connected client a DISCONNECT and close its connection. The session stays, under
|
|
496
|
+
* its own expiry. False when the client is not connected.
|
|
497
|
+
*/
|
|
498
|
+
disconnect(clientId: string, reasonCode?: number): boolean;
|
|
499
|
+
/**
|
|
500
|
+
* Cut the transport of one client, or of every client: live connections die without a
|
|
501
|
+
* DISCONNECT and new CONNECTs are dropped unanswered until {@link restoreTransport}.
|
|
502
|
+
* Sessions are left to their own expiry. Returns how many connections were dropped.
|
|
503
|
+
*/
|
|
504
|
+
cutTransport(clientId?: string): number;
|
|
505
|
+
/** Let one client, or with no argument every client, connect again. */
|
|
506
|
+
restoreTransport(clientId?: string): void;
|
|
507
|
+
/** Targets currently cut: client identifiers, or `*` for every client. */
|
|
508
|
+
transportCuts(): string[];
|
|
509
|
+
clients(): ClientInfo[];
|
|
510
|
+
subscriptionList(clientId?: string): SubscriptionRecord[];
|
|
511
|
+
/** The last {@link PUBLISH_LOG_SIZE} publishes, oldest first. */
|
|
512
|
+
publishLog(): PublishRecord[];
|
|
513
|
+
/** Publishes since the last reset, including ones the log no longer holds. */
|
|
514
|
+
publishCount(): number;
|
|
515
|
+
/** @internal Called by a connection for each decoded packet. */
|
|
516
|
+
accept(connection: BrokerConnection, packet: Packet): void;
|
|
517
|
+
/** @internal A protocol violation: answer with `reasonCode` and close. */
|
|
518
|
+
fail(connection: BrokerConnection, reasonCode: number): void;
|
|
519
|
+
/** @internal The transport is gone, or was just closed by the broker. */
|
|
520
|
+
detach(connection: BrokerConnection, keepSession?: boolean): void;
|
|
521
|
+
/** The session's connection ended at `at`: discard it, or start its expiry (4.1). */
|
|
522
|
+
private endConnection;
|
|
523
|
+
private destroySession;
|
|
524
|
+
/** Send DISCONNECT with `reasonCode`, close the transport, and settle the session. */
|
|
525
|
+
private shutdown;
|
|
526
|
+
private isCut;
|
|
527
|
+
private onConnect;
|
|
528
|
+
private openSession;
|
|
529
|
+
/** 4.4: resend what was sent and never acknowledged, then whatever queued up meanwhile. */
|
|
530
|
+
private resume;
|
|
531
|
+
private onDisconnect;
|
|
532
|
+
private onPublish;
|
|
533
|
+
private onPuback;
|
|
534
|
+
private onSubscribe;
|
|
535
|
+
private onUnsubscribe;
|
|
536
|
+
private route;
|
|
537
|
+
private deliver;
|
|
538
|
+
/** Send as much of a client's queue as its Receive Maximum allows (4.9). */
|
|
539
|
+
private pump;
|
|
540
|
+
private sendMessage;
|
|
541
|
+
private record;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** Hand a new transport to a broker (`broker.connect`, or `routeConnection` for tenants). */
|
|
545
|
+
type Attach = (transport: Transport, info: TransportInfo) => Connection;
|
|
546
|
+
type TcpListenOptions = {
|
|
547
|
+
/** Default `0`: the OS picks a free port. */
|
|
548
|
+
port?: number;
|
|
549
|
+
/** Default `127.0.0.1`. */
|
|
550
|
+
host?: string;
|
|
551
|
+
};
|
|
552
|
+
type TcpListening = {
|
|
553
|
+
host: string;
|
|
554
|
+
port: number;
|
|
555
|
+
/** `mqtt://host:port`. */
|
|
556
|
+
url: string;
|
|
557
|
+
/** Stop listening and drop every connection. */
|
|
558
|
+
close(): Promise<void>;
|
|
559
|
+
};
|
|
560
|
+
/** Serve MQTT over TCP. */
|
|
561
|
+
declare const listenTcp: (attach: Attach, options?: TcpListenOptions) => Promise<TcpListening>;
|
|
562
|
+
type WebSocketOptions = {
|
|
563
|
+
/**
|
|
564
|
+
* The broker side for an upgrade request to `pathname`, or `undefined` when that path does
|
|
565
|
+
* not speak MQTT (the upgrade is answered 404).
|
|
566
|
+
*/
|
|
567
|
+
route(pathname: string): Attach | undefined;
|
|
568
|
+
/** Subprotocols accepted. Default `["mqtt"]` (MQTT 5.0 section 6). */
|
|
569
|
+
subprotocols?: readonly string[];
|
|
570
|
+
/** Answer 400 to an upgrade offering none of them. Default `true`. */
|
|
571
|
+
requireSubprotocol?: boolean;
|
|
572
|
+
/**
|
|
573
|
+
* Largest message, all its fragments together, before the connection is closed with 1009
|
|
574
|
+
* (RFC 6455 7.4.1). Default: the largest MQTT packet, 268,435,460 bytes.
|
|
575
|
+
*/
|
|
576
|
+
maxMessageBytes?: number;
|
|
577
|
+
};
|
|
578
|
+
type WebSocketListening = {
|
|
579
|
+
/** Stop accepting upgrades and drop every WebSocket connection. */
|
|
580
|
+
close(): void;
|
|
581
|
+
};
|
|
582
|
+
/** Serve MQTT over WebSocket on a `node:http` server's `upgrade` event. */
|
|
583
|
+
declare const attachWebSocket: (server: Server, options: WebSocketOptions) => WebSocketListening;
|
|
584
|
+
/**
|
|
585
|
+
* An in-process connection as a Node stream, for a client library that takes one instead of a
|
|
586
|
+
* URL (`new MqttClient(() => memoryStream(attach), options)` with `mqtt`). No socket is opened.
|
|
587
|
+
*/
|
|
588
|
+
declare const memoryStream: (attach: Attach) => Duplex;
|
|
589
|
+
/** What a service runtime exposes for its MQTT side; `serveMqtt` binds listeners to it. */
|
|
590
|
+
type MqttRuntime = {
|
|
591
|
+
attach(transport: Transport, info?: TransportInfo, namespace?: string): Connection;
|
|
592
|
+
webSocketPath(pathname: string): {
|
|
593
|
+
namespace: string | undefined;
|
|
594
|
+
} | undefined;
|
|
595
|
+
/** Listener URLs, reported by the runtime's health route. */
|
|
596
|
+
readonly endpoints: {
|
|
597
|
+
mqtt?: string;
|
|
598
|
+
websocket?: string;
|
|
599
|
+
};
|
|
600
|
+
namespaces(): string[];
|
|
601
|
+
instance(namespace?: string): {
|
|
602
|
+
broker: Broker;
|
|
603
|
+
};
|
|
604
|
+
/** Replaced by `serveMqtt` with one that also closes what it bound. */
|
|
605
|
+
stop(): void;
|
|
606
|
+
};
|
|
607
|
+
type ServeMqttOptions = TcpListenOptions & {
|
|
608
|
+
/**
|
|
609
|
+
* How often (real ms) expiries are settled when nothing else touches the brokers. A suite
|
|
610
|
+
* that moves the runtime clock needs none of it. Default 1000.
|
|
611
|
+
*/
|
|
612
|
+
sweepMs?: number;
|
|
613
|
+
/** WebSocket subprotocols accepted. Default `["mqtt"]`. */
|
|
614
|
+
subprotocols?: readonly string[];
|
|
615
|
+
};
|
|
616
|
+
type MqttListening = {
|
|
617
|
+
tcp: TcpListening;
|
|
618
|
+
/** Serve MQTT over WebSocket on a bound HTTP server; returns its `ws://…/mqtt` URL. */
|
|
619
|
+
attachWebSocket(http: {
|
|
620
|
+
server: Server;
|
|
621
|
+
url: string;
|
|
622
|
+
}): string;
|
|
623
|
+
};
|
|
624
|
+
/**
|
|
625
|
+
* Bind a runtime's MQTT listeners: TCP now, WebSocket once its HTTP server is up, and a timer
|
|
626
|
+
* that settles expiries in real time. `runtime.stop()` then closes all of it and drops every
|
|
627
|
+
* client, and so does closing the HTTP server.
|
|
628
|
+
*/
|
|
629
|
+
declare const serveMqtt: (runtime: MqttRuntime, options?: ServeMqttOptions) => Promise<MqttListening>;
|
|
630
|
+
|
|
631
|
+
export { attachWebSocket, listenTcp, memoryStream, serveMqtt };
|
|
632
|
+
export type { Attach, MqttListening, MqttRuntime, ServeMqttOptions, TcpListenOptions, TcpListening, WebSocketListening, WebSocketOptions };
|