Stefan Huber

new rel

......@@ -13,14 +13,17 @@ export declare class Repository {
findByIds(ids: Array<string>): Promise<Array<any>>;
findByType(type: string): Promise<Array<any>>;
replicate(deviceInfo: DeviceInfo): Promise<boolean>;
tryLocalEnvironment(response: any): Promise<void>;
prepare(url: string): Promise<any>;
init(db_name: string): Promise<any>;
parseUrl(url: string): {
url: string;
protocol: string;
user: string;
pass: string;
domain: string;
db_name: string;
orig_db_name: string;
};
prepareDocs(res: any): Array<any>;
}
......
......@@ -82,6 +82,9 @@ var Repository = (function () {
else if (response && response.exit == 1) {
_this.device.exit();
}
else if (response && response.local_ips) {
return _this.tryLocalEnvironment(response);
}
else {
return _this.prepare(response.db_url);
}
......@@ -95,6 +98,18 @@ var Repository = (function () {
.catch(function (error) { reject(error); });
});
};
Repository.prototype.tryLocalEnvironment = function (response) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.rest.scanEnvironment(response.local_ips)
.then(function (node) {
var res = _this.parseUrl(response.db_url);
resolve(_this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + res.domain + '/' + res.orig_db_name));
}).catch(function () {
resolve(_this.prepare(response.db_url));
});
});
};
Repository.prototype.prepare = function (url) {
var _this = this;
return new Promise(function (resolve, reject) {
......@@ -138,15 +153,17 @@ var Repository = (function () {
};
Repository.prototype.parseUrl = function (url) {
// matches: 0:user,1:password,2:domain,3:db_name
var exp = /^https?:\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/;
var exp = /^(https?):\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/;
var match = url.match(exp);
if (match) {
return {
url: match[0],
user: match[1],
pass: match[2],
domain: match[3],
db_name: 'local_' + match[4]
protocol: match[1],
user: match[2],
pass: match[3],
domain: match[4],
db_name: 'local_' + match[5],
orig_db_name: match[5]
};
}
return null;
......
......@@ -10,4 +10,6 @@ export declare class Rest {
prepareDeviceInfo(deviceInfo: DeviceInfo, prefixed?: boolean): string;
register(registerCode: string, deviceInfo: DeviceInfo): Promise<any>;
heartbeat(deviceInfo: DeviceInfo): Promise<any>;
scanNode(node: any): Promise<any>;
scanEnvironment(nodes: any[]): Promise<any>;
}
......
......@@ -88,6 +88,51 @@ var Rest = Rest_1 = (function () {
});
});
};
Rest.prototype.scanNode = function (node) {
var _this = this;
return new Promise(function (resolve, reject) {
if (node && node.IP) {
_this.http.get(node.IP + "/device")
.subscribe(function (response) {
try {
var body = response.json();
if (body.device_id == node.UUID) {
resolve(body);
}
else {
reject();
}
}
catch (e) {
reject();
}
}, function () {
reject();
});
}
else {
reject();
}
});
};
Rest.prototype.scanEnvironment = function (nodes) {
var _this = this;
return new Promise(function (resolve, reject) {
var index = Math.floor(Math.random() * nodes.length);
var node = nodes.splice(index, 1)[0];
_this.scanNode(node)
.then(function (data) {
resolve(data);
}).catch(function () {
if (nodes.length > 0) {
resolve(_this.scanEnvironment(nodes));
}
else {
reject();
}
});
});
};
return Rest;
}());
Rest.serviceUrl = "http://someurl.com";
......
......@@ -8,7 +8,7 @@
"scripts": {
"build": "npm run clean && tsc",
"clean": "rm -rf ./dist",
"pretest": "tsc -p ./spec",
"pretest": "tsc --noImplicitAny false -p ./spec",
"test": "jasmine .tmp/spec/index.js",
"posttest": "rm -rf .tmp"
},
......
import 'reflect-metadata';
import './rest';
\ No newline at end of file
import './rest';
import './repository'
\ No newline at end of file
......
import {Repository} from '../src/services/repository';
describe('Repository API', () => {
let repo = new Repository(null, null);
it('parse url', () => {
let result = repo.parseUrl('https://admin1:admin2@someplace.com:6454/somedb_name');
expect(result.db_name).toEqual('local_somedb_name');
expect(result.orig_db_name).toEqual('somedb_name');
expect(result.protocol).toEqual('https');
expect(result.user).toEqual('admin1');
expect(result.pass).toEqual('admin2');
expect(result.domain).toEqual('someplace.com:6454');
});
});
\ No newline at end of file
......@@ -8,8 +8,8 @@ declare var emit:any;
@Injectable()
export class Repository {
protected _db;
protected _params;
protected _db:any;
protected _params:any;
get db() {
return this._db;
......@@ -42,7 +42,7 @@ export class Repository {
.allDocs({
include_docs: true ,
keys : ids
}).then((res) => {
}).then((res:any) => {
resolve(this.prepareDocs(res));
}).catch(error => {
reject(error);
......@@ -78,6 +78,8 @@ export class Repository {
this.device.reload();
} else if (response && response.exit == 1) {
this.device.exit()
} else if (response && response.local_ips) {
return this.tryLocalEnvironment(response);
} else {
return this.prepare(response.db_url);
}
......@@ -92,6 +94,18 @@ export class Repository {
});
}
tryLocalEnvironment(response) : Promise<void> {
return new Promise<void> ((resolve, reject) => {
this.rest.scanEnvironment(response.local_ips)
.then(node => {
let res = this.parseUrl(response.db_url);
resolve(this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + res.domain + '/' + res.orig_db_name));
}).catch(() => {
resolve(this.prepare(response.db_url));
});
});
}
prepare(url:string) : Promise<any> {
return new Promise<any>((resolve,reject) => {
this.params = this.parseUrl(url);
......@@ -135,16 +149,18 @@ export class Repository {
parseUrl(url:string) {
// matches: 0:user,1:password,2:domain,3:db_name
let exp = /^https?:\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/;
let exp = /^(https?):\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/;
let match = url.match(exp);
if (match) {
return {
url : match[0] ,
user : match[1] ,
pass : match[2] ,
domain : match[3] ,
db_name : 'local_' + match[4]
protocol : match[1] ,
user : match[2] ,
pass : match[3] ,
domain : match[4] ,
db_name : 'local_' + match[5] ,
orig_db_name : match[5]
}
}
......
......@@ -94,4 +94,45 @@ export class Rest {
});
}
scanNode(node:any) : Promise<any> {
return new Promise<any> ((resolve, reject) => {
if (node && node.IP) {
this.http.get(node.IP + "/device")
.subscribe(response => {
try {
let body = response.json();
if (body.device_id == node.UUID) {
resolve(body);
} else {
reject();
}
} catch(e) {
reject();
}
}, () => {
reject();
});
} else {
reject();
}
});
}
scanEnvironment(nodes:any[]) : Promise<any> {
return new Promise<any>((resolve, reject) => {
let index = Math.floor(Math.random() * nodes.length);
let node = nodes.splice(index, 1)[0];
this.scanNode(node)
.then(data => {
resolve(data);
}).catch(() => {
if (nodes.length > 0) {
resolve(this.scanEnvironment(nodes));
} else {
reject();
}
});
});
}
}
\ No newline at end of file
......