This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
common.js
239 lines (207 loc) · 6.75 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
// node native modules
var fs = require('fs');
// external dependencies
var chalk = require('chalk');
var prettyjson = require('prettyjson');
var uuid = require('uuid');
var _ = require('lodash');
// sdk dependencies
var errors = require('azure-iot-common').errors;
var Message = require('azure-iot-common').Message;
var ConnectionString = require('azure-iothub').ConnectionString;
var SharedAccessSignature = require('azure-iothub').SharedAccessSignature;
function createDeviceConnectionString(deviceInfo, hubHostName) {
var cs = 'HostName=' + hubHostName + ';DeviceId=' + deviceInfo.deviceId;
if (deviceInfo.authentication.SymmetricKey.primaryKey) {
cs += ';SharedAccessKey=' + deviceInfo.authentication.SymmetricKey.primaryKey;
} else if (deviceInfo.authentication.SymmetricKey.secondaryKey) {
cs += ';SharedAccessKey=' + deviceInfo.authentication.SymmetricKey.secondaryKey;
} else if (deviceInfo.authentication.x509Thumbprint.primaryThumbprint || deviceInfo.authentication.x509Thumbprint.secondaryThumbprint || (deviceInfo.authentication.type === 'certificateAuthority')) {
cs += ';x509=true';
} else {
cs = null;
}
return cs;
}
function createMessageFromArgument(messageArg, ack) {
var message;
function createMessage (body) {
var msg = new Message(body);
msg.messageId = uuid.v4();
if (ack) {
msg.ack = ack;
}
return msg;
}
/**
* The message passed on the command line can either be the payload, or the message itself.
* To try and figure this out, we try to parse the message as JSON. If it works, we then look for the messageId property.
* If it can't be parse or if there is no messageId property then we treat the argument as a payload. If the argument is
* proper JSON and has a messageId property, we consider that the argument is a full message object.
*/
try {
var tmpMessage = JSON.parse(messageArg);
if (tmpMessage.messageId) {
message = _.merge(new Message(), tmpMessage);
message.ack = tmpMessage.ack || ack;
} else {
message = createMessage(messageArg);
}
} catch(e) {
if (e instanceof SyntaxError) {
message = createMessage(messageArg);
} else {
throw e;
}
}
return message;
}
function inputError(message) {
printErrorAndExit(message, 'Input Error:');
}
function printErrorWithHintAndExit(message, hint, prefix) {
printErrorAndExit(message + chalk.yellow.bold('Hint: ') + hint, prefix);
}
function printErrorAndExit(message, prefix) {
if (!prefix) {
prefix = 'Error:';
}
console.error(chalk.red.bold(prefix) + ' ' + message);
process.exit(1);
}
function serviceError(err) {
var message = err.toString();
if (message.lastIndexOf('Error:', 0) === 0) {
message = message.slice('Error:'.length);
}
printErrorAndExit(message);
}
function printSuccess(message) {
console.log(chalk.green(message));
}
/**
* printDevice will display a device either pretty-printed or as raw JSON.
*
* @param {any} device The device object received from the IoT hub registry.
* @param {any} hubHostName used to build the connection string.
* @param {any} propertyFilter Filter the properties that should be displayed.
* @param {any} rawOutput Boolean indicating whether the output should be pretty-printed or displayed as raw JSON.
*/
function printDevice(device, hubHostName, propertyFilter, rawOutput) {
var output = createDeviceJSONObject(device, hubHostName, propertyFilter);
output = rawOutput ? JSON.stringify(output) : prettyjson.render(output);
console.log(output);
}
function createDeviceJSONObject(device, hubHostName, propertyFilter) {
var filtered = {};
if (propertyFilter) {
var props = propertyFilter.split(',');
props.forEach(function (prop) {
prop = prop.trim();
var parts = prop.split('.');
var src = device;
var dst = filtered;
for (var i = 0; i < parts.length; ++i) {
var part = parts[i];
if (src[part]) {
if (i + 1 === parts.length) {
dst[part] = src[part];
}
else {
dst[part] = {};
src = src[part];
dst = dst[part];
}
}
}
});
}
else {
filtered = device;
}
var result = filtered;
result.connectionString = createDeviceConnectionString(device, hubHostName);
return result;
}
function configLoc() {
if (process.platform === 'darwin') {
return {
dir: process.env.HOME + '/Library/Application Support/iothub-explorer',
file: 'config'
};
}
else if (process.platform === 'linux') {
return {
dir: process.env.HOME,
file: '.iothub-explorer'
};
}
else if (process.platform === 'win32') {
return {
dir: process.env.LOCALAPPDATA + '/iothub-explorer',
file: 'config'
};
}
else {
inputError('\'login\' not supported on this platform');
}
}
function loadSasFromUserFile() {
var sas;
var loc = configLoc();
try {
sas = fs.readFileSync(loc.dir + '/' + loc.file, 'utf8');
}
catch (err) { // swallow file not found exception
if (err.code !== 'ENOENT') throw err;
}
return sas;
}
function getSas(connectionString) {
var sas;
if (connectionString) {
var cn;
try {
cn = ConnectionString.parse(connectionString);
} catch (e) {
if (e instanceof errors.ArgumentError) {
inputError('Could not parse connection string: ' + connectionString);
} else {
throw e;
}
}
var expiry = Math.floor(Date.now() / 1000) + 3600;
sas = SharedAccessSignature.create(cn.HostName, cn.SharedAccessKeyName, cn.SharedAccessKey, expiry).toString();
} else {
sas = loadSasFromUserFile();
}
if (!sas) {
inputError('You must either use the login command or the --login argument for iothub-explorer to authenticate with your IoT Hub instance');
}
return sas;
}
function getHostFromSas(sas) {
return SharedAccessSignature.parse(sas).sr;
}
function showDeprecationText(newCommand) {
console.log(chalk.bold(chalk.red("The equivalent command in the Azure CLI is: ") + chalk.green(newCommand)));
console.log('--------');
}
module.exports = {
inputError: inputError,
serviceError: serviceError,
printSuccess: printSuccess,
printErrorWithHintAndExit: printErrorWithHintAndExit,
printErrorAndExit: printErrorAndExit,
printDevice: printDevice,
createDeviceJSONObject: createDeviceJSONObject,
getHostFromSas: getHostFromSas,
getSas: getSas,
configLoc: configLoc,
createMessageFromArgument: createMessageFromArgument,
createDeviceConnectionString: createDeviceConnectionString,
showDeprecationText: showDeprecationText
};