@transitive-sdk/utils-web 0.12.1 → 0.12.3
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 +33 -15
- package/package.json +1 -1
package/dist/utils-web.js
CHANGED
|
@@ -374,7 +374,11 @@ var require_common = __commonJS({
|
|
|
374
374
|
callback && callback(count);
|
|
375
375
|
}, delay);
|
|
376
376
|
};
|
|
377
|
-
var getRandomId = () =>
|
|
377
|
+
var getRandomId = (bytes = 6) => {
|
|
378
|
+
const buffer = new Uint8Array(bytes);
|
|
379
|
+
crypto.getRandomValues(buffer);
|
|
380
|
+
return buffer.reduce((memo, i) => memo + i.toString(36), "");
|
|
381
|
+
};
|
|
378
382
|
var versionCompare = (a, b) => semverCompare(semverMinVersion(a), semverMinVersion(b));
|
|
379
383
|
var mergeVersions2 = (versionsObject, subTopic = void 0, options = {}) => {
|
|
380
384
|
if (!versionsObject) {
|
|
@@ -649,7 +653,8 @@ var require_MqttSync = __commonJS({
|
|
|
649
653
|
isSubTopicOf,
|
|
650
654
|
versionCompare,
|
|
651
655
|
encodeTopicElement,
|
|
652
|
-
visitAncestor
|
|
656
|
+
visitAncestor,
|
|
657
|
+
getRandomId
|
|
653
658
|
} = require_common();
|
|
654
659
|
var { DataCache } = require_DataCache();
|
|
655
660
|
var log2 = getLogger2("MqttSync");
|
|
@@ -1086,16 +1091,24 @@ var require_MqttSync = __commonJS({
|
|
|
1086
1091
|
* Remote Procedure Calls (RPC)
|
|
1087
1092
|
*/
|
|
1088
1093
|
/* Handle RPC requests */
|
|
1089
|
-
handleRPCRequest(topic, json) {
|
|
1094
|
+
async handleRPCRequest(topic, json) {
|
|
1090
1095
|
log2.debug("handling RPC request for", topic, json);
|
|
1091
1096
|
const handler = this.rpcHandlers[topic];
|
|
1092
1097
|
const result = handler(json.args);
|
|
1093
1098
|
const responseTopic = `${topic.replace("/request", "/response")}/${json.id}`;
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
+
if (result instanceof Promise) {
|
|
1100
|
+
result.then((resultValue) => this.mqtt.publish(
|
|
1101
|
+
responseTopic,
|
|
1102
|
+
JSON.stringify({ id: json.id, result: resultValue }),
|
|
1103
|
+
{ retain: false, qos: 2 }
|
|
1104
|
+
));
|
|
1105
|
+
} else {
|
|
1106
|
+
this.mqtt.publish(
|
|
1107
|
+
responseTopic,
|
|
1108
|
+
JSON.stringify({ id: json.id, result }),
|
|
1109
|
+
{ retain: false, qos: 2 }
|
|
1110
|
+
);
|
|
1111
|
+
}
|
|
1099
1112
|
}
|
|
1100
1113
|
/* Handle RPC response */
|
|
1101
1114
|
handleRPCResponse(topic, json) {
|
|
@@ -1106,8 +1119,8 @@ var require_MqttSync = __commonJS({
|
|
|
1106
1119
|
}
|
|
1107
1120
|
/** Register an RPC request handler. Example:
|
|
1108
1121
|
* ```js
|
|
1109
|
-
* mqttSync.register('/
|
|
1110
|
-
* log.debug('running /
|
|
1122
|
+
* mqttSync.register('/mySquare', arg => {
|
|
1123
|
+
* log.debug('running /mySquare with args', arg);
|
|
1111
1124
|
* return arg * arg;
|
|
1112
1125
|
* });
|
|
1113
1126
|
* ```
|
|
@@ -1117,6 +1130,11 @@ var require_MqttSync = __commonJS({
|
|
|
1117
1130
|
* topics in their respective namespace. In the cloud and on the web you will
|
|
1118
1131
|
* need to use the respective namespace, i.e.,
|
|
1119
1132
|
* `/orgId/deviceId/@scope/capName/capVersion/`.
|
|
1133
|
+
*
|
|
1134
|
+
* #### Async/Await
|
|
1135
|
+
* Yes, you can make the handler `async` and use `await` inside of it. This
|
|
1136
|
+
* will be handled correctly, i.e., MqttSync will await the result of the
|
|
1137
|
+
* handler before responding to the RPC request client.
|
|
1120
1138
|
*/
|
|
1121
1139
|
register(command, handler) {
|
|
1122
1140
|
log2.debug("registering RPC handler for", command);
|
|
@@ -1132,19 +1150,19 @@ var require_MqttSync = __commonJS({
|
|
|
1132
1150
|
}
|
|
1133
1151
|
/** Make an RPC request. Example:
|
|
1134
1152
|
* ```js
|
|
1135
|
-
* mqttSync.call('/
|
|
1136
|
-
* log.debug(`Called /
|
|
1153
|
+
* mqttSync.call('/mySquare', 11, result => {
|
|
1154
|
+
* log.debug(`Called /mySquare with arg 11 and got ${result}`);
|
|
1137
1155
|
* });
|
|
1138
1156
|
* ```
|
|
1139
1157
|
* Alternative you can omit the callback and use async/await:
|
|
1140
1158
|
* ```js
|
|
1141
|
-
* const result = await mqttSync.call('/
|
|
1142
|
-
* log.debug(`Called /
|
|
1159
|
+
* const result = await mqttSync.call('/mySquare', 11);
|
|
1160
|
+
* log.debug(`Called /mySquare with arg 11 and got ${result}`);
|
|
1143
1161
|
* ```
|
|
1144
1162
|
* See the note about namespaces in `register`.
|
|
1145
1163
|
*/
|
|
1146
1164
|
call(command, args, callback = void 0) {
|
|
1147
|
-
const id =
|
|
1165
|
+
const id = getRandomId();
|
|
1148
1166
|
const responseTopic = `${command}/response/${id}`;
|
|
1149
1167
|
this.mqtt.subscribe(responseTopic, { rap: true, qos: 2 }, (err, granted) => {
|
|
1150
1168
|
if (err) {
|