-
Notifications
You must be signed in to change notification settings - Fork 2
/
Subscription.ts
236 lines (208 loc) · 6.28 KB
/
Subscription.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { EventEmitter } from 'events';
import * as PubNub from 'pubnub';
import RestClient, { BASE_URL, API_VERSION } from './RestClient';
/**
* There are 3 ways to subscribe for notifications: by event filters; by subscription id; by subscription data.
*/
export default class Subscription extends EventEmitter {
rest: RestClient;
id: string;
eventFilters: string[]; // Without REST base url, can be set by user
// Readonly Subscription data
expirationTime: number; // Epoch time in ms.
subscribeKey: string;
address: string; // PubNub channels
encryptionKey: string; // AES encryptionKey
refreshTimer: NodeJS.Timer;
retryInterval = 5000;
pubnub: any;
debug: boolean;
private reqId = 0;
constructor(restClient: RestClient, opts?: { debug?: boolean }) {
super();
this.rest = restClient;
opts = opts || {};
this.debug = opts.debug;
}
/**
* expiresIn Optional. Subscription lifetime in seconds. Max value is 7 days (604800 sec)
* The 'expiresIn' is not supported.
*
* Errors migh occur:
* errorCode: 'SUB-505',
* message: 'Subscriptions limit exceeded'
*/
async subscribe(filters?: string[]) {
if (filters) {
this.eventFilters = filters;
}
let { eventFilters, id } = this;
if (!eventFilters && !id) {
throw new Error('Event filter or subscription id is needed.');
}
if (eventFilters && id) {
await this.refresh();
return;
}
this.reqId++;
let reqId = this.reqId;
let res: Response;
if (eventFilters) {
res = await this.rest.post('/subscription', { eventFilters: prefixFilters(eventFilters), deliveryMode });
} else {
res = await this.rest.get('/subscription/' + id);
}
let subscription = await res.json();
this.setData(subscription, reqId);
}
/**
* Set subscription data from referesh, newly created or get by id.
*/
setData(subscription, reqId?: number) { // This functions is the only place to parse subscription data.
if (reqId && reqId < this.reqId) {
// Subscription is canceled
// The request is too slow that former request arrived early than this one, so should ignore this response
return;
}
if (subscription.id !== this.id || !this.subscribeKey) {
this.id = subscription.id;
this.subscribeKey = subscription.deliveryMode.subscriberKey;
this.address = subscription.deliveryMode.address;
this.encryptionKey = subscription.deliveryMode.encryptionKey;
this.connectPushServer();
}
this.expirationTime = Date.parse(subscription.expirationTime);
this.eventFilters = unprefixFilters(subscription.eventFilters);
let timeout = Math.max(this.expirationTime - Date.now() - refreshHandicap, 1000);
this.scheduleRefresh(timeout);
}
async cancel() {
this.clearRefreshTimer();
this.disconnectPushServer();
const id = this.id;
this.subscriptionDeleted();
this.reqId++;
await this.rest.delete('/subscription/' + id);
}
onMessage(listener: (...args: any[]) => void) {
this.on(EventMessage, listener);
}
private clearRefreshTimer() {
if (this.refreshTimer) {
clearTimeout(this.refreshTimer);
this.refreshTimer = null;
}
}
/**
* Subscription deleted from the RC Platform
*/
private subscriptionDeleted() {
this.id = null;
this.address = null;
this.encryptionKey = null;
this.expirationTime = 0;
this.subscribeKey = null;
}
private connectPushServer() {
this.disconnectPushServer();
let pubnub = new PubNub({ subscribeKey: this.subscribeKey, restore: true, ssl: true, logVerbosity: this.debug });
// Wrong address pubnub won't report error.
pubnub.subscribe({ channels: [this.address] });
let message = msg => {
let decrypted = pubnub.decrypt(msg.message, this.encryptionKey, {
encryptKey: false,
keyEncoding: 'base64',
keyLength: 128,
mode: 'ecb'
});
// TODO Filter out duplicated notifications by uuid.
this.emit(EventMessage, decrypted);
};
let status = status => {
/*
Subscribe success event:
{ category: 'PNConnectedCategory',
operation: 'PNSubscribeOperation'... }
Subscribe fail event:
{ error: true,
operation: 'PNSubscribeOperation',
category: 'PNBadRequestCategory'... }
*/
if (status.error) {
let e = new Error('PubNub error status, category: ' + status.category);
e['detail'] = status;
this.emit(EventStatusError, e);
// pubnub.reconnect();
} else {
this.emit(EventStatus, status);
}
};
pubnub.addListener({
message,
status
});
this.pubnub = pubnub;
}
private disconnectPushServer() {
if (!this.pubnub) {
return;
}
this.pubnub.removeAllListeners();
this.pubnub.destroy();
this.pubnub = null;
}
private scheduleRefresh(timeout: number) {
this.clearRefreshTimer();
this.refreshTimer = setTimeout(async () => {
this.refreshTimer = null;
this.refresh().catch(e => {
e.message = `Subscription refresh failed, will retry in ${this.retryInterval}ms. Cause: ${e.message}`;
this.emit(EventRefreshError, e);
this.scheduleRefresh(this.retryInterval);
});
}, timeout);
}
private async refresh() {
if (Date.now() >= this.expirationTime) {
this.id = null;
await this.subscribe();
return;
}
let subscription;
this.reqId++;
let reqId = this.reqId;
try {
let res = await this.rest.put('/subscription/' + this.id, { eventFilters: prefixFilters(this.eventFilters), deliveryMode });
subscription = await res.json();
} catch (e) {
if (reqId < this.reqId) return;
if (e.code === ErrorNotFound) { // The subscription is invalidated, resubscribe
this.id = null;
await this.subscribe(this.eventFilters);
return;
}
throw e;
}
this.setData(subscription, reqId);
this.emit(EventRefreshSuccess);
}
}
// Prefix with REST base url
function prefixFilters(filters: string[]) {
return filters.map(f => BASE_URL + API_VERSION + f);
}
function unprefixFilters(filters: string[]) {
return filters.map(f => f.replace(BASE_URL + API_VERSION, ''));
}
const deliveryMode = { transportType: 'PubNub', encryption: true };
// In ms
const refreshHandicap = 10 * 1000;
// In seconds
// export const MAX_LIFETIME = 604800;
const ErrorNotFound = 'CMN-102';
// The list of events the subscription may emit.
const EventMessage = 'message';
const EventStatusError = 'StatusError'; // PubNub status event
const EventStatus = 'status'; // PubNub status event
const EventRefreshSuccess = 'RefreshSuccess';
const EventRefreshError = 'RefreshError';