Browse Source

Refactor into class

xo
Luke Childs 8 years ago
parent
commit
183f5f177b
  1. 115
      src/index.js

115
src/index.js

@ -3,66 +3,77 @@ const NodeCache = require('node-cache');
const querystring = require('querystring'); const querystring = require('querystring');
const pkg = require('../package.json'); const pkg = require('../package.json');
const cache = new NodeCache(); class Onionoo {
const baseUrl = 'https://onionoo.torproject.org'; // Constructor returns a new object so instance properties are private
const endpoints = [ constructor(options = {}) {
'summary',
'details',
'bandwidth',
'weights',
'clients',
'uptime'
];
// Returns cache max age from response headers // Set default options
function checkResponseMaxAge(response) { this.options = Object.assign({}, {
const cacheControl = response.headers['cache-control']; cache: new NodeCache(),
const maxAgeRegex = /max-age=(\d+)/; baseUrl: 'https://onionoo.torproject.org',
const maxAge = cacheControl && cacheControl.match(maxAgeRegex); endpoints: [
return maxAge && maxAge[1]; 'summary',
} 'details',
'bandwidth',
'weights',
'clients',
'uptime'
]
}, options);
// Returns a function to make requests to a given endpoint // Return object containing endpoint methods
function createEndpointMethod(endpoint) { return this.options.endpoints.reduce((onionoo, endpoint) => {
return args => new Promise((resolve, reject) => { onionoo[endpoint] = this.createEndpointMethod(endpoint);
// Build query string (don't encode ':' for search filters) return onionoo;
const qs = querystring.encode(args).replace(/%3A/g, ':'); }, {});
}
// Build url // Returns cache max age from response headers
const url = `${baseUrl}/${endpoint}?${qs}`; checkResponseMaxAge(response) {
const cacheControl = response.headers['cache-control'];
const maxAgeRegex = /max-age=(\d+)/;
const maxAge = cacheControl && cacheControl.match(maxAgeRegex);
return maxAge && maxAge[1];
}
// Check for url in cache // Returns a function to make requests to a given endpoint
const cachedResult = cache.get(url); createEndpointMethod(endpoint) {
if(cachedResult) { return args => new Promise((resolve, reject) => {
resolve(cachedResult);
} else {
// Make request // Build query string (don't encode ':' for search filters)
const options = { const qs = querystring.encode(args).replace(/%3A/g, ':');
json: true,
'user-agent': `onionoo-node-client v${pkg.version} (${pkg.homepage})`
};
resolve(got(url, options)
.then(response => {
// Cache response // Build url
const ttl = checkResponseMaxAge(response); const url = `${this.options.baseUrl}/${endpoint}?${qs}`;
if(ttl) {
cache.set(url, response.body, ttl);
}
// Resolve response // Check for url in cache
return response.body; const cachedResult = this.options.cache.get(url);
})); if(cachedResult) {
} resolve(cachedResult);
}); } else {
}
// Make request
const options = {
json: true,
'user-agent': `onionoo-node-client v${pkg.version} (${pkg.homepage})`
};
resolve(got(url, options)
.then(response => {
// Return object containing endpoint methods // Cache response
module.exports = endpoints.reduce((onionoo, endpoint) => { const ttl = this.checkResponseMaxAge(response);
onionoo[endpoint] = createEndpointMethod(endpoint); if(ttl) {
this.options.cache.set(url, response.body, ttl);
}
// Resolve response
return response.body;
}));
}
});
}
}
return onionoo; module.exports = Onionoo;
}, {});

Loading…
Cancel
Save