mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-16 08:26:59 +03:00
migrate to typescript
This commit is contained in:
committed by
Dainius Dauksevicius
parent
c462323dd5
commit
08d27265fc
@@ -1,5 +0,0 @@
|
||||
module.exports = function corsMiddleware(req, res, next) {
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||
next();
|
||||
}
|
||||
7
src/middleware/cors.ts
Normal file
7
src/middleware/cors.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import {NextFunction, Request, Response} from 'express';
|
||||
|
||||
export function corsMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||
next();
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
const log = require('../utils/logger.js'); // log not logger to not interfere with function name
|
||||
|
||||
module.exports = function logger (req, res, next) {
|
||||
log.info("Request recieved: " + req.method + " " + req.url);
|
||||
next();
|
||||
}
|
||||
7
src/middleware/logger.ts
Normal file
7
src/middleware/logger.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import {Logger} from '../utils/logger';
|
||||
import {NextFunction, Request, Response} from 'express';
|
||||
|
||||
export function loggerMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||
Logger.info(`Request received: ${req.method} ${req.url}`);
|
||||
next();
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
const getIP = require('../utils/getIP.js');
|
||||
const getHash = require('../utils/getHash.js');
|
||||
const rateLimit = require('express-rate-limit');
|
||||
|
||||
module.exports = (limitConfig) => rateLimit({
|
||||
windowMs: limitConfig.windowMs,
|
||||
max: limitConfig.max,
|
||||
message: limitConfig.message,
|
||||
statusCode: limitConfig.statusCode,
|
||||
headers: false,
|
||||
keyGenerator: (req /*, res*/) => {
|
||||
return getHash(getIP(req), 1);
|
||||
},
|
||||
skip: (/*req, res*/) => {
|
||||
// skip rate limit if running in test mode
|
||||
return process.env.npm_lifecycle_script === 'node test.js';
|
||||
}
|
||||
});
|
||||
21
src/middleware/requestRateLimit.ts
Normal file
21
src/middleware/requestRateLimit.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import {getIP} from '../utils/getIP';
|
||||
import {getHash} from '../utils/getHash';
|
||||
import rateLimit from 'express-rate-limit';
|
||||
import {RateLimitConfig} from '../types/config.model';
|
||||
|
||||
export function rateLimitMiddleware(limitConfig: RateLimitConfig): rateLimit.RateLimit {
|
||||
return rateLimit({
|
||||
windowMs: limitConfig.windowMs,
|
||||
max: limitConfig.max,
|
||||
message: limitConfig.message,
|
||||
statusCode: limitConfig.statusCode,
|
||||
headers: false,
|
||||
keyGenerator: (req) => {
|
||||
return getHash(getIP(req), 1);
|
||||
},
|
||||
skip: (/*req, res*/) => {
|
||||
// skip rate limit if running in test mode
|
||||
return process.env.npm_lifecycle_script === 'ts-node test.ts';
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
const config = require('../config.js');
|
||||
const getIP = require('../utils/getIP.js');
|
||||
const getHash = require('../utils/getHash.js');
|
||||
const logger = require('../utils/logger.js');
|
||||
|
||||
module.exports = function userCounter(req, res, next) {
|
||||
fetch(config.userCounterURL + "/api/v1/addIP?hashedIP=" + getHash(getIP(req), 1), { method: "POST" })
|
||||
.catch(() => logger.debug("Failing to connect to user counter at: " + config.userCounterURL))
|
||||
|
||||
next();
|
||||
}
|
||||
13
src/middleware/userCounter.ts
Normal file
13
src/middleware/userCounter.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import fetch from 'node-fetch';
|
||||
import {Logger} from '../utils/logger';
|
||||
import {config} from '../config';
|
||||
import {getIP} from '../utils/getIP';
|
||||
import {getHash} from '../utils/getHash';
|
||||
import {NextFunction, Request, Response} from 'express';
|
||||
|
||||
export function userCounter(req: Request, res: Response, next: NextFunction) {
|
||||
fetch(config.userCounterURL + "/api/v1/addIP?hashedIP=" + getHash(getIP(req), 1), {method: "POST"})
|
||||
.catch(() => Logger.debug("Failing to connect to user counter at: " + config.userCounterURL));
|
||||
|
||||
next();
|
||||
}
|
||||
Reference in New Issue
Block a user