@aws-amplify/pubsub 4.2.10-cloud-logging.7 → 4.2.10-cloud-logging.8

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +24 -49
  2. package/dist/aws-amplify-pubsub.js +238 -202
  3. package/dist/aws-amplify-pubsub.js.map +1 -1
  4. package/dist/aws-amplify-pubsub.min.js +2 -2
  5. package/dist/aws-amplify-pubsub.min.js.map +1 -1
  6. package/lib/Providers/AWSAppSyncProvider.d.ts +3 -0
  7. package/lib/Providers/AWSAppSyncProvider.js +3 -0
  8. package/lib/Providers/AWSAppSyncProvider.js.map +1 -1
  9. package/lib/Providers/AWSAppSyncRealTimeProvider.d.ts +19 -4
  10. package/lib/Providers/AWSAppSyncRealTimeProvider.js +174 -154
  11. package/lib/Providers/AWSAppSyncRealTimeProvider.js.map +1 -1
  12. package/lib/Providers/AWSIotProvider.d.ts +6 -1
  13. package/lib/Providers/AWSIotProvider.js +3 -2
  14. package/lib/Providers/AWSIotProvider.js.map +1 -1
  15. package/lib/Providers/MqttOverWSProvider.d.ts +14 -11
  16. package/lib/Providers/MqttOverWSProvider.js +17 -10
  17. package/lib/Providers/MqttOverWSProvider.js.map +1 -1
  18. package/lib/Providers/PubSubProvider.d.ts +7 -7
  19. package/lib/Providers/PubSubProvider.js.map +1 -1
  20. package/lib/PubSub.d.ts +6 -6
  21. package/lib/PubSub.js +2 -2
  22. package/lib/PubSub.js.map +1 -1
  23. package/lib/types/Provider.d.ts +3 -3
  24. package/lib/types/PubSub.d.ts +5 -1
  25. package/lib-esm/Providers/AWSAppSyncProvider.d.ts +3 -0
  26. package/lib-esm/Providers/AWSAppSyncProvider.js +3 -0
  27. package/lib-esm/Providers/AWSAppSyncProvider.js.map +1 -1
  28. package/lib-esm/Providers/AWSAppSyncRealTimeProvider.d.ts +19 -4
  29. package/lib-esm/Providers/AWSAppSyncRealTimeProvider.js +174 -154
  30. package/lib-esm/Providers/AWSAppSyncRealTimeProvider.js.map +1 -1
  31. package/lib-esm/Providers/AWSIotProvider.d.ts +6 -1
  32. package/lib-esm/Providers/AWSIotProvider.js +3 -2
  33. package/lib-esm/Providers/AWSIotProvider.js.map +1 -1
  34. package/lib-esm/Providers/MqttOverWSProvider.d.ts +14 -11
  35. package/lib-esm/Providers/MqttOverWSProvider.js +17 -10
  36. package/lib-esm/Providers/MqttOverWSProvider.js.map +1 -1
  37. package/lib-esm/Providers/PubSubProvider.d.ts +7 -7
  38. package/lib-esm/Providers/PubSubProvider.js.map +1 -1
  39. package/lib-esm/PubSub.d.ts +6 -6
  40. package/lib-esm/PubSub.js +2 -2
  41. package/lib-esm/PubSub.js.map +1 -1
  42. package/lib-esm/types/Provider.d.ts +3 -3
  43. package/lib-esm/types/PubSub.d.ts +5 -1
  44. package/package.json +5 -5
  45. package/src/Providers/AWSAppSyncProvider.ts +27 -26
  46. package/src/Providers/AWSAppSyncRealTimeProvider.ts +247 -191
  47. package/src/Providers/AWSIotProvider.ts +10 -1
  48. package/src/Providers/MqttOverWSProvider.ts +52 -33
  49. package/src/Providers/PubSubProvider.ts +8 -8
  50. package/src/PubSub.ts +10 -10
  51. package/src/types/Provider.ts +3 -7
  52. package/src/types/PubSub.ts +6 -1
@@ -10,12 +10,21 @@
10
10
  * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
11
  * and limitations under the License.
12
12
  */
13
- import { MqttOverWSProvider } from './MqttOverWSProvider';
13
+ import { MqttOverWSProvider, MqttProviderOptions } from './MqttOverWSProvider';
14
14
  import { Signer, Credentials } from '@aws-amplify/core';
15
15
 
16
16
  const SERVICE_NAME = 'iotdevicegateway';
17
17
 
18
+ export interface AWSIoTProviderOptions extends MqttProviderOptions {
19
+ aws_pubsub_region?: string;
20
+ aws_pubsub_endpoint?: string;
21
+ }
22
+
18
23
  export class AWSIoTProvider extends MqttOverWSProvider {
24
+ constructor(options: AWSIoTProviderOptions = {}) {
25
+ super(options);
26
+ }
27
+
19
28
  protected get region() {
20
29
  return this.options.aws_pubsub_region;
21
30
  }
@@ -15,7 +15,7 @@ import { v4 as uuid } from 'uuid';
15
15
  import Observable from 'zen-observable-ts';
16
16
 
17
17
  import { AbstractPubSubProvider } from './PubSubProvider';
18
- import { ProvidertOptions, SubscriptionObserver } from '../types';
18
+ import { ProviderOptions, SubscriptionObserver } from '../types';
19
19
  import { ConsoleLogger as Logger } from '@aws-amplify/core';
20
20
 
21
21
  const logger = new Logger('MqttOverWSProvider');
@@ -34,32 +34,40 @@ export function mqttTopicMatch(filter: string, topic: string) {
34
34
  return length === topicArray.length;
35
35
  }
36
36
 
37
- export interface MqttProvidertOptions extends ProvidertOptions {
37
+ export interface MqttProviderOptions extends ProviderOptions {
38
38
  clientId?: string;
39
39
  url?: string;
40
40
  }
41
41
 
42
+ /**
43
+ * @deprecated Migrated to MqttProviderOptions
44
+ */
45
+ export type MqttProvidertOptions = MqttProviderOptions;
46
+
42
47
  class ClientsQueue {
43
48
  private promises: Map<string, Promise<any>> = new Map();
44
49
 
45
- async get(clientId: string, clientFactory: (string) => Promise<any>) {
46
- let promise = this.promises.get(clientId);
47
- if (promise) {
48
- return promise;
50
+ async get(clientId: string, clientFactory?: (input: string) => Promise<any>) {
51
+ const cachedPromise = this.promises.get(clientId);
52
+ if (cachedPromise) {
53
+ return cachedPromise;
49
54
  }
50
55
 
51
- promise = clientFactory(clientId);
56
+ if (clientFactory) {
57
+ const newPromise = clientFactory(clientId);
52
58
 
53
- this.promises.set(clientId, promise);
59
+ this.promises.set(clientId, newPromise);
54
60
 
55
- return promise;
61
+ return newPromise;
62
+ }
63
+ return undefined;
56
64
  }
57
65
 
58
66
  get allClients() {
59
67
  return Array.from(this.promises.keys());
60
68
  }
61
69
 
62
- remove(clientId) {
70
+ remove(clientId: string) {
63
71
  this.promises.delete(clientId);
64
72
  }
65
73
  }
@@ -69,7 +77,7 @@ const topicSymbol = typeof Symbol !== 'undefined' ? Symbol('topic') : '@@topic';
69
77
  export class MqttOverWSProvider extends AbstractPubSubProvider {
70
78
  private _clientsQueue = new ClientsQueue();
71
79
 
72
- constructor(options: MqttProvidertOptions = {}) {
80
+ constructor(options: MqttProviderOptions = {}) {
73
81
  super({ ...options, clientId: options.clientId || uuid() });
74
82
  }
75
83
 
@@ -90,7 +98,7 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
90
98
  .aws_appsync_dangerously_connect_to_http_endpoint_for_testing;
91
99
  }
92
100
 
93
- protected getTopicForValue(value) {
101
+ protected getTopicForValue(value: any) {
94
102
  return typeof value === 'object' && value[topicSymbol];
95
103
  }
96
104
 
@@ -98,11 +106,21 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
98
106
  return 'MqttOverWSProvider';
99
107
  }
100
108
 
101
- public onDisconnect({ clientId, errorCode, ...args }) {
109
+ public onDisconnect({
110
+ clientId,
111
+ errorCode,
112
+ ...args
113
+ }: {
114
+ clientId?: string;
115
+ errorCode?: number;
116
+ }) {
102
117
  if (errorCode !== 0) {
103
118
  logger.warn(clientId, JSON.stringify({ errorCode, ...args }, null, 2));
104
119
 
105
- const topicsToDelete = [];
120
+ const topicsToDelete: string[] = [];
121
+ if (!clientId) {
122
+ return;
123
+ }
106
124
  const clientIdObservers = this._clientIdObservers.get(clientId);
107
125
  if (!clientIdObservers) {
108
126
  return;
@@ -128,10 +146,7 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
128
146
  }
129
147
  }
130
148
 
131
- public async newClient({
132
- url,
133
- clientId,
134
- }: MqttProvidertOptions): Promise<any> {
149
+ public async newClient({ url, clientId }: MqttProviderOptions): Promise<any> {
135
150
  logger.debug('Creating new MQTT client', clientId);
136
151
 
137
152
  // @ts-ignore
@@ -140,10 +155,18 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
140
155
  client.onMessageArrived = ({
141
156
  destinationName: topic,
142
157
  payloadString: msg,
158
+ }: {
159
+ destinationName: string;
160
+ payloadString: string;
143
161
  }) => {
144
162
  this._onMessage(topic, msg);
145
163
  };
146
- client.onConnectionLost = ({ errorCode, ...args }) => {
164
+ client.onConnectionLost = ({
165
+ errorCode,
166
+ ...args
167
+ }: {
168
+ errorCode: number;
169
+ }) => {
147
170
  this.onDisconnect({ clientId, errorCode, ...args });
148
171
  };
149
172
 
@@ -161,7 +184,7 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
161
184
 
162
185
  protected async connect(
163
186
  clientId: string,
164
- options: MqttProvidertOptions = {}
187
+ options: MqttProviderOptions = {}
165
188
  ): Promise<any> {
166
189
  return await this.clientsQueue.get(clientId, clientId =>
167
190
  this.newClient({ ...options, clientId })
@@ -169,7 +192,7 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
169
192
  }
170
193
 
171
194
  protected async disconnect(clientId: string): Promise<void> {
172
- const client = await this.clientsQueue.get(clientId, () => null);
195
+ const client = await this.clientsQueue.get(clientId);
173
196
 
174
197
  if (client && client.isConnected()) {
175
198
  client.disconnect();
@@ -189,19 +212,15 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
189
212
  targetTopics.forEach(topic => client.send(topic, message));
190
213
  }
191
214
 
192
- protected _topicObservers: Map<
193
- string,
194
- Set<SubscriptionObserver<any>>
195
- > = new Map();
215
+ protected _topicObservers: Map<string, Set<SubscriptionObserver<any>>> =
216
+ new Map();
196
217
 
197
- protected _clientIdObservers: Map<
198
- string,
199
- Set<SubscriptionObserver<any>>
200
- > = new Map();
218
+ protected _clientIdObservers: Map<string, Set<SubscriptionObserver<any>>> =
219
+ new Map();
201
220
 
202
221
  private _onMessage(topic: string, msg: any) {
203
222
  try {
204
- const matchedTopicObservers = [];
223
+ const matchedTopicObservers: Set<SubscriptionObserver<any>>[] = [];
205
224
  this._topicObservers.forEach((observerForTopic, observerTopic) => {
206
225
  if (mqttTopicMatch(observerTopic, topic)) {
207
226
  matchedTopicObservers.push(observerForTopic);
@@ -223,7 +242,7 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
223
242
 
224
243
  subscribe(
225
244
  topics: string[] | string,
226
- options: MqttProvidertOptions = {}
245
+ options: MqttProviderOptions = {}
227
246
  ): Observable<any> {
228
247
  const targetTopics = ([] as string[]).concat(topics);
229
248
  logger.debug('Subscribing to topic(s)', targetTopics.join(','));
@@ -271,9 +290,9 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
271
290
  logger.debug('Unsubscribing from topic(s)', targetTopics.join(','));
272
291
 
273
292
  if (client) {
274
- this._clientIdObservers.get(clientId).delete(observer);
293
+ this._clientIdObservers.get(clientId)?.delete(observer);
275
294
  // No more observers per client => client not needed anymore
276
- if (this._clientIdObservers.get(clientId).size === 0) {
295
+ if (this._clientIdObservers.get(clientId)?.size === 0) {
277
296
  this.disconnect(clientId);
278
297
  this._clientIdObservers.delete(clientId);
279
298
  }
@@ -11,19 +11,19 @@
11
11
  * and limitations under the License.
12
12
  */
13
13
  import Observable from 'zen-observable-ts';
14
- import { PubSubProvider, ProvidertOptions } from '../types';
14
+ import { PubSubProvider, ProviderOptions } from '../types';
15
15
  import { ConsoleLogger as Logger } from '@aws-amplify/core';
16
16
 
17
17
  const logger = new Logger('AbstractPubSubProvider');
18
18
 
19
19
  export abstract class AbstractPubSubProvider implements PubSubProvider {
20
- private _config: ProvidertOptions;
20
+ private _config: ProviderOptions;
21
21
 
22
- constructor(options: ProvidertOptions = {}) {
22
+ constructor(options: ProviderOptions = {}) {
23
23
  this._config = options;
24
24
  }
25
25
 
26
- configure(config: ProvidertOptions = {}): ProvidertOptions {
26
+ configure(config: ProviderOptions = {}): ProviderOptions {
27
27
  this._config = { ...config, ...this._config };
28
28
 
29
29
  logger.debug(`configure ${this.getProviderName()}`, this._config);
@@ -37,20 +37,20 @@ export abstract class AbstractPubSubProvider implements PubSubProvider {
37
37
 
38
38
  abstract getProviderName(): string;
39
39
 
40
- protected get options(): ProvidertOptions {
40
+ protected get options(): ProviderOptions {
41
41
  return { ...this._config };
42
42
  }
43
43
 
44
- public abstract newClient(clientOptions: ProvidertOptions): Promise<any>;
44
+ public abstract newClient(clientOptions: ProviderOptions): Promise<any>;
45
45
 
46
46
  public abstract publish(
47
47
  topics: string[] | string,
48
48
  msg: any,
49
- options?: ProvidertOptions
49
+ options?: ProviderOptions
50
50
  ): void;
51
51
 
52
52
  public abstract subscribe(
53
53
  topics: string[] | string,
54
- options?: ProvidertOptions
54
+ options?: ProviderOptions
55
55
  ): Observable<any>;
56
56
  }
package/src/PubSub.ts CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  INTERNAL_AWS_APPSYNC_PUBSUB_PROVIDER,
21
21
  INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER,
22
22
  } from '@aws-amplify/core';
23
- import { PubSubProvider, PubSubOptions, ProvidertOptions } from './types';
23
+ import { PubSubProvider, PubSubOptions, ProviderOptions } from './types';
24
24
  import { AWSAppSyncProvider, AWSAppSyncRealTimeProvider } from './Providers';
25
25
 
26
26
  const { isNode } = browserOrNode();
@@ -34,12 +34,12 @@ export class PubSubClass {
34
34
  /**
35
35
  * Internal instance of AWSAppSyncProvider used by the API category to subscribe to AppSync
36
36
  */
37
- private _awsAppSyncProvider: AWSAppSyncProvider;
37
+ private _awsAppSyncProvider?: AWSAppSyncProvider;
38
38
 
39
39
  /**
40
40
  * Internal instance of AWSAppSyncRealTimeProvider used by the API category to subscribe to AppSync
41
41
  */
42
- private _awsAppSyncRealTimeProvider: AWSAppSyncRealTimeProvider;
42
+ private _awsAppSyncRealTimeProvider?: AWSAppSyncRealTimeProvider;
43
43
 
44
44
  /**
45
45
  * Lazy instantiate AWSAppSyncProvider when it is required by the API category
@@ -68,8 +68,8 @@ export class PubSubClass {
68
68
  *
69
69
  * @param {PubSubOptions} options - Configuration object for PubSub
70
70
  */
71
- constructor(options: PubSubOptions) {
72
- this._options = options;
71
+ constructor(options?: PubSubOptions) {
72
+ this._options = options ?? {};
73
73
  logger.debug('PubSub Options', this._options);
74
74
  this._pluggables = [];
75
75
  this.subscribe = this.subscribe.bind(this);
@@ -120,7 +120,7 @@ export class PubSubClass {
120
120
  );
121
121
  }
122
122
 
123
- private getProviderByName(providerName) {
123
+ private getProviderByName(providerName: string | symbol) {
124
124
  if (providerName === INTERNAL_AWS_APPSYNC_PUBSUB_PROVIDER) {
125
125
  return this.awsAppSyncProvider;
126
126
  }
@@ -133,7 +133,7 @@ export class PubSubClass {
133
133
  );
134
134
  }
135
135
 
136
- private getProviders(options: ProvidertOptions = {}) {
136
+ private getProviders(options: ProviderOptions = {}) {
137
137
  const { provider: providerName } = options;
138
138
  if (!providerName) {
139
139
  return this._pluggables;
@@ -150,7 +150,7 @@ export class PubSubClass {
150
150
  async publish(
151
151
  topics: string[] | string,
152
152
  msg: any,
153
- options?: ProvidertOptions
153
+ options?: ProviderOptions
154
154
  ) {
155
155
  return Promise.all(
156
156
  this.getProviders(options).map(provider =>
@@ -161,7 +161,7 @@ export class PubSubClass {
161
161
 
162
162
  subscribe(
163
163
  topics: string[] | string,
164
- options?: ProvidertOptions
164
+ options?: ProviderOptions
165
165
  ): Observable<any> {
166
166
  if (isNode && this._options && this._options.ssr) {
167
167
  throw new Error(
@@ -194,5 +194,5 @@ export class PubSubClass {
194
194
  }
195
195
  }
196
196
 
197
- export const PubSub = new PubSubClass(null);
197
+ export const PubSub = new PubSubClass();
198
198
  Amplify.register(PubSub);
@@ -11,7 +11,7 @@
11
11
  * and limitations under the License.
12
12
  */
13
13
  import Observable from 'zen-observable-ts';
14
- import { ProvidertOptions } from './PubSub';
14
+ import { ProviderOptions } from './PubSub';
15
15
 
16
16
  export interface PubSubProvider {
17
17
  // configure your provider
@@ -23,14 +23,10 @@ export interface PubSubProvider {
23
23
  // return the name of you provider
24
24
  getProviderName(): string;
25
25
 
26
- publish(
27
- topics: string[] | string,
28
- msg: any,
29
- options?: ProvidertOptions
30
- ): void;
26
+ publish(topics: string[] | string, msg: any, options?: ProviderOptions): void;
31
27
 
32
28
  subscribe(
33
29
  topics: string[] | string,
34
- options?: ProvidertOptions
30
+ options?: ProviderOptions
35
31
  ): Observable<any>;
36
32
  }
@@ -14,6 +14,11 @@ export interface PubSubOptions {
14
14
  [key: string]: any;
15
15
  }
16
16
 
17
- export interface ProvidertOptions {
17
+ export interface ProviderOptions {
18
18
  [key: string]: any;
19
19
  }
20
+
21
+ /**
22
+ * @deprecated Migrated to ProviderOptions
23
+ */
24
+ export type ProvidertOptions = ProviderOptions;