Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
07d76bc
intitial jest and typescript config
shalarewicz May 17, 2022
0656e58
RateLimiter spec closes #9. Test for token bucket algorithm closes #4.
shalarewicz May 17, 2022
778f0a6
updated test script to pass without any tests
shalarewicz May 17, 2022
37012d2
resolving some errors with eslint and ts
evanmcneely May 19, 2022
2aaf2f4
Created types and function definitions for the get type weights function
evanmcneely May 19, 2022
7973e37
renamed the file to buildTypeWeights
evanmcneely May 19, 2022
7f5f655
started to layout the test structure for buildTypeWeight function
evanmcneely May 19, 2022
094847e
added a default type weights configuration to the function parameters
evanmcneely May 19, 2022
06a119c
moved types to @types folder
evanmcneely May 20, 2022
f3c1118
wrote tests for buildTypeWeights functionality and configurability
evanmcneely May 20, 2022
96ab047
added some tests and reworked expected output
evanmcneely May 20, 2022
43e0dd2
finish writing and proof reading tests
evanmcneely May 20, 2022
7b05588
finished writing an overview of implementation for buildTypeWeightsFr…
evanmcneely May 20, 2022
6f9d873
fixed a ts error in tokenBucket.ts
evanmcneely May 20, 2022
1164121
skipped all tests for now
evanmcneely May 20, 2022
9d08980
added types to varibales in token bucket tests
evanmcneely May 20, 2022
d781198
add todo items to describe how additional functionality should be tested
evanmcneely May 21, 2022
22bf283
Merge branch 'dev' into em/typeWeightsTest
evanmcneely May 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }], "@babel/preset-typescript"],
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 15 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@
"commonjs": true,
"es2021": true
},
"extends": ["eslint:recommended", "airbnb-base"],
"extends": [
"eslint:recommended",
"airbnb-base",
"airbnb-typescript/base",
"plugin:@typescript-eslint/recommended",
"plugin:import/typescript",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest"
"ecmaVersion": "latest",
"project": "./tsconfig.eslint.json"
},
"plugins": ["@typescript-eslint"],
"plugins": ["import"],
"rules": {
"indent": [
"warn",
2,
4,
{
"SwitchCase": 1
"SwitchCase": 2
}
]
}
},
"ignorePatterns": ["jest.config.js"]
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ branches:
- dev
script:
- 'npm run lint'
# - "npm run test"
- 'npm run test'
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [],
"settings": {},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.formatOnSave": true,
}
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
roots: ['./test'],
preset: 'ts-jest',
testEnvironment: 'node',
moduleFileExtensions: ['js', 'ts'],
};
11,773 changes: 9,757 additions & 2,016 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A GraphQL rate limiting library using query complexity analysis.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest --passWithNoTests",
"lint": "eslint src",
"lint:fix": "eslint --fix src",
"prettier": "prettier --write .",
Expand All @@ -22,17 +22,30 @@
},
"homepage": "https://github.com/oslabs-beta/graph-beaver#readme",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.23.0",
"@typescript-eslint/parser": "^5.23.0",
"@babel/core": "^7.17.12",
"@babel/preset-env": "^7.17.12",
"@babel/preset-typescript": "^7.17.12",
"@types/jest": "^27.5.1",
"@typescript-eslint/eslint-plugin": "^5.24.0",
"@typescript-eslint/parser": "^5.24.0",
"babel-jest": "^28.1.0",
"eslint": "^8.15.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"husky": "^8.0.1",
"jest": "^28.1.0",
"lint-staged": "^12.4.1",
"prettier": "2.6.2"
"prettier": "2.6.2",
"ts-jest": "^28.0.2",
"typescript": "^4.6.4"
},
"lint-staged": {
"*.{js, ts}": "eslint --cache --fix",
"*.{js,ts,css,md}": "prettier --write --ignore-unknown"
},
"dependencies": {
"graphql": "^16.5.0"
}
}
20 changes: 20 additions & 0 deletions src/@types/buildTypeWeights.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
interface Fields {
[index: string]: number;
}

interface Type {
weight: number;
fields: Fields;
}

interface TypeWeightObject {
[index: string]: Type;
}

interface TypeWeightConfig {
mutation?: number;
query?: number;
object?: number;
scalar?: number;
connection?: number;
}
14 changes: 14 additions & 0 deletions src/@types/rateLimit.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface RateLimiter {
/**
* Checks if a request is allowed under the given conditions and withdraws the specified number of tokens
* @param uuid Unique identifier for the user associated with the request
* @param tokens Number of tokens being used in this request. Optional
* @returns true if the request is allowed
*/
processRequest: (uuid: string, tokens?: number) => boolean;
/**
* Connects the RateLimiter instance to a db to cache current token usage for connected users
* @param uri database connection string
*/
connect: (uri: string) => void;
}
26 changes: 26 additions & 0 deletions src/analysis/buildTypeWeights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { GraphQLSchema } from 'graphql/type/schema';

/**
* The default typeWeightsConfig object is based off of Shopifys implementation of query
* cost analysis. Our function should input a users configuration of type weights or fall
* back on shopifys settings. We can change this later.
*
* This function should
* - itreate through the schema object and create the typeWeightObject as described in the tests
* - validate that the typeWeightsConfig parameter has no negative values (throw an error if it does)
*
* @param schema
* @param typeWeightsConfig
*/
function buildTypeWeightsFromSchema(
schema: GraphQLSchema,
typeWeightsConfig: TypeWeightConfig = {
mutation: 10,
object: 1,
scalar: 0,
connection: 2,
}
): TypeWeightObject {
throw Error(`getTypeWeightsFromSchema is not implemented.`);
}
export default buildTypeWeightsFromSchema;
3 changes: 0 additions & 3 deletions src/index.js

This file was deleted.

35 changes: 35 additions & 0 deletions src/rateLimiters/tokenBucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
*
*/
class TokenBucket implements RateLimiter {
capacity: number;

refillRate: number;

/**
* Create a new instance of a TokenBucket rate limiter that can be connected to any database store
* @param capacity max token bucket capacity
* @param refillRate rate at which the token bucket is refilled
*/
constructor(capacity: number, refillRate: number) {
this.capacity = capacity;
this.refillRate = refillRate;
}

processRequest(uuid: string, tokens?: number): boolean {
throw Error(`TokenBucket.processRequest not implemented, ${this}`);
}

connect(uri: string) {
throw Error(`TokenBucket.connect not implemented, ${this}`);
}

/**
* @returns current size of the token bucket.
*/
getSize(uuid: string): number {
throw Error(`TokenBucket.connect not implemented, ${this}`);
}
}

export default TokenBucket;
Loading