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
rajashidattapy committed
Aug 3, 2026 at 13:47 UTC
79b3a1faf35b5b9d2c76b925bf04c894c6d66584
2 files changed
+74
-2
ts/packages/core/src/utils/version.ts
+15
-2
@@ -19,9 +19,15 @@ export function isNewerVersion(version1: string, version2: string): boolean {
19
return false;
20
}
21
22
+/** Upper bound on the npm registry version check. Kept short: it is best-effort and unawaited. */
23
+const VERSION_CHECK_TIMEOUT_MS = 2000;
24
+
25
/**
26
* Checks for the latest version of the Composio SDK from NPM.
27
* If a newer version is available, it logs a warning to the console.
28
+ *
29
+ * Best-effort: never throws, and never keeps the process alive for more than
30
+ * {@link VERSION_CHECK_TIMEOUT_MS}.
31
*/
32
export async function checkForLatestVersionFromNPM(currentVersion: string) {
33
try {
@@ -42,8 +48,15 @@ export async function checkForLatestVersionFromNPM(currentVersion: string) {
48
return;
49
}
50
45
- // @TODO: Check if fetch is available, if not use node-fetch
46
- const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`);
51
+ // Bounded and abortable: this check is a non-essential background nicety that
52
+ // nobody awaits, so it must never extend process lifetime. An in-flight fetch
53
+ // keeps its socket referenced on the event loop, so without a signal a slow or
54
+ // black-holed registry (corporate proxy, egress firewall that drops rather than
55
+ // rejects, npm incident) keeps short-lived scripts alive until undici's own
56
+ // timeouts fire. The catch below already swallows the resulting TimeoutError.
57
+ const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`, {
58
+ signal: AbortSignal.timeout(VERSION_CHECK_TIMEOUT_MS),
59
+ });
60
const data = await response.json();
61
const latestVersion = data.version;
62
ts/packages/core/test/utils/version.test.ts
new
+59
@@ -0,0 +1,59 @@
1
+import { describe, it, expect, vi, afterEach } from 'vitest';
2
+import { checkForLatestVersionFromNPM } from '../../src/utils/version';
3
+
4
+describe('checkForLatestVersionFromNPM', () => {
5
+ afterEach(() => {
6
+ vi.unstubAllGlobals();
7
+ });
8
+
9
+ it('bounds the registry request with an AbortSignal so it cannot outlive the process', async () => {
10
+ const fetchMock = vi.fn(async () => new Response(JSON.stringify({ version: '0.0.1' })));
11
+ vi.stubGlobal('fetch', fetchMock);
12
+
13
+ await checkForLatestVersionFromNPM('1.0.0');
14
+
15
+ expect(fetchMock).toHaveBeenCalledOnce();
16
+ const init = fetchMock.mock.calls[0]![1] as RequestInit | undefined;
17
+ expect(init?.signal).toBeInstanceOf(AbortSignal);
18
+ });
19
+
20
+ it('gives up instead of hanging when the registry never responds', async () => {
21
+ // A registry that accepts the request and never answers. Without a timeout this
22
+ // promise never settles, which is exactly what keeps a real process alive.
23
+ vi.stubGlobal(
24
+ 'fetch',
25
+ vi.fn(
26
+ (_url: string, init?: RequestInit) =>
27
+ new Promise<Response>((_resolve, reject) => {
28
+ init?.signal?.addEventListener('abort', () => reject(init.signal!.reason));
29
+ })
30
+ )
31
+ );
32
+
33
+ // Resolves (rather than hanging) only because the signal aborts it; the
34
+ // function swallows the resulting TimeoutError.
35
+ await expect(checkForLatestVersionFromNPM('1.0.0')).resolves.toBeUndefined();
36
+ });
37
+
38
+ it('never rejects when the registry errors', async () => {
39
+ vi.stubGlobal(
40
+ 'fetch',
41
+ vi.fn(async () => {
42
+ throw new Error('ENOTFOUND registry.npmjs.org');
43
+ })
44
+ );
45
+
46
+ await expect(checkForLatestVersionFromNPM('1.0.0')).resolves.toBeUndefined();
47
+ });
48
+
49
+ it('skips the request entirely for non-semver and prerelease versions', async () => {
50
+ const fetchMock = vi.fn(async () => new Response(JSON.stringify({ version: '9.9.9' })));
51
+ vi.stubGlobal('fetch', fetchMock);
52
+
53
+ await checkForLatestVersionFromNPM('not-a-version');
54
+ await checkForLatestVersionFromNPM('1.0.0-alpha.1');
55
+ await checkForLatestVersionFromNPM('1.0.0-beta.3');
56
+
57
+ expect(fetchMock).not.toHaveBeenCalled();
58
+ });
59
+});