Commit 58088be2 authored by 邓晓峰's avatar 邓晓峰

fix: 修复format function undefined

parent 019c4945
import _typeof from "@babel/runtime/helpers/esm/typeof"; import _typeof from "@babel/runtime/helpers/esm/typeof";
import { ERROR, MESSAGE_TYPE } from './consts'; import { ERROR, MESSAGE_TYPE } from './consts';
import Message from './Message'; import Message from './Message';
import WireMessage from './WireMessage';
/* eslint-disable */ /* eslint-disable */
/** /**
...@@ -20,10 +21,10 @@ export var validate = function validate(obj, keys) { ...@@ -20,10 +21,10 @@ export var validate = function validate(obj, keys) {
if (keys.hasOwnProperty(key)) { if (keys.hasOwnProperty(key)) {
if (_typeof(obj[key]) !== keys[key]) throw new Error(format(ERROR.INVALID_TYPE, [_typeof(obj[key]), key])); if (_typeof(obj[key]) !== keys[key]) throw new Error(format(ERROR.INVALID_TYPE, [_typeof(obj[key]), key]));
} else { } else {
var errorStr = "Unknown property, " + key + ". Valid properties are:"; var errorStr = 'Unknown property, ' + key + '. Valid properties are:';
for (var validKey in keys) { for (var validKey in keys) {
if (keys.hasOwnProperty(validKey)) errorStr = errorStr + " " + validKey; if (keys.hasOwnProperty(validKey)) errorStr = errorStr + ' ' + validKey;
} }
throw new Error(errorStr); throw new Error(errorStr);
...@@ -32,12 +33,12 @@ export var validate = function validate(obj, keys) { ...@@ -32,12 +33,12 @@ export var validate = function validate(obj, keys) {
} }
}; };
/** /**
* Format an error message text. * Format an error message text.
* @private * @private
* @param {error} ERROR value above. * @param {error} ERROR value above.
* @param {substitutions} [array] substituted into the text. * @param {substitutions} [array] substituted into the text.
* @return the text with the substitutions made. * @return the text with the substitutions made.
*/ */
export var format = function format(error, substitutions) { export var format = function format(error, substitutions) {
var text = error.text; var text = error.text;
...@@ -46,7 +47,7 @@ export var format = function format(error, substitutions) { ...@@ -46,7 +47,7 @@ export var format = function format(error, substitutions) {
var field, start; var field, start;
for (var i = 0; i < substitutions.length; i++) { for (var i = 0; i < substitutions.length; i++) {
field = "{" + i + "}"; field = '{' + i + '}';
start = text.indexOf(field); start = text.indexOf(field);
if (start > 0) { if (start > 0) {
...@@ -76,7 +77,7 @@ export function decodeMessage(input, pos) { ...@@ -76,7 +77,7 @@ export function decodeMessage(input, pos) {
} }
digit = input[pos++]; digit = input[pos++];
remLength += (digit & 0x7F) * multiplier; remLength += (digit & 0x7f) * multiplier;
multiplier *= 128; multiplier *= 128;
} while ((digit & 0x80) !== 0); } while ((digit & 0x80) !== 0);
...@@ -151,9 +152,9 @@ export function readUint16(buffer, offset) { ...@@ -151,9 +152,9 @@ export function readUint16(buffer, offset) {
return 256 * buffer[offset] + buffer[offset + 1]; return 256 * buffer[offset] + buffer[offset + 1];
} }
/** /**
* Encodes an MQTT Multi-Byte Integer * Encodes an MQTT Multi-Byte Integer
* @private * @private
*/ */
export function encodeMBI(number) { export function encodeMBI(number) {
var output = new Array(1); var output = new Array(1);
...@@ -173,9 +174,9 @@ export function encodeMBI(number) { ...@@ -173,9 +174,9 @@ export function encodeMBI(number) {
return output; return output;
} }
/** /**
* Takes a String and calculates its length in bytes when encoded in UTF8. * Takes a String and calculates its length in bytes when encoded in UTF8.
* @private * @private
*/ */
export function UTF8Length(input) { export function UTF8Length(input) {
var output = 0; var output = 0;
...@@ -183,23 +184,23 @@ export function UTF8Length(input) { ...@@ -183,23 +184,23 @@ export function UTF8Length(input) {
for (var i = 0; i < input.length; i++) { for (var i = 0; i < input.length; i++) {
var charCode = input.charCodeAt(i); var charCode = input.charCodeAt(i);
if (charCode > 0x7FF) { if (charCode > 0x7ff) {
// Surrogate pair means its a 4 byte character // Surrogate pair means its a 4 byte character
if (0xD800 <= charCode && charCode <= 0xDBFF) { if (0xd800 <= charCode && charCode <= 0xdbff) {
i++; i++;
output++; output++;
} }
output += 3; output += 3;
} else if (charCode > 0x7F) output += 2;else output++; } else if (charCode > 0x7f) output += 2;else output++;
} }
return output; return output;
} }
/** /**
* Takes a String and writes it into an array as UTF8 encoded bytes. * Takes a String and writes it into an array as UTF8 encoded bytes.
* @private * @private
*/ */
export function stringToUTF8(input, output, start) { export function stringToUTF8(input, output, start) {
var pos = start; var pos = start;
...@@ -207,37 +208,37 @@ export function stringToUTF8(input, output, start) { ...@@ -207,37 +208,37 @@ export function stringToUTF8(input, output, start) {
for (var i = 0; i < input.length; i++) { for (var i = 0; i < input.length; i++) {
var charCode = input.charCodeAt(i); // Check for a surrogate pair. var charCode = input.charCodeAt(i); // Check for a surrogate pair.
if (0xD800 <= charCode && charCode <= 0xDBFF) { if (0xd800 <= charCode && charCode <= 0xdbff) {
var lowCharCode = input.charCodeAt(++i); var lowCharCode = input.charCodeAt(++i);
if (isNaN(lowCharCode)) { if (isNaN(lowCharCode)) {
throw new Error(format(ERROR.MALFORMED_UNICODE, [charCode, lowCharCode])); throw new Error(format(ERROR.MALFORMED_UNICODE, [charCode, lowCharCode]));
} }
charCode = (charCode - 0xD800 << 10) + (lowCharCode - 0xDC00) + 0x10000; charCode = (charCode - 0xd800 << 10) + (lowCharCode - 0xdc00) + 0x10000;
} }
if (charCode <= 0x7F) { if (charCode <= 0x7f) {
output[pos++] = charCode; output[pos++] = charCode;
} else if (charCode <= 0x7FF) { } else if (charCode <= 0x7ff) {
output[pos++] = charCode >> 6 & 0x1F | 0xC0; output[pos++] = charCode >> 6 & 0x1f | 0xc0;
output[pos++] = charCode & 0x3F | 0x80; output[pos++] = charCode & 0x3f | 0x80;
} else if (charCode <= 0xFFFF) { } else if (charCode <= 0xffff) {
output[pos++] = charCode >> 12 & 0x0F | 0xE0; output[pos++] = charCode >> 12 & 0x0f | 0xe0;
output[pos++] = charCode >> 6 & 0x3F | 0x80; output[pos++] = charCode >> 6 & 0x3f | 0x80;
output[pos++] = charCode & 0x3F | 0x80; output[pos++] = charCode & 0x3f | 0x80;
} else { } else {
output[pos++] = charCode >> 18 & 0x07 | 0xF0; output[pos++] = charCode >> 18 & 0x07 | 0xf0;
output[pos++] = charCode >> 12 & 0x3F | 0x80; output[pos++] = charCode >> 12 & 0x3f | 0x80;
output[pos++] = charCode >> 6 & 0x3F | 0x80; output[pos++] = charCode >> 6 & 0x3f | 0x80;
output[pos++] = charCode & 0x3F | 0x80; output[pos++] = charCode & 0x3f | 0x80;
} }
} }
return output; return output;
} }
export function parseUTF8(input, offset, length) { export function parseUTF8(input, offset, length) {
var output = ""; var output = '';
var utf16; var utf16;
var pos = offset; var pos = offset;
...@@ -245,28 +246,28 @@ export function parseUTF8(input, offset, length) { ...@@ -245,28 +246,28 @@ export function parseUTF8(input, offset, length) {
var byte1 = input[pos++]; var byte1 = input[pos++];
if (byte1 < 128) utf16 = byte1;else { if (byte1 < 128) utf16 = byte1;else {
var byte2 = input[pos++] - 128; var byte2 = input[pos++] - 128;
if (byte2 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), ""])); if (byte2 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), '']));
if (byte1 < 0xE0) // 2 byte character if (byte1 < 0xe0) // 2 byte character
utf16 = 64 * (byte1 - 0xC0) + byte2;else { utf16 = 64 * (byte1 - 0xc0) + byte2;else {
var byte3 = input[pos++] - 128; var byte3 = input[pos++] - 128;
if (byte3 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16)])); if (byte3 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16)]));
if (byte1 < 0xF0) // 3 byte character if (byte1 < 0xf0) // 3 byte character
utf16 = 4096 * (byte1 - 0xE0) + 64 * byte2 + byte3;else { utf16 = 4096 * (byte1 - 0xe0) + 64 * byte2 + byte3;else {
var byte4 = input[pos++] - 128; var byte4 = input[pos++] - 128;
if (byte4 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)])); if (byte4 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
if (byte1 < 0xF8) // 4 byte character if (byte1 < 0xf8) // 4 byte character
utf16 = 262144 * (byte1 - 0xF0) + 4096 * byte2 + 64 * byte3 + byte4;else // longer encodings are not supported utf16 = 262144 * (byte1 - 0xf0) + 4096 * byte2 + 64 * byte3 + byte4; // longer encodings are not supported
throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)])); else throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
} }
} }
} }
if (utf16 > 0xFFFF) // 4 byte character - express as a surrogate pair if (utf16 > 0xffff) {
{ // 4 byte character - express as a surrogate pair
utf16 -= 0x10000; utf16 -= 0x10000;
output += String.fromCharCode(0xD800 + (utf16 >> 10)); // lead character output += String.fromCharCode(0xd800 + (utf16 >> 10)); // lead character
utf16 = 0xDC00 + (utf16 & 0x3FF); // trail character utf16 = 0xdc00 + (utf16 & 0x3ff); // trail character
} }
output += String.fromCharCode(utf16); output += String.fromCharCode(utf16);
......
...@@ -21,6 +21,8 @@ var _consts = require("./consts"); ...@@ -21,6 +21,8 @@ var _consts = require("./consts");
var _Message = _interopRequireDefault(require("./Message")); var _Message = _interopRequireDefault(require("./Message"));
var _WireMessage = _interopRequireDefault(require("./WireMessage"));
/* eslint-disable */ /* eslint-disable */
/** /**
...@@ -39,10 +41,10 @@ var validate = function validate(obj, keys) { ...@@ -39,10 +41,10 @@ var validate = function validate(obj, keys) {
if (keys.hasOwnProperty(key)) { if (keys.hasOwnProperty(key)) {
if ((0, _typeof2.default)(obj[key]) !== keys[key]) throw new Error(format(_consts.ERROR.INVALID_TYPE, [(0, _typeof2.default)(obj[key]), key])); if ((0, _typeof2.default)(obj[key]) !== keys[key]) throw new Error(format(_consts.ERROR.INVALID_TYPE, [(0, _typeof2.default)(obj[key]), key]));
} else { } else {
var errorStr = "Unknown property, " + key + ". Valid properties are:"; var errorStr = 'Unknown property, ' + key + '. Valid properties are:';
for (var validKey in keys) { for (var validKey in keys) {
if (keys.hasOwnProperty(validKey)) errorStr = errorStr + " " + validKey; if (keys.hasOwnProperty(validKey)) errorStr = errorStr + ' ' + validKey;
} }
throw new Error(errorStr); throw new Error(errorStr);
...@@ -51,12 +53,12 @@ var validate = function validate(obj, keys) { ...@@ -51,12 +53,12 @@ var validate = function validate(obj, keys) {
} }
}; };
/** /**
* Format an error message text. * Format an error message text.
* @private * @private
* @param {error} ERROR value above. * @param {error} ERROR value above.
* @param {substitutions} [array] substituted into the text. * @param {substitutions} [array] substituted into the text.
* @return the text with the substitutions made. * @return the text with the substitutions made.
*/ */
exports.validate = validate; exports.validate = validate;
...@@ -68,7 +70,7 @@ var format = function format(error, substitutions) { ...@@ -68,7 +70,7 @@ var format = function format(error, substitutions) {
var field, start; var field, start;
for (var i = 0; i < substitutions.length; i++) { for (var i = 0; i < substitutions.length; i++) {
field = "{" + i + "}"; field = '{' + i + '}';
start = text.indexOf(field); start = text.indexOf(field);
if (start > 0) { if (start > 0) {
...@@ -101,7 +103,7 @@ function decodeMessage(input, pos) { ...@@ -101,7 +103,7 @@ function decodeMessage(input, pos) {
} }
digit = input[pos++]; digit = input[pos++];
remLength += (digit & 0x7F) * multiplier; remLength += (digit & 0x7f) * multiplier;
multiplier *= 128; multiplier *= 128;
} while ((digit & 0x80) !== 0); } while ((digit & 0x80) !== 0);
...@@ -111,7 +113,7 @@ function decodeMessage(input, pos) { ...@@ -111,7 +113,7 @@ function decodeMessage(input, pos) {
return [null, startingPos]; return [null, startingPos];
} }
var wireMessage = new WireMessage(type); var wireMessage = new _WireMessage.default(type);
switch (type) { switch (type) {
case _consts.MESSAGE_TYPE.CONNACK: case _consts.MESSAGE_TYPE.CONNACK:
...@@ -179,9 +181,9 @@ function readUint16(buffer, offset) { ...@@ -179,9 +181,9 @@ function readUint16(buffer, offset) {
return 256 * buffer[offset] + buffer[offset + 1]; return 256 * buffer[offset] + buffer[offset + 1];
} }
/** /**
* Encodes an MQTT Multi-Byte Integer * Encodes an MQTT Multi-Byte Integer
* @private * @private
*/ */
function encodeMBI(number) { function encodeMBI(number) {
...@@ -202,9 +204,9 @@ function encodeMBI(number) { ...@@ -202,9 +204,9 @@ function encodeMBI(number) {
return output; return output;
} }
/** /**
* Takes a String and calculates its length in bytes when encoded in UTF8. * Takes a String and calculates its length in bytes when encoded in UTF8.
* @private * @private
*/ */
function UTF8Length(input) { function UTF8Length(input) {
...@@ -213,23 +215,23 @@ function UTF8Length(input) { ...@@ -213,23 +215,23 @@ function UTF8Length(input) {
for (var i = 0; i < input.length; i++) { for (var i = 0; i < input.length; i++) {
var charCode = input.charCodeAt(i); var charCode = input.charCodeAt(i);
if (charCode > 0x7FF) { if (charCode > 0x7ff) {
// Surrogate pair means its a 4 byte character // Surrogate pair means its a 4 byte character
if (0xD800 <= charCode && charCode <= 0xDBFF) { if (0xd800 <= charCode && charCode <= 0xdbff) {
i++; i++;
output++; output++;
} }
output += 3; output += 3;
} else if (charCode > 0x7F) output += 2;else output++; } else if (charCode > 0x7f) output += 2;else output++;
} }
return output; return output;
} }
/** /**
* Takes a String and writes it into an array as UTF8 encoded bytes. * Takes a String and writes it into an array as UTF8 encoded bytes.
* @private * @private
*/ */
function stringToUTF8(input, output, start) { function stringToUTF8(input, output, start) {
...@@ -238,30 +240,30 @@ function stringToUTF8(input, output, start) { ...@@ -238,30 +240,30 @@ function stringToUTF8(input, output, start) {
for (var i = 0; i < input.length; i++) { for (var i = 0; i < input.length; i++) {
var charCode = input.charCodeAt(i); // Check for a surrogate pair. var charCode = input.charCodeAt(i); // Check for a surrogate pair.
if (0xD800 <= charCode && charCode <= 0xDBFF) { if (0xd800 <= charCode && charCode <= 0xdbff) {
var lowCharCode = input.charCodeAt(++i); var lowCharCode = input.charCodeAt(++i);
if (isNaN(lowCharCode)) { if (isNaN(lowCharCode)) {
throw new Error(format(_consts.ERROR.MALFORMED_UNICODE, [charCode, lowCharCode])); throw new Error(format(_consts.ERROR.MALFORMED_UNICODE, [charCode, lowCharCode]));
} }
charCode = (charCode - 0xD800 << 10) + (lowCharCode - 0xDC00) + 0x10000; charCode = (charCode - 0xd800 << 10) + (lowCharCode - 0xdc00) + 0x10000;
} }
if (charCode <= 0x7F) { if (charCode <= 0x7f) {
output[pos++] = charCode; output[pos++] = charCode;
} else if (charCode <= 0x7FF) { } else if (charCode <= 0x7ff) {
output[pos++] = charCode >> 6 & 0x1F | 0xC0; output[pos++] = charCode >> 6 & 0x1f | 0xc0;
output[pos++] = charCode & 0x3F | 0x80; output[pos++] = charCode & 0x3f | 0x80;
} else if (charCode <= 0xFFFF) { } else if (charCode <= 0xffff) {
output[pos++] = charCode >> 12 & 0x0F | 0xE0; output[pos++] = charCode >> 12 & 0x0f | 0xe0;
output[pos++] = charCode >> 6 & 0x3F | 0x80; output[pos++] = charCode >> 6 & 0x3f | 0x80;
output[pos++] = charCode & 0x3F | 0x80; output[pos++] = charCode & 0x3f | 0x80;
} else { } else {
output[pos++] = charCode >> 18 & 0x07 | 0xF0; output[pos++] = charCode >> 18 & 0x07 | 0xf0;
output[pos++] = charCode >> 12 & 0x3F | 0x80; output[pos++] = charCode >> 12 & 0x3f | 0x80;
output[pos++] = charCode >> 6 & 0x3F | 0x80; output[pos++] = charCode >> 6 & 0x3f | 0x80;
output[pos++] = charCode & 0x3F | 0x80; output[pos++] = charCode & 0x3f | 0x80;
} }
} }
...@@ -269,7 +271,7 @@ function stringToUTF8(input, output, start) { ...@@ -269,7 +271,7 @@ function stringToUTF8(input, output, start) {
} }
function parseUTF8(input, offset, length) { function parseUTF8(input, offset, length) {
var output = ""; var output = '';
var utf16; var utf16;
var pos = offset; var pos = offset;
...@@ -277,28 +279,28 @@ function parseUTF8(input, offset, length) { ...@@ -277,28 +279,28 @@ function parseUTF8(input, offset, length) {
var byte1 = input[pos++]; var byte1 = input[pos++];
if (byte1 < 128) utf16 = byte1;else { if (byte1 < 128) utf16 = byte1;else {
var byte2 = input[pos++] - 128; var byte2 = input[pos++] - 128;
if (byte2 < 0) throw new Error(format(_consts.ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), ""])); if (byte2 < 0) throw new Error(format(_consts.ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), '']));
if (byte1 < 0xE0) // 2 byte character if (byte1 < 0xe0) // 2 byte character
utf16 = 64 * (byte1 - 0xC0) + byte2;else { utf16 = 64 * (byte1 - 0xc0) + byte2;else {
var byte3 = input[pos++] - 128; var byte3 = input[pos++] - 128;
if (byte3 < 0) throw new Error(format(_consts.ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16)])); if (byte3 < 0) throw new Error(format(_consts.ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16)]));
if (byte1 < 0xF0) // 3 byte character if (byte1 < 0xf0) // 3 byte character
utf16 = 4096 * (byte1 - 0xE0) + 64 * byte2 + byte3;else { utf16 = 4096 * (byte1 - 0xe0) + 64 * byte2 + byte3;else {
var byte4 = input[pos++] - 128; var byte4 = input[pos++] - 128;
if (byte4 < 0) throw new Error(format(_consts.ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)])); if (byte4 < 0) throw new Error(format(_consts.ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
if (byte1 < 0xF8) // 4 byte character if (byte1 < 0xf8) // 4 byte character
utf16 = 262144 * (byte1 - 0xF0) + 4096 * byte2 + 64 * byte3 + byte4;else // longer encodings are not supported utf16 = 262144 * (byte1 - 0xf0) + 4096 * byte2 + 64 * byte3 + byte4; // longer encodings are not supported
throw new Error(format(_consts.ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)])); else throw new Error(format(_consts.ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
} }
} }
} }
if (utf16 > 0xFFFF) // 4 byte character - express as a surrogate pair if (utf16 > 0xffff) {
{ // 4 byte character - express as a surrogate pair
utf16 -= 0x10000; utf16 -= 0x10000;
output += String.fromCharCode(0xD800 + (utf16 >> 10)); // lead character output += String.fromCharCode(0xd800 + (utf16 >> 10)); // lead character
utf16 = 0xDC00 + (utf16 & 0x3FF); // trail character utf16 = 0xdc00 + (utf16 & 0x3ff); // trail character
} }
output += String.fromCharCode(utf16); output += String.fromCharCode(utf16);
......
import ClientImplementation from './ClientImplementation'; import ClientImplementation from './ClientImplementation';
import { ERROR } from './consts'; import { ERROR } from './consts';
import { validate } from './utils'; import { validate, format } from './utils';
/* eslint-disable */ /* eslint-disable */
const Client = function (host, port, path, clientId) { const Client = function (host, port, path, clientId) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment