From d14c87d70a5990dc60c855f5c42883d1d9e8da3e Mon Sep 17 00:00:00 2001 From: yieldray Date: Mon, 11 Mar 2024 21:10:51 +0800 Subject: [PATCH] feat: support timeout for test command Release-As: 0.1.2 --- README.md | 16 +++++++++++----- cli.mjs | 29 ++++++++++++++++++----------- registry.mjs | 12 ++++++++---- tty.mjs | 6 +++--- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index ac4b00a..8d82f17 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,13 @@ Like [dnrm](https://github.com/markthree/dnrm), but in pure Node.js # install the `nrml` command globally npm install -g nrm-lite +# test if it's installed nrml --help + +# examples +nrml ls +nrml use taobao +nrml test ``` ## Usage @@ -26,11 +32,11 @@ nrml --help nrm-lite Usage: - nrml ls List registry - nrml use [name] Use registry - nrml test Test registry speed - nrml rc Open .npmrc file - nrml help Show this help + nrml ls List registry + nrml use Use registry + nrml test [] Test registry speed, optional timeout in second (default: 2) + nrml rc Open .npmrc file + nrml help Show this help Global Options: --local Use local .npmrc file, rather than the global one (default: false) ``` diff --git a/cli.mjs b/cli.mjs index ca9c78c..d437c20 100644 --- a/cli.mjs +++ b/cli.mjs @@ -43,6 +43,10 @@ const { local } = values * @type {string} */ let name +/** + * @type {any} + */ +let timeout switch (command) { case 'h': @@ -53,8 +57,8 @@ switch (command) { ls() break case 'test': - name = positionals[1] - test(name) + timeout = positionals[1] || '2' + test(Number.parseFloat(timeout) * 1000) break case 'rc': rc() @@ -77,11 +81,13 @@ function help() { console.error(`${c.green(pkg.name)} v${pkg.version} ${c.bold('Usage:')} - nrml ls List registry - nrml use ${c.gray('[name]')} Use registry - nrml test Test registry speed - nrml rc Open .npmrc file - nrml help Show this help + nrml ls List registry + nrml use ${c.gray('')} Use registry + nrml test ${c.gray( + '[]' + )} Test registry speed, optional timeout in second (default: 2) + nrml rc Open .npmrc file + nrml help Show this help ${c.bold('Global Options:')} --local Use local .npmrc file, rather than the global one (default: false)`) process.exit(1) @@ -113,19 +119,20 @@ async function use(name) { } /** - * @param {string} name + * @param {number} timeoutLimit */ -async function test(name) { +async function test(timeoutLimit) { const info = await Promise.all( Object.entries(REGISTRIES).map(async ([name, url]) => ({ name, url, - timeSpent: await speedTest(url), + timeSpent: await speedTest(url, timeoutLimit), })) ) const currentRegistry = await getRegistry(local) - printRegistries(currentRegistry, info, 2000) + printRegistries(currentRegistry, info, timeoutLimit) + process.exit(0) } async function rc() { diff --git a/registry.mjs b/registry.mjs index beaaf11..838bb27 100644 --- a/registry.mjs +++ b/registry.mjs @@ -50,17 +50,21 @@ export async function findRegistryFromStream(stream) { /** * @param {string} url + * @param {number} timeoutLimit - in milliseconds */ -export async function speedTest(url, milliseconds = 2000) { +export async function speedTest(url, timeoutLimit) { try { const beginTime = Date.now() await fetch(url, { method: 'HEAD', - signal: AbortSignal.timeout(milliseconds), + signal: AbortSignal.timeout(timeoutLimit), }) const timeSpent = Date.now() - beginTime return timeSpent - } catch { - return null + } catch (e) { + if (e instanceof DOMException) { + return Infinity + } + return null // Network Error } } diff --git a/tty.mjs b/tty.mjs index 1d0dcc9..64b0a88 100644 --- a/tty.mjs +++ b/tty.mjs @@ -67,9 +67,9 @@ export function printRegistries( if (url === currentRegistryUrl) row = c.blue(row) if (timeoutLimit) { - timeSpent ??= Infinity - - if (timeSpent >= timeoutLimit) { + if (!timeSpent) { + row += c.red(` (Error)`) + } else if (timeSpent >= timeoutLimit) { row += c.red(` (>${(timeoutLimit / 1000).toFixed(1)}s)`) } else if (timeSpent >= timeoutLimit / 2) { row += c.yellow(` (${(timeSpent / 1000).toFixed(2)}s)`)