@aws-amplify/pubsub 6.0.1-api-v6-models.9351bcf.0 → 6.0.1-api-v6-models.891fe0d.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/lib/Providers/AWSIot.js +27 -49
- package/lib/Providers/MqttOverWS.d.ts +5 -3
- package/lib/Providers/MqttOverWS.js +196 -320
- package/lib/Providers/PubSub.d.ts +3 -4
- package/lib/Providers/PubSub.js +12 -18
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/PubSub.d.ts +11 -2
- package/lib/utils/ConnectionStateMonitor.js +51 -64
- package/lib/utils/ReachabilityMonitor/index.js +2 -2
- package/lib/utils/ReachabilityMonitor/index.native.js +4 -6
- package/lib/utils/ReconnectionMonitor.js +20 -24
- package/lib-esm/Providers/AWSIot.js +24 -47
- package/lib-esm/Providers/MqttOverWS.d.ts +5 -3
- package/lib-esm/Providers/MqttOverWS.js +186 -312
- package/lib-esm/Providers/PubSub.d.ts +3 -4
- package/lib-esm/Providers/PubSub.js +11 -18
- package/lib-esm/Providers/constants.js +10 -10
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/PubSub.d.ts +11 -2
- package/lib-esm/utils/ConnectionStateMonitor.js +49 -63
- package/lib-esm/utils/ReachabilityMonitor/index.js +1 -1
- package/lib-esm/utils/ReachabilityMonitor/index.native.js +1 -3
- package/lib-esm/utils/ReconnectionMonitor.js +19 -24
- package/package.json +6 -10
- package/src/Providers/MqttOverWS.ts +9 -7
- package/src/Providers/PubSub.ts +10 -13
- package/src/types/PubSub.ts +13 -9
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { __assign } from "tslib";
|
|
4
3
|
import { Observable, map, filter } from 'rxjs';
|
|
5
4
|
import { ConnectionState } from '../types/PubSub';
|
|
6
5
|
import { ReachabilityMonitor } from './ReachabilityMonitor';
|
|
7
|
-
export
|
|
6
|
+
export const CONNECTION_CHANGE = {
|
|
8
7
|
KEEP_ALIVE_MISSED: { keepAliveState: 'unhealthy' },
|
|
9
8
|
KEEP_ALIVE: { keepAliveState: 'healthy' },
|
|
10
9
|
CONNECTION_ESTABLISHED: { connectionState: 'connected' },
|
|
@@ -21,9 +20,8 @@ export var CONNECTION_CHANGE = {
|
|
|
21
20
|
ONLINE: { networkState: 'connected' },
|
|
22
21
|
OFFLINE: { networkState: 'disconnected' },
|
|
23
22
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
var _this = this;
|
|
23
|
+
export class ConnectionStateMonitor {
|
|
24
|
+
constructor() {
|
|
27
25
|
this._networkMonitoringSubscription = undefined;
|
|
28
26
|
this._linkedConnectionState = {
|
|
29
27
|
networkState: 'connected',
|
|
@@ -32,73 +30,61 @@ var ConnectionStateMonitor = /** @class */ (function () {
|
|
|
32
30
|
keepAliveState: 'healthy',
|
|
33
31
|
};
|
|
34
32
|
// Attempt to update the state with the current actual network state
|
|
35
|
-
this._initialNetworkStateSubscription = ReachabilityMonitor().subscribe(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
_this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);
|
|
39
|
-
(_b = _this._initialNetworkStateSubscription) === null || _b === void 0 ? void 0 : _b.unsubscribe();
|
|
33
|
+
this._initialNetworkStateSubscription = ReachabilityMonitor().subscribe(({ online }) => {
|
|
34
|
+
this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);
|
|
35
|
+
this._initialNetworkStateSubscription?.unsubscribe();
|
|
40
36
|
});
|
|
41
37
|
this._linkedConnectionStateObservable =
|
|
42
|
-
new Observable(
|
|
43
|
-
connectionStateObserver.next(
|
|
44
|
-
|
|
38
|
+
new Observable(connectionStateObserver => {
|
|
39
|
+
connectionStateObserver.next(this._linkedConnectionState);
|
|
40
|
+
this._linkedConnectionStateObserver = connectionStateObserver;
|
|
45
41
|
});
|
|
46
42
|
}
|
|
47
43
|
/**
|
|
48
44
|
* Turn network state monitoring on if it isn't on already
|
|
49
45
|
*/
|
|
50
|
-
|
|
51
|
-
var _this = this;
|
|
52
|
-
var _a;
|
|
46
|
+
enableNetworkMonitoring() {
|
|
53
47
|
// If no initial network state was discovered, stop trying
|
|
54
|
-
|
|
48
|
+
this._initialNetworkStateSubscription?.unsubscribe();
|
|
55
49
|
// Maintain the network state based on the reachability monitor
|
|
56
50
|
if (this._networkMonitoringSubscription === undefined) {
|
|
57
|
-
this._networkMonitoringSubscription = ReachabilityMonitor().subscribe(
|
|
58
|
-
|
|
59
|
-
_this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);
|
|
51
|
+
this._networkMonitoringSubscription = ReachabilityMonitor().subscribe(({ online }) => {
|
|
52
|
+
this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);
|
|
60
53
|
});
|
|
61
54
|
}
|
|
62
|
-
}
|
|
55
|
+
}
|
|
63
56
|
/**
|
|
64
57
|
* Turn network state monitoring off if it isn't off already
|
|
65
58
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
(_a = this._networkMonitoringSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
59
|
+
disableNetworkMonitoring() {
|
|
60
|
+
this._networkMonitoringSubscription?.unsubscribe();
|
|
69
61
|
this._networkMonitoringSubscription = undefined;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return this.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}));
|
|
93
|
-
},
|
|
94
|
-
enumerable: false,
|
|
95
|
-
configurable: true
|
|
96
|
-
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Get the observable that allows us to monitor the connection state
|
|
65
|
+
*
|
|
66
|
+
* @returns {Observable<ConnectionState>} - The observable that emits ConnectionState updates
|
|
67
|
+
*/
|
|
68
|
+
get connectionStateObservable() {
|
|
69
|
+
let previous;
|
|
70
|
+
// The linked state aggregates state changes to any of the network, connection,
|
|
71
|
+
// intendedConnection and keepAliveHealth. Some states will change these independent
|
|
72
|
+
// states without changing the overall connection state.
|
|
73
|
+
// After translating from linked states to ConnectionState, then remove any duplicates
|
|
74
|
+
return this._linkedConnectionStateObservable
|
|
75
|
+
.pipe(map(value => {
|
|
76
|
+
return this.connectionStatesTranslator(value);
|
|
77
|
+
}))
|
|
78
|
+
.pipe(filter(current => {
|
|
79
|
+
const toInclude = current !== previous;
|
|
80
|
+
previous = current;
|
|
81
|
+
return toInclude;
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
97
84
|
/*
|
|
98
85
|
* Updates local connection state and emits the full state to the observer.
|
|
99
86
|
*/
|
|
100
|
-
|
|
101
|
-
var _a;
|
|
87
|
+
record(statusUpdates) {
|
|
102
88
|
// Maintain the network monitor
|
|
103
89
|
if (statusUpdates.intendedConnectionState === 'connected') {
|
|
104
90
|
this.enableNetworkMonitoring();
|
|
@@ -107,15 +93,17 @@ var ConnectionStateMonitor = /** @class */ (function () {
|
|
|
107
93
|
this.disableNetworkMonitoring();
|
|
108
94
|
}
|
|
109
95
|
// Maintain the socket state
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
96
|
+
const newSocketStatus = {
|
|
97
|
+
...this._linkedConnectionState,
|
|
98
|
+
...statusUpdates,
|
|
99
|
+
};
|
|
100
|
+
this._linkedConnectionState = { ...newSocketStatus };
|
|
101
|
+
this._linkedConnectionStateObserver?.next(this._linkedConnectionState);
|
|
102
|
+
}
|
|
114
103
|
/*
|
|
115
104
|
* Translate the ConnectionState structure into a specific ConnectionState string literal union
|
|
116
105
|
*/
|
|
117
|
-
|
|
118
|
-
var connectionState = _a.connectionState, networkState = _a.networkState, intendedConnectionState = _a.intendedConnectionState, keepAliveState = _a.keepAliveState;
|
|
106
|
+
connectionStatesTranslator({ connectionState, networkState, intendedConnectionState, keepAliveState, }) {
|
|
119
107
|
if (connectionState === 'connected' && networkState === 'disconnected')
|
|
120
108
|
return ConnectionState.ConnectedPendingNetwork;
|
|
121
109
|
if (connectionState === 'connected' &&
|
|
@@ -136,7 +124,5 @@ var ConnectionStateMonitor = /** @class */ (function () {
|
|
|
136
124
|
if (connectionState === 'disconnected')
|
|
137
125
|
return ConnectionState.Disconnected;
|
|
138
126
|
return ConnectionState.Connected;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
}());
|
|
142
|
-
export { ConnectionStateMonitor };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { Reachability } from '@aws-amplify/core/internals/utils';
|
|
4
|
-
export
|
|
4
|
+
export const ReachabilityMonitor = () => new Reachability().networkMonitor();
|
|
@@ -2,6 +2,4 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { Reachability } from '@aws-amplify/core/internals/utils';
|
|
4
4
|
import { default as NetInfo } from '@react-native-community/netinfo';
|
|
5
|
-
export
|
|
6
|
-
return new Reachability().networkMonitor(NetInfo);
|
|
7
|
-
};
|
|
5
|
+
export const ReachabilityMonitor = () => new Reachability().networkMonitor(NetInfo);
|
|
@@ -10,31 +10,30 @@ export var ReconnectEvent;
|
|
|
10
10
|
* Attempting to reconnect only once creates unrecoverable states when the network state isn't
|
|
11
11
|
* supported by the browser, so this keeps retrying every minute until halted.
|
|
12
12
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
export class ReconnectionMonitor {
|
|
14
|
+
constructor() {
|
|
15
15
|
this.reconnectObservers = [];
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Add reconnect observer to the list of observers to alert on reconnect
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
addObserver(reconnectObserver) {
|
|
21
21
|
this.reconnectObservers.push(reconnectObserver);
|
|
22
|
-
}
|
|
22
|
+
}
|
|
23
23
|
/**
|
|
24
24
|
* Given a reconnect event, start the appropriate behavior
|
|
25
25
|
*/
|
|
26
|
-
|
|
27
|
-
var _this = this;
|
|
26
|
+
record(event) {
|
|
28
27
|
if (event === ReconnectEvent.START_RECONNECT) {
|
|
29
28
|
// If the reconnection hasn't been started
|
|
30
29
|
if (this.reconnectSetTimeoutId === undefined &&
|
|
31
30
|
this.reconnectIntervalId === undefined) {
|
|
32
|
-
this.reconnectSetTimeoutId = setTimeout(
|
|
31
|
+
this.reconnectSetTimeoutId = setTimeout(() => {
|
|
33
32
|
// Reconnect now
|
|
34
|
-
|
|
33
|
+
this._triggerReconnect();
|
|
35
34
|
// Retry reconnect every periodically until it works
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
this.reconnectIntervalId = setInterval(() => {
|
|
36
|
+
this._triggerReconnect();
|
|
38
37
|
}, RECONNECT_INTERVAL);
|
|
39
38
|
}, RECONNECT_DELAY);
|
|
40
39
|
}
|
|
@@ -49,22 +48,18 @@ var ReconnectionMonitor = /** @class */ (function () {
|
|
|
49
48
|
this.reconnectSetTimeoutId = undefined;
|
|
50
49
|
}
|
|
51
50
|
}
|
|
52
|
-
}
|
|
51
|
+
}
|
|
53
52
|
/**
|
|
54
53
|
* Complete all reconnect observers
|
|
55
54
|
*/
|
|
56
|
-
|
|
57
|
-
this.reconnectObservers.forEach(
|
|
58
|
-
|
|
59
|
-
(_a = reconnectObserver.complete) === null || _a === void 0 ? void 0 : _a.call(reconnectObserver);
|
|
55
|
+
close() {
|
|
56
|
+
this.reconnectObservers.forEach(reconnectObserver => {
|
|
57
|
+
reconnectObserver.complete?.();
|
|
60
58
|
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
this.reconnectObservers.forEach(
|
|
64
|
-
|
|
65
|
-
(_a = reconnectObserver.next) === null || _a === void 0 ? void 0 : _a.call(reconnectObserver);
|
|
59
|
+
}
|
|
60
|
+
_triggerReconnect() {
|
|
61
|
+
this.reconnectObservers.forEach(reconnectObserver => {
|
|
62
|
+
reconnectObserver.next?.();
|
|
66
63
|
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
}());
|
|
70
|
-
export { ReconnectionMonitor };
|
|
64
|
+
}
|
|
65
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/pubsub",
|
|
3
|
-
"version": "6.0.1-api-v6-models.
|
|
3
|
+
"version": "6.0.1-api-v6-models.891fe0d.0+891fe0d",
|
|
4
4
|
"description": "Pubsub category of aws-amplify",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib-esm/index.js",
|
|
@@ -28,10 +28,7 @@
|
|
|
28
28
|
"ts-coverage": "typescript-coverage-report -p ./tsconfig.json -t 93.0 -i src/vendor/paho-mqtt.js"
|
|
29
29
|
},
|
|
30
30
|
"typesVersions": {
|
|
31
|
-
">=4.
|
|
32
|
-
"*": [
|
|
33
|
-
"./lib-esm/index.d.ts"
|
|
34
|
-
],
|
|
31
|
+
">=4.2": {
|
|
35
32
|
"iot": [
|
|
36
33
|
"./lib-esm/clients/iot.d.ts"
|
|
37
34
|
],
|
|
@@ -75,7 +72,7 @@
|
|
|
75
72
|
"mqtt"
|
|
76
73
|
],
|
|
77
74
|
"dependencies": {
|
|
78
|
-
"@aws-amplify/auth": "6.0.1-api-v6-models.
|
|
75
|
+
"@aws-amplify/auth": "6.0.1-api-v6-models.891fe0d.0+891fe0d",
|
|
79
76
|
"buffer": "4.9.2",
|
|
80
77
|
"graphql": "15.8.0",
|
|
81
78
|
"rxjs": "^7.8.1",
|
|
@@ -83,11 +80,10 @@
|
|
|
83
80
|
"url": "0.11.0"
|
|
84
81
|
},
|
|
85
82
|
"peerDependencies": {
|
|
86
|
-
"@aws-amplify/core": "6.0.1-api-v6-models.
|
|
83
|
+
"@aws-amplify/core": "6.0.1-api-v6-models.891fe0d.0+891fe0d"
|
|
87
84
|
},
|
|
88
85
|
"devDependencies": {
|
|
89
|
-
"@aws-amplify/core": "6.0.1-api-v6-models.
|
|
90
|
-
"cpx": "^1.5.0",
|
|
86
|
+
"@aws-amplify/core": "6.0.1-api-v6-models.891fe0d.0+891fe0d",
|
|
91
87
|
"typescript": "5.0.2"
|
|
92
88
|
},
|
|
93
89
|
"size-limit": [
|
|
@@ -148,5 +144,5 @@
|
|
|
148
144
|
"lib-esm"
|
|
149
145
|
]
|
|
150
146
|
},
|
|
151
|
-
"gitHead": "
|
|
147
|
+
"gitHead": "891fe0dc64e168de42b3b8d42def3eb9aed89efa"
|
|
152
148
|
}
|
|
@@ -11,6 +11,8 @@ import {
|
|
|
11
11
|
PubSubContentObserver,
|
|
12
12
|
PubSubContent,
|
|
13
13
|
PubSubOptions,
|
|
14
|
+
PublishInput,
|
|
15
|
+
SubscribeInput,
|
|
14
16
|
} from '../types/PubSub';
|
|
15
17
|
import { Hub, HubPayload, ConsoleLogger as Logger } from '@aws-amplify/core';
|
|
16
18
|
import { amplifyUuid } from '@aws-amplify/core/internals/utils';
|
|
@@ -248,15 +250,15 @@ export class MqttOverWS extends AbstractPubSub<MqttOptions> {
|
|
|
248
250
|
this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
|
|
249
251
|
}
|
|
250
252
|
|
|
251
|
-
async publish(topics
|
|
253
|
+
async publish({ topics, message }: PublishInput) {
|
|
252
254
|
const targetTopics = ([] as string[]).concat(topics);
|
|
253
|
-
const
|
|
255
|
+
const msg = JSON.stringify(message);
|
|
254
256
|
|
|
255
257
|
const client = await this.clientsQueue.get(this.clientId);
|
|
256
258
|
|
|
257
259
|
if (client) {
|
|
258
260
|
logger.debug('Publishing to topic(s)', targetTopics.join(','), message);
|
|
259
|
-
targetTopics.forEach(topic => client.send(topic,
|
|
261
|
+
targetTopics.forEach(topic => client.send(topic, msg));
|
|
260
262
|
} else {
|
|
261
263
|
logger.debug(
|
|
262
264
|
'Publishing to topic(s) failed',
|
|
@@ -295,10 +297,10 @@ export class MqttOverWS extends AbstractPubSub<MqttOptions> {
|
|
|
295
297
|
}
|
|
296
298
|
}
|
|
297
299
|
|
|
298
|
-
subscribe(
|
|
299
|
-
topics
|
|
300
|
-
options
|
|
301
|
-
): Observable<PubSubContent> {
|
|
300
|
+
subscribe({
|
|
301
|
+
topics,
|
|
302
|
+
options = {},
|
|
303
|
+
}: SubscribeInput & { options?: MqttOptions }): Observable<PubSubContent> {
|
|
302
304
|
const targetTopics = ([] as string[]).concat(topics);
|
|
303
305
|
logger.debug('Subscribing to topic(s)', targetTopics.join(','));
|
|
304
306
|
let reconnectSubscription: Subscription;
|
package/src/Providers/PubSub.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { Observable } from 'rxjs';
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
PubSubBase,
|
|
6
|
+
PubSubOptions,
|
|
7
|
+
PubSubContent,
|
|
8
|
+
PublishInput,
|
|
9
|
+
SubscribeInput,
|
|
10
|
+
} from '../types/PubSub';
|
|
6
11
|
import { ConsoleLogger as Logger } from '@aws-amplify/core';
|
|
7
12
|
const logger = new Logger('AbstractPubSubProvider');
|
|
8
13
|
|
|
@@ -29,15 +34,7 @@ export abstract class AbstractPubSub<T extends PubSubOptions>
|
|
|
29
34
|
|
|
30
35
|
public abstract newClient(clientOptions: T): Promise<any>;
|
|
31
36
|
|
|
32
|
-
public abstract publish(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
options?: T
|
|
36
|
-
): void;
|
|
37
|
-
|
|
38
|
-
public abstract subscribe(
|
|
39
|
-
topics: string[] | string,
|
|
40
|
-
options?: T,
|
|
41
|
-
customUserAgentDetails?: CustomUserAgentDetails
|
|
42
|
-
): Observable<PubSubContent>;
|
|
37
|
+
public abstract publish(input: PublishInput): void;
|
|
38
|
+
|
|
39
|
+
public abstract subscribe(input: SubscribeInput): Observable<PubSubContent>;
|
|
43
40
|
}
|
package/src/types/PubSub.ts
CHANGED
|
@@ -64,14 +64,18 @@ export interface PubSubBase {
|
|
|
64
64
|
// configure your provider
|
|
65
65
|
configure(config: Record<string, unknown>): Record<string, unknown>;
|
|
66
66
|
|
|
67
|
-
publish(
|
|
68
|
-
topics: string[] | string,
|
|
69
|
-
msg: PubSubContent,
|
|
70
|
-
options?: PubSubOptions
|
|
71
|
-
): void;
|
|
67
|
+
publish(input: PublishInput): void;
|
|
72
68
|
|
|
73
|
-
subscribe(
|
|
74
|
-
topics: string[] | string,
|
|
75
|
-
options?: PubSubOptions
|
|
76
|
-
): Observable<PubSubContent>;
|
|
69
|
+
subscribe(input: SubscribeInput): Observable<PubSubContent>;
|
|
77
70
|
}
|
|
71
|
+
|
|
72
|
+
export type PublishInput = {
|
|
73
|
+
topics: string[] | string;
|
|
74
|
+
message: PubSubContent;
|
|
75
|
+
options?: PubSubOptions;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export type SubscribeInput = {
|
|
79
|
+
topics: string[] | string;
|
|
80
|
+
options?: PubSubOptions;
|
|
81
|
+
};
|