@amplitude/analytics-core 0.5.0 → 0.6.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/lib/cjs/config.d.ts +1 -3
- package/lib/cjs/config.d.ts.map +1 -1
- package/lib/cjs/config.js +0 -2
- package/lib/cjs/config.js.map +1 -1
- package/lib/cjs/core-client.d.ts +2 -0
- package/lib/cjs/core-client.d.ts.map +1 -1
- package/lib/cjs/core-client.js +5 -7
- package/lib/cjs/core-client.js.map +1 -1
- package/lib/cjs/plugins/destination.d.ts +11 -9
- package/lib/cjs/plugins/destination.d.ts.map +1 -1
- package/lib/cjs/plugins/destination.js +63 -69
- package/lib/cjs/plugins/destination.js.map +1 -1
- package/lib/cjs/storage/memory.d.ts +6 -6
- package/lib/cjs/storage/memory.d.ts.map +1 -1
- package/lib/cjs/storage/memory.js +40 -7
- package/lib/cjs/storage/memory.js.map +1 -1
- package/lib/cjs/timeline.d.ts +12 -7
- package/lib/cjs/timeline.d.ts.map +1 -1
- package/lib/cjs/timeline.js +154 -138
- package/lib/cjs/timeline.js.map +1 -1
- package/lib/cjs/utils/return-wrapper.d.ts +1 -1
- package/lib/cjs/utils/return-wrapper.d.ts.map +1 -1
- package/lib/cjs/utils/return-wrapper.js.map +1 -1
- package/lib/cjs/utils/valid-properties.d.ts.map +1 -1
- package/lib/cjs/utils/valid-properties.js +3 -0
- package/lib/cjs/utils/valid-properties.js.map +1 -1
- package/lib/esm/config.d.ts +1 -3
- package/lib/esm/config.d.ts.map +1 -1
- package/lib/esm/config.js +0 -2
- package/lib/esm/config.js.map +1 -1
- package/lib/esm/core-client.d.ts +2 -0
- package/lib/esm/core-client.d.ts.map +1 -1
- package/lib/esm/core-client.js +6 -8
- package/lib/esm/core-client.js.map +1 -1
- package/lib/esm/plugins/destination.d.ts +11 -9
- package/lib/esm/plugins/destination.d.ts.map +1 -1
- package/lib/esm/plugins/destination.js +63 -69
- package/lib/esm/plugins/destination.js.map +1 -1
- package/lib/esm/storage/memory.d.ts +6 -6
- package/lib/esm/storage/memory.d.ts.map +1 -1
- package/lib/esm/storage/memory.js +40 -7
- package/lib/esm/storage/memory.js.map +1 -1
- package/lib/esm/timeline.d.ts +12 -7
- package/lib/esm/timeline.d.ts.map +1 -1
- package/lib/esm/timeline.js +153 -130
- package/lib/esm/timeline.js.map +1 -1
- package/lib/esm/utils/return-wrapper.d.ts +1 -1
- package/lib/esm/utils/return-wrapper.d.ts.map +1 -1
- package/lib/esm/utils/return-wrapper.js.map +1 -1
- package/lib/esm/utils/valid-properties.d.ts.map +1 -1
- package/lib/esm/utils/valid-properties.js +3 -0
- package/lib/esm/utils/valid-properties.js.map +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core-client.js","sourceRoot":"","sources":["../../src/core-client.ts"],"names":[],"mappings":";AAUA,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"core-client.js","sourceRoot":"","sources":["../../src/core-client.ts"],"names":[],"mappings":";AAUA,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD;IASE,uBAAY,IAAiB;QAAjB,qBAAA,EAAA,iBAAiB;QAgB7B,aAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAf/B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,gDAAgD;IAChD,4BAAI,GAAJ,UAAK,OAA2B,EAAE,OAA2B,EAAE,MAAS;QACtE,IAAI,CAAC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CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAAC;;;KAC/C;IAEK,8BAAM,GAAZ,UAAa,UAAkB;;;gBAC7B,sBAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,UAAU,CAAC,EAAC;;;KAC7C;IAEK,gCAAQ,GAAd,UAAe,KAAY;;;;;;;wBAER,qBAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EAAA;;wBAArD,MAAM,GAAG,SAA4C;wBAC3D,IAAI,MAAM,CAAC,IAAI,KAAK,GAAG,EAAE;4BACvB,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;yBAChD;6BAAM;4BACL,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;yBAClD;wBACD,sBAAO,MAAM,EAAC;;;wBAER,OAAO,GAAG,MAAM,CAAC,GAAC,CAAC,CAAC;wBAC1B,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;wBAC1C,sBAAO,WAAW,CAAC,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAC;;;;;KAEzC;IAED,iCAAS,GAAT,UAAU,MAAe;QACvB,IAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC3B,MAAM,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC;IAED,6BAAK,GAAL;QACE,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IAC/B,CAAC;IACH,oBAAC;AAAD,CAAC,AAhFD,IAgFC","sourcesContent":["import {\n CoreClient,\n Config,\n Event,\n BaseEvent,\n EventOptions,\n Identify,\n Plugin,\n Revenue,\n} from '@amplitude/analytics-types';\nimport {\n createGroupIdentifyEvent,\n createIdentifyEvent,\n createTrackEvent,\n createRevenueEvent,\n createGroupEvent,\n} from './utils/event-builder';\nimport { Timeline } from './timeline';\nimport { buildResult } from './utils/result-builder';\nexport class AmplitudeCore<T extends Config> implements CoreClient<T> {\n name: string;\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n config: T;\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n timeline: Timeline;\n\n constructor(name = '$default') {\n this.name = name;\n }\n\n // NOTE: Do not use `_apiKey` and `_userId` here\n init(_apiKey: string | undefined, _userId: string | undefined, config: T) {\n this.config = config;\n this.timeline = new Timeline();\n return Promise.resolve();\n }\n\n track(eventInput: BaseEvent | string, eventProperties?: Record<string, any>, eventOptions?: EventOptions) {\n const event = createTrackEvent(eventInput, eventProperties, eventOptions);\n return this.dispatch(event);\n }\n\n logEvent = this.track.bind(this);\n\n identify(identify: Identify, eventOptions?: EventOptions) {\n const event = createIdentifyEvent(identify, eventOptions);\n return this.dispatch(event);\n }\n\n groupIdentify(groupType: string, groupName: string | string[], identify: Identify, eventOptions?: EventOptions) {\n const event = createGroupIdentifyEvent(groupType, groupName, identify, eventOptions);\n return this.dispatch(event);\n }\n\n setGroup(groupType: string, groupName: string | string[]) {\n const event = createGroupEvent(groupType, groupName);\n return this.dispatch(event);\n }\n\n revenue(revenue: Revenue, eventOptions?: EventOptions) {\n const event = createRevenueEvent(revenue, eventOptions);\n return this.dispatch(event);\n }\n\n async add(plugin: Plugin) {\n const config = this.config;\n return this.timeline.register(plugin, config);\n }\n\n async remove(pluginName: string) {\n return this.timeline.deregister(pluginName);\n }\n\n async dispatch(event: Event) {\n try {\n const result = await this.timeline.push(event, this.config);\n if (result.code === 200) {\n this.config.loggerProvider.log(result.message);\n } else {\n this.config.loggerProvider.error(result.message);\n }\n return result;\n } catch (e) {\n const message = String(e);\n this.config.loggerProvider.error(message);\n return buildResult(event, 0, message);\n }\n }\n\n setOptOut(optOut: boolean) {\n const config = this.config;\n config.optOut = Boolean(optOut);\n }\n\n flush() {\n return this.timeline.flush();\n }\n}\n"]}
|
|
@@ -2,20 +2,18 @@ import { Config, DestinationContext as Context, DestinationPlugin, Event, Invali
|
|
|
2
2
|
export declare class Destination implements DestinationPlugin {
|
|
3
3
|
name: string;
|
|
4
4
|
type: PluginType.DESTINATION;
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
retryTimeout: number;
|
|
6
|
+
throttleTimeout: number;
|
|
7
7
|
storageKey: string;
|
|
8
|
-
backup: Set<Event>;
|
|
9
8
|
config: Config;
|
|
10
9
|
scheduled: boolean;
|
|
11
10
|
queue: Context[];
|
|
12
|
-
queueBuffer: Set<Context>;
|
|
13
11
|
setup(config: Config): Promise<undefined>;
|
|
14
12
|
execute(event: Event): Promise<Result>;
|
|
15
13
|
addToQueue(...list: Context[]): void;
|
|
16
14
|
schedule(timeout: number): void;
|
|
17
|
-
flush(
|
|
18
|
-
send(list: Context[]): Promise<void>;
|
|
15
|
+
flush(useRetry?: boolean): Promise<void>;
|
|
16
|
+
send(list: Context[], useRetry?: boolean): Promise<void>;
|
|
19
17
|
handleReponse(res: Response, list: Context[]): void;
|
|
20
18
|
handleSuccessResponse(res: SuccessResponse, list: Context[]): void;
|
|
21
19
|
handleInvalidResponse(res: InvalidResponse, list: Context[]): void;
|
|
@@ -23,8 +21,12 @@ export declare class Destination implements DestinationPlugin {
|
|
|
23
21
|
handleRateLimitResponse(res: RateLimitResponse, list: Context[]): void;
|
|
24
22
|
handleOtherReponse(list: Context[]): void;
|
|
25
23
|
fulfillRequest(list: Context[], code: number, message: string): void;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Saves events to storage
|
|
26
|
+
* This is called on
|
|
27
|
+
* 1) new events are added to queue; or
|
|
28
|
+
* 2) response comes back for a request
|
|
29
|
+
*/
|
|
30
|
+
saveEvents(): void;
|
|
29
31
|
}
|
|
30
32
|
//# sourceMappingURL=destination.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"destination.d.ts","sourceRoot":"","sources":["../../../src/plugins/destination.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,kBAAkB,IAAI,OAAO,EAC7B,iBAAiB,EACjB,KAAK,EACL,eAAe,EACf,uBAAuB,EACvB,UAAU,EACV,iBAAiB,EACjB,QAAQ,EACR,MAAM,EAEN,eAAe,EAChB,MAAM,4BAA4B,CAAC;AAOpC,qBAAa,WAAY,YAAW,iBAAiB;IACnD,IAAI,SAAe;IACnB,IAAI,yBAAmC;IAEvC,
|
|
1
|
+
{"version":3,"file":"destination.d.ts","sourceRoot":"","sources":["../../../src/plugins/destination.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,kBAAkB,IAAI,OAAO,EAC7B,iBAAiB,EACjB,KAAK,EACL,eAAe,EACf,uBAAuB,EACvB,UAAU,EACV,iBAAiB,EACjB,QAAQ,EACR,MAAM,EAEN,eAAe,EAChB,MAAM,4BAA4B,CAAC;AAOpC,qBAAa,WAAY,YAAW,iBAAiB;IACnD,IAAI,SAAe;IACnB,IAAI,yBAAmC;IAEvC,YAAY,SAAQ;IACpB,eAAe,SAAS;IACxB,UAAU,SAAM;IAIhB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,UAAS;IAClB,KAAK,EAAE,OAAO,EAAE,CAAM;IAEhB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAa/C,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC;IAYtC,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE;IA0B7B,QAAQ,CAAC,OAAO,EAAE,MAAM;IAalB,KAAK,CAAC,QAAQ,UAAQ;IAUtB,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,QAAQ,UAAO;IA8B3C,aAAa,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE;IAwB5C,qBAAqB,CAAC,GAAG,EAAE,eAAe,EAAE,IAAI,EAAE,OAAO,EAAE;IAI3D,qBAAqB,CAAC,GAAG,EAAE,eAAe,EAAE,IAAI,EAAE,OAAO,EAAE;IAyB3D,6BAA6B,CAAC,GAAG,EAAE,uBAAuB,EAAE,IAAI,EAAE,OAAO,EAAE;IAS3E,uBAAuB,CAAC,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,OAAO,EAAE;IAyB/D,kBAAkB,CAAC,IAAI,EAAE,OAAO,EAAE;IASlC,cAAc,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAK7D;;;;;OAKG;IACH,UAAU;CAOX"}
|
|
@@ -9,24 +9,32 @@ var Destination = /** @class */ (function () {
|
|
|
9
9
|
function Destination() {
|
|
10
10
|
this.name = 'amplitude';
|
|
11
11
|
this.type = PluginType.DESTINATION;
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
12
|
+
this.retryTimeout = 1000;
|
|
13
|
+
this.throttleTimeout = 30000;
|
|
14
14
|
this.storageKey = '';
|
|
15
|
-
this.backup = new Set();
|
|
16
15
|
this.scheduled = false;
|
|
17
16
|
this.queue = [];
|
|
18
|
-
this.queueBuffer = new Set();
|
|
19
17
|
}
|
|
20
18
|
Destination.prototype.setup = function (config) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
20
|
+
var unsent;
|
|
21
|
+
var _this = this;
|
|
22
|
+
return __generator(this, function (_a) {
|
|
23
|
+
switch (_a.label) {
|
|
24
|
+
case 0:
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.storageKey = "".concat(STORAGE_PREFIX, "_").concat(this.config.apiKey.substring(0, 10));
|
|
27
|
+
return [4 /*yield*/, this.config.storageProvider.get(this.storageKey)];
|
|
28
|
+
case 1:
|
|
29
|
+
unsent = _a.sent();
|
|
30
|
+
this.saveEvents(); // sets storage to '[]'
|
|
31
|
+
if (unsent && unsent.length > 0) {
|
|
32
|
+
void Promise.all(unsent.map(function (event) { return _this.execute(event); })).catch();
|
|
33
|
+
}
|
|
34
|
+
return [2 /*return*/, Promise.resolve(undefined)];
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
30
38
|
};
|
|
31
39
|
Destination.prototype.execute = function (event) {
|
|
32
40
|
var _this = this;
|
|
@@ -35,9 +43,9 @@ var Destination = /** @class */ (function () {
|
|
|
35
43
|
event: event,
|
|
36
44
|
attempts: 0,
|
|
37
45
|
callback: function (result) { return resolve(result); },
|
|
38
|
-
|
|
46
|
+
timeout: 0,
|
|
39
47
|
};
|
|
40
|
-
_this.addToQueue(context);
|
|
48
|
+
void _this.addToQueue(context);
|
|
41
49
|
});
|
|
42
50
|
};
|
|
43
51
|
Destination.prototype.addToQueue = function () {
|
|
@@ -48,30 +56,24 @@ var Destination = /** @class */ (function () {
|
|
|
48
56
|
}
|
|
49
57
|
var tryable = list.filter(function (context) {
|
|
50
58
|
if (context.attempts < _this.config.flushMaxRetries) {
|
|
59
|
+
context.attempts += 1;
|
|
51
60
|
return true;
|
|
52
61
|
}
|
|
53
|
-
_this.fulfillRequest([context], 500, Status.Unknown);
|
|
62
|
+
void _this.fulfillRequest([context], 500, Status.Unknown);
|
|
54
63
|
return false;
|
|
55
64
|
});
|
|
56
|
-
this.addToBackup.apply(this, __spreadArray([], __read(tryable.map(function (context) { return context.event; })), false));
|
|
57
65
|
tryable.forEach(function (context) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
context.delay = 0;
|
|
61
|
-
if (context.attempts === 1 && !context.delay) {
|
|
62
|
-
_this.queue = _this.queue.concat(context);
|
|
66
|
+
_this.queue = _this.queue.concat(context);
|
|
67
|
+
if (context.timeout === 0) {
|
|
63
68
|
_this.schedule(_this.config.flushIntervalMillis);
|
|
64
69
|
return;
|
|
65
70
|
}
|
|
66
|
-
_this.queueBuffer.add(context);
|
|
67
71
|
setTimeout(function () {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
_this.schedule(_this.config.flushIntervalMillis);
|
|
72
|
-
}
|
|
73
|
-
}, delay || context.attempts * _this.backoff);
|
|
72
|
+
context.timeout = 0;
|
|
73
|
+
_this.schedule(0);
|
|
74
|
+
}, context.timeout);
|
|
74
75
|
});
|
|
76
|
+
this.saveEvents();
|
|
75
77
|
};
|
|
76
78
|
Destination.prototype.schedule = function (timeout) {
|
|
77
79
|
var _this = this;
|
|
@@ -79,7 +81,7 @@ var Destination = /** @class */ (function () {
|
|
|
79
81
|
return;
|
|
80
82
|
this.scheduled = true;
|
|
81
83
|
setTimeout(function () {
|
|
82
|
-
void _this.flush().then(function () {
|
|
84
|
+
void _this.flush(true).then(function () {
|
|
83
85
|
_this.scheduled = false;
|
|
84
86
|
if (_this.queue.length > 0) {
|
|
85
87
|
_this.schedule(timeout);
|
|
@@ -87,22 +89,20 @@ var Destination = /** @class */ (function () {
|
|
|
87
89
|
});
|
|
88
90
|
}, timeout);
|
|
89
91
|
};
|
|
90
|
-
Destination.prototype.flush = function (
|
|
91
|
-
if (
|
|
92
|
+
Destination.prototype.flush = function (useRetry) {
|
|
93
|
+
if (useRetry === void 0) { useRetry = false; }
|
|
92
94
|
return __awaiter(this, void 0, void 0, function () {
|
|
93
|
-
var list, batches;
|
|
95
|
+
var list, later, batches;
|
|
94
96
|
var _this = this;
|
|
95
97
|
return __generator(this, function (_a) {
|
|
96
98
|
switch (_a.label) {
|
|
97
99
|
case 0:
|
|
98
|
-
list =
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this.queueBuffer.clear();
|
|
103
|
-
}
|
|
100
|
+
list = [];
|
|
101
|
+
later = [];
|
|
102
|
+
this.queue.forEach(function (context) { return (context.timeout === 0 ? list.push(context) : later.push(context)); });
|
|
103
|
+
this.queue = later;
|
|
104
104
|
batches = chunk(list, this.config.flushQueueSize);
|
|
105
|
-
return [4 /*yield*/, Promise.all(batches.map(function (batch) { return _this.send(batch); }))];
|
|
105
|
+
return [4 /*yield*/, Promise.all(batches.map(function (batch) { return _this.send(batch, useRetry); }))];
|
|
106
106
|
case 1:
|
|
107
107
|
_a.sent();
|
|
108
108
|
return [2 /*return*/];
|
|
@@ -110,7 +110,8 @@ var Destination = /** @class */ (function () {
|
|
|
110
110
|
});
|
|
111
111
|
});
|
|
112
112
|
};
|
|
113
|
-
Destination.prototype.send = function (list) {
|
|
113
|
+
Destination.prototype.send = function (list, useRetry) {
|
|
114
|
+
if (useRetry === void 0) { useRetry = true; }
|
|
114
115
|
return __awaiter(this, void 0, void 0, function () {
|
|
115
116
|
var payload, serverUrl, res, e_1;
|
|
116
117
|
return __generator(this, function (_a) {
|
|
@@ -137,6 +138,10 @@ var Destination = /** @class */ (function () {
|
|
|
137
138
|
this.fulfillRequest(list, 0, UNEXPECTED_ERROR_MESSAGE);
|
|
138
139
|
return [2 /*return*/];
|
|
139
140
|
}
|
|
141
|
+
if (!useRetry) {
|
|
142
|
+
this.fulfillRequest(list, res.statusCode, res.status);
|
|
143
|
+
return [2 /*return*/];
|
|
144
|
+
}
|
|
140
145
|
this.handleReponse(res, list);
|
|
141
146
|
return [3 /*break*/, 4];
|
|
142
147
|
case 3:
|
|
@@ -210,46 +215,35 @@ var Destination = /** @class */ (function () {
|
|
|
210
215
|
return;
|
|
211
216
|
}
|
|
212
217
|
if (throttledIndexSet.has(index)) {
|
|
213
|
-
context.
|
|
218
|
+
context.timeout = _this.throttleTimeout;
|
|
214
219
|
}
|
|
215
220
|
return true;
|
|
216
221
|
});
|
|
217
222
|
this.addToQueue.apply(this, __spreadArray([], __read(retry), false));
|
|
218
223
|
};
|
|
219
224
|
Destination.prototype.handleOtherReponse = function (list) {
|
|
220
|
-
|
|
225
|
+
var _this = this;
|
|
226
|
+
this.addToQueue.apply(this, __spreadArray([], __read(list.map(function (context) {
|
|
227
|
+
context.timeout = context.attempts * _this.retryTimeout;
|
|
228
|
+
return context;
|
|
229
|
+
})), false));
|
|
221
230
|
};
|
|
222
231
|
Destination.prototype.fulfillRequest = function (list, code, message) {
|
|
223
|
-
this.
|
|
232
|
+
this.saveEvents();
|
|
224
233
|
list.forEach(function (context) { return context.callback(buildResult(context.event, code, message)); });
|
|
225
234
|
};
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
235
|
+
/**
|
|
236
|
+
* Saves events to storage
|
|
237
|
+
* This is called on
|
|
238
|
+
* 1) new events are added to queue; or
|
|
239
|
+
* 2) response comes back for a request
|
|
240
|
+
*/
|
|
241
|
+
Destination.prototype.saveEvents = function () {
|
|
242
|
+
if (!this.config.saveEvents) {
|
|
233
243
|
return;
|
|
234
|
-
events.forEach(function (event) { return _this.backup.add(event); });
|
|
235
|
-
this.snapshot();
|
|
236
|
-
};
|
|
237
|
-
Destination.prototype.removeFromBackup = function () {
|
|
238
|
-
var _this = this;
|
|
239
|
-
var events = [];
|
|
240
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
241
|
-
events[_i] = arguments[_i];
|
|
242
244
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
events.forEach(function (event) { return _this.backup.delete(event); });
|
|
246
|
-
this.snapshot();
|
|
247
|
-
};
|
|
248
|
-
Destination.prototype.snapshot = function () {
|
|
249
|
-
if (!this.config.saveEvents)
|
|
250
|
-
return;
|
|
251
|
-
var events = Array.from(this.backup);
|
|
252
|
-
this.config.storageProvider.set(this.storageKey, events);
|
|
245
|
+
var events = Array.from(this.queue.map(function (context) { return context.event; }));
|
|
246
|
+
void this.config.storageProvider.set(this.storageKey, events);
|
|
253
247
|
};
|
|
254
248
|
return Destination;
|
|
255
249
|
}());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"destination.js","sourceRoot":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CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAC3D,CAAC;IACH,kBAAC;AAAD,CAAC,AA3OD,IA2OC","sourcesContent":["import {\n Config,\n DestinationContext as Context,\n DestinationPlugin,\n Event,\n InvalidResponse,\n PayloadTooLargeResponse,\n PluginType,\n RateLimitResponse,\n Response,\n Result,\n Status,\n SuccessResponse,\n} from '@amplitude/analytics-types';\nimport { MISSING_API_KEY_MESSAGE, SUCCESS_MESSAGE, UNEXPECTED_ERROR_MESSAGE } from '../messages';\nimport { STORAGE_PREFIX } from '../constants';\nimport { chunk } from '../utils/chunk';\nimport { buildResult } from '../utils/result-builder';\nimport { createServerConfig } from '../config';\n\nexport class Destination implements DestinationPlugin {\n name = 'amplitude';\n type = PluginType.DESTINATION as const;\n\n backoff = 1000;\n throttle = 30000;\n storageKey = '';\n backup: Set<Event> = new Set();\n // this.config is defined in setup() which will always be called first\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n config: Config;\n scheduled = false;\n queue: Context[] = [];\n queueBuffer: Set<Context> = new Set();\n\n setup(config: Config) {\n this.config = config;\n\n this.storageKey = `${STORAGE_PREFIX}_${this.config.apiKey.substring(0, 10)}`;\n const unsent = this.config.storageProvider.get(this.storageKey);\n this.snapshot(); // sets storage to '[]'\n if (unsent && unsent.length > 0) {\n void Promise.all(unsent.map((event) => this.execute(event))).catch();\n }\n\n return Promise.resolve(undefined);\n }\n\n execute(event: Event): Promise<Result> {\n return new Promise((resolve) => {\n const context = {\n event,\n attempts: 0,\n callback: (result: Result) => resolve(result),\n delay: 0,\n };\n this.addToQueue(context);\n });\n }\n\n addToQueue(...list: Context[]) {\n const tryable = list.filter((context) => {\n if (context.attempts < this.config.flushMaxRetries) {\n return true;\n }\n this.fulfillRequest([context], 500, Status.Unknown);\n return false;\n });\n\n this.addToBackup(...tryable.map((context) => context.event));\n tryable.forEach((context) => {\n context.attempts += 1;\n const delay = context.delay;\n context.delay = 0;\n if (context.attempts === 1 && !context.delay) {\n this.queue = this.queue.concat(context);\n this.schedule(this.config.flushIntervalMillis);\n return;\n }\n this.queueBuffer.add(context);\n setTimeout(() => {\n if (this.queueBuffer.has(context)) {\n this.queueBuffer.delete(context);\n this.queue = this.queue.concat(context);\n this.schedule(this.config.flushIntervalMillis);\n }\n }, delay || context.attempts * this.backoff);\n });\n }\n\n schedule(timeout: number) {\n if (this.scheduled) return;\n this.scheduled = true;\n setTimeout(() => {\n void this.flush().then(() => {\n this.scheduled = false;\n if (this.queue.length > 0) {\n this.schedule(timeout);\n }\n });\n }, timeout);\n }\n\n async flush(includeQueueBuffer = false) {\n const list = this.queue;\n this.queue = [];\n\n if (includeQueueBuffer) {\n list.push(...this.queueBuffer.values());\n this.queueBuffer.clear();\n }\n const batches = chunk(list, this.config.flushQueueSize);\n await Promise.all(batches.map((batch) => this.send(batch)));\n }\n\n async send(list: Context[]) {\n if (!this.config.apiKey) {\n return this.fulfillRequest(list, 400, MISSING_API_KEY_MESSAGE);\n }\n\n const payload = {\n api_key: this.config.apiKey,\n events: list.map((context) => context.event),\n options: {\n min_id_length: this.config.minIdLength,\n },\n };\n\n try {\n const { serverUrl } = createServerConfig(this.config.serverUrl, this.config.serverZone, this.config.useBatch);\n const res = await this.config.transportProvider.send(serverUrl, payload);\n if (res === null) {\n this.fulfillRequest(list, 0, UNEXPECTED_ERROR_MESSAGE);\n return;\n }\n this.handleReponse(res, list);\n } catch (e) {\n this.fulfillRequest(list, 0, String(e));\n }\n }\n\n handleReponse(res: Response, list: Context[]) {\n const { status } = res;\n switch (status) {\n case Status.Success:\n this.handleSuccessResponse(res, list);\n break;\n\n case Status.Invalid:\n this.handleInvalidResponse(res, list);\n break;\n\n case Status.PayloadTooLarge:\n this.handlePayloadTooLargeResponse(res, list);\n break;\n\n case Status.RateLimit:\n this.handleRateLimitResponse(res, list);\n break;\n\n default:\n this.handleOtherReponse(list);\n }\n }\n\n handleSuccessResponse(res: SuccessResponse, list: Context[]) {\n this.fulfillRequest(list, res.statusCode, SUCCESS_MESSAGE);\n }\n\n handleInvalidResponse(res: InvalidResponse, list: Context[]) {\n if (res.body.missingField) {\n this.fulfillRequest(list, res.statusCode, res.body.error);\n return;\n }\n\n const dropIndex = [\n ...Object.values(res.body.eventsWithInvalidFields),\n ...Object.values(res.body.eventsWithMissingFields),\n ...Object.values(res.body.eventsWithInvalidIdLengths),\n ...res.body.silencedEvents,\n ].flat();\n const dropIndexSet = new Set(dropIndex);\n\n const retry = list.filter((context, index) => {\n if (dropIndexSet.has(index)) {\n this.fulfillRequest([context], res.statusCode, res.body.error);\n return;\n }\n return true;\n });\n\n this.addToQueue(...retry);\n }\n\n handlePayloadTooLargeResponse(res: PayloadTooLargeResponse, list: Context[]) {\n if (list.length === 1) {\n this.fulfillRequest(list, res.statusCode, res.body.error);\n return;\n }\n this.config.flushQueueSize /= 2;\n this.addToQueue(...list);\n }\n\n handleRateLimitResponse(res: RateLimitResponse, list: Context[]) {\n const dropUserIds = Object.keys(res.body.exceededDailyQuotaUsers);\n const dropDeviceIds = Object.keys(res.body.exceededDailyQuotaDevices);\n const throttledIndex = res.body.throttledEvents;\n const dropUserIdsSet = new Set(dropUserIds);\n const dropDeviceIdsSet = new Set(dropDeviceIds);\n const throttledIndexSet = new Set(throttledIndex);\n\n const retry = list.filter((context, index) => {\n if (\n (context.event.user_id && dropUserIdsSet.has(context.event.user_id)) ||\n (context.event.device_id && dropDeviceIdsSet.has(context.event.device_id))\n ) {\n this.fulfillRequest([context], res.statusCode, res.body.error);\n return;\n }\n if (throttledIndexSet.has(index)) {\n context.delay = this.throttle;\n }\n return true;\n });\n\n this.addToQueue(...retry);\n }\n\n handleOtherReponse(list: Context[]) {\n this.addToQueue(...list);\n }\n\n fulfillRequest(list: Context[], code: number, message: string) {\n this.removeFromBackup(...list.map((context) => context.event));\n list.forEach((context) => context.callback(buildResult(context.event, code, message)));\n }\n\n addToBackup(...events: Event[]) {\n if (!this.config.saveEvents) return;\n events.forEach((event) => this.backup.add(event));\n this.snapshot();\n }\n\n removeFromBackup(...events: Event[]) {\n if (!this.config.saveEvents) return;\n events.forEach((event) => this.backup.delete(event));\n this.snapshot();\n }\n\n snapshot() {\n if (!this.config.saveEvents) return;\n const events = Array.from(this.backup);\n this.config.storageProvider.set(this.storageKey, events);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"destination.js","sourceRoot":"","sources":["../../../src/plugins/destination.ts"],"names":[],"mappings":";AAAA,OAAO,EAOL,UAAU,EAIV,MAAM,GAEP,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,uBAAuB,EAAE,eAAe,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AACjG,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAE/C;IAAA;QACE,SAAI,GAAG,WAAW,CAAC;QACnB,SAAI,GAAG,UAAU,CAAC,WAAoB,CAAC;QAEvC,iBAAY,GAAG,IAAI,CAAC;QACpB,oBAAe,GAAG,KAAK,CAAC;QACxB,eAAU,GAAG,EAAE,CAAC;QAKhB,cAAS,GAAG,KAAK,CAAC;QAClB,UAAK,GAAc,EAAE,CAAC;IA4NxB,CAAC;IA1NO,2BAAK,GAAX,UAAY,MAAc;;;;;;;wBACxB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;wBAErB,IAAI,CAAC,UAAU,GAAG,UAAG,cAAc,cAAI,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAE,CAAC;wBAC9D,qBAAM,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,C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,IAAe;QAAlC,iBAOC;QANC,IAAI,CAAC,UAAU,OAAf,IAAI,2BACC,IAAI,CAAC,GAAG,CAAC,UAAC,OAAO;YAClB,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,QAAQ,GAAG,KAAI,CAAC,YAAY,CAAC;YACvD,OAAO,OAAO,CAAC;QACjB,CAAC,CAAC,WACF;IACJ,CAAC;IAED,oCAAc,GAAd,UAAe,IAAe,EAAE,IAAY,EAAE,OAAe;QAC3D,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,IAAI,CAAC,OAAO,CAAC,UAAC,OAAO,IAAK,OAAA,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,EAA3D,CAA2D,CAAC,CAAC;IACzF,CAAC;IAED;;;;;OAKG;IACH,gCAAU,GAAV;QACE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE;YAC3B,OAAO;SACR;QACD,IAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,UAAC,OAAO,IAAK,OAAA,OAAO,CAAC,KAAK,EAAb,CAAa,CAAC,CAAC,CAAC;QACtE,KAAK,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAChE,CAAC;IACH,kBAAC;AAAD,CAAC,AAxOD,IAwOC","sourcesContent":["import {\n Config,\n DestinationContext as Context,\n DestinationPlugin,\n Event,\n InvalidResponse,\n PayloadTooLargeResponse,\n PluginType,\n RateLimitResponse,\n Response,\n Result,\n Status,\n SuccessResponse,\n} from '@amplitude/analytics-types';\nimport { MISSING_API_KEY_MESSAGE, SUCCESS_MESSAGE, UNEXPECTED_ERROR_MESSAGE } from '../messages';\nimport { STORAGE_PREFIX } from '../constants';\nimport { chunk } from '../utils/chunk';\nimport { buildResult } from '../utils/result-builder';\nimport { createServerConfig } from '../config';\n\nexport class Destination implements DestinationPlugin {\n name = 'amplitude';\n type = PluginType.DESTINATION as const;\n\n retryTimeout = 1000;\n throttleTimeout = 30000;\n storageKey = '';\n // this.config is defined in setup() which will always be called first\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n config: Config;\n scheduled = false;\n queue: Context[] = [];\n\n async setup(config: Config): Promise<undefined> {\n this.config = config;\n\n this.storageKey = `${STORAGE_PREFIX}_${this.config.apiKey.substring(0, 10)}`;\n const unsent = await this.config.storageProvider.get(this.storageKey);\n this.saveEvents(); // sets storage to '[]'\n if (unsent && unsent.length > 0) {\n void Promise.all(unsent.map((event) => this.execute(event))).catch();\n }\n\n return Promise.resolve(undefined);\n }\n\n execute(event: Event): Promise<Result> {\n return new Promise((resolve) => {\n const context = {\n event,\n attempts: 0,\n callback: (result: Result) => resolve(result),\n timeout: 0,\n };\n void this.addToQueue(context);\n });\n }\n\n addToQueue(...list: Context[]) {\n const tryable = list.filter((context) => {\n if (context.attempts < this.config.flushMaxRetries) {\n context.attempts += 1;\n return true;\n }\n void this.fulfillRequest([context], 500, Status.Unknown);\n return false;\n });\n\n tryable.forEach((context) => {\n this.queue = this.queue.concat(context);\n if (context.timeout === 0) {\n this.schedule(this.config.flushIntervalMillis);\n return;\n }\n\n setTimeout(() => {\n context.timeout = 0;\n this.schedule(0);\n }, context.timeout);\n });\n\n this.saveEvents();\n }\n\n schedule(timeout: number) {\n if (this.scheduled) return;\n this.scheduled = true;\n setTimeout(() => {\n void this.flush(true).then(() => {\n this.scheduled = false;\n if (this.queue.length > 0) {\n this.schedule(timeout);\n }\n });\n }, timeout);\n }\n\n async flush(useRetry = false) {\n const list: Context[] = [];\n const later: Context[] = [];\n this.queue.forEach((context) => (context.timeout === 0 ? list.push(context) : later.push(context)));\n this.queue = later;\n\n const batches = chunk(list, this.config.flushQueueSize);\n await Promise.all(batches.map((batch) => this.send(batch, useRetry)));\n }\n\n async send(list: Context[], useRetry = true) {\n if (!this.config.apiKey) {\n return this.fulfillRequest(list, 400, MISSING_API_KEY_MESSAGE);\n }\n\n const payload = {\n api_key: this.config.apiKey,\n events: list.map((context) => context.event),\n options: {\n min_id_length: this.config.minIdLength,\n },\n };\n\n try {\n const { serverUrl } = createServerConfig(this.config.serverUrl, this.config.serverZone, this.config.useBatch);\n const res = await this.config.transportProvider.send(serverUrl, payload);\n if (res === null) {\n this.fulfillRequest(list, 0, UNEXPECTED_ERROR_MESSAGE);\n return;\n }\n if (!useRetry) {\n this.fulfillRequest(list, res.statusCode, res.status);\n return;\n }\n this.handleReponse(res, list);\n } catch (e) {\n this.fulfillRequest(list, 0, String(e));\n }\n }\n\n handleReponse(res: Response, list: Context[]) {\n const { status } = res;\n switch (status) {\n case Status.Success:\n this.handleSuccessResponse(res, list);\n break;\n\n case Status.Invalid:\n this.handleInvalidResponse(res, list);\n break;\n\n case Status.PayloadTooLarge:\n this.handlePayloadTooLargeResponse(res, list);\n break;\n\n case Status.RateLimit:\n this.handleRateLimitResponse(res, list);\n break;\n\n default:\n this.handleOtherReponse(list);\n }\n }\n\n handleSuccessResponse(res: SuccessResponse, list: Context[]) {\n this.fulfillRequest(list, res.statusCode, SUCCESS_MESSAGE);\n }\n\n handleInvalidResponse(res: InvalidResponse, list: Context[]) {\n if (res.body.missingField) {\n this.fulfillRequest(list, res.statusCode, res.body.error);\n return;\n }\n\n const dropIndex = [\n ...Object.values(res.body.eventsWithInvalidFields),\n ...Object.values(res.body.eventsWithMissingFields),\n ...Object.values(res.body.eventsWithInvalidIdLengths),\n ...res.body.silencedEvents,\n ].flat();\n const dropIndexSet = new Set(dropIndex);\n\n const retry = list.filter((context, index) => {\n if (dropIndexSet.has(index)) {\n this.fulfillRequest([context], res.statusCode, res.body.error);\n return;\n }\n return true;\n });\n\n this.addToQueue(...retry);\n }\n\n handlePayloadTooLargeResponse(res: PayloadTooLargeResponse, list: Context[]) {\n if (list.length === 1) {\n this.fulfillRequest(list, res.statusCode, res.body.error);\n return;\n }\n this.config.flushQueueSize /= 2;\n this.addToQueue(...list);\n }\n\n handleRateLimitResponse(res: RateLimitResponse, list: Context[]) {\n const dropUserIds = Object.keys(res.body.exceededDailyQuotaUsers);\n const dropDeviceIds = Object.keys(res.body.exceededDailyQuotaDevices);\n const throttledIndex = res.body.throttledEvents;\n const dropUserIdsSet = new Set(dropUserIds);\n const dropDeviceIdsSet = new Set(dropDeviceIds);\n const throttledIndexSet = new Set(throttledIndex);\n\n const retry = list.filter((context, index) => {\n if (\n (context.event.user_id && dropUserIdsSet.has(context.event.user_id)) ||\n (context.event.device_id && dropDeviceIdsSet.has(context.event.device_id))\n ) {\n this.fulfillRequest([context], res.statusCode, res.body.error);\n return;\n }\n if (throttledIndexSet.has(index)) {\n context.timeout = this.throttleTimeout;\n }\n return true;\n });\n\n this.addToQueue(...retry);\n }\n\n handleOtherReponse(list: Context[]) {\n this.addToQueue(\n ...list.map((context) => {\n context.timeout = context.attempts * this.retryTimeout;\n return context;\n }),\n );\n }\n\n fulfillRequest(list: Context[], code: number, message: string) {\n this.saveEvents();\n list.forEach((context) => context.callback(buildResult(context.event, code, message)));\n }\n\n /**\n * Saves events to storage\n * This is called on\n * 1) new events are added to queue; or\n * 2) response comes back for a request\n */\n saveEvents() {\n if (!this.config.saveEvents) {\n return;\n }\n const events = Array.from(this.queue.map((context) => context.event));\n void this.config.storageProvider.set(this.storageKey, events);\n }\n}\n"]}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Storage } from '@amplitude/analytics-types';
|
|
2
2
|
export declare class MemoryStorage<T> implements Storage<T> {
|
|
3
3
|
memoryStorage: Map<string, T>;
|
|
4
|
-
isEnabled(): boolean
|
|
5
|
-
get(key: string): T | undefined
|
|
6
|
-
getRaw(key: string): string | undefined
|
|
7
|
-
set(key: string, value: T): void
|
|
8
|
-
remove(key: string): void
|
|
9
|
-
reset(): void
|
|
4
|
+
isEnabled(): Promise<boolean>;
|
|
5
|
+
get(key: string): Promise<T | undefined>;
|
|
6
|
+
getRaw(key: string): Promise<string | undefined>;
|
|
7
|
+
set(key: string, value: T): Promise<void>;
|
|
8
|
+
remove(key: string): Promise<void>;
|
|
9
|
+
reset(): Promise<void>;
|
|
10
10
|
}
|
|
11
11
|
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../../src/storage/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAErD,qBAAa,aAAa,CAAC,CAAC,CAAE,YAAW,OAAO,CAAC,CAAC,CAAC;IACjD,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAa;
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../../src/storage/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAErD,qBAAa,aAAa,CAAC,CAAC,CAAE,YAAW,OAAO,CAAC,CAAC,CAAC;IACjD,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAa;IAEpC,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAI7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAIxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAKhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAIzC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B"}
|
|
@@ -1,25 +1,58 @@
|
|
|
1
|
+
import { __awaiter, __generator } from "tslib";
|
|
1
2
|
var MemoryStorage = /** @class */ (function () {
|
|
2
3
|
function MemoryStorage() {
|
|
3
4
|
this.memoryStorage = new Map();
|
|
4
5
|
}
|
|
5
6
|
MemoryStorage.prototype.isEnabled = function () {
|
|
6
|
-
return
|
|
7
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
8
|
+
return __generator(this, function (_a) {
|
|
9
|
+
return [2 /*return*/, true];
|
|
10
|
+
});
|
|
11
|
+
});
|
|
7
12
|
};
|
|
8
13
|
MemoryStorage.prototype.get = function (key) {
|
|
9
|
-
return this
|
|
14
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
15
|
+
return __generator(this, function (_a) {
|
|
16
|
+
return [2 /*return*/, this.memoryStorage.get(key)];
|
|
17
|
+
});
|
|
18
|
+
});
|
|
10
19
|
};
|
|
11
20
|
MemoryStorage.prototype.getRaw = function (key) {
|
|
12
|
-
|
|
13
|
-
|
|
21
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
22
|
+
var value;
|
|
23
|
+
return __generator(this, function (_a) {
|
|
24
|
+
switch (_a.label) {
|
|
25
|
+
case 0: return [4 /*yield*/, this.get(key)];
|
|
26
|
+
case 1:
|
|
27
|
+
value = _a.sent();
|
|
28
|
+
return [2 /*return*/, value ? JSON.stringify(value) : undefined];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
14
32
|
};
|
|
15
33
|
MemoryStorage.prototype.set = function (key, value) {
|
|
16
|
-
this
|
|
34
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
35
|
+
return __generator(this, function (_a) {
|
|
36
|
+
this.memoryStorage.set(key, value);
|
|
37
|
+
return [2 /*return*/];
|
|
38
|
+
});
|
|
39
|
+
});
|
|
17
40
|
};
|
|
18
41
|
MemoryStorage.prototype.remove = function (key) {
|
|
19
|
-
this
|
|
42
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
43
|
+
return __generator(this, function (_a) {
|
|
44
|
+
this.memoryStorage.delete(key);
|
|
45
|
+
return [2 /*return*/];
|
|
46
|
+
});
|
|
47
|
+
});
|
|
20
48
|
};
|
|
21
49
|
MemoryStorage.prototype.reset = function () {
|
|
22
|
-
this
|
|
50
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
51
|
+
return __generator(this, function (_a) {
|
|
52
|
+
this.memoryStorage.clear();
|
|
53
|
+
return [2 /*return*/];
|
|
54
|
+
});
|
|
55
|
+
});
|
|
23
56
|
};
|
|
24
57
|
return MemoryStorage;
|
|
25
58
|
}());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../../src/storage/memory.ts"],"names":[],"mappings":"AAEA;IAAA;QACE,kBAAa,GAAmB,IAAI,GAAG,EAAE,CAAC;IA0B5C,CAAC;
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../../src/storage/memory.ts"],"names":[],"mappings":";AAEA;IAAA;QACE,kBAAa,GAAmB,IAAI,GAAG,EAAE,CAAC;IA0B5C,CAAC;IAxBO,iCAAS,GAAf;;;gBACE,sBAAO,IAAI,EAAC;;;KACb;IAEK,2BAAG,GAAT,UAAU,GAAW;;;gBACnB,sBAAO,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,EAAC;;;KACpC;IAEK,8BAAM,GAAZ,UAAa,GAAW;;;;;4BACR,qBAAM,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAA;;wBAA3B,KAAK,GAAG,SAAmB;wBACjC,sBAAO,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,EAAC;;;;KAClD;IAEK,2BAAG,GAAT,UAAU,GAAW,EAAE,KAAQ;;;gBAC7B,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;;;;KACpC;IAEK,8BAAM,GAAZ,UAAa,GAAW;;;gBACtB,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;;;;KAChC;IAEK,6BAAK,GAAX;;;gBACE,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,CAAC;;;;KAC5B;IACH,oBAAC;AAAD,CAAC,AA3BD,IA2BC","sourcesContent":["import { Storage } from '@amplitude/analytics-types';\n\nexport class MemoryStorage<T> implements Storage<T> {\n memoryStorage: Map<string, T> = new Map();\n\n async isEnabled(): Promise<boolean> {\n return true;\n }\n\n async get(key: string): Promise<T | undefined> {\n return this.memoryStorage.get(key);\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n const value = await this.get(key);\n return value ? JSON.stringify(value) : undefined;\n }\n\n async set(key: string, value: T): Promise<void> {\n this.memoryStorage.set(key, value);\n }\n\n async remove(key: string): Promise<void> {\n this.memoryStorage.delete(key);\n }\n\n async reset(): Promise<void> {\n this.memoryStorage.clear();\n }\n}\n"]}
|
package/lib/esm/timeline.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { Config, Event, EventCallback, Plugin, Result } from '@amplitude/analytics-types';
|
|
2
|
-
export declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
export declare class Timeline {
|
|
3
|
+
queue: [Event, EventCallback][];
|
|
4
|
+
applying: boolean;
|
|
5
|
+
flushing: boolean;
|
|
6
|
+
plugins: Plugin[];
|
|
7
|
+
register(plugin: Plugin, config: Config): Promise<void>;
|
|
8
|
+
deregister(pluginName: string): Promise<void>;
|
|
9
|
+
push(event: Event, config: Config): Promise<Result>;
|
|
10
|
+
scheduleApply(timeout: number): void;
|
|
11
|
+
apply(item: [Event, EventCallback] | undefined): Promise<void>;
|
|
12
|
+
flush(): Promise<void>;
|
|
13
|
+
}
|
|
9
14
|
//# sourceMappingURL=timeline.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeline.d.ts","sourceRoot":"","sources":["../../src/timeline.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"timeline.d.ts","sourceRoot":"","sources":["../../src/timeline.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,MAAM,EAGN,KAAK,EACL,aAAa,EACb,MAAM,EAEN,MAAM,EACP,MAAM,4BAA4B,CAAC;AAIpC,qBAAa,QAAQ;IACnB,KAAK,EAAE,CAAC,KAAK,EAAE,aAAa,CAAC,EAAE,CAAM;IACrC,QAAQ,UAAS;IACjB,QAAQ,UAAS;IACjB,OAAO,EAAE,MAAM,EAAE,CAAM;IAEjB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAK7C,UAAU,CAAC,UAAU,EAAE,MAAM;IAQ7B,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM;IAWjC,aAAa,CAAC,OAAO,EAAE,MAAM;IAavB,KAAK,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,aAAa,CAAC,GAAG,SAAS;IAwC9C,KAAK;CAgBZ"}
|