perimeterx-js-core 0.29.0 → 0.30.0

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.
@@ -63,24 +63,21 @@ var HttpActivityClient = /** @class */ (function () {
63
63
  */
64
64
  HttpActivityClient.prototype.sendActivities = function (context) {
65
65
  return __awaiter(this, void 0, void 0, function () {
66
- var activities, e_1;
66
+ var activities;
67
67
  return __generator(this, function (_a) {
68
- switch (_a.label) {
69
- case 0:
70
- _a.trys.push([0, 2, , 3]);
71
- activities = this.createActivities(context);
72
- if (!activities || activities.length === 0) {
73
- context.logger.debug('no activities to send');
74
- return [2 /*return*/, true];
75
- }
76
- return [4 /*yield*/, this.postActivities(activities, context.logger)];
77
- case 1: return [2 /*return*/, _a.sent()];
78
- case 2:
79
- e_1 = _a.sent();
80
- context.logger.error("unable to send activities - ".concat(e_1));
81
- return [2 /*return*/, false];
82
- case 3: return [2 /*return*/];
68
+ try {
69
+ activities = this.createActivities(context);
70
+ if (!activities || activities.length === 0) {
71
+ context.logger.debug('no activities to send');
72
+ return [2 /*return*/, true];
73
+ }
74
+ return [2 /*return*/, this.postActivities(activities, context.logger)];
83
75
  }
76
+ catch (e) {
77
+ context.logger.error("unable to send activities - ".concat(e));
78
+ return [2 /*return*/, false];
79
+ }
80
+ return [2 /*return*/];
84
81
  });
85
82
  });
86
83
  };
@@ -171,11 +168,12 @@ var HttpActivityClient = /** @class */ (function () {
171
168
  */
172
169
  HttpActivityClient.prototype.postActivities = function (activities, logger) {
173
170
  return __awaiter(this, void 0, void 0, function () {
174
- var url, method, headers, body, req, res;
171
+ var url, method, headers, body, req, res, e_1;
175
172
  var _a;
176
173
  return __generator(this, function (_b) {
177
174
  switch (_b.label) {
178
175
  case 0:
176
+ _b.trys.push([0, 2, , 3]);
179
177
  url = "".concat(this.config.backendCollectorUrl).concat(constants_1.ACTIVITIES_ENDPOINT);
180
178
  method = http_1.HttpMethod.POST;
181
179
  headers = (_a = {},
@@ -192,6 +190,11 @@ var HttpActivityClient = /** @class */ (function () {
192
190
  case 1:
193
191
  res = _b.sent();
194
192
  return [2 /*return*/, (res === null || res === void 0 ? void 0 : res.status) === 200];
193
+ case 2:
194
+ e_1 = _b.sent();
195
+ logger.error("failed to post activities - ".concat(e_1));
196
+ return [2 /*return*/, false];
197
+ case 3: return [2 /*return*/];
195
198
  }
196
199
  });
197
200
  });
@@ -782,6 +782,13 @@ var ConfigurationBase = /** @class */ (function () {
782
782
  enumerable: false,
783
783
  configurable: true
784
784
  });
785
+ Object.defineProperty(ConfigurationBase.prototype, "awaitAsyncHttpRequests", {
786
+ get: function () {
787
+ return true;
788
+ },
789
+ enumerable: false,
790
+ configurable: true
791
+ });
785
792
  Object.defineProperty(ConfigurationBase.prototype, "isPostEnforceEnabled", {
786
793
  get: function () {
787
794
  return true;
@@ -23,8 +23,8 @@ var EndEnforcerFlow = /** @class */ (function (_super) {
23
23
  function EndEnforcerFlow(config, _a) {
24
24
  var logServiceClient = _a.logServiceClient, telemetry = _a.telemetry, remoteConfigManager = _a.remoteConfigManager, activityClient = _a.activityClient;
25
25
  var phases = [
26
- new phase_1.SendAsyncActivitiesPhase(activityClient),
27
- new phase_1.SendTelemetryActivityPhase(telemetry),
26
+ new phase_1.SendAsyncActivitiesPhase(config, activityClient),
27
+ new phase_1.SendTelemetryActivityPhase(config, telemetry),
28
28
  ];
29
29
  if (remoteConfigManager) {
30
30
  phases.push(new phase_1.UpdateRemoteConfigPhase(config, remoteConfigManager));
@@ -42,16 +42,20 @@ var action_1 = require("../../action/index.js");
42
42
  * The `SendAsyncActivitiesPhase` class is responsible for sending all necessary asynchronous activities based on the context.
43
43
  */
44
44
  var SendAsyncActivitiesPhase = /** @class */ (function () {
45
- function SendAsyncActivitiesPhase(activityClient) {
45
+ function SendAsyncActivitiesPhase(config, activityClient) {
46
+ this.config = config;
46
47
  this.activityClient = activityClient;
47
48
  }
48
49
  SendAsyncActivitiesPhase.prototype.execute = function (context) {
49
50
  return __awaiter(this, void 0, void 0, function () {
51
+ var promise;
50
52
  return __generator(this, function (_a) {
51
53
  switch (_a.label) {
52
54
  case 0:
53
55
  if (![action_1.Action.PASS_REQUEST, action_1.Action.BLOCK, action_1.Action.SIMULATED_BLOCK].includes(context.action)) return [3 /*break*/, 2];
54
- return [4 /*yield*/, this.activityClient.sendActivities(context)];
56
+ promise = this.activityClient.sendActivities(context);
57
+ if (!this.config.awaitAsyncHttpRequests) return [3 /*break*/, 2];
58
+ return [4 /*yield*/, promise];
55
59
  case 1:
56
60
  _a.sent();
57
61
  _a.label = 2;
@@ -41,16 +41,20 @@ exports.SendLogsPhase = void 0;
41
41
  * The `SendLogsPhase` class is responsible for sending logs to the HUMAN logging service.
42
42
  */
43
43
  var SendLogsPhase = /** @class */ (function () {
44
- function SendLogsPhase(_config, logServiceClient) {
44
+ function SendLogsPhase(config, logServiceClient) {
45
+ this.config = config;
45
46
  this.logServiceClient = logServiceClient;
46
47
  }
47
48
  SendLogsPhase.prototype.execute = function (context) {
48
49
  return __awaiter(this, void 0, void 0, function () {
50
+ var promise;
49
51
  return __generator(this, function (_a) {
50
52
  switch (_a.label) {
51
53
  case 0:
52
54
  if (!context.shouldSendLogs) return [3 /*break*/, 2];
53
- return [4 /*yield*/, this.logServiceClient.sendLogs(context)];
55
+ promise = this.logServiceClient.sendLogs(context);
56
+ if (!this.config.awaitAsyncHttpRequests) return [3 /*break*/, 2];
57
+ return [4 /*yield*/, promise];
54
58
  case 1:
55
59
  _a.sent();
56
60
  _a.label = 2;
@@ -41,16 +41,20 @@ exports.SendTelemetryActivityPhase = void 0;
41
41
  * The `SendTelemetryActivityPhase` class is responsible for sending the telemetry activity if necessary.
42
42
  */
43
43
  var SendTelemetryActivityPhase = /** @class */ (function () {
44
- function SendTelemetryActivityPhase(telemetry) {
44
+ function SendTelemetryActivityPhase(config, telemetry) {
45
+ this.config = config;
45
46
  this.telemetry = telemetry;
46
47
  }
47
48
  SendTelemetryActivityPhase.prototype.execute = function (context) {
48
49
  return __awaiter(this, void 0, void 0, function () {
50
+ var promise;
49
51
  return __generator(this, function (_a) {
50
52
  switch (_a.label) {
51
53
  case 0:
52
54
  if (!context.shouldSendTelemetry) return [3 /*break*/, 2];
53
- return [4 /*yield*/, this.telemetry.sendTelemetry(context)];
55
+ promise = this.telemetry.sendTelemetry(context);
56
+ if (!this.config.awaitAsyncHttpRequests) return [3 /*break*/, 2];
57
+ return [4 /*yield*/, promise];
54
58
  case 1:
55
59
  _a.sent();
56
60
  _a.label = 2;
@@ -15,4 +15,4 @@ exports.PUSH_DATA_FEATURE_HEADER_NAME = 'x-px-feature';
15
15
  exports.EMAIL_ADDRESS_REGEX = /^[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}$/;
16
16
  exports.URL_REGEX = /^(https?:)\/\/(([^@\s:\/]+):?([^@\s\/]*)@)?(([^:\/?#]*)(?:\:([0-9]+))?)(\/?[^?#]*)(\?[^#]*|)(#.*|)$/;
17
17
  exports.REGEX_STRUCTURE = /^\/(.+?)\/([gimsuyvd]*)$/;
18
- exports.CORE_MODULE_VERSION = 'JS Core 0.29.0';
18
+ exports.CORE_MODULE_VERSION = 'JS Core 0.30.0';
@@ -31,7 +31,7 @@ export class HttpActivityClient {
31
31
  context.logger.debug('no activities to send');
32
32
  return true;
33
33
  }
34
- return await this.postActivities(activities, context.logger);
34
+ return this.postActivities(activities, context.logger);
35
35
  }
36
36
  catch (e) {
37
37
  context.logger.error(`unable to send activities - ${e}`);
@@ -124,16 +124,22 @@ export class HttpActivityClient {
124
124
  * @protected
125
125
  */
126
126
  async postActivities(activities, logger) {
127
- const url = `${this.config.backendCollectorUrl}${ACTIVITIES_ENDPOINT}`;
128
- const method = HttpMethod.POST;
129
- const headers = {
130
- [CONTENT_TYPE_HEADER_NAME]: [ContentType.APPLICATION_JSON],
131
- [AUTHORIZATION_HEADER_NAME]: [getAuthorizationHeader(this.config.authToken)],
132
- };
133
- const body = activities.length === 1 ? JSON.stringify(activities[0]) : JSON.stringify(activities);
134
- logger.debug(`sending ${activities.map(({ type }) => `${type} activity`).join(', ')} to ${url}`);
135
- const req = new OutgoingRequestImpl({ url, method, headers, body });
136
- const res = await this.httpClient.send(req, { timeoutMs: this.config.asyncTimeout });
137
- return res?.status === 200;
127
+ try {
128
+ const url = `${this.config.backendCollectorUrl}${ACTIVITIES_ENDPOINT}`;
129
+ const method = HttpMethod.POST;
130
+ const headers = {
131
+ [CONTENT_TYPE_HEADER_NAME]: [ContentType.APPLICATION_JSON],
132
+ [AUTHORIZATION_HEADER_NAME]: [getAuthorizationHeader(this.config.authToken)],
133
+ };
134
+ const body = activities.length === 1 ? JSON.stringify(activities[0]) : JSON.stringify(activities);
135
+ logger.debug(`sending ${activities.map(({ type }) => `${type} activity`).join(', ')} to ${url}`);
136
+ const req = new OutgoingRequestImpl({ url, method, headers, body });
137
+ const res = await this.httpClient.send(req, { timeoutMs: this.config.asyncTimeout });
138
+ return res?.status === 200;
139
+ }
140
+ catch (e) {
141
+ logger.error(`failed to post activities - ${e}`);
142
+ return false;
143
+ }
138
144
  }
139
145
  }
@@ -408,6 +408,9 @@ export class ConfigurationBase {
408
408
  get enableBlockedUrlOnCaptchaBlockPage() {
409
409
  return true;
410
410
  }
411
+ get awaitAsyncHttpRequests() {
412
+ return true;
413
+ }
411
414
  get isPostEnforceEnabled() {
412
415
  return true;
413
416
  }
@@ -3,8 +3,8 @@ import { Flow } from './Flow.js';
3
3
  export class EndEnforcerFlow extends Flow {
4
4
  constructor(config, { logServiceClient, telemetry, remoteConfigManager, activityClient, }) {
5
5
  const phases = [
6
- new SendAsyncActivitiesPhase(activityClient),
7
- new SendTelemetryActivityPhase(telemetry),
6
+ new SendAsyncActivitiesPhase(config, activityClient),
7
+ new SendTelemetryActivityPhase(config, telemetry),
8
8
  ];
9
9
  if (remoteConfigManager) {
10
10
  phases.push(new UpdateRemoteConfigPhase(config, remoteConfigManager));
@@ -3,13 +3,19 @@ import { Action } from '../../action/index.js';
3
3
  * The `SendAsyncActivitiesPhase` class is responsible for sending all necessary asynchronous activities based on the context.
4
4
  */
5
5
  export class SendAsyncActivitiesPhase {
6
+ config;
6
7
  activityClient;
7
- constructor(activityClient) {
8
+ constructor(config, activityClient) {
9
+ this.config = config;
8
10
  this.activityClient = activityClient;
9
11
  }
10
12
  async execute(context) {
11
13
  if ([Action.PASS_REQUEST, Action.BLOCK, Action.SIMULATED_BLOCK].includes(context.action)) {
12
- await this.activityClient.sendActivities(context);
14
+ // ensure error handling is done in the sendActivities method
15
+ const promise = this.activityClient.sendActivities(context);
16
+ if (this.config.awaitAsyncHttpRequests) {
17
+ await promise;
18
+ }
13
19
  }
14
20
  return { done: false };
15
21
  }
@@ -2,13 +2,19 @@
2
2
  * The `SendLogsPhase` class is responsible for sending logs to the HUMAN logging service.
3
3
  */
4
4
  export class SendLogsPhase {
5
+ config;
5
6
  logServiceClient;
6
- constructor(_config, logServiceClient) {
7
+ constructor(config, logServiceClient) {
8
+ this.config = config;
7
9
  this.logServiceClient = logServiceClient;
8
10
  }
9
11
  async execute(context) {
10
12
  if (context.shouldSendLogs) {
11
- await this.logServiceClient.sendLogs(context);
13
+ // ensure error handling is done in the sendLogs method
14
+ const promise = this.logServiceClient.sendLogs(context);
15
+ if (this.config.awaitAsyncHttpRequests) {
16
+ await promise;
17
+ }
12
18
  }
13
19
  return { done: false };
14
20
  }
@@ -2,13 +2,19 @@
2
2
  * The `SendTelemetryActivityPhase` class is responsible for sending the telemetry activity if necessary.
3
3
  */
4
4
  export class SendTelemetryActivityPhase {
5
+ config;
5
6
  telemetry;
6
- constructor(telemetry) {
7
+ constructor(config, telemetry) {
8
+ this.config = config;
7
9
  this.telemetry = telemetry;
8
10
  }
9
11
  async execute(context) {
10
12
  if (context.shouldSendTelemetry) {
11
- await this.telemetry.sendTelemetry(context);
13
+ // ensure error handling is done in the sendTelemetry method
14
+ const promise = this.telemetry.sendTelemetry(context);
15
+ if (this.config.awaitAsyncHttpRequests) {
16
+ await promise;
17
+ }
12
18
  }
13
19
  return { done: false };
14
20
  }
@@ -12,4 +12,4 @@ export const PUSH_DATA_FEATURE_HEADER_NAME = 'x-px-feature';
12
12
  export const EMAIL_ADDRESS_REGEX = /^[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}$/;
13
13
  export const URL_REGEX = /^(https?:)\/\/(([^@\s:\/]+):?([^@\s\/]*)@)?(([^:\/?#]*)(?:\:([0-9]+))?)(\/?[^?#]*)(\?[^#]*|)(#.*|)$/;
14
14
  export const REGEX_STRUCTURE = /^\/(.+?)\/([gimsuyvd]*)$/;
15
- export const CORE_MODULE_VERSION = 'JS Core 0.29.0';
15
+ export const CORE_MODULE_VERSION = 'JS Core 0.30.0';
@@ -144,5 +144,6 @@ export declare abstract class ConfigurationBase<Req, Res, Added, Removed extends
144
144
  get snippetInjectionEnabled(): boolean;
145
145
  get createCustomSnippet(): CustomSnippetFunction<Req, Res, Added, Removed> | null;
146
146
  get enableBlockedUrlOnCaptchaBlockPage(): boolean;
147
+ get awaitAsyncHttpRequests(): boolean;
147
148
  get isPostEnforceEnabled(): boolean;
148
149
  }
@@ -396,6 +396,10 @@ export interface IConfiguration<Req, Res, Added, Removed> {
396
396
  * Whether the enforcer has access to the HTTP response, which is required for the postEnforce function call
397
397
  */
398
398
  readonly isPostEnforceEnabled: boolean;
399
+ /**
400
+ * Whether to await asynchronous HTTP requests (e.g., async activities, remote logger, telemetry)
401
+ */
402
+ readonly awaitAsyncHttpRequests: boolean;
399
403
  /**
400
404
  * The version of the token the enforcer is able to parse.
401
405
  */
@@ -1,3 +1,4 @@
1
+ import { IConfiguration } from '../../config';
1
2
  import { IContext } from '../../context';
2
3
  import { IActivityClient } from '../../activities';
3
4
  import { PhaseResult } from '../PhaseResult';
@@ -5,8 +6,9 @@ import { IPhase } from '../IPhase';
5
6
  /**
6
7
  * The `SendAsyncActivitiesPhase` class is responsible for sending all necessary asynchronous activities based on the context.
7
8
  */
8
- export declare class SendAsyncActivitiesPhase<Req, Res> implements IPhase<Req, Res> {
9
+ export declare class SendAsyncActivitiesPhase<Req, Res, Added, Removed> implements IPhase<Req, Res> {
10
+ protected readonly config: IConfiguration<Req, Res, Added, Removed>;
9
11
  protected readonly activityClient: IActivityClient<Req, Res>;
10
- constructor(activityClient: IActivityClient<Req, Res>);
12
+ constructor(config: IConfiguration<Req, Res, Added, Removed>, activityClient: IActivityClient<Req, Res>);
11
13
  execute(context: IContext<Req, Res>): Promise<PhaseResult>;
12
14
  }
@@ -7,7 +7,8 @@ import { ILogServiceClient } from '../../logger';
7
7
  * The `SendLogsPhase` class is responsible for sending logs to the HUMAN logging service.
8
8
  */
9
9
  export declare class SendLogsPhase<Req, Res, Added, Removed> implements IPhase<Req, Res> {
10
+ private readonly config;
10
11
  private readonly logServiceClient;
11
- constructor(_config: IConfiguration<Req, Res, Added, Removed>, logServiceClient: ILogServiceClient<Req, Res>);
12
+ constructor(config: IConfiguration<Req, Res, Added, Removed>, logServiceClient: ILogServiceClient<Req, Res>);
12
13
  execute(context: IContext<Req, Res>): Promise<PhaseResult>;
13
14
  }
@@ -2,12 +2,14 @@ import { IPhase } from '../IPhase';
2
2
  import { PhaseResult } from '../PhaseResult';
3
3
  import { IContext } from '../../context';
4
4
  import { ITelemetry } from '../../telemetry';
5
+ import { IConfiguration } from '../../config';
5
6
  /**
6
7
  * The `SendTelemetryActivityPhase` class is responsible for sending the telemetry activity if necessary.
7
8
  */
8
- export declare class SendTelemetryActivityPhase<Req, Res> implements IPhase<Req, Res> {
9
+ export declare class SendTelemetryActivityPhase<Req, Res, Added, Removed> implements IPhase<Req, Res> {
10
+ protected readonly config: IConfiguration<Req, Res, Added, Removed>;
9
11
  protected readonly telemetry: ITelemetry<Req, Res>;
10
- constructor(telemetry: ITelemetry<Req, Res>);
12
+ constructor(config: IConfiguration<Req, Res, Added, Removed>, telemetry: ITelemetry<Req, Res>);
11
13
  execute(context: IContext<Req, Res>): Promise<PhaseResult>;
12
14
  protected sendTelemetry(context: IContext<Req, Res>): Promise<void>;
13
15
  }
@@ -12,4 +12,4 @@ export declare const PUSH_DATA_FEATURE_HEADER_NAME = "x-px-feature";
12
12
  export declare const EMAIL_ADDRESS_REGEX: RegExp;
13
13
  export declare const URL_REGEX: RegExp;
14
14
  export declare const REGEX_STRUCTURE: RegExp;
15
- export declare const CORE_MODULE_VERSION = "JS Core 0.29.0";
15
+ export declare const CORE_MODULE_VERSION = "JS Core 0.30.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "perimeterx-js-core",
3
- "version": "0.29.0",
3
+ "version": "0.30.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "typesVersions": {