@croct/sdk 0.17.10 → 0.17.11
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/constants.cjs +1 -1
- package/constants.cjs.map +1 -1
- package/constants.d.ts +2 -2
- package/constants.js +1 -1
- package/constants.js.map +1 -1
- package/package.json +1 -1
- package/src/activeRecord.ts +0 -150
- package/src/apiKey.ts +0 -208
- package/src/base64Url.ts +0 -22
- package/src/cache/cache.ts +0 -22
- package/src/cache/cookieCache.ts +0 -88
- package/src/cache/fallbackCache.ts +0 -29
- package/src/cache/inMemoryCache.ts +0 -21
- package/src/cache/index.ts +0 -4
- package/src/cache/localStorageCache.ts +0 -85
- package/src/channel/channel.ts +0 -52
- package/src/channel/encodedChannel.ts +0 -21
- package/src/channel/guaranteedChannel.ts +0 -131
- package/src/channel/httpBeaconChannel.ts +0 -128
- package/src/channel/index.ts +0 -7
- package/src/channel/queuedChannel.ts +0 -143
- package/src/channel/retryChannel.ts +0 -94
- package/src/channel/sandboxChannel.ts +0 -47
- package/src/cid/assigner.ts +0 -3
- package/src/cid/cachedAssigner.ts +0 -68
- package/src/cid/fixedAssigner.ts +0 -13
- package/src/cid/index.ts +0 -4
- package/src/cid/remoteAssigner.ts +0 -57
- package/src/constants.ts +0 -4
- package/src/container.ts +0 -410
- package/src/contentFetcher.ts +0 -290
- package/src/context.ts +0 -139
- package/src/error.ts +0 -31
- package/src/evaluator.ts +0 -314
- package/src/eventManager.ts +0 -53
- package/src/eventSubjectProcessor.ts +0 -85
- package/src/facade/contentFetcherFacade.ts +0 -69
- package/src/facade/evaluatorFacade.ts +0 -111
- package/src/facade/index.ts +0 -7
- package/src/facade/sdkFacade.ts +0 -310
- package/src/facade/sessionFacade.ts +0 -14
- package/src/facade/sessionPatch.ts +0 -32
- package/src/facade/trackerFacade.ts +0 -98
- package/src/facade/userFacade.ts +0 -26
- package/src/facade/userPatch.ts +0 -32
- package/src/help.ts +0 -24
- package/src/index.ts +0 -4
- package/src/logging/consoleLogger.ts +0 -38
- package/src/logging/filteredLogger.ts +0 -57
- package/src/logging/index.ts +0 -5
- package/src/logging/logger.ts +0 -13
- package/src/logging/namespacedLogger.ts +0 -32
- package/src/logging/nullLogger.ts +0 -19
- package/src/namespacedStorage.ts +0 -69
- package/src/patch.ts +0 -64
- package/src/queue/capacityRestrictedQueue.ts +0 -44
- package/src/queue/inMemoryQueue.ts +0 -43
- package/src/queue/index.ts +0 -5
- package/src/queue/monitoredQueue.ts +0 -168
- package/src/queue/persistentQueue.ts +0 -73
- package/src/queue/queue.ts +0 -15
- package/src/retry/arbitraryPolicy.ts +0 -21
- package/src/retry/backoffPolicy.ts +0 -84
- package/src/retry/index.ts +0 -5
- package/src/retry/maxAttemptsPolicy.ts +0 -28
- package/src/retry/neverPolicy.ts +0 -11
- package/src/retry/policy.ts +0 -5
- package/src/schema/attributeSchema.ts +0 -6
- package/src/schema/contentFetcherSchemas.ts +0 -23
- package/src/schema/contentSchemas.ts +0 -44
- package/src/schema/contextSchemas.ts +0 -5
- package/src/schema/ecommerceSchemas.ts +0 -179
- package/src/schema/evaluatorSchemas.ts +0 -52
- package/src/schema/eventSchemas.ts +0 -134
- package/src/schema/index.ts +0 -11
- package/src/schema/loggerSchema.ts +0 -12
- package/src/schema/operationSchemas.ts +0 -102
- package/src/schema/sdkFacadeSchemas.ts +0 -64
- package/src/schema/sdkSchemas.ts +0 -82
- package/src/schema/tokenSchema.ts +0 -42
- package/src/schema/userSchema.ts +0 -184
- package/src/sdk.ts +0 -183
- package/src/sdkEvents.ts +0 -15
- package/src/sourceLocation.ts +0 -85
- package/src/tab.ts +0 -148
- package/src/token/cachedTokenStore.ts +0 -34
- package/src/token/inMemoryTokenStore.ts +0 -13
- package/src/token/index.ts +0 -4
- package/src/token/replicatedTokenStore.ts +0 -21
- package/src/token/token.ts +0 -301
- package/src/tracker.ts +0 -504
- package/src/trackingEvents.ts +0 -452
- package/src/transformer.ts +0 -7
- package/src/utilityTypes.ts +0 -3
- package/src/uuid.ts +0 -43
- package/src/validation/arrayType.ts +0 -71
- package/src/validation/booleanType.ts +0 -22
- package/src/validation/functionType.ts +0 -22
- package/src/validation/index.ts +0 -12
- package/src/validation/jsonType.ts +0 -156
- package/src/validation/mixedSchema.ts +0 -7
- package/src/validation/nullType.ts +0 -22
- package/src/validation/numberType.ts +0 -59
- package/src/validation/objectType.ts +0 -138
- package/src/validation/schema.ts +0 -21
- package/src/validation/stringType.ts +0 -118
- package/src/validation/unionType.ts +0 -53
- package/src/validation/violation.ts +0 -23
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import {Logger} from './logger';
|
|
2
|
-
|
|
3
|
-
export class NullLogger implements Logger {
|
|
4
|
-
public debug(): void {
|
|
5
|
-
// suppress debug logs
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
public info(): void {
|
|
9
|
-
// suppress info logs
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public warn(): void {
|
|
13
|
-
// suppress warning logs
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public error(): void {
|
|
17
|
-
// suppress error logs
|
|
18
|
-
}
|
|
19
|
-
}
|
package/src/namespacedStorage.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
export class NamespacedStorage implements Storage {
|
|
2
|
-
private readonly storage: Storage;
|
|
3
|
-
|
|
4
|
-
private readonly namespace: string;
|
|
5
|
-
|
|
6
|
-
public constructor(storage: Storage, namespace: string) {
|
|
7
|
-
if (namespace === '') {
|
|
8
|
-
throw new Error('The namespace cannot be empty.');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
this.storage = storage;
|
|
12
|
-
this.namespace = namespace;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public get length(): number {
|
|
16
|
-
return this.getKeys().length;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public clear(): void {
|
|
20
|
-
for (const key of this.getKeys()) {
|
|
21
|
-
this.storage.removeItem(key);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public getItem(key: string): string | null {
|
|
26
|
-
return this.storage.getItem(this.getPrefixedKey(key));
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public key(index: number): string | null {
|
|
30
|
-
const keys = this.getKeys();
|
|
31
|
-
|
|
32
|
-
if (index >= keys.length) {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return keys[index].substring(this.namespace.length + 1);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public removeItem(key: string): void {
|
|
40
|
-
this.storage.removeItem(this.getPrefixedKey(key));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public setItem(key: string, value: string): void {
|
|
44
|
-
this.storage.setItem(this.getPrefixedKey(key), value);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private getKeys(): string[] {
|
|
48
|
-
const keys = [];
|
|
49
|
-
const prefix = this.getPrefix();
|
|
50
|
-
|
|
51
|
-
for (let index = 0; index < this.storage.length; index++) {
|
|
52
|
-
const key = this.storage.key(index);
|
|
53
|
-
|
|
54
|
-
if (key !== null && key.indexOf(prefix) === 0) {
|
|
55
|
-
keys.push(key);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return keys;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
private getPrefixedKey(key: string): string {
|
|
63
|
-
return this.getPrefix() + key;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private getPrefix(): string {
|
|
67
|
-
return `${this.namespace}.`;
|
|
68
|
-
}
|
|
69
|
-
}
|
package/src/patch.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import {JsonStructure, JsonValue} from '@croct/json';
|
|
2
|
-
|
|
3
|
-
interface AbstractOperation {
|
|
4
|
-
type: string;
|
|
5
|
-
path: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
interface UnsetOperation extends AbstractOperation {
|
|
9
|
-
type: 'unset';
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface ClearOperation extends AbstractOperation {
|
|
13
|
-
type: 'clear';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface SetOperation extends AbstractOperation {
|
|
17
|
-
type: 'set';
|
|
18
|
-
value: JsonValue;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface AddOperation extends AbstractOperation {
|
|
22
|
-
type: 'add';
|
|
23
|
-
value: JsonValue;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface CombineOperation extends AbstractOperation {
|
|
27
|
-
type: 'combine';
|
|
28
|
-
value: JsonValue;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface MergeOperation extends AbstractOperation {
|
|
32
|
-
type: 'merge';
|
|
33
|
-
value: JsonStructure;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface IncrementOperation extends AbstractOperation {
|
|
37
|
-
type: 'increment';
|
|
38
|
-
value: JsonValue;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
interface DecrementOperation extends AbstractOperation {
|
|
42
|
-
type: 'decrement';
|
|
43
|
-
value: JsonValue;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
interface removeOperation extends AbstractOperation {
|
|
47
|
-
type: 'remove';
|
|
48
|
-
value: JsonValue;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type Operation =
|
|
52
|
-
UnsetOperation
|
|
53
|
-
| ClearOperation
|
|
54
|
-
| AddOperation
|
|
55
|
-
| SetOperation
|
|
56
|
-
| CombineOperation
|
|
57
|
-
| MergeOperation
|
|
58
|
-
| IncrementOperation
|
|
59
|
-
| DecrementOperation
|
|
60
|
-
| removeOperation;
|
|
61
|
-
|
|
62
|
-
export interface Patch {
|
|
63
|
-
operations: Operation[];
|
|
64
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import {Queue} from './queue';
|
|
2
|
-
|
|
3
|
-
export class CapacityRestrictedQueue<T> implements Queue<T> {
|
|
4
|
-
private readonly queue: Queue<T>;
|
|
5
|
-
|
|
6
|
-
private readonly capacity: number;
|
|
7
|
-
|
|
8
|
-
public constructor(queue: Queue<T>, capacity: number) {
|
|
9
|
-
this.queue = queue;
|
|
10
|
-
this.capacity = capacity;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public all(): T[] {
|
|
14
|
-
return this.queue.all();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public getCapacity(): number {
|
|
18
|
-
return this.capacity;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public isEmpty(): boolean {
|
|
22
|
-
return this.queue.isEmpty();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public length(): number {
|
|
26
|
-
return Math.min(this.capacity, this.queue.length());
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public peek(): T | null {
|
|
30
|
-
return this.queue.peek();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public push(value: T): void {
|
|
34
|
-
if (this.queue.length() >= this.capacity) {
|
|
35
|
-
throw new Error('Maximum queue capacity reached.');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
this.queue.push(value);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public shift(): T {
|
|
42
|
-
return this.queue.shift();
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import {Queue} from './queue';
|
|
2
|
-
|
|
3
|
-
export class InMemoryQueue<T> implements Queue<T> {
|
|
4
|
-
private queue: T[] = [];
|
|
5
|
-
|
|
6
|
-
public constructor(...values: T[]) {
|
|
7
|
-
this.queue.unshift(...values);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public all(): T[] {
|
|
11
|
-
return this.queue.slice();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public getCapacity(): number {
|
|
15
|
-
return Infinity;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public isEmpty(): boolean {
|
|
19
|
-
return this.queue.length === 0;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public push(value: T): void {
|
|
23
|
-
this.queue.push(value);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public peek(): T | null {
|
|
27
|
-
return this.queue[0] ?? null;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public shift(): T {
|
|
31
|
-
const value = this.queue.shift();
|
|
32
|
-
|
|
33
|
-
if (value === undefined) {
|
|
34
|
-
throw new Error('The queue is empty.');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public length(): number {
|
|
41
|
-
return this.queue.length;
|
|
42
|
-
}
|
|
43
|
-
}
|
package/src/queue/index.ts
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import {Queue} from './queue';
|
|
2
|
-
import {Logger, NullLogger} from '../logging';
|
|
3
|
-
|
|
4
|
-
export type QueueStatus = 'halfEmpty' | 'almostEmpty' | 'empty' | 'halfFull' | 'almostFull' | 'full';
|
|
5
|
-
|
|
6
|
-
export type QueueCallback<T> = {
|
|
7
|
-
(queue: Queue<T>): void,
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export class MonitoredQueue<T> implements Queue<T> {
|
|
11
|
-
private readonly queue: Queue<T>;
|
|
12
|
-
|
|
13
|
-
private readonly logger: Logger;
|
|
14
|
-
|
|
15
|
-
private readonly callbacks: Partial<{[key in QueueStatus]: Array<QueueCallback<T>>}> = {};
|
|
16
|
-
|
|
17
|
-
private status: QueueStatus;
|
|
18
|
-
|
|
19
|
-
public constructor(queue: Queue<T>, logger?: Logger) {
|
|
20
|
-
this.queue = queue;
|
|
21
|
-
this.logger = logger ?? new NullLogger();
|
|
22
|
-
|
|
23
|
-
this.updateStatus();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public all(): T[] {
|
|
27
|
-
return this.queue.all();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public getCapacity(): number {
|
|
31
|
-
return this.queue.getCapacity();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public addCallback(status: QueueStatus, callback: QueueCallback<T>): void {
|
|
35
|
-
const callbacks = this.callbacks[status] ?? [];
|
|
36
|
-
|
|
37
|
-
if (!callbacks.includes(callback)) {
|
|
38
|
-
callbacks.push(callback);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
this.callbacks[status] = callbacks;
|
|
42
|
-
|
|
43
|
-
switch (this.status) {
|
|
44
|
-
case status:
|
|
45
|
-
callback(this);
|
|
46
|
-
|
|
47
|
-
break;
|
|
48
|
-
|
|
49
|
-
case 'empty':
|
|
50
|
-
case 'almostEmpty':
|
|
51
|
-
if (status === 'halfEmpty') {
|
|
52
|
-
callback(this);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
break;
|
|
56
|
-
|
|
57
|
-
case 'full':
|
|
58
|
-
case 'almostFull':
|
|
59
|
-
if (status === 'halfFull') {
|
|
60
|
-
callback(this);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public removeCallback(type: QueueStatus, callback: QueueCallback<T>): void {
|
|
68
|
-
const callbacks = this.callbacks[type];
|
|
69
|
-
|
|
70
|
-
if (callbacks == null) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const index = callbacks.indexOf(callback);
|
|
75
|
-
|
|
76
|
-
if (index >= 0) {
|
|
77
|
-
callbacks.splice(index, 1);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private setStatus(status: QueueStatus): void {
|
|
82
|
-
if (this.status === status) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
this.logger.debug(`Queue status changed to "${status}"`);
|
|
87
|
-
|
|
88
|
-
this.report(status);
|
|
89
|
-
|
|
90
|
-
this.status = status;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
private report(status: QueueStatus): void {
|
|
94
|
-
const callbacks = this.callbacks[status];
|
|
95
|
-
|
|
96
|
-
if (callbacks !== undefined) {
|
|
97
|
-
callbacks.forEach(callback => callback(this));
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
switch (status) {
|
|
101
|
-
case 'empty':
|
|
102
|
-
case 'almostEmpty':
|
|
103
|
-
this.report('halfEmpty');
|
|
104
|
-
|
|
105
|
-
break;
|
|
106
|
-
|
|
107
|
-
case 'full':
|
|
108
|
-
case 'almostFull':
|
|
109
|
-
this.report('halfFull');
|
|
110
|
-
|
|
111
|
-
break;
|
|
112
|
-
|
|
113
|
-
default:
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public isEmpty(): boolean {
|
|
119
|
-
return this.queue.isEmpty();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public length(): number {
|
|
123
|
-
return this.queue.length();
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public peek(): T | null {
|
|
127
|
-
return this.queue.peek();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
public push(value: T): void {
|
|
131
|
-
this.queue.push(value);
|
|
132
|
-
|
|
133
|
-
this.updateStatus();
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
public shift(): T {
|
|
137
|
-
const value = this.queue.shift();
|
|
138
|
-
|
|
139
|
-
this.updateStatus();
|
|
140
|
-
|
|
141
|
-
return value;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
private updateStatus(): void {
|
|
145
|
-
const length = this.queue.length();
|
|
146
|
-
const capacity = this.getCapacity();
|
|
147
|
-
|
|
148
|
-
if (length <= capacity * 0.5) {
|
|
149
|
-
if (length === 0) {
|
|
150
|
-
this.setStatus('empty');
|
|
151
|
-
} else if (length <= capacity * 0.25) {
|
|
152
|
-
this.setStatus('almostEmpty');
|
|
153
|
-
} else {
|
|
154
|
-
this.setStatus('halfEmpty');
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (length >= capacity) {
|
|
161
|
-
this.setStatus('full');
|
|
162
|
-
} else if (length >= capacity * 0.75) {
|
|
163
|
-
this.setStatus('almostFull');
|
|
164
|
-
} else {
|
|
165
|
-
this.setStatus('halfFull');
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import {Queue} from './queue';
|
|
2
|
-
|
|
3
|
-
export class PersistentQueue<T> implements Queue<T> {
|
|
4
|
-
private readonly storage: Storage;
|
|
5
|
-
|
|
6
|
-
private readonly key: string;
|
|
7
|
-
|
|
8
|
-
public constructor(storage: Storage, key = 'queue') {
|
|
9
|
-
this.storage = storage;
|
|
10
|
-
this.key = key;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public all(): T[] {
|
|
14
|
-
return this.queue.slice();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public getCapacity(): number {
|
|
18
|
-
return Number.MAX_SAFE_INTEGER;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public isEmpty(): boolean {
|
|
22
|
-
return this.length() === 0;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public length(): number {
|
|
26
|
-
return this.queue.length;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public push(value: T): void {
|
|
30
|
-
this.save([...this.queue, value]);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public peek(): T | null {
|
|
34
|
-
const item = this.queue[0];
|
|
35
|
-
|
|
36
|
-
if (item === undefined) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return item;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public shift(): T {
|
|
44
|
-
const queue = [...this.queue];
|
|
45
|
-
const value = queue.shift();
|
|
46
|
-
|
|
47
|
-
if (value === undefined) {
|
|
48
|
-
throw new Error('The queue is empty.');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.save(queue);
|
|
52
|
-
|
|
53
|
-
return value;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private get queue(): readonly T[] {
|
|
57
|
-
const data = this.storage.getItem(this.key);
|
|
58
|
-
|
|
59
|
-
if (data === null) {
|
|
60
|
-
return [];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
return JSON.parse(data);
|
|
65
|
-
} catch {
|
|
66
|
-
return [];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private save(data: T[]): void {
|
|
71
|
-
this.storage.setItem(this.key, JSON.stringify(data));
|
|
72
|
-
}
|
|
73
|
-
}
|
package/src/queue/queue.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import {RetryPolicy} from './policy';
|
|
2
|
-
|
|
3
|
-
export class ArbitraryPolicy<T> implements RetryPolicy<T> {
|
|
4
|
-
private readonly delays: number[];
|
|
5
|
-
|
|
6
|
-
public constructor(delays: number[]) {
|
|
7
|
-
if (delays.length < 1) {
|
|
8
|
-
throw new Error('The list of delays cannot be empty.');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
this.delays = [...delays];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public getDelay(attempt: number): number {
|
|
15
|
-
return this.delays[Math.min(attempt < 0 ? 0 : attempt, this.delays.length - 1)];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public shouldRetry(): boolean {
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import {RetryPolicy} from './policy';
|
|
2
|
-
|
|
3
|
-
type Options = {
|
|
4
|
-
minRetryDelay: number, // min retry delay in ms (used in exp. backoff calcs)
|
|
5
|
-
maxRetryDelay: number, // max retry delay in ms (used in exp. backoff calcs)
|
|
6
|
-
backoffFactor: number, // exponential backoff factor (attempts^n)
|
|
7
|
-
backoffJitter: number, // jitter factor for backoff calcs (0 is usually fine)
|
|
8
|
-
maxAttempts: number,
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export class BackoffPolicy<T> implements RetryPolicy<T> {
|
|
12
|
-
private readonly minRetryDelay: number = 1000;
|
|
13
|
-
|
|
14
|
-
private readonly maxRetryDelay: number = 30000;
|
|
15
|
-
|
|
16
|
-
private readonly backoffFactor: number = 2;
|
|
17
|
-
|
|
18
|
-
private readonly backoffJitter: number = 1;
|
|
19
|
-
|
|
20
|
-
private readonly maxAttempts: number = Infinity;
|
|
21
|
-
|
|
22
|
-
public constructor(options: Partial<Options> = {}) {
|
|
23
|
-
const {
|
|
24
|
-
minRetryDelay = this.minRetryDelay,
|
|
25
|
-
maxRetryDelay = this.maxRetryDelay,
|
|
26
|
-
backoffFactor = this.backoffFactor,
|
|
27
|
-
backoffJitter = this.backoffJitter,
|
|
28
|
-
maxAttempts = this.maxAttempts,
|
|
29
|
-
} = options;
|
|
30
|
-
|
|
31
|
-
if (minRetryDelay < 0) {
|
|
32
|
-
throw new Error('The minimum retry delay must be non-negative.');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (maxRetryDelay < minRetryDelay) {
|
|
36
|
-
throw new Error('The maximum retry delay must be greater than the minimum.');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (backoffFactor < 1) {
|
|
40
|
-
throw new Error('The backoff factor must be greater than zero.');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (backoffJitter < 0) {
|
|
44
|
-
throw new Error('The backoff jitter must be non-negative.');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (maxAttempts < 0) {
|
|
48
|
-
throw new Error('The maximum attempts must be non-negative.');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.minRetryDelay = minRetryDelay;
|
|
52
|
-
this.maxRetryDelay = maxRetryDelay;
|
|
53
|
-
this.backoffFactor = backoffFactor;
|
|
54
|
-
this.backoffJitter = backoffJitter;
|
|
55
|
-
this.maxAttempts = maxAttempts;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Full Jitter algorithm
|
|
60
|
-
*
|
|
61
|
-
* @see https://www.awsarchitectureblog.com/2015/03/backoff.html
|
|
62
|
-
*/
|
|
63
|
-
public getDelay(attempt: number): number {
|
|
64
|
-
let delay = Math.min(Math.max(this.backoffFactor ** attempt, this.minRetryDelay), this.maxRetryDelay);
|
|
65
|
-
|
|
66
|
-
if (this.backoffJitter > 0) {
|
|
67
|
-
// Jitter will result in a random value between the minimum and
|
|
68
|
-
// calculated delay for a given attempt.
|
|
69
|
-
const min = Math.ceil(this.minRetryDelay);
|
|
70
|
-
const max = Math.floor(delay);
|
|
71
|
-
|
|
72
|
-
delay = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Removing any fractional digits
|
|
76
|
-
delay -= delay % 1;
|
|
77
|
-
|
|
78
|
-
return delay;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public shouldRetry(attempt: number): boolean {
|
|
82
|
-
return attempt < this.maxAttempts;
|
|
83
|
-
}
|
|
84
|
-
}
|
package/src/retry/index.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import {RetryPolicy} from './policy';
|
|
2
|
-
|
|
3
|
-
export class MaxAttemptsPolicy<T> implements RetryPolicy<T> {
|
|
4
|
-
private readonly maxAttempts: number;
|
|
5
|
-
|
|
6
|
-
private readonly delay: number;
|
|
7
|
-
|
|
8
|
-
public constructor(delay: number, maxAttempts: number) {
|
|
9
|
-
if (delay < 0) {
|
|
10
|
-
throw new Error('Delay must be non-negative.');
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (maxAttempts < 0) {
|
|
14
|
-
throw new Error('Max attempts must be non-negative.');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
this.maxAttempts = maxAttempts;
|
|
18
|
-
this.delay = delay;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public getDelay(): number {
|
|
22
|
-
return this.delay;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public shouldRetry(attempt: number): boolean {
|
|
26
|
-
return attempt < this.maxAttempts;
|
|
27
|
-
}
|
|
28
|
-
}
|
package/src/retry/neverPolicy.ts
DELETED
package/src/retry/policy.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import {ObjectType, NumberType, JsonObjectType, StringType, UnionType} from '../validation';
|
|
2
|
-
|
|
3
|
-
export const fetchOptionsSchema = new ObjectType({
|
|
4
|
-
properties: {
|
|
5
|
-
timeout: new NumberType({
|
|
6
|
-
integer: true,
|
|
7
|
-
minimum: 0,
|
|
8
|
-
}),
|
|
9
|
-
version: new UnionType(
|
|
10
|
-
new StringType({
|
|
11
|
-
pattern: /^\d+$/,
|
|
12
|
-
}),
|
|
13
|
-
new NumberType({
|
|
14
|
-
integer: true,
|
|
15
|
-
minimum: 1,
|
|
16
|
-
}),
|
|
17
|
-
),
|
|
18
|
-
preferredLocale: new StringType({
|
|
19
|
-
pattern: /^[a-z]{2,3}([-_][a-z]{2,3})?$/i,
|
|
20
|
-
}),
|
|
21
|
-
attributes: new JsonObjectType(),
|
|
22
|
-
},
|
|
23
|
-
});
|