@escape.tech/graphql-armor-max-tokens
Advanced tools
Comparing version 1.0.2 to 1.1.0
import { Plugin } from '@envelop/types'; | ||
import { GraphQLArmorCallbackConfiguration } from '@escape.tech/graphql-armor-types'; | ||
import { Source } from 'graphql'; | ||
@@ -6,13 +7,14 @@ import { ParseOptions, Parser } from 'graphql/language/parser'; | ||
n: number; | ||
}; | ||
declare class MaxTokensParserWLexer extends Parser { | ||
} & GraphQLArmorCallbackConfiguration; | ||
export declare type MaxTokensOptions = { | ||
n?: number; | ||
} & GraphQLArmorCallbackConfiguration; | ||
export declare const maxTokenDefaultOptions: Required<MaxTokensOptions>; | ||
export declare class MaxTokensParserWLexer extends Parser { | ||
private _tokenCount; | ||
private readonly config; | ||
get tokenCount(): number; | ||
constructor(source: string | Source, options: maxTokensParserWLexerOptions); | ||
constructor(source: string | Source, options?: maxTokensParserWLexerOptions); | ||
} | ||
declare type MaxTokensOptions = { | ||
n?: number; | ||
}; | ||
declare const maxTokenDefaultOptions: Required<MaxTokensOptions>; | ||
declare function maxTokensPlugin(options?: MaxTokensOptions): Plugin; | ||
export { MaxTokensOptions, maxTokenDefaultOptions, maxTokensPlugin, MaxTokensParserWLexer }; | ||
export declare function maxTokensPlugin(config?: MaxTokensOptions): Plugin; | ||
export {}; |
@@ -6,2 +6,3 @@ 'use strict'; | ||
var graphql = require('graphql'); | ||
var error = require('graphql/error'); | ||
var parser = require('graphql/language/parser'); | ||
@@ -24,2 +25,8 @@ | ||
const maxTokenDefaultOptions = { | ||
n: 1000, | ||
onAccept: [], | ||
onReject: [], | ||
throwOnRejection: true | ||
}; | ||
class MaxTokensParserWLexer extends parser.Parser { | ||
@@ -35,2 +42,7 @@ get tokenCount() { | ||
_defineProperty(this, "config", void 0); | ||
this.config = Object.assign({}, maxTokenDefaultOptions, ...Object.entries(options !== null && options !== void 0 ? options : {}).map(([k, v]) => v === undefined ? {} : { | ||
[k]: v | ||
})); | ||
const lexer = this._lexer; | ||
@@ -47,6 +59,17 @@ this._lexer = new Proxy(lexer, { | ||
if (this._tokenCount > options.n) { | ||
throw new graphql.GraphQLError(`Syntax Error: Token limit of ${options.n} exceeded, found ${this._tokenCount}.`, { | ||
source: this._lexer.source, | ||
positions: [token.start] | ||
if (this._tokenCount > this.config.n) { | ||
const err = error.syntaxError(this._lexer.source, token.start, `Token limit of ${this.config.n} exceeded, found ${this._tokenCount}.`); | ||
for (const handler of this.config.onReject) { | ||
handler(null, err); | ||
} | ||
if (this.config.throwOnRejection) { | ||
throw err; | ||
} | ||
} | ||
for (const handler of this.config.onAccept) { | ||
handler(null, { | ||
n: this._tokenCount | ||
}); | ||
@@ -64,18 +87,6 @@ } | ||
} // new ParserWithLexer(context.source, { ...options, n: options?.n ?? maxTokenDefaultOptions.n }); | ||
const maxTokenDefaultOptions = { | ||
n: 1000 | ||
}; | ||
function maxTokensPlugin(options) { | ||
var _options$n; | ||
const maxTokenCount = (_options$n = options === null || options === void 0 ? void 0 : options.n) !== null && _options$n !== void 0 ? _options$n : maxTokenDefaultOptions.n; | ||
} | ||
function maxTokensPlugin(config) { | ||
function parseWithTokenLimit(source, options) { | ||
const parser = new MaxTokensParserWLexer(source, { ...options, | ||
n: maxTokenCount | ||
}); | ||
const parser = new MaxTokensParserWLexer(source, Object.assign({}, options, config)); | ||
return parser.parseDocument(); | ||
@@ -82,0 +93,0 @@ } |
@@ -6,2 +6,3 @@ 'use strict'; | ||
var graphql = require('graphql'); | ||
var error = require('graphql/error'); | ||
var parser = require('graphql/language/parser'); | ||
@@ -24,2 +25,8 @@ | ||
const maxTokenDefaultOptions = { | ||
n: 1000, | ||
onAccept: [], | ||
onReject: [], | ||
throwOnRejection: true | ||
}; | ||
class MaxTokensParserWLexer extends parser.Parser { | ||
@@ -35,2 +42,7 @@ get tokenCount() { | ||
_defineProperty(this, "config", void 0); | ||
this.config = Object.assign({}, maxTokenDefaultOptions, ...Object.entries(options !== null && options !== void 0 ? options : {}).map(([k, v]) => v === undefined ? {} : { | ||
[k]: v | ||
})); | ||
const lexer = this._lexer; | ||
@@ -47,6 +59,17 @@ this._lexer = new Proxy(lexer, { | ||
if (this._tokenCount > options.n) { | ||
throw new graphql.GraphQLError(`Syntax Error: Token limit of ${options.n} exceeded, found ${this._tokenCount}.`, { | ||
source: this._lexer.source, | ||
positions: [token.start] | ||
if (this._tokenCount > this.config.n) { | ||
const err = error.syntaxError(this._lexer.source, token.start, `Token limit of ${this.config.n} exceeded, found ${this._tokenCount}.`); | ||
for (const handler of this.config.onReject) { | ||
handler(null, err); | ||
} | ||
if (this.config.throwOnRejection) { | ||
throw err; | ||
} | ||
} | ||
for (const handler of this.config.onAccept) { | ||
handler(null, { | ||
n: this._tokenCount | ||
}); | ||
@@ -64,18 +87,6 @@ } | ||
} // new ParserWithLexer(context.source, { ...options, n: options?.n ?? maxTokenDefaultOptions.n }); | ||
const maxTokenDefaultOptions = { | ||
n: 1000 | ||
}; | ||
function maxTokensPlugin(options) { | ||
var _options$n; | ||
const maxTokenCount = (_options$n = options === null || options === void 0 ? void 0 : options.n) !== null && _options$n !== void 0 ? _options$n : maxTokenDefaultOptions.n; | ||
} | ||
function maxTokensPlugin(config) { | ||
function parseWithTokenLimit(source, options) { | ||
const parser = new MaxTokensParserWLexer(source, { ...options, | ||
n: maxTokenCount | ||
}); | ||
const parser = new MaxTokensParserWLexer(source, Object.assign({}, options, config)); | ||
return parser.parseDocument(); | ||
@@ -82,0 +93,0 @@ } |
@@ -1,2 +0,3 @@ | ||
import { TokenKind, GraphQLError } from 'graphql'; | ||
import { TokenKind } from 'graphql'; | ||
import { syntaxError } from 'graphql/error'; | ||
import { Parser } from 'graphql/language/parser'; | ||
@@ -19,2 +20,8 @@ | ||
const maxTokenDefaultOptions = { | ||
n: 1000, | ||
onAccept: [], | ||
onReject: [], | ||
throwOnRejection: true | ||
}; | ||
class MaxTokensParserWLexer extends Parser { | ||
@@ -30,2 +37,7 @@ get tokenCount() { | ||
_defineProperty(this, "config", void 0); | ||
this.config = Object.assign({}, maxTokenDefaultOptions, ...Object.entries(options !== null && options !== void 0 ? options : {}).map(([k, v]) => v === undefined ? {} : { | ||
[k]: v | ||
})); | ||
const lexer = this._lexer; | ||
@@ -42,6 +54,17 @@ this._lexer = new Proxy(lexer, { | ||
if (this._tokenCount > options.n) { | ||
throw new GraphQLError(`Syntax Error: Token limit of ${options.n} exceeded, found ${this._tokenCount}.`, { | ||
source: this._lexer.source, | ||
positions: [token.start] | ||
if (this._tokenCount > this.config.n) { | ||
const err = syntaxError(this._lexer.source, token.start, `Token limit of ${this.config.n} exceeded, found ${this._tokenCount}.`); | ||
for (const handler of this.config.onReject) { | ||
handler(null, err); | ||
} | ||
if (this.config.throwOnRejection) { | ||
throw err; | ||
} | ||
} | ||
for (const handler of this.config.onAccept) { | ||
handler(null, { | ||
n: this._tokenCount | ||
}); | ||
@@ -59,18 +82,6 @@ } | ||
} // new ParserWithLexer(context.source, { ...options, n: options?.n ?? maxTokenDefaultOptions.n }); | ||
const maxTokenDefaultOptions = { | ||
n: 1000 | ||
}; | ||
function maxTokensPlugin(options) { | ||
var _options$n; | ||
const maxTokenCount = (_options$n = options === null || options === void 0 ? void 0 : options.n) !== null && _options$n !== void 0 ? _options$n : maxTokenDefaultOptions.n; | ||
} | ||
function maxTokensPlugin(config) { | ||
function parseWithTokenLimit(source, options) { | ||
const parser = new MaxTokensParserWLexer(source, { ...options, | ||
n: maxTokenCount | ||
}); | ||
const parser = new MaxTokensParserWLexer(source, Object.assign({}, options, config)); | ||
return parser.parseDocument(); | ||
@@ -77,0 +88,0 @@ } |
{ | ||
"name": "@escape.tech/graphql-armor-max-tokens", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Limit the number of tokens allowed in a GraphQL document.", | ||
@@ -36,5 +36,6 @@ "packageManager": "yarn@3.2.3", | ||
"@envelop/types": "2.4.0", | ||
"@escape.tech/graphql-armor-types": "0.1.0", | ||
"graphql": "16.6.0", | ||
"typescript": "4.8.3" | ||
"typescript": "4.8.4" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10430
268
6