parseUrl.js
3.17 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict";
var isObject = require("is-object");
var isString = require("is-string");
var urllib = require("url");
var inputType = require("./inputType");
var InputTypes = require("./InputTypes");
var isDefaultPort = require("./isDefaultPort");
var parsePath = require("./parsePath");
var typeofUrl = require("./typeofUrl");
var UrlType = require("./UrlType");
var _directoryIndexes = ["index.html"];
function isDirectoryIndex(filename, directoryIndexes)
{
// TODO :: not necessarily -- what if it's a query- or more relative url?
if (filename === null) return true;
if (Array.isArray(directoryIndexes) === false)
{
directoryIndexes = _directoryIndexes;
}
return directoryIndexes.indexOf(filename) > -1;
}
function parseOptions(parseQueryString, slashesDenoteHost)
{
var options = {};
// parseUrl(url, options)
if (isObject(parseQueryString) === true)
{
options = parseQueryString;
}
// parseUrl(url, parseQueryString, slashesDenoteHost)
else
{
options =
{
parseQueryString: parseQueryString,
slashesDenoteHost: slashesDenoteHost
};
}
return options;
}
function parseUrl(url, parseQueryString, slashesDenoteHost)
{
var parsedPath;
var options = parseOptions(parseQueryString, slashesDenoteHost);
var type = inputType(url);
// TODO :: support url objects not from core? (see urllib.format)
switch (type)
{
case InputTypes.STRING:
{
url = urllib.parse(url, true, options.slashesDenoteHost);
break;
}
case InputTypes.CORE_URL_OBJECT:
{
// If was parsed with parseQueryString=false
if (
( url.search==null && url.query==null ) ||
( isString(url.search)===true && isString(url.query)===true )
)
{
// Reparse
// TODO :: use "querystring" package (that node uses) ?
url = urllib.parse(url.href, true, options.slashesDenoteHost);
}
break;
}
case InputTypes.URLOBJ:
{
return url;
}
default:
{
throw new Error("invalid input type");
}
}
parsedPath = parsePath(url.pathname);
url.extra = {};
if (isString(url.protocol) === true)
{
if (url.protocol[url.protocol.length-1] === ":")
{
url.extra.protocolTruncated = url.protocol.substr(0, url.protocol.length-1);
}
}
else
{
url.extra.protocolTruncated = null;
}
url.extra.portIsDefault = isDefaultPort(url.extra.protocolTruncated, url.port, options.defaultPorts);
url.extra.directory = parsedPath.dir;
url.extra.directoryLeadingSlash = parsedPath.leadingSlash;
url.extra.filename = parsedPath.filename;
url.extra.filenameIsIndex = isDirectoryIndex(parsedPath.filename, options.directoryIndexes);
url.extra.query = url.query;
// Preserve backwards compatibility with core url package
if (options.parseQueryString !== true)
{
// `search` is always a string since we parsed query
if (url.search !== "")
{
if (url.search[0] === "?")
{
// Remove prefixed "?"
url.query = url.search.substr(1);
}
else
{
// TODO :: add test for this
url.query = url.search;
}
}
else
{
url.search = null;
url.query = null;
}
}
url.extra.type = typeofUrl(url);
return url;
}
module.exports = parseUrl;