index-bak.js
2.98 KB
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
"use strict";
var pathlib = require("path");
var urllib = require("url");
var _findUrlRelation = require("./findUrlRelation");
var _joinPath = require("./joinPath");
var _joinQuery = require("./joinQuery");
var _normalizePath = require("./normalizePath");
var _parsePath = require("./parsePath");
var _parseUrl = require("./parseUrl");
function areInternal(url1, url2/*, defaultPorts*/)
{
// If impossible to determine
if (url1.protocol===null && url2.protocol===null) return null;
if (url1.host===null && url2.host===null) return null;
return url1.protocol===url2.protocol &&
url1.auth===url2.auth &&
url1.host===url2.host &&
url1.port===url2.port;
}
function areSamePage(url1, url2/*, defaultPorts*/, internalOverride)
{
var isInternal = internalOverride===undefined ? areInternal(url1,url2) : internalOverride;
// If internal could not be determined, than neither can same page
if (isInternal === null)
{
return null;
}
return isInternal===true &&
url1.path===url2.path &&
url1.search===url2.search;
}
function format(urlObj/*, defaultPorts*/)
{
return urllib.format(urlObj);
}
function normalize(url)
{
}
function resolve(from, to/*, defaultPorts, directoryIndexes*/)
{
// TODO :: detect if `parseQueryString` was used in any non-strings
if (typeof from==="string" || from instanceof String) from = parse(from);
if (typeof to==="string" || to instanceof String) to = parse(to);
if (from.protocol!==to.protocol && from.protocol!==null && to.protocol!==null) return to;
if (from.auth!==to.auth && from.auth!==null && to.auth!==null) return to;
if (from.host!==to.host && from.host!==null && to.host!==null) return to;
var resolved =
{
protocol: to.protocol || from.protocol,
slashes: to.slashes || from.slashes,
auth: to.auth || from.auth,
host: to.host || from.host,
port: to.port || from.port,
hostname: to.hostname || from.hostname,
hash: null,
search: null,
query: null,
pathname: null,
path: null,
href: null/*,
extra:
{
}*/
};
// TODO :: can't use "/" because it cancels out relative urls
resolved.pathname = pathlib.resolve(from.pathname || "/", to.pathname || "") || null;
// TODO :: Normalize before resolve (and use as arg) -- if it's faster
var from_pathname_normalized = pathlib.normalize(from.pathname || "") || null;
if (resolved.pathname === from_pathname_normalized)
{
resolved.query = to.query || from.query;
resolved.search = to.search || from.search;
}
else
{
resolved.query = to.query;
resolved.search = to.search;
}
resolved.hash = to.hash;
resolved.path = (resolved.pathname || "") + (resolved.search || "") + (resolved.hash || "");
resolved.path = resolved.path || null;
resolved.href = format(resolved/*, defaultPorts*/);
return resolved;
}
module.exports =
{
areInternal: areInternal,
areSamePage: areSamePage,
format: format,
parse: _parseUrl,
resolve: resolve
};