Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.

Commit 0446910

Browse files
author
Axel Rindle
committed
Provide option for disabling traffic reduction
See https://github.com/axelrindle/gvc-json-reduce for the related project
1 parent d0719d3 commit 0446910

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

examples/rest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const versionCheck = require('../lib/main');
22
const options = {
3-
token: '',
43
repo: 'github-version-checker',
54
owner: 'axelrindle',
6-
currentVersion: require('../package.json').version
5+
currentVersion: require('../package.json').version,
6+
reduceTraffic: false
77
};
88

99
versionCheck(options)

lib/check.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const https = require('https');
33
const github_graphql = require('github-graphql-client');
44
const semver = require('semver');
55
const query = require('./query');
6+
const pkg = require('../package.json');
67

78
/**
89
* Executes a GraphQL query on the API to fetch either the latest release or tag.
@@ -47,18 +48,12 @@ const graphql = (options, callback) => {
4748
* @param callback {function|undefined} The callback function.
4849
*/
4950
const rest = (options, callback) => {
50-
const apiUrl = `https://api.github.com/repos/${options.owner}/${options.repo}/releases`;
51-
const opts = {
52-
hostname: 'gvc-reduce-json.axelrindle.de',
53-
path: '/?url=' + apiUrl,
54-
method: 'GET'
55-
};
56-
const req = https.request(opts, res => {
51+
const handler = res => {
5752
const chunks = [];
5853
res.on('data', chunk => {
5954
return chunks.push(chunk.toString());
6055
});
61-
res.on('end', function() {
56+
res.on('end', () => {
6257
// Make sure there are no errors and try to parse the response
6358
if (res.statusCode !== 200) {
6459
return callback(new Error(chunks), null);
@@ -88,8 +83,25 @@ const rest = (options, callback) => {
8883
callback(null, null);
8984
}
9085
});
91-
});
92-
req.on('error', function(err) {
86+
};
87+
88+
const apiUrl = `https://api.github.com/repos/${options.owner}/${options.repo}/releases`;
89+
let req;
90+
if (options.reduceTraffic) {
91+
const opts = {
92+
hostname: 'gvc-reduce-json.axelrindle.de',
93+
path: '/?url=' + apiUrl
94+
};
95+
req = https.get(opts, handler);
96+
} else {
97+
const opts = {
98+
headers: {
99+
'User-Agent': `${pkg.name} v${pkg.version}`
100+
}
101+
};
102+
req = https.get(apiUrl, opts, handler);
103+
}
104+
req.on('error', err => {
93105
callback(err, null);
94106
});
95107
req.end();
@@ -106,6 +118,7 @@ const rest = (options, callback) => {
106118
module.exports = (options, callback) => {
107119
// get options
108120
options.token = options.token || process.env.GITHUB_API_TOKEN || undefined;
121+
options.reduceTraffic = options.reduceTraffic !== undefined ? options.reduceTraffic : true;
109122

110123
// check if required options are defined
111124
if (!options.repo) {

0 commit comments

Comments
 (0)