fixed issue with redis models
This commit is contained in:
+45
-15
@@ -7,6 +7,11 @@ const sleep = require('./sleep');
|
||||
|
||||
// https://dns.google/resolve?name=${name}&type=TXT
|
||||
|
||||
AcmeClient.setLogger((message) => {
|
||||
console.log('ACME:', message);
|
||||
});
|
||||
|
||||
|
||||
class LetsEncrypt{
|
||||
static AcmeClient = AcmeClient;
|
||||
|
||||
@@ -43,32 +48,57 @@ class LetsEncrypt{
|
||||
|
||||
try{
|
||||
domain = domain.replace(/^\*\./, '');
|
||||
|
||||
const [key, csr] = await AcmeClient.crypto.createCsr({
|
||||
altNames: [domain, `*.${domain}`],
|
||||
});
|
||||
|
||||
let dnsToAdd = 0;
|
||||
let dnsFound = 0;
|
||||
|
||||
const cert = await this.client.auto({
|
||||
csr,
|
||||
email: 'wmantly@gmail.com',
|
||||
termsOfServiceAgreed: true,
|
||||
challengePriority: ['dns-01'],
|
||||
skipChallengeVerification: true,
|
||||
challengeCreateFn: async (authz, challenge, keyAuthorization) => {
|
||||
// console.log(`start TXT record key=_acme-challenge.${authz.identifier.value} value=${keyAuthorization}`)
|
||||
|
||||
let resCheck = await axios.get(`https://dns.google/resolve?name=_acme-challenge.${authz.identifier.value}&type=TXT`);
|
||||
if(resCheck.data.Answer.some(record => record.data === keyAuthorization)) return;
|
||||
|
||||
await options.challengeCreateFn(authz, challenge, keyAuthorization);
|
||||
|
||||
let checkCount = 0;
|
||||
while(true){
|
||||
await sleep(1500);
|
||||
let res = await axios.get(`https://dns.google/resolve?name=_acme-challenge.${authz.identifier.value}&type=TXT`);
|
||||
if(res.data.Answer.some(record => record.data === keyAuthorization)){
|
||||
// console.log(`found record for key=_acme-challenge.${authz.identifier.value} value=${keyAuthorization}`)
|
||||
break;
|
||||
try{
|
||||
console.log('challenge', challenge)
|
||||
console.log(`start TXT record key=_acme-challenge.${authz.identifier.value} value=${keyAuthorization} challenge=${challenge} googleDNS=https://dns.google/resolve?name=_acme-challenge.${authz.identifier.value}&type=TXT`)
|
||||
dnsToAdd++
|
||||
let resCheck = await axios.get(`https://dns.google/resolve?name=_acme-challenge.${authz.identifier.value}&type=TXT`);
|
||||
if(resCheck.data.Answer && resCheck.data.Answer.some(record => record.data === keyAuthorization)){
|
||||
await sleep(1000);
|
||||
dnsFound++
|
||||
if(dnsFound === dnsToAdd){
|
||||
options.onDnsCheckFound(authz, dnsFound)
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(checkCount++ > 60) throw new Error('challengeCreateFn validation timed out');
|
||||
|
||||
await options.challengeCreateFn(authz, challenge, keyAuthorization);
|
||||
|
||||
let checkCount = 0;
|
||||
while(true){
|
||||
options.onDnsCheck(authz, checkCount);
|
||||
let res = await axios.get(`https://dns.google/resolve?name=_acme-challenge.${authz.identifier.value}&type=TXT`);
|
||||
// console.log(keyAuthorization, res.data);
|
||||
if(res.data.Answer && res.data.Answer.some(record => record.data === keyAuthorization)){
|
||||
dnsFound++
|
||||
if(dnsFound === dnsToAdd){
|
||||
options.onDnsCheckFound(authz, dnsFound)
|
||||
}
|
||||
// console.log(`found record for key=_acme-challenge.${authz.identifier.value} value=${keyAuthorization}`)
|
||||
await sleep(10000);
|
||||
break;
|
||||
}
|
||||
if(checkCount++ > 60) throw new Error('challengeCreateFn validation timed out');
|
||||
await sleep(1500);
|
||||
}
|
||||
}catch(error){
|
||||
console.log('dns check failed error:', error)
|
||||
options.onDnsCheckFail(authz, error)
|
||||
}
|
||||
},
|
||||
challengeRemoveFn: options.challengeRemoveFn,
|
||||
|
||||
@@ -12,9 +12,13 @@ function ModelPs(model){
|
||||
}
|
||||
|
||||
function publish(prop, res, req){
|
||||
if(!['add', 'create', 'update', 'remove'].includes(prop)) return;
|
||||
try{
|
||||
if(!['add', 'create', 'update', 'remove'].includes(prop)) return;
|
||||
|
||||
ps.publish(`model:${Model.name}:${prop}:${getIndex(res, req)}`, res);
|
||||
ps.publish(`model:${Model.name}:${prop}:${getIndex(res, req)}`, res);
|
||||
}catch(error){
|
||||
console.log('ModelPs.publish ERROR', error)
|
||||
}
|
||||
}
|
||||
|
||||
return new Proxy(model, {
|
||||
@@ -28,16 +32,22 @@ function ModelPs(model){
|
||||
const targetValue = Reflect.get(target, propKey, receiver);
|
||||
if (typeof targetValue === 'function') {
|
||||
return function(...args){
|
||||
try{
|
||||
// let res = targetValue.apply(this, args); // (A)
|
||||
let res = Reflect.apply(targetValue, this, args);
|
||||
if(targetValue.constructor.name === 'AsyncFunction'){
|
||||
res.then(function(res){
|
||||
publish(propKey, res, ...args);
|
||||
});
|
||||
}else{
|
||||
publish(propKey, res, ...args)
|
||||
var res = Reflect.apply(targetValue, this, args);
|
||||
if(targetValue.constructor.name === 'AsyncFunction'){
|
||||
res.then(function(res){
|
||||
publish(propKey, res, ...args);
|
||||
}).catch(function(error){
|
||||
console.log('toDo, publish errors...')
|
||||
});
|
||||
}else{
|
||||
publish(propKey, res, ...args)
|
||||
}
|
||||
return res;
|
||||
}catch(error){
|
||||
console.log("grrrr")
|
||||
}
|
||||
return res;
|
||||
}
|
||||
} else {
|
||||
return targetValue;
|
||||
|
||||
@@ -33,6 +33,8 @@ function processKeys(map, data, partial){
|
||||
continue;
|
||||
}
|
||||
|
||||
// console.log(key, data[key], map[key].default, data.hasOwnProperty(key) && data[key] !== undefined ? data[key] : returnOrCall(map[key].default))
|
||||
|
||||
out[key] = data.hasOwnProperty(key) && data[key] !== undefined ? data[key] : returnOrCall(map[key].default);
|
||||
|
||||
if(data.hasOwnProperty(key) && process_type[map[key].type]){
|
||||
@@ -46,7 +48,6 @@ function processKeys(map, data, partial){
|
||||
}
|
||||
|
||||
if(errors.length !== 0){
|
||||
console.log('errors', errors)
|
||||
throw new ObjectValidateError(errors);
|
||||
return {__errors__: errors};
|
||||
}
|
||||
@@ -79,9 +80,10 @@ function parseToString(data){
|
||||
return (types[typeof(data)] || String)(data);
|
||||
}
|
||||
|
||||
function ObjectValidateError(message) {
|
||||
function ObjectValidateError(message){
|
||||
this.name = 'ObjectValidateError';
|
||||
this.message = (message || {});
|
||||
this.keys = (message || {})
|
||||
this.status = 422;
|
||||
}
|
||||
|
||||
|
||||
+35
-11
@@ -47,17 +47,17 @@ class Table{
|
||||
}catch(error){
|
||||
throw error;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static async exists(index){
|
||||
try{
|
||||
await this.get(data);
|
||||
|
||||
return true;
|
||||
}catch(error){
|
||||
return false;
|
||||
if(typeof index === 'object'){
|
||||
index = index[this._key];
|
||||
}
|
||||
|
||||
return await client.SISMEMBER(
|
||||
redisPrefix(this.prototype.constructor.name),
|
||||
index
|
||||
);
|
||||
}
|
||||
|
||||
static async list(){
|
||||
@@ -86,6 +86,7 @@ class Table{
|
||||
static async create(data){
|
||||
// Add a entry to this redis table.
|
||||
try{
|
||||
|
||||
// Validate the passed data by the keyMap schema.
|
||||
data = objValidate.processKeys(this._keyMap, data);
|
||||
|
||||
@@ -94,6 +95,10 @@ class Table{
|
||||
let error = new Error('EntryNameUsed');
|
||||
error.name = 'EntryNameUsed';
|
||||
error.message = `${this.prototype.constructor.name}:${data[this._key]} already exists`;
|
||||
error.keys = [{
|
||||
key: this._key,
|
||||
message: `${this.prototype.constructor.name}:${data[this._key]} already exists`
|
||||
}]
|
||||
error.status = 409;
|
||||
|
||||
throw error;
|
||||
@@ -107,7 +112,7 @@ class Table{
|
||||
|
||||
// Add the values for this entry.
|
||||
for(let key of Object.keys(data)){
|
||||
if(!data[key]) continue;
|
||||
if(data[key] === undefined) continue;
|
||||
await client.HSET(
|
||||
redisPrefix(`${this.prototype.constructor.name}_${data[this._key]}`),
|
||||
key,
|
||||
@@ -125,9 +130,26 @@ class Table{
|
||||
async update(data, key){
|
||||
// Update an existing entry.
|
||||
try{
|
||||
// Validate the passed data, ignoring required fields.
|
||||
data = objValidate.processKeys(this.constructor._keyMap, data, true);
|
||||
|
||||
// Check to see if entry name changed.
|
||||
if(data[this.constructor._key] && data[this.constructor._key] !== this[this.constructor._key]){
|
||||
// Remove the index key from the tables members list.
|
||||
|
||||
if(data[this.constructor._key] && await this.constructor.exists(data)){
|
||||
let error = new Error('EntryNameUsed');
|
||||
error.name = 'EntryNameUsed';
|
||||
error.message = `${this.constructor.name}:${data[this.constructor._key]} already exists`;
|
||||
error.keys = [{
|
||||
key: this.constructor._key,
|
||||
message: `${this.constructor.name}:${data[this.constructor._key]} already exists`
|
||||
}]
|
||||
error.status = 409;
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
await client.SREM(
|
||||
redisPrefix(this.constructor.name),
|
||||
this[this.constructor._key]
|
||||
@@ -139,12 +161,14 @@ class Table{
|
||||
data[this.constructor._key]
|
||||
);
|
||||
|
||||
await client.RENAME(
|
||||
redisPrefix(`${this.constructor.name}_${this[this.constructor._key]}`),
|
||||
redisPrefix(`${this.constructor.name}_${data[this.constructor._key]}`),
|
||||
);
|
||||
|
||||
}
|
||||
// Update what ever fields that where passed.
|
||||
|
||||
// Validate the passed data, ignoring required fields.
|
||||
data = objValidate.processKeys(this.constructor._keyMap, data, true);
|
||||
|
||||
// Loop over the data fields and apply them to redis
|
||||
for(let key of Object.keys(data)){
|
||||
this[key] = data[key];
|
||||
|
||||
Reference in New Issue
Block a user