perimeterx-js-core 0.24.1 → 0.24.3

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.
@@ -126,16 +126,8 @@ var DefaultGraphQLParser = /** @class */ (function () {
126
126
  return __awaiter(this, void 0, void 0, function () {
127
127
  var operationNameToTypeMap;
128
128
  return __generator(this, function (_a) {
129
- if (!operation.query || typeof operation.query !== 'string') {
130
- context.logger.debug('no query found');
131
- return [2 /*return*/, null];
132
- }
133
- operationNameToTypeMap = this.getOperationNameToTypeMap(operation.query);
134
- if (!operationNameToTypeMap) {
135
- context.logger.debug('operationNameToTypeMap returned null');
136
- return [2 /*return*/, null];
137
- }
138
- return [2 /*return*/, this.getGraphQLData(operationNameToTypeMap, operation, context)];
129
+ operationNameToTypeMap = operation.query ? this.getOperationNameToTypeMap(operation.query) : null;
130
+ return [2 /*return*/, this.getGraphQLData(operation, context, operationNameToTypeMap)];
139
131
  });
140
132
  });
141
133
  };
@@ -147,17 +139,17 @@ var DefaultGraphQLParser = /** @class */ (function () {
147
139
  while ((match = pattern.exec(query)) !== null) {
148
140
  var operationType = match[1];
149
141
  var operationName = match[2];
150
- if ((0, utils_1.isNullOrUndefined)(operationName) || map[operationName]) {
151
- // query contains two operations with the same name which is illegal
152
- return null;
153
- }
154
- else {
155
- map[operationName] = operationType;
142
+ if (!(0, utils_1.isNullOrUndefined)(operationName)) {
143
+ // if already exists in map, then query contains two operations with
144
+ // the same name which is illegal, so we use the first one only
145
+ if (!map[operationName]) {
146
+ map[operationName] = operationType;
147
+ }
156
148
  }
157
149
  }
158
150
  return map;
159
151
  };
160
- DefaultGraphQLParser.prototype.getGraphQLData = function (operationNameToTypeMap, operation, context) {
152
+ DefaultGraphQLParser.prototype.getGraphQLData = function (operation, context, operationNameToTypeMap) {
161
153
  return __awaiter(this, void 0, void 0, function () {
162
154
  var name, type, data, keywords;
163
155
  return __generator(this, function (_a) {
@@ -165,9 +157,6 @@ var DefaultGraphQLParser = /** @class */ (function () {
165
157
  case 0:
166
158
  name = this.getOperationName(operationNameToTypeMap, operation);
167
159
  type = this.getOperationType(operation, name, operationNameToTypeMap);
168
- if (!type) {
169
- return [2 /*return*/, null];
170
- }
171
160
  data = { type: type };
172
161
  if (name) {
173
162
  data.name = name;
@@ -190,25 +179,31 @@ var DefaultGraphQLParser = /** @class */ (function () {
190
179
  });
191
180
  };
192
181
  DefaultGraphQLParser.prototype.getOperationType = function (operation, operationName, operationNameToTypeMap) {
193
- if (operationName && operationNameToTypeMap[operationName]) {
182
+ var _a;
183
+ if (operationName && (operationNameToTypeMap === null || operationNameToTypeMap === void 0 ? void 0 : operationNameToTypeMap[operationName])) {
194
184
  return operationNameToTypeMap[operationName];
195
185
  }
196
186
  if (this.isGraphqlQueryShorthand(operation.query)) {
197
187
  return model_1.GraphQLOperationType.QUERY;
198
188
  }
199
- var match = operation.query.match(new RegExp("^\\s*(".concat(Object.values(model_1.GraphQLOperationType).join('|'), ")(?:\\s|{)")));
189
+ var match = (_a = operation.query) === null || _a === void 0 ? void 0 : _a.match(new RegExp("^\\s*(".concat(Object.values(model_1.GraphQLOperationType).join('|'), ")(?:\\s|{)")));
200
190
  if ((match === null || match === void 0 ? void 0 : match[1]) && !operationName) {
201
191
  return match[1];
202
192
  }
203
193
  return null;
204
194
  };
205
195
  DefaultGraphQLParser.prototype.isGraphqlQueryShorthand = function (query) {
206
- return /^\s*{/.test(query);
196
+ return query ? /^\s*{/.test(query) : false;
207
197
  };
208
198
  DefaultGraphQLParser.prototype.getOperationName = function (operationNameToTypeMap, operation) {
209
- var _a;
210
- return (operation.operationName ||
211
- (Object.keys(operationNameToTypeMap).length === 1 ? (_a = Object.keys(operationNameToTypeMap)) === null || _a === void 0 ? void 0 : _a[0] : undefined));
199
+ if (operation.operationName) {
200
+ return operation.operationName;
201
+ }
202
+ if (operationNameToTypeMap) {
203
+ var operationNames = Object.keys(operationNameToTypeMap);
204
+ return operationNames.length === 1 ? operationNames[0] : undefined;
205
+ }
206
+ return undefined;
212
207
  };
213
208
  DefaultGraphQLParser.prototype.getQueryKeywords = function (query, context) {
214
209
  return __awaiter(this, void 0, void 0, function () {
@@ -260,7 +255,7 @@ var DefaultGraphQLParser = /** @class */ (function () {
260
255
  try {
261
256
  this.config.graphqlKeywords.forEach(function (keyword) {
262
257
  var pattern = _this.toGlobalRegExp(keyword);
263
- var matchGroup = query.match(pattern);
258
+ var matchGroup = query === null || query === void 0 ? void 0 : query.match(pattern);
264
259
  if (!matchGroup) {
265
260
  return;
266
261
  }
@@ -161,6 +161,10 @@ var DefaultTelemetry = /** @class */ (function () {
161
161
  osName: 'os_name',
162
162
  nodeName: 'node_name',
163
163
  });
164
+ (0, utils_1.transferExistingProperties)(this.config, activity.details, {
165
+ remoteConfigId: 'remote_config_id',
166
+ remoteConfigVersion: 'remote_config_version',
167
+ });
164
168
  return activity;
165
169
  };
166
170
  return DefaultTelemetry;
@@ -14,4 +14,4 @@ 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]+))?)(\/?[^?#]*)(\?[^#]*|)(#.*|)$/;
16
16
  exports.REGEX_STRUCTURE = /^\/(.+?)\/([gimsuyvd]*)$/;
17
- exports.CORE_MODULE_VERSION = 'JS Core 0.24.1';
17
+ exports.CORE_MODULE_VERSION = 'JS Core 0.24.3';
@@ -54,16 +54,8 @@ export class DefaultGraphQLParser {
54
54
  return data.filter((el) => !isNullOrUndefined(el));
55
55
  }
56
56
  async parseGraphQLOperation(operation, context) {
57
- if (!operation.query || typeof operation.query !== 'string') {
58
- context.logger.debug('no query found');
59
- return null;
60
- }
61
- const operationNameToTypeMap = this.getOperationNameToTypeMap(operation.query);
62
- if (!operationNameToTypeMap) {
63
- context.logger.debug('operationNameToTypeMap returned null');
64
- return null;
65
- }
66
- return this.getGraphQLData(operationNameToTypeMap, operation, context);
57
+ const operationNameToTypeMap = operation.query ? this.getOperationNameToTypeMap(operation.query) : null;
58
+ return this.getGraphQLData(operation, context, operationNameToTypeMap);
67
59
  }
68
60
  getOperationNameToTypeMap(query) {
69
61
  const operationTypesString = Object.values(GraphQLOperationType).join('|');
@@ -73,22 +65,19 @@ export class DefaultGraphQLParser {
73
65
  while ((match = pattern.exec(query)) !== null) {
74
66
  const operationType = match[1];
75
67
  const operationName = match[2];
76
- if (isNullOrUndefined(operationName) || map[operationName]) {
77
- // query contains two operations with the same name which is illegal
78
- return null;
79
- }
80
- else {
81
- map[operationName] = operationType;
68
+ if (!isNullOrUndefined(operationName)) {
69
+ // if already exists in map, then query contains two operations with
70
+ // the same name which is illegal, so we use the first one only
71
+ if (!map[operationName]) {
72
+ map[operationName] = operationType;
73
+ }
82
74
  }
83
75
  }
84
76
  return map;
85
77
  }
86
- async getGraphQLData(operationNameToTypeMap, operation, context) {
78
+ async getGraphQLData(operation, context, operationNameToTypeMap) {
87
79
  const name = this.getOperationName(operationNameToTypeMap, operation);
88
80
  const type = this.getOperationType(operation, name, operationNameToTypeMap);
89
- if (!type) {
90
- return null;
91
- }
92
81
  const data = { type };
93
82
  if (name) {
94
83
  data.name = name;
@@ -106,24 +95,30 @@ export class DefaultGraphQLParser {
106
95
  return data;
107
96
  }
108
97
  getOperationType(operation, operationName, operationNameToTypeMap) {
109
- if (operationName && operationNameToTypeMap[operationName]) {
98
+ if (operationName && operationNameToTypeMap?.[operationName]) {
110
99
  return operationNameToTypeMap[operationName];
111
100
  }
112
101
  if (this.isGraphqlQueryShorthand(operation.query)) {
113
102
  return GraphQLOperationType.QUERY;
114
103
  }
115
- const match = operation.query.match(new RegExp(`^\\s*(${Object.values(GraphQLOperationType).join('|')})(?:\\s|{)`));
104
+ const match = operation.query?.match(new RegExp(`^\\s*(${Object.values(GraphQLOperationType).join('|')})(?:\\s|{)`));
116
105
  if (match?.[1] && !operationName) {
117
106
  return match[1];
118
107
  }
119
108
  return null;
120
109
  }
121
110
  isGraphqlQueryShorthand(query) {
122
- return /^\s*{/.test(query);
111
+ return query ? /^\s*{/.test(query) : false;
123
112
  }
124
113
  getOperationName(operationNameToTypeMap, operation) {
125
- return (operation.operationName ||
126
- (Object.keys(operationNameToTypeMap).length === 1 ? Object.keys(operationNameToTypeMap)?.[0] : undefined));
114
+ if (operation.operationName) {
115
+ return operation.operationName;
116
+ }
117
+ if (operationNameToTypeMap) {
118
+ const operationNames = Object.keys(operationNameToTypeMap);
119
+ return operationNames.length === 1 ? operationNames[0] : undefined;
120
+ }
121
+ return undefined;
127
122
  }
128
123
  async getQueryKeywords(query, context) {
129
124
  if (this.config.extractGraphQLKeywords && typeof this.config.extractGraphQLKeywords === 'function') {
@@ -156,7 +151,7 @@ export class DefaultGraphQLParser {
156
151
  try {
157
152
  this.config.graphqlKeywords.forEach((keyword) => {
158
153
  const pattern = this.toGlobalRegExp(keyword);
159
- let matchGroup = query.match(pattern);
154
+ let matchGroup = query?.match(pattern);
160
155
  if (!matchGroup) {
161
156
  return;
162
157
  }
@@ -83,6 +83,10 @@ export class DefaultTelemetry {
83
83
  osName: 'os_name',
84
84
  nodeName: 'node_name',
85
85
  });
86
+ transferExistingProperties(this.config, activity.details, {
87
+ remoteConfigId: 'remote_config_id',
88
+ remoteConfigVersion: 'remote_config_version',
89
+ });
86
90
  return activity;
87
91
  }
88
92
  }
@@ -11,4 +11,4 @@ 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]+))?)(\/?[^?#]*)(\?[^#]*|)(#.*|)$/;
13
13
  export const REGEX_STRUCTURE = /^\/(.+?)\/([gimsuyvd]*)$/;
14
- export const CORE_MODULE_VERSION = 'JS Core 0.24.1';
14
+ export const CORE_MODULE_VERSION = 'JS Core 0.24.3';
@@ -15,15 +15,15 @@ export declare class DefaultGraphQLParser<Req, Res, Added, Removed> implements I
15
15
  protected parseGraphQLOperations(operations: GraphQLOperation[], context: ReadonlyContext<Req, Res>): Promise<GraphQLData[]>;
16
16
  protected parseGraphQLOperation(operation: GraphQLOperation, context: ReadonlyContext<Req, Res>): Promise<GraphQLData | null>;
17
17
  protected getOperationNameToTypeMap(query: string): Record<string, GraphQLOperationType> | null;
18
- protected getGraphQLData(operationNameToTypeMap: Record<string, GraphQLOperationType>, operation: GraphQLOperation, context: ReadonlyContext<Req, Res>): Promise<GraphQLData | null>;
19
- protected getOperationType(operation: GraphQLOperation, operationName: string | undefined, operationNameToTypeMap: Record<string, GraphQLOperationType>): GraphQLOperationType | null;
20
- protected isGraphqlQueryShorthand(query: string): boolean;
21
- protected getOperationName(operationNameToTypeMap: Record<string, GraphQLOperationType>, operation: GraphQLOperation): string | undefined;
22
- protected getQueryKeywords(query: string, context: ReadonlyContext<Req, Res>): Promise<string[] | null>;
18
+ protected getGraphQLData(operation: GraphQLOperation, context: ReadonlyContext<Req, Res>, operationNameToTypeMap: Record<string, GraphQLOperationType> | null): Promise<GraphQLData | null>;
19
+ protected getOperationType(operation: GraphQLOperation, operationName: string | undefined, operationNameToTypeMap: Record<string, GraphQLOperationType> | null): GraphQLOperationType | null;
20
+ protected isGraphqlQueryShorthand(query: string | undefined): boolean;
21
+ protected getOperationName(operationNameToTypeMap: Record<string, GraphQLOperationType> | null, operation: GraphQLOperation): string | undefined;
22
+ protected getQueryKeywords(query: string | undefined, context: ReadonlyContext<Req, Res>): Promise<string[] | null>;
23
23
  protected cleanKeywords(keywords: string[]): string[];
24
- protected getQueryKeywordsFromCustomFunction(query: string, context: ReadonlyContext<Req, Res>, extractKeywords: ExtractGraphQLKeywordsFunction): Promise<string[] | null>;
25
- protected getQueryKeywordsFromArray(query: string, context: ReadonlyContext<Req, Res>): string[] | null;
26
- protected isSensitiveOperation(operationName: string | undefined, operationType: GraphQLOperationType, keywords: string[] | null): boolean;
24
+ protected getQueryKeywordsFromCustomFunction(query: string | undefined, context: ReadonlyContext<Req, Res>, extractKeywords: ExtractGraphQLKeywordsFunction): Promise<string[] | null>;
25
+ protected getQueryKeywordsFromArray(query: string | undefined, context: ReadonlyContext<Req, Res>): string[] | null;
26
+ protected isSensitiveOperation(operationName: string | undefined, operationType: GraphQLOperationType | null, keywords: string[] | null): boolean;
27
27
  protected extractGraphQLVariableNames(variables: Record<string, unknown>): string[];
28
28
  protected toGlobalRegExp(pattern: string | RegExp): RegExp;
29
29
  }
@@ -1 +1 @@
1
- export type ExtractGraphQLKeywordsFunction = (graphqlQuery: string) => string[] | Promise<string[]>;
1
+ export type ExtractGraphQLKeywordsFunction = (graphqlQuery: string | undefined) => string[] | Promise<string[]>;
@@ -1,6 +1,6 @@
1
1
  import { GraphQLOperationType } from './GraphQLOperationType';
2
2
  export type GraphQLData = {
3
- type?: GraphQLOperationType;
3
+ type?: GraphQLOperationType | null;
4
4
  keywords?: string[];
5
5
  name?: string;
6
6
  sensitive?: boolean;
@@ -1,5 +1,5 @@
1
1
  export type GraphQLOperation = {
2
- query: string;
2
+ query?: string;
3
3
  operationName?: string;
4
4
  variables?: Record<string, unknown>;
5
5
  };
@@ -6,6 +6,8 @@ export type TelemetryActivityDetails<Req, Res, Added, Removed> = {
6
6
  update_reason: 'command';
7
7
  node_name?: string;
8
8
  os_name?: string;
9
+ remote_config_id?: string;
10
+ remote_config_version?: string;
9
11
  };
10
12
  export type TelemetryActivity<Req, Res, Added, Removed> = {
11
13
  type: ActivityType.ENFORCER_TELEMETRY;
@@ -11,4 +11,4 @@ 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
13
  export declare const REGEX_STRUCTURE: RegExp;
14
- export declare const CORE_MODULE_VERSION = "JS Core 0.24.1";
14
+ export declare const CORE_MODULE_VERSION = "JS Core 0.24.3";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "perimeterx-js-core",
3
- "version": "0.24.1",
3
+ "version": "0.24.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "typesVersions": {