aws-appsync-subscription-link 3.1.3 → 4.0.0-next.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.
@@ -1,25 +0,0 @@
1
- /*!
2
- * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- import { ApolloLink, Observable, Operation, FetchResult } from "@apollo/client/core";
6
- import * as ZenObservable from 'zen-observable-ts';
7
- declare type MqttConnectionInfo = {
8
- client: string;
9
- url: string;
10
- topics: string[];
11
- };
12
- export declare const CONTROL_EVENTS_KEY = "@@controlEvents";
13
- export declare class SubscriptionHandshakeLink extends ApolloLink {
14
- private subsInfoContextKey;
15
- private topicObservers;
16
- private clientObservers;
17
- constructor(subsInfoContextKey: any);
18
- request(operation: Operation): Observable<FetchResult> | null;
19
- connectNewClients(connectionInfo: MqttConnectionInfo[], observer: ZenObservable.Observer<FetchResult>, operation: Operation): Promise<any[]>;
20
- connectNewClient(connectionInfo: MqttConnectionInfo, observer: ZenObservable.Observer<FetchResult>, selectionNames: string[]): Promise<any>;
21
- subscribeToTopics<T>(client: any, topics: string[], observer: ZenObservable.Observer<T>): Promise<unknown[]>;
22
- subscribeToTopic<T>(client: any, topic: string, observer: ZenObservable.Observer<T>): Promise<unknown>;
23
- onMessage: (topic: string, message: string, selectionNames: string[]) => void;
24
- }
25
- export {};
@@ -1,272 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
- var __assign = (this && this.__assign) || function () {
16
- __assign = Object.assign || function(t) {
17
- for (var s, i = 1, n = arguments.length; i < n; i++) {
18
- s = arguments[i];
19
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
- t[p] = s[p];
21
- }
22
- return t;
23
- };
24
- return __assign.apply(this, arguments);
25
- };
26
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
- return new (P || (P = Promise))(function (resolve, reject) {
29
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
- step((generator = generator.apply(thisArg, _arguments || [])).next());
33
- });
34
- };
35
- var __generator = (this && this.__generator) || function (thisArg, body) {
36
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
37
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
38
- function verb(n) { return function (v) { return step([n, v]); }; }
39
- function step(op) {
40
- if (f) throw new TypeError("Generator is already executing.");
41
- while (_) try {
42
- 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;
43
- if (y = 0, t) op = [op[0] & 2, t.value];
44
- switch (op[0]) {
45
- case 0: case 1: t = op; break;
46
- case 4: _.label++; return { value: op[1], done: false };
47
- case 5: _.label++; y = op[1]; op = [0]; continue;
48
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
49
- default:
50
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
51
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
52
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
53
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
54
- if (t[2]) _.ops.pop();
55
- _.trys.pop(); continue;
56
- }
57
- op = body.call(thisArg, _);
58
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
59
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
60
- }
61
- };
62
- var __rest = (this && this.__rest) || function (s, e) {
63
- var t = {};
64
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
65
- t[p] = s[p];
66
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
67
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
68
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
69
- t[p[i]] = s[p[i]];
70
- }
71
- return t;
72
- };
73
- Object.defineProperty(exports, "__esModule", { value: true });
74
- /*!
75
- * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
76
- * SPDX-License-Identifier: Apache-2.0
77
- */
78
- var core_1 = require("@apollo/client/core");
79
- var utils_1 = require("./utils");
80
- var Paho = require("./vendor/paho-mqtt");
81
- var utilities_1 = require("@apollo/client/utilities");
82
- var logger = utils_1.rootLogger.extend('subscriptions');
83
- var mqttLogger = logger.extend('mqtt');
84
- exports.CONTROL_EVENTS_KEY = '@@controlEvents';
85
- var SubscriptionHandshakeLink = /** @class */ (function (_super) {
86
- __extends(SubscriptionHandshakeLink, _super);
87
- function SubscriptionHandshakeLink(subsInfoContextKey) {
88
- var _this = _super.call(this) || this;
89
- _this.topicObservers = new Map();
90
- _this.clientObservers = new Map();
91
- _this.onMessage = function (topic, message, selectionNames) {
92
- var parsedMessage = JSON.parse(message);
93
- var observers = _this.topicObservers.get(topic);
94
- var data = selectionNames.reduce(function (acc, name) { return (acc[name] = acc[name] || null, acc); }, parsedMessage.data || {});
95
- logger('Message received', { data: data, topic: topic, observers: observers });
96
- observers.forEach(function (observer) {
97
- try {
98
- observer.next(__assign(__assign({}, parsedMessage), { data: data }));
99
- }
100
- catch (err) {
101
- logger(err);
102
- }
103
- });
104
- };
105
- _this.subsInfoContextKey = subsInfoContextKey;
106
- return _this;
107
- }
108
- SubscriptionHandshakeLink.prototype.request = function (operation) {
109
- var _a;
110
- var _this = this;
111
- var _b = operation.getContext(), _c = this.subsInfoContextKey, subsInfo = _b[_c], _d = _b.controlMessages, _e = exports.CONTROL_EVENTS_KEY, controlEvents = (_d === void 0 ? (_a = {}, _a[exports.CONTROL_EVENTS_KEY] = undefined, _a) : _d)[_e];
112
- var _f = subsInfo.extensions, _g = (_f === void 0 ? { subscription: { newSubscriptions: {}, mqttConnections: [] } } : _f).subscription, newSubscriptions = _g.newSubscriptions, mqttConnections = _g.mqttConnections, _h = subsInfo.errors, errors = _h === void 0 ? [] : _h;
113
- if (errors && errors.length) {
114
- return new core_1.Observable(function (observer) {
115
- observer.error(new core_1.ApolloError({
116
- errorMessage: 'Error during subscription handshake',
117
- extraInfo: { errors: errors },
118
- graphQLErrors: errors
119
- }));
120
- return function () { };
121
- });
122
- }
123
- var newSubscriptionTopics = Object.keys(newSubscriptions).map(function (subKey) { return newSubscriptions[subKey].topic; });
124
- var existingTopicsWithObserver = new Set(newSubscriptionTopics.filter(function (t) { return _this.topicObservers.has(t); }));
125
- var newTopics = new Set(newSubscriptionTopics.filter(function (t) { return !existingTopicsWithObserver.has(t); }));
126
- return new core_1.Observable(function (observer) {
127
- existingTopicsWithObserver.forEach(function (t) {
128
- _this.topicObservers.get(t).add(observer);
129
- var anObserver = Array.from(_this.topicObservers.get(t)).find(function () { return true; });
130
- var clientId = Array.from(_this.clientObservers).find(function (_a) {
131
- var observers = _a[1].observers;
132
- return observers.has(anObserver);
133
- })[0];
134
- _this.clientObservers.get(clientId).observers.add(observer);
135
- });
136
- var newTopicsConnectionInfo = mqttConnections
137
- .filter(function (c) { return c.topics.some(function (t) { return newTopics.has(t); }); })
138
- .map(function (_a) {
139
- var topics = _a.topics, rest = __rest(_a, ["topics"]);
140
- return (__assign(__assign({}, rest), { topics: topics.filter(function (t) { return newTopics.has(t); }) }));
141
- });
142
- _this.connectNewClients(newTopicsConnectionInfo, observer, operation);
143
- return function () {
144
- var clientsForCurrentObserver = Array.from(_this.clientObservers).filter(function (_a) {
145
- var observers = _a[1].observers;
146
- return observers.has(observer);
147
- });
148
- clientsForCurrentObserver.forEach(function (_a) {
149
- var clientId = _a[0];
150
- return _this.clientObservers.get(clientId).observers.delete(observer);
151
- });
152
- _this.clientObservers.forEach(function (_a) {
153
- var observers = _a.observers, client = _a.client;
154
- if (observers.size === 0) {
155
- if (client.isConnected()) {
156
- client.disconnect();
157
- }
158
- _this.clientObservers.delete(client.clientId);
159
- }
160
- });
161
- _this.clientObservers = new Map(Array.from(_this.clientObservers).filter(function (_a) {
162
- var observers = _a[1].observers;
163
- return observers.size > 0;
164
- }));
165
- _this.topicObservers.forEach(function (observers) { return observers.delete(observer); });
166
- _this.topicObservers = new Map(Array.from(_this.topicObservers)
167
- .filter(function (_a) {
168
- var observers = _a[1];
169
- return observers.size > 0;
170
- }));
171
- };
172
- }).filter(function (data) {
173
- var _a = data.extensions, _b = (_a === void 0 ? {} : _a).controlMsgType, controlMsgType = _b === void 0 ? undefined : _b;
174
- var isControlMsg = typeof controlMsgType !== 'undefined';
175
- return controlEvents === true || !isControlMsg;
176
- });
177
- };
178
- SubscriptionHandshakeLink.prototype.connectNewClients = function (connectionInfo, observer, operation) {
179
- return __awaiter(this, void 0, void 0, function () {
180
- var query, selectionNames, result, data;
181
- var _this = this;
182
- return __generator(this, function (_a) {
183
- query = operation.query;
184
- selectionNames = utilities_1.getMainDefinition(query).selectionSet.selections.map(function (_a) {
185
- var value = _a.name.value;
186
- return value;
187
- });
188
- result = Promise.all(connectionInfo.map(function (c) { return _this.connectNewClient(c, observer, selectionNames); }));
189
- data = selectionNames.reduce(function (acc, name) { return (acc[name] = acc[name] || null, acc); }, {});
190
- observer.next({
191
- data: data,
192
- extensions: {
193
- controlMsgType: 'CONNECTED',
194
- controlMsgInfo: {
195
- connectionInfo: connectionInfo,
196
- },
197
- }
198
- });
199
- return [2 /*return*/, result];
200
- });
201
- });
202
- };
203
- ;
204
- SubscriptionHandshakeLink.prototype.connectNewClient = function (connectionInfo, observer, selectionNames) {
205
- return __awaiter(this, void 0, void 0, function () {
206
- var clientId, url, topics, client;
207
- var _this = this;
208
- return __generator(this, function (_a) {
209
- switch (_a.label) {
210
- case 0:
211
- clientId = connectionInfo.client, url = connectionInfo.url, topics = connectionInfo.topics;
212
- client = new Paho.Client(url, clientId);
213
- client.trace = mqttLogger.bind(null, clientId);
214
- client.onConnectionLost = function (_a) {
215
- var errorCode = _a.errorCode, args = __rest(_a, ["errorCode"]);
216
- if (errorCode !== 0) {
217
- topics.forEach(function (t) {
218
- if (_this.topicObservers.has(t)) {
219
- _this.topicObservers.get(t).forEach(function (observer) { return observer.error(__assign(__assign({}, args), { permanent: true })); });
220
- }
221
- });
222
- }
223
- topics.forEach(function (t) { return _this.topicObservers.delete(t); });
224
- };
225
- client.onMessageArrived = function (_a) {
226
- var destinationName = _a.destinationName, payloadString = _a.payloadString;
227
- return _this.onMessage(destinationName, payloadString, selectionNames);
228
- };
229
- return [4 /*yield*/, new Promise(function (resolve, reject) {
230
- client.connect({
231
- useSSL: url.indexOf('wss://') === 0,
232
- mqttVersion: 3,
233
- onSuccess: function () { return resolve(client); },
234
- onFailure: reject,
235
- });
236
- })];
237
- case 1:
238
- _a.sent();
239
- return [4 /*yield*/, this.subscribeToTopics(client, topics, observer)];
240
- case 2:
241
- _a.sent();
242
- return [2 /*return*/, client];
243
- }
244
- });
245
- });
246
- };
247
- SubscriptionHandshakeLink.prototype.subscribeToTopics = function (client, topics, observer) {
248
- var _this = this;
249
- return Promise.all(topics.map(function (topic) { return _this.subscribeToTopic(client, topic, observer); }));
250
- };
251
- SubscriptionHandshakeLink.prototype.subscribeToTopic = function (client, topic, observer) {
252
- var _this = this;
253
- return new Promise(function (resolve, reject) {
254
- client.subscribe(topic, {
255
- onSuccess: function () {
256
- if (!_this.topicObservers.has(topic)) {
257
- _this.topicObservers.set(topic, new Set());
258
- }
259
- if (!_this.clientObservers.has(client.clientId)) {
260
- _this.clientObservers.set(client.clientId, { client: client, observers: new Set() });
261
- }
262
- _this.topicObservers.get(topic).add(observer);
263
- _this.clientObservers.get(client.clientId).observers.add(observer);
264
- resolve(topic);
265
- },
266
- onFailure: reject,
267
- });
268
- });
269
- };
270
- return SubscriptionHandshakeLink;
271
- }(core_1.ApolloLink));
272
- exports.SubscriptionHandshakeLink = SubscriptionHandshakeLink;
@@ -1,87 +0,0 @@
1
- import { AuthOptions } from "aws-appsync-auth-link";
2
- import * as ZenObservable from 'zen-observable-ts';
3
- export declare enum SUBSCRIPTION_STATUS {
4
- PENDING = 0,
5
- CONNECTED = 1,
6
- FAILED = 2
7
- }
8
- export declare enum SOCKET_STATUS {
9
- CLOSED = 0,
10
- READY = 1,
11
- CONNECTING = 2
12
- }
13
- export declare enum MESSAGE_TYPES {
14
- /**
15
- * Client -> Server message.
16
- * This message type is the first message after handshake and this will initialize AWS AppSync RealTime communication
17
- */
18
- GQL_CONNECTION_INIT = "connection_init",
19
- /**
20
- * Server -> Client message
21
- * This message type is in case there is an issue with AWS AppSync RealTime when establishing connection
22
- */
23
- GQL_CONNECTION_ERROR = "connection_error",
24
- /**
25
- * Server -> Client message.
26
- * This message type is for the ack response from AWS AppSync RealTime for GQL_CONNECTION_INIT message
27
- */
28
- GQL_CONNECTION_ACK = "connection_ack",
29
- /**
30
- * Client -> Server message.
31
- * This message type is for register subscriptions with AWS AppSync RealTime
32
- */
33
- GQL_START = "start",
34
- /**
35
- * Server -> Client message.
36
- * This message type is for the ack response from AWS AppSync RealTime for GQL_START message
37
- */
38
- GQL_START_ACK = "start_ack",
39
- /**
40
- * Server -> Client message.
41
- * This message type is for subscription message from AWS AppSync RealTime
42
- */
43
- GQL_DATA = "data",
44
- /**
45
- * Server -> Client message.
46
- * This message type helps the client to know is still receiving messages from AWS AppSync RealTime
47
- */
48
- GQL_CONNECTION_KEEP_ALIVE = "ka",
49
- /**
50
- * Client -> Server message.
51
- * This message type is for unregister subscriptions with AWS AppSync RealTime
52
- */
53
- GQL_STOP = "stop",
54
- /**
55
- * Server -> Client message.
56
- * This message type is for the ack response from AWS AppSync RealTime for GQL_STOP message
57
- */
58
- GQL_COMPLETE = "complete",
59
- /**
60
- * Server -> Client message.
61
- * This message type is for sending error messages from AWS AppSync RealTime to the client
62
- */
63
- GQL_ERROR = "error"
64
- }
65
- export declare enum CONTROL_MSG {
66
- CONNECTION_CLOSED = "Connection closed",
67
- TIMEOUT_DISCONNECT = "Timeout disconnect",
68
- SUBSCRIPTION_ACK = "Subscription ack"
69
- }
70
- export declare type UrlInfo = {
71
- url: string;
72
- auth: AuthOptions;
73
- region: string;
74
- };
75
- export declare type AppSyncRealTimeSubscriptionConfig = UrlInfo & {
76
- keepAliveTimeoutMs?: number;
77
- };
78
- export declare type ObserverQuery = {
79
- observer: ZenObservable.SubscriptionObserver<any>;
80
- query: string;
81
- variables: object;
82
- subscriptionState: SUBSCRIPTION_STATUS;
83
- subscriptionReadyCallback?: Function;
84
- subscriptionFailedCallback?: Function;
85
- startAckTimeoutId?: number;
86
- };
87
- export declare type DelayFunction = (attempt: number, args?: any[], error?: Error) => number | false;
@@ -1,75 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //#region Subscription link enums
4
- var SUBSCRIPTION_STATUS;
5
- (function (SUBSCRIPTION_STATUS) {
6
- SUBSCRIPTION_STATUS[SUBSCRIPTION_STATUS["PENDING"] = 0] = "PENDING";
7
- SUBSCRIPTION_STATUS[SUBSCRIPTION_STATUS["CONNECTED"] = 1] = "CONNECTED";
8
- SUBSCRIPTION_STATUS[SUBSCRIPTION_STATUS["FAILED"] = 2] = "FAILED";
9
- })(SUBSCRIPTION_STATUS = exports.SUBSCRIPTION_STATUS || (exports.SUBSCRIPTION_STATUS = {}));
10
- var SOCKET_STATUS;
11
- (function (SOCKET_STATUS) {
12
- SOCKET_STATUS[SOCKET_STATUS["CLOSED"] = 0] = "CLOSED";
13
- SOCKET_STATUS[SOCKET_STATUS["READY"] = 1] = "READY";
14
- SOCKET_STATUS[SOCKET_STATUS["CONNECTING"] = 2] = "CONNECTING";
15
- })(SOCKET_STATUS = exports.SOCKET_STATUS || (exports.SOCKET_STATUS = {}));
16
- var MESSAGE_TYPES;
17
- (function (MESSAGE_TYPES) {
18
- /**
19
- * Client -> Server message.
20
- * This message type is the first message after handshake and this will initialize AWS AppSync RealTime communication
21
- */
22
- MESSAGE_TYPES["GQL_CONNECTION_INIT"] = "connection_init";
23
- /**
24
- * Server -> Client message
25
- * This message type is in case there is an issue with AWS AppSync RealTime when establishing connection
26
- */
27
- MESSAGE_TYPES["GQL_CONNECTION_ERROR"] = "connection_error";
28
- /**
29
- * Server -> Client message.
30
- * This message type is for the ack response from AWS AppSync RealTime for GQL_CONNECTION_INIT message
31
- */
32
- MESSAGE_TYPES["GQL_CONNECTION_ACK"] = "connection_ack";
33
- /**
34
- * Client -> Server message.
35
- * This message type is for register subscriptions with AWS AppSync RealTime
36
- */
37
- MESSAGE_TYPES["GQL_START"] = "start";
38
- /**
39
- * Server -> Client message.
40
- * This message type is for the ack response from AWS AppSync RealTime for GQL_START message
41
- */
42
- MESSAGE_TYPES["GQL_START_ACK"] = "start_ack";
43
- /**
44
- * Server -> Client message.
45
- * This message type is for subscription message from AWS AppSync RealTime
46
- */
47
- MESSAGE_TYPES["GQL_DATA"] = "data";
48
- /**
49
- * Server -> Client message.
50
- * This message type helps the client to know is still receiving messages from AWS AppSync RealTime
51
- */
52
- MESSAGE_TYPES["GQL_CONNECTION_KEEP_ALIVE"] = "ka";
53
- /**
54
- * Client -> Server message.
55
- * This message type is for unregister subscriptions with AWS AppSync RealTime
56
- */
57
- MESSAGE_TYPES["GQL_STOP"] = "stop";
58
- /**
59
- * Server -> Client message.
60
- * This message type is for the ack response from AWS AppSync RealTime for GQL_STOP message
61
- */
62
- MESSAGE_TYPES["GQL_COMPLETE"] = "complete";
63
- /**
64
- * Server -> Client message.
65
- * This message type is for sending error messages from AWS AppSync RealTime to the client
66
- */
67
- MESSAGE_TYPES["GQL_ERROR"] = "error"; // Server -> Client
68
- })(MESSAGE_TYPES = exports.MESSAGE_TYPES || (exports.MESSAGE_TYPES = {}));
69
- var CONTROL_MSG;
70
- (function (CONTROL_MSG) {
71
- CONTROL_MSG["CONNECTION_CLOSED"] = "Connection closed";
72
- CONTROL_MSG["TIMEOUT_DISCONNECT"] = "Timeout disconnect";
73
- CONTROL_MSG["SUBSCRIPTION_ACK"] = "Subscription ack";
74
- })(CONTROL_MSG = exports.CONTROL_MSG || (exports.CONTROL_MSG = {}));
75
- //#endregion
@@ -1,5 +0,0 @@
1
- /*!
2
- * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- export { default as rootLogger } from './logger';
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /*!
4
- * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
- * SPDX-License-Identifier: Apache-2.0
6
- */
7
- var logger_1 = require("./logger");
8
- exports.rootLogger = logger_1.default;
@@ -1,5 +0,0 @@
1
- export declare type Logger = Function & {
2
- extend(category: string): Logger;
3
- };
4
- declare const debugLogger: Logger;
5
- export default debugLogger;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- var __spreadArrays = (this && this.__spreadArrays) || function () {
3
- for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
4
- for (var r = Array(s), k = 0, i = 0; i < il; i++)
5
- for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
6
- r[k] = a[j];
7
- return r;
8
- };
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- var debug_1 = require("debug");
11
- var debugLogger = debug_1.default('aws-appsync');
12
- var extend = function (category) {
13
- if (category === void 0) { category = ''; }
14
- var newCategory = category ? __spreadArrays(this.namespace.split(':'), [category]).join(':') : this.namespace;
15
- var result = debug_1.default(newCategory);
16
- result.extend = extend.bind(result);
17
- return result;
18
- };
19
- debugLogger.extend = extend.bind(debugLogger);
20
- exports.default = debugLogger;
@@ -1,19 +0,0 @@
1
- import { DelayFunction } from "../types";
2
- /**
3
- * Internal use of Subscription link
4
- * @private
5
- */
6
- export declare class NonRetryableError extends Error {
7
- readonly nonRetryable = true;
8
- constructor(message: string);
9
- }
10
- /**
11
- * @private
12
- * Internal use of Subscription link
13
- */
14
- export declare function retry(functionToRetry: Function, args: any[], delayFn: DelayFunction, attempt?: number): any;
15
- /**
16
- * @private
17
- * Internal use of Subscription link
18
- */
19
- export declare const jitteredExponentialRetry: (functionToRetry: Function, args: any[], maxDelayMs?: number) => any;
@@ -1,130 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
17
- return new (P || (P = Promise))(function (resolve, reject) {
18
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
19
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
20
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
21
- step((generator = generator.apply(thisArg, _arguments || [])).next());
22
- });
23
- };
24
- var __generator = (this && this.__generator) || function (thisArg, body) {
25
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
26
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
27
- function verb(n) { return function (v) { return step([n, v]); }; }
28
- function step(op) {
29
- if (f) throw new TypeError("Generator is already executing.");
30
- while (_) try {
31
- 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;
32
- if (y = 0, t) op = [op[0] & 2, t.value];
33
- switch (op[0]) {
34
- case 0: case 1: t = op; break;
35
- case 4: _.label++; return { value: op[1], done: false };
36
- case 5: _.label++; y = op[1]; op = [0]; continue;
37
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
38
- default:
39
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
40
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
41
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
42
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
43
- if (t[2]) _.ops.pop();
44
- _.trys.pop(); continue;
45
- }
46
- op = body.call(thisArg, _);
47
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
48
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
49
- }
50
- };
51
- Object.defineProperty(exports, "__esModule", { value: true });
52
- var index_1 = require("./index");
53
- var logger = index_1.rootLogger.extend("retry");
54
- var MAX_DELAY_MS = 5000;
55
- /**
56
- * Internal use of Subscription link
57
- * @private
58
- */
59
- var NonRetryableError = /** @class */ (function (_super) {
60
- __extends(NonRetryableError, _super);
61
- function NonRetryableError(message) {
62
- var _this = _super.call(this, message) || this;
63
- _this.nonRetryable = true;
64
- return _this;
65
- }
66
- return NonRetryableError;
67
- }(Error));
68
- exports.NonRetryableError = NonRetryableError;
69
- var isNonRetryableError = function (obj) {
70
- var key = "nonRetryable";
71
- return obj && obj[key];
72
- };
73
- /**
74
- * @private
75
- * Internal use of Subscription link
76
- */
77
- function retry(functionToRetry, args, delayFn, attempt) {
78
- if (attempt === void 0) { attempt = 1; }
79
- return __awaiter(this, void 0, void 0, function () {
80
- var err_1, retryIn_1;
81
- return __generator(this, function (_a) {
82
- switch (_a.label) {
83
- case 0:
84
- logger("Attempt #" + attempt + " for this vars: " + JSON.stringify(args));
85
- _a.label = 1;
86
- case 1:
87
- _a.trys.push([1, 3, , 8]);
88
- return [4 /*yield*/, functionToRetry.apply(undefined, args)];
89
- case 2:
90
- _a.sent();
91
- return [3 /*break*/, 8];
92
- case 3:
93
- err_1 = _a.sent();
94
- logger("error " + err_1);
95
- if (isNonRetryableError(err_1)) {
96
- logger("non retryable error");
97
- throw err_1;
98
- }
99
- retryIn_1 = delayFn(attempt, args, err_1);
100
- logger("retryIn ", retryIn_1);
101
- if (!(retryIn_1 !== false)) return [3 /*break*/, 6];
102
- return [4 /*yield*/, new Promise(function (res) { return setTimeout(res, retryIn_1); })];
103
- case 4:
104
- _a.sent();
105
- return [4 /*yield*/, retry(functionToRetry, args, delayFn, attempt + 1)];
106
- case 5: return [2 /*return*/, _a.sent()];
107
- case 6: throw err_1;
108
- case 7: return [3 /*break*/, 8];
109
- case 8: return [2 /*return*/];
110
- }
111
- });
112
- });
113
- }
114
- exports.retry = retry;
115
- function jitteredBackoff(maxDelayMs) {
116
- var BASE_TIME_MS = 100;
117
- var JITTER_FACTOR = 100;
118
- return function (attempt) {
119
- var delay = Math.pow(2, attempt) * BASE_TIME_MS + JITTER_FACTOR * Math.random();
120
- return delay > maxDelayMs ? false : delay;
121
- };
122
- }
123
- /**
124
- * @private
125
- * Internal use of Subscription link
126
- */
127
- exports.jitteredExponentialRetry = function (functionToRetry, args, maxDelayMs) {
128
- if (maxDelayMs === void 0) { maxDelayMs = MAX_DELAY_MS; }
129
- return retry(functionToRetry, args, jitteredBackoff(maxDelayMs));
130
- };