@aws-amplify/pubsub 5.5.6-api-v6-models.b3abc9b.0 → 6.0.0
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/iot/packages.json +7 -0
- package/lib/Providers/AWSIot.d.ts +10 -0
- package/lib/Providers/AWSIot.js +34 -0
- package/lib/Providers/{MqttOverWSProvider.d.ts → MqttOverWS.d.ts} +16 -16
- package/lib/Providers/MqttOverWS.js +274 -0
- package/lib/Providers/PubSub.d.ts +11 -0
- package/lib/Providers/PubSub.js +19 -0
- package/lib/Providers/constants.d.ts +1 -1
- package/lib/Providers/constants.js +3 -4
- package/lib/Providers/index.d.ts +3 -4
- package/lib/Providers/index.js +8 -10
- package/lib/clients/iot.d.ts +1 -0
- package/lib/clients/iot.js +7 -0
- package/lib/clients/mqtt.d.ts +1 -0
- package/lib/clients/mqtt.js +7 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +7 -11
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/PubSub.d.ts +21 -3
- package/lib/types/PubSub.js +1 -1
- package/lib/types/index.d.ts +0 -1
- package/lib/types/index.js +5 -3
- package/lib/utils/ConnectionStateMonitor.d.ts +5 -5
- package/lib/utils/ConnectionStateMonitor.js +52 -64
- package/lib/utils/ReachabilityMonitor/index.d.ts +1 -3
- package/lib/utils/ReachabilityMonitor/index.js +4 -3
- package/lib/utils/ReachabilityMonitor/index.native.d.ts +1 -3
- package/lib/utils/ReachabilityMonitor/index.native.js +7 -8
- package/lib/utils/ReconnectionMonitor.d.ts +1 -1
- package/lib/utils/ReconnectionMonitor.js +21 -25
- package/lib-esm/Providers/AWSIot.d.ts +10 -0
- package/lib-esm/Providers/AWSIot.js +30 -0
- package/lib-esm/Providers/{MqttOverWSProvider.d.ts → MqttOverWS.d.ts} +16 -16
- package/lib-esm/Providers/MqttOverWS.js +268 -0
- package/lib-esm/Providers/PubSub.d.ts +11 -0
- package/lib-esm/Providers/PubSub.js +15 -0
- package/lib-esm/Providers/constants.d.ts +1 -1
- package/lib-esm/Providers/constants.js +11 -14
- package/lib-esm/Providers/index.d.ts +3 -4
- package/lib-esm/Providers/index.js +3 -5
- package/lib-esm/clients/iot.d.ts +1 -0
- package/{src/internals/index.ts → lib-esm/clients/iot.js} +1 -1
- package/lib-esm/clients/mqtt.d.ts +1 -0
- package/lib-esm/{internals/index.js → clients/mqtt.js} +1 -2
- package/lib-esm/index.d.ts +2 -2
- package/lib-esm/index.js +2 -3
- package/lib-esm/tsconfig.tsbuildinfo +1 -0
- package/lib-esm/types/PubSub.d.ts +21 -3
- package/lib-esm/types/PubSub.js +0 -1
- package/lib-esm/types/index.d.ts +0 -1
- package/lib-esm/types/index.js +2 -1
- package/lib-esm/utils/ConnectionStateMonitor.d.ts +5 -5
- package/lib-esm/utils/ConnectionStateMonitor.js +50 -64
- package/lib-esm/utils/ReachabilityMonitor/index.d.ts +1 -3
- package/lib-esm/utils/ReachabilityMonitor/index.js +2 -3
- package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +1 -3
- package/lib-esm/utils/ReachabilityMonitor/index.native.js +2 -5
- package/lib-esm/utils/ReconnectionMonitor.d.ts +1 -1
- package/lib-esm/utils/ReconnectionMonitor.js +19 -25
- package/mqtt/packages.json +7 -0
- package/package.json +146 -126
- package/src/Providers/{AWSIotProvider.ts → AWSIot.ts} +17 -15
- package/src/Providers/{MqttOverWSProvider.ts → MqttOverWS.ts} +38 -50
- package/src/Providers/PubSub.ts +40 -0
- package/src/Providers/constants.ts +2 -6
- package/src/Providers/index.ts +3 -12
- package/src/clients/iot.ts +4 -0
- package/src/clients/mqtt.ts +7 -0
- package/src/index.ts +3 -12
- package/src/types/PubSub.ts +28 -5
- package/src/types/index.ts +0 -1
- package/src/utils/ConnectionStateMonitor.ts +17 -13
- package/src/utils/ReachabilityMonitor/index.native.ts +2 -1
- package/src/utils/ReachabilityMonitor/index.ts +1 -1
- package/src/utils/ReconnectionMonitor.ts +1 -1
- package/internals/package.json +0 -8
- package/lib/.tsbuildinfo +0 -3
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +0 -77
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +0 -856
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js.map +0 -1
- package/lib/Providers/AWSIotProvider.d.ts +0 -11
- package/lib/Providers/AWSIotProvider.js +0 -53
- package/lib/Providers/AWSIotProvider.js.map +0 -1
- package/lib/Providers/MqttOverWSProvider.js +0 -396
- package/lib/Providers/MqttOverWSProvider.js.map +0 -1
- package/lib/Providers/PubSubProvider.d.ts +0 -15
- package/lib/Providers/PubSubProvider.js +0 -28
- package/lib/Providers/PubSubProvider.js.map +0 -1
- package/lib/Providers/constants.js.map +0 -1
- package/lib/Providers/index.js.map +0 -1
- package/lib/PubSub.d.ts +0 -13
- package/lib/PubSub.js +0 -22
- package/lib/PubSub.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/internals/InternalPubSub.d.ts +0 -50
- package/lib/internals/InternalPubSub.js +0 -136
- package/lib/internals/InternalPubSub.js.map +0 -1
- package/lib/internals/index.d.ts +0 -1
- package/lib/internals/index.js +0 -8
- package/lib/internals/index.js.map +0 -1
- package/lib/types/Provider.d.ts +0 -18
- package/lib/types/Provider.js +0 -3
- package/lib/types/Provider.js.map +0 -1
- package/lib/types/PubSub.js.map +0 -1
- package/lib/types/index.js.map +0 -1
- package/lib/utils/ConnectionStateMonitor.js.map +0 -1
- package/lib/utils/ReachabilityMonitor/index.js.map +0 -1
- package/lib/utils/ReachabilityMonitor/index.native.js.map +0 -1
- package/lib/utils/ReconnectionMonitor.js.map +0 -1
- package/lib/vendor/paho-mqtt.d.ts +0 -2
- package/lib/vendor/paho-mqtt.js.map +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +0 -77
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +0 -854
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js.map +0 -1
- package/lib-esm/Providers/AWSIotProvider.d.ts +0 -11
- package/lib-esm/Providers/AWSIotProvider.js +0 -51
- package/lib-esm/Providers/AWSIotProvider.js.map +0 -1
- package/lib-esm/Providers/MqttOverWSProvider.js +0 -393
- package/lib-esm/Providers/MqttOverWSProvider.js.map +0 -1
- package/lib-esm/Providers/PubSubProvider.d.ts +0 -15
- package/lib-esm/Providers/PubSubProvider.js +0 -26
- package/lib-esm/Providers/PubSubProvider.js.map +0 -1
- package/lib-esm/Providers/constants.js.map +0 -1
- package/lib-esm/Providers/index.js.map +0 -1
- package/lib-esm/PubSub.d.ts +0 -13
- package/lib-esm/PubSub.js +0 -20
- package/lib-esm/PubSub.js.map +0 -1
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/internals/InternalPubSub.d.ts +0 -50
- package/lib-esm/internals/InternalPubSub.js +0 -134
- package/lib-esm/internals/InternalPubSub.js.map +0 -1
- package/lib-esm/internals/index.d.ts +0 -1
- package/lib-esm/internals/index.js.map +0 -1
- package/lib-esm/types/Provider.d.ts +0 -18
- package/lib-esm/types/Provider.js +0 -1
- package/lib-esm/types/Provider.js.map +0 -1
- package/lib-esm/types/PubSub.js.map +0 -1
- package/lib-esm/types/index.js.map +0 -1
- package/lib-esm/utils/ConnectionStateMonitor.js.map +0 -1
- package/lib-esm/utils/ReachabilityMonitor/index.js.map +0 -1
- package/lib-esm/utils/ReachabilityMonitor/index.native.js.map +0 -1
- package/lib-esm/utils/ReconnectionMonitor.js.map +0 -1
- package/lib-esm/vendor/paho-mqtt.d.ts +0 -2
- package/lib-esm/vendor/paho-mqtt.js.map +0 -1
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +0 -1049
- package/src/Providers/PubSubProvider.ts +0 -53
- package/src/PubSub.ts +0 -33
- package/src/internals/InternalPubSub.ts +0 -185
- package/src/types/Provider.ts +0 -37
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MqttOverWS, MqttOptions } from './MqttOverWS';
|
|
2
|
+
export interface AWSIoTOptions extends MqttOptions {
|
|
3
|
+
region?: string;
|
|
4
|
+
endpoint?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class AWSIoT extends MqttOverWS {
|
|
7
|
+
constructor(options?: AWSIoTOptions);
|
|
8
|
+
protected get region(): string | undefined;
|
|
9
|
+
protected get endpoint(): Promise<string>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AWSIoT = void 0;
|
|
4
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
const MqttOverWS_1 = require("./MqttOverWS");
|
|
7
|
+
const utils_1 = require("@aws-amplify/core/internals/utils");
|
|
8
|
+
const core_1 = require("@aws-amplify/core");
|
|
9
|
+
const SERVICE_NAME = 'iotdevicegateway';
|
|
10
|
+
class AWSIoT extends MqttOverWS_1.MqttOverWS {
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
super(options);
|
|
13
|
+
}
|
|
14
|
+
get region() {
|
|
15
|
+
return this.options?.region;
|
|
16
|
+
}
|
|
17
|
+
get endpoint() {
|
|
18
|
+
return (async () => {
|
|
19
|
+
const endpoint = this.options.endpoint;
|
|
20
|
+
const serviceInfo = {
|
|
21
|
+
service: SERVICE_NAME,
|
|
22
|
+
region: this.region,
|
|
23
|
+
};
|
|
24
|
+
const session = await (0, core_1.fetchAuthSession)();
|
|
25
|
+
if (!session.credentials) {
|
|
26
|
+
throw new Error('No auth session credentials');
|
|
27
|
+
}
|
|
28
|
+
const { accessKeyId: access_key, secretAccessKey: secret_key, sessionToken: session_token, } = session.credentials;
|
|
29
|
+
const result = utils_1.Signer.signUrl(endpoint, { access_key, secret_key, session_token }, serviceInfo);
|
|
30
|
+
return result;
|
|
31
|
+
})();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.AWSIoT = AWSIoT;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import Observable from '
|
|
2
|
-
import {
|
|
3
|
-
import { PubSubContentObserver, PubSubContent } from '../types/PubSub';
|
|
4
|
-
import { ProviderOptions } from '../types/Provider';
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { AbstractPubSub } from './PubSub';
|
|
3
|
+
import { PubSubContentObserver, PubSubContent, PubSubOptions, PublishInput, SubscribeInput } from '../types/PubSub';
|
|
5
4
|
export declare function mqttTopicMatch(filter: string, topic: string): boolean;
|
|
6
|
-
export interface
|
|
5
|
+
export interface MqttOptions extends PubSubOptions {
|
|
7
6
|
clientId?: string;
|
|
8
7
|
url?: string;
|
|
9
|
-
|
|
8
|
+
endpoint?: string;
|
|
10
9
|
}
|
|
11
10
|
interface PahoClient {
|
|
12
11
|
onMessageArrived: (params: {
|
|
@@ -23,36 +22,37 @@ interface PahoClient {
|
|
|
23
22
|
isConnected: () => boolean;
|
|
24
23
|
subscribe: (topic: string) => void;
|
|
25
24
|
unsubscribe: (topic: string) => void;
|
|
26
|
-
send(topic: string, message: string):
|
|
25
|
+
send(topic: string, message: string): void;
|
|
27
26
|
}
|
|
28
27
|
declare class ClientsQueue {
|
|
29
28
|
private promises;
|
|
30
|
-
get(clientId: string, clientFactory?: (input: string) => Promise<PahoClient | undefined>): Promise<PahoClient>;
|
|
29
|
+
get(clientId: string, clientFactory?: (input: string) => Promise<PahoClient | undefined>): Promise<PahoClient | undefined>;
|
|
31
30
|
get allClients(): string[];
|
|
32
31
|
remove(clientId: string): void;
|
|
33
32
|
}
|
|
34
|
-
export declare class
|
|
33
|
+
export declare class MqttOverWS extends AbstractPubSub<MqttOptions> {
|
|
35
34
|
private _clientsQueue;
|
|
36
|
-
private connectionState
|
|
35
|
+
private connectionState?;
|
|
37
36
|
private readonly connectionStateMonitor;
|
|
38
37
|
private readonly reconnectionMonitor;
|
|
39
|
-
constructor(options?:
|
|
38
|
+
constructor(options?: MqttOptions);
|
|
40
39
|
protected get clientId(): string;
|
|
41
40
|
protected get endpoint(): Promise<string | undefined>;
|
|
42
41
|
protected get clientsQueue(): ClientsQueue;
|
|
43
42
|
protected get isSSLEnabled(): boolean;
|
|
44
|
-
getProviderName(): string;
|
|
45
43
|
onDisconnect({ clientId, errorCode, ...args }: {
|
|
46
44
|
clientId?: string;
|
|
47
45
|
errorCode?: number;
|
|
48
46
|
}): void;
|
|
49
|
-
newClient({ url, clientId
|
|
50
|
-
protected connect(clientId: string, options?:
|
|
47
|
+
newClient({ url, clientId }: MqttOptions): Promise<PahoClient>;
|
|
48
|
+
protected connect(clientId: string, options?: MqttOptions): Promise<PahoClient | undefined>;
|
|
51
49
|
protected disconnect(clientId: string): Promise<void>;
|
|
52
|
-
publish(topics
|
|
50
|
+
publish({ topics, message }: PublishInput): Promise<void>;
|
|
53
51
|
protected _topicObservers: Map<string, Set<PubSubContentObserver>>;
|
|
54
52
|
protected _clientIdObservers: Map<string, Set<PubSubContentObserver>>;
|
|
55
53
|
private _onMessage;
|
|
56
|
-
subscribe(topics
|
|
54
|
+
subscribe({ topics, options, }: SubscribeInput & {
|
|
55
|
+
options?: MqttOptions;
|
|
56
|
+
}): Observable<PubSubContent>;
|
|
57
57
|
}
|
|
58
58
|
export {};
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.MqttOverWS = exports.mqttTopicMatch = void 0;
|
|
6
|
+
const tslib_1 = require("tslib");
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const Paho = tslib_1.__importStar(require("../vendor/paho-mqtt"));
|
|
9
|
+
const rxjs_1 = require("rxjs");
|
|
10
|
+
const PubSub_1 = require("./PubSub");
|
|
11
|
+
const PubSub_2 = require("../types/PubSub");
|
|
12
|
+
const core_1 = require("@aws-amplify/core");
|
|
13
|
+
const utils_1 = require("@aws-amplify/core/internals/utils");
|
|
14
|
+
const ConnectionStateMonitor_1 = require("../utils/ConnectionStateMonitor");
|
|
15
|
+
const ReconnectionMonitor_1 = require("../utils/ReconnectionMonitor");
|
|
16
|
+
const constants_1 = require("./constants");
|
|
17
|
+
const logger = new core_1.ConsoleLogger('MqttOverWS');
|
|
18
|
+
function mqttTopicMatch(filter, topic) {
|
|
19
|
+
const filterArray = filter.split('/');
|
|
20
|
+
const length = filterArray.length;
|
|
21
|
+
const topicArray = topic.split('/');
|
|
22
|
+
for (let i = 0; i < length; ++i) {
|
|
23
|
+
const left = filterArray[i];
|
|
24
|
+
const right = topicArray[i];
|
|
25
|
+
if (left === '#')
|
|
26
|
+
return topicArray.length >= length;
|
|
27
|
+
if (left !== '+' && left !== right)
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return length === topicArray.length;
|
|
31
|
+
}
|
|
32
|
+
exports.mqttTopicMatch = mqttTopicMatch;
|
|
33
|
+
class ClientsQueue {
|
|
34
|
+
constructor() {
|
|
35
|
+
this.promises = new Map();
|
|
36
|
+
}
|
|
37
|
+
async get(clientId, clientFactory) {
|
|
38
|
+
const cachedPromise = this.promises.get(clientId);
|
|
39
|
+
if (cachedPromise)
|
|
40
|
+
return cachedPromise;
|
|
41
|
+
if (clientFactory) {
|
|
42
|
+
const newPromise = clientFactory(clientId);
|
|
43
|
+
this.promises.set(clientId, newPromise);
|
|
44
|
+
newPromise.catch(() => this.promises.delete(clientId));
|
|
45
|
+
return newPromise;
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
get allClients() {
|
|
50
|
+
return Array.from(this.promises.keys());
|
|
51
|
+
}
|
|
52
|
+
remove(clientId) {
|
|
53
|
+
this.promises.delete(clientId);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const dispatchPubSubEvent = (payload) => {
|
|
57
|
+
core_1.Hub.dispatch('pubsub', payload, 'PubSub', constants_1.AMPLIFY_SYMBOL);
|
|
58
|
+
};
|
|
59
|
+
const topicSymbol = typeof Symbol !== 'undefined' ? Symbol('topic') : '@@topic';
|
|
60
|
+
class MqttOverWS extends PubSub_1.AbstractPubSub {
|
|
61
|
+
constructor(options = {}) {
|
|
62
|
+
super({ ...options, clientId: options.clientId || (0, utils_1.amplifyUuid)() });
|
|
63
|
+
this._clientsQueue = new ClientsQueue();
|
|
64
|
+
this.connectionStateMonitor = new ConnectionStateMonitor_1.ConnectionStateMonitor();
|
|
65
|
+
this.reconnectionMonitor = new ReconnectionMonitor_1.ReconnectionMonitor();
|
|
66
|
+
this._topicObservers = new Map();
|
|
67
|
+
this._clientIdObservers = new Map();
|
|
68
|
+
// Monitor the connection health state and pass changes along to Hub
|
|
69
|
+
this.connectionStateMonitor.connectionStateObservable.subscribe(connectionStateChange => {
|
|
70
|
+
dispatchPubSubEvent({
|
|
71
|
+
event: constants_1.CONNECTION_STATE_CHANGE,
|
|
72
|
+
data: {
|
|
73
|
+
provider: this,
|
|
74
|
+
connectionState: connectionStateChange,
|
|
75
|
+
},
|
|
76
|
+
message: `Connection state is ${connectionStateChange}`,
|
|
77
|
+
});
|
|
78
|
+
this.connectionState = connectionStateChange;
|
|
79
|
+
// Trigger reconnection when the connection is disrupted
|
|
80
|
+
if (connectionStateChange === PubSub_2.ConnectionState.ConnectionDisrupted) {
|
|
81
|
+
this.reconnectionMonitor.record(ReconnectionMonitor_1.ReconnectEvent.START_RECONNECT);
|
|
82
|
+
}
|
|
83
|
+
else if (connectionStateChange !== PubSub_2.ConnectionState.Connecting) {
|
|
84
|
+
// Trigger connected to halt reconnection attempts
|
|
85
|
+
this.reconnectionMonitor.record(ReconnectionMonitor_1.ReconnectEvent.HALT_RECONNECT);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
get clientId() {
|
|
90
|
+
return this.options.clientId;
|
|
91
|
+
}
|
|
92
|
+
get endpoint() {
|
|
93
|
+
return Promise.resolve(this.options.endpoint);
|
|
94
|
+
}
|
|
95
|
+
get clientsQueue() {
|
|
96
|
+
return this._clientsQueue;
|
|
97
|
+
}
|
|
98
|
+
get isSSLEnabled() {
|
|
99
|
+
return !this.options['aws_appsync_dangerously_connect_to_http_endpoint_for_testing'];
|
|
100
|
+
}
|
|
101
|
+
onDisconnect({ clientId, errorCode, ...args }) {
|
|
102
|
+
if (errorCode !== 0) {
|
|
103
|
+
logger.warn(clientId, JSON.stringify({ errorCode, ...args }, null, 2));
|
|
104
|
+
if (!clientId) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const clientIdObservers = this._clientIdObservers.get(clientId);
|
|
108
|
+
if (!clientIdObservers) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
this.disconnect(clientId);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async newClient({ url, clientId }) {
|
|
115
|
+
logger.debug('Creating new MQTT client', clientId);
|
|
116
|
+
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.OPENING_CONNECTION);
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
const client = new Paho.Client(url, clientId);
|
|
119
|
+
client.onMessageArrived = ({ destinationName: topic, payloadString: msg, }) => {
|
|
120
|
+
this._onMessage(topic, msg);
|
|
121
|
+
};
|
|
122
|
+
client.onConnectionLost = ({ errorCode, ...args }) => {
|
|
123
|
+
this.onDisconnect({ clientId, errorCode, ...args });
|
|
124
|
+
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
|
|
125
|
+
};
|
|
126
|
+
const connected = await new Promise((resolve, reject) => {
|
|
127
|
+
client.connect({
|
|
128
|
+
useSSL: this.isSSLEnabled,
|
|
129
|
+
mqttVersion: 3,
|
|
130
|
+
onSuccess: () => resolve(true),
|
|
131
|
+
onFailure: () => {
|
|
132
|
+
if (clientId)
|
|
133
|
+
this._clientsQueue.remove(clientId);
|
|
134
|
+
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
|
|
135
|
+
resolve(false);
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
if (connected) {
|
|
140
|
+
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CONNECTION_ESTABLISHED);
|
|
141
|
+
}
|
|
142
|
+
return client;
|
|
143
|
+
}
|
|
144
|
+
async connect(clientId, options = {}) {
|
|
145
|
+
return await this.clientsQueue.get(clientId, async (clientId) => {
|
|
146
|
+
const client = await this.newClient({ ...options, clientId });
|
|
147
|
+
if (client) {
|
|
148
|
+
// Once connected, subscribe to all topics registered observers
|
|
149
|
+
this._topicObservers.forEach((_value, key) => {
|
|
150
|
+
client.subscribe(key);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return client;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
async disconnect(clientId) {
|
|
157
|
+
const client = await this.clientsQueue.get(clientId);
|
|
158
|
+
if (client && client.isConnected()) {
|
|
159
|
+
client.disconnect();
|
|
160
|
+
}
|
|
161
|
+
this.clientsQueue.remove(clientId);
|
|
162
|
+
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSED);
|
|
163
|
+
}
|
|
164
|
+
async publish({ topics, message }) {
|
|
165
|
+
const targetTopics = [].concat(topics);
|
|
166
|
+
const msg = JSON.stringify(message);
|
|
167
|
+
const client = await this.clientsQueue.get(this.clientId);
|
|
168
|
+
if (client) {
|
|
169
|
+
logger.debug('Publishing to topic(s)', targetTopics.join(','), message);
|
|
170
|
+
targetTopics.forEach(topic => client.send(topic, msg));
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
logger.debug('Publishing to topic(s) failed', targetTopics.join(','), message);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
_onMessage(topic, msg) {
|
|
177
|
+
try {
|
|
178
|
+
const matchedTopicObservers = [];
|
|
179
|
+
this._topicObservers.forEach((observerForTopic, observerTopic) => {
|
|
180
|
+
if (mqttTopicMatch(observerTopic, topic)) {
|
|
181
|
+
matchedTopicObservers.push(observerForTopic);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
const parsedMessage = JSON.parse(msg);
|
|
185
|
+
if (typeof parsedMessage === 'object') {
|
|
186
|
+
// @ts-ignore
|
|
187
|
+
parsedMessage[topicSymbol] = topic;
|
|
188
|
+
}
|
|
189
|
+
matchedTopicObservers.forEach(observersForTopic => {
|
|
190
|
+
observersForTopic.forEach(observer => observer.next(parsedMessage));
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
logger.warn('Error handling message', error, msg);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
subscribe({ topics, options = {}, }) {
|
|
198
|
+
const targetTopics = [].concat(topics);
|
|
199
|
+
logger.debug('Subscribing to topic(s)', targetTopics.join(','));
|
|
200
|
+
let reconnectSubscription;
|
|
201
|
+
return new rxjs_1.Observable(observer => {
|
|
202
|
+
targetTopics.forEach(topic => {
|
|
203
|
+
// this._topicObservers is used to notify the observers according to the topic received on the message
|
|
204
|
+
let observersForTopic = this._topicObservers.get(topic);
|
|
205
|
+
if (!observersForTopic) {
|
|
206
|
+
observersForTopic = new Set();
|
|
207
|
+
this._topicObservers.set(topic, observersForTopic);
|
|
208
|
+
}
|
|
209
|
+
observersForTopic.add(observer);
|
|
210
|
+
});
|
|
211
|
+
const { clientId = this.clientId } = options;
|
|
212
|
+
// this._clientIdObservers is used to close observers when client gets disconnected
|
|
213
|
+
let observersForClientId = this._clientIdObservers.get(clientId);
|
|
214
|
+
if (!observersForClientId) {
|
|
215
|
+
observersForClientId = new Set();
|
|
216
|
+
}
|
|
217
|
+
if (observersForClientId) {
|
|
218
|
+
observersForClientId.add(observer);
|
|
219
|
+
this._clientIdObservers.set(clientId, observersForClientId);
|
|
220
|
+
}
|
|
221
|
+
(async () => {
|
|
222
|
+
const getClient = async () => {
|
|
223
|
+
try {
|
|
224
|
+
const { url = await this.endpoint } = options;
|
|
225
|
+
const client = await this.connect(clientId, { url });
|
|
226
|
+
if (client !== undefined) {
|
|
227
|
+
targetTopics.forEach(topic => {
|
|
228
|
+
client.subscribe(topic);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (e) {
|
|
233
|
+
logger.debug('Error forming connection', e);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
// Establish the initial connection
|
|
237
|
+
await getClient();
|
|
238
|
+
// Add an observable to the reconnection list to manage reconnection for this subscription
|
|
239
|
+
reconnectSubscription = new rxjs_1.Observable(observer => {
|
|
240
|
+
this.reconnectionMonitor.addObserver(observer);
|
|
241
|
+
}).subscribe(() => {
|
|
242
|
+
getClient();
|
|
243
|
+
});
|
|
244
|
+
})();
|
|
245
|
+
return async () => {
|
|
246
|
+
const client = await this.clientsQueue.get(clientId);
|
|
247
|
+
reconnectSubscription?.unsubscribe();
|
|
248
|
+
if (client) {
|
|
249
|
+
this._clientIdObservers.get(clientId)?.delete(observer);
|
|
250
|
+
// No more observers per client => client not needed anymore
|
|
251
|
+
if (this._clientIdObservers.get(clientId)?.size === 0) {
|
|
252
|
+
this.disconnect(clientId);
|
|
253
|
+
this.connectionStateMonitor.record(ConnectionStateMonitor_1.CONNECTION_CHANGE.CLOSING_CONNECTION);
|
|
254
|
+
this._clientIdObservers.delete(clientId);
|
|
255
|
+
}
|
|
256
|
+
targetTopics.forEach(topic => {
|
|
257
|
+
const observersForTopic = this._topicObservers.get(topic) ||
|
|
258
|
+
new Set();
|
|
259
|
+
observersForTopic.delete(observer);
|
|
260
|
+
// if no observers exists for the topic, topic should be removed
|
|
261
|
+
if (observersForTopic.size === 0) {
|
|
262
|
+
this._topicObservers.delete(topic);
|
|
263
|
+
if (client.isConnected()) {
|
|
264
|
+
client.unsubscribe(topic);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
return null;
|
|
270
|
+
};
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
exports.MqttOverWS = MqttOverWS;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { PubSubBase, PubSubOptions, PubSubContent, PublishInput, SubscribeInput } from '../types/PubSub';
|
|
3
|
+
export declare abstract class AbstractPubSub<T extends PubSubOptions> implements PubSubBase {
|
|
4
|
+
private _config;
|
|
5
|
+
constructor(options: T);
|
|
6
|
+
configure(config: T): T;
|
|
7
|
+
protected get options(): T;
|
|
8
|
+
abstract newClient(clientOptions: T): Promise<any>;
|
|
9
|
+
abstract publish(input: PublishInput): void;
|
|
10
|
+
abstract subscribe(input: SubscribeInput): Observable<PubSubContent>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractPubSub = void 0;
|
|
4
|
+
const core_1 = require("@aws-amplify/core");
|
|
5
|
+
const logger = new core_1.ConsoleLogger('AbstractPubSubProvider');
|
|
6
|
+
class AbstractPubSub {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this._config = options;
|
|
9
|
+
}
|
|
10
|
+
configure(config) {
|
|
11
|
+
this._config = { ...config, ...this._config };
|
|
12
|
+
logger.debug(`configure`, this._config);
|
|
13
|
+
return this.options;
|
|
14
|
+
}
|
|
15
|
+
get options() {
|
|
16
|
+
return { ...this._config };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.AbstractPubSub = AbstractPubSub;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { AMPLIFY_SYMBOL } from '@aws-amplify/core/internals/utils';
|
|
1
2
|
export declare const MAX_DELAY_MS = 5000;
|
|
2
3
|
export declare const NON_RETRYABLE_CODES: number[];
|
|
3
4
|
export declare const CONNECTION_STATE_CHANGE = "ConnectionStateChange";
|
|
@@ -63,7 +64,6 @@ export declare enum SOCKET_STATUS {
|
|
|
63
64
|
READY = 1,
|
|
64
65
|
CONNECTING = 2
|
|
65
66
|
}
|
|
66
|
-
export declare const AMPLIFY_SYMBOL: Symbol;
|
|
67
67
|
export declare const AWS_APPSYNC_REALTIME_HEADERS: {
|
|
68
68
|
accept: string;
|
|
69
69
|
'content-encoding': string;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RECONNECT_INTERVAL = exports.RECONNECT_DELAY = exports.DEFAULT_KEEP_ALIVE_ALERT_TIMEOUT = exports.DEFAULT_KEEP_ALIVE_TIMEOUT = exports.START_ACK_TIMEOUT = exports.CONNECTION_INIT_TIMEOUT = exports.AWS_APPSYNC_REALTIME_HEADERS = exports.SOCKET_STATUS = exports.SUBSCRIPTION_STATUS = exports.MESSAGE_TYPES = exports.CONNECTION_STATE_CHANGE = exports.NON_RETRYABLE_CODES = exports.MAX_DELAY_MS = exports.AMPLIFY_SYMBOL = void 0;
|
|
3
4
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
5
|
// SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
var utils_1 = require("@aws-amplify/core/internals/utils");
|
|
7
|
+
Object.defineProperty(exports, "AMPLIFY_SYMBOL", { enumerable: true, get: function () { return utils_1.AMPLIFY_SYMBOL; } });
|
|
5
8
|
exports.MAX_DELAY_MS = 5000;
|
|
6
9
|
exports.NON_RETRYABLE_CODES = [400, 401, 403];
|
|
7
10
|
exports.CONNECTION_STATE_CHANGE = 'ConnectionStateChange';
|
|
@@ -70,9 +73,6 @@ var SOCKET_STATUS;
|
|
|
70
73
|
SOCKET_STATUS[SOCKET_STATUS["READY"] = 1] = "READY";
|
|
71
74
|
SOCKET_STATUS[SOCKET_STATUS["CONNECTING"] = 2] = "CONNECTING";
|
|
72
75
|
})(SOCKET_STATUS = exports.SOCKET_STATUS || (exports.SOCKET_STATUS = {}));
|
|
73
|
-
exports.AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'
|
|
74
|
-
? Symbol.for('amplify_default')
|
|
75
|
-
: '@@amplify_default');
|
|
76
76
|
exports.AWS_APPSYNC_REALTIME_HEADERS = {
|
|
77
77
|
accept: 'application/json, text/javascript',
|
|
78
78
|
'content-encoding': 'amz-1.0',
|
|
@@ -102,4 +102,3 @@ exports.RECONNECT_DELAY = 5 * 1000;
|
|
|
102
102
|
* Default interval time in milleseconds between when reconnect is re-attempted
|
|
103
103
|
*/
|
|
104
104
|
exports.RECONNECT_INTERVAL = 60 * 1000;
|
|
105
|
-
//# sourceMappingURL=constants.js.map
|
package/lib/Providers/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export { MqttProviderOptions, MqttOverWSProvider, mqttTopicMatch, } from './MqttOverWSProvider';
|
|
1
|
+
export { AbstractPubSub } from './PubSub';
|
|
2
|
+
export { AWSIoT, AWSIoTOptions } from './AWSIot';
|
|
3
|
+
export { MqttOptions, MqttOverWS, mqttTopicMatch } from './MqttOverWS';
|
package/lib/Providers/index.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mqttTopicMatch = exports.MqttOverWS = exports.AWSIoT = exports.AbstractPubSub = void 0;
|
|
3
4
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
5
|
// SPDX-License-Identifier: Apache-2.0
|
|
5
|
-
var
|
|
6
|
-
exports
|
|
7
|
-
var
|
|
8
|
-
exports
|
|
9
|
-
var
|
|
10
|
-
exports
|
|
11
|
-
|
|
12
|
-
exports.MqttOverWSProvider = MqttOverWSProvider_1.MqttOverWSProvider;
|
|
13
|
-
exports.mqttTopicMatch = MqttOverWSProvider_1.mqttTopicMatch;
|
|
14
|
-
//# sourceMappingURL=index.js.map
|
|
6
|
+
var PubSub_1 = require("./PubSub");
|
|
7
|
+
Object.defineProperty(exports, "AbstractPubSub", { enumerable: true, get: function () { return PubSub_1.AbstractPubSub; } });
|
|
8
|
+
var AWSIot_1 = require("./AWSIot");
|
|
9
|
+
Object.defineProperty(exports, "AWSIoT", { enumerable: true, get: function () { return AWSIot_1.AWSIoT; } });
|
|
10
|
+
var MqttOverWS_1 = require("./MqttOverWS");
|
|
11
|
+
Object.defineProperty(exports, "MqttOverWS", { enumerable: true, get: function () { return MqttOverWS_1.MqttOverWS; } });
|
|
12
|
+
Object.defineProperty(exports, "mqttTopicMatch", { enumerable: true, get: function () { return MqttOverWS_1.mqttTopicMatch; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { AWSIoT as PubSub, AWSIoTOptions as PubSubOptions } from '../Providers';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.PubSub = void 0;
|
|
6
|
+
var Providers_1 = require("../Providers");
|
|
7
|
+
Object.defineProperty(exports, "PubSub", { enumerable: true, get: function () { return Providers_1.AWSIoT; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MqttOverWS as PubSub, MqttOptions as PubSubOptions, } from '../Providers';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.PubSub = void 0;
|
|
6
|
+
var Providers_1 = require("../Providers");
|
|
7
|
+
Object.defineProperty(exports, "PubSub", { enumerable: true, get: function () { return Providers_1.MqttOverWS; } });
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { PubSub } from './PubSub';
|
|
2
1
|
export { CONNECTION_STATE_CHANGE } from './Providers/constants';
|
|
3
2
|
export { ConnectionState, CONTROL_MSG } from './types';
|
|
4
|
-
export {
|
|
3
|
+
export { mqttTopicMatch } from './Providers';
|
|
4
|
+
export { PubSub, PubSubOptions } from './clients/iot';
|
package/lib/index.js
CHANGED
|
@@ -2,17 +2,13 @@
|
|
|
2
2
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
|
|
6
|
-
exports.PubSub = PubSub_1.PubSub;
|
|
5
|
+
exports.PubSub = exports.mqttTopicMatch = exports.CONTROL_MSG = exports.ConnectionState = exports.CONNECTION_STATE_CHANGE = void 0;
|
|
7
6
|
var constants_1 = require("./Providers/constants");
|
|
8
|
-
exports
|
|
7
|
+
Object.defineProperty(exports, "CONNECTION_STATE_CHANGE", { enumerable: true, get: function () { return constants_1.CONNECTION_STATE_CHANGE; } });
|
|
9
8
|
var types_1 = require("./types");
|
|
10
|
-
exports
|
|
11
|
-
exports
|
|
9
|
+
Object.defineProperty(exports, "ConnectionState", { enumerable: true, get: function () { return types_1.ConnectionState; } });
|
|
10
|
+
Object.defineProperty(exports, "CONTROL_MSG", { enumerable: true, get: function () { return types_1.CONTROL_MSG; } });
|
|
12
11
|
var Providers_1 = require("./Providers");
|
|
13
|
-
exports
|
|
14
|
-
|
|
15
|
-
exports
|
|
16
|
-
exports.MqttOverWSProvider = Providers_1.MqttOverWSProvider;
|
|
17
|
-
exports.mqttTopicMatch = Providers_1.mqttTopicMatch;
|
|
18
|
-
//# sourceMappingURL=index.js.map
|
|
12
|
+
Object.defineProperty(exports, "mqttTopicMatch", { enumerable: true, get: function () { return Providers_1.mqttTopicMatch; } });
|
|
13
|
+
var iot_1 = require("./clients/iot");
|
|
14
|
+
Object.defineProperty(exports, "PubSub", { enumerable: true, get: function () { return iot_1.PubSub; } });
|