Skip to content

Commit 61c3f14

Browse files
author
Bret Ambrose
committed
Merge branch 'JsMqtt-7-Client' into JsMqtt-8-311
2 parents ca4498a + 8227937 commit 61c3f14

2 files changed

Lines changed: 30 additions & 30 deletions

File tree

lib/browser/mqtt_internal/client.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -233,30 +233,6 @@ function buildClientConfigLogString(prefix: string, config: ClientConfig) : stri
233233
return result;
234234
}
235235

236-
function validateUnsignedInteger(value: number, fieldName: string) {
237-
if (!Number.isInteger(value) || value < 0) {
238-
throw new CrtError(`Field "${fieldName}" with value "${value}" is not a valid unsigned integer`);
239-
}
240-
}
241-
242-
function validateOptionalUnsignedInteger(value: number | undefined, fieldName: string) {
243-
if (value === undefined) {
244-
return;
245-
}
246-
247-
validateUnsignedInteger(value, fieldName);
248-
}
249-
250-
function validateOptionalPositiveU32(value: number | undefined, fieldName: string) {
251-
if (value === undefined) {
252-
return;
253-
}
254-
255-
if (!Number.isInteger(value) || value <= 0 || value > (256 * 256 * 256 * 256 - 1)) {
256-
throw new CrtError(`Field "${fieldName}" with value "${value}" is not a valid positive 32 bit integer`);
257-
}
258-
}
259-
260236
function validateConnectOptions(options : ConnectOptions, mode : ProtocolMode) {
261237
if (options.connectPacketTransformer && (typeof options.connectPacketTransformer !== "function")) {
262238
throw new CrtError("connectPacketTransformer must be a function");
@@ -278,7 +254,7 @@ function validateConnectOptions(options : ConnectOptions, mode : ProtocolMode) {
278254
validate.validateU16(options.receiveMaximum, "receiveMaximum");
279255
}
280256

281-
validateOptionalPositiveU32(options.maximumPacketSizeBytes, "maximumPacketSizeBytes");
257+
validate.validateOptionalPositiveU32(options.maximumPacketSizeBytes, "maximumPacketSizeBytes");
282258
validate.validateOptionalU32(options.willDelayIntervalSeconds, "willDelayIntervalSeconds");
283259

284260
if (options.will !== undefined) {
@@ -299,21 +275,21 @@ function validateConfig(config: ClientConfig) {
299275

300276
validateConnectOptions(config.connectOptions, config.protocolVersion);
301277

302-
validateOptionalUnsignedInteger(config.pingTimeoutMillis, "pingTimeoutMillis");
278+
validate.validateOptionalUnsignedInteger(config.pingTimeoutMillis, "pingTimeoutMillis");
303279

304280
if (!config.connectionFactory || (typeof config.connectionFactory !== "function")) {
305281
throw new CrtError("connectionFactory must be a valid function");
306282
}
307283

308-
validateUnsignedInteger(config.connectTimeoutMillis, "connectTimeoutMillis");
284+
validate.validateUnsignedInteger(config.connectTimeoutMillis, "connectTimeoutMillis");
309285

310286
if (config.retryJitterMode !== undefined && mqtt5.RetryJitterType[config.retryJitterMode] == undefined) {
311287
throw new CrtError("Invalid value for retryJitterMode");
312288
}
313289

314-
validateOptionalUnsignedInteger(config.minReconnectDelayMs, "minReconnectDelayMs");
315-
validateOptionalUnsignedInteger(config.maxReconnectDelayMs, "maxReconnectDelayMs");
316-
validateOptionalUnsignedInteger(config.resetConnectionFailureCountMillis, "resetConnectionFailureCountMillis");
290+
validate.validateOptionalUnsignedInteger(config.minReconnectDelayMs, "minReconnectDelayMs");
291+
validate.validateOptionalUnsignedInteger(config.maxReconnectDelayMs, "maxReconnectDelayMs");
292+
validate.validateOptionalUnsignedInteger(config.resetConnectionFailureCountMillis, "resetConnectionFailureCountMillis");
317293

318294
if (config.resubscribeMode !== undefined && ResubscribeModeType[config.resubscribeMode] == undefined) {
319295
throw new CrtError("Invalid value for resubscribeMode");

lib/browser/mqtt_internal/validate.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,30 @@ function validateOptionalVli(value: number | undefined, fieldName: string) {
170170
}
171171
}
172172

173+
export function validateUnsignedInteger(value: number, fieldName: string) {
174+
if (!Number.isInteger(value) || value < 0) {
175+
throw new CrtError(`Field "${fieldName}" with value "${value}" is not a valid unsigned integer`);
176+
}
177+
}
178+
179+
export function validateOptionalUnsignedInteger(value: number | undefined, fieldName: string) {
180+
if (value === undefined) {
181+
return;
182+
}
183+
184+
validateUnsignedInteger(value, fieldName);
185+
}
186+
187+
export function validateOptionalPositiveU32(value: number | undefined, fieldName: string) {
188+
if (value === undefined) {
189+
return;
190+
}
191+
192+
if (!Number.isInteger(value) || value <= 0 || value > (256 * 256 * 256 * 256 - 1)) {
193+
throw new CrtError(`Field "${fieldName}" with value "${value}" is not a valid positive 32 bit integer`);
194+
}
195+
}
196+
173197
// we don't validate length here because we don't know encoding length without doing a utf-8 conversion.
174198
// This means we validate string length in the internal validators which checks ArrayBuffer lengths.
175199
function validateString(value: any, fieldName: string) {

0 commit comments

Comments
 (0)