Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service Unavailable #26

Open
andresgarcia0313 opened this issue Aug 3, 2023 · 3 comments
Open

Service Unavailable #26

andresgarcia0313 opened this issue Aug 3, 2023 · 3 comments

Comments

@andresgarcia0313
Copy link

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

nodepackages@1.0.0 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
Copy link
Author

@andresgarcia0313
Copy link
Author

I closed it by mistake

@zqw450397740
Copy link

Hello, is this service still available? I tried querying for View and List but it said "service unavailable".

Error

/Users/.../node_modules/npm-api/lib/view.js:47
      throw new Error(response.statusText);
            ^

Error: Service Unavailable
    at View.query (/Users/.../node_modules/npm-api/lib/view.js:47:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Code

const View = require("npm-api/lib/view.js");
const view = new View('graphql');
let results = view.query({
    group_level: 2,
    startkey: JSON.stringify(['graphql']),
    endkey: JSON.stringify(['graphql', {}])
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants