File Explorer

/proc/2/root/var/runtime/node_modules/@aws-sdk/node_modules/aws-crt/dist.browser/browser

This explorer reads the filesystem of the server it runs on, so /workspace/user isn't present here. Browsing and the terminal still work against this server's own disk from /.

mqtt.js29.7 KB · 653 lines
"use strict";/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. */var __extends = (this && this.__extends) || (function () {    var extendStatics = function (d, b) {        extendStatics = Object.setPrototypeOf ||            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };        return extendStatics(d, b);    };    return function (d, b) {        if (typeof b !== "function" && b !== null)            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");        extendStatics(d, b);        function __() { this.constructor = d; }        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());    };})();var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {    if (k2 === undefined) k2 = k;    var desc = Object.getOwnPropertyDescriptor(m, k);    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {      desc = { enumerable: true, get: function() { return m[k]; } };    }    Object.defineProperty(o, k2, desc);}) : (function(o, m, k, k2) {    if (k2 === undefined) k2 = k;    o[k2] = m[k];}));var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {    Object.defineProperty(o, "default", { enumerable: true, value: v });}) : function(o, v) {    o["default"] = v;});var __importStar = (this && this.__importStar) || function (mod) {    if (mod && mod.__esModule) return mod;    var result = {};    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);    __setModuleDefault(result, mod);    return result;};var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }    return new (P || (P = Promise))(function (resolve, reject) {        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }        step((generator = generator.apply(thisArg, _arguments || [])).next());    });};var __generator = (this && this.__generator) || function (thisArg, body) {    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;    function verb(n) { return function (v) { return step([n, v]); }; }    function step(op) {        if (f) throw new TypeError("Generator is already executing.");        while (g && (g = 0, op[0] && (_ = 0)), _) try {            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;            if (y = 0, t) op = [op[0] & 2, t.value];            switch (op[0]) {                case 0: case 1: t = op; break;                case 4: _.label++; return { value: op[1], done: false };                case 5: _.label++; y = op[1]; op = [0]; continue;                case 7: op = _.ops.pop(); _.trys.pop(); continue;                default:                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }                    if (t[2]) _.ops.pop();                    _.trys.pop(); continue;            }            op = body.call(thisArg, _);        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };    }};var __values = (this && this.__values) || function(o) {    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;    if (m) return m.call(o);    if (o && typeof o.length === "number") return {        next: function () {            if (o && i >= o.length) o = void 0;            return { value: o && o[i++], done: !o };        }    };    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");};Object.defineProperty(exports, "__esModule", { value: true });exports.MqttClientConnection = exports.MqttClient = exports.MqttWill = exports.QoS = void 0;/** * * A module containing support for mqtt connection establishment and operations. * * @packageDocumentation * @module mqtt * @mergeTarget */var internal_mqtt_client = __importStar(require("./mqtt_internal/client"));var mqtt5 = __importStar(require("../common/mqtt5"));var ws = __importStar(require("./ws"));var auth = __importStar(require("./auth"));var promise = __importStar(require("../common/promise"));var model = __importStar(require("./mqtt_internal/model"));var mqtt_shared = __importStar(require("../common/mqtt_shared"));var mqtt_shared_browser = __importStar(require("./mqtt_shared_browser"));var aws_iot_metrics = __importStar(require("./aws_iot_metrics"));var trie_1 = require("./trie");var event_1 = require("../common/event");var error_1 = require("./error");var mqtt_1 = require("../common/mqtt");var events_1 = require("events");var mqtt_2 = require("../common/mqtt");Object.defineProperty(exports, "QoS", { enumerable: true, get: function () { return mqtt_2.QoS; } });Object.defineProperty(exports, "MqttWill", { enumerable: true, get: function () { return mqtt_2.MqttWill; } });/** * MQTT client * * @category MQTT */var MqttClient = /** @class */ (function () {    function MqttClient(bootstrap) {    }    /**     * Creates a new {@link MqttClientConnection}     * @param config Configuration for the connection     * @returns A new connection     */    MqttClient.prototype.new_connection = function (config) {        return new MqttClientConnection(this, config);    };    return MqttClient;}());exports.MqttClient = MqttClient;/** * @internal */var MqttBrowserClientState;(function (MqttBrowserClientState) {    MqttBrowserClientState[MqttBrowserClientState["Connected"] = 0] = "Connected";    MqttBrowserClientState[MqttBrowserClientState["Stopped"] = 1] = "Stopped";})(MqttBrowserClientState || (MqttBrowserClientState = {}));;/** @internal */var TopicTrie = /** @class */ (function (_super) {    __extends(TopicTrie, _super);    function TopicTrie() {        return _super.call(this, '/') || this;    }    TopicTrie.prototype.find_node = function (key, op) {        var e_1, _a;        var parts = this.split_key(key);        var current = this.root;        var parent = undefined;        try {            for (var parts_1 = __values(parts), parts_1_1 = parts_1.next(); !parts_1_1.done; parts_1_1 = parts_1.next()) {                var part = parts_1_1.value;                var child = current.children.get(part);                if (!child) {                    child = current.children.get('#');                    if (child) {                        return child;                    }                    child = current.children.get('+');                }                if (!child) {                    if (op == trie_1.TrieOp.Insert) {                        current.children.set(part, child = new trie_1.Node(part));                    }                    else {                        return undefined;                    }                }                parent = current;                current = child;            }        }        catch (e_1_1) { e_1 = { error: e_1_1 }; }        finally {            try {                if (parts_1_1 && !parts_1_1.done && (_a = parts_1.return)) _a.call(parts_1);            }            finally { if (e_1) throw e_1.error; }        }        if (parent && op == trie_1.TrieOp.Delete) {            parent.children.delete(current.key);        }        return current;    };    return TopicTrie;}(trie_1.Trie));/** * MQTT client connection * * @category MQTT */var MqttClientConnection = /** @class */ (function (_super) {    __extends(MqttClientConnection, _super);    /**     * @param client The client that owns this connection     * @param config The configuration for this connection     */    function MqttClientConnection(client, config) {        var _this = _super.call(this) || this;        _this.client = client;        _this.config = config;        _this.subscriptions = new TopicTrie();        _this.connection_count = 0;        _this.connectPromise = undefined;        _this.isSessionPresent = false;        _this.currentState = MqttBrowserClientState.Stopped;        _this.desiredState = MqttBrowserClientState.Stopped;        _this.on_connection_success = function (event) {            _this.on_online(event.connack.sessionPresent);        };        _this.on_online = function (session_present) {            _this.isSessionPresent = session_present;            _this.currentState = MqttBrowserClientState.Connected;            if (++_this.connection_count == 1) {                _this.emit('connect', session_present);            }            else {                /** Reset reconnect times after reconnect succeed. */                _this.emit('resume', 0, session_present);            }            // Call connection success every time we connect, whether it is a first connect or a reconnect            var successCallbackData = { session_present: session_present };            _this.emit('connection_success', successCallbackData);        };        _this.on_connection_failure = function (event) {            var failureCallbackData = { error: event.error };            _this.emit('connection_failure', failureCallbackData);        };        _this.on_disconnection = function (event) {            _this.currentState = MqttBrowserClientState.Stopped;            _this.emit('interrupt', -1);        };        _this.on_stopped = function (event) {            _this.emit("closed");            _this.emit("disconnect");        };        _this.on_publish_received = function (event) {            var _a;            var packet = event.publish;            var topic = packet.topicName;            var payload = packet.payload;            var retain = (_a = packet.retain) !== null && _a !== void 0 ? _a : false;            var array_buffer = payload ? model.payloadToArrayBuffer(payload) : new ArrayBuffer(0);            var callback = _this.subscriptions.find(topic);            if (callback) {                callback(topic, array_buffer, packet.duplicate, packet.qos, retain);            }            _this.emit('message', topic, array_buffer, packet.duplicate, packet.qos, retain);        };        if (!config) {            throw new error_1.CrtError("MqttClientConnection constructor: config not defined");        }        var internalConnectOptions = {            keepAliveIntervalSeconds: _this.config.keep_alive ? _this.config.keep_alive : 1200,            resumeSessionPolicy: _this.config.clean_session ? internal_mqtt_client.ResumeSessionPolicyType.Never : internal_mqtt_client.ResumeSessionPolicyType.Always,        };        if (_this.config.client_id) {            internalConnectOptions.clientId = _this.config.client_id;        }        if (_this.config.disable_metrics) {            if (_this.config.username) {                internalConnectOptions.username = _this.config.username;            }        }        else {            internalConnectOptions.username = mqtt_shared_browser.buildFinalUsernameFromMetrics(aws_iot_metrics.create_metrics_mqtt3(_this.config), _this.config.username);        }        if (_this.config.password) {            internalConnectOptions.password = mqtt_shared_browser.normalize_payload_to_buffer(_this.config.password);        }        if (_this.config.will) {            internalConnectOptions.will = {                topicName: _this.config.will.topic,                payload: mqtt_shared_browser.normalize_payload_to_buffer(_this.config.will.payload),                qos: _this.config.will.qos,                retain: _this.config.will.retain,            };        }        // If the credentials are set but no the credentials_provider        if (_this.config.credentials_provider == undefined &&            _this.config.credentials != undefined) {            var provider = new auth.StaticCredentialProvider({ aws_region: _this.config.credentials.aws_region,                aws_access_id: _this.config.credentials.aws_access_id,                aws_secret_key: _this.config.credentials.aws_secret_key,                aws_sts_token: _this.config.credentials.aws_sts_token });            _this.config.credentials_provider = provider;        }        var thisConfig = _this.config;        var connectionFactory = function () {            var _this = this;            return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {                var conn;                return __generator(this, function (_a) {                    switch (_a.label) {                        case 0:                            if (!thisConfig.credentials_provider) return [3 /*break*/, 2];                            return [4 /*yield*/, thisConfig.credentials_provider.refreshCredentials()];                        case 1:                            _a.sent();                            _a.label = 2;                        case 2:                            conn = ws.create_websocket_stream(thisConfig);                            conn.on('error', function (err) {                                reject(err);                            });                            conn.on('connect', function () {                                resolve(conn);                            });                            return [2 /*return*/];                    }                });            }); });        };        var internalConfig = {            protocolVersion: mqtt_shared.ProtocolMode.Mqtt311,            offlineQueuePolicy: internal_mqtt_client.OfflineQueuePolicy.PreserveQos1PlusPublishes,            connectOptions: internalConnectOptions,            pingTimeoutMillis: _this.config.ping_timeout ? _this.config.ping_timeout : 30 * 1000,            connectionFactory: connectionFactory,            // yes this is wrong, but this is how the old client was configured            // ideally, we'd use the socket options timeout value here            connectTimeoutMillis: _this.config.ping_timeout ? _this.config.ping_timeout : 30 * 1000,            retryJitterMode: mqtt5.RetryJitterType.Default,            // this was how the previous mqtt-js integration behaved            resubscribeMode: internal_mqtt_client.ResubscribeModeType.EnabledAlways        };        if (config.reconnect_min_sec !== undefined) {            internalConfig.minReconnectDelayMs = config.reconnect_min_sec * 1000;        }        if (config.reconnect_max_sec !== undefined) {            internalConfig.maxReconnectDelayMs = config.reconnect_max_sec * 1000;        }        // we never implemented a reset delay in the browser client, so reset immediately after connecting        internalConfig.resetConnectionFailureCountMillis = 0;        _this.internalClient = new internal_mqtt_client.Client(internalConfig);        _this.internalClient.on("connectionSuccess", _this.on_connection_success);        _this.internalClient.on("connectionFailure", _this.on_connection_failure);        _this.internalClient.on("disconnection", _this.on_disconnection);        _this.internalClient.on("publishReceived", _this.on_publish_received);        _this.internalClient.on("stopped", _this.on_stopped);        return _this;    }    MqttClientConnection.prototype.on = function (event, listener) {        return _super.prototype.on.call(this, event, listener);    };    /**     * Open the actual connection to the server (async).     * @returns A Promise which completes whether the connection succeeds or fails.     *          If connection fails, the Promise will reject with an exception.     *          If connection succeeds, the Promise will return a boolean that is     *          true for resuming an existing session, or false if the session is new     */    MqttClientConnection.prototype.connect = function () {        return __awaiter(this, void 0, void 0, function () {            var client, internalClient, isStarted, listeners_1;            var _this = this;            return __generator(this, function (_a) {                client = this;                internalClient = this.internalClient;                isStarted = client.desiredState == MqttBrowserClientState.Connected;                this.desiredState = MqttBrowserClientState.Connected;                setTimeout(function () { _this.uncork(); }, 0);                if (this.currentState == MqttBrowserClientState.Connected && isStarted) {                    return [2 /*return*/, new Promise(function (resolve, reject) {                            resolve(client.isSessionPresent);                        })];                }                if (!this.connectPromise) {                    this.connectPromise = promise.newLiftedPromise();                    listeners_1 = {};                    listeners_1.connectionSuccess = function (event) {                        if (client.connectPromise) {                            client.connectPromise.resolve(event.connack.sessionPresent);                            client.connectPromise = undefined;                        }                        if (listeners_1.connectionFailure) {                            internalClient.removeListener("connectionFailure", listeners_1.connectionFailure);                        }                    };                    listeners_1.connectionFailure = function (event) {                        if (client.connectPromise) {                            client.connectPromise.reject(event.error);                            client.connectPromise = undefined;                        }                        if (listeners_1.connectionSuccess) {                            internalClient.removeListener("connectionSuccess", listeners_1.connectionSuccess);                        }                    };                    internalClient.once("connectionSuccess", listeners_1.connectionSuccess);                    internalClient.once("connectionFailure", listeners_1.connectionFailure);                }                if (!isStarted) {                    internalClient.start();                }                return [2 /*return*/, this.connectPromise.promise];            });        });    };    /**     * The connection will automatically reconnect. To cease reconnection attempts, call {@link disconnect}.     * To resume the connection, call {@link connect}.     * @deprecated     */    MqttClientConnection.prototype.reconnect = function () {        return __awaiter(this, void 0, void 0, function () {            return __generator(this, function (_a) {                return [2 /*return*/, this.connect()];            });        });    };    /**     * Publish message (async).     * If the device is offline, the PUBLISH packet will be sent once the connection resumes.     *     * @param topic Topic name     * @param payload Contents of message     * @param qos Quality of Service for delivering this message     * @param retain If true, the server will store the message and its QoS so that it can be     *               delivered to future subscribers whose subscriptions match the topic name     * @returns Promise which returns a {@link MqttRequest} which will contain the packet id of     *          the PUBLISH packet.     *     * * For QoS 0, completes as soon as the packet is sent.     * * For QoS 1, completes when PUBACK is received.     * * For QoS 2, completes when PUBCOMP is received.     */    MqttClientConnection.prototype.publish = function (topic, payload, qos, retain) {        if (retain === void 0) { retain = false; }        return __awaiter(this, void 0, void 0, function () {            var payload_data, options, publishResult, id;            return __generator(this, function (_a) {                switch (_a.label) {                    case 0:                        payload_data = mqtt_shared.normalize_payload(payload);                        options = {};                        if (this.config.protocol_operation_timeout) {                            options.timeoutInMillis = this.config.protocol_operation_timeout;                        }                        return [4 /*yield*/, this.internalClient.publish({                                topicName: topic,                                qos: qos,                                payload: payload_data,                                retain: retain                            }, options)];                    case 1:                        publishResult = _a.sent();                        id = undefined;                        if (qos == mqtt_1.QoS.AtLeastOnce) {                            id = publishResult.packet.packetId;                        }                        return [2 /*return*/, { packet_id: id }];                }            });        });    };    /**     * Subscribe to a topic filter (async).     * The client sends a SUBSCRIBE packet and the server responds with a SUBACK.     *     * subscribe() may be called while the device is offline, though the async     * operation cannot complete successfully until the connection resumes.     *     * Once subscribed, `callback` is invoked each time a message matching     * the `topic` is received. It is possible for such messages to arrive before     * the SUBACK is received.     *     * @param topic Subscribe to this topic filter, which may include wildcards     * @param qos Maximum requested QoS that server may use when sending messages to the client.     *            The server may grant a lower QoS in the SUBACK     * @param on_message Optional callback invoked when message received.     * @returns Promise which returns a {@link MqttSubscribeRequest} which will contain the     *          result of the SUBSCRIBE. The Promise resolves when a SUBACK is returned     *          from the server or is rejected when an exception occurs.     */    MqttClientConnection.prototype.subscribe = function (topic, qos, on_message) {        return __awaiter(this, void 0, void 0, function () {            var options, suback;            return __generator(this, function (_a) {                switch (_a.label) {                    case 0:                        if (typeof (topic) !== 'string') {                            return [2 /*return*/, Promise.reject("topic is not a string")];                        }                        if (typeof (qos) !== 'number') {                            return [2 /*return*/, Promise.reject("qos is not a number")];                        }                        this.subscriptions.insert(topic, on_message);                        options = {};                        if (this.config.protocol_operation_timeout) {                            options.timeoutInMillis = this.config.protocol_operation_timeout;                        }                        return [4 /*yield*/, this.internalClient.subscribe({                                subscriptions: [                                    {                                        topicFilter: topic,                                        qos: qos                                    }                                ]                            }, options)];                    case 1:                        suback = _a.sent();                        return [2 /*return*/, {                                topic: topic,                                /*                                 * 128 is not modeled in QoS, either on our side nor mqtt-js's side.                                 * We have always passed this 128 to the user and it is not reasonable to extend                                 * our output type with 128 since it's also our input type and we don't want anyone                                 * to pass 128 to us.                                 *                                 * The 5 client solves this by making the output type a completely separate enum.                                 *                                 * By doing this cast, we make the type checker ignore this edge case.                                 */                                qos: suback.reasonCodes[0]                            }];                }            });        });    };    /**     * Unsubscribe from a topic filter (async).     * The client sends an UNSUBSCRIBE packet, and the server responds with an UNSUBACK.     * @param topic The topic filter to unsubscribe from. May contain wildcards.     * @returns Promise which returns a {@link MqttRequest} which will contain the packet id     *          of the UNSUBSCRIBE packet being acknowledged. Promise is resolved when an     *          UNSUBACK is received from the server or is rejected when an exception occurs.     */    MqttClientConnection.prototype.unsubscribe = function (topic) {        return __awaiter(this, void 0, void 0, function () {            var options, unsuback;            return __generator(this, function (_a) {                switch (_a.label) {                    case 0:                        if (typeof (topic) !== 'string') {                            return [2 /*return*/, Promise.reject("topic is not a string")];                        }                        this.subscriptions.remove(topic);                        options = {};                        if (this.config.protocol_operation_timeout) {                            options.timeoutInMillis = this.config.protocol_operation_timeout;                        }                        return [4 /*yield*/, this.internalClient.unsubscribe({                                topicFilters: [                                    topic                                ]                            }, options)];                    case 1:                        unsuback = _a.sent();                        return [2 /*return*/, {                                packet_id: unsuback.packetId                            }];                }            });        });    };    /**     * Close the connection (async).     * @returns Promise which completes when the connection is closed.     */    MqttClientConnection.prototype.disconnect = function () {        return __awaiter(this, void 0, void 0, function () {            var isStarted, stopped;            return __generator(this, function (_a) {                isStarted = this.desiredState == MqttBrowserClientState.Connected;                this.desiredState = MqttBrowserClientState.Stopped;                if (!isStarted && this.currentState == MqttBrowserClientState.Stopped) {                    return [2 /*return*/, Promise.resolve()];                }                stopped = (0, events_1.once)(this.internalClient, "stopped");                if (isStarted) {                    this.internalClient.stop();                }                return [2 /*return*/, stopped];            });        });    };    /**     * Queries whether the client is currently connected     *     * @returns whether the client is currently connected     */    MqttClientConnection.prototype.is_connected = function () {        return this.currentState == MqttBrowserClientState.Connected;    };    /**     * Emitted when the connection successfully establishes itself for the first time     *     * @event     */    MqttClientConnection.CONNECT = 'connect';    /**     * Emitted when connection has disconnected successfully.     *     * @event     */    MqttClientConnection.DISCONNECT = 'disconnect';    /**     * Emitted when an error occurs.  The error will contain the error     * code and message.     *     * @event     */    MqttClientConnection.ERROR = 'error';    /**     * Emitted when the connection is dropped unexpectedly. The error will contain the error     * code and message.  The underlying mqtt implementation will attempt to reconnect.     *     * @event     */    MqttClientConnection.INTERRUPT = 'interrupt';    /**     * Emitted when the connection reconnects (after an interrupt). Only triggers on connections after the initial one.     *     * @event     */    MqttClientConnection.RESUME = 'resume';    /**     * Emitted when any MQTT publish message arrives.     *     * @event     */    MqttClientConnection.MESSAGE = 'message';    /**     * Emitted on every successful connect and reconnect.     * Will contain a boolean indicating whether the connection resumed a session.     *     * @event     */    MqttClientConnection.CONNECTION_SUCCESS = 'connection_success';    /**     * Emitted on an unsuccessful connect and reconnect.     * Will contain an error code indicating the reason for the unsuccessful connection.     *     * @event     */    MqttClientConnection.CONNECTION_FAILURE = 'connection_failure';    /**     * Emitted when the MQTT connection was disconnected and shutdown successfully.     *     * @event     */    MqttClientConnection.CLOSED = 'closed';    return MqttClientConnection;}(event_1.BufferedEventEmitter));exports.MqttClientConnection = MqttClientConnection;//# sourceMappingURL=mqtt.js.map