perimeterx-js-core 0.1.1 → 0.2.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.
@@ -2,11 +2,19 @@ import { IConfiguration } from '../config';
2
2
  import { IContext } from '../context';
3
3
  import { IHttpClient } from '../http';
4
4
  import { IActivityClient } from './IActivityClient';
5
+ import { Activity } from './model/Activity';
5
6
  export declare class HttpActivityClient implements IActivityClient {
6
7
  private readonly config;
7
8
  private readonly httpClient;
8
9
  constructor(config: IConfiguration, httpClient: IHttpClient);
9
10
  sendActivities(context: IContext): Promise<boolean>;
10
- private createActivities;
11
- private postActivities;
11
+ protected createActivities(context: IContext): Activity[];
12
+ /**
13
+ * Allows for expansions or alterations to the async activity if needed.
14
+ * @param activity
15
+ * @returns Activity
16
+ * @protected
17
+ */
18
+ protected finalizeActivity(activity: Activity): Activity;
19
+ protected postActivities(activities: Activity[]): Promise<boolean>;
12
20
  }
@@ -74,7 +74,16 @@ var HttpActivityClient = /** @class */ (function () {
74
74
  else if (context.passReason) {
75
75
  activities.push((0, utils_2.createActivity)(ActivityType_1.ActivityType.PAGE_REQUESTED, this.config, context));
76
76
  }
77
- return activities;
77
+ return activities.map(this.finalizeActivity);
78
+ };
79
+ /**
80
+ * Allows for expansions or alterations to the async activity if needed.
81
+ * @param activity
82
+ * @returns Activity
83
+ * @protected
84
+ */
85
+ HttpActivityClient.prototype.finalizeActivity = function (activity) {
86
+ return activity;
78
87
  };
79
88
  HttpActivityClient.prototype.postActivities = function (activities) {
80
89
  return __awaiter(this, void 0, void 0, function () {
@@ -0,0 +1,21 @@
1
+ import { HttpActivityClient } from './HttpActivityClient';
2
+ import { IConfiguration } from '../config';
3
+ import { IHttpClient } from '../http';
4
+ import { Activity } from './model/Activity';
5
+ export declare class HttpBatchedActivityClient extends HttpActivityClient {
6
+ private readonly batchSize;
7
+ private readonly timeoutMs;
8
+ private readonly maxBufferSize;
9
+ private buffer;
10
+ private timeoutId;
11
+ constructor(config: IConfiguration, httpClient: IHttpClient);
12
+ stop(): void;
13
+ protected postActivities(activities: Activity[]): Promise<boolean>;
14
+ private addToBuffer;
15
+ private shouldFlush;
16
+ protected triggerFlush(): Promise<boolean>;
17
+ private flush;
18
+ protected clear(): void;
19
+ private startTimer;
20
+ private stopTimer;
21
+ }
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.HttpBatchedActivityClient = void 0;
55
+ var HttpActivityClient_1 = require("./HttpActivityClient");
56
+ var HttpBatchedActivityClient = /** @class */ (function (_super) {
57
+ __extends(HttpBatchedActivityClient, _super);
58
+ function HttpBatchedActivityClient(config, httpClient) {
59
+ var _this = _super.call(this, config, httpClient) || this;
60
+ _this.buffer = [];
61
+ _this.batchSize = config.maxActivityBatchSize;
62
+ _this.timeoutMs = config.activityBatchTimeoutMs;
63
+ _this.maxBufferSize = _this.batchSize * 2;
64
+ _this.startTimer();
65
+ return _this;
66
+ }
67
+ HttpBatchedActivityClient.prototype.stop = function () {
68
+ this.stopTimer();
69
+ };
70
+ HttpBatchedActivityClient.prototype.postActivities = function (activities) {
71
+ return __awaiter(this, void 0, void 0, function () {
72
+ return __generator(this, function (_a) {
73
+ this.addToBuffer(activities);
74
+ return [2 /*return*/, this.shouldFlush() ? this.triggerFlush() : true];
75
+ });
76
+ });
77
+ };
78
+ HttpBatchedActivityClient.prototype.addToBuffer = function (activities) {
79
+ this.buffer = activities.concat(this.buffer);
80
+ if (this.buffer.length > this.maxBufferSize) {
81
+ this.buffer = this.buffer.slice(0, this.maxBufferSize);
82
+ }
83
+ };
84
+ HttpBatchedActivityClient.prototype.shouldFlush = function () {
85
+ return this.buffer.length >= this.batchSize;
86
+ };
87
+ HttpBatchedActivityClient.prototype.triggerFlush = function () {
88
+ return __awaiter(this, void 0, void 0, function () {
89
+ var sentSuccessfully, _a;
90
+ return __generator(this, function (_b) {
91
+ switch (_b.label) {
92
+ case 0:
93
+ this.stopTimer();
94
+ _a = this.buffer.length > 0;
95
+ if (!_a) return [3 /*break*/, 2];
96
+ return [4 /*yield*/, this.flush()];
97
+ case 1:
98
+ _a = (_b.sent());
99
+ _b.label = 2;
100
+ case 2:
101
+ sentSuccessfully = _a;
102
+ this.startTimer();
103
+ return [2 /*return*/, sentSuccessfully];
104
+ }
105
+ });
106
+ });
107
+ };
108
+ HttpBatchedActivityClient.prototype.flush = function () {
109
+ return __awaiter(this, void 0, void 0, function () {
110
+ var sentSuccessfully;
111
+ return __generator(this, function (_a) {
112
+ switch (_a.label) {
113
+ case 0: return [4 /*yield*/, _super.prototype.postActivities.call(this, this.buffer)];
114
+ case 1:
115
+ sentSuccessfully = _a.sent();
116
+ if (sentSuccessfully) {
117
+ this.clear();
118
+ }
119
+ return [2 /*return*/, sentSuccessfully];
120
+ }
121
+ });
122
+ });
123
+ };
124
+ HttpBatchedActivityClient.prototype.clear = function () {
125
+ this.buffer = [];
126
+ };
127
+ HttpBatchedActivityClient.prototype.startTimer = function () {
128
+ var _this = this;
129
+ this.timeoutId = setTimeout(function () { return _this.triggerFlush(); }, this.timeoutMs);
130
+ };
131
+ HttpBatchedActivityClient.prototype.stopTimer = function () {
132
+ clearTimeout(this.timeoutId);
133
+ this.timeoutId = null;
134
+ };
135
+ return HttpBatchedActivityClient;
136
+ }(HttpActivityClient_1.HttpActivityClient));
137
+ exports.HttpBatchedActivityClient = HttpBatchedActivityClient;
@@ -3,3 +3,4 @@ export { Activity } from './model/Activity';
3
3
  export { ActivityTypeDetails, ActivityDetails, BlockActivityDetails, PageRequestedActivityDetails, } from './model/ActivityDetails';
4
4
  export { ActivityType } from './ActivityType';
5
5
  export { HttpActivityClient } from './HttpActivityClient';
6
+ export { HttpBatchedActivityClient } from './HttpBatchedActivityClient';
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpActivityClient = exports.ActivityType = void 0;
3
+ exports.HttpBatchedActivityClient = exports.HttpActivityClient = exports.ActivityType = void 0;
4
4
  var ActivityType_1 = require("./ActivityType");
5
5
  Object.defineProperty(exports, "ActivityType", { enumerable: true, get: function () { return ActivityType_1.ActivityType; } });
6
6
  var HttpActivityClient_1 = require("./HttpActivityClient");
7
7
  Object.defineProperty(exports, "HttpActivityClient", { enumerable: true, get: function () { return HttpActivityClient_1.HttpActivityClient; } });
8
+ var HttpBatchedActivityClient_1 = require("./HttpBatchedActivityClient");
9
+ Object.defineProperty(exports, "HttpBatchedActivityClient", { enumerable: true, get: function () { return HttpBatchedActivityClient_1.HttpBatchedActivityClient; } });
@@ -20,7 +20,7 @@ exports.DEFAULT_CONFIGURATIONS = {
20
20
  px_module_mode: ModuleMode_1.ModuleMode.MONITOR,
21
21
  px_additional_activity_handler: null,
22
22
  px_advanced_blocking_response_enabled: true,
23
- px_max_activity_batch_size: 20,
23
+ px_max_activity_batch_size: 0,
24
24
  px_batch_activities_timeout_ms: 1000,
25
25
  px_bypass_monitor_header: '',
26
26
  px_csp_enabled: false,
@@ -157,6 +157,14 @@ export interface IConfiguration<ParamsType extends ConfigurationParams = Configu
157
157
  * The maximum expected length of the user-agent beyond which it will be truncated.
158
158
  */
159
159
  readonly userAgentMaxLength: number;
160
+ /**
161
+ * The batch size at which asynchronous activities are sent to the collector.
162
+ */
163
+ readonly maxActivityBatchSize: number;
164
+ /**
165
+ * The maximum amount of time to wait before sending asynchronous activities to the collector.
166
+ */
167
+ readonly activityBatchTimeoutMs: number;
160
168
  /**
161
169
  * A function returning CustomParameters that will be added to the enforcer activities.
162
170
  */
@@ -51,6 +51,8 @@ export declare abstract class StaticConfigurationBase<ParamsType extends Configu
51
51
  get riskCookieMinIterations(): number;
52
52
  get riskCookieMaxLength(): number;
53
53
  get userAgentMaxLength(): number;
54
+ get maxActivityBatchSize(): number;
55
+ get activityBatchTimeoutMs(): number;
54
56
  get enrichCustomParameters(): CustomParametersFunction;
55
57
  get additionalActivityHandler(): AdditionalActivityHandler;
56
58
  get altBackendCaptchaUrl(): string;
@@ -329,6 +329,20 @@ var StaticConfigurationBase = /** @class */ (function () {
329
329
  enumerable: false,
330
330
  configurable: true
331
331
  });
332
+ Object.defineProperty(StaticConfigurationBase.prototype, "maxActivityBatchSize", {
333
+ get: function () {
334
+ return this.configParams.px_max_activity_batch_size;
335
+ },
336
+ enumerable: false,
337
+ configurable: true
338
+ });
339
+ Object.defineProperty(StaticConfigurationBase.prototype, "activityBatchTimeoutMs", {
340
+ get: function () {
341
+ return this.configParams.px_batch_activities_timeout_ms;
342
+ },
343
+ enumerable: false,
344
+ configurable: true
345
+ });
332
346
  Object.defineProperty(StaticConfigurationBase.prototype, "enrichCustomParameters", {
333
347
  get: function () {
334
348
  return this.configParams.px_enrich_custom_parameters || null;
@@ -75,7 +75,11 @@ var EnforcerBase = /** @class */ (function () {
75
75
  var httpClient = options.httpClient;
76
76
  this.firstParty = options.firstParty || new first_party_1.DefaultFirstParty(this.config, httpClient);
77
77
  this.telemetry = options.telemetry || new telemetry_1.DefaultTelemetry(this.config, httpClient, base64Utils, hashUtils);
78
- this.activityClient = options.activityClient || new activities_1.HttpActivityClient(this.config, httpClient);
78
+ this.activityClient =
79
+ options.activityClient ||
80
+ (this.config.maxActivityBatchSize > 1
81
+ ? new activities_1.HttpBatchedActivityClient(this.config, httpClient)
82
+ : new activities_1.HttpActivityClient(this.config, httpClient));
79
83
  var riskResponseHandler = options.tokenVersion === risk_token_1.TokenVersion.V2
80
84
  ? new risk_api_1.RiskResponseV2Handler(this.config)
81
85
  : new risk_api_1.RiskResponseV3Handler(this.config);
@@ -15,7 +15,20 @@ export declare class PostRiskApiClient<RiskResponseType extends RiskResponseV2 |
15
15
  shouldRetrieveScore(context: IContext): boolean;
16
16
  private unsetScoreFields;
17
17
  retrieveScore(context: IContext): Promise<boolean>;
18
+ /**
19
+ * Creates the RiskActivity payload
20
+ * @param context
21
+ * @returns RiskActivity
22
+ * @protected
23
+ */
18
24
  protected createRiskActivity(context: IContext): RiskActivity;
25
+ /**
26
+ * Protected function in case expansions or alterations to the risk activity are needed for certain platforms.
27
+ * @param riskActivity
28
+ * @returns RiskActivity
29
+ * @protected
30
+ */
31
+ protected finalizeRiskActivity(riskActivity: RiskActivity): RiskActivity;
19
32
  private addOptionalRiskFields;
20
33
  private addOptionalRiskFieldsToRoot;
21
34
  private addOptionalRiskFieldsToAdditional;
@@ -117,6 +117,12 @@ var PostRiskApiClient = /** @class */ (function () {
117
117
  });
118
118
  });
119
119
  };
120
+ /**
121
+ * Creates the RiskActivity payload
122
+ * @param context
123
+ * @returns RiskActivity
124
+ * @protected
125
+ */
120
126
  PostRiskApiClient.prototype.createRiskActivity = function (context) {
121
127
  var riskActivity = {
122
128
  request: {
@@ -136,6 +142,15 @@ var PostRiskApiClient = /** @class */ (function () {
136
142
  },
137
143
  };
138
144
  this.addOptionalRiskFields(riskActivity, context);
145
+ return this.finalizeRiskActivity(riskActivity);
146
+ };
147
+ /**
148
+ * Protected function in case expansions or alterations to the risk activity are needed for certain platforms.
149
+ * @param riskActivity
150
+ * @returns RiskActivity
151
+ * @protected
152
+ */
153
+ PostRiskApiClient.prototype.finalizeRiskActivity = function (riskActivity) {
139
154
  return riskActivity;
140
155
  };
141
156
  PostRiskApiClient.prototype.addOptionalRiskFields = function (riskActivity, context) {
@@ -97,7 +97,7 @@ var RiskTokenScoreRetriever = /** @class */ (function () {
97
97
  case 0: return [4 /*yield*/, token.verify(context)];
98
98
  case 1:
99
99
  result = _a.sent();
100
- if (result === TokenVerificationResult_1.TokenVerificationResult.DECRYPTION_FAILED) {
100
+ if (result === TokenVerificationResult_1.TokenVerificationResult.DECRYPTION_FAILED || result === TokenVerificationResult_1.TokenVerificationResult.CANNOT_VERIFY) {
101
101
  return [2 /*return*/, this.handleFailure(context, risk_api_1.S2SCallReason.COOKIE_DECRYPTION_FAILED)];
102
102
  }
103
103
  if (result === TokenVerificationResult_1.TokenVerificationResult.VALIDATION_FAILED) {
@@ -7,4 +7,4 @@ export declare const BYPASS_MONITOR_HEADER_VALUE = "1";
7
7
  export declare const X_PX_AUTHORIZATION_HEADER_NAME = "x-px-authorization";
8
8
  export declare const X_PX_ORIGINAL_TOKEN_HEADER_NAME = "x-px-original-token";
9
9
  export declare const X_PX_BYPASS_REASON_HEADER_NAME = "x-px-bypass-reason";
10
- export declare const CORE_MODULE_VERSION = "JS Core 0.1.1";
10
+ export declare const CORE_MODULE_VERSION = "JS Core 0.2.0";
@@ -10,4 +10,4 @@ exports.BYPASS_MONITOR_HEADER_VALUE = '1';
10
10
  exports.X_PX_AUTHORIZATION_HEADER_NAME = 'x-px-authorization';
11
11
  exports.X_PX_ORIGINAL_TOKEN_HEADER_NAME = 'x-px-original-token';
12
12
  exports.X_PX_BYPASS_REASON_HEADER_NAME = 'x-px-bypass-reason';
13
- exports.CORE_MODULE_VERSION = 'JS Core 0.1.1';
13
+ exports.CORE_MODULE_VERSION = 'JS Core 0.2.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "perimeterx-js-core",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -15,7 +15,7 @@
15
15
  "test": "mocha",
16
16
  "coverage": "nyc npm run test",
17
17
  "pre-commit": "./node_modules/.bin/lint-staged",
18
- "postinstall": "./node_modules/.bin/husky install"
18
+ "prepare": "husky install"
19
19
  },
20
20
  "dependencies": {
21
21
  "cookie": "^0.5.0",