familysearch
Advanced tools
Comparing version 0.0.5 to 1.0.0
@@ -6,4 +6,7 @@ | ||
, error = debuggers('familysearch:error') | ||
, perf = debuggers('familysearch:perf') | ||
, requestlog = debuggers('familysearch:request') | ||
, CACHE_ONLY = process.env.CACHE_ONLY | ||
, utils = require('./utils'); | ||
@@ -25,7 +28,28 @@ | ||
this.queuing = {}; | ||
this.cache_queue = {} | ||
this.getMeta(); | ||
}; | ||
var EXTRA_LINKS = { | ||
"person-not-a-match-template" : { | ||
"template" : "https://familysearch.org/platform/tree/persons/{pid}/not-a-match.json{?access_token}", | ||
"type" : "application/atom+xml,application/json,application/x-gedcomx-atom+json,application/xml,text/html", | ||
"accept" : "*/*", | ||
"allow" : "GET", | ||
"title" : "Person Not A Match" | ||
}, | ||
} | ||
function addExtraLinks(links) { | ||
for (var name in EXTRA_LINKS) { | ||
links[name] = EXTRA_LINKS[name] | ||
} | ||
} | ||
FS.prototype = { | ||
metaUrl: 'https://familysearch.org/.well-known/app-meta', | ||
onDone: function (func) { | ||
if (this.links !== null) return func() | ||
this.ondone.push(func) | ||
}, | ||
getMeta: function () { | ||
@@ -46,2 +70,3 @@ var self = this; | ||
self.links = res.body.links; | ||
addExtraLinks(self.links) | ||
debug('Got meta links', self.links.length); | ||
@@ -67,5 +92,8 @@ for (var name in self.links) { | ||
} | ||
if (!this.links) { | ||
return next(new Error("Links not fetched yet...")) | ||
} | ||
if (!this.links[endpoint]) { | ||
error('Invalid endpoint', endpoint); | ||
return next('Invalid endpoint'); | ||
return next('Invalid endpoint: ' + endpoint); | ||
} | ||
@@ -137,2 +165,68 @@ var url = this.links[endpoint].href || this.links[endpoint].url(options) | ||
}) | ||
}, | ||
bouncy_cached: function (collection, options, done) { | ||
var key = options.path + ' : ' + JSON.stringify(options.data) + ' : ' + options.token | ||
if (this.cache_queue[key]) { | ||
perf('bounce', key) | ||
return this.cache_queue[key].push(done) | ||
} | ||
var queue = this.cache_queue[key] = [done] | ||
, that = this | ||
this.cached(collection, options, function (err, data) { | ||
for (var i=0; i<queue.length; i++) { | ||
queue[i](err, data) | ||
} | ||
delete that.cache_queue[key] | ||
}) | ||
}, | ||
cached: function (collection, options, done) { | ||
var endpoint = options.path | ||
, post = options.data | ||
, token = options.token | ||
, newtime = options.newtime || 0 | ||
var that = this | ||
, start = new Date() | ||
function gotten(err, data, res) { | ||
if (err) return done(err, data) | ||
perf('Made request', endpoint, post, new Date().getTime() - start.getTime()) | ||
collection.update({ | ||
endpoint: endpoint, | ||
post: post | ||
}, { | ||
endpoint: endpoint, | ||
post: post, | ||
data: data, | ||
etag: res.header.etag, | ||
time: new Date() | ||
}, {upsert: true}, function () { | ||
perf('Saved in cache', endpoint, post, new Date().getTime() - start.getTime()) | ||
done(err, data, res) | ||
}) | ||
} | ||
perf('looking for', endpoint, post, start) | ||
collection.findOne({ | ||
endpoint: endpoint, | ||
post: post | ||
}, function (err, cached) { | ||
if (err) { | ||
console.error('Error getting cached') | ||
return that.get(endpoint, post, token, gotten) | ||
} | ||
if (!cached) return that.get(endpoint, post, token, gotten) | ||
var now = new Date().getTime() | ||
// skip the api call | ||
if (CACHE_ONLY || (now - cached.time.getTime())/1000 < newtime) { | ||
perf('Skipping request, cache is new enough', endpoint, post, new Date().getTime() - start.getTime()) | ||
return done(null, cached.data, null) | ||
} | ||
that.get(endpoint, post, token, cached.etag, function (err, data, res) { | ||
if (err) return done(err) | ||
if (res.status === 304) { | ||
perf('Cache hit from request', endpoint, post, new Date().getTime() - start.getTime()) | ||
return done(null, cached.data, res) | ||
} | ||
gotten(err, data, res) | ||
}) | ||
}) | ||
} | ||
@@ -139,0 +233,0 @@ }; |
{ | ||
"name": "familysearch", | ||
"version": "0.0.5", | ||
"version": "1.0.0", | ||
"description": "FamilySearch API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11698
283
1
1