Showing
9 changed files
with
157 additions
and
14 deletions
| ... | @@ -13,14 +13,17 @@ export declare class Repository { | ... | @@ -13,14 +13,17 @@ export declare class Repository { |
| 13 | findByIds(ids: Array<string>): Promise<Array<any>>; | 13 | findByIds(ids: Array<string>): Promise<Array<any>>; |
| 14 | findByType(type: string): Promise<Array<any>>; | 14 | findByType(type: string): Promise<Array<any>>; |
| 15 | replicate(deviceInfo: DeviceInfo): Promise<boolean>; | 15 | replicate(deviceInfo: DeviceInfo): Promise<boolean>; |
| 16 | + tryLocalEnvironment(response: any): Promise<void>; | ||
| 16 | prepare(url: string): Promise<any>; | 17 | prepare(url: string): Promise<any>; |
| 17 | init(db_name: string): Promise<any>; | 18 | init(db_name: string): Promise<any>; |
| 18 | parseUrl(url: string): { | 19 | parseUrl(url: string): { |
| 19 | url: string; | 20 | url: string; |
| 21 | + protocol: string; | ||
| 20 | user: string; | 22 | user: string; |
| 21 | pass: string; | 23 | pass: string; |
| 22 | domain: string; | 24 | domain: string; |
| 23 | db_name: string; | 25 | db_name: string; |
| 26 | + orig_db_name: string; | ||
| 24 | }; | 27 | }; |
| 25 | prepareDocs(res: any): Array<any>; | 28 | prepareDocs(res: any): Array<any>; |
| 26 | } | 29 | } | ... | ... |
| ... | @@ -82,6 +82,9 @@ var Repository = (function () { | ... | @@ -82,6 +82,9 @@ var Repository = (function () { |
| 82 | else if (response && response.exit == 1) { | 82 | else if (response && response.exit == 1) { |
| 83 | _this.device.exit(); | 83 | _this.device.exit(); |
| 84 | } | 84 | } |
| 85 | + else if (response && response.local_ips) { | ||
| 86 | + return _this.tryLocalEnvironment(response); | ||
| 87 | + } | ||
| 85 | else { | 88 | else { |
| 86 | return _this.prepare(response.db_url); | 89 | return _this.prepare(response.db_url); |
| 87 | } | 90 | } |
| ... | @@ -95,6 +98,18 @@ var Repository = (function () { | ... | @@ -95,6 +98,18 @@ var Repository = (function () { |
| 95 | .catch(function (error) { reject(error); }); | 98 | .catch(function (error) { reject(error); }); |
| 96 | }); | 99 | }); |
| 97 | }; | 100 | }; |
| 101 | + Repository.prototype.tryLocalEnvironment = function (response) { | ||
| 102 | + var _this = this; | ||
| 103 | + return new Promise(function (resolve, reject) { | ||
| 104 | + _this.rest.scanEnvironment(response.local_ips) | ||
| 105 | + .then(function (node) { | ||
| 106 | + var res = _this.parseUrl(response.db_url); | ||
| 107 | + resolve(_this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + res.domain + '/' + res.orig_db_name)); | ||
| 108 | + }).catch(function () { | ||
| 109 | + resolve(_this.prepare(response.db_url)); | ||
| 110 | + }); | ||
| 111 | + }); | ||
| 112 | + }; | ||
| 98 | Repository.prototype.prepare = function (url) { | 113 | Repository.prototype.prepare = function (url) { |
| 99 | var _this = this; | 114 | var _this = this; |
| 100 | return new Promise(function (resolve, reject) { | 115 | return new Promise(function (resolve, reject) { |
| ... | @@ -138,15 +153,17 @@ var Repository = (function () { | ... | @@ -138,15 +153,17 @@ var Repository = (function () { |
| 138 | }; | 153 | }; |
| 139 | Repository.prototype.parseUrl = function (url) { | 154 | Repository.prototype.parseUrl = function (url) { |
| 140 | // matches: 0:user,1:password,2:domain,3:db_name | 155 | // matches: 0:user,1:password,2:domain,3:db_name |
| 141 | - var exp = /^https?:\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/; | 156 | + var exp = /^(https?):\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/; |
| 142 | var match = url.match(exp); | 157 | var match = url.match(exp); |
| 143 | if (match) { | 158 | if (match) { |
| 144 | return { | 159 | return { |
| 145 | url: match[0], | 160 | url: match[0], |
| 146 | - user: match[1], | 161 | + protocol: match[1], |
| 147 | - pass: match[2], | 162 | + user: match[2], |
| 148 | - domain: match[3], | 163 | + pass: match[3], |
| 149 | - db_name: 'local_' + match[4] | 164 | + domain: match[4], |
| 165 | + db_name: 'local_' + match[5], | ||
| 166 | + orig_db_name: match[5] | ||
| 150 | }; | 167 | }; |
| 151 | } | 168 | } |
| 152 | return null; | 169 | return null; | ... | ... |
| ... | @@ -10,4 +10,6 @@ export declare class Rest { | ... | @@ -10,4 +10,6 @@ export declare class Rest { |
| 10 | prepareDeviceInfo(deviceInfo: DeviceInfo, prefixed?: boolean): string; | 10 | prepareDeviceInfo(deviceInfo: DeviceInfo, prefixed?: boolean): string; |
| 11 | register(registerCode: string, deviceInfo: DeviceInfo): Promise<any>; | 11 | register(registerCode: string, deviceInfo: DeviceInfo): Promise<any>; |
| 12 | heartbeat(deviceInfo: DeviceInfo): Promise<any>; | 12 | heartbeat(deviceInfo: DeviceInfo): Promise<any>; |
| 13 | + scanNode(node: any): Promise<any>; | ||
| 14 | + scanEnvironment(nodes: any[]): Promise<any>; | ||
| 13 | } | 15 | } | ... | ... |
| ... | @@ -88,6 +88,51 @@ var Rest = Rest_1 = (function () { | ... | @@ -88,6 +88,51 @@ var Rest = Rest_1 = (function () { |
| 88 | }); | 88 | }); |
| 89 | }); | 89 | }); |
| 90 | }; | 90 | }; |
| 91 | + Rest.prototype.scanNode = function (node) { | ||
| 92 | + var _this = this; | ||
| 93 | + return new Promise(function (resolve, reject) { | ||
| 94 | + if (node && node.IP) { | ||
| 95 | + _this.http.get(node.IP + "/device") | ||
| 96 | + .subscribe(function (response) { | ||
| 97 | + try { | ||
| 98 | + var body = response.json(); | ||
| 99 | + if (body.device_id == node.UUID) { | ||
| 100 | + resolve(body); | ||
| 101 | + } | ||
| 102 | + else { | ||
| 103 | + reject(); | ||
| 104 | + } | ||
| 105 | + } | ||
| 106 | + catch (e) { | ||
| 107 | + reject(); | ||
| 108 | + } | ||
| 109 | + }, function () { | ||
| 110 | + reject(); | ||
| 111 | + }); | ||
| 112 | + } | ||
| 113 | + else { | ||
| 114 | + reject(); | ||
| 115 | + } | ||
| 116 | + }); | ||
| 117 | + }; | ||
| 118 | + Rest.prototype.scanEnvironment = function (nodes) { | ||
| 119 | + var _this = this; | ||
| 120 | + return new Promise(function (resolve, reject) { | ||
| 121 | + var index = Math.floor(Math.random() * nodes.length); | ||
| 122 | + var node = nodes.splice(index, 1)[0]; | ||
| 123 | + _this.scanNode(node) | ||
| 124 | + .then(function (data) { | ||
| 125 | + resolve(data); | ||
| 126 | + }).catch(function () { | ||
| 127 | + if (nodes.length > 0) { | ||
| 128 | + resolve(_this.scanEnvironment(nodes)); | ||
| 129 | + } | ||
| 130 | + else { | ||
| 131 | + reject(); | ||
| 132 | + } | ||
| 133 | + }); | ||
| 134 | + }); | ||
| 135 | + }; | ||
| 91 | return Rest; | 136 | return Rest; |
| 92 | }()); | 137 | }()); |
| 93 | Rest.serviceUrl = "http://someurl.com"; | 138 | Rest.serviceUrl = "http://someurl.com"; | ... | ... |
| ... | @@ -8,7 +8,7 @@ | ... | @@ -8,7 +8,7 @@ |
| 8 | "scripts": { | 8 | "scripts": { |
| 9 | "build": "npm run clean && tsc", | 9 | "build": "npm run clean && tsc", |
| 10 | "clean": "rm -rf ./dist", | 10 | "clean": "rm -rf ./dist", |
| 11 | - "pretest": "tsc -p ./spec", | 11 | + "pretest": "tsc --noImplicitAny false -p ./spec", |
| 12 | "test": "jasmine .tmp/spec/index.js", | 12 | "test": "jasmine .tmp/spec/index.js", |
| 13 | "posttest": "rm -rf .tmp" | 13 | "posttest": "rm -rf .tmp" |
| 14 | }, | 14 | }, | ... | ... |
spec/repository.ts
0 → 100644
| 1 | +import {Repository} from '../src/services/repository'; | ||
| 2 | + | ||
| 3 | +describe('Repository API', () => { | ||
| 4 | + | ||
| 5 | + let repo = new Repository(null, null); | ||
| 6 | + | ||
| 7 | + it('parse url', () => { | ||
| 8 | + let result = repo.parseUrl('https://admin1:admin2@someplace.com:6454/somedb_name'); | ||
| 9 | + | ||
| 10 | + expect(result.db_name).toEqual('local_somedb_name'); | ||
| 11 | + expect(result.orig_db_name).toEqual('somedb_name'); | ||
| 12 | + expect(result.protocol).toEqual('https'); | ||
| 13 | + expect(result.user).toEqual('admin1'); | ||
| 14 | + expect(result.pass).toEqual('admin2'); | ||
| 15 | + expect(result.domain).toEqual('someplace.com:6454'); | ||
| 16 | + }); | ||
| 17 | + | ||
| 18 | +}); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -8,8 +8,8 @@ declare var emit:any; | ... | @@ -8,8 +8,8 @@ declare var emit:any; |
| 8 | @Injectable() | 8 | @Injectable() |
| 9 | export class Repository { | 9 | export class Repository { |
| 10 | 10 | ||
| 11 | - protected _db; | 11 | + protected _db:any; |
| 12 | - protected _params; | 12 | + protected _params:any; |
| 13 | 13 | ||
| 14 | get db() { | 14 | get db() { |
| 15 | return this._db; | 15 | return this._db; |
| ... | @@ -42,7 +42,7 @@ export class Repository { | ... | @@ -42,7 +42,7 @@ export class Repository { |
| 42 | .allDocs({ | 42 | .allDocs({ |
| 43 | include_docs: true , | 43 | include_docs: true , |
| 44 | keys : ids | 44 | keys : ids |
| 45 | - }).then((res) => { | 45 | + }).then((res:any) => { |
| 46 | resolve(this.prepareDocs(res)); | 46 | resolve(this.prepareDocs(res)); |
| 47 | }).catch(error => { | 47 | }).catch(error => { |
| 48 | reject(error); | 48 | reject(error); |
| ... | @@ -78,6 +78,8 @@ export class Repository { | ... | @@ -78,6 +78,8 @@ export class Repository { |
| 78 | this.device.reload(); | 78 | this.device.reload(); |
| 79 | } else if (response && response.exit == 1) { | 79 | } else if (response && response.exit == 1) { |
| 80 | this.device.exit() | 80 | this.device.exit() |
| 81 | + } else if (response && response.local_ips) { | ||
| 82 | + return this.tryLocalEnvironment(response); | ||
| 81 | } else { | 83 | } else { |
| 82 | return this.prepare(response.db_url); | 84 | return this.prepare(response.db_url); |
| 83 | } | 85 | } |
| ... | @@ -92,6 +94,18 @@ export class Repository { | ... | @@ -92,6 +94,18 @@ export class Repository { |
| 92 | }); | 94 | }); |
| 93 | } | 95 | } |
| 94 | 96 | ||
| 97 | + tryLocalEnvironment(response) : Promise<void> { | ||
| 98 | + return new Promise<void> ((resolve, reject) => { | ||
| 99 | + this.rest.scanEnvironment(response.local_ips) | ||
| 100 | + .then(node => { | ||
| 101 | + let res = this.parseUrl(response.db_url); | ||
| 102 | + resolve(this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + res.domain + '/' + res.orig_db_name)); | ||
| 103 | + }).catch(() => { | ||
| 104 | + resolve(this.prepare(response.db_url)); | ||
| 105 | + }); | ||
| 106 | + }); | ||
| 107 | + } | ||
| 108 | + | ||
| 95 | prepare(url:string) : Promise<any> { | 109 | prepare(url:string) : Promise<any> { |
| 96 | return new Promise<any>((resolve,reject) => { | 110 | return new Promise<any>((resolve,reject) => { |
| 97 | this.params = this.parseUrl(url); | 111 | this.params = this.parseUrl(url); |
| ... | @@ -135,16 +149,18 @@ export class Repository { | ... | @@ -135,16 +149,18 @@ export class Repository { |
| 135 | 149 | ||
| 136 | parseUrl(url:string) { | 150 | parseUrl(url:string) { |
| 137 | // matches: 0:user,1:password,2:domain,3:db_name | 151 | // matches: 0:user,1:password,2:domain,3:db_name |
| 138 | - let exp = /^https?:\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/; | 152 | + let exp = /^(https?):\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/; |
| 139 | let match = url.match(exp); | 153 | let match = url.match(exp); |
| 140 | 154 | ||
| 141 | if (match) { | 155 | if (match) { |
| 142 | return { | 156 | return { |
| 143 | url : match[0] , | 157 | url : match[0] , |
| 144 | - user : match[1] , | 158 | + protocol : match[1] , |
| 145 | - pass : match[2] , | 159 | + user : match[2] , |
| 146 | - domain : match[3] , | 160 | + pass : match[3] , |
| 147 | - db_name : 'local_' + match[4] | 161 | + domain : match[4] , |
| 162 | + db_name : 'local_' + match[5] , | ||
| 163 | + orig_db_name : match[5] | ||
| 148 | } | 164 | } |
| 149 | } | 165 | } |
| 150 | 166 | ... | ... |
| ... | @@ -94,4 +94,45 @@ export class Rest { | ... | @@ -94,4 +94,45 @@ export class Rest { |
| 94 | }); | 94 | }); |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | + scanNode(node:any) : Promise<any> { | ||
| 98 | + return new Promise<any> ((resolve, reject) => { | ||
| 99 | + if (node && node.IP) { | ||
| 100 | + this.http.get(node.IP + "/device") | ||
| 101 | + .subscribe(response => { | ||
| 102 | + try { | ||
| 103 | + let body = response.json(); | ||
| 104 | + if (body.device_id == node.UUID) { | ||
| 105 | + resolve(body); | ||
| 106 | + } else { | ||
| 107 | + reject(); | ||
| 108 | + } | ||
| 109 | + } catch(e) { | ||
| 110 | + reject(); | ||
| 111 | + } | ||
| 112 | + }, () => { | ||
| 113 | + reject(); | ||
| 114 | + }); | ||
| 115 | + } else { | ||
| 116 | + reject(); | ||
| 117 | + } | ||
| 118 | + }); | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + scanEnvironment(nodes:any[]) : Promise<any> { | ||
| 122 | + return new Promise<any>((resolve, reject) => { | ||
| 123 | + let index = Math.floor(Math.random() * nodes.length); | ||
| 124 | + let node = nodes.splice(index, 1)[0]; | ||
| 125 | + this.scanNode(node) | ||
| 126 | + .then(data => { | ||
| 127 | + resolve(data); | ||
| 128 | + }).catch(() => { | ||
| 129 | + if (nodes.length > 0) { | ||
| 130 | + resolve(this.scanEnvironment(nodes)); | ||
| 131 | + } else { | ||
| 132 | + reject(); | ||
| 133 | + } | ||
| 134 | + }); | ||
| 135 | + }); | ||
| 136 | + } | ||
| 137 | + | ||
| 97 | } | 138 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment