From ed008438ec3295afb01001ac128e0bd1928489c8 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: Wed, 17 Jul 2024 18:54:12 +0800 Subject: [PATCH] feat: verdaccio-audit support timeout option --- packages/plugins/audit/src/audit.ts | 11 ++++++++++- packages/plugins/audit/src/types.ts | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/plugins/audit/src/audit.ts b/packages/plugins/audit/src/audit.ts index 4b9e2bfbc..500db853d 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,14 @@ 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; }