Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Perform a HEAD or GET request based on the need to crawl
let status = 0;
let state = LinkState.BROKEN;
let data = '';
let shouldRecurse = false;
try {
let res = await gaxios.request({
method: opts.crawl ? 'GET' : 'HEAD',
url: opts.url.href,
responseType: opts.crawl ? 'text' : 'stream',
validateStatus: () => true,
});
// If we got an HTTP 405, the server may not like HEAD. GET instead!
if (res.status === 405) {
res = await gaxios.request({
method: 'GET',
url: opts.url.href,
responseType: 'stream',
validateStatus: () => true,
});
}
// Assume any 2xx status is 👌
status = res.status;
if (res.status >= 200 && res.status < 300) {
state = LinkState.OK;
}
data = res.data;
shouldRecurse = isHtml(res);
} catch (err) {
// request failure: invalid domain name, etc.
? JSON.parse(req.body.toString('utf8'))
: req.body) as Scheduled;
// PubSub messages have their payload encoded in body.message.data
// as a base64 blob.
if (body.message && body.message.data) {
body = JSON.parse(Buffer.from(body.message.data, 'base64').toString());
}
if (body.repo) {
// Job was scheduled for a single repository:
this.receivePromise(body.repo, id, body, eventName);
} else {
// Job should be run on all managed repositories:
const url =
'https://raw.githubusercontent.com/googleapis/sloth/master/repos.json';
const res = await request({ url });
const { repos } = res.data;
// We process WORK_SIZE repos in parallel:
const WORK_SIZE = 3;
while (repos.length) {
await Promise.all(
repos.splice(0, WORK_SIZE).map(repo => {
return this.receivePromise(repo.repo, id, body, eventName);
})
);
}
}
}
// reasons for this:
//
// 1. the DNS is slow in some GCP environments; by checking both, we might
// detect the runtime environment signficantly faster.
// 2. we can't just check the IP, which is tarpitted and slow to respond
// on a user's local machine.
//
// Additional logic has been added to make sure that we don't create an
// unhandled rejection in scenarios where a failure happens sometime
// after a success.
//
// Note, however, if a failure happens prior to a success, a rejection should
// occur, this is for folks running locally.
//
let responded = false;
const r1: Promise = request(options)
.then(res => {
responded = true;
return res;
})
.catch(err => {
if (responded) {
return r2;
} else {
responded = true;
throw err;
}
});
const r2: Promise = request(secondaryOptions)
.then(res => {
responded = true;
return res;
async function getRepoIssues(repo: Repo, flags?: Flags): Promise {
const [owner, name] = repo.repo.split('/');
const result = {issues: new Array(), repo};
let res!: GaxiosResponse;
const rootUrl = 'https://drghs.endpoints.devrel-prod.cloud.goog/api/v1';
const url = `${rootUrl}/${repo.repo}/issues?key=${apiKey}&closed=false`;
try {
res = await request({url});
} catch (e) {
console.warn(`Error fetching issues for ${repo.repo}.`);
//console.warn(e);
return result;
}
if (!res.data || !res.data.Issues) {
return result;
}
res!.data.Issues.forEach(r => {
const api = getApi(r, repo);
const issue: Issue = {
owner,
name,
language: repo.language,
async function quickstart() {
const url = 'https://www.googleapis.com/discovery/v1/apis/';
const res = await request({url});
console.log(`status: ${res.status}`);
console.log(`data:`);
console.log(res.data);
}
quickstart();
//
let responded = false;
const r1: Promise = request(options)
.then(res => {
responded = true;
return res;
})
.catch(err => {
if (responded) {
return r2;
} else {
responded = true;
throw err;
}
});
const r2: Promise = request(secondaryOptions)
.then(res => {
responded = true;
return res;
})
.catch(err => {
if (responded) {
return r1;
} else {
responded = true;
throw err;
}
});
return Promise.race([r1, r2]);
}
private async apiPost(path: string, body?: {}): Promise {
const url = urlParse('https://api.github.com');
url.pathname = posixPath.join(this.pathPrefix, path);
const resp = await request({
method: 'POST',
url: urlFormat(url),
data: body,
...this.getAxiosConfig(),
});
return resp.data;
}
private async apiGet(
path: string,
params?: QueryParams
): Promise {
const url = urlParse('https://api.github.com', true);
url.pathname = posixPath.join(this.pathPrefix, path);
if (params) {
url.query = params;
}
const resp = await request({
method: 'GET',
url: urlFormat(url),
...this.getAxiosConfig(),
});
return resp.data;
}