@transitive-sdk/utils-web 0.12.0 → 0.12.2
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/dist/utils-web.js +42 -37
- package/package.json +1 -1
package/dist/utils-web.js
CHANGED
|
@@ -655,7 +655,6 @@ var require_MqttSync = __commonJS({
|
|
|
655
655
|
var log2 = getLogger2("MqttSync");
|
|
656
656
|
var HEARTBEAT_TOPIC = "$SYS/broker/uptime";
|
|
657
657
|
var specialKey = "$_";
|
|
658
|
-
var RPC_PREFIX = "_RPCv1_";
|
|
659
658
|
var noop = () => {
|
|
660
659
|
};
|
|
661
660
|
var clone2 = (payload) => {
|
|
@@ -733,8 +732,10 @@ var require_MqttSync = __commonJS({
|
|
|
733
732
|
topic = pathToTopic2(path);
|
|
734
733
|
}
|
|
735
734
|
const json = mqttParsePayload(payload);
|
|
736
|
-
if (
|
|
737
|
-
this.
|
|
735
|
+
if (this.rpcHandlers[topic]) {
|
|
736
|
+
this.handleRPCRequest(topic, json);
|
|
737
|
+
} else if (this.rpcCallbacks[topic]) {
|
|
738
|
+
this.handleRPCResponse(topic, json);
|
|
738
739
|
} else if (this.isPublished(topic)) {
|
|
739
740
|
this.publishedMessages.updateFromArray([...path, specialKey], json);
|
|
740
741
|
this.data.update(topic, json, { external: true });
|
|
@@ -1084,38 +1085,37 @@ var require_MqttSync = __commonJS({
|
|
|
1084
1085
|
/* --------------------------------------------------------------------------
|
|
1085
1086
|
* Remote Procedure Calls (RPC)
|
|
1086
1087
|
*/
|
|
1087
|
-
/* Handle RPC requests
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1088
|
+
/* Handle RPC requests */
|
|
1089
|
+
async handleRPCRequest(topic, json) {
|
|
1090
|
+
log2.debug("handling RPC request for", topic, json);
|
|
1091
|
+
const handler = this.rpcHandlers[topic];
|
|
1092
|
+
const result = handler(json.args);
|
|
1093
|
+
const responseTopic = `${topic.replace("/request", "/response")}/${json.id}`;
|
|
1094
|
+
if (result instanceof Promise) {
|
|
1095
|
+
result.then((resultValue) => this.mqtt.publish(
|
|
1096
|
+
responseTopic,
|
|
1097
|
+
JSON.stringify({ id: json.id, result: resultValue }),
|
|
1098
|
+
{ retain: false, qos: 2 }
|
|
1099
|
+
));
|
|
1100
|
+
} else {
|
|
1100
1101
|
this.mqtt.publish(
|
|
1101
1102
|
responseTopic,
|
|
1102
|
-
JSON.stringify({ id: json.id,
|
|
1103
|
+
JSON.stringify({ id: json.id, result }),
|
|
1103
1104
|
{ retain: false, qos: 2 }
|
|
1104
1105
|
);
|
|
1105
|
-
} else if (type == "response") {
|
|
1106
|
-
const requestId = path[1];
|
|
1107
|
-
log2.debug("handle RPC response", requestId, json);
|
|
1108
|
-
this.rpcCallbacks[requestId]?.(json.result);
|
|
1109
|
-
delete this.rpcCallbacks[requestId];
|
|
1110
|
-
this.mqtt.unsubscribe(`/${RPC_PREFIX}/response/${requestId}`);
|
|
1111
|
-
} else {
|
|
1112
|
-
log2.warn("Unknown RPC type", path);
|
|
1113
1106
|
}
|
|
1114
1107
|
}
|
|
1108
|
+
/* Handle RPC response */
|
|
1109
|
+
handleRPCResponse(topic, json) {
|
|
1110
|
+
log2.debug("handle RPC response", topic, json);
|
|
1111
|
+
this.rpcCallbacks[topic](json.result);
|
|
1112
|
+
delete this.rpcCallbacks[topic];
|
|
1113
|
+
this.mqtt.unsubscribe(topic);
|
|
1114
|
+
}
|
|
1115
1115
|
/** Register an RPC request handler. Example:
|
|
1116
1116
|
* ```js
|
|
1117
|
-
* mqttSync.register('/
|
|
1118
|
-
* log.debug('running /
|
|
1117
|
+
* mqttSync.register('/mySquare', arg => {
|
|
1118
|
+
* log.debug('running /mySquare with args', arg);
|
|
1119
1119
|
* return arg * arg;
|
|
1120
1120
|
* });
|
|
1121
1121
|
* ```
|
|
@@ -1125,11 +1125,16 @@ var require_MqttSync = __commonJS({
|
|
|
1125
1125
|
* topics in their respective namespace. In the cloud and on the web you will
|
|
1126
1126
|
* need to use the respective namespace, i.e.,
|
|
1127
1127
|
* `/orgId/deviceId/@scope/capName/capVersion/`.
|
|
1128
|
+
*
|
|
1129
|
+
* #### Async/Await
|
|
1130
|
+
* Yes, you can make the handler `async` and use `await` inside of it. This
|
|
1131
|
+
* will be handled correctly, i.e., MqttSync will await the result of the
|
|
1132
|
+
* handler before responding to the RPC request client.
|
|
1128
1133
|
*/
|
|
1129
1134
|
register(command, handler) {
|
|
1130
1135
|
log2.debug("registering RPC handler for", command);
|
|
1131
|
-
|
|
1132
|
-
|
|
1136
|
+
const requestTopic = `${command}/request`;
|
|
1137
|
+
this.rpcHandlers[requestTopic] = handler;
|
|
1133
1138
|
this.mqtt.subscribe(requestTopic, { rap: true, qos: 2 }, (err, granted) => {
|
|
1134
1139
|
if (err) {
|
|
1135
1140
|
log2.warn(`Error subscribing to RPC topic ${requestTopic}`, err);
|
|
@@ -1140,20 +1145,20 @@ var require_MqttSync = __commonJS({
|
|
|
1140
1145
|
}
|
|
1141
1146
|
/** Make an RPC request. Example:
|
|
1142
1147
|
* ```js
|
|
1143
|
-
* mqttSync.call('/
|
|
1144
|
-
* log.debug(`Called /
|
|
1148
|
+
* mqttSync.call('/mySquare', 11, result => {
|
|
1149
|
+
* log.debug(`Called /mySquare with arg 11 and got ${result}`);
|
|
1145
1150
|
* });
|
|
1146
1151
|
* ```
|
|
1147
1152
|
* Alternative you can omit the callback and use async/await:
|
|
1148
1153
|
* ```js
|
|
1149
|
-
* const result = await mqttSync.call('/
|
|
1150
|
-
* log.debug(`Called /
|
|
1154
|
+
* const result = await mqttSync.call('/mySquare', 11);
|
|
1155
|
+
* log.debug(`Called /mySquare with arg 11 and got ${result}`);
|
|
1151
1156
|
* ```
|
|
1152
1157
|
* See the note about namespaces in `register`.
|
|
1153
1158
|
*/
|
|
1154
1159
|
call(command, args, callback = void 0) {
|
|
1155
1160
|
const id = crypto.randomUUID();
|
|
1156
|
-
const responseTopic =
|
|
1161
|
+
const responseTopic = `${command}/response/${id}`;
|
|
1157
1162
|
this.mqtt.subscribe(responseTopic, { rap: true, qos: 2 }, (err, granted) => {
|
|
1158
1163
|
if (err) {
|
|
1159
1164
|
log2.warn(`Error subscribing to RPC response topic ${responseTopic}`, err);
|
|
@@ -1161,7 +1166,7 @@ var require_MqttSync = __commonJS({
|
|
|
1161
1166
|
log2.warn(`Not allowed to subscribe to RPC response topic ${responseTopic}`);
|
|
1162
1167
|
}
|
|
1163
1168
|
});
|
|
1164
|
-
const requestTopic =
|
|
1169
|
+
const requestTopic = `${command}/request`;
|
|
1165
1170
|
log2.debug("calling RPC", requestTopic);
|
|
1166
1171
|
this.mqtt.publish(
|
|
1167
1172
|
requestTopic,
|
|
@@ -1169,10 +1174,10 @@ var require_MqttSync = __commonJS({
|
|
|
1169
1174
|
{ retain: false, qos: 2 }
|
|
1170
1175
|
);
|
|
1171
1176
|
if (callback) {
|
|
1172
|
-
this.rpcCallbacks[
|
|
1177
|
+
this.rpcCallbacks[responseTopic] = callback;
|
|
1173
1178
|
} else {
|
|
1174
1179
|
return new Promise((resolve, reject) => {
|
|
1175
|
-
this.rpcCallbacks[
|
|
1180
|
+
this.rpcCallbacks[responseTopic] = resolve;
|
|
1176
1181
|
});
|
|
1177
1182
|
}
|
|
1178
1183
|
}
|