Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

feat(version): implement timeout for NPM version check and add tests

rajashidattapy79b3a1faf35b
diff --git a/ts/packages/core/src/utils/version.ts b/ts/packages/core/src/utils/version.ts
index 4d76726f5..e99fbfe62 100644
--- a/ts/packages/core/src/utils/version.ts
+++ b/ts/packages/core/src/utils/version.ts
@@ -19,9 +19,15 @@ export function isNewerVersion(version1: string, version2: string): boolean {
return false;
}
+/** Upper bound on the npm registry version check. Kept short: it is best-effort and unawaited. */
+const VERSION_CHECK_TIMEOUT_MS = 2000;
+
/**
* Checks for the latest version of the Composio SDK from NPM.
* If a newer version is available, it logs a warning to the console.
+ *
+ * Best-effort: never throws, and never keeps the process alive for more than
+ * {@link VERSION_CHECK_TIMEOUT_MS}.
*/
export async function checkForLatestVersionFromNPM(currentVersion: string) {
try {
@@ -42,8 +48,15 @@ export async function checkForLatestVersionFromNPM(currentVersion: string) {
return;
}
- // @TODO: Check if fetch is available, if not use node-fetch
- const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`);
+ // Bounded and abortable: this check is a non-essential background nicety that
+ // nobody awaits, so it must never extend process lifetime. An in-flight fetch
+ // keeps its socket referenced on the event loop, so without a signal a slow or
+ // black-holed registry (corporate proxy, egress firewall that drops rather than
+ // rejects, npm incident) keeps short-lived scripts alive until undici's own
+ // timeouts fire. The catch below already swallows the resulting TimeoutError.
+ const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`, {
+ signal: AbortSignal.timeout(VERSION_CHECK_TIMEOUT_MS),
+ });
const data = await response.json();
const latestVersion = data.version;
diff --git a/ts/packages/core/test/utils/version.test.ts b/ts/packages/core/test/utils/version.test.ts
new file mode 100644
index 000000000..2d9796beb
--- /dev/null
+++ b/ts/packages/core/test/utils/version.test.ts
@@ -0,0 +1,59 @@
+import { describe, it, expect, vi, afterEach } from 'vitest';
+import { checkForLatestVersionFromNPM } from '../../src/utils/version';
+
+describe('checkForLatestVersionFromNPM', () => {
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it('bounds the registry request with an AbortSignal so it cannot outlive the process', async () => {
+ const fetchMock = vi.fn(async () => new Response(JSON.stringify({ version: '0.0.1' })));
+ vi.stubGlobal('fetch', fetchMock);
+
+ await checkForLatestVersionFromNPM('1.0.0');
+
+ expect(fetchMock).toHaveBeenCalledOnce();
+ const init = fetchMock.mock.calls[0]![1] as RequestInit | undefined;
+ expect(init?.signal).toBeInstanceOf(AbortSignal);
+ });
+
+ it('gives up instead of hanging when the registry never responds', async () => {
+ // A registry that accepts the request and never answers. Without a timeout this
+ // promise never settles, which is exactly what keeps a real process alive.
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(
+ (_url: string, init?: RequestInit) =>
+ new Promise<Response>((_resolve, reject) => {
+ init?.signal?.addEventListener('abort', () => reject(init.signal!.reason));
+ })
+ )
+ );
+
+ // Resolves (rather than hanging) only because the signal aborts it; the
+ // function swallows the resulting TimeoutError.
+ await expect(checkForLatestVersionFromNPM('1.0.0')).resolves.toBeUndefined();
+ });
+
+ it('never rejects when the registry errors', async () => {
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () => {
+ throw new Error('ENOTFOUND registry.npmjs.org');
+ })
+ );
+
+ await expect(checkForLatestVersionFromNPM('1.0.0')).resolves.toBeUndefined();
+ });
+
+ it('skips the request entirely for non-semver and prerelease versions', async () => {
+ const fetchMock = vi.fn(async () => new Response(JSON.stringify({ version: '9.9.9' })));
+ vi.stubGlobal('fetch', fetchMock);
+
+ await checkForLatestVersionFromNPM('not-a-version');
+ await checkForLatestVersionFromNPM('1.0.0-alpha.1');
+ await checkForLatestVersionFromNPM('1.0.0-beta.3');
+
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+});