Skip to content

Commit

Permalink
feature userAccessPolicy: code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
light-source committed Jan 10, 2025
1 parent 557de24 commit e3b0188
Show file tree
Hide file tree
Showing 137 changed files with 364 additions and 308 deletions.
11 changes: 11 additions & 0 deletions packages/user-access-policy/aliases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import path from "node:path";

const aliases = {
"@api": path.resolve(__dirname, "./src/api"),
"@blacklist": path.resolve(__dirname, "./src/blacklist"),
"@captchaConfig": path.resolve(__dirname, "./src/captchaConfig"),
"@rules": path.resolve(__dirname, "./src/rules"),
"@tests": path.resolve(__dirname, "./src/tests"),
};

export { aliases };
2 changes: 1 addition & 1 deletion packages/user-access-policy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"scripts": {
"clean": "tsc --build --clean",
"build": "tsc --build --verbose",
"test": "vitest --run",
"test": "NODE_ENV=${NODE_ENV:-test}; npx vitest run --config ./vite.test.config.ts",
"storage:benchmark:mongoose": "npx tsx ./src/rule/storage/benchmark/mongooseRulesStorageBenchmark.ts",
"storage:benchmark:mongoose:populate": "npm run storage:benchmark:mongoose -- populate --db-url=localhost:27017 --entities-count-per-type=100000",
"storage:benchmark:mongoose:measure-find": "npm run storage:benchmark:mongoose -- measure-find --db-url=localhost:27017 --target-ip-v4=0.0.136.184 --target-ip-v6=0000:0000:0000:0000:0000:0000:0000:7530"
Expand Down
2 changes: 1 addition & 1 deletion packages/user-access-policy/src/api/apiEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { ZodType, z } from "zod";
import type { ApiResponse } from "./response/apiResponse.js";
import type {ApiResponse} from "@api/response/apiResponse.js";

interface ApiEndpoint<T extends ZodType | undefined> {
processRequest: T extends ZodType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import type { Logger } from "@prosopo/common";
import type { Request, Response } from "express";
import type { ZodType } from "zod";
import type { ApiEndpoint } from "../apiEndpoint.js";
import type {ApiEndpoint} from "@api/apiEndpoint.js";

class ExpressEndpointAdapter {
public constructor(private readonly logger: Logger) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Router } from "express";
import type { RulesStorage } from "../../rule/storage/rulesStorage.js";
import type { ApiRoutesProvider } from "../route/apiRoutesProvider.js";
import type { ExpressRoutesRegistrar } from "./expressRoutesRegistrar.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {ApiRoutesProvider} from "@api/route/apiRoutesProvider.js";
import type {ExpressRoutesRegistrar} from "@api/express/expressRoutesRegistrar.js";

class ExpressRouterFactory {
public createRouter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { Request, Response, Router } from "express";
import type { ApiRoute } from "../route/apiRoute.js";
import type { ExpressEndpointAdapter } from "./expressEndpointAdapter.js";
import type {ApiRoute} from "@api/route/apiRoute.js";
import type {ExpressEndpointAdapter} from "@api/express/expressEndpointAdapter.js";

class ExpressRoutesRegistrar {
public constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { ApiResponseStatus } from "./apiResponseStatus.js";
import type {ApiResponseStatus} from "@api/response/apiResponseStatus.js";

interface ApiResponse {
status: ApiResponseStatus;
Expand Down
2 changes: 1 addition & 1 deletion packages/user-access-policy/src/api/route/apiRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { ZodType } from "zod";
import type { ApiEndpoint } from "../apiEndpoint.js";
import type {ApiEndpoint} from "@api/apiEndpoint.js";

interface ApiRoute {
path: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { RulesStorage } from "../../rule/storage/rulesStorage.js";
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,6 +12,7 @@ import type { RulesStorage } from "../../rule/storage/rulesStorage.js";
// See the License for the specific language governing permissions and
// limitations under the License.
import type { ApiRoute } from "./apiRoute.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";

interface ApiRoutesProvider {
getRoutes(rulesStorage: RulesStorage): ApiRoute[];
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { RulesStorage } from "../rule/storage/rulesStorage.js";
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,8 +11,9 @@ import type { RulesStorage } from "../rule/storage/rulesStorage.js";
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { RequestInspector } from "./requestInspector.js";
import { RequestRulesInspector } from "./requestRulesInspector.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {RequestInspector} from "@blacklist/inspector/requestInspector.js";
import {RequestRulesInspector} from "@blacklist/inspector/requestRulesInspector.js";

const createRequestInspector = (
rulesStorage: RulesStorage,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { IPAddress } from "@prosopo/types";
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,8 +11,9 @@ import type { IPAddress } from "@prosopo/types";
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { RulesStorage } from "../rule/storage/rulesStorage.js";
import type { RequestInspector } from "./requestInspector.js";
import type { IPAddress } from "@prosopo/types";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {RequestInspector} from "@blacklist/inspector/requestInspector.js";

class RequestRulesInspector implements RequestInspector {
public constructor(private readonly rulesStorage: RulesStorage) {}
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type { zodConfig } from "./zodConfig.js";
import type {zodConfig} from "@captchaConfig/zodConfig.js";

type Config = z.infer<typeof zodConfig>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type { zodImageCaptchaConfig } from "./zodImageCaptchaConfig.js";
import type {zodImageCaptchaConfig} from "@captchaConfig/image/zodImageCaptchaConfig.js";

type ImageCaptchaConfig = z.infer<typeof zodImageCaptchaConfig>;

Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Schema } from "mongoose";
import type { ImageCaptchaConfig } from "./imageCaptchaConfig.js";
import type {ImageCaptchaConfig} from "@captchaConfig/image/imageCaptchaConfig.js";

const mongooseImageCaptchaConfig = new Schema<ImageCaptchaConfig>(
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import type {
IPAddress,
ProsopoCaptchaCountConfigSchemaOutput,
} from "@prosopo/types";
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,7 +11,11 @@ import type {
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { RulesStorage } from "../../../rule/storage/rulesStorage.js";
import type {
IPAddress,
ProsopoCaptchaCountConfigSchemaOutput,
} from "@prosopo/types";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";

interface CaptchaConfigResolver {
resolveConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { CaptchaConfigResolver } from "./captchaConfigResolver.js";
import { ImageCaptchaConfigResolver } from "./imageCaptchaConfigResolver.js";
import {ImageCaptchaConfigResolver} from "@captchaConfig/image/resolver/imageCaptchaConfigResolver.js";
import type {CaptchaConfigResolver} from "@captchaConfig/image/resolver/captchaConfigResolver.js";

const createImageCaptchaConfigResolver = (): CaptchaConfigResolver => {
return new ImageCaptchaConfigResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import type {
IPAddress,
ProsopoCaptchaCountConfigSchemaOutput,
} from "@prosopo/types";
import type { Rule } from "../../../rule/rule.js";
import type { RulesStorage } from "../../../rule/storage/rulesStorage.js";
import type { ImageCaptchaConfig } from "../imageCaptchaConfig.js";
import type { CaptchaConfigResolver } from "./captchaConfigResolver.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {CaptchaConfigResolver} from "@captchaConfig/image/resolver/captchaConfigResolver.js";
import type {Rule} from "@rules/rule/rule.js";
import type {ImageCaptchaConfig} from "@captchaConfig/image/imageCaptchaConfig.js";


class ImageCaptchaConfigResolver implements CaptchaConfigResolver {
public async resolveConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Schema } from "mongoose";
import type { Config } from "./config.js";
import { mongooseImageCaptchaConfig } from "./imageCaptcha/mongooseImageCaptchaConfig.js";
import type {Config} from "@captchaConfig/config.js";
import {mongooseImageCaptchaConfig} from "@captchaConfig/image/mongooseImageCaptchaConfig.js";

const mongooseConfig = new Schema<Config>(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { object } from "zod";
import { zodImageCaptchaConfig } from "./imageCaptcha/zodImageCaptchaConfig.js";
import {zodImageCaptchaConfig} from "@captchaConfig/image/zodImageCaptchaConfig.js";

const zodConfig = object({
imageCaptcha: zodImageCaptchaConfig.optional(),
Expand Down
Empty file.
18 changes: 9 additions & 9 deletions packages/user-access-policy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { createImageCaptchaConfigResolver } from "./config/imageCaptcha/resolver/createImageCaptchaConfigResolver.js";
import { createRequestInspector } from "./requestInspector/createRequestInspector.js";
import type { RequestInspector } from "./requestInspector/requestInspector.js";
import { createExpressRuleRouter } from "./rule/api/createExpressRuleRouter.js";
import { getExpressApiRuleRateLimits } from "./rule/api/getExpressApiRuleRateLimits.js";
import type { Rule } from "./rule/rule.js";
import { createMongooseRulesStorage } from "./rule/storage/createMongooseRulesStorage.js";
import { getMongooseRuleRecordSchema } from "./rule/storage/record/getMongooseRuleRecordSchema.js";
import type { RulesStorage } from "./rule/storage/rulesStorage.js";
import { createImageCaptchaConfigResolver } from "@captchaConfig/image/resolver/createImageCaptchaConfigResolver.js";
import { createRequestInspector } from "@blacklist/inspector/createRequestInspector.js";
import type { RequestInspector } from "@blacklist/inspector/requestInspector.js";
import { createExpressRuleRouter } from "@rules/api/createExpressRuleRouter.js";
import { getExpressApiRuleRateLimits } from "@rules/api/getExpressApiRuleRateLimits.js";
import type { Rule } from "@rules/rule/rule.js";
import { createMongooseRulesStorage } from "@rules/storage/mongoose/createMongooseRulesStorage.js";
import { getMongooseRuleRecordSchema } from "@rules/storage/mongoose/getMongooseRuleRecordSchema.js";
import type { RulesStorage } from "@rules/storage/rulesStorage.js";

export {
type Rule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
const apiRulePaths = {
INSERT_MANY: "/v1/prosopo/user-access-policy/rule/insert-many",
DELETE_MANY: "/v1/prosopo/user-access-policy/rule/delete-many",
INSERT_MANY: "/v1/prosopo/user-access-policy/rules/insert-many",
DELETE_MANY: "/v1/prosopo/user-access-policy/rules/delete-many",
};

export { apiRulePaths };
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ApiRoute } from "../../api/route/apiRoute.js";
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,11 +11,12 @@ import type { ApiRoute } from "../../api/route/apiRoute.js";
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { ApiRoutesProvider } from "../../api/route/apiRoutesProvider.js";
import type { RulesStorage } from "../storage/rulesStorage.js";
import { apiRulePaths } from "./apiRulePaths.js";
import { ApiDeleteManyRulesEndpoint } from "./deleteMany/apiDeleteManyRulesEndpoint.js";
import { InsertManyRulesEndpoint } from "./insertMany/insertManyRulesEndpoint.js";
import type {ApiRoute} from "@api/route/apiRoute.js";
import {ApiDeleteManyRulesEndpoint} from "@rules/api/deleteMany/apiDeleteManyRulesEndpoint.js";
import {InsertManyRulesEndpoint} from "@rules/api/insertMany/insertManyRulesEndpoint.js";
import {apiRulePaths} from "@rules/api/apiRulePaths.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {ApiRoutesProvider} from "@api/route/apiRoutesProvider.js";

class ApiRuleRoutesProvider implements ApiRoutesProvider {
public getRoutes(rulesStorage: RulesStorage): ApiRoute[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import type { Logger } from "@prosopo/common";
import type { Router } from "express";
import { ExpressEndpointAdapter } from "../../api/express/expressEndpointAdapter.js";
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,10 +11,13 @@ import { ExpressEndpointAdapter } from "../../api/express/expressEndpointAdapter
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { ExpressRouterFactory } from "../../api/express/expressRouterFactory.js";
import { ExpressRoutesRegistrar } from "../../api/express/expressRoutesRegistrar.js";
import type { RulesStorage } from "../storage/rulesStorage.js";
import { ApiRuleRoutesProvider } from "./apiRuleRoutesProvider.js";
import type { Logger } from "@prosopo/common";
import type { Router } from "express";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import {ExpressRouterFactory} from "@api/express/expressRouterFactory.js";
import {ApiRuleRoutesProvider} from "@rules/api/apiRuleRoutesProvider.js";
import {ExpressRoutesRegistrar} from "@api/express/expressRoutesRegistrar.js";
import {ExpressEndpointAdapter} from "@api/express/expressEndpointAdapter.js";

const createExpressRuleRouter = (
rulesStorage: RulesStorage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type { ApiEndpoint } from "../../../api/apiEndpoint.js";
import type { ApiResponse } from "../../../api/response/apiResponse.js";
import { ApiResponseStatus } from "../../../api/response/apiResponseStatus.js";
import type { RulesStorage } from "../../storage/rulesStorage.js";
import { zodDeleteManyRulesSchema } from "./zodDeleteManyRulesSchema.js";
import type {ApiEndpoint} from "@api/apiEndpoint.js";
import {zodDeleteManyRulesSchema} from "@rules/api/deleteMany/zodDeleteManyRulesSchema.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {ApiResponse} from "@api/response/apiResponse.js";
import {ApiResponseStatus} from "@api/response/apiResponseStatus.js";

class ApiDeleteManyRulesEndpoint
implements ApiEndpoint<typeof zodDeleteManyRulesSchema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { array, object, string } from "zod";
import { zodIp } from "../../../ip/zodIp.js";
import {zodIp} from "@rules/rule/ip/zodIp.js";

const zodDeleteManyRulesSchema = array(
object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { apiRulePaths } from "./apiRulePaths.js";
import {apiRulePaths} from "@rules/api/apiRulePaths.js";

const getExpressApiRuleRateLimits = () => {
const defaultWindowsMs = 60000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type { ApiEndpoint } from "../../../api/apiEndpoint.js";
import type { ApiResponse } from "../../../api/response/apiResponse.js";
import { ApiResponseStatus } from "../../../api/response/apiResponseStatus.js";
import type { RulesStorage } from "../../storage/rulesStorage.js";
import { zodInsertManyRulesSchema } from "./zodInsertManyRulesSchema.js";
import type {ApiEndpoint} from "@api/apiEndpoint.js";
import {zodInsertManyRulesSchema} from "@rules/api/insertMany/zodInsertManyRulesSchema.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {ApiResponse} from "@api/response/apiResponse.js";
import {ApiResponseStatus} from "@api/response/apiResponseStatus.js";

class InsertManyRulesEndpoint
implements ApiEndpoint<typeof zodInsertManyRulesSchema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { array, boolean, object, string } from "zod";
import { zodConfig } from "../../../config/zodConfig.js";
import { zodIp } from "../../../ip/zodIp.js";
import {zodIp} from "@rules/rule/ip/zodIp.js";
import {zodConfig} from "@captchaConfig/zodConfig.js";

const zodInsertManyRulesSchema = array(
object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type { zodIp } from "./zodIp.js";
import type {zodIp} from "@rules/rule/ip/zodIp.js";

type Ip = z.infer<typeof zodIp>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Schema } from "mongoose";
import type { Ip } from "./ip.js";
import { mongooseIpV4 } from "./v4/mongooseIpV4.js";
import { mongooseIpV6 } from "./v6/mongooseIpV6.js";
import type {Ip} from "@rules/rule/ip/ip.js";
import {mongooseIpV4} from "@rules/rule/ip/v4/single/mongooseIpV4.js";
import {mongooseIpV6} from "@rules/rule/ip/v6/single/mongooseIpV6.js";

const mongooseIp = new Schema<Ip>(
{
Expand Down
Loading

0 comments on commit e3b0188

Please sign in to comment.