-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
121 lines (109 loc) · 3.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var agents = {};
var Db = require('./src/Db'),
Storage = require('./src/Storage'),
Http = require('./src/Http'),
Cache = require('./impl/cache').Cache;
exports = module.exports = function (obj) {
obj = parseOption(obj);
var protocol = obj.protocol;
if (protocol[protocol.length - 1] !== ':') {
obj.protocol = protocol + ':';
} else {
protocol = protocol.substr(0, protocol.length - 1);
}
var hash = obj.protocol + '://' + obj.username + ':' + obj.password + '@' + obj.host + ':' + obj.port + '/' + obj.resource;
if (agents.hasOwnProperty(hash)) {
return agents[hash];
}
var arr = obj.clusters;
if (arr && !arr.length) {
arr = null;
delete obj.clusters;
}
if (arr) {
if (typeof arr === 'string') {
arr = obj.clusters = arr.split('|');
}
arr.forEach(function (item, i, arr) {
if (typeof item === 'string') {
arr[i] = parseOption(protocol + '://' + item);
}
});
}
var module;
do {
try {
module = require('./impl/' + protocol);
break;
} catch (e) {
}
try {
module = require('rapid-' + protocol);
break;
} catch (e) {
}
try {
module = require(protocol);
break;
} catch (e) {
}
throw new Error('Protocol ' + protocol + ' unrecognized, is module installed?');
} while (0);
var ret = module.instance(obj);
if (ret.impl.db) {
Db.call(ret, obj);
}
if (ret.impl.storage) {
Storage.call(ret, obj);
}
if (ret.impl.http) {
Http.call(ret, obj);
}
if (obj.cache) {
var cache = obj.cache;
if (!cache.impl) {
cache = exports(cache);
}
Cache.wrap(ret, cache);
}
return ret;
};
exports.Db = Db;
exports.Storage = Storage;
exports.Http = Http;
exports.Cache = Cache;
if (global.rapid && rapid.resource) {
rapid.config.watch('access', function (obj) {
for (var arr = Object.keys(obj), L = arr.length, i = 0; i < L; i++) {
var key = arr[i];
rapid.resource.define(key, exports(obj[key]));
}
});
}
var URL = require('url');
function parseOption(obj) {
if (typeof obj === 'object') {
return obj;
}
var url = URL.parse(obj, true), ret = url.query, tmp;
ret.protocol = url.protocol.substr(0, url.protocol.length - 1);
if (tmp = url.hostname) {
ret.host = tmp;
}
if (tmp = url.port) {
ret.port = tmp;
}
if (tmp = url.auth) {
var idx = tmp.indexOf(':');
if (idx + 1) {
ret.user = tmp.substr(0, idx);
ret.password = tmp.substr(idx + 1);
} else {
ret.user = tmp;
}
}
if (tmp = url.pathname && url.pathname.substr(1)) {
ret.resource = tmp;
}
return ret;
}