@graphql-mesh/plugin-rate-limit 0.0.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.
- package/index.d.ts +3 -0
- package/index.js +91 -0
- package/index.mjs +87 -0
- package/package.json +40 -0
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
4
|
+
|
|
5
|
+
const stringInterpolation = require('@graphql-mesh/string-interpolation');
|
|
6
|
+
const crossHelpers = require('@graphql-mesh/cross-helpers');
|
|
7
|
+
const utils = require('@graphql-tools/utils');
|
|
8
|
+
const minimatch = _interopDefault(require('minimatch'));
|
|
9
|
+
const graphql = require('graphql');
|
|
10
|
+
|
|
11
|
+
function deleteNode(parent, remaining, currentKey) {
|
|
12
|
+
const nextKey = remaining.shift();
|
|
13
|
+
if (nextKey) {
|
|
14
|
+
const nextParent = currentKey ? parent[currentKey] : parent;
|
|
15
|
+
return deleteNode(nextParent, remaining, nextKey);
|
|
16
|
+
}
|
|
17
|
+
delete parent[currentKey];
|
|
18
|
+
}
|
|
19
|
+
function useMeshRateLimit(options) {
|
|
20
|
+
return {
|
|
21
|
+
async onExecute(onExecuteArgs) {
|
|
22
|
+
const typeInfo = new graphql.TypeInfo(onExecuteArgs.args.schema);
|
|
23
|
+
const errors = [];
|
|
24
|
+
const jobs = [];
|
|
25
|
+
let remainingFields = 0;
|
|
26
|
+
graphql.visit(onExecuteArgs.args.document, graphql.visitInParallel(options.config.map(config => {
|
|
27
|
+
const typeMatcher = new minimatch.Minimatch(config.type);
|
|
28
|
+
const fieldMatcher = new minimatch.Minimatch(config.field);
|
|
29
|
+
const identifier = stringInterpolation.stringInterpolator.parse(config.identifier, {
|
|
30
|
+
env: crossHelpers.process.env,
|
|
31
|
+
root: onExecuteArgs.args.rootValue,
|
|
32
|
+
context: onExecuteArgs.args.contextValue,
|
|
33
|
+
});
|
|
34
|
+
return graphql.visitWithTypeInfo(typeInfo, {
|
|
35
|
+
Field: (fieldNode, key, parent, path) => {
|
|
36
|
+
const parentType = typeInfo.getParentType();
|
|
37
|
+
if (typeMatcher.match(parentType.name)) {
|
|
38
|
+
const fieldDef = typeInfo.getFieldDef();
|
|
39
|
+
if (fieldMatcher.match(fieldDef.name)) {
|
|
40
|
+
const cacheKey = `rate-limit-${identifier}-${parentType.name}.${fieldDef.name}`;
|
|
41
|
+
const remainingTokens$ = options.cache.get(cacheKey);
|
|
42
|
+
jobs.push(remainingTokens$.then((remainingTokens) => {
|
|
43
|
+
var _a;
|
|
44
|
+
if (remainingTokens == null) {
|
|
45
|
+
remainingTokens = config.max;
|
|
46
|
+
}
|
|
47
|
+
if (remainingTokens === 0) {
|
|
48
|
+
errors.push(utils.createGraphQLError(`Rate limit of "${parentType.name}.${fieldDef.name}" exceeded for "${identifier}"`, {
|
|
49
|
+
path: [((_a = fieldNode.alias) === null || _a === void 0 ? void 0 : _a.value) || fieldDef.name],
|
|
50
|
+
}));
|
|
51
|
+
deleteNode(parent, [...path]);
|
|
52
|
+
remainingFields--;
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return options.cache.set(cacheKey, remainingTokens - 1, {
|
|
56
|
+
ttl: config.ttl / 1000,
|
|
57
|
+
});
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
remainingFields++;
|
|
62
|
+
return false;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
})));
|
|
66
|
+
await Promise.all(jobs);
|
|
67
|
+
if (errors.length > 0) {
|
|
68
|
+
// If there is a field left in the final selection set
|
|
69
|
+
if (remainingFields > 0) {
|
|
70
|
+
// Add the errors to the final result
|
|
71
|
+
return {
|
|
72
|
+
onExecuteDone(onExecuteDoneArgs) {
|
|
73
|
+
onExecuteDoneArgs.setResult({
|
|
74
|
+
...onExecuteDoneArgs.result,
|
|
75
|
+
errors,
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
// If there is no need to continue the execution, stop
|
|
81
|
+
onExecuteArgs.setResultAndStopExecution({
|
|
82
|
+
data: null,
|
|
83
|
+
errors,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
module.exports = useMeshRateLimit;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
|
|
2
|
+
import { process } from '@graphql-mesh/cross-helpers';
|
|
3
|
+
import { createGraphQLError } from '@graphql-tools/utils';
|
|
4
|
+
import minimatch from 'minimatch';
|
|
5
|
+
import { TypeInfo, visit, visitInParallel, visitWithTypeInfo } from 'graphql';
|
|
6
|
+
|
|
7
|
+
function deleteNode(parent, remaining, currentKey) {
|
|
8
|
+
const nextKey = remaining.shift();
|
|
9
|
+
if (nextKey) {
|
|
10
|
+
const nextParent = currentKey ? parent[currentKey] : parent;
|
|
11
|
+
return deleteNode(nextParent, remaining, nextKey);
|
|
12
|
+
}
|
|
13
|
+
delete parent[currentKey];
|
|
14
|
+
}
|
|
15
|
+
function useMeshRateLimit(options) {
|
|
16
|
+
return {
|
|
17
|
+
async onExecute(onExecuteArgs) {
|
|
18
|
+
const typeInfo = new TypeInfo(onExecuteArgs.args.schema);
|
|
19
|
+
const errors = [];
|
|
20
|
+
const jobs = [];
|
|
21
|
+
let remainingFields = 0;
|
|
22
|
+
visit(onExecuteArgs.args.document, visitInParallel(options.config.map(config => {
|
|
23
|
+
const typeMatcher = new minimatch.Minimatch(config.type);
|
|
24
|
+
const fieldMatcher = new minimatch.Minimatch(config.field);
|
|
25
|
+
const identifier = stringInterpolator.parse(config.identifier, {
|
|
26
|
+
env: process.env,
|
|
27
|
+
root: onExecuteArgs.args.rootValue,
|
|
28
|
+
context: onExecuteArgs.args.contextValue,
|
|
29
|
+
});
|
|
30
|
+
return visitWithTypeInfo(typeInfo, {
|
|
31
|
+
Field: (fieldNode, key, parent, path) => {
|
|
32
|
+
const parentType = typeInfo.getParentType();
|
|
33
|
+
if (typeMatcher.match(parentType.name)) {
|
|
34
|
+
const fieldDef = typeInfo.getFieldDef();
|
|
35
|
+
if (fieldMatcher.match(fieldDef.name)) {
|
|
36
|
+
const cacheKey = `rate-limit-${identifier}-${parentType.name}.${fieldDef.name}`;
|
|
37
|
+
const remainingTokens$ = options.cache.get(cacheKey);
|
|
38
|
+
jobs.push(remainingTokens$.then((remainingTokens) => {
|
|
39
|
+
var _a;
|
|
40
|
+
if (remainingTokens == null) {
|
|
41
|
+
remainingTokens = config.max;
|
|
42
|
+
}
|
|
43
|
+
if (remainingTokens === 0) {
|
|
44
|
+
errors.push(createGraphQLError(`Rate limit of "${parentType.name}.${fieldDef.name}" exceeded for "${identifier}"`, {
|
|
45
|
+
path: [((_a = fieldNode.alias) === null || _a === void 0 ? void 0 : _a.value) || fieldDef.name],
|
|
46
|
+
}));
|
|
47
|
+
deleteNode(parent, [...path]);
|
|
48
|
+
remainingFields--;
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return options.cache.set(cacheKey, remainingTokens - 1, {
|
|
52
|
+
ttl: config.ttl / 1000,
|
|
53
|
+
});
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
remainingFields++;
|
|
58
|
+
return false;
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
})));
|
|
62
|
+
await Promise.all(jobs);
|
|
63
|
+
if (errors.length > 0) {
|
|
64
|
+
// If there is a field left in the final selection set
|
|
65
|
+
if (remainingFields > 0) {
|
|
66
|
+
// Add the errors to the final result
|
|
67
|
+
return {
|
|
68
|
+
onExecuteDone(onExecuteDoneArgs) {
|
|
69
|
+
onExecuteDoneArgs.setResult({
|
|
70
|
+
...onExecuteDoneArgs.result,
|
|
71
|
+
errors,
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
// If there is no need to continue the execution, stop
|
|
77
|
+
onExecuteArgs.setResultAndStopExecution({
|
|
78
|
+
data: null,
|
|
79
|
+
errors,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export default useMeshRateLimit;
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphql-mesh/plugin-rate-limit",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"peerDependencies": {
|
|
6
|
+
"graphql": "*"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@envelop/core": "^2.3.2",
|
|
10
|
+
"@graphql-mesh/cross-helpers": "0.1.6",
|
|
11
|
+
"@graphql-mesh/string-interpolation": "0.3.0",
|
|
12
|
+
"@graphql-mesh/types": "0.76.0",
|
|
13
|
+
"@graphql-tools/utils": "8.8.0",
|
|
14
|
+
"minimatch": "5.1.0",
|
|
15
|
+
"tslib": "^2.4.0"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "Urigo/graphql-mesh",
|
|
20
|
+
"directory": "packages/plugins/response-cache"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"main": "index.js",
|
|
24
|
+
"module": "index.mjs",
|
|
25
|
+
"typings": "index.d.ts",
|
|
26
|
+
"typescript": {
|
|
27
|
+
"definition": "index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"require": "./index.js",
|
|
32
|
+
"import": "./index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"./*": {
|
|
35
|
+
"require": "./*.js",
|
|
36
|
+
"import": "./*.mjs"
|
|
37
|
+
},
|
|
38
|
+
"./package.json": "./package.json"
|
|
39
|
+
}
|
|
40
|
+
}
|