-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl-mgr.js
141 lines (131 loc) · 3.16 KB
/
url-mgr.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const matcher = /^(([a-zA-Z-]+):\/*?)([a-zA-Z.@-]+)(:([0-9]{2,}))?(\/?[a-zA-Z0-9._~:/?#[\]%@!$&'()*+,;=-]*?)??(\?([a-zA-Z0-9._~:/?#[\]%@!$&'()*+,;=-]*))?$/;
exports.Url = function(url){
let protocol, domain, port, path, query;
const queryObject = {};
const defPorts = {
'http': '80',
'https': '443'
};
const queryProxy = new Proxy(queryObject, {
get: (target, key) => {
return target[key];
},
set: (target, key, value) => {
if(value){
target[key] = value.toString();
}else{
delete target[key];
}
},
deleteProperty: (target, key) => {
delete target[key];
},
enumerate: (target) => {
return Object.assign({}, target);
},
ownKeys: (target) => {
return Reflect.ownKeys(target);
},
has: (target, key) => {
return key in target;
},
getOwnPropertyDescriptor: (target, key) => {
let value = target[key];
return value ? {
value: value,
writable: true,
enumerable: true,
configurable: true,
} : undefined;
},
});
const parseQuery = (queryString) => {
for(let key in queryObject){
delete queryObject[key];
}
if(typeof queryString !== 'undefined'){
queryString.split('&').forEach(function(x){
if(!x || x === ''){
return;
}
let q = x.match(/(.+?)=(.*)/); //Need to use this instead of .split() in case the value contains an "="
queryObject[q[1]] = q[2];
});
}
return queryObject;
};
const stringifyQuery = (query) => {
let a = [];
for(let q in query){
a.push(q + '=' + (query[q] || ''));
}
return a.join('&');
};
const parse = function(url){
let matches = url.match(matcher);
protocol = matches[2];
domain = matches[3];
port = typeof matches[5] === 'undefined' ? defPorts[protocol] : matches[5];
path = typeof matches[6] === 'undefined' ? '/' : matches[6];
parseQuery(matches[8]);
};
Object.defineProperties(this, {
'protocol': {
get: function(){return protocol;},
set: function(value){protocol = value;},
enumerable: true
},
'domain': {
get: function(){return domain;},
set: function(value){domain = value;},
enumerable: true
},
'port': {
get: function(){return port;},
set: function(value){port = value;},
enumerable: true
},
'path': {
get: function(){return path;},
set: function(value){path = value;},
enumerable: true
},
'query': {
get: function(){return queryProxy;}
},
'queryString': {
get: function(){return stringifyQuery(queryObject);},
set: function(value){parseQuery(value);},
enumerable: true
},
'url': {
get: function(){
let queryString = stringifyQuery(queryObject);
return protocol + '://' + domain + (port === defPorts[protocol] ? '' : ':' + port) + path + (queryString.length > 0 ? '?' + queryString : '');
},
set: function(value){
parse(value);
},
enumerable: true
},
'inspect': {
value: function(depth, opts){
return {
protocol: protocol,
domain: domain,
port: port,
path: path,
query: query,
queryString: stringifyQuery(queryObject),
url: this.url
};
},
writable: false
},
'toString': {
value: function(){return this.url;},
writable: false
}
});
parse(url);
};