graphql 15.10.2 → 15.11.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.
Files changed (99) hide show
  1. package/error/GraphQLError.js +7 -6
  2. package/error/GraphQLError.js.flow +3 -3
  3. package/error/GraphQLError.mjs +8 -7
  4. package/execution/execute.d.ts +2 -0
  5. package/execution/execute.js +33 -12
  6. package/execution/execute.js.flow +32 -11
  7. package/execution/execute.mjs +33 -12
  8. package/execution/values.d.ts +1 -1
  9. package/execution/values.js +5 -5
  10. package/execution/values.js.flow +5 -2
  11. package/execution/values.mjs +4 -4
  12. package/graphql.d.ts +1 -0
  13. package/graphql.js +4 -1
  14. package/graphql.js.flow +5 -1
  15. package/graphql.mjs +4 -1
  16. package/jsutils/SetMap.js +56 -0
  17. package/jsutils/SetMap.js.flow +39 -0
  18. package/jsutils/SetMap.mjs +47 -0
  19. package/language/lexer.js +21 -2
  20. package/language/lexer.js.flow +24 -2
  21. package/language/lexer.mjs +21 -2
  22. package/language/location.d.ts +5 -0
  23. package/language/location.js +64 -0
  24. package/language/location.js.flow +39 -0
  25. package/language/location.mjs +62 -0
  26. package/language/parser.js +34 -19
  27. package/language/parser.js.flow +54 -10
  28. package/language/parser.mjs +34 -19
  29. package/language/printer.d.ts +7 -0
  30. package/language/printer.js +251 -118
  31. package/language/printer.js.flow +345 -141
  32. package/language/printer.mjs +246 -116
  33. package/language/reducer.js +99 -0
  34. package/language/reducer.js.flow +89 -0
  35. package/language/reducer.mjs +89 -0
  36. package/package.json +1 -4
  37. package/subscription/subscribe.d.ts +2 -0
  38. package/subscription/subscribe.js +4 -3
  39. package/subscription/subscribe.js.flow +6 -0
  40. package/subscription/subscribe.mjs +4 -3
  41. package/type/definition.d.ts +2 -1
  42. package/type/definition.js +8 -8
  43. package/type/definition.js.flow +13 -7
  44. package/type/definition.mjs +7 -7
  45. package/type/scalars.js +5 -5
  46. package/type/scalars.js.flow +1 -1
  47. package/type/scalars.mjs +1 -1
  48. package/utilities/buildASTSchema.js +3 -0
  49. package/utilities/buildASTSchema.js.flow +3 -0
  50. package/utilities/buildASTSchema.mjs +3 -0
  51. package/utilities/coerceInputValue.d.ts +1 -0
  52. package/utilities/coerceInputValue.js +9 -8
  53. package/utilities/coerceInputValue.js.flow +42 -9
  54. package/utilities/coerceInputValue.mjs +10 -9
  55. package/utilities/findBreakingChanges.js +3 -23
  56. package/utilities/findBreakingChanges.js.flow +2 -15
  57. package/utilities/findBreakingChanges.mjs +2 -21
  58. package/utilities/sortValueNode.js +52 -0
  59. package/utilities/sortValueNode.js.flow +42 -0
  60. package/utilities/sortValueNode.mjs +42 -0
  61. package/utilities/valueFromAST.js +2 -2
  62. package/utilities/valueFromAST.js.flow +8 -2
  63. package/utilities/valueFromAST.mjs +2 -2
  64. package/validation/ValidationContext.d.ts +8 -0
  65. package/validation/ValidationContext.js +82 -24
  66. package/validation/ValidationContext.js.flow +70 -21
  67. package/validation/ValidationContext.mjs +80 -24
  68. package/validation/rules/FieldsOnCorrectTypeRule.js +2 -2
  69. package/validation/rules/FieldsOnCorrectTypeRule.js.flow +8 -2
  70. package/validation/rules/FieldsOnCorrectTypeRule.mjs +2 -2
  71. package/validation/rules/KnownArgumentNamesRule.js +8 -9
  72. package/validation/rules/KnownArgumentNamesRule.js.flow +16 -6
  73. package/validation/rules/KnownArgumentNamesRule.mjs +8 -9
  74. package/validation/rules/KnownTypeNamesRule.js +1 -1
  75. package/validation/rules/KnownTypeNamesRule.js.flow +6 -4
  76. package/validation/rules/KnownTypeNamesRule.mjs +1 -1
  77. package/validation/rules/MaxIntrospectionDepthRule.js +37 -17
  78. package/validation/rules/MaxIntrospectionDepthRule.js.flow +46 -15
  79. package/validation/rules/MaxIntrospectionDepthRule.mjs +37 -17
  80. package/validation/rules/NoUnusedFragmentsRule.js +6 -13
  81. package/validation/rules/NoUnusedFragmentsRule.js.flow +6 -10
  82. package/validation/rules/NoUnusedFragmentsRule.mjs +6 -13
  83. package/validation/rules/OverlappingFieldsCanBeMergedRule.js +402 -208
  84. package/validation/rules/OverlappingFieldsCanBeMergedRule.js.flow +566 -330
  85. package/validation/rules/OverlappingFieldsCanBeMergedRule.mjs +400 -206
  86. package/validation/rules/ValuesOfCorrectTypeRule.js +6 -6
  87. package/validation/rules/ValuesOfCorrectTypeRule.js.flow +11 -6
  88. package/validation/rules/ValuesOfCorrectTypeRule.mjs +4 -4
  89. package/validation/validate.d.ts +1 -1
  90. package/validation/validate.js +6 -2
  91. package/validation/validate.js.flow +8 -2
  92. package/validation/validate.mjs +5 -2
  93. package/validation/validationAbortedError.d.ts +4 -0
  94. package/validation/validationAbortedError.js +12 -0
  95. package/validation/validationAbortedError.js.flow +7 -0
  96. package/validation/validationAbortedError.mjs +4 -0
  97. package/version.js +3 -3
  98. package/version.js.flow +3 -3
  99. package/version.mjs +3 -3
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Maps an order-independent set of member identities to a value.
3
+ * Each member receives a map-local ID; sorted IDs form the exact key.
4
+ * @internal
5
+ */
6
+ export var SetMap = /*#__PURE__*/function () {
7
+ function SetMap() {
8
+ this._entries = new Map();
9
+ this._memberIds = new Map();
10
+ this._nextMemberId = 0;
11
+ }
12
+
13
+ var _proto = SetMap.prototype;
14
+
15
+ _proto.get = function get(members) {
16
+ return this._entries.get(this._key(members));
17
+ };
18
+
19
+ _proto.set = function set(members, value) {
20
+ this._entries.set(this._key(members), value);
21
+ };
22
+
23
+ _proto._key = function _key(members) {
24
+ var ids = [];
25
+
26
+ for (var _i2 = 0, _Array$from2 = Array.from(members); _i2 < _Array$from2.length; _i2++) {
27
+ var member = _Array$from2[_i2];
28
+
29
+ var id = this._memberIds.get(member);
30
+
31
+ if (id === undefined) {
32
+ id = this._nextMemberId++;
33
+
34
+ this._memberIds.set(member, id);
35
+ }
36
+
37
+ ids.push(id);
38
+ }
39
+
40
+ ids.sort(function (id1, id2) {
41
+ return id1 - id2;
42
+ });
43
+ return ids.join(',');
44
+ };
45
+
46
+ return SetMap;
47
+ }();
package/language/lexer.js CHANGED
@@ -38,6 +38,10 @@ var Lexer = /*#__PURE__*/function () {
38
38
  /**
39
39
  * The character offset at which the current line begins.
40
40
  */
41
+
42
+ /** @internal */
43
+
44
+ /** @internal */
41
45
  function Lexer(source) {
42
46
  var startOfFileToken = new _ast.Token(_tokenKind.TokenKind.SOF, 0, 0, 0, 0, null);
43
47
  this.source = source;
@@ -45,6 +49,8 @@ var Lexer = /*#__PURE__*/function () {
45
49
  this.token = startOfFileToken;
46
50
  this.line = 1;
47
51
  this.lineStart = 0;
52
+ this._maxComments = undefined;
53
+ this._commentCount = 0;
48
54
  }
49
55
  /**
50
56
  * Advances the token stream to the next non-ignored token.
@@ -158,7 +164,7 @@ function readToken(lexer, prev) {
158
164
 
159
165
  case 35:
160
166
  // #
161
- return readComment(source, pos, _line, _col, prev);
167
+ return readComment(lexer, pos, _line, _col, prev);
162
168
 
163
169
  case 36:
164
170
  // $
@@ -388,7 +394,20 @@ function unexpectedCharacterMessage(code) {
388
394
  */
389
395
 
390
396
 
391
- function readComment(source, start, line, col, prev) {
397
+ function readComment(lexer, start, line, col, prev) {
398
+ var maxComments = lexer._maxComments;
399
+
400
+ if (maxComments !== undefined) {
401
+ var nextCommentCount = lexer._commentCount + 1;
402
+
403
+ if (nextCommentCount > maxComments) {
404
+ throw (0, _syntaxError.syntaxError)(lexer.source, start, "Document contains more than ".concat(maxComments, " comments. Parsing aborted."));
405
+ }
406
+
407
+ lexer._commentCount = nextCommentCount;
408
+ }
409
+
410
+ var source = lexer.source;
392
411
  var body = source.body;
393
412
  var code;
394
413
  var position = start;
@@ -38,6 +38,12 @@ export class Lexer {
38
38
  */
39
39
  lineStart: number;
40
40
 
41
+ /** @internal */
42
+ _maxComments: number | void;
43
+
44
+ /** @internal */
45
+ _commentCount: number;
46
+
41
47
  constructor(source: Source) {
42
48
  const startOfFileToken = new Token(TokenKind.SOF, 0, 0, 0, 0, null);
43
49
 
@@ -46,6 +52,8 @@ export class Lexer {
46
52
  this.token = startOfFileToken;
47
53
  this.line = 1;
48
54
  this.lineStart = 0;
55
+ this._maxComments = undefined;
56
+ this._commentCount = 0;
49
57
  }
50
58
 
51
59
  /**
@@ -152,7 +160,7 @@ function readToken(lexer: Lexer, prev: Token): Token {
152
160
  case 33: // !
153
161
  return new Token(TokenKind.BANG, pos, pos + 1, line, col, prev);
154
162
  case 35: // #
155
- return readComment(source, pos, line, col, prev);
163
+ return readComment(lexer, pos, line, col, prev);
156
164
  case 36: // $
157
165
  return new Token(TokenKind.DOLLAR, pos, pos + 1, line, col, prev);
158
166
  case 38: // &
@@ -291,12 +299,26 @@ function unexpectedCharacterMessage(code: number): string {
291
299
  * #[\u0009\u0020-\uFFFF]*
292
300
  */
293
301
  function readComment(
294
- source: Source,
302
+ lexer: Lexer,
295
303
  start: number,
296
304
  line: number,
297
305
  col: number,
298
306
  prev: Token | null,
299
307
  ): Token {
308
+ const maxComments = lexer._maxComments;
309
+ if (maxComments !== undefined) {
310
+ const nextCommentCount = lexer._commentCount + 1;
311
+ if (nextCommentCount > maxComments) {
312
+ throw syntaxError(
313
+ lexer.source,
314
+ start,
315
+ `Document contains more than ${maxComments} comments. Parsing aborted.`,
316
+ );
317
+ }
318
+ lexer._commentCount = nextCommentCount;
319
+ }
320
+
321
+ const source = lexer.source;
300
322
  const body = source.body;
301
323
  let code;
302
324
  let position = start;
@@ -27,6 +27,10 @@ export var Lexer = /*#__PURE__*/function () {
27
27
  /**
28
28
  * The character offset at which the current line begins.
29
29
  */
30
+
31
+ /** @internal */
32
+
33
+ /** @internal */
30
34
  function Lexer(source) {
31
35
  var startOfFileToken = new Token(TokenKind.SOF, 0, 0, 0, 0, null);
32
36
  this.source = source;
@@ -34,6 +38,8 @@ export var Lexer = /*#__PURE__*/function () {
34
38
  this.token = startOfFileToken;
35
39
  this.line = 1;
36
40
  this.lineStart = 0;
41
+ this._maxComments = undefined;
42
+ this._commentCount = 0;
37
43
  }
38
44
  /**
39
45
  * Advances the token stream to the next non-ignored token.
@@ -144,7 +150,7 @@ function readToken(lexer, prev) {
144
150
 
145
151
  case 35:
146
152
  // #
147
- return readComment(source, pos, _line, _col, prev);
153
+ return readComment(lexer, pos, _line, _col, prev);
148
154
 
149
155
  case 36:
150
156
  // $
@@ -374,7 +380,20 @@ function unexpectedCharacterMessage(code) {
374
380
  */
375
381
 
376
382
 
377
- function readComment(source, start, line, col, prev) {
383
+ function readComment(lexer, start, line, col, prev) {
384
+ var maxComments = lexer._maxComments;
385
+
386
+ if (maxComments !== undefined) {
387
+ var nextCommentCount = lexer._commentCount + 1;
388
+
389
+ if (nextCommentCount > maxComments) {
390
+ throw syntaxError(lexer.source, start, "Document contains more than ".concat(maxComments, " comments. Parsing aborted."));
391
+ }
392
+
393
+ lexer._commentCount = nextCommentCount;
394
+ }
395
+
396
+ var source = lexer.source;
378
397
  var body = source.body;
379
398
  var code;
380
399
  var position = start;
@@ -13,3 +13,8 @@ export interface SourceLocation {
13
13
  * line and column as a SourceLocation.
14
14
  */
15
15
  export function getLocation(source: Source, position: number): SourceLocation;
16
+
17
+ /** @internal */
18
+ export function getLocations(
19
+ locations: ReadonlyArray<{ source: Source; start: number }>,
20
+ ): Array<SourceLocation>;
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getLocation = getLocation;
7
+ exports.getLocations = getLocations;
7
8
 
8
9
  /**
9
10
  * Represents a location in a Source.
@@ -29,3 +30,66 @@ function getLocation(source, position) {
29
30
  column: column
30
31
  };
31
32
  }
33
+ /** @internal */
34
+
35
+
36
+ function getLocations(locations) {
37
+ if (locations.length < 2) {
38
+ return locations.map(function (_ref) {
39
+ var source = _ref.source,
40
+ start = _ref.start;
41
+ return getLocation(source, start);
42
+ });
43
+ }
44
+
45
+ var bySource = new Map();
46
+ locations.forEach(function (_ref2, index) {
47
+ var source = _ref2.source,
48
+ start = _ref2.start;
49
+ var positions = bySource.get(source);
50
+
51
+ if (positions === undefined) {
52
+ positions = [];
53
+ bySource.set(source, positions);
54
+ }
55
+
56
+ positions.push({
57
+ position: start,
58
+ index: index
59
+ });
60
+ });
61
+ var result = new Array(locations.length);
62
+
63
+ for (var _i2 = 0, _Array$from2 = Array.from(bySource); _i2 < _Array$from2.length; _i2++) {
64
+ var _ref4 = _Array$from2[_i2];
65
+ var source = _ref4[0];
66
+ var positions = _ref4[1];
67
+ var lineRegexp = /\r\n|[\n\r]/g;
68
+ var match = lineRegexp.exec(source.body);
69
+ var line = 1;
70
+ var lastLineStart = 0; // These arrays are local; sorting never changes the caller's input.
71
+
72
+ positions.sort(function (a, b) {
73
+ return a.position - b.position;
74
+ });
75
+
76
+ for (var _i4 = 0; _i4 < positions.length; _i4++) {
77
+ var _ref6 = positions[_i4];
78
+ var position = _ref6.position;
79
+ var index = _ref6.index;
80
+
81
+ while (match !== null && match.index < position) {
82
+ lastLineStart = match.index + match[0].length;
83
+ line++;
84
+ match = lineRegexp.exec(source.body);
85
+ }
86
+
87
+ result[index] = {
88
+ line: line,
89
+ column: position + 1 - lastLineStart
90
+ };
91
+ }
92
+ }
93
+
94
+ return result;
95
+ }
@@ -24,3 +24,42 @@ export function getLocation(source: Source, position: number): SourceLocation {
24
24
  }
25
25
  return { line, column };
26
26
  }
27
+
28
+ /** @internal */
29
+ export function getLocations(
30
+ locations: $ReadOnlyArray<{ +source: Source, +start: number, ... }>,
31
+ ): Array<SourceLocation> {
32
+ if (locations.length < 2) {
33
+ return locations.map(({ source, start }) => getLocation(source, start));
34
+ }
35
+ const bySource: Map<
36
+ Source,
37
+ Array<{| position: number, index: number |}>,
38
+ > = new Map();
39
+ locations.forEach(({ source, start }, index) => {
40
+ let positions = bySource.get(source);
41
+ if (positions === undefined) {
42
+ positions = [];
43
+ bySource.set(source, positions);
44
+ }
45
+ positions.push({ position: start, index });
46
+ });
47
+ const result: Array<SourceLocation> = new Array(locations.length);
48
+ for (const [source, positions] of Array.from(bySource)) {
49
+ const lineRegexp = /\r\n|[\n\r]/g;
50
+ let match = lineRegexp.exec(source.body);
51
+ let line = 1;
52
+ let lastLineStart = 0;
53
+ // These arrays are local; sorting never changes the caller's input.
54
+ positions.sort((a, b) => a.position - b.position);
55
+ for (const { position, index } of positions) {
56
+ while (match !== null && match.index < position) {
57
+ lastLineStart = match.index + match[0].length;
58
+ line++;
59
+ match = lineRegexp.exec(source.body);
60
+ }
61
+ result[index] = { line, column: position + 1 - lastLineStart };
62
+ }
63
+ }
64
+ return result;
65
+ }
@@ -22,3 +22,65 @@ export function getLocation(source, position) {
22
22
  column: column
23
23
  };
24
24
  }
25
+ /** @internal */
26
+
27
+ export function getLocations(locations) {
28
+ if (locations.length < 2) {
29
+ return locations.map(function (_ref) {
30
+ var source = _ref.source,
31
+ start = _ref.start;
32
+ return getLocation(source, start);
33
+ });
34
+ }
35
+
36
+ var bySource = new Map();
37
+ locations.forEach(function (_ref2, index) {
38
+ var source = _ref2.source,
39
+ start = _ref2.start;
40
+ var positions = bySource.get(source);
41
+
42
+ if (positions === undefined) {
43
+ positions = [];
44
+ bySource.set(source, positions);
45
+ }
46
+
47
+ positions.push({
48
+ position: start,
49
+ index: index
50
+ });
51
+ });
52
+ var result = new Array(locations.length);
53
+
54
+ for (var _i2 = 0, _Array$from2 = Array.from(bySource); _i2 < _Array$from2.length; _i2++) {
55
+ var _ref4 = _Array$from2[_i2];
56
+ var source = _ref4[0];
57
+ var positions = _ref4[1];
58
+ var lineRegexp = /\r\n|[\n\r]/g;
59
+ var match = lineRegexp.exec(source.body);
60
+ var line = 1;
61
+ var lastLineStart = 0; // These arrays are local; sorting never changes the caller's input.
62
+
63
+ positions.sort(function (a, b) {
64
+ return a.position - b.position;
65
+ });
66
+
67
+ for (var _i4 = 0; _i4 < positions.length; _i4++) {
68
+ var _ref6 = positions[_i4];
69
+ var position = _ref6.position;
70
+ var index = _ref6.index;
71
+
72
+ while (match !== null && match.index < position) {
73
+ lastLineStart = match.index + match[0].length;
74
+ line++;
75
+ match = lineRegexp.exec(source.body);
76
+ }
77
+
78
+ result[index] = {
79
+ line: line,
80
+ column: position + 1 - lastLineStart
81
+ };
82
+ }
83
+ }
84
+
85
+ return result;
86
+ }
@@ -86,6 +86,14 @@ var Parser = /*#__PURE__*/function () {
86
86
  var sourceObj = (0, _source.isSource)(source) ? source : new _source.Source(source);
87
87
  this._lexer = new _lexer.Lexer(sourceObj);
88
88
  this._options = options;
89
+ this._tokenCounter = 0;
90
+ this._lexer._maxComments = options === null || options === void 0 ? void 0 : options.maxComments;
91
+ this._lexer._commentCount = 0;
92
+ var maxSourceLength = options === null || options === void 0 ? void 0 : options.maxSourceLength;
93
+
94
+ if (maxSourceLength !== undefined && this._lexer.source.body.length > maxSourceLength) {
95
+ throw (0, _syntaxError.syntaxError)(this._lexer.source, maxSourceLength, "Source contains more than ".concat(maxSourceLength, " UTF-16 code units. Parsing aborted."));
96
+ }
89
97
  }
90
98
  /**
91
99
  * Converts a name lex token into a name parse node.
@@ -461,8 +469,7 @@ var Parser = /*#__PURE__*/function () {
461
469
  return this.parseObject(isConst);
462
470
 
463
471
  case _tokenKind.TokenKind.INT:
464
- this._lexer.advance();
465
-
472
+ this.advanceLexer();
466
473
  return {
467
474
  kind: _kinds.Kind.INT,
468
475
  value: token.value,
@@ -470,8 +477,7 @@ var Parser = /*#__PURE__*/function () {
470
477
  };
471
478
 
472
479
  case _tokenKind.TokenKind.FLOAT:
473
- this._lexer.advance();
474
-
480
+ this.advanceLexer();
475
481
  return {
476
482
  kind: _kinds.Kind.FLOAT,
477
483
  value: token.value,
@@ -483,7 +489,7 @@ var Parser = /*#__PURE__*/function () {
483
489
  return this.parseStringLiteral();
484
490
 
485
491
  case _tokenKind.TokenKind.NAME:
486
- this._lexer.advance();
492
+ this.advanceLexer();
487
493
 
488
494
  switch (token.value) {
489
495
  case 'true':
@@ -527,9 +533,7 @@ var Parser = /*#__PURE__*/function () {
527
533
 
528
534
  _proto.parseStringLiteral = function parseStringLiteral() {
529
535
  var token = this._lexer.token;
530
-
531
- this._lexer.advance();
532
-
536
+ this.advanceLexer();
533
537
  return {
534
538
  kind: _kinds.Kind.STRING,
535
539
  value: token.value,
@@ -858,10 +862,8 @@ var Parser = /*#__PURE__*/function () {
858
862
 
859
863
  // Legacy support for the SDL?
860
864
  if (((_this$_options3 = this._options) === null || _this$_options3 === void 0 ? void 0 : _this$_options3.allowLegacySDLEmptyFields) === true && this.peek(_tokenKind.TokenKind.BRACE_L) && this._lexer.lookahead().kind === _tokenKind.TokenKind.BRACE_R) {
861
- this._lexer.advance();
862
-
863
- this._lexer.advance();
864
-
865
+ this.advanceLexer();
866
+ this.advanceLexer();
865
867
  return [];
866
868
  }
867
869
 
@@ -1405,8 +1407,7 @@ var Parser = /*#__PURE__*/function () {
1405
1407
  var token = this._lexer.token;
1406
1408
 
1407
1409
  if (token.kind === kind) {
1408
- this._lexer.advance();
1409
-
1410
+ this.advanceLexer();
1410
1411
  return token;
1411
1412
  }
1412
1413
 
@@ -1422,8 +1423,7 @@ var Parser = /*#__PURE__*/function () {
1422
1423
  var token = this._lexer.token;
1423
1424
 
1424
1425
  if (token.kind === kind) {
1425
- this._lexer.advance();
1426
-
1426
+ this.advanceLexer();
1427
1427
  return token;
1428
1428
  }
1429
1429
 
@@ -1439,7 +1439,7 @@ var Parser = /*#__PURE__*/function () {
1439
1439
  var token = this._lexer.token;
1440
1440
 
1441
1441
  if (token.kind === _tokenKind.TokenKind.NAME && token.value === value) {
1442
- this._lexer.advance();
1442
+ this.advanceLexer();
1443
1443
  } else {
1444
1444
  throw (0, _syntaxError.syntaxError)(this._lexer.source, token.start, "Expected \"".concat(value, "\", found ").concat(getTokenDesc(token), "."));
1445
1445
  }
@@ -1454,8 +1454,7 @@ var Parser = /*#__PURE__*/function () {
1454
1454
  var token = this._lexer.token;
1455
1455
 
1456
1456
  if (token.kind === _tokenKind.TokenKind.NAME && token.value === value) {
1457
- this._lexer.advance();
1458
-
1457
+ this.advanceLexer();
1459
1458
  return true;
1460
1459
  }
1461
1460
 
@@ -1543,6 +1542,22 @@ var Parser = /*#__PURE__*/function () {
1543
1542
  return nodes;
1544
1543
  };
1545
1544
 
1545
+ _proto.advanceLexer = function advanceLexer() {
1546
+ var _this$_options5;
1547
+
1548
+ var maxLexicalTokens = (_this$_options5 = this._options) === null || _this$_options5 === void 0 ? void 0 : _this$_options5.maxLexicalTokens;
1549
+
1550
+ var token = this._lexer.advance();
1551
+
1552
+ if (token.kind !== _tokenKind.TokenKind.EOF) {
1553
+ ++this._tokenCounter;
1554
+
1555
+ if (maxLexicalTokens !== undefined && this._tokenCounter > maxLexicalTokens) {
1556
+ throw (0, _syntaxError.syntaxError)(this._lexer.source, token.start, "Document contains more than ".concat(maxLexicalTokens, " tokens. Parsing aborted."));
1557
+ }
1558
+ }
1559
+ };
1560
+
1546
1561
  return Parser;
1547
1562
  }();
1548
1563
  /**
@@ -67,6 +67,15 @@ export type ParseOptions = {|
67
67
  */
68
68
  noLocation?: boolean,
69
69
 
70
+ /** Maximum number of non-comment tokens allowed in a document. */
71
+ maxLexicalTokens?: number,
72
+
73
+ /** Maximum number of comments. */
74
+ maxComments?: number,
75
+
76
+ /** Maximum source length in UTF-16 code units. */
77
+ maxSourceLength?: number,
78
+
70
79
  /**
71
80
  * If enabled, the parser will parse empty fields sets in the Schema
72
81
  * Definition Language. Otherwise, the parser will follow the current
@@ -174,12 +183,29 @@ export function parseType(
174
183
  export class Parser {
175
184
  _options: ?ParseOptions;
176
185
  _lexer: Lexer;
186
+ _tokenCounter: number;
177
187
 
178
188
  constructor(source: string | Source, options?: ParseOptions) {
179
189
  const sourceObj = isSource(source) ? source : new Source(source);
180
190
 
181
191
  this._lexer = new Lexer(sourceObj);
182
192
  this._options = options;
193
+ this._tokenCounter = 0;
194
+
195
+ this._lexer._maxComments = options?.maxComments;
196
+ this._lexer._commentCount = 0;
197
+
198
+ const maxSourceLength = options?.maxSourceLength;
199
+ if (
200
+ maxSourceLength !== undefined &&
201
+ this._lexer.source.body.length > maxSourceLength
202
+ ) {
203
+ throw syntaxError(
204
+ this._lexer.source,
205
+ maxSourceLength,
206
+ `Source contains more than ${maxSourceLength} UTF-16 code units. Parsing aborted.`,
207
+ );
208
+ }
183
209
  }
184
210
 
185
211
  /**
@@ -541,14 +567,14 @@ export class Parser {
541
567
  case TokenKind.BRACE_L:
542
568
  return this.parseObject(isConst);
543
569
  case TokenKind.INT:
544
- this._lexer.advance();
570
+ this.advanceLexer();
545
571
  return {
546
572
  kind: Kind.INT,
547
573
  value: ((token.value: any): string),
548
574
  loc: this.loc(token),
549
575
  };
550
576
  case TokenKind.FLOAT:
551
- this._lexer.advance();
577
+ this.advanceLexer();
552
578
  return {
553
579
  kind: Kind.FLOAT,
554
580
  value: ((token.value: any): string),
@@ -558,7 +584,7 @@ export class Parser {
558
584
  case TokenKind.BLOCK_STRING:
559
585
  return this.parseStringLiteral();
560
586
  case TokenKind.NAME:
561
- this._lexer.advance();
587
+ this.advanceLexer();
562
588
  switch (token.value) {
563
589
  case 'true':
564
590
  return { kind: Kind.BOOLEAN, value: true, loc: this.loc(token) };
@@ -584,7 +610,7 @@ export class Parser {
584
610
 
585
611
  parseStringLiteral(): StringValueNode {
586
612
  const token = this._lexer.token;
587
- this._lexer.advance();
613
+ this.advanceLexer();
588
614
  return {
589
615
  kind: Kind.STRING,
590
616
  value: ((token.value: any): string),
@@ -886,8 +912,8 @@ export class Parser {
886
912
  this.peek(TokenKind.BRACE_L) &&
887
913
  this._lexer.lookahead().kind === TokenKind.BRACE_R
888
914
  ) {
889
- this._lexer.advance();
890
- this._lexer.advance();
915
+ this.advanceLexer();
916
+ this.advanceLexer();
891
917
  return [];
892
918
  }
893
919
  return this.optionalMany(
@@ -1413,7 +1439,7 @@ export class Parser {
1413
1439
  expectToken(kind: TokenKindEnum): Token {
1414
1440
  const token = this._lexer.token;
1415
1441
  if (token.kind === kind) {
1416
- this._lexer.advance();
1442
+ this.advanceLexer();
1417
1443
  return token;
1418
1444
  }
1419
1445
 
@@ -1431,7 +1457,7 @@ export class Parser {
1431
1457
  expectOptionalToken(kind: TokenKindEnum): ?Token {
1432
1458
  const token = this._lexer.token;
1433
1459
  if (token.kind === kind) {
1434
- this._lexer.advance();
1460
+ this.advanceLexer();
1435
1461
  return token;
1436
1462
  }
1437
1463
  return undefined;
@@ -1444,7 +1470,7 @@ export class Parser {
1444
1470
  expectKeyword(value: string) {
1445
1471
  const token = this._lexer.token;
1446
1472
  if (token.kind === TokenKind.NAME && token.value === value) {
1447
- this._lexer.advance();
1473
+ this.advanceLexer();
1448
1474
  } else {
1449
1475
  throw syntaxError(
1450
1476
  this._lexer.source,
@@ -1461,7 +1487,7 @@ export class Parser {
1461
1487
  expectOptionalKeyword(value: string): boolean {
1462
1488
  const token = this._lexer.token;
1463
1489
  if (token.kind === TokenKind.NAME && token.value === value) {
1464
- this._lexer.advance();
1490
+ this.advanceLexer();
1465
1491
  return true;
1466
1492
  }
1467
1493
  return false;
@@ -1550,6 +1576,24 @@ export class Parser {
1550
1576
  } while (this.expectOptionalToken(delimiterKind));
1551
1577
  return nodes;
1552
1578
  }
1579
+
1580
+ advanceLexer(): void {
1581
+ const maxLexicalTokens = this._options?.maxLexicalTokens;
1582
+ const token = this._lexer.advance();
1583
+ if (token.kind !== TokenKind.EOF) {
1584
+ ++this._tokenCounter;
1585
+ if (
1586
+ maxLexicalTokens !== undefined &&
1587
+ this._tokenCounter > maxLexicalTokens
1588
+ ) {
1589
+ throw syntaxError(
1590
+ this._lexer.source,
1591
+ token.start,
1592
+ `Document contains more than ${maxLexicalTokens} tokens. Parsing aborted.`,
1593
+ );
1594
+ }
1595
+ }
1596
+ }
1553
1597
  }
1554
1598
 
1555
1599
  /**