@graphql-mesh/plugin-rate-limit 0.0.1-alpha-d8211f7b2.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 +61 -0
- package/index.mjs +59 -0
- package/package.json +38 -0
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const stringInterpolation = require('@graphql-mesh/string-interpolation');
|
|
4
|
+
const graphql = require('graphql');
|
|
5
|
+
const crossHelpers = require('@graphql-mesh/cross-helpers');
|
|
6
|
+
|
|
7
|
+
function useMeshRateLimit(options) {
|
|
8
|
+
const pathRateLimitDef = new Map();
|
|
9
|
+
const tokenMap = new Map();
|
|
10
|
+
const timeouts = new Set();
|
|
11
|
+
if (options.config) {
|
|
12
|
+
options.config.forEach(config => {
|
|
13
|
+
pathRateLimitDef.set(`${config.type}.${config.field}`, config);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
if (options.pubsub) {
|
|
17
|
+
const id = options.pubsub.subscribe('destroy', () => {
|
|
18
|
+
options.pubsub.unsubscribe(id);
|
|
19
|
+
timeouts.forEach(timeout => clearTimeout(timeout));
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
onValidate(onValidateParams) {
|
|
24
|
+
onValidateParams.addValidationRule(validationContext => ({
|
|
25
|
+
Field: () => {
|
|
26
|
+
const parentType = validationContext.getParentType();
|
|
27
|
+
const fieldDef = validationContext.getFieldDef();
|
|
28
|
+
const path = `${parentType.name}.${fieldDef.name}`;
|
|
29
|
+
const rateLimitConfig = pathRateLimitDef.get(path);
|
|
30
|
+
if (rateLimitConfig) {
|
|
31
|
+
const identifier = stringInterpolation.stringInterpolator.parse(rateLimitConfig.identifier, {
|
|
32
|
+
env: crossHelpers.process.env,
|
|
33
|
+
context: onValidateParams.context,
|
|
34
|
+
});
|
|
35
|
+
const mapKey = `${identifier}-${path}`;
|
|
36
|
+
let remainingTokens = tokenMap.get(mapKey);
|
|
37
|
+
if (remainingTokens == null) {
|
|
38
|
+
remainingTokens = rateLimitConfig.max;
|
|
39
|
+
const timeout = setTimeout(() => {
|
|
40
|
+
tokenMap.delete(mapKey);
|
|
41
|
+
timeouts.delete(timeout);
|
|
42
|
+
}, rateLimitConfig.ttl);
|
|
43
|
+
timeouts.add(timeout);
|
|
44
|
+
}
|
|
45
|
+
if (remainingTokens === 0) {
|
|
46
|
+
validationContext.reportError(new graphql.GraphQLError(`Rate limit of "${path}" exceeded for "${identifier}"`));
|
|
47
|
+
// Remove this field from the selection set
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
tokenMap.set(mapKey, remainingTokens - 1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
},
|
|
56
|
+
}));
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = useMeshRateLimit;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
|
|
2
|
+
import { GraphQLError } from 'graphql';
|
|
3
|
+
import { process } from '@graphql-mesh/cross-helpers';
|
|
4
|
+
|
|
5
|
+
function useMeshRateLimit(options) {
|
|
6
|
+
const pathRateLimitDef = new Map();
|
|
7
|
+
const tokenMap = new Map();
|
|
8
|
+
const timeouts = new Set();
|
|
9
|
+
if (options.config) {
|
|
10
|
+
options.config.forEach(config => {
|
|
11
|
+
pathRateLimitDef.set(`${config.type}.${config.field}`, config);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
if (options.pubsub) {
|
|
15
|
+
const id = options.pubsub.subscribe('destroy', () => {
|
|
16
|
+
options.pubsub.unsubscribe(id);
|
|
17
|
+
timeouts.forEach(timeout => clearTimeout(timeout));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
onValidate(onValidateParams) {
|
|
22
|
+
onValidateParams.addValidationRule(validationContext => ({
|
|
23
|
+
Field: () => {
|
|
24
|
+
const parentType = validationContext.getParentType();
|
|
25
|
+
const fieldDef = validationContext.getFieldDef();
|
|
26
|
+
const path = `${parentType.name}.${fieldDef.name}`;
|
|
27
|
+
const rateLimitConfig = pathRateLimitDef.get(path);
|
|
28
|
+
if (rateLimitConfig) {
|
|
29
|
+
const identifier = stringInterpolator.parse(rateLimitConfig.identifier, {
|
|
30
|
+
env: process.env,
|
|
31
|
+
context: onValidateParams.context,
|
|
32
|
+
});
|
|
33
|
+
const mapKey = `${identifier}-${path}`;
|
|
34
|
+
let remainingTokens = tokenMap.get(mapKey);
|
|
35
|
+
if (remainingTokens == null) {
|
|
36
|
+
remainingTokens = rateLimitConfig.max;
|
|
37
|
+
const timeout = setTimeout(() => {
|
|
38
|
+
tokenMap.delete(mapKey);
|
|
39
|
+
timeouts.delete(timeout);
|
|
40
|
+
}, rateLimitConfig.ttl);
|
|
41
|
+
timeouts.add(timeout);
|
|
42
|
+
}
|
|
43
|
+
if (remainingTokens === 0) {
|
|
44
|
+
validationContext.reportError(new GraphQLError(`Rate limit of "${path}" exceeded for "${identifier}"`));
|
|
45
|
+
// Remove this field from the selection set
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
tokenMap.set(mapKey, remainingTokens - 1);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
},
|
|
54
|
+
}));
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default useMeshRateLimit;
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphql-mesh/plugin-rate-limit",
|
|
3
|
+
"version": "0.0.1-alpha-d8211f7b2.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.77.0-alpha-d8211f7b2.0",
|
|
13
|
+
"tslib": "^2.4.0"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "Urigo/graphql-mesh",
|
|
18
|
+
"directory": "packages/plugins/response-cache"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"main": "index.js",
|
|
22
|
+
"module": "index.mjs",
|
|
23
|
+
"typings": "index.d.ts",
|
|
24
|
+
"typescript": {
|
|
25
|
+
"definition": "index.d.ts"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"require": "./index.js",
|
|
30
|
+
"import": "./index.mjs"
|
|
31
|
+
},
|
|
32
|
+
"./*": {
|
|
33
|
+
"require": "./*.js",
|
|
34
|
+
"import": "./*.mjs"
|
|
35
|
+
},
|
|
36
|
+
"./package.json": "./package.json"
|
|
37
|
+
}
|
|
38
|
+
}
|