npm-api icon indicating copy to clipboard operation
npm-api copied to clipboard

Service Unavailable

Open andresgarcia0313 opened this issue 1 year ago • 2 comments

Subject: Report on npm package search

Description: We have noticed some intermittent "Service Unavailable" errors while searching for npm packages. We wanted to inform you about this so you can investigate and address the issue. We greatly appreciate your efforts in providing this npm package for free, and we are confident that you'll be able to resolve it with your skill and kindness. Thank you for your valuable work!

Code

const NpmApi = require('npm-api'); const npm = new NpmApi(); const axios = require('axios');

/**

  • Searches for packages that match the given keyword and displays the results sorted by downloads.
  • @async
  • @param {string} searchText - The keyword to search for packages on npm.
  • @returns {Promise} - A promise that resolves when the search is complete. */ async function searchPackagesByText(searchText) { try { const view = npm.view('byKeyword'); const query = { key: JSON.stringify([searchText]) }; const results = await view.query(query); const sortedPackages = results.rows.sort((a, b) => b.value.downloads - a.value.downloads); console.log(Results for "${searchText}":); sortedPackages.forEach((package) => { console.log(${package.key[0]} - downloads: ${package.value.downloads}); }); } catch (error) { console.error('Error while searching and listing packages:', error); } }

/**

  • Searches for multiple packages sequentially and waits for a specified time between searches.

  • @async

  • @param {string} searchText - The keyword to search for packages on npm.

  • @returns {Promise} - A promise that resolves when all package searches are complete. */ async function searchDelayedPackages(searchText) { try { const waitTime = 10000; // 10 seconds (adjust the wait time as needed) const isNpmAvailable = await checkAvailabilityNpm();

    if (!isNpmAvailable) { console.log('The npm API is not available at this time. Please try again later.'); return; }

    const packagesToSearch = ['express', 'lodash', 'axios', 'react'];

    for (const packageName of packagesToSearch) { await searchPackagesByText(packageName); await wait(waitTime); } } catch (error) { console.error('Error searching packages:', error); } }

/**

  • Checks if the npm registry API is available.
  • @async
  • @returns {Promise} - A promise that resolves to a boolean indicating whether the npm registry API is available. */ async function checkAvailabilityNpm() { try { const response = await axios.get('https://registry.npmjs.org/'); return response.status === 200; } catch (error) { return false; } }

/**

  • Asynchronous function to wait for a specified time.
  • @param {number} time - The time in milliseconds to wait.
  • @returns {Promise} - A promise that resolves after the specified time. */ function wait(time) { return new Promise((resolve) => { setTimeout(resolve, time); }); }

// Initiating the package search with the keyword 'lodash'. searchDelayedPackages('lodash');

PS C:\Archivos\Desarrollo\Javascript\NodePackages> npm start

[email protected] start node program.js

Error al buscar y listar paquetes: Error: Service Unavailable at View.query (C:\Archivos\Desarrollo\Javascript\NodePackages\node_modules\npm-api\lib\view.js:47:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async buscarPaquetesPorTexto (C:\Archivos\Desarrollo\Javascript\NodePackages\program.js:9:21) at async buscarPaquetesConRetraso (C:\Archivos\Desarrollo\Javascript\NodePackages\program.js:37:7) Error al buscar y listar paquetes: Error: Service Unavailable at View.query (C:\Archivos\Desarrollo\Javascript\NodePackages\node_modules\npm-api\lib\view.js:47:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async buscarPaquetesPorTexto (C:\Archivos\Desarrollo\Javascript\NodePackages\program.js:9:21) at async buscarPaquetesConRetraso (C:\Archivos\Desarrollo\Javascript\NodePackages\program.js:37:7) Error al buscar y listar paquetes: Error: Service Unavailable at View.query (C:\Archivos\Desarrollo\Javascript\NodePackages\node_modules\npm-api\lib\view.js:47:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async buscarPaquetesPorTexto (C:\Archivos\Desarrollo\Javascript\NodePackages\program.js:9:21) at async buscarPaquetesConRetraso (C:\Archivos\Desarrollo\Javascript\NodePackages\program.js:37:7) Error al buscar y listar paquetes: Error: Service Unavailable at View.query (C:\Archivos\Desarrollo\Javascript\NodePackages\node_modules\npm-api\lib\view.js:47:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async buscarPaquetesPorTexto (C:\Archivos\Desarrollo\Javascript\NodePackages\program.js:9:21) at async buscarPaquetesConRetraso (C:\Archivos\Desarrollo\Javascript\NodePackages\program.js:37:7) PS C:\Archivos\Desarrollo\Javascript\NodePackages>

andresgarcia0313 avatar Aug 03 '23 16:08 andresgarcia0313