Skip to content

Commit

Permalink
Exposes ability to reset cache, check for token, get token, and clear…
Browse files Browse the repository at this point in the history
… token for a particular domain
  • Loading branch information
aultac committed Apr 30, 2020
1 parent 6847c6f commit 9f188e7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oada/oada-cache",
"version": "3.1.7",
"version": "3.1.8",
"description": "node library for interacting with and locally caching data served on an oada-compliant server",
"main": "./src/index.js",
"directories": {
Expand Down
40 changes: 39 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const _TOKEN = require("./token");
// debug
const error = require("debug")("oada-cache:index:error");
const info = require("debug")("oada-cache:index:info");
const trace = require("debug")("oada-cache:index:trace");

let dbprefix = "";
const setDbPrefix = pfx => (dbprefix = pfx);
Expand All @@ -20,6 +21,10 @@ process.on('unhandledRejection', (reason, p) => {
console.log(reason);
});

function domainToCacheName(domain) {
return urlLib.parse(domain).hostname.replace(/\./g, "_");
}

var connect = async function connect({
domain,
options,
Expand Down Expand Up @@ -70,7 +75,7 @@ var connect = async function connect({
var NAME =
cache && cache.name
? cache.name
: urlLib.parse(domain).hostname.replace(/\./g, "_");
: domainToCacheName(domain); //urlLib.parse(domain).hostname.replace(/\./g, "_");
var EXPIRES = cache && cache.expires ? cache.expires : undefined;

function _replaceLinks(obj) {
Expand Down Expand Up @@ -869,7 +874,40 @@ var connect = async function connect({
};
};

const resetDomainCache = async function(domain) {
trace('resetDomainCache: setting up cache');
const cache = setupCache({ name: domainToCacheName(domain), dbprefix })
trace('resetDomainCache: cache is setup, awaiting reset');
await cache.resetCache();
trace('resetDomainCache: cache reset done, awaiting domain token reset');
await clearDomainToken(domain);
trace('resetDomainCache: DONE!');
}

const clearDomainToken = async function(domain) {
trace('clearDomainToken: creating new Token lib');
const token = new _TOKEN({ domain, dbprefix });
trace('clearDomainToken: created new Token lib, awaiting cleanUp()');
await token.cleanUp();
trace('clearDomainToken: DONE!');
}

const getDomainToken = async function(domain) {
trace('getDomainToken: creating token lib');
const token = new _TOKEN({domain,dbprefix});
trace('getDomainToken: token lib created, checking for token in DB');
return await token.checkTokenDB(); // returns the token string
}

const haveDomainToken = async function(domain) {
return !!(await getDomainToken(domain));
}

module.exports = {
connect,
resetDomainCache,
clearDomainToken,
haveDomainToken,
getDomainToken,
setDbPrefix,
};
15 changes: 11 additions & 4 deletions src/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ const _ = require("lodash");
//const debug = require("debug")("oada-cache:token");
const crypto = require("crypto");
const oadaIdClient = require("@oada/oada-id-client");
//const error = require('debug')('oada-cache:index:error');
//const info = require('debug')('oada-cache:index:info');
const error = require('debug')('oada-cache:token:error');
const info = require('debug')('oada-cache:token:info');
const trace = require('debug')('oada-cache:token:trace');

class Token {
constructor(param = {}) {
Expand All @@ -40,19 +41,22 @@ class Token {
self._domain = param.domain || "localhost";
self._options = param.options;
self._dbprefix = param.dbprefix || "";
trace('constructor: domain = ', self._domain, ', dbprefix = ', self._dbprefix);

// creating database nae based on the domain
// creating database name based on the domain
// ensured one to one correspondence with the domain
// i.e., token belongs to that domain
const hash = crypto.createHash("sha256");
hash.update(self._domain);
self._name = self._dbprefix + hash.digest("hex");
trace('Token DB name is: ', self._name);

self._isSet = self._token ? true : false;
self._tokenDB = new PouchDB(self._name);
self._id = "OadaTokenID";
self._rev = null;
self.token = self._token ? self._token : "";
trace('constructor: self.token = ', self.token);
} //constructor

/**
Expand All @@ -63,10 +67,11 @@ class Token {
try {
//getting the doc from the server if exists
let doc = await this._tokenDB.get(this._id);
// debug("received document ->", doc);
trace('checkTokenDB: received doc ', doc);
result = doc.token;
this._rev = doc._rev;
} catch (err) {
error('ERROR: failed to tokenDB.get('+this._id+'). Error was: ', err);
return result;
}
return result;
Expand All @@ -80,8 +85,10 @@ class Token {
// Get a token
let TOKEN = null; //returned to the chache library
if (this.isSet()) {
trace('setup: token is already set on self, using that: ', this.token);
TOKEN = this.token;
} else {
trace('setup: token is not set, checking tokenDB');
// get token from local cache
TOKEN = await this.checkTokenDB();

Expand Down

0 comments on commit 9f188e7

Please sign in to comment.