Skip to content

Commit

Permalink
feat(rate limiting): use the github bearer token to authenticate requ…
Browse files Browse the repository at this point in the history
…ests to increase the rate limiting of the github api
  • Loading branch information
Fresa committed Dec 29, 2022
1 parent 581d7b3 commit 23441b7
Showing 1 changed file with 52 additions and 55 deletions.
107 changes: 52 additions & 55 deletions fetch_artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,6 @@ const core = new (class {
}
})();

const getAsync = async (url, asStream = false) => {
const getAsyncCalback = (url, resolve, reject) => {
https.get(
url,
{
headers: {
Accept: asStream ? 'text/html' : 'application/vnd.github.v3+json',
'User-Agent': 'artifact-downloader'
}
},
(res) => {
if (res.statusCode === 301 || res.statusCode === 302) {
return getAsyncCalback(res.headers.location, resolve, reject);
}

if (asStream) {
return resolve({
status: res.statusCode,
statusText: res.statusMessage,
data: res
});
}

let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
resolve({
status: res.statusCode,
statusText: res.statusMessage,
data: JSON.parse(rawData)
});
});
res.on('error', (err) => {
reject({
error: {
status: res.statusCode,
statusText: res.statusMessage,
data: err
}
});
});
}
);
};

return new Promise((resolve, reject) =>
getAsyncCalback(url, resolve, reject)
);
};

const getEnvironmentVariable = (name) => {
const value = process.env[name];
if (['', null, undefined].includes(value)) {
Expand All @@ -92,9 +40,58 @@ try {
const api_url = getEnvironmentVariable('GITHUB_API_URL');
const token = getEnvironmentVariable('INPUT_GITHUB_TOKEN');

if (token) {
core.info('Token set');
}
const getAsync = async (url, asStream = false) => {
const getAsyncCalback = (url, resolve, reject) => {
https.get(
url,
{
headers: {
Accept: asStream ? 'text/html' : 'application/vnd.github.v3+json',
'User-Agent': 'artifact-downloader',
Authorization: `Bearer ${token}`
}
},
(res) => {
if (res.statusCode === 301 || res.statusCode === 302) {
return getAsyncCalback(res.headers.location, resolve, reject);
}

if (asStream) {
return resolve({
status: res.statusCode,
statusText: res.statusMessage,
data: res
});
}

let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
resolve({
status: res.statusCode,
statusText: res.statusMessage,
data: JSON.parse(rawData)
});
});
res.on('error', (err) => {
reject({
error: {
status: res.statusCode,
statusText: res.statusMessage,
data: err
}
});
});
}
);
};

return new Promise((resolve, reject) =>
getAsyncCalback(url, resolve, reject)
);
};

const ensureSuccessStatusCode = (response) => {
if (response.status !== 200) {
Expand Down

0 comments on commit 23441b7

Please sign in to comment.