Skip to content

Commit

Permalink
feat: support timeout for test command
Browse files Browse the repository at this point in the history
Release-As: 0.1.2
  • Loading branch information
YieldRay committed Mar 11, 2024
1 parent b94224f commit d14c87d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <name> Use registry
nrml test [<timeout>] 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)
```
29 changes: 18 additions & 11 deletions cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const { local } = values
* @type {string}
*/
let name
/**
* @type {any}
*/
let timeout

switch (command) {
case 'h':
Expand All @@ -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()
Expand All @@ -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('<name>')} Use registry
nrml test ${c.gray(
'[<timeout>]'
)} 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)
Expand Down Expand Up @@ -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() {
Expand Down
12 changes: 8 additions & 4 deletions registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 3 additions & 3 deletions tty.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`)
Expand Down

0 comments on commit d14c87d

Please sign in to comment.