|
10 | 10 | const { StargazersChecker } = require('./StargazersChecker'); |
11 | 11 | const { FetchRepositoryStargazers } = require('./FetchRepositoryStargazers'); |
12 | 12 |
|
| 13 | +// The stargazers list is walked one page at a time and a username that never |
| 14 | +// starred the repository only settles on the very last page. Both limits below |
| 15 | +// stop that walk from growing with the repository and taking the whole request |
| 16 | +// down with it, the flag only controls a footer on the card so falling back to |
| 17 | +// "not a stargazer" is far better than failing to answer at all. |
| 18 | +const MAX_PAGES = 10; |
| 19 | +const DEADLINE_IN_MILLISECONDS = 4000; |
| 20 | + |
13 | 21 | const StargazersController = async (username, items) => { |
14 | 22 |
|
15 | 23 | // The default value or the max allowed items from the github |
16 | 24 | // to be fetch for each page. |
17 | 25 | if (typeof items === 'undefined') { |
18 | | - items = '100'; |
| 26 | + items = '100'; |
| 27 | + } |
| 28 | + |
| 29 | + if (!username) { |
| 30 | + return false; |
19 | 31 | } |
20 | 32 |
|
| 33 | + const deadline = Date.now() + DEADLINE_IN_MILLISECONDS; |
| 34 | + |
21 | 35 | let isStargazer = false; |
22 | 36 | let nextPage = true; |
23 | 37 | let stargazerCheckingEnd = false; |
24 | 38 | let cursor = ''; |
| 39 | + let pages = 0; |
25 | 40 |
|
26 | | - do { |
| 41 | + try { |
27 | 42 |
|
28 | | - let repositoryStargazers = {}; |
| 43 | + do { |
29 | 44 |
|
30 | | - if (cursor) { |
31 | | - repositoryStargazers = await FetchRepositoryStargazers(items, cursor); |
32 | | - } else { |
33 | | - repositoryStargazers = await FetchRepositoryStargazers(items); |
34 | | - } |
| 45 | + let repositoryStargazers = {}; |
35 | 46 |
|
36 | | - if (typeof repositoryStargazers.data !== 'undefined' && typeof repositoryStargazers.data.repository.stargazers !== 'undefined') { |
37 | | - |
38 | | - isStargazer = StargazersChecker(username, repositoryStargazers.data.repository.stargazers); |
39 | | - |
40 | | - // Is a legit user who starred the repository :-) |
41 | | - if (isStargazer) { |
42 | | - stargazerCheckingEnd = true; |
43 | | - break; |
| 47 | + if (cursor) { |
| 48 | + repositoryStargazers = await FetchRepositoryStargazers(items, cursor); |
| 49 | + } else { |
| 50 | + repositoryStargazers = await FetchRepositoryStargazers(items); |
44 | 51 | } |
45 | | - |
46 | | - nextPage = repositoryStargazers.data.repository.stargazers.pageInfo.hasNextPage; |
47 | 52 |
|
48 | | - // At this point we are now sure that the user |
49 | | - // is not a stargazer of the repository. |
50 | | - if (!nextPage) { |
51 | | - break; |
| 53 | + pages++; |
| 54 | + |
| 55 | + if (typeof repositoryStargazers.data !== 'undefined' && typeof repositoryStargazers.data.repository.stargazers !== 'undefined') { |
| 56 | + |
| 57 | + isStargazer = StargazersChecker(username, repositoryStargazers.data.repository.stargazers); |
| 58 | + |
| 59 | + // Is a legit user who starred the repository :-) |
| 60 | + if (isStargazer) { |
| 61 | + stargazerCheckingEnd = true; |
| 62 | + break; |
| 63 | + } |
| 64 | + |
| 65 | + nextPage = repositoryStargazers.data.repository.stargazers.pageInfo.hasNextPage; |
| 66 | + |
| 67 | + // At this point we are now sure that the user |
| 68 | + // is not a stargazer of the repository. |
| 69 | + if (!nextPage) { |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + // Give up the search once either budget is spent, the remaining pages |
| 74 | + // are not worth the risk of timing out the request. |
| 75 | + if (pages >= MAX_PAGES || Date.now() >= deadline) { |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + // Continue the searching if the user is a stargazer for the repository. |
| 80 | + // Maybe we can find it on the next page. |
| 81 | + const edges = repositoryStargazers.data.repository.stargazers.edges; |
| 82 | + cursor = edges[edges.length - 1].cursor; |
| 83 | + continue; |
52 | 84 | } |
53 | 85 |
|
54 | | - // Continue the searching if the user is a stargazer for the repository. |
55 | | - // Maybe we can find it on the next page. |
56 | | - const edges = repositoryStargazers.data.repository.stargazers.edges; |
57 | | - cursor = edges[edges.length - 1].cursor; |
58 | | - continue; |
59 | | - } |
| 86 | + stargazerCheckingEnd = true; |
| 87 | + |
| 88 | + } while (!stargazerCheckingEnd && nextPage); |
60 | 89 |
|
61 | | - stargazerCheckingEnd = true; |
| 90 | + } catch (error) { |
62 | 91 |
|
63 | | - } while (!stargazerCheckingEnd && nextPage); |
| 92 | + // Never let a cosmetic lookup take down the card. |
| 93 | + console.error(error); |
| 94 | + return false; |
| 95 | + } |
64 | 96 |
|
65 | 97 | return isStargazer; |
66 | 98 |
|
|
0 commit comments