@croct/sdk 0.11.0-alpha → 0.11.0-alpha.2
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/activeRecord.js +1 -0
- package/activeRecord.js.map +1 -0
- package/base64Url.js +1 -0
- package/base64Url.js.map +1 -0
- package/cache/cache.js +1 -0
- package/cache/cache.js.map +1 -0
- package/cache/fallbackCache.js +1 -0
- package/cache/fallbackCache.js.map +1 -0
- package/cache/inMemoryCache.js +1 -0
- package/cache/inMemoryCache.js.map +1 -0
- package/cache/index.js +1 -0
- package/cache/index.js.map +1 -0
- package/cache/localStorageCache.js +1 -0
- package/cache/localStorageCache.js.map +1 -0
- package/channel/beaconSocketChannel.js +1 -0
- package/channel/beaconSocketChannel.js.map +1 -0
- package/channel/channel.js +1 -0
- package/channel/channel.js.map +1 -0
- package/channel/encodedChannel.js +1 -0
- package/channel/encodedChannel.js.map +1 -0
- package/channel/guaranteedChannel.js +1 -0
- package/channel/guaranteedChannel.js.map +1 -0
- package/channel/index.js +1 -0
- package/channel/index.js.map +1 -0
- package/channel/queuedChannel.js +1 -0
- package/channel/queuedChannel.js.map +1 -0
- package/channel/retryChannel.js +1 -0
- package/channel/retryChannel.js.map +1 -0
- package/channel/sandboxChannel.js +1 -0
- package/channel/sandboxChannel.js.map +1 -0
- package/channel/socketChannel.js +1 -0
- package/channel/socketChannel.js.map +1 -0
- package/cid/assigner.js +1 -0
- package/cid/assigner.js.map +1 -0
- package/cid/cachedAssigner.js +1 -0
- package/cid/cachedAssigner.js.map +1 -0
- package/cid/fixedAssigner.js +1 -0
- package/cid/fixedAssigner.js.map +1 -0
- package/cid/index.js +1 -0
- package/cid/index.js.map +1 -0
- package/cid/remoteAssigner.js +1 -0
- package/cid/remoteAssigner.js.map +1 -0
- package/constants.d.ts +5 -5
- package/constants.js +2 -1
- package/constants.js.map +1 -0
- package/container.js +1 -0
- package/container.js.map +1 -0
- package/contentFetcher.js +1 -0
- package/contentFetcher.js.map +1 -0
- package/context.js +1 -0
- package/context.js.map +1 -0
- package/error.js +1 -0
- package/error.js.map +1 -0
- package/evaluator.js +1 -0
- package/evaluator.js.map +1 -0
- package/eventManager.js +1 -0
- package/eventManager.js.map +1 -0
- package/facade/contentFetcherFacade.js +1 -0
- package/facade/contentFetcherFacade.js.map +1 -0
- package/facade/evaluatorFacade.js +1 -0
- package/facade/evaluatorFacade.js.map +1 -0
- package/facade/index.js +1 -0
- package/facade/index.js.map +1 -0
- package/facade/sdkFacade.js +1 -0
- package/facade/sdkFacade.js.map +1 -0
- package/facade/sessionFacade.js +1 -0
- package/facade/sessionFacade.js.map +1 -0
- package/facade/sessionPatch.js +1 -0
- package/facade/sessionPatch.js.map +1 -0
- package/facade/trackerFacade.js +1 -0
- package/facade/trackerFacade.js.map +1 -0
- package/facade/userFacade.js +1 -0
- package/facade/userFacade.js.map +1 -0
- package/facade/userPatch.js +1 -0
- package/facade/userPatch.js.map +1 -0
- package/index.js +1 -0
- package/index.js.map +1 -0
- package/logging/consoleLogger.js +1 -0
- package/logging/consoleLogger.js.map +1 -0
- package/logging/index.js +1 -0
- package/logging/index.js.map +1 -0
- package/logging/logger.js +1 -0
- package/logging/logger.js.map +1 -0
- package/logging/namespacedLogger.js +1 -0
- package/logging/namespacedLogger.js.map +1 -0
- package/logging/nullLogger.js +1 -0
- package/logging/nullLogger.js.map +1 -0
- package/namespacedStorage.js +1 -0
- package/namespacedStorage.js.map +1 -0
- package/package.json +3 -2
- package/patch.js +1 -0
- package/patch.js.map +1 -0
- package/queue/capacityRestrictedQueue.js +1 -0
- package/queue/capacityRestrictedQueue.js.map +1 -0
- package/queue/inMemoryQueue.js +1 -0
- package/queue/inMemoryQueue.js.map +1 -0
- package/queue/index.js +1 -0
- package/queue/index.js.map +1 -0
- package/queue/monitoredQueue.js +1 -0
- package/queue/monitoredQueue.js.map +1 -0
- package/queue/persistentQueue.js +1 -0
- package/queue/persistentQueue.js.map +1 -0
- package/queue/queue.js +1 -0
- package/queue/queue.js.map +1 -0
- package/retry/arbitraryPolicy.js +1 -0
- package/retry/arbitraryPolicy.js.map +1 -0
- package/retry/backoffPolicy.js +1 -0
- package/retry/backoffPolicy.js.map +1 -0
- package/retry/index.js +1 -0
- package/retry/index.js.map +1 -0
- package/retry/maxAttemptsPolicy.js +1 -0
- package/retry/maxAttemptsPolicy.js.map +1 -0
- package/retry/neverPolicy.js +1 -0
- package/retry/neverPolicy.js.map +1 -0
- package/retry/policy.js +1 -0
- package/retry/policy.js.map +1 -0
- package/schema/attributeSchema.js +1 -0
- package/schema/attributeSchema.js.map +1 -0
- package/schema/contentFetcherSchemas.js +1 -0
- package/schema/contentFetcherSchemas.js.map +1 -0
- package/schema/contentSchemas.js +1 -0
- package/schema/contentSchemas.js.map +1 -0
- package/schema/contextSchemas.js +1 -0
- package/schema/contextSchemas.js.map +1 -0
- package/schema/ecommerceSchemas.js +1 -0
- package/schema/ecommerceSchemas.js.map +1 -0
- package/schema/evaluatorSchemas.js +1 -0
- package/schema/evaluatorSchemas.js.map +1 -0
- package/schema/eventSchemas.js +1 -0
- package/schema/eventSchemas.js.map +1 -0
- package/schema/index.js +1 -0
- package/schema/index.js.map +1 -0
- package/schema/loggerSchema.js +1 -0
- package/schema/loggerSchema.js.map +1 -0
- package/schema/operationSchemas.js +1 -0
- package/schema/operationSchemas.js.map +1 -0
- package/schema/sdkFacadeSchemas.js +1 -0
- package/schema/sdkFacadeSchemas.js.map +1 -0
- package/schema/sdkSchemas.js +1 -0
- package/schema/sdkSchemas.js.map +1 -0
- package/schema/tokenSchema.js +1 -0
- package/schema/tokenSchema.js.map +1 -0
- package/schema/userSchema.js +1 -0
- package/schema/userSchema.js.map +1 -0
- package/sdk.js +1 -0
- package/sdk.js.map +1 -0
- package/sdkEvents.js +1 -0
- package/sdkEvents.js.map +1 -0
- package/sourceLocation.js +1 -0
- package/sourceLocation.js.map +1 -0
- package/src/activeRecord.ts +150 -0
- package/src/base64Url.ts +18 -0
- package/src/cache/cache.ts +15 -0
- package/src/cache/fallbackCache.ts +29 -0
- package/src/cache/inMemoryCache.ts +21 -0
- package/src/cache/index.ts +4 -0
- package/src/cache/localStorageCache.ts +85 -0
- package/src/channel/beaconSocketChannel.ts +153 -0
- package/src/channel/channel.ts +20 -0
- package/src/channel/encodedChannel.ts +21 -0
- package/src/channel/guaranteedChannel.ts +131 -0
- package/src/channel/index.ts +8 -0
- package/src/channel/queuedChannel.ts +112 -0
- package/src/channel/retryChannel.ts +90 -0
- package/src/channel/sandboxChannel.ts +43 -0
- package/src/channel/socketChannel.ts +217 -0
- package/src/cid/assigner.ts +3 -0
- package/src/cid/cachedAssigner.ts +35 -0
- package/src/cid/fixedAssigner.ts +13 -0
- package/src/cid/index.ts +4 -0
- package/src/cid/remoteAssigner.ts +47 -0
- package/src/constants.ts +6 -0
- package/src/container.ts +388 -0
- package/src/contentFetcher.ts +226 -0
- package/src/context.ts +137 -0
- package/src/error.ts +31 -0
- package/src/evaluator.ts +251 -0
- package/src/eventManager.ts +53 -0
- package/src/facade/contentFetcherFacade.ts +69 -0
- package/src/facade/evaluatorFacade.ts +152 -0
- package/src/facade/index.ts +7 -0
- package/src/facade/sdkFacade.ts +291 -0
- package/src/facade/sessionFacade.ts +14 -0
- package/src/facade/sessionPatch.ts +32 -0
- package/src/facade/trackerFacade.ts +98 -0
- package/src/facade/userFacade.ts +26 -0
- package/src/facade/userPatch.ts +32 -0
- package/src/index.ts +4 -0
- package/src/logging/consoleLogger.ts +37 -0
- package/src/logging/index.ts +4 -0
- package/src/logging/logger.ts +13 -0
- package/src/logging/namespacedLogger.ts +32 -0
- package/src/logging/nullLogger.ts +19 -0
- package/src/namespacedStorage.ts +69 -0
- package/src/patch.ts +64 -0
- package/src/queue/capacityRestrictedQueue.ts +44 -0
- package/src/queue/inMemoryQueue.ts +43 -0
- package/src/queue/index.ts +5 -0
- package/src/queue/monitoredQueue.ts +168 -0
- package/src/queue/persistentQueue.ts +84 -0
- package/src/queue/queue.ts +15 -0
- package/src/retry/arbitraryPolicy.ts +21 -0
- package/src/retry/backoffPolicy.ts +84 -0
- package/src/retry/index.ts +5 -0
- package/src/retry/maxAttemptsPolicy.ts +28 -0
- package/src/retry/neverPolicy.ts +11 -0
- package/src/retry/policy.ts +5 -0
- package/src/schema/attributeSchema.ts +6 -0
- package/src/schema/contentFetcherSchemas.ts +23 -0
- package/src/schema/contentSchemas.ts +44 -0
- package/src/schema/contextSchemas.ts +5 -0
- package/src/schema/ecommerceSchemas.ts +179 -0
- package/src/schema/evaluatorSchemas.ts +11 -0
- package/src/schema/eventSchemas.ts +150 -0
- package/src/schema/index.ts +11 -0
- package/src/schema/loggerSchema.ts +12 -0
- package/src/schema/operationSchemas.ts +102 -0
- package/src/schema/sdkFacadeSchemas.ts +44 -0
- package/src/schema/sdkSchemas.ts +49 -0
- package/src/schema/tokenSchema.ts +42 -0
- package/src/schema/userSchema.ts +184 -0
- package/src/sdk.ts +174 -0
- package/src/sdkEvents.ts +15 -0
- package/src/sourceLocation.ts +85 -0
- package/src/tab.ts +148 -0
- package/src/token/cachedTokenStore.ts +34 -0
- package/src/token/inMemoryTokenStore.ts +13 -0
- package/src/token/index.ts +4 -0
- package/src/token/replicatedTokenStore.ts +21 -0
- package/src/token/token.ts +164 -0
- package/src/tracker.ts +460 -0
- package/src/trackingEvents.ts +456 -0
- package/src/transformer.ts +7 -0
- package/src/utilityTypes.ts +3 -0
- package/src/uuid.ts +43 -0
- package/src/validation/arrayType.ts +71 -0
- package/src/validation/booleanType.ts +22 -0
- package/src/validation/functionType.ts +22 -0
- package/src/validation/index.ts +12 -0
- package/src/validation/jsonType.ts +157 -0
- package/src/validation/mixedSchema.ts +7 -0
- package/src/validation/nullType.ts +22 -0
- package/src/validation/numberType.ts +59 -0
- package/src/validation/objectType.ts +138 -0
- package/src/validation/schema.ts +21 -0
- package/src/validation/stringType.ts +118 -0
- package/src/validation/unionType.ts +53 -0
- package/src/validation/violation.ts +23 -0
- package/tab.js +1 -0
- package/tab.js.map +1 -0
- package/token/cachedTokenStore.js +1 -0
- package/token/cachedTokenStore.js.map +1 -0
- package/token/inMemoryTokenStore.js +1 -0
- package/token/inMemoryTokenStore.js.map +1 -0
- package/token/index.js +1 -0
- package/token/index.js.map +1 -0
- package/token/replicatedTokenStore.js +1 -0
- package/token/replicatedTokenStore.js.map +1 -0
- package/token/token.js +1 -0
- package/token/token.js.map +1 -0
- package/tracker.js +1 -0
- package/tracker.js.map +1 -0
- package/trackingEvents.js +1 -0
- package/trackingEvents.js.map +1 -0
- package/transformer.js +1 -0
- package/transformer.js.map +1 -0
- package/utilityTypes.js +1 -0
- package/utilityTypes.js.map +1 -0
- package/uuid.js +1 -0
- package/uuid.js.map +1 -0
- package/validation/arrayType.js +1 -0
- package/validation/arrayType.js.map +1 -0
- package/validation/booleanType.js +1 -0
- package/validation/booleanType.js.map +1 -0
- package/validation/functionType.js +1 -0
- package/validation/functionType.js.map +1 -0
- package/validation/index.js +1 -0
- package/validation/index.js.map +1 -0
- package/validation/jsonType.js +1 -0
- package/validation/jsonType.js.map +1 -0
- package/validation/mixedSchema.js +1 -0
- package/validation/mixedSchema.js.map +1 -0
- package/validation/nullType.js +1 -0
- package/validation/nullType.js.map +1 -0
- package/validation/numberType.js +1 -0
- package/validation/numberType.js.map +1 -0
- package/validation/objectType.js +1 -0
- package/validation/objectType.js.map +1 -0
- package/validation/schema.js +1 -0
- package/validation/schema.js.map +1 -0
- package/validation/stringType.js +1 -0
- package/validation/stringType.js.map +1 -0
- package/validation/unionType.js +1 -0
- package/validation/unionType.js.map +1 -0
- package/validation/violation.js +1 -0
- package/validation/violation.js.map +1 -0
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {Logger, NullLogger} from '../logging';
|
|
2
|
+
import {Cache} from '../cache';
|
|
3
|
+
import {CidAssigner} from './assigner';
|
|
4
|
+
|
|
5
|
+
export class CachedAssigner implements CidAssigner {
|
|
6
|
+
private readonly assigner: CidAssigner;
|
|
7
|
+
|
|
8
|
+
private readonly cache: Cache;
|
|
9
|
+
|
|
10
|
+
private readonly logger: Logger;
|
|
11
|
+
|
|
12
|
+
public constructor(assigner: CidAssigner, cache: Cache, logger?: Logger) {
|
|
13
|
+
this.assigner = assigner;
|
|
14
|
+
this.cache = cache;
|
|
15
|
+
this.logger = logger ?? new NullLogger();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public async assignCid(): Promise<string> {
|
|
19
|
+
const cid = this.cache.get();
|
|
20
|
+
|
|
21
|
+
if (cid !== null) {
|
|
22
|
+
this.logger.debug('Previous CID loaded from cache');
|
|
23
|
+
|
|
24
|
+
return cid;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const newCid = await this.assigner.assignCid();
|
|
28
|
+
|
|
29
|
+
this.cache.put(newCid);
|
|
30
|
+
|
|
31
|
+
this.logger.debug('New CID stored into cache');
|
|
32
|
+
|
|
33
|
+
return newCid;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {CidAssigner} from './assigner';
|
|
2
|
+
|
|
3
|
+
export class FixedAssigner implements CidAssigner {
|
|
4
|
+
private readonly cid: string;
|
|
5
|
+
|
|
6
|
+
public constructor(cid: string) {
|
|
7
|
+
this.cid = cid;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public assignCid(): Promise<string> {
|
|
11
|
+
return Promise.resolve(this.cid);
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/cid/index.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {Logger, NullLogger} from '../logging';
|
|
2
|
+
import {formatCause} from '../error';
|
|
3
|
+
import {CidAssigner} from './assigner';
|
|
4
|
+
|
|
5
|
+
export class RemoteAssigner implements CidAssigner {
|
|
6
|
+
private readonly logger: Logger;
|
|
7
|
+
|
|
8
|
+
private readonly endpoint: string;
|
|
9
|
+
|
|
10
|
+
private pending?: Promise<string>;
|
|
11
|
+
|
|
12
|
+
public constructor(endpoint: string, logger?: Logger) {
|
|
13
|
+
this.endpoint = endpoint;
|
|
14
|
+
this.logger = logger ?? new NullLogger();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public assignCid(): Promise<string> {
|
|
18
|
+
if (this.pending === undefined) {
|
|
19
|
+
this.pending = this.fetchCid().finally(() => {
|
|
20
|
+
this.pending = undefined;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return this.pending;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private async fetchCid(): Promise<string> {
|
|
28
|
+
const options: RequestInit = {
|
|
29
|
+
method: 'GET',
|
|
30
|
+
credentials: 'include',
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const response = await window.fetch(this.endpoint, options);
|
|
34
|
+
|
|
35
|
+
if (!response.ok) {
|
|
36
|
+
const error = new Error(`Failed to assign CID: ${formatCause(response.statusText)}`);
|
|
37
|
+
|
|
38
|
+
this.logger.error(error.message);
|
|
39
|
+
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.logger.debug('New CID successfully assigned');
|
|
44
|
+
|
|
45
|
+
return response.text();
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const CID_ASSIGNER_ENDPOINT_URL = '<@cidAssignerEndpointUrl@>';
|
|
2
|
+
export const TRACKER_ENDPOINT_URL = '<@trackerEndpointUrl@>';
|
|
3
|
+
export const EVALUATION_ENDPOINT_URL = '<@evaluationEndpointUrl@>';
|
|
4
|
+
export const CONTENT_ENDPOINT_URL = '<@contentEndpointUrl@>';
|
|
5
|
+
export const MAX_QUERY_LENGTH = parseInt('<@maxQueryLength@>', 10);
|
|
6
|
+
export const VERSION = '<@version@>';
|