@croct/sdk 0.16.2 → 0.17.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/cache/cookieCache.d.ts +1 -1
- package/cache/cookieCache.js +12 -2
- package/cache/cookieCache.js.map +1 -1
- package/channel/channel.d.ts +7 -0
- package/channel/channel.js +23 -0
- package/channel/channel.js.map +1 -1
- package/channel/guaranteedChannel.js +4 -3
- package/channel/guaranteedChannel.js.map +1 -1
- package/channel/httpBeaconChannel.d.ts +23 -0
- package/channel/httpBeaconChannel.js +89 -0
- package/channel/httpBeaconChannel.js.map +1 -0
- package/channel/index.d.ts +1 -2
- package/channel/index.js +3 -5
- package/channel/index.js.map +1 -1
- package/channel/queuedChannel.js +5 -4
- package/channel/queuedChannel.js.map +1 -1
- package/channel/retryChannel.js +8 -4
- package/channel/retryChannel.js.map +1 -1
- package/channel/sandboxChannel.js +4 -0
- package/channel/sandboxChannel.js.map +1 -1
- package/constants.d.ts +2 -2
- package/constants.js +1 -1
- package/container.d.ts +3 -0
- package/container.js +15 -14
- package/container.js.map +1 -1
- package/contentFetcher.d.ts +3 -4
- package/contentFetcher.js +37 -27
- package/contentFetcher.js.map +1 -1
- package/evaluator.d.ts +2 -4
- package/evaluator.js +47 -39
- package/evaluator.js.map +1 -1
- package/facade/contentFetcherFacade.d.ts +1 -6
- package/facade/contentFetcherFacade.js +2 -5
- package/facade/contentFetcherFacade.js.map +1 -1
- package/facade/sdkFacade.d.ts +3 -6
- package/facade/sdkFacade.js +3 -7
- package/facade/sdkFacade.js.map +1 -1
- package/help.d.ts +3 -0
- package/help.js +26 -0
- package/help.js.map +1 -0
- package/package.json +1 -3
- package/queue/persistentQueue.d.ts +1 -3
- package/queue/persistentQueue.js +9 -16
- package/queue/persistentQueue.js.map +1 -1
- package/schema/eventSchemas.js +0 -16
- package/schema/eventSchemas.js.map +1 -1
- package/schema/sdkFacadeSchemas.js +5 -1
- package/schema/sdkFacadeSchemas.js.map +1 -1
- package/schema/sdkSchemas.js +7 -0
- package/schema/sdkSchemas.js.map +1 -1
- package/sdk.d.ts +2 -0
- package/sdk.js +1 -2
- package/sdk.js.map +1 -1
- package/src/cache/cookieCache.ts +13 -2
- package/src/channel/channel.ts +32 -0
- package/src/channel/guaranteedChannel.ts +4 -4
- package/src/channel/httpBeaconChannel.ts +128 -0
- package/src/channel/index.ts +1 -2
- package/src/channel/queuedChannel.ts +5 -5
- package/src/channel/retryChannel.ts +9 -5
- package/src/channel/sandboxChannel.ts +5 -1
- package/src/container.ts +17 -19
- package/src/contentFetcher.ts +41 -29
- package/src/evaluator.ts +74 -65
- package/src/facade/contentFetcherFacade.ts +2 -11
- package/src/facade/sdkFacade.ts +5 -14
- package/src/help.ts +24 -0
- package/src/queue/persistentQueue.ts +11 -22
- package/src/schema/eventSchemas.ts +0 -16
- package/src/schema/sdkFacadeSchemas.ts +14 -2
- package/src/schema/sdkSchemas.ts +7 -0
- package/src/sdk.ts +3 -2
- package/src/trackingEvents.ts +0 -4
- package/trackingEvents.d.ts +0 -4
- package/trackingEvents.js.map +1 -1
- package/channel/beaconSocketChannel.d.ts +0 -37
- package/channel/beaconSocketChannel.js +0 -83
- package/channel/beaconSocketChannel.js.map +0 -1
- package/channel/socketChannel.d.ts +0 -31
- package/channel/socketChannel.js +0 -145
- package/channel/socketChannel.js.map +0 -1
- package/src/channel/beaconSocketChannel.ts +0 -153
- package/src/channel/socketChannel.ts +0 -217
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
import {Logger, NullLogger} from '../logging';
|
|
2
|
-
import {ChannelListener, DuplexChannel} from './channel';
|
|
3
|
-
import {formatCause} from '../error';
|
|
4
|
-
|
|
5
|
-
type Input = string | ArrayBufferLike | Blob | ArrayBufferView;
|
|
6
|
-
type Output = string | ArrayBuffer | Blob;
|
|
7
|
-
|
|
8
|
-
type Options = {
|
|
9
|
-
closeTimeout: number,
|
|
10
|
-
connectionTimeout: number,
|
|
11
|
-
protocols: string | string[],
|
|
12
|
-
binaryType?: BinaryType,
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type Configuration = Partial<Options> & {
|
|
16
|
-
url: string,
|
|
17
|
-
logger?: Logger,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export class SocketChannel<I extends Input, O extends Output> implements DuplexChannel<I, O> {
|
|
21
|
-
private readonly url: string;
|
|
22
|
-
|
|
23
|
-
private readonly logger: Logger;
|
|
24
|
-
|
|
25
|
-
private readonly options: Options;
|
|
26
|
-
|
|
27
|
-
private readonly listeners: Array<ChannelListener<I>> = [];
|
|
28
|
-
|
|
29
|
-
private connection?: Promise<WebSocket>;
|
|
30
|
-
|
|
31
|
-
private closed = false;
|
|
32
|
-
|
|
33
|
-
public constructor({url, logger, ...options}: Configuration) {
|
|
34
|
-
this.url = url;
|
|
35
|
-
this.logger = logger ?? new NullLogger();
|
|
36
|
-
this.options = {
|
|
37
|
-
...options,
|
|
38
|
-
closeTimeout: options.closeTimeout ?? 5000,
|
|
39
|
-
connectionTimeout: options.connectionTimeout ?? 10000,
|
|
40
|
-
protocols: options.protocols ?? [],
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public get connected(): Promise<boolean> {
|
|
45
|
-
if (this.connection === undefined) {
|
|
46
|
-
return Promise.resolve(false);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return this.connection.then(() => true, () => false);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public publish(message: O): Promise<void> {
|
|
53
|
-
return this.connect().then(socket => {
|
|
54
|
-
socket.send(message);
|
|
55
|
-
|
|
56
|
-
this.logger.debug('Message sent.');
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public subscribe(listener: ChannelListener<I>): void {
|
|
61
|
-
if (!this.listeners.includes(listener)) {
|
|
62
|
-
this.listeners.push(listener);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public unsubscribe(listener: ChannelListener<I>): void {
|
|
67
|
-
const index = this.listeners.indexOf(listener);
|
|
68
|
-
|
|
69
|
-
if (index >= 0) {
|
|
70
|
-
this.listeners.splice(index, 1);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private notify(message: I): void {
|
|
75
|
-
this.listeners.forEach(dispatch => dispatch(message));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private connect(): Promise<WebSocket> {
|
|
79
|
-
if (this.closed) {
|
|
80
|
-
return Promise.reject(new Error('Channel has been closed.'));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (this.connection !== undefined) {
|
|
84
|
-
return this.connection
|
|
85
|
-
.then(connection => {
|
|
86
|
-
const state = connection.readyState;
|
|
87
|
-
|
|
88
|
-
if (state === WebSocket.OPEN) {
|
|
89
|
-
return connection;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
throw new Error('Connection lost.');
|
|
93
|
-
})
|
|
94
|
-
.catch(() => {
|
|
95
|
-
// Reconnect
|
|
96
|
-
delete this.connection;
|
|
97
|
-
|
|
98
|
-
return this.connect();
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
this.connection = new Promise((resolve, reject): void => {
|
|
103
|
-
this.logger.debug('Connecting...');
|
|
104
|
-
|
|
105
|
-
const connection = new window.WebSocket(this.url, this.options.protocols);
|
|
106
|
-
|
|
107
|
-
if (this.options.binaryType !== undefined) {
|
|
108
|
-
connection.binaryType = this.options.binaryType;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const abortListener = (): void => {
|
|
112
|
-
const reason = 'Maximum connection timeout reached.';
|
|
113
|
-
|
|
114
|
-
this.logger.error(reason);
|
|
115
|
-
|
|
116
|
-
reject(new Error(reason));
|
|
117
|
-
|
|
118
|
-
connection.close(1000, reason);
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const abortTimer: number = window.setTimeout(abortListener, this.options.connectionTimeout);
|
|
122
|
-
|
|
123
|
-
const openListener = (): void => {
|
|
124
|
-
window.clearTimeout(abortTimer);
|
|
125
|
-
|
|
126
|
-
this.logger.info('Connection established.');
|
|
127
|
-
|
|
128
|
-
connection.removeEventListener('open', openListener);
|
|
129
|
-
|
|
130
|
-
resolve(connection);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const errorListener = (): void => {
|
|
134
|
-
if (!this.closed) {
|
|
135
|
-
this.logger.error('Connection error.');
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
const messageListener = (event: MessageEvent): void => {
|
|
140
|
-
this.logger.debug('Message received.');
|
|
141
|
-
|
|
142
|
-
this.notify(event.data);
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
const closeListener = (event: CloseEvent): void => {
|
|
146
|
-
window.clearTimeout(abortTimer);
|
|
147
|
-
|
|
148
|
-
const reason = `${formatCause(event.reason ?? 'unknown')} (code ${event.code})`;
|
|
149
|
-
const message = `Connection has been closed, reason: ${reason}`;
|
|
150
|
-
|
|
151
|
-
if (!this.closed) {
|
|
152
|
-
this.logger.info(message);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
connection.removeEventListener('open', openListener);
|
|
156
|
-
connection.removeEventListener('error', errorListener);
|
|
157
|
-
connection.removeEventListener('close', closeListener);
|
|
158
|
-
connection.removeEventListener('message', messageListener);
|
|
159
|
-
|
|
160
|
-
reject(new Error(message));
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
connection.addEventListener('open', openListener, {once: true});
|
|
164
|
-
connection.addEventListener('close', closeListener, {once: true});
|
|
165
|
-
connection.addEventListener('error', errorListener);
|
|
166
|
-
connection.addEventListener('message', messageListener);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
return this.connection;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
public close(): Promise<void> {
|
|
173
|
-
this.logger.debug('Closing connection...');
|
|
174
|
-
|
|
175
|
-
return new Promise((resolve, reject): void => {
|
|
176
|
-
this.closed = true;
|
|
177
|
-
|
|
178
|
-
if (this.connection === undefined) {
|
|
179
|
-
this.logger.debug('Connection is not open.');
|
|
180
|
-
|
|
181
|
-
resolve();
|
|
182
|
-
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
this.connection.then(
|
|
187
|
-
(connection): void => {
|
|
188
|
-
let abortTimer: number | undefined;
|
|
189
|
-
|
|
190
|
-
const abort = (): void => {
|
|
191
|
-
this.logger.warn('Connection could not be closed within the timeout period.');
|
|
192
|
-
|
|
193
|
-
reject(new Error('Maximum close timeout reached.'));
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
const close = (): void => {
|
|
197
|
-
window.clearTimeout(abortTimer);
|
|
198
|
-
|
|
199
|
-
this.logger.info('Connection gracefully closed.');
|
|
200
|
-
|
|
201
|
-
resolve();
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
connection.addEventListener('close', close, {once: true});
|
|
205
|
-
connection.close(1000, 'Deliberate disconnection.');
|
|
206
|
-
|
|
207
|
-
abortTimer = window.setTimeout(abort, this.options.closeTimeout);
|
|
208
|
-
},
|
|
209
|
-
() => {
|
|
210
|
-
this.logger.info('Connection closed.');
|
|
211
|
-
|
|
212
|
-
resolve();
|
|
213
|
-
},
|
|
214
|
-
);
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
}
|