(resp: Response, hadToken: boolean)
| 60 | } |
| 61 | |
| 62 | async function classify(resp: Response, hadToken: boolean): Promise<PushPermission> { |
| 63 | switch (resp.status) { |
| 64 | case 200: { |
| 65 | let body: unknown; |
| 66 | try { |
| 67 | body = await resp.json(); |
| 68 | } catch (err) { |
| 69 | log.warn({ err }, '[permissions] probe got 200 with unparseable JSON body'); |
| 70 | return { kind: 'unknown', error: 'malformed-response' }; |
| 71 | } |
| 72 | const push = readPushFlag(body); |
| 73 | if (push === null) { |
| 74 | log.warn( |
| 75 | { bodyKeys: typeof body === 'object' && body !== null ? Object.keys(body) : [] }, |
| 76 | '[permissions] probe got 200 without permissions.push field', |
| 77 | ); |
| 78 | return { kind: 'unknown', error: 'malformed-response' }; |
| 79 | } |
| 80 | return push ? { kind: 'allowed' } : { kind: 'denied', reason: 'no-collaborator' }; |
| 81 | } |
| 82 | case 401: |
| 83 | return { kind: 'unknown', error: 'token-invalid' }; |
| 84 | case 403: |
| 85 | return resp.headers.get('x-ratelimit-remaining') === '0' |
| 86 | ? { kind: 'unknown', error: 'rate-limit' } |
| 87 | : { kind: 'unknown', error: 'token-invalid' }; |
| 88 | case 429: |
| 89 | return { kind: 'unknown', error: 'rate-limit' }; |
| 90 | case 404: |
| 91 | return hadToken |
| 92 | ? { kind: 'denied', reason: 'private-no-access' } |
| 93 | : { kind: 'denied', reason: 'repo-not-found' }; |
| 94 | default: |
| 95 | log.warn({ httpStatus: resp.status }, '[permissions] probe got unexpected HTTP status'); |
| 96 | return { kind: 'unknown', error: 'malformed-response' }; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | async function resolveProbeTokenWithSource( |
| 101 | host: string, |
no test coverage detected