From 136e992bb952bdb97c335b9b959405b0ee7e0ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Wang=28=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7=29?= <308487730@qq.com> Date: Thu, 18 Jul 2024 03:59:36 +0800 Subject: [PATCH] feat: verdaccio-audit support timeout option (#4718) * feat: verdaccio-audit support timeout option * Create many-bees-tickle.md * Update audit.ts * update docs * Update README.md --------- Co-authored-by: Juan Picado --- .changeset/many-bees-tickle.md | 5 +++++ packages/config/src/conf/default.yaml | 1 + packages/config/src/conf/docker.yaml | 1 + packages/plugins/audit/README.md | 1 + packages/plugins/audit/src/audit.ts | 14 +++++++++++++- packages/plugins/audit/src/types.ts | 1 + website/docs/config.md | 1 + website/versioned_docs/version-5.x/config.md | 1 + 8 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 .changeset/many-bees-tickle.md diff --git a/.changeset/many-bees-tickle.md b/.changeset/many-bees-tickle.md new file mode 100644 index 000000000..847cff649 --- /dev/null +++ b/.changeset/many-bees-tickle.md @@ -0,0 +1,5 @@ +--- +'verdaccio-audit': minor +--- + +feat: verdaccio-audit support timeout option diff --git a/packages/config/src/conf/default.yaml b/packages/config/src/conf/default.yaml index 474237f76..e90ee95e3 100644 --- a/packages/config/src/conf/default.yaml +++ b/packages/config/src/conf/default.yaml @@ -187,6 +187,7 @@ server: middlewares: audit: enabled: true + # timeout: 10000 # https://verdaccio.org/docs/logger # log settings diff --git a/packages/config/src/conf/docker.yaml b/packages/config/src/conf/docker.yaml index 6973f9bc4..0aa99f61b 100644 --- a/packages/config/src/conf/docker.yaml +++ b/packages/config/src/conf/docker.yaml @@ -193,6 +193,7 @@ server: middlewares: audit: enabled: true + # timeout: 10000 # https://verdaccio.org/docs/logger # log settings diff --git a/packages/plugins/audit/README.md b/packages/plugins/audit/README.md index 79cc78277..ea079cfb3 100644 --- a/packages/plugins/audit/README.md +++ b/packages/plugins/audit/README.md @@ -26,6 +26,7 @@ middlewares: audit: enabled: true strict_ssl: true # optional, defaults to true + timeout: 1000 ``` ### Strict SSL diff --git a/packages/plugins/audit/src/audit.ts b/packages/plugins/audit/src/audit.ts index 4b9e2bfbc..94b272f19 100644 --- a/packages/plugins/audit/src/audit.ts +++ b/packages/plugins/audit/src/audit.ts @@ -19,11 +19,13 @@ export default class ProxyAudit public enabled: boolean; public logger: Logger; public strict_ssl: boolean; + public timeout: number; public constructor(config: ConfigAudit, options: pluginUtils.PluginOptions) { super(config, options); this.enabled = config.enabled || false; this.strict_ssl = config.strict_ssl !== undefined ? config.strict_ssl : true; + this.timeout = config.timeout ?? 1000 * 60 * 1; this.logger = options.logger; } @@ -57,7 +59,17 @@ export default class ProxyAudit const auditEndpoint = `${REGISTRY_DOMAIN}${req.baseUrl}${req.route.path}`; this.logger.debug('fetching audit from ' + auditEndpoint); - const response = await fetch(auditEndpoint, requestOptions); + const controller = new AbortController(); + + setTimeout( + () => controller.abort(`Fetch ${auditEndpoint} timeout ${this.timeout}ms`), + this.timeout + ); + + const response = await fetch(auditEndpoint, { + ...requestOptions, + signal: controller.signal, + }); if (response.ok) { res.status(response.status).send(await response.json()); diff --git a/packages/plugins/audit/src/types.ts b/packages/plugins/audit/src/types.ts index b7d0fe4a5..102849ff3 100644 --- a/packages/plugins/audit/src/types.ts +++ b/packages/plugins/audit/src/types.ts @@ -2,4 +2,5 @@ export interface ConfigAudit { enabled: boolean; max_body?: string; strict_ssl?: boolean; + timeout?: number; } diff --git a/website/docs/config.md b/website/docs/config.md index be4543b30..2596b74a4 100644 --- a/website/docs/config.md +++ b/website/docs/config.md @@ -364,6 +364,7 @@ a built-in middleware plugin to handle this command. middlewares: audit: enabled: true + # timeout: 10000 ``` ### Experiments {#experiments} diff --git a/website/versioned_docs/version-5.x/config.md b/website/versioned_docs/version-5.x/config.md index c7ec22022..8a5a0c0be 100644 --- a/website/versioned_docs/version-5.x/config.md +++ b/website/versioned_docs/version-5.x/config.md @@ -387,6 +387,7 @@ a built-in middleware plugin to handle this command. middlewares: audit: enabled: true + # timeout: 10000 ``` ### Experiments {#experiments}