@@ -3,6 +3,7 @@ const https = require('https');
33const github_graphql = require ( 'github-graphql-client' ) ;
44const semver = require ( 'semver' ) ;
55const 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*/
4950const 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) => {
106118module . 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