@azure/web-pubsub-express 1.0.6-alpha.20240930.1 → 1.0.6-alpha.20241002.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 +165 -0
- package/dist/commonjs/cloudEventsDispatcher.d.ts.map +1 -1
- package/dist/commonjs/cloudEventsDispatcher.js +143 -8
- package/dist/commonjs/cloudEventsDispatcher.js.map +1 -1
- package/dist/commonjs/cloudEventsProtocols.d.ts +174 -1
- package/dist/commonjs/cloudEventsProtocols.d.ts.map +1 -1
- package/dist/commonjs/cloudEventsProtocols.js.map +1 -1
- package/dist/commonjs/enum/MqttErrorCodes/mqttDisconnectReasonCode.d.ts +180 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttDisconnectReasonCode.d.ts.map +1 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttDisconnectReasonCode.js +186 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttDisconnectReasonCode.js.map +1 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttV311ConnectReturnCode.d.ts +31 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttV311ConnectReturnCode.d.ts.map +1 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttV311ConnectReturnCode.js +37 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttV311ConnectReturnCode.js.map +1 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttV500ConnectReasonCode.d.ts +112 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttV500ConnectReasonCode.d.ts.map +1 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttV500ConnectReasonCode.js +118 -0
- package/dist/commonjs/enum/MqttErrorCodes/mqttV500ConnectReasonCode.js.map +1 -0
- package/dist/commonjs/index.d.ts +3 -0
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +473 -8
- package/dist/commonjs/index.js.map +1 -1
- package/dist/esm/cloudEventsDispatcher.d.ts.map +1 -1
- package/dist/esm/cloudEventsDispatcher.js +143 -8
- package/dist/esm/cloudEventsDispatcher.js.map +1 -1
- package/dist/esm/cloudEventsProtocols.d.ts +174 -1
- package/dist/esm/cloudEventsProtocols.d.ts.map +1 -1
- package/dist/esm/cloudEventsProtocols.js.map +1 -1
- package/dist/esm/enum/MqttErrorCodes/mqttDisconnectReasonCode.d.ts +180 -0
- package/dist/esm/enum/MqttErrorCodes/mqttDisconnectReasonCode.d.ts.map +1 -0
- package/dist/esm/enum/MqttErrorCodes/mqttDisconnectReasonCode.js +183 -0
- package/dist/esm/enum/MqttErrorCodes/mqttDisconnectReasonCode.js.map +1 -0
- package/dist/esm/enum/MqttErrorCodes/mqttV311ConnectReturnCode.d.ts +31 -0
- package/dist/esm/enum/MqttErrorCodes/mqttV311ConnectReturnCode.d.ts.map +1 -0
- package/dist/esm/enum/MqttErrorCodes/mqttV311ConnectReturnCode.js +34 -0
- package/dist/esm/enum/MqttErrorCodes/mqttV311ConnectReturnCode.js.map +1 -0
- package/dist/esm/enum/MqttErrorCodes/mqttV500ConnectReasonCode.d.ts +112 -0
- package/dist/esm/enum/MqttErrorCodes/mqttV500ConnectReasonCode.d.ts.map +1 -0
- package/dist/esm/enum/MqttErrorCodes/mqttV500ConnectReasonCode.js +115 -0
- package/dist/esm/enum/MqttErrorCodes/mqttV500ConnectReasonCode.js.map +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/web-pubsub-express.d.ts +507 -1
- package/package.json +1 -1
package/dist/commonjs/index.js
CHANGED
|
@@ -67,6 +67,155 @@ function readRequestBody(req) {
|
|
|
67
67
|
*/
|
|
68
68
|
const logger = logger$1.createClientLogger("web-pubsub-express");
|
|
69
69
|
|
|
70
|
+
// Copyright (c) Microsoft Corporation.
|
|
71
|
+
// Licensed under the MIT License.
|
|
72
|
+
/**
|
|
73
|
+
* MQTT 3.1.1 Connect Return Codes.
|
|
74
|
+
*/
|
|
75
|
+
exports.MqttV311ConnectReturnCode = void 0;
|
|
76
|
+
(function (MqttV311ConnectReturnCode) {
|
|
77
|
+
/**
|
|
78
|
+
* 0x01: Connection refused, unacceptable protocol version
|
|
79
|
+
* The Server does not support the level of the MQTT protocol requested by the Client.
|
|
80
|
+
*/
|
|
81
|
+
MqttV311ConnectReturnCode[MqttV311ConnectReturnCode["UnacceptableProtocolVersion"] = 1] = "UnacceptableProtocolVersion";
|
|
82
|
+
/**
|
|
83
|
+
* 0x02: Connection refused, identifier rejected
|
|
84
|
+
* The Client identifier is correct UTF-8 but not allowed by the Server.
|
|
85
|
+
*/
|
|
86
|
+
MqttV311ConnectReturnCode[MqttV311ConnectReturnCode["IdentifierRejected"] = 2] = "IdentifierRejected";
|
|
87
|
+
/**
|
|
88
|
+
* 0x03: Connection refused, server unavailable
|
|
89
|
+
* The Network Connection has been made but the MQTT service is unavailable.
|
|
90
|
+
*/
|
|
91
|
+
MqttV311ConnectReturnCode[MqttV311ConnectReturnCode["ServerUnavailable"] = 3] = "ServerUnavailable";
|
|
92
|
+
/**
|
|
93
|
+
* 0x04: Connection refused, bad user name or password
|
|
94
|
+
* The data in the user name or password is malformed.
|
|
95
|
+
*/
|
|
96
|
+
MqttV311ConnectReturnCode[MqttV311ConnectReturnCode["BadUsernameOrPassword"] = 4] = "BadUsernameOrPassword";
|
|
97
|
+
/**
|
|
98
|
+
* 0x05: Connection refused, not authorized
|
|
99
|
+
* The Client is not authorized to connect.
|
|
100
|
+
*/
|
|
101
|
+
MqttV311ConnectReturnCode[MqttV311ConnectReturnCode["NotAuthorized"] = 5] = "NotAuthorized";
|
|
102
|
+
})(exports.MqttV311ConnectReturnCode || (exports.MqttV311ConnectReturnCode = {}));
|
|
103
|
+
|
|
104
|
+
// Copyright (c) Microsoft Corporation.
|
|
105
|
+
// Licensed under the MIT License.
|
|
106
|
+
/**
|
|
107
|
+
* MQTT Connect Reason Codes
|
|
108
|
+
* These codes represent the reasons for the outcome of an MQTT CONNECT packet as per MQTT 5.0 specification.
|
|
109
|
+
*/
|
|
110
|
+
exports.MqttV500ConnectReasonCode = void 0;
|
|
111
|
+
(function (MqttV500ConnectReasonCode) {
|
|
112
|
+
/**
|
|
113
|
+
* 0x80 - Unspecified error
|
|
114
|
+
* Description: The Server does not wish to reveal the reason for the failure, or none of the other Reason Codes apply.
|
|
115
|
+
*/
|
|
116
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["UnspecifiedError"] = 128] = "UnspecifiedError";
|
|
117
|
+
/**
|
|
118
|
+
* 0x81 - Malformed Packet
|
|
119
|
+
* Description: Data within the CONNECT packet could not be correctly parsed.
|
|
120
|
+
*/
|
|
121
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["MalformedPacket"] = 129] = "MalformedPacket";
|
|
122
|
+
/**
|
|
123
|
+
* 0x82 - Protocol Error
|
|
124
|
+
* Description: Data in the CONNECT packet does not conform to this specification.
|
|
125
|
+
*/
|
|
126
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["ProtocolError"] = 130] = "ProtocolError";
|
|
127
|
+
/**
|
|
128
|
+
* 0x83 - Implementation specific error
|
|
129
|
+
* Description: The CONNECT is valid but is not accepted by this Server.
|
|
130
|
+
*/
|
|
131
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["ImplementationSpecificError"] = 131] = "ImplementationSpecificError";
|
|
132
|
+
/**
|
|
133
|
+
* 0x84 - Unsupported Protocol Version
|
|
134
|
+
* Description: The Server does not support the version of the MQTT protocol requested by the Client.
|
|
135
|
+
*/
|
|
136
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["UnsupportedProtocolVersion"] = 132] = "UnsupportedProtocolVersion";
|
|
137
|
+
/**
|
|
138
|
+
* 0x85 - Client Identifier not valid
|
|
139
|
+
* Description: The Client Identifier is a valid string but is not allowed by the Server.
|
|
140
|
+
*/
|
|
141
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["ClientIdentifierNotValid"] = 133] = "ClientIdentifierNotValid";
|
|
142
|
+
/**
|
|
143
|
+
* 0x86 - Bad User Name or Password
|
|
144
|
+
* Description: The Server does not accept the User Name or Password specified by the Client.
|
|
145
|
+
*/
|
|
146
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["BadUserNameOrPassword"] = 134] = "BadUserNameOrPassword";
|
|
147
|
+
/**
|
|
148
|
+
* 0x87 - Not authorized
|
|
149
|
+
* Description: The Client is not authorized to connect.
|
|
150
|
+
*/
|
|
151
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["NotAuthorized"] = 135] = "NotAuthorized";
|
|
152
|
+
/**
|
|
153
|
+
* 0x88 - Server unavailable
|
|
154
|
+
* Description: The MQTT Server is not available.
|
|
155
|
+
*/
|
|
156
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["ServerUnavailable"] = 136] = "ServerUnavailable";
|
|
157
|
+
/**
|
|
158
|
+
* 0x89 - Server busy
|
|
159
|
+
* Description: The Server is busy. Try again later.
|
|
160
|
+
*/
|
|
161
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["ServerBusy"] = 137] = "ServerBusy";
|
|
162
|
+
/**
|
|
163
|
+
* 0x8A - Banned
|
|
164
|
+
* Description: This Client has been banned by administrative action. Contact the server administrator.
|
|
165
|
+
*/
|
|
166
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["Banned"] = 138] = "Banned";
|
|
167
|
+
/**
|
|
168
|
+
* 0x8C - Bad authentication method
|
|
169
|
+
* Description: The authentication method is not supported or does not match the authentication method currently in use.
|
|
170
|
+
*/
|
|
171
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["BadAuthenticationMethod"] = 140] = "BadAuthenticationMethod";
|
|
172
|
+
/**
|
|
173
|
+
* 0x90 - Topic Name invalid
|
|
174
|
+
* Description: The Will Topic Name is not malformed, but is not accepted by this Server.
|
|
175
|
+
*/
|
|
176
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["TopicNameInvalid"] = 144] = "TopicNameInvalid";
|
|
177
|
+
/**
|
|
178
|
+
* 0x95 - Packet too large
|
|
179
|
+
* Description: The CONNECT packet exceeded the maximum permissible size.
|
|
180
|
+
*/
|
|
181
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["PacketTooLarge"] = 149] = "PacketTooLarge";
|
|
182
|
+
/**
|
|
183
|
+
* 0x97 - Quota exceeded
|
|
184
|
+
* Description: An implementation or administrative imposed limit has been exceeded.
|
|
185
|
+
*/
|
|
186
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["QuotaExceeded"] = 151] = "QuotaExceeded";
|
|
187
|
+
/**
|
|
188
|
+
* 0x99 - Payload format invalid
|
|
189
|
+
* Description: The Will Payload does not match the specified Payload Format Indicator.
|
|
190
|
+
*/
|
|
191
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["PayloadFormatInvalid"] = 153] = "PayloadFormatInvalid";
|
|
192
|
+
/**
|
|
193
|
+
* 0x9A - Retain not supported
|
|
194
|
+
* Description: The Server does not support retained messages, and Will Retain was set to 1.
|
|
195
|
+
*/
|
|
196
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["RetainNotSupported"] = 154] = "RetainNotSupported";
|
|
197
|
+
/**
|
|
198
|
+
* 0x9B - QoS not supported
|
|
199
|
+
* Description: The Server does not support the QoS set in Will QoS.
|
|
200
|
+
*/
|
|
201
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["QosNotSupported"] = 155] = "QosNotSupported";
|
|
202
|
+
/**
|
|
203
|
+
* 0x9C - Use another server
|
|
204
|
+
* Description: The Client should temporarily use another server.
|
|
205
|
+
*/
|
|
206
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["UseAnotherServer"] = 156] = "UseAnotherServer";
|
|
207
|
+
/**
|
|
208
|
+
* 0x9D - Server moved
|
|
209
|
+
* Description: The Client should permanently use another server.
|
|
210
|
+
*/
|
|
211
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["ServerMoved"] = 157] = "ServerMoved";
|
|
212
|
+
/**
|
|
213
|
+
* 0x9F - Connection rate exceeded
|
|
214
|
+
* Description: The connection rate limit has been exceeded.
|
|
215
|
+
*/
|
|
216
|
+
MqttV500ConnectReasonCode[MqttV500ConnectReasonCode["ConnectionRateExceeded"] = 159] = "ConnectionRateExceeded";
|
|
217
|
+
})(exports.MqttV500ConnectReasonCode || (exports.MqttV500ConnectReasonCode = {}));
|
|
218
|
+
|
|
70
219
|
// Copyright (c) Microsoft Corporation.
|
|
71
220
|
// Licensed under the MIT License.
|
|
72
221
|
var EventType;
|
|
@@ -85,21 +234,31 @@ function getConnectResponseHandler(connectRequest, response) {
|
|
|
85
234
|
modified = true;
|
|
86
235
|
},
|
|
87
236
|
success(res) {
|
|
88
|
-
response.statusCode = 200;
|
|
89
237
|
if (modified) {
|
|
90
238
|
response.setHeader("ce-connectionState", toBase64JsonString(states));
|
|
91
239
|
}
|
|
92
240
|
if (res === undefined) {
|
|
241
|
+
response.statusCode = 204;
|
|
93
242
|
response.end();
|
|
94
243
|
}
|
|
95
244
|
else {
|
|
245
|
+
response.statusCode = 200;
|
|
96
246
|
response.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
97
247
|
response.end(JSON.stringify(res));
|
|
98
248
|
}
|
|
99
249
|
},
|
|
100
250
|
fail(code, detail) {
|
|
101
|
-
response
|
|
102
|
-
|
|
251
|
+
handleConnectErrorResponse(connectRequest, response, code, detail);
|
|
252
|
+
},
|
|
253
|
+
failWith(res) {
|
|
254
|
+
if ("mqtt" in res) {
|
|
255
|
+
response.statusCode = getStatusCodeFromMqttConnectCode(res.mqtt.code);
|
|
256
|
+
response.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
257
|
+
response.end(JSON.stringify(res));
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
handleConnectErrorResponse(connectRequest, response, res.code, res.detail);
|
|
261
|
+
}
|
|
103
262
|
},
|
|
104
263
|
};
|
|
105
264
|
return handler;
|
|
@@ -138,7 +297,7 @@ function getUserEventResponseHandler(userRequest, response) {
|
|
|
138
297
|
return handler;
|
|
139
298
|
}
|
|
140
299
|
function getContext(request, origin) {
|
|
141
|
-
const
|
|
300
|
+
const baseContext = {
|
|
142
301
|
signature: getHttpHeader(request, "ce-signature"),
|
|
143
302
|
userId: getHttpHeader(request, "ce-userid"),
|
|
144
303
|
hub: getHttpHeader(request, "ce-hub"),
|
|
@@ -146,9 +305,18 @@ function getContext(request, origin) {
|
|
|
146
305
|
eventName: getHttpHeader(request, "ce-eventname"),
|
|
147
306
|
origin: origin,
|
|
148
307
|
states: fromBase64JsonString(getHttpHeader(request, "ce-connectionstate")),
|
|
308
|
+
clientProtocol: "default",
|
|
149
309
|
};
|
|
150
|
-
|
|
151
|
-
|
|
310
|
+
if (isMqttRequest(request)) {
|
|
311
|
+
const mqttProperties = {
|
|
312
|
+
physicalConnectionId: getHttpHeader(request, "ce-physicalConnectionId"),
|
|
313
|
+
sessionId: getHttpHeader(request, "ce-sessionId"),
|
|
314
|
+
};
|
|
315
|
+
return Object.assign(Object.assign({}, baseContext), { clientProtocol: "mqtt", mqtt: mqttProperties });
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
return baseContext;
|
|
319
|
+
}
|
|
152
320
|
}
|
|
153
321
|
function tryGetWebPubSubEvent(req) {
|
|
154
322
|
// check ce-type to see if it is a valid WebPubSub CloudEvent request
|
|
@@ -175,9 +343,116 @@ function tryGetWebPubSubEvent(req) {
|
|
|
175
343
|
return undefined;
|
|
176
344
|
}
|
|
177
345
|
}
|
|
346
|
+
function getStatusCodeFromMqttConnectCode(mqttConnectCode) {
|
|
347
|
+
if (mqttConnectCode < 0x80) {
|
|
348
|
+
switch (mqttConnectCode) {
|
|
349
|
+
case exports.MqttV311ConnectReturnCode.UnacceptableProtocolVersion:
|
|
350
|
+
case exports.MqttV311ConnectReturnCode.IdentifierRejected:
|
|
351
|
+
return 400; // BadRequest
|
|
352
|
+
case exports.MqttV311ConnectReturnCode.ServerUnavailable:
|
|
353
|
+
return 503; // ServiceUnavailable
|
|
354
|
+
case exports.MqttV311ConnectReturnCode.BadUsernameOrPassword:
|
|
355
|
+
case exports.MqttV311ConnectReturnCode.NotAuthorized:
|
|
356
|
+
return 401; // Unauthorized
|
|
357
|
+
default:
|
|
358
|
+
logger.warning(`Invalid MQTT connect return code: ${mqttConnectCode}.`);
|
|
359
|
+
return 500; // InternalServerError
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
switch (mqttConnectCode) {
|
|
364
|
+
case exports.MqttV500ConnectReasonCode.NotAuthorized:
|
|
365
|
+
case exports.MqttV500ConnectReasonCode.BadUserNameOrPassword:
|
|
366
|
+
return 401; // Unauthorized
|
|
367
|
+
case exports.MqttV500ConnectReasonCode.ClientIdentifierNotValid:
|
|
368
|
+
case exports.MqttV500ConnectReasonCode.MalformedPacket:
|
|
369
|
+
case exports.MqttV500ConnectReasonCode.UnsupportedProtocolVersion:
|
|
370
|
+
case exports.MqttV500ConnectReasonCode.BadAuthenticationMethod:
|
|
371
|
+
case exports.MqttV500ConnectReasonCode.TopicNameInvalid:
|
|
372
|
+
case exports.MqttV500ConnectReasonCode.PayloadFormatInvalid:
|
|
373
|
+
case exports.MqttV500ConnectReasonCode.ImplementationSpecificError:
|
|
374
|
+
case exports.MqttV500ConnectReasonCode.PacketTooLarge:
|
|
375
|
+
case exports.MqttV500ConnectReasonCode.RetainNotSupported:
|
|
376
|
+
case exports.MqttV500ConnectReasonCode.QosNotSupported:
|
|
377
|
+
return 400; // BadRequest
|
|
378
|
+
case exports.MqttV500ConnectReasonCode.QuotaExceeded:
|
|
379
|
+
case exports.MqttV500ConnectReasonCode.ConnectionRateExceeded:
|
|
380
|
+
return 429; // TooManyRequests
|
|
381
|
+
case exports.MqttV500ConnectReasonCode.Banned:
|
|
382
|
+
return 403; // Forbidden
|
|
383
|
+
case exports.MqttV500ConnectReasonCode.UseAnotherServer:
|
|
384
|
+
case exports.MqttV500ConnectReasonCode.ServerMoved:
|
|
385
|
+
case exports.MqttV500ConnectReasonCode.ServerUnavailable:
|
|
386
|
+
case exports.MqttV500ConnectReasonCode.ServerBusy:
|
|
387
|
+
case exports.MqttV500ConnectReasonCode.UnspecifiedError:
|
|
388
|
+
return 500; // InternalServerError
|
|
389
|
+
default:
|
|
390
|
+
logger.warning(`Invalid MQTT connect return code: ${mqttConnectCode}.`);
|
|
391
|
+
return 500; // InternalServerError
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function getMqttConnectCodeFromStatusCode(statusCode, protocolVersion) {
|
|
396
|
+
if (protocolVersion === 4) {
|
|
397
|
+
switch (statusCode) {
|
|
398
|
+
case 400:
|
|
399
|
+
return exports.MqttV311ConnectReturnCode.BadUsernameOrPassword;
|
|
400
|
+
case 401:
|
|
401
|
+
return exports.MqttV311ConnectReturnCode.NotAuthorized;
|
|
402
|
+
case 500:
|
|
403
|
+
return exports.MqttV311ConnectReturnCode.ServerUnavailable;
|
|
404
|
+
default:
|
|
405
|
+
logger.warning(`Unsupported HTTP Status Code: ${statusCode}.`);
|
|
406
|
+
return exports.MqttV311ConnectReturnCode.ServerUnavailable;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
else if (protocolVersion === 5) {
|
|
410
|
+
switch (statusCode) {
|
|
411
|
+
case 400:
|
|
412
|
+
return exports.MqttV500ConnectReasonCode.BadUserNameOrPassword;
|
|
413
|
+
case 401:
|
|
414
|
+
return exports.MqttV500ConnectReasonCode.NotAuthorized;
|
|
415
|
+
case 500:
|
|
416
|
+
return exports.MqttV500ConnectReasonCode.UnspecifiedError;
|
|
417
|
+
default:
|
|
418
|
+
logger.warning(`Unsupported HTTP Status Code: ${statusCode}.`);
|
|
419
|
+
return exports.MqttV500ConnectReasonCode.UnspecifiedError;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
logger.warning(`Invalid MQTT protocol version: ${protocolVersion}.`);
|
|
424
|
+
return exports.MqttV311ConnectReturnCode.UnacceptableProtocolVersion;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
function handleConnectErrorResponse(connectRequest, response, code, detail) {
|
|
428
|
+
const isMqttReq = connectRequest.context.clientProtocol === "mqtt";
|
|
429
|
+
if (isMqttReq) {
|
|
430
|
+
const protocolVersion = connectRequest.mqtt.protocolVersion;
|
|
431
|
+
const mqttErrorResponse = {
|
|
432
|
+
mqtt: {
|
|
433
|
+
code: getMqttConnectCodeFromStatusCode(code, protocolVersion),
|
|
434
|
+
reason: detail,
|
|
435
|
+
},
|
|
436
|
+
};
|
|
437
|
+
response.statusCode = code;
|
|
438
|
+
response.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
439
|
+
response.end(JSON.stringify(mqttErrorResponse));
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
response.statusCode = code;
|
|
443
|
+
response.end(detail !== null && detail !== void 0 ? detail : "");
|
|
444
|
+
}
|
|
445
|
+
}
|
|
178
446
|
function isWebPubSubRequest(req) {
|
|
179
447
|
return getHttpHeader(req, "ce-awpsversion") !== undefined;
|
|
180
448
|
}
|
|
449
|
+
function isMqttRequest(req) {
|
|
450
|
+
const subprotocol = getHttpHeader(req, "ce-subprotocol");
|
|
451
|
+
const physicalConnectionId = getHttpHeader(req, "ce-physicalConnectionId");
|
|
452
|
+
return (subprotocol !== undefined &&
|
|
453
|
+
subprotocol.toLowerCase().includes("mqtt") &&
|
|
454
|
+
physicalConnectionId !== undefined);
|
|
455
|
+
}
|
|
181
456
|
async function readUserEventRequest(request, origin) {
|
|
182
457
|
const contentTypeheader = getHttpHeader(request, "content-type");
|
|
183
458
|
if (contentTypeheader === undefined) {
|
|
@@ -268,10 +543,13 @@ class CloudEventsDispatcher {
|
|
|
268
543
|
if ((hub === null || hub === void 0 ? void 0 : hub.toUpperCase()) !== this.hub.toUpperCase()) {
|
|
269
544
|
return false;
|
|
270
545
|
}
|
|
546
|
+
const isMqtt = isMqttRequest(request);
|
|
271
547
|
// No need to read body if handler is not specified
|
|
272
548
|
switch (eventType) {
|
|
273
549
|
case EventType.Connect:
|
|
274
550
|
if (!((_a = this.eventHandler) === null || _a === void 0 ? void 0 : _a.handleConnect)) {
|
|
551
|
+
if (isMqtt)
|
|
552
|
+
response.statusCode = 204;
|
|
275
553
|
response.end();
|
|
276
554
|
return true;
|
|
277
555
|
}
|
|
@@ -300,7 +578,9 @@ class CloudEventsDispatcher {
|
|
|
300
578
|
}
|
|
301
579
|
switch (eventType) {
|
|
302
580
|
case EventType.Connect: {
|
|
303
|
-
const connectRequest =
|
|
581
|
+
const connectRequest = isMqtt
|
|
582
|
+
? await readSystemEventRequest(request, origin)
|
|
583
|
+
: await readSystemEventRequest(request, origin);
|
|
304
584
|
// service passes out query property, assign it to queries
|
|
305
585
|
connectRequest.queries = connectRequest.query;
|
|
306
586
|
logger.verbose(connectRequest);
|
|
@@ -318,7 +598,9 @@ class CloudEventsDispatcher {
|
|
|
318
598
|
case EventType.Disconnected: {
|
|
319
599
|
// for unblocking events, we responds to the service as early as possible
|
|
320
600
|
response.end();
|
|
321
|
-
const disconnectedRequest =
|
|
601
|
+
const disconnectedRequest = isMqtt
|
|
602
|
+
? await readSystemEventRequest(request, origin)
|
|
603
|
+
: await readSystemEventRequest(request, origin);
|
|
322
604
|
logger.verbose(disconnectedRequest);
|
|
323
605
|
this.eventHandler.onDisconnected(disconnectedRequest);
|
|
324
606
|
return true;
|
|
@@ -413,5 +695,188 @@ class WebPubSubEventHandler {
|
|
|
413
695
|
}
|
|
414
696
|
}
|
|
415
697
|
|
|
698
|
+
// Copyright (c) Microsoft Corporation.
|
|
699
|
+
// Licensed under the MIT License.
|
|
700
|
+
/**
|
|
701
|
+
* MQTT 5.0 Disconnect Reason Codes.
|
|
702
|
+
*/
|
|
703
|
+
exports.MqttDisconnectReasonCode = void 0;
|
|
704
|
+
(function (MqttDisconnectReasonCode) {
|
|
705
|
+
/**
|
|
706
|
+
* 0x00 - Normal disconnection
|
|
707
|
+
* Sent by: Client or Server
|
|
708
|
+
* Description: Close the connection normally. Do not send the Will Message.
|
|
709
|
+
*/
|
|
710
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["NormalDisconnection"] = 0] = "NormalDisconnection";
|
|
711
|
+
/**
|
|
712
|
+
* 0x04 - Disconnect with Will Message
|
|
713
|
+
* Sent by: Client
|
|
714
|
+
* Description: The Client wishes to disconnect but requires that the Server also publishes its Will Message.
|
|
715
|
+
*/
|
|
716
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["DisconnectWithWillMessage"] = 4] = "DisconnectWithWillMessage";
|
|
717
|
+
/**
|
|
718
|
+
* 0x80 - Unspecified error
|
|
719
|
+
* Sent by: Client or Server
|
|
720
|
+
* Description: The Connection is closed but the sender either does not wish to reveal the reason, or none of the other Reason Codes apply.
|
|
721
|
+
*/
|
|
722
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["UnspecifiedError"] = 128] = "UnspecifiedError";
|
|
723
|
+
/**
|
|
724
|
+
* 0x81 - Malformed Packet
|
|
725
|
+
* Sent by: Client or Server
|
|
726
|
+
* Description: The received packet does not conform to this specification.
|
|
727
|
+
*/
|
|
728
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["MalformedPacket"] = 129] = "MalformedPacket";
|
|
729
|
+
/**
|
|
730
|
+
* 0x82 - Protocol Error
|
|
731
|
+
* Sent by: Client or Server
|
|
732
|
+
* Description: An unexpected or out of order packet was received.
|
|
733
|
+
*/
|
|
734
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["ProtocolError"] = 130] = "ProtocolError";
|
|
735
|
+
/**
|
|
736
|
+
* 0x83 - Implementation specific error
|
|
737
|
+
* Sent by: Client or Server
|
|
738
|
+
* Description: The packet received is valid but cannot be processed by this implementation.
|
|
739
|
+
*/
|
|
740
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["ImplementationSpecificError"] = 131] = "ImplementationSpecificError";
|
|
741
|
+
/**
|
|
742
|
+
* 0x87 - Not authorized
|
|
743
|
+
* Sent by: Server
|
|
744
|
+
* Description: The request is not authorized.
|
|
745
|
+
*/
|
|
746
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["NotAuthorized"] = 135] = "NotAuthorized";
|
|
747
|
+
/**
|
|
748
|
+
* 0x89 - Server busy
|
|
749
|
+
* Sent by: Server
|
|
750
|
+
* Description: The Server is busy and cannot continue processing requests from this Client.
|
|
751
|
+
*/
|
|
752
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["ServerBusy"] = 137] = "ServerBusy";
|
|
753
|
+
/**
|
|
754
|
+
* 0x8B - Server shutting down
|
|
755
|
+
* Sent by: Server
|
|
756
|
+
* Description: The Server is shutting down.
|
|
757
|
+
*/
|
|
758
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["ServerShuttingDown"] = 139] = "ServerShuttingDown";
|
|
759
|
+
/**
|
|
760
|
+
* 0x8D - Keep Alive timeout
|
|
761
|
+
* Sent by: Server
|
|
762
|
+
* Description: The Connection is closed because no packet has been received for 1.5 times the Keepalive time.
|
|
763
|
+
*/
|
|
764
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["KeepAliveTimeout"] = 141] = "KeepAliveTimeout";
|
|
765
|
+
/**
|
|
766
|
+
* 0x8E - Session taken over
|
|
767
|
+
* Sent by: Server
|
|
768
|
+
* Description: Another Connection using the same ClientID has connected causing this Connection to be closed.
|
|
769
|
+
*/
|
|
770
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["SessionTakenOver"] = 142] = "SessionTakenOver";
|
|
771
|
+
/**
|
|
772
|
+
* 0x8F - Topic Filter invalid
|
|
773
|
+
* Sent by: Server
|
|
774
|
+
* Description: The Topic Filter is correctly formed, but is not accepted by this Server.
|
|
775
|
+
*/
|
|
776
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["TopicFilterInvalid"] = 143] = "TopicFilterInvalid";
|
|
777
|
+
/**
|
|
778
|
+
* 0x90 - Topic Name invalid
|
|
779
|
+
* Sent by: Client or Server
|
|
780
|
+
* Description: The Topic Name is correctly formed, but is not accepted by this Client or Server.
|
|
781
|
+
*/
|
|
782
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["TopicNameInvalid"] = 144] = "TopicNameInvalid";
|
|
783
|
+
/**
|
|
784
|
+
* 0x93 - Receive Maximum exceeded
|
|
785
|
+
* Sent by: Client or Server
|
|
786
|
+
* Description: The Client or Server has received more than Receive Maximum publication for which it has not sent PUBACK or PUBCOMP.
|
|
787
|
+
*/
|
|
788
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["ReceiveMaximumExceeded"] = 147] = "ReceiveMaximumExceeded";
|
|
789
|
+
/**
|
|
790
|
+
* 0x94 - Topic Alias invalid
|
|
791
|
+
* Sent by: Client or Server
|
|
792
|
+
* Description: The Client or Server has received a PUBLISH packet containing a Topic Alias which is greater than the Maximum Topic Alias it sent in the CONNECT or CONNACK packet.
|
|
793
|
+
*/
|
|
794
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["TopicAliasInvalid"] = 148] = "TopicAliasInvalid";
|
|
795
|
+
/**
|
|
796
|
+
* 0x95 - Packet too large
|
|
797
|
+
* Sent by: Client or Server
|
|
798
|
+
* Description: The packet size is greater than Maximum Packet Size for this Client or Server.
|
|
799
|
+
*/
|
|
800
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["PacketTooLarge"] = 149] = "PacketTooLarge";
|
|
801
|
+
/**
|
|
802
|
+
* 0x96 - Message rate too high
|
|
803
|
+
* Sent by: Client or Server
|
|
804
|
+
* Description: The received data rate is too high.
|
|
805
|
+
*/
|
|
806
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["MessageRateTooHigh"] = 150] = "MessageRateTooHigh";
|
|
807
|
+
/**
|
|
808
|
+
* 0x97 - Quota exceeded
|
|
809
|
+
* Sent by: Client or Server
|
|
810
|
+
* Description: An implementation or administrative imposed limit has been exceeded.
|
|
811
|
+
*/
|
|
812
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["QuotaExceeded"] = 151] = "QuotaExceeded";
|
|
813
|
+
/**
|
|
814
|
+
* 0x98 - Administrative action
|
|
815
|
+
* Sent by: Client or Server
|
|
816
|
+
* Description: The Connection is closed due to an administrative action.
|
|
817
|
+
*/
|
|
818
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["AdministrativeAction"] = 152] = "AdministrativeAction";
|
|
819
|
+
/**
|
|
820
|
+
* 0x99 - Payload format invalid
|
|
821
|
+
* Sent by: Client or Server
|
|
822
|
+
* Description: The payload format does not match the one specified by the Payload Format Indicator.
|
|
823
|
+
*/
|
|
824
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["PayloadFormatInvalid"] = 153] = "PayloadFormatInvalid";
|
|
825
|
+
/**
|
|
826
|
+
* 0x9A - Retain not supported
|
|
827
|
+
* Sent by: Server
|
|
828
|
+
* Description: The Server does not support retained messages.
|
|
829
|
+
*/
|
|
830
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["RetainNotSupported"] = 154] = "RetainNotSupported";
|
|
831
|
+
/**
|
|
832
|
+
* 0x9B - QoS not supported
|
|
833
|
+
* Sent by: Server
|
|
834
|
+
* Description: The Client specified a QoS greater than the QoS specified in a Maximum QoS in the CONNACK.
|
|
835
|
+
*/
|
|
836
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["QosNotSupported"] = 155] = "QosNotSupported";
|
|
837
|
+
/**
|
|
838
|
+
* 0x9C - Use another server
|
|
839
|
+
* Sent by: Server
|
|
840
|
+
* Description: The Client should temporarily change its Server.
|
|
841
|
+
*/
|
|
842
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["UseAnotherServer"] = 156] = "UseAnotherServer";
|
|
843
|
+
/**
|
|
844
|
+
* 0x9D - Server moved
|
|
845
|
+
* Sent by: Server
|
|
846
|
+
* Description: The Server is moved and the Client should permanently change its server location.
|
|
847
|
+
*/
|
|
848
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["ServerMoved"] = 157] = "ServerMoved";
|
|
849
|
+
/**
|
|
850
|
+
* 0x9E - Shared Subscriptions not supported
|
|
851
|
+
* Sent by: Server
|
|
852
|
+
* Description: The Server does not support Shared Subscriptions.
|
|
853
|
+
*/
|
|
854
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["SharedSubscriptionsNotSupported"] = 158] = "SharedSubscriptionsNotSupported";
|
|
855
|
+
/**
|
|
856
|
+
* 0x9F - Connection rate exceeded
|
|
857
|
+
* Sent by: Server
|
|
858
|
+
* Description: This connection is closed because the connection rate is too high.
|
|
859
|
+
*/
|
|
860
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["ConnectionRateExceeded"] = 159] = "ConnectionRateExceeded";
|
|
861
|
+
/**
|
|
862
|
+
* 0xA0 - Maximum connect time
|
|
863
|
+
* Sent by: Server
|
|
864
|
+
* Description: The maximum connection time authorized for this connection has been exceeded.
|
|
865
|
+
*/
|
|
866
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["MaximumConnectTime"] = 160] = "MaximumConnectTime";
|
|
867
|
+
/**
|
|
868
|
+
* 0xA1 - Subscription Identifiers not supported
|
|
869
|
+
* Sent by: Server
|
|
870
|
+
* Description: The Server does not support Subscription Identifiers; the subscription is not accepted.
|
|
871
|
+
*/
|
|
872
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["SubscriptionIdentifiersNotSupported"] = 161] = "SubscriptionIdentifiersNotSupported";
|
|
873
|
+
/**
|
|
874
|
+
* 0xA2 - Wildcard Subscriptions not supported
|
|
875
|
+
* Sent by: Server
|
|
876
|
+
* Description: The Server does not support Wildcard Subscriptions; the subscription is not accepted.
|
|
877
|
+
*/
|
|
878
|
+
MqttDisconnectReasonCode[MqttDisconnectReasonCode["WildcardSubscriptionsNotSupported"] = 162] = "WildcardSubscriptionsNotSupported";
|
|
879
|
+
})(exports.MqttDisconnectReasonCode || (exports.MqttDisconnectReasonCode = {}));
|
|
880
|
+
|
|
416
881
|
exports.WebPubSubEventHandler = WebPubSubEventHandler;
|
|
417
882
|
//# sourceMappingURL=index.js.map
|