perimeterx-js-core 0.15.0 → 0.15.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.
@@ -61,10 +61,12 @@ var HttpBatchedActivityClient = /** @class */ (function (_super) {
61
61
  _this.batchSize = config.maxActivityBatchSize;
62
62
  _this.timeoutMs = config.activityBatchTimeoutMs;
63
63
  _this.maxBufferSize = _this.batchSize * 2;
64
+ _this.shouldKill = false;
64
65
  _this.startTimer();
65
66
  return _this;
66
67
  }
67
68
  HttpBatchedActivityClient.prototype.stop = function () {
69
+ this.shouldKill = true;
68
70
  this.stopTimer();
69
71
  };
70
72
  HttpBatchedActivityClient.prototype.postActivities = function (activities) {
@@ -99,7 +101,9 @@ var HttpBatchedActivityClient = /** @class */ (function (_super) {
99
101
  _b.label = 2;
100
102
  case 2:
101
103
  sentSuccessfully = _a;
102
- this.startTimer();
104
+ if (!this.shouldKill) {
105
+ this.startTimer();
106
+ }
103
107
  return [2 /*return*/, sentSuccessfully];
104
108
  }
105
109
  });
@@ -107,14 +111,17 @@ var HttpBatchedActivityClient = /** @class */ (function (_super) {
107
111
  };
108
112
  HttpBatchedActivityClient.prototype.flush = function () {
109
113
  return __awaiter(this, void 0, void 0, function () {
110
- var sentSuccessfully;
114
+ var bufferCopy, sentSuccessfully;
111
115
  return __generator(this, function (_a) {
112
116
  switch (_a.label) {
113
- case 0: return [4 /*yield*/, _super.prototype.postActivities.call(this, this.buffer)];
117
+ case 0:
118
+ bufferCopy = this.buffer.concat();
119
+ this.clear();
120
+ return [4 /*yield*/, _super.prototype.postActivities.call(this, bufferCopy)];
114
121
  case 1:
115
122
  sentSuccessfully = _a.sent();
116
- if (sentSuccessfully) {
117
- this.clear();
123
+ if (!sentSuccessfully) {
124
+ this.buffer = this.buffer.concat(bufferCopy);
118
125
  }
119
126
  return [2 /*return*/, sentSuccessfully];
120
127
  }
@@ -154,17 +154,18 @@ var EnforcerBase = /** @class */ (function () {
154
154
  case 3:
155
155
  _a.sent();
156
156
  return [2 /*return*/, result.response ? this.convertToRes.apply(this, __spreadArray([result.response], args, false)) : null];
157
- case 4:
158
- this.preserveContext.apply(this, __spreadArray([context], args, false));
159
- return [4 /*yield*/, this.enforceFlow.execute(context)];
157
+ case 4: return [4 /*yield*/, this.preserveContext.apply(this, __spreadArray([context], args, false))];
160
158
  case 5:
159
+ _a.sent();
160
+ return [4 /*yield*/, this.enforceFlow.execute(context)];
161
+ case 6:
161
162
  result = _a.sent();
162
- if (!result.done) return [3 /*break*/, 7];
163
+ if (!result.done) return [3 /*break*/, 8];
163
164
  return [4 /*yield*/, this.endEnforcerFlow.execute(context)];
164
- case 6:
165
+ case 7:
165
166
  _a.sent();
166
167
  return [2 /*return*/, result.response ? this.convertToRes.apply(this, __spreadArray([result.response], args, false)) : null];
167
- case 7: return [2 /*return*/, null];
168
+ case 8: return [2 /*return*/, null];
168
169
  }
169
170
  });
170
171
  });
@@ -21,11 +21,7 @@ var EndEnforcerFlow = /** @class */ (function (_super) {
21
21
  __extends(EndEnforcerFlow, _super);
22
22
  function EndEnforcerFlow(config, _a) {
23
23
  var logServiceClient = _a.logServiceClient;
24
- var phasesArray = [];
25
- if (logServiceClient) {
26
- phasesArray.push(new impl_1.SendLogsPhase(config, logServiceClient));
27
- }
28
- return _super.call(this, phasesArray) || this;
24
+ return _super.call(this, [new impl_1.SendLogsPhase(config, logServiceClient)]) || this;
29
25
  }
30
26
  return EndEnforcerFlow;
31
27
  }(impl_1.CompositePhase));
@@ -43,15 +43,16 @@ var SendLogsPhase = /** @class */ (function () {
43
43
  this.logServiceClient = logServiceClient;
44
44
  }
45
45
  SendLogsPhase.prototype.execute = function (context) {
46
+ var _a;
46
47
  return __awaiter(this, void 0, void 0, function () {
47
- return __generator(this, function (_a) {
48
- switch (_a.label) {
48
+ return __generator(this, function (_b) {
49
+ switch (_b.label) {
49
50
  case 0:
50
51
  if (!context.shouldSendLogs) return [3 /*break*/, 2];
51
- return [4 /*yield*/, this.logServiceClient.sendLogs(context, this.config.logger.getLogs())];
52
+ return [4 /*yield*/, ((_a = this.logServiceClient) === null || _a === void 0 ? void 0 : _a.sendLogs(context, this.config.logger.getLogs()))];
52
53
  case 1:
53
- _a.sent();
54
- _a.label = 2;
54
+ _b.sent();
55
+ _b.label = 2;
55
56
  case 2:
56
57
  this.config.logger.clearLogs();
57
58
  return [2 /*return*/, { done: false }];
@@ -13,4 +13,4 @@ exports.PUSH_DATA_HMAC_HEADER_NAME = 'x-px-pushdata';
13
13
  exports.PUSH_DATA_FEATURE_HEADER_NAME = 'x-px-feature';
14
14
  exports.EMAIL_ADDRESS_REGEX = /^[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}$/;
15
15
  exports.URL_REGEX = /^(https?\:)\/\/(([^@\s:]+):?([^@\s]*)@)?(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/;
16
- exports.CORE_MODULE_VERSION = 'JS Core 0.15.0';
16
+ exports.CORE_MODULE_VERSION = 'JS Core 0.15.2';
@@ -5,15 +5,18 @@ export class HttpBatchedActivityClient extends HttpActivityClient {
5
5
  maxBufferSize;
6
6
  buffer;
7
7
  timeoutId;
8
+ shouldKill;
8
9
  constructor(config, httpClient) {
9
10
  super(config, httpClient);
10
11
  this.buffer = [];
11
12
  this.batchSize = config.maxActivityBatchSize;
12
13
  this.timeoutMs = config.activityBatchTimeoutMs;
13
14
  this.maxBufferSize = this.batchSize * 2;
15
+ this.shouldKill = false;
14
16
  this.startTimer();
15
17
  }
16
18
  stop() {
19
+ this.shouldKill = true;
17
20
  this.stopTimer();
18
21
  }
19
22
  async postActivities(activities) {
@@ -32,13 +35,17 @@ export class HttpBatchedActivityClient extends HttpActivityClient {
32
35
  async triggerFlush() {
33
36
  this.stopTimer();
34
37
  const sentSuccessfully = this.buffer.length > 0 && (await this.flush());
35
- this.startTimer();
38
+ if (!this.shouldKill) {
39
+ this.startTimer();
40
+ }
36
41
  return sentSuccessfully;
37
42
  }
38
43
  async flush() {
39
- const sentSuccessfully = await super.postActivities(this.buffer);
40
- if (sentSuccessfully) {
41
- this.clear();
44
+ const bufferCopy = this.buffer.concat();
45
+ this.clear();
46
+ const sentSuccessfully = await super.postActivities(bufferCopy);
47
+ if (!sentSuccessfully) {
48
+ this.buffer = this.buffer.concat(bufferCopy);
42
49
  }
43
50
  return sentSuccessfully;
44
51
  }
@@ -75,7 +75,7 @@ export class EnforcerBase {
75
75
  await this.endEnforcerFlow.execute(context);
76
76
  return result.response ? this.convertToRes(result.response, ...args) : null;
77
77
  }
78
- this.preserveContext(context, ...args);
78
+ await this.preserveContext(context, ...args);
79
79
  result = await this.enforceFlow.execute(context);
80
80
  if (result.done) {
81
81
  await this.endEnforcerFlow.execute(context);
@@ -1,10 +1,6 @@
1
1
  import { CompositePhase, SendLogsPhase } from '../impl';
2
2
  export class EndEnforcerFlow extends CompositePhase {
3
3
  constructor(config, { logServiceClient }) {
4
- const phasesArray = [];
5
- if (logServiceClient) {
6
- phasesArray.push(new SendLogsPhase(config, logServiceClient));
7
- }
8
- super(phasesArray);
4
+ super([new SendLogsPhase(config, logServiceClient)]);
9
5
  }
10
6
  }
@@ -7,7 +7,7 @@ export class SendLogsPhase {
7
7
  }
8
8
  async execute(context) {
9
9
  if (context.shouldSendLogs) {
10
- await this.logServiceClient.sendLogs(context, this.config.logger.getLogs());
10
+ await this.logServiceClient?.sendLogs(context, this.config.logger.getLogs());
11
11
  }
12
12
  this.config.logger.clearLogs();
13
13
  return { done: false };
@@ -10,4 +10,4 @@ export const PUSH_DATA_HMAC_HEADER_NAME = 'x-px-pushdata';
10
10
  export const PUSH_DATA_FEATURE_HEADER_NAME = 'x-px-feature';
11
11
  export const EMAIL_ADDRESS_REGEX = /^[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}$/;
12
12
  export const URL_REGEX = /^(https?\:)\/\/(([^@\s:]+):?([^@\s]*)@)?(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/;
13
- export const CORE_MODULE_VERSION = 'JS Core 0.15.0';
13
+ export const CORE_MODULE_VERSION = 'JS Core 0.15.2';
@@ -1,6 +1,6 @@
1
1
  import { HttpActivityClient } from './HttpActivityClient';
2
2
  import { IConfiguration } from '../config';
3
- import { AsyncActivity } from './model/AsyncActivity';
3
+ import { AsyncActivity } from './model';
4
4
  import { IHttpClient } from '../http';
5
5
  export declare class HttpBatchedActivityClient<Req, Res> extends HttpActivityClient<Req, Res> {
6
6
  private readonly batchSize;
@@ -8,6 +8,7 @@ export declare class HttpBatchedActivityClient<Req, Res> extends HttpActivityCli
8
8
  private readonly maxBufferSize;
9
9
  private buffer;
10
10
  private timeoutId;
11
+ private shouldKill;
11
12
  constructor(config: IConfiguration<Req, Res>, httpClient: IHttpClient);
12
13
  stop(): void;
13
14
  protected postActivities(activities: AsyncActivity[]): Promise<boolean>;
@@ -8,6 +8,7 @@ import { Products } from '../products';
8
8
  import { IActivityClient } from '../activities';
9
9
  import { IEnforcer } from './IEnforcer';
10
10
  import { EnforcerBaseOptions, EnforcerOptionsType } from './options';
11
+ import { AsyncOrSync } from 'ts-essentials';
11
12
  export declare abstract class EnforcerBase<TokenV extends TokenVersion, Req, Res, EnforceArgs extends any[], PostEnforceArgs extends any[]> implements IEnforcer<EnforceArgs, PostEnforceArgs, Promise<null | Res>, Promise<void>> {
12
13
  protected readonly config: IConfiguration<Req, Res>;
13
14
  protected readonly filterFlow: IPhase<Req, Res>;
@@ -21,7 +22,7 @@ export declare abstract class EnforcerBase<TokenV extends TokenVersion, Req, Res
21
22
  * @returns IContext - The context for the request.
22
23
  * @protected
23
24
  */
24
- protected abstract constructContext(...args: EnforceArgs): IContext<Req, Res>;
25
+ protected abstract constructContext(...args: EnforceArgs): AsyncOrSync<IContext<Req, Res>>;
25
26
  /**
26
27
  * Saves the provided context for future use in the postEnforce function.
27
28
  * @param context - The IContext that should be saved
@@ -29,7 +30,7 @@ export declare abstract class EnforcerBase<TokenV extends TokenVersion, Req, Res
29
30
  * @returns void
30
31
  * @protected
31
32
  */
32
- protected abstract preserveContext(context: IContext<Req, Res>, ...args: EnforceArgs): void;
33
+ protected abstract preserveContext(context: IContext<Req, Res>, ...args: EnforceArgs): AsyncOrSync<void>;
33
34
  /**
34
35
  * A function that retrieves the already generated context for use in the postEnforce function.
35
36
  * @param args - The PostEnforce args required to enforce the incoming request.
@@ -10,4 +10,4 @@ export declare const PUSH_DATA_HMAC_HEADER_NAME = "x-px-pushdata";
10
10
  export declare const PUSH_DATA_FEATURE_HEADER_NAME = "x-px-feature";
11
11
  export declare const EMAIL_ADDRESS_REGEX: RegExp;
12
12
  export declare const URL_REGEX: RegExp;
13
- export declare const CORE_MODULE_VERSION = "JS Core 0.15.0";
13
+ export declare const CORE_MODULE_VERSION = "JS Core 0.15.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "perimeterx-js-core",
3
- "version": "0.15.0",
3
+ "version": "0.15.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "typesVersions": {