统一前后端代码格式化配置

This commit is contained in:
yml2213
2026-08-16 17:27:12 +08:00
parent 8705f6a6a1
commit 5c7c3e14e3
278 changed files with 6386 additions and 5500 deletions
+64 -67
View File
@@ -1,116 +1,113 @@
import test from "node:test";
import assert from "node:assert/strict";
import type { NextFunction, Request, Response } from "express";
import test from 'node:test'
import assert from 'node:assert/strict'
import type { NextFunction, Request, Response } from 'express'
import {
createRateLimitMiddleware,
resetRateLimitBucketsForTest,
} from "./rate-limit.js";
import { createRateLimitMiddleware, resetRateLimitBucketsForTest } from './rate-limit.js'
test("createRateLimitMiddleware allows requests within the window", () => {
resetRateLimitBucketsForTest();
test('createRateLimitMiddleware allows requests within the window', () => {
resetRateLimitBucketsForTest()
const limiter = createRateLimitMiddleware({
scope: "test:allow",
scope: 'test:allow',
windowMs: 60_000,
max: 2,
});
const req = createMockRequest();
const res = createMockResponse();
let nextCount = 0;
})
const req = createMockRequest()
const res = createMockResponse()
let nextCount = 0
const next: NextFunction = () => {
nextCount += 1;
};
nextCount += 1
}
limiter(req, res, next);
limiter(req, res, next);
limiter(req, res, next)
limiter(req, res, next)
assert.equal(nextCount, 2);
assert.equal(res.statusCode, 200);
});
assert.equal(nextCount, 2)
assert.equal(res.statusCode, 200)
})
test("createRateLimitMiddleware blocks requests over the limit", () => {
resetRateLimitBucketsForTest();
test('createRateLimitMiddleware blocks requests over the limit', () => {
resetRateLimitBucketsForTest()
const limiter = createRateLimitMiddleware({
scope: "test:block",
scope: 'test:block',
windowMs: 60_000,
max: 1,
});
const req = createMockRequest();
const res = createMockResponse();
let nextCount = 0;
})
const req = createMockRequest()
const res = createMockResponse()
let nextCount = 0
const next: NextFunction = () => {
nextCount += 1;
};
nextCount += 1
}
limiter(req, res, next);
limiter(req, res, next);
limiter(req, res, next)
limiter(req, res, next)
assert.equal(nextCount, 1);
assert.equal(res.statusCode, 429);
assert.equal(res.headers["Retry-After"], "60");
assert.equal(res.body.errorCode, "rate_limited");
});
assert.equal(nextCount, 1)
assert.equal(res.statusCode, 429)
assert.equal(res.headers['Retry-After'], '60')
assert.equal(res.body.errorCode, 'rate_limited')
})
test("createRateLimitMiddleware supports custom limit responses", () => {
resetRateLimitBucketsForTest();
test('createRateLimitMiddleware supports custom limit responses', () => {
resetRateLimitBucketsForTest()
const limiter = createRateLimitMiddleware({
scope: "test:custom",
scope: 'test:custom',
windowMs: 60_000,
max: 1,
onLimit: (_req, res) => {
res.status(200).json({ code: 429, message: "limited" });
res.status(200).json({ code: 429, message: 'limited' })
},
});
const req = createMockRequest();
const res = createMockResponse();
})
const req = createMockRequest()
const res = createMockResponse()
limiter(req, res, () => {});
limiter(req, res, () => {});
limiter(req, res, () => {})
limiter(req, res, () => {})
assert.equal(res.statusCode, 200);
assert.deepEqual(res.body, { code: 429, message: "limited" });
});
assert.equal(res.statusCode, 200)
assert.deepEqual(res.body, { code: 429, message: 'limited' })
})
function createMockRequest(): Request {
return {
ip: "127.0.0.1",
originalUrl: "/test",
ip: '127.0.0.1',
originalUrl: '/test',
headers: {},
socket: {
remoteAddress: "127.0.0.1",
remoteAddress: '127.0.0.1',
},
} as Request;
} as Request
}
function createMockResponse(): Response & {
statusCode: number;
headers: Record<string, string>;
body: any;
statusCode: number
headers: Record<string, string>
body: any
} {
const res = {
statusCode: 200,
headers: {} as Record<string, string>,
body: null as any,
setHeader(name: string, value: string) {
this.headers[name] = value;
return this;
this.headers[name] = value
return this
},
status(statusCode: number) {
this.statusCode = statusCode;
return this;
this.statusCode = statusCode
return this
},
json(body: any) {
this.body = body;
return this;
this.body = body
return this
},
};
}
return res as Response & {
statusCode: number;
headers: Record<string, string>;
body: any;
};
statusCode: number
headers: Record<string, string>
body: any
}
}