@croct/sdk 0.9.1 → 0.11.0-alpha
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 +32 -36
- package/cache/fallbackCache.js +15 -32
- package/cache/inMemoryCache.js +9 -10
- package/cache/index.js +1 -1
- package/cache/localStorageCache.js +24 -25
- package/channel/beaconSocketChannel.d.ts +1 -1
- package/channel/beaconSocketChannel.js +49 -79
- package/channel/channel.d.ts +1 -1
- package/channel/encodedChannel.js +8 -10
- package/channel/guaranteedChannel.d.ts +4 -4
- package/channel/guaranteedChannel.js +41 -43
- package/channel/index.js +1 -1
- package/channel/queuedChannel.js +35 -64
- package/channel/retryChannel.d.ts +1 -1
- package/channel/retryChannel.js +44 -77
- package/channel/sandboxChannel.js +17 -18
- package/channel/socketChannel.d.ts +4 -4
- package/channel/socketChannel.js +77 -79
- package/cid/cachedAssigner.js +15 -27
- package/cid/fixedAssigner.js +5 -6
- package/cid/index.js +1 -1
- package/cid/remoteAssigner.js +23 -36
- package/constants.d.ts +3 -2
- package/constants.js +6 -5
- package/container.d.ts +13 -6
- package/container.js +152 -168
- package/contentFetcher.d.ts +59 -0
- package/contentFetcher.js +129 -0
- package/context.d.ts +3 -3
- package/context.js +36 -38
- package/error.js +2 -2
- package/evaluator.d.ts +33 -24
- package/evaluator.js +126 -117
- package/eventManager.d.ts +1 -1
- package/eventManager.js +14 -15
- package/facade/contentFetcherFacade.d.ts +27 -0
- package/facade/contentFetcherFacade.js +40 -0
- package/facade/evaluatorFacade.d.ts +13 -3
- package/facade/evaluatorFacade.js +57 -72
- package/facade/sdkFacade.d.ts +10 -3
- package/facade/sdkFacade.js +129 -141
- package/facade/sessionFacade.js +6 -7
- package/facade/sessionPatch.js +9 -13
- package/facade/trackerFacade.js +32 -38
- package/facade/userFacade.js +10 -11
- package/facade/userPatch.js +9 -13
- package/index.js +2 -2
- package/logging/consoleLogger.js +18 -35
- package/logging/index.js +1 -1
- package/logging/namespacedLogger.js +14 -15
- package/logging/nullLogger.js +10 -13
- package/namespacedStorage.js +30 -47
- package/package.json +13 -16
- package/patch.d.ts +1 -1
- package/queue/capacityRestrictedQueue.js +17 -18
- package/queue/inMemoryQueue.js +22 -28
- package/queue/index.js +1 -1
- package/queue/monitoredQueue.d.ts +2 -2
- package/queue/monitoredQueue.js +39 -40
- package/queue/persistentQueue.js +33 -38
- package/retry/arbitraryPolicy.js +8 -10
- package/retry/backoffPolicy.d.ts +1 -1
- package/retry/backoffPolicy.js +11 -13
- package/retry/index.js +1 -1
- package/retry/maxAttemptsPolicy.js +7 -8
- package/retry/neverPolicy.js +6 -9
- package/schema/attributeSchema.js +1 -1
- package/schema/contentFetcherSchemas.d.ts +2 -0
- package/schema/contentFetcherSchemas.js +22 -0
- package/schema/contentSchemas.js +1 -1
- package/schema/contextSchemas.js +1 -1
- package/schema/ecommerceSchemas.js +1 -1
- package/schema/evaluatorSchemas.d.ts +2 -0
- package/schema/{evaluationSchemas.js → evaluatorSchemas.js} +3 -3
- package/schema/eventSchemas.js +8 -9
- package/schema/index.d.ts +2 -1
- package/schema/index.js +3 -2
- package/schema/loggerSchema.js +1 -1
- package/schema/operationSchemas.js +8 -8
- package/schema/sdkFacadeSchemas.js +9 -6
- package/schema/sdkSchemas.js +8 -5
- package/schema/tokenSchema.js +5 -4
- package/schema/userSchema.js +2 -2
- package/sdk.d.ts +9 -3
- package/sdk.js +81 -127
- package/sdkEvents.d.ts +3 -3
- package/sourceLocation.d.ts +3 -3
- package/sourceLocation.js +13 -14
- package/tab.d.ts +5 -5
- package/tab.js +50 -80
- package/token/cachedTokenStore.js +9 -10
- package/token/inMemoryTokenStore.js +7 -8
- package/token/index.js +1 -1
- package/token/replicatedTokenStore.js +7 -8
- package/token/token.d.ts +9 -5
- package/token/token.js +63 -57
- package/tracker.d.ts +4 -4
- package/tracker.js +145 -122
- package/trackingEvents.d.ts +36 -36
- package/trackingEvents.js +12 -6
- package/transformer.js +1 -1
- package/utilityTypes.d.ts +2 -2
- package/uuid.js +9 -7
- package/validation/arrayType.d.ts +2 -2
- package/validation/arrayType.js +29 -27
- package/validation/booleanType.js +11 -15
- package/validation/functionType.js +11 -15
- package/validation/index.js +1 -1
- package/validation/jsonType.d.ts +2 -2
- package/validation/jsonType.js +61 -80
- package/validation/mixedSchema.js +4 -7
- package/validation/nullType.js +11 -15
- package/validation/numberType.d.ts +1 -1
- package/validation/numberType.js +23 -22
- package/validation/objectType.d.ts +1 -1
- package/validation/objectType.js +61 -73
- package/validation/schema.js +6 -10
- package/validation/stringType.d.ts +1 -1
- package/validation/stringType.js +36 -47
- package/validation/unionType.js +27 -77
- package/validation/violation.js +1 -2
- package/schema/evaluationSchemas.d.ts +0 -2
package/activeRecord.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ActiveRecord = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var operationSchema = {
|
|
4
|
+
const schema_1 = require("./schema");
|
|
5
|
+
const operationSchema = {
|
|
7
6
|
add: schema_1.addOperation,
|
|
8
7
|
set: schema_1.setOperation,
|
|
9
8
|
merge: schema_1.mergeOperation,
|
|
@@ -14,11 +13,11 @@ var operationSchema = {
|
|
|
14
13
|
unset: schema_1.unsetOperation,
|
|
15
14
|
remove: schema_1.removeOperation,
|
|
16
15
|
};
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
class ActiveRecord {
|
|
17
|
+
constructor() {
|
|
19
18
|
this.operations = [];
|
|
20
19
|
}
|
|
21
|
-
|
|
20
|
+
set(propertyOrValue, value) {
|
|
22
21
|
if (typeof propertyOrValue === 'string') {
|
|
23
22
|
return this.pushOperation({
|
|
24
23
|
type: 'set',
|
|
@@ -31,22 +30,22 @@ var ActiveRecord = /** @class */ (function () {
|
|
|
31
30
|
path: '.',
|
|
32
31
|
value: propertyOrValue,
|
|
33
32
|
});
|
|
34
|
-
}
|
|
35
|
-
|
|
33
|
+
}
|
|
34
|
+
add(property, value) {
|
|
36
35
|
return this.pushOperation({
|
|
37
36
|
type: 'add',
|
|
38
37
|
path: property,
|
|
39
38
|
value: value,
|
|
40
39
|
});
|
|
41
|
-
}
|
|
42
|
-
|
|
40
|
+
}
|
|
41
|
+
combine(property, value) {
|
|
43
42
|
return this.pushOperation({
|
|
44
43
|
type: 'combine',
|
|
45
44
|
path: property,
|
|
46
45
|
value: value,
|
|
47
46
|
});
|
|
48
|
-
}
|
|
49
|
-
|
|
47
|
+
}
|
|
48
|
+
merge(propertyOrValue, value) {
|
|
50
49
|
if (typeof propertyOrValue === 'string') {
|
|
51
50
|
return this.pushOperation({
|
|
52
51
|
type: 'merge',
|
|
@@ -59,58 +58,55 @@ var ActiveRecord = /** @class */ (function () {
|
|
|
59
58
|
path: '.',
|
|
60
59
|
value: propertyOrValue,
|
|
61
60
|
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (amount === void 0) { amount = 1; }
|
|
61
|
+
}
|
|
62
|
+
increment(property, amount = 1) {
|
|
65
63
|
return this.pushOperation({
|
|
66
64
|
type: 'increment',
|
|
67
65
|
path: property,
|
|
68
66
|
value: amount,
|
|
69
67
|
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (amount === void 0) { amount = 1; }
|
|
68
|
+
}
|
|
69
|
+
decrement(property, amount = 1) {
|
|
73
70
|
return this.pushOperation({
|
|
74
71
|
type: 'decrement',
|
|
75
72
|
path: property,
|
|
76
73
|
value: amount,
|
|
77
74
|
});
|
|
78
|
-
}
|
|
79
|
-
|
|
75
|
+
}
|
|
76
|
+
clear(property) {
|
|
80
77
|
return this.pushOperation({
|
|
81
78
|
type: 'clear',
|
|
82
79
|
path: property,
|
|
83
80
|
});
|
|
84
|
-
}
|
|
85
|
-
|
|
81
|
+
}
|
|
82
|
+
unset(property) {
|
|
86
83
|
return this.pushOperation({
|
|
87
84
|
type: 'unset',
|
|
88
85
|
path: property,
|
|
89
86
|
});
|
|
90
|
-
}
|
|
91
|
-
|
|
87
|
+
}
|
|
88
|
+
remove(property, value) {
|
|
92
89
|
return this.pushOperation({
|
|
93
90
|
type: 'remove',
|
|
94
91
|
path: property,
|
|
95
92
|
value: value,
|
|
96
93
|
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
94
|
+
}
|
|
95
|
+
pushOperation(operation) {
|
|
96
|
+
const { type, ...data } = operation;
|
|
100
97
|
operationSchema[type].validate(data);
|
|
101
98
|
this.operations.push(operation);
|
|
102
99
|
return this;
|
|
103
|
-
}
|
|
104
|
-
|
|
100
|
+
}
|
|
101
|
+
reset() {
|
|
105
102
|
this.operations.splice(0);
|
|
106
103
|
return this;
|
|
107
|
-
}
|
|
108
|
-
|
|
104
|
+
}
|
|
105
|
+
isDirty() {
|
|
109
106
|
return this.operations.length > 0;
|
|
110
|
-
}
|
|
111
|
-
|
|
107
|
+
}
|
|
108
|
+
buildPatch() {
|
|
112
109
|
return { operations: this.operations.slice() };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
}());
|
|
110
|
+
}
|
|
111
|
+
}
|
|
116
112
|
exports.ActiveRecord = ActiveRecord;
|
package/cache/fallbackCache.js
CHANGED
|
@@ -1,41 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FallbackCache = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function FallbackCache() {
|
|
7
|
-
var caches = [];
|
|
8
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
9
|
-
caches[_i] = arguments[_i];
|
|
10
|
-
}
|
|
4
|
+
class FallbackCache {
|
|
5
|
+
constructor(...caches) {
|
|
11
6
|
this.caches = caches;
|
|
12
7
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var value = cache.get();
|
|
19
|
-
if (value !== null) {
|
|
20
|
-
return value;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
25
|
-
finally {
|
|
26
|
-
try {
|
|
27
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
8
|
+
get() {
|
|
9
|
+
for (const cache of this.caches) {
|
|
10
|
+
const value = cache.get();
|
|
11
|
+
if (value !== null) {
|
|
12
|
+
return value;
|
|
28
13
|
}
|
|
29
|
-
finally { if (e_1) throw e_1.error; }
|
|
30
14
|
}
|
|
31
15
|
return null;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
this.caches.forEach(
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
this.caches.forEach(
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
}());
|
|
16
|
+
}
|
|
17
|
+
put(value) {
|
|
18
|
+
this.caches.forEach(cache => cache.put(value));
|
|
19
|
+
}
|
|
20
|
+
clear() {
|
|
21
|
+
this.caches.forEach(cache => cache.clear());
|
|
22
|
+
}
|
|
23
|
+
}
|
|
41
24
|
exports.FallbackCache = FallbackCache;
|
package/cache/inMemoryCache.js
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.InMemoryCache = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
class InMemoryCache {
|
|
5
|
+
constructor(cache) {
|
|
6
6
|
this.cache = cache;
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
get() {
|
|
9
9
|
var _a;
|
|
10
10
|
return (_a = this.cache) !== null && _a !== void 0 ? _a : null;
|
|
11
|
-
}
|
|
12
|
-
|
|
11
|
+
}
|
|
12
|
+
put(value) {
|
|
13
13
|
this.cache = value;
|
|
14
|
-
}
|
|
15
|
-
|
|
14
|
+
}
|
|
15
|
+
clear() {
|
|
16
16
|
delete this.cache;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
}());
|
|
17
|
+
}
|
|
18
|
+
}
|
|
20
19
|
exports.InMemoryCache = InMemoryCache;
|
package/cache/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LocalStorageCache = exports.InMemoryCache = exports.FallbackCache = void 0;
|
|
4
|
-
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
5
|
tslib_1.__exportStar(require("./cache"), exports);
|
|
6
6
|
var fallbackCache_1 = require("./fallbackCache");
|
|
7
7
|
Object.defineProperty(exports, "FallbackCache", { enumerable: true, get: function () { return fallbackCache_1.FallbackCache; } });
|
|
@@ -1,50 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LocalStorageCache = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
class LocalStorageCache {
|
|
5
|
+
constructor(storage, key) {
|
|
6
6
|
this.listeners = [];
|
|
7
7
|
this.storage = storage;
|
|
8
8
|
this.key = key;
|
|
9
9
|
this.value = storage.getItem(key);
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
static autoSync(cache) {
|
|
12
|
+
const listener = cache.sync.bind(cache);
|
|
13
13
|
window.addEventListener('storage', listener);
|
|
14
|
-
return
|
|
15
|
-
}
|
|
16
|
-
|
|
14
|
+
return () => window.removeEventListener('storage', listener);
|
|
15
|
+
}
|
|
16
|
+
get() {
|
|
17
17
|
return this.value;
|
|
18
|
-
}
|
|
19
|
-
|
|
18
|
+
}
|
|
19
|
+
put(value) {
|
|
20
20
|
this.storage.setItem(this.key, value);
|
|
21
21
|
if (this.value !== value) {
|
|
22
22
|
this.value = value;
|
|
23
23
|
this.notifyChange(value);
|
|
24
24
|
}
|
|
25
|
-
}
|
|
26
|
-
|
|
25
|
+
}
|
|
26
|
+
clear() {
|
|
27
27
|
this.storage.removeItem(this.key);
|
|
28
28
|
if (this.value !== null) {
|
|
29
29
|
this.value = null;
|
|
30
30
|
this.notifyChange(null);
|
|
31
31
|
}
|
|
32
|
-
}
|
|
33
|
-
|
|
32
|
+
}
|
|
33
|
+
addListener(listener) {
|
|
34
34
|
if (!this.listeners.includes(listener)) {
|
|
35
35
|
this.listeners.push(listener);
|
|
36
36
|
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
}
|
|
38
|
+
removeListener(listener) {
|
|
39
|
+
const index = this.listeners.indexOf(listener);
|
|
40
40
|
if (index > -1) {
|
|
41
41
|
this.listeners.splice(index, 1);
|
|
42
42
|
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
this.listeners.forEach(
|
|
46
|
-
}
|
|
47
|
-
|
|
43
|
+
}
|
|
44
|
+
notifyChange(value) {
|
|
45
|
+
this.listeners.forEach(listener => listener(value));
|
|
46
|
+
}
|
|
47
|
+
sync(event) {
|
|
48
48
|
if (event.storageArea !== this.storage || (event.key !== null && event.key !== this.key)) {
|
|
49
49
|
// Ignore unrelated changes
|
|
50
50
|
return;
|
|
@@ -55,12 +55,11 @@ var LocalStorageCache = /** @class */ (function () {
|
|
|
55
55
|
* In case of cascading changes, it prevents notifying listeners
|
|
56
56
|
* about intermediate states already outdated at this point.
|
|
57
57
|
*/
|
|
58
|
-
|
|
58
|
+
const value = this.storage.getItem(this.key);
|
|
59
59
|
if (this.value !== value) {
|
|
60
60
|
this.value = value;
|
|
61
61
|
this.notifyChange(value);
|
|
62
62
|
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
}());
|
|
63
|
+
}
|
|
64
|
+
}
|
|
66
65
|
exports.LocalStorageCache = LocalStorageCache;
|
|
@@ -5,7 +5,7 @@ import { CidAssigner } from '../cid';
|
|
|
5
5
|
export interface DuplexChannelFactory {
|
|
6
6
|
(url: string, logger: Logger): DuplexChannel<string, string>;
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
type Configuration = {
|
|
9
9
|
logger?: Logger;
|
|
10
10
|
loggerFactory?: LoggerFactory;
|
|
11
11
|
tokenParameter: string;
|
|
@@ -1,112 +1,82 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BeaconSocketChannel = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function BeaconSocketChannel(configuration) {
|
|
4
|
+
const logging_1 = require("../logging");
|
|
5
|
+
class BeaconSocketChannel {
|
|
6
|
+
constructor(configuration) {
|
|
8
7
|
var _a, _b;
|
|
9
8
|
this.listeners = [];
|
|
10
9
|
this.connectionIndex = 0;
|
|
11
10
|
this.socketFactory = configuration.channelFactory;
|
|
12
11
|
this.logger = (_a = configuration.logger) !== null && _a !== void 0 ? _a : new logging_1.NullLogger();
|
|
13
|
-
this.loggerFactory = (_b = configuration.loggerFactory) !== null && _b !== void 0 ? _b : (
|
|
12
|
+
this.loggerFactory = (_b = configuration.loggerFactory) !== null && _b !== void 0 ? _b : (() => new logging_1.NullLogger());
|
|
14
13
|
this.cidAssigner = configuration.cidAssigner;
|
|
15
14
|
this.cidParameter = configuration.cidParameter;
|
|
16
15
|
this.trackerEndpointUrl = configuration.trackerEndpointUrl;
|
|
17
16
|
this.tokenParameter = configuration.tokenParameter;
|
|
18
17
|
this.notify = this.notify.bind(this);
|
|
19
18
|
}
|
|
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
|
-
BeaconSocketChannel.prototype.createSocketChannel = function (token) {
|
|
55
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
56
|
-
var endpoint, _a, _b, _c, channel;
|
|
57
|
-
return tslib_1.__generator(this, function (_d) {
|
|
58
|
-
switch (_d.label) {
|
|
59
|
-
case 0:
|
|
60
|
-
endpoint = new URL(this.trackerEndpointUrl);
|
|
61
|
-
_b = (_a = endpoint.searchParams).append;
|
|
62
|
-
_c = [this.cidParameter];
|
|
63
|
-
return [4 /*yield*/, this.cidAssigner.assignCid()];
|
|
64
|
-
case 1:
|
|
65
|
-
_b.apply(_a, _c.concat([_d.sent()]));
|
|
66
|
-
if (token !== undefined) {
|
|
67
|
-
endpoint.searchParams.append(this.tokenParameter, token);
|
|
68
|
-
}
|
|
69
|
-
channel = this.socketFactory(endpoint.toString(), this.loggerFactory("WebSocket#".concat(this.connectionIndex)));
|
|
70
|
-
this.connectionIndex += 1;
|
|
71
|
-
channel.subscribe(this.notify);
|
|
72
|
-
return [2 /*return*/, channel];
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
};
|
|
77
|
-
BeaconSocketChannel.prototype.subscribe = function (listener) {
|
|
19
|
+
async publish({ id: receiptId, message }) {
|
|
20
|
+
const { token, timestamp, context, payload } = JSON.parse(message);
|
|
21
|
+
if (this.token !== token || this.socketChannel === undefined) {
|
|
22
|
+
if (this.socketChannel !== undefined) {
|
|
23
|
+
this.logger.info('Connection no longer valid for current message.');
|
|
24
|
+
this.socketChannel.unsubscribe(this.notify);
|
|
25
|
+
await this.socketChannel.close();
|
|
26
|
+
}
|
|
27
|
+
this.token = token;
|
|
28
|
+
this.socketChannel = await this.createSocketChannel(token);
|
|
29
|
+
}
|
|
30
|
+
return this.socketChannel.publish(JSON.stringify({
|
|
31
|
+
receiptId: receiptId,
|
|
32
|
+
originalTime: timestamp,
|
|
33
|
+
departureTime: Date.now(),
|
|
34
|
+
context: context,
|
|
35
|
+
payload: payload,
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
async createSocketChannel(token) {
|
|
39
|
+
const endpoint = new URL(this.trackerEndpointUrl);
|
|
40
|
+
endpoint.searchParams.append(this.cidParameter, await this.cidAssigner.assignCid());
|
|
41
|
+
if (token !== undefined) {
|
|
42
|
+
endpoint.searchParams.append(this.tokenParameter, token);
|
|
43
|
+
}
|
|
44
|
+
const channel = this.socketFactory(endpoint.toString(), this.loggerFactory(`WebSocket#${this.connectionIndex}`));
|
|
45
|
+
this.connectionIndex += 1;
|
|
46
|
+
channel.subscribe(this.notify);
|
|
47
|
+
return channel;
|
|
48
|
+
}
|
|
49
|
+
subscribe(listener) {
|
|
78
50
|
if (!this.listeners.includes(listener)) {
|
|
79
51
|
this.listeners.push(listener);
|
|
80
52
|
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
53
|
+
}
|
|
54
|
+
unsubscribe(listener) {
|
|
55
|
+
const index = this.listeners.indexOf(listener);
|
|
84
56
|
if (index >= 0) {
|
|
85
57
|
this.listeners.splice(index, 1);
|
|
86
58
|
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
var confirmation;
|
|
59
|
+
}
|
|
60
|
+
notify(message) {
|
|
61
|
+
let confirmation;
|
|
91
62
|
try {
|
|
92
63
|
confirmation = JSON.parse(message);
|
|
93
64
|
}
|
|
94
|
-
catch
|
|
65
|
+
catch {
|
|
95
66
|
this.logger.error('Invalid JSON message received.');
|
|
96
67
|
return;
|
|
97
68
|
}
|
|
98
|
-
|
|
99
|
-
violations.forEach(
|
|
69
|
+
const { violations = [], receiptId } = confirmation;
|
|
70
|
+
violations.forEach(violation => this.logger.error(violation.message));
|
|
100
71
|
if (receiptId !== null) {
|
|
101
|
-
this.listeners.forEach(
|
|
72
|
+
this.listeners.forEach(dispatch => dispatch(receiptId));
|
|
102
73
|
}
|
|
103
|
-
}
|
|
104
|
-
|
|
74
|
+
}
|
|
75
|
+
close() {
|
|
105
76
|
if (this.socketChannel === undefined) {
|
|
106
77
|
return Promise.resolve();
|
|
107
78
|
}
|
|
108
79
|
return this.socketChannel.close();
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
}());
|
|
80
|
+
}
|
|
81
|
+
}
|
|
112
82
|
exports.BeaconSocketChannel = BeaconSocketChannel;
|
package/channel/channel.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export interface Closeable {
|
|
|
4
4
|
export interface OutputChannel<O> extends Closeable {
|
|
5
5
|
publish(message: O): Promise<void>;
|
|
6
6
|
}
|
|
7
|
-
export
|
|
7
|
+
export type ChannelListener<T> = {
|
|
8
8
|
(message: T): void;
|
|
9
9
|
};
|
|
10
10
|
export interface InputChannel<I> extends Closeable {
|
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EncodedChannel = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
class EncodedChannel {
|
|
5
|
+
constructor(channel, encoder) {
|
|
6
6
|
this.channel = channel;
|
|
7
7
|
this.encode = encoder;
|
|
8
8
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
EncodedChannel.prototype.close = function () {
|
|
9
|
+
publish(message) {
|
|
10
|
+
return this.encode(message).then((result => this.channel.publish(result)));
|
|
11
|
+
}
|
|
12
|
+
close() {
|
|
14
13
|
return this.channel.close();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
}());
|
|
14
|
+
}
|
|
15
|
+
}
|
|
18
16
|
exports.EncodedChannel = EncodedChannel;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { Logger } from '../logging';
|
|
2
2
|
import { DuplexChannel, OutputChannel } from './channel';
|
|
3
|
-
export
|
|
3
|
+
export type MessageStamper<M, S> = {
|
|
4
4
|
generate(message: M): S;
|
|
5
5
|
};
|
|
6
6
|
export declare class TimeStamper implements MessageStamper<any, string> {
|
|
7
7
|
generate(): string;
|
|
8
8
|
}
|
|
9
|
-
export
|
|
9
|
+
export type Envelope<M, S> = {
|
|
10
10
|
id: S;
|
|
11
11
|
message: M;
|
|
12
12
|
};
|
|
13
|
-
|
|
13
|
+
type Options = {
|
|
14
14
|
ackTimeout: number;
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
type Configuration<M, S> = Partial<Options> & {
|
|
17
17
|
channel: DuplexChannel<S, Envelope<M, S>>;
|
|
18
18
|
stamper: MessageStamper<M, S>;
|
|
19
19
|
logger?: Logger;
|