@mehmetb/rollup-plugin-node-builtins 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +101 -0
  2. package/dist/constants.js +474 -0
  3. package/dist/rollup-plugin-node-builtins.cjs.js +77 -0
  4. package/dist/rollup-plugin-node-builtins.es6.js +75 -0
  5. package/package.json +42 -0
  6. package/src/es6/assert.js +488 -0
  7. package/src/es6/console.js +13 -0
  8. package/src/es6/domain.js +100 -0
  9. package/src/es6/empty.js +1 -0
  10. package/src/es6/events.js +475 -0
  11. package/src/es6/http-lib/capability.js +52 -0
  12. package/src/es6/http-lib/request.js +278 -0
  13. package/src/es6/http-lib/response.js +185 -0
  14. package/src/es6/http-lib/to-arraybuffer.js +30 -0
  15. package/src/es6/http.js +167 -0
  16. package/src/es6/inherits.js +25 -0
  17. package/src/es6/os.js +113 -0
  18. package/src/es6/path.js +234 -0
  19. package/src/es6/punycode.js +475 -0
  20. package/src/es6/qs.js +147 -0
  21. package/src/es6/readable-stream/buffer-list.js +59 -0
  22. package/src/es6/readable-stream/duplex.js +45 -0
  23. package/src/es6/readable-stream/passthrough.js +15 -0
  24. package/src/es6/readable-stream/readable.js +896 -0
  25. package/src/es6/readable-stream/transform.js +174 -0
  26. package/src/es6/readable-stream/writable.js +483 -0
  27. package/src/es6/setimmediate.js +185 -0
  28. package/src/es6/stream.js +110 -0
  29. package/src/es6/string-decoder.js +220 -0
  30. package/src/es6/timers.js +76 -0
  31. package/src/es6/tty.js +20 -0
  32. package/src/es6/url.js +745 -0
  33. package/src/es6/util.js +598 -0
  34. package/src/es6/vm.js +202 -0
  35. package/src/es6/zlib-lib/LICENSE +21 -0
  36. package/src/es6/zlib-lib/adler32.js +31 -0
  37. package/src/es6/zlib-lib/binding.js +269 -0
  38. package/src/es6/zlib-lib/crc32.js +40 -0
  39. package/src/es6/zlib-lib/deflate.js +1862 -0
  40. package/src/es6/zlib-lib/inffast.js +325 -0
  41. package/src/es6/zlib-lib/inflate.js +1650 -0
  42. package/src/es6/zlib-lib/inftrees.js +329 -0
  43. package/src/es6/zlib-lib/messages.js +11 -0
  44. package/src/es6/zlib-lib/trees.js +1220 -0
  45. package/src/es6/zlib-lib/utils.js +73 -0
  46. package/src/es6/zlib-lib/zstream.js +28 -0
  47. package/src/es6/zlib.js +635 -0
  48. package/src/index.js +73 -0
package/src/es6/url.js ADDED
@@ -0,0 +1,745 @@
1
+ // Copyright Joyent, Inc. and other Node contributors.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a
4
+ // copy of this software and associated documentation files (the
5
+ // "Software"), to deal in the Software without restriction, including
6
+ // without limitation the rights to use, copy, modify, merge, publish,
7
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ // persons to whom the Software is furnished to do so, subject to the
9
+ // following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be included
12
+ // in all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+
23
+ import {toASCII} from 'punycode';
24
+ import {isObject,isString,isNullOrUndefined,isNull} from 'util';
25
+ import {parse as qsParse,stringify as qsStringify} from 'querystring';
26
+ export {
27
+ urlParse as parse,
28
+ urlResolve as resolve,
29
+ urlResolveObject as resolveObject,
30
+ urlFormat as format
31
+ };
32
+ export default {
33
+ parse: urlParse,
34
+ resolve: urlResolve,
35
+ resolveObject: urlResolveObject,
36
+ format: urlFormat,
37
+ Url: Url
38
+ }
39
+ export function Url() {
40
+ this.protocol = null;
41
+ this.slashes = null;
42
+ this.auth = null;
43
+ this.host = null;
44
+ this.port = null;
45
+ this.hostname = null;
46
+ this.hash = null;
47
+ this.search = null;
48
+ this.query = null;
49
+ this.pathname = null;
50
+ this.path = null;
51
+ this.href = null;
52
+ }
53
+
54
+ // Reference: RFC 3986, RFC 1808, RFC 2396
55
+
56
+ // define these here so at least they only have to be
57
+ // compiled once on the first module load.
58
+ var protocolPattern = /^([a-z0-9.+-]+:)/i,
59
+ portPattern = /:[0-9]*$/,
60
+
61
+ // Special case for a simple path URL
62
+ simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/,
63
+
64
+ // RFC 2396: characters reserved for delimiting URLs.
65
+ // We actually just auto-escape these.
66
+ delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'],
67
+
68
+ // RFC 2396: characters not allowed for various reasons.
69
+ unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims),
70
+
71
+ // Allowed by RFCs, but cause of XSS attacks. Always escape these.
72
+ autoEscape = ['\''].concat(unwise),
73
+ // Characters that are never ever allowed in a hostname.
74
+ // Note that any invalid chars are also handled, but these
75
+ // are the ones that are *expected* to be seen, so we fast-path
76
+ // them.
77
+ nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape),
78
+ hostEndingChars = ['/', '?', '#'],
79
+ hostnameMaxLen = 255,
80
+ hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/,
81
+ hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/,
82
+ // protocols that can allow "unsafe" and "unwise" chars.
83
+ unsafeProtocol = {
84
+ 'javascript': true,
85
+ 'javascript:': true
86
+ },
87
+ // protocols that never have a hostname.
88
+ hostlessProtocol = {
89
+ 'javascript': true,
90
+ 'javascript:': true
91
+ },
92
+ // protocols that always contain a // bit.
93
+ slashedProtocol = {
94
+ 'http': true,
95
+ 'https': true,
96
+ 'ftp': true,
97
+ 'gopher': true,
98
+ 'file': true,
99
+ 'http:': true,
100
+ 'https:': true,
101
+ 'ftp:': true,
102
+ 'gopher:': true,
103
+ 'file:': true
104
+ };
105
+
106
+ function urlParse(url, parseQueryString, slashesDenoteHost) {
107
+ if (url && isObject(url) && url instanceof Url) return url;
108
+
109
+ var u = new Url;
110
+ u.parse(url, parseQueryString, slashesDenoteHost);
111
+ return u;
112
+ }
113
+ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
114
+ return parse(this, url, parseQueryString, slashesDenoteHost);
115
+ }
116
+
117
+ function parse(self, url, parseQueryString, slashesDenoteHost) {
118
+ if (!isString(url)) {
119
+ throw new TypeError('Parameter \'url\' must be a string, not ' + typeof url);
120
+ }
121
+
122
+ // Copy chrome, IE, opera backslash-handling behavior.
123
+ // Back slashes before the query string get converted to forward slashes
124
+ // See: https://code.google.com/p/chromium/issues/detail?id=25916
125
+ var queryIndex = url.indexOf('?'),
126
+ splitter =
127
+ (queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#',
128
+ uSplit = url.split(splitter),
129
+ slashRegex = /\\/g;
130
+ uSplit[0] = uSplit[0].replace(slashRegex, '/');
131
+ url = uSplit.join(splitter);
132
+
133
+ var rest = url;
134
+
135
+ // trim before proceeding.
136
+ // This is to support parse stuff like " http://foo.com \n"
137
+ rest = rest.trim();
138
+
139
+ if (!slashesDenoteHost && url.split('#').length === 1) {
140
+ // Try fast path regexp
141
+ var simplePath = simplePathPattern.exec(rest);
142
+ if (simplePath) {
143
+ self.path = rest;
144
+ self.href = rest;
145
+ self.pathname = simplePath[1];
146
+ if (simplePath[2]) {
147
+ self.search = simplePath[2];
148
+ if (parseQueryString) {
149
+ self.query = qsParse(self.search.substr(1));
150
+ } else {
151
+ self.query = self.search.substr(1);
152
+ }
153
+ } else if (parseQueryString) {
154
+ self.search = '';
155
+ self.query = {};
156
+ }
157
+ return self;
158
+ }
159
+ }
160
+
161
+ var proto = protocolPattern.exec(rest);
162
+ if (proto) {
163
+ proto = proto[0];
164
+ var lowerProto = proto.toLowerCase();
165
+ self.protocol = lowerProto;
166
+ rest = rest.substr(proto.length);
167
+ }
168
+
169
+ // figure out if it's got a host
170
+ // user@server is *always* interpreted as a hostname, and url
171
+ // resolution will treat //foo/bar as host=foo,path=bar because that's
172
+ // how the browser resolves relative URLs.
173
+ if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) {
174
+ var slashes = rest.substr(0, 2) === '//';
175
+ if (slashes && !(proto && hostlessProtocol[proto])) {
176
+ rest = rest.substr(2);
177
+ self.slashes = true;
178
+ }
179
+ }
180
+ var i, hec, l, p;
181
+ if (!hostlessProtocol[proto] &&
182
+ (slashes || (proto && !slashedProtocol[proto]))) {
183
+
184
+ // there's a hostname.
185
+ // the first instance of /, ?, ;, or # ends the host.
186
+ //
187
+ // If there is an @ in the hostname, then non-host chars *are* allowed
188
+ // to the left of the last @ sign, unless some host-ending character
189
+ // comes *before* the @-sign.
190
+ // URLs are obnoxious.
191
+ //
192
+ // ex:
193
+ // http://a@b@c/ => user:a@b host:c
194
+ // http://a@b?@c => user:a host:c path:/?@c
195
+
196
+ // v0.12 TODO(isaacs): This is not quite how Chrome does things.
197
+ // Review our test case against browsers more comprehensively.
198
+
199
+ // find the first instance of any hostEndingChars
200
+ var hostEnd = -1;
201
+ for (i = 0; i < hostEndingChars.length; i++) {
202
+ hec = rest.indexOf(hostEndingChars[i]);
203
+ if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
204
+ hostEnd = hec;
205
+ }
206
+
207
+ // at this point, either we have an explicit point where the
208
+ // auth portion cannot go past, or the last @ char is the decider.
209
+ var auth, atSign;
210
+ if (hostEnd === -1) {
211
+ // atSign can be anywhere.
212
+ atSign = rest.lastIndexOf('@');
213
+ } else {
214
+ // atSign must be in auth portion.
215
+ // http://a@b/c@d => host:b auth:a path:/c@d
216
+ atSign = rest.lastIndexOf('@', hostEnd);
217
+ }
218
+
219
+ // Now we have a portion which is definitely the auth.
220
+ // Pull that off.
221
+ if (atSign !== -1) {
222
+ auth = rest.slice(0, atSign);
223
+ rest = rest.slice(atSign + 1);
224
+ self.auth = decodeURIComponent(auth);
225
+ }
226
+
227
+ // the host is the remaining to the left of the first non-host char
228
+ hostEnd = -1;
229
+ for (i = 0; i < nonHostChars.length; i++) {
230
+ hec = rest.indexOf(nonHostChars[i]);
231
+ if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
232
+ hostEnd = hec;
233
+ }
234
+ // if we still have not hit it, then the entire thing is a host.
235
+ if (hostEnd === -1)
236
+ hostEnd = rest.length;
237
+
238
+ self.host = rest.slice(0, hostEnd);
239
+ rest = rest.slice(hostEnd);
240
+
241
+ // pull out port.
242
+ parseHost(self);
243
+
244
+ // we've indicated that there is a hostname,
245
+ // so even if it's empty, it has to be present.
246
+ self.hostname = self.hostname || '';
247
+
248
+ // if hostname begins with [ and ends with ]
249
+ // assume that it's an IPv6 address.
250
+ var ipv6Hostname = self.hostname[0] === '[' &&
251
+ self.hostname[self.hostname.length - 1] === ']';
252
+
253
+ // validate a little.
254
+ if (!ipv6Hostname) {
255
+ var hostparts = self.hostname.split(/\./);
256
+ for (i = 0, l = hostparts.length; i < l; i++) {
257
+ var part = hostparts[i];
258
+ if (!part) continue;
259
+ if (!part.match(hostnamePartPattern)) {
260
+ var newpart = '';
261
+ for (var j = 0, k = part.length; j < k; j++) {
262
+ if (part.charCodeAt(j) > 127) {
263
+ // we replace non-ASCII char with a temporary placeholder
264
+ // we need this to make sure size of hostname is not
265
+ // broken by replacing non-ASCII by nothing
266
+ newpart += 'x';
267
+ } else {
268
+ newpart += part[j];
269
+ }
270
+ }
271
+ // we test again with ASCII char only
272
+ if (!newpart.match(hostnamePartPattern)) {
273
+ var validParts = hostparts.slice(0, i);
274
+ var notHost = hostparts.slice(i + 1);
275
+ var bit = part.match(hostnamePartStart);
276
+ if (bit) {
277
+ validParts.push(bit[1]);
278
+ notHost.unshift(bit[2]);
279
+ }
280
+ if (notHost.length) {
281
+ rest = '/' + notHost.join('.') + rest;
282
+ }
283
+ self.hostname = validParts.join('.');
284
+ break;
285
+ }
286
+ }
287
+ }
288
+ }
289
+
290
+ if (self.hostname.length > hostnameMaxLen) {
291
+ self.hostname = '';
292
+ } else {
293
+ // hostnames are always lower case.
294
+ self.hostname = self.hostname.toLowerCase();
295
+ }
296
+
297
+ if (!ipv6Hostname) {
298
+ // IDNA Support: Returns a punycoded representation of "domain".
299
+ // It only converts parts of the domain name that
300
+ // have non-ASCII characters, i.e. it doesn't matter if
301
+ // you call it with a domain that already is ASCII-only.
302
+ self.hostname = toASCII(self.hostname);
303
+ }
304
+
305
+ p = self.port ? ':' + self.port : '';
306
+ var h = self.hostname || '';
307
+ self.host = h + p;
308
+ self.href += self.host;
309
+
310
+ // strip [ and ] from the hostname
311
+ // the host field still retains them, though
312
+ if (ipv6Hostname) {
313
+ self.hostname = self.hostname.substr(1, self.hostname.length - 2);
314
+ if (rest[0] !== '/') {
315
+ rest = '/' + rest;
316
+ }
317
+ }
318
+ }
319
+
320
+ // now rest is set to the post-host stuff.
321
+ // chop off any delim chars.
322
+ if (!unsafeProtocol[lowerProto]) {
323
+
324
+ // First, make 100% sure that any "autoEscape" chars get
325
+ // escaped, even if encodeURIComponent doesn't think they
326
+ // need to be.
327
+ for (i = 0, l = autoEscape.length; i < l; i++) {
328
+ var ae = autoEscape[i];
329
+ if (rest.indexOf(ae) === -1)
330
+ continue;
331
+ var esc = encodeURIComponent(ae);
332
+ if (esc === ae) {
333
+ esc = escape(ae);
334
+ }
335
+ rest = rest.split(ae).join(esc);
336
+ }
337
+ }
338
+
339
+
340
+ // chop off from the tail first.
341
+ var hash = rest.indexOf('#');
342
+ if (hash !== -1) {
343
+ // got a fragment string.
344
+ self.hash = rest.substr(hash);
345
+ rest = rest.slice(0, hash);
346
+ }
347
+ var qm = rest.indexOf('?');
348
+ if (qm !== -1) {
349
+ self.search = rest.substr(qm);
350
+ self.query = rest.substr(qm + 1);
351
+ if (parseQueryString) {
352
+ self.query = qsParse(self.query);
353
+ }
354
+ rest = rest.slice(0, qm);
355
+ } else if (parseQueryString) {
356
+ // no query string, but parseQueryString still requested
357
+ self.search = '';
358
+ self.query = {};
359
+ }
360
+ if (rest) self.pathname = rest;
361
+ if (slashedProtocol[lowerProto] &&
362
+ self.hostname && !self.pathname) {
363
+ self.pathname = '/';
364
+ }
365
+
366
+ //to support http.request
367
+ if (self.pathname || self.search) {
368
+ p = self.pathname || '';
369
+ var s = self.search || '';
370
+ self.path = p + s;
371
+ }
372
+
373
+ // finally, reconstruct the href based on what has been validated.
374
+ self.href = format(self);
375
+ return self;
376
+ }
377
+
378
+ // format a parsed object into a url string
379
+ function urlFormat(obj) {
380
+ // ensure it's an object, and not a string url.
381
+ // If it's an obj, this is a no-op.
382
+ // this way, you can call url_format() on strings
383
+ // to clean up potentially wonky urls.
384
+ if (isString(obj)) obj = parse({}, obj);
385
+ return format(obj);
386
+ }
387
+
388
+ function format(self) {
389
+ var auth = self.auth || '';
390
+ if (auth) {
391
+ auth = encodeURIComponent(auth);
392
+ auth = auth.replace(/%3A/i, ':');
393
+ auth += '@';
394
+ }
395
+
396
+ var protocol = self.protocol || '',
397
+ pathname = self.pathname || '',
398
+ hash = self.hash || '',
399
+ host = false,
400
+ query = '';
401
+
402
+ if (self.host) {
403
+ host = auth + self.host;
404
+ } else if (self.hostname) {
405
+ host = auth + (self.hostname.indexOf(':') === -1 ?
406
+ self.hostname :
407
+ '[' + this.hostname + ']');
408
+ if (self.port) {
409
+ host += ':' + self.port;
410
+ }
411
+ }
412
+
413
+ if (self.query &&
414
+ isObject(self.query) &&
415
+ Object.keys(self.query).length) {
416
+ query = qsStringify(self.query);
417
+ }
418
+
419
+ var search = self.search || (query && ('?' + query)) || '';
420
+
421
+ if (protocol && protocol.substr(-1) !== ':') protocol += ':';
422
+
423
+ // only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
424
+ // unless they had them to begin with.
425
+ if (self.slashes ||
426
+ (!protocol || slashedProtocol[protocol]) && host !== false) {
427
+ host = '//' + (host || '');
428
+ if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname;
429
+ } else if (!host) {
430
+ host = '';
431
+ }
432
+
433
+ if (hash && hash.charAt(0) !== '#') hash = '#' + hash;
434
+ if (search && search.charAt(0) !== '?') search = '?' + search;
435
+
436
+ pathname = pathname.replace(/[?#]/g, function(match) {
437
+ return encodeURIComponent(match);
438
+ });
439
+ search = search.replace('#', '%23');
440
+
441
+ return protocol + host + pathname + search + hash;
442
+ }
443
+
444
+ Url.prototype.format = function() {
445
+ return format(this);
446
+ }
447
+
448
+ function urlResolve(source, relative) {
449
+ return urlParse(source, false, true).resolve(relative);
450
+ }
451
+
452
+ Url.prototype.resolve = function(relative) {
453
+ return this.resolveObject(urlParse(relative, false, true)).format();
454
+ };
455
+
456
+ function urlResolveObject(source, relative) {
457
+ if (!source) return relative;
458
+ return urlParse(source, false, true).resolveObject(relative);
459
+ }
460
+
461
+ Url.prototype.resolveObject = function(relative) {
462
+ if (isString(relative)) {
463
+ var rel = new Url();
464
+ rel.parse(relative, false, true);
465
+ relative = rel;
466
+ }
467
+
468
+ var result = new Url();
469
+ var tkeys = Object.keys(this);
470
+ for (var tk = 0; tk < tkeys.length; tk++) {
471
+ var tkey = tkeys[tk];
472
+ result[tkey] = this[tkey];
473
+ }
474
+
475
+ // hash is always overridden, no matter what.
476
+ // even href="" will remove it.
477
+ result.hash = relative.hash;
478
+
479
+ // if the relative url is empty, then there's nothing left to do here.
480
+ if (relative.href === '') {
481
+ result.href = result.format();
482
+ return result;
483
+ }
484
+
485
+ // hrefs like //foo/bar always cut to the protocol.
486
+ if (relative.slashes && !relative.protocol) {
487
+ // take everything except the protocol from relative
488
+ var rkeys = Object.keys(relative);
489
+ for (var rk = 0; rk < rkeys.length; rk++) {
490
+ var rkey = rkeys[rk];
491
+ if (rkey !== 'protocol')
492
+ result[rkey] = relative[rkey];
493
+ }
494
+
495
+ //urlParse appends trailing / to urls like http://www.example.com
496
+ if (slashedProtocol[result.protocol] &&
497
+ result.hostname && !result.pathname) {
498
+ result.path = result.pathname = '/';
499
+ }
500
+
501
+ result.href = result.format();
502
+ return result;
503
+ }
504
+ var relPath;
505
+ if (relative.protocol && relative.protocol !== result.protocol) {
506
+ // if it's a known url protocol, then changing
507
+ // the protocol does weird things
508
+ // first, if it's not file:, then we MUST have a host,
509
+ // and if there was a path
510
+ // to begin with, then we MUST have a path.
511
+ // if it is file:, then the host is dropped,
512
+ // because that's known to be hostless.
513
+ // anything else is assumed to be absolute.
514
+ if (!slashedProtocol[relative.protocol]) {
515
+ var keys = Object.keys(relative);
516
+ for (var v = 0; v < keys.length; v++) {
517
+ var k = keys[v];
518
+ result[k] = relative[k];
519
+ }
520
+ result.href = result.format();
521
+ return result;
522
+ }
523
+
524
+ result.protocol = relative.protocol;
525
+ if (!relative.host && !hostlessProtocol[relative.protocol]) {
526
+ relPath = (relative.pathname || '').split('/');
527
+ while (relPath.length && !(relative.host = relPath.shift()));
528
+ if (!relative.host) relative.host = '';
529
+ if (!relative.hostname) relative.hostname = '';
530
+ if (relPath[0] !== '') relPath.unshift('');
531
+ if (relPath.length < 2) relPath.unshift('');
532
+ result.pathname = relPath.join('/');
533
+ } else {
534
+ result.pathname = relative.pathname;
535
+ }
536
+ result.search = relative.search;
537
+ result.query = relative.query;
538
+ result.host = relative.host || '';
539
+ result.auth = relative.auth;
540
+ result.hostname = relative.hostname || relative.host;
541
+ result.port = relative.port;
542
+ // to support http.request
543
+ if (result.pathname || result.search) {
544
+ var p = result.pathname || '';
545
+ var s = result.search || '';
546
+ result.path = p + s;
547
+ }
548
+ result.slashes = result.slashes || relative.slashes;
549
+ result.href = result.format();
550
+ return result;
551
+ }
552
+
553
+ var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'),
554
+ isRelAbs = (
555
+ relative.host ||
556
+ relative.pathname && relative.pathname.charAt(0) === '/'
557
+ ),
558
+ mustEndAbs = (isRelAbs || isSourceAbs ||
559
+ (result.host && relative.pathname)),
560
+ removeAllDots = mustEndAbs,
561
+ srcPath = result.pathname && result.pathname.split('/') || [],
562
+ psychotic = result.protocol && !slashedProtocol[result.protocol];
563
+ relPath = relative.pathname && relative.pathname.split('/') || [];
564
+ // if the url is a non-slashed url, then relative
565
+ // links like ../.. should be able
566
+ // to crawl up to the hostname, as well. This is strange.
567
+ // result.protocol has already been set by now.
568
+ // Later on, put the first path part into the host field.
569
+ if (psychotic) {
570
+ result.hostname = '';
571
+ result.port = null;
572
+ if (result.host) {
573
+ if (srcPath[0] === '') srcPath[0] = result.host;
574
+ else srcPath.unshift(result.host);
575
+ }
576
+ result.host = '';
577
+ if (relative.protocol) {
578
+ relative.hostname = null;
579
+ relative.port = null;
580
+ if (relative.host) {
581
+ if (relPath[0] === '') relPath[0] = relative.host;
582
+ else relPath.unshift(relative.host);
583
+ }
584
+ relative.host = null;
585
+ }
586
+ mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === '');
587
+ }
588
+ var authInHost;
589
+ if (isRelAbs) {
590
+ // it's absolute.
591
+ result.host = (relative.host || relative.host === '') ?
592
+ relative.host : result.host;
593
+ result.hostname = (relative.hostname || relative.hostname === '') ?
594
+ relative.hostname : result.hostname;
595
+ result.search = relative.search;
596
+ result.query = relative.query;
597
+ srcPath = relPath;
598
+ // fall through to the dot-handling below.
599
+ } else if (relPath.length) {
600
+ // it's relative
601
+ // throw away the existing file, and take the new path instead.
602
+ if (!srcPath) srcPath = [];
603
+ srcPath.pop();
604
+ srcPath = srcPath.concat(relPath);
605
+ result.search = relative.search;
606
+ result.query = relative.query;
607
+ } else if (!isNullOrUndefined(relative.search)) {
608
+ // just pull out the search.
609
+ // like href='?foo'.
610
+ // Put this after the other two cases because it simplifies the booleans
611
+ if (psychotic) {
612
+ result.hostname = result.host = srcPath.shift();
613
+ //occationaly the auth can get stuck only in host
614
+ //this especially happens in cases like
615
+ //url.resolveObject('mailto:local1@domain1', 'local2@domain2')
616
+ authInHost = result.host && result.host.indexOf('@') > 0 ?
617
+ result.host.split('@') : false;
618
+ if (authInHost) {
619
+ result.auth = authInHost.shift();
620
+ result.host = result.hostname = authInHost.shift();
621
+ }
622
+ }
623
+ result.search = relative.search;
624
+ result.query = relative.query;
625
+ //to support http.request
626
+ if (!isNull(result.pathname) || !isNull(result.search)) {
627
+ result.path = (result.pathname ? result.pathname : '') +
628
+ (result.search ? result.search : '');
629
+ }
630
+ result.href = result.format();
631
+ return result;
632
+ }
633
+
634
+ if (!srcPath.length) {
635
+ // no path at all. easy.
636
+ // we've already handled the other stuff above.
637
+ result.pathname = null;
638
+ //to support http.request
639
+ if (result.search) {
640
+ result.path = '/' + result.search;
641
+ } else {
642
+ result.path = null;
643
+ }
644
+ result.href = result.format();
645
+ return result;
646
+ }
647
+
648
+ // if a url ENDs in . or .., then it must get a trailing slash.
649
+ // however, if it ends in anything else non-slashy,
650
+ // then it must NOT get a trailing slash.
651
+ var last = srcPath.slice(-1)[0];
652
+ var hasTrailingSlash = (
653
+ (result.host || relative.host || srcPath.length > 1) &&
654
+ (last === '.' || last === '..') || last === '');
655
+
656
+ // strip single dots, resolve double dots to parent dir
657
+ // if the path tries to go above the root, `up` ends up > 0
658
+ var up = 0;
659
+ for (var i = srcPath.length; i >= 0; i--) {
660
+ last = srcPath[i];
661
+ if (last === '.') {
662
+ srcPath.splice(i, 1);
663
+ } else if (last === '..') {
664
+ srcPath.splice(i, 1);
665
+ up++;
666
+ } else if (up) {
667
+ srcPath.splice(i, 1);
668
+ up--;
669
+ }
670
+ }
671
+
672
+ // if the path is allowed to go above the root, restore leading ..s
673
+ if (!mustEndAbs && !removeAllDots) {
674
+ for (; up--; up) {
675
+ srcPath.unshift('..');
676
+ }
677
+ }
678
+
679
+ if (mustEndAbs && srcPath[0] !== '' &&
680
+ (!srcPath[0] || srcPath[0].charAt(0) !== '/')) {
681
+ srcPath.unshift('');
682
+ }
683
+
684
+ if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) {
685
+ srcPath.push('');
686
+ }
687
+
688
+ var isAbsolute = srcPath[0] === '' ||
689
+ (srcPath[0] && srcPath[0].charAt(0) === '/');
690
+
691
+ // put the host back
692
+ if (psychotic) {
693
+ result.hostname = result.host = isAbsolute ? '' :
694
+ srcPath.length ? srcPath.shift() : '';
695
+ //occationaly the auth can get stuck only in host
696
+ //this especially happens in cases like
697
+ //url.resolveObject('mailto:local1@domain1', 'local2@domain2')
698
+ authInHost = result.host && result.host.indexOf('@') > 0 ?
699
+ result.host.split('@') : false;
700
+ if (authInHost) {
701
+ result.auth = authInHost.shift();
702
+ result.host = result.hostname = authInHost.shift();
703
+ }
704
+ }
705
+
706
+ mustEndAbs = mustEndAbs || (result.host && srcPath.length);
707
+
708
+ if (mustEndAbs && !isAbsolute) {
709
+ srcPath.unshift('');
710
+ }
711
+
712
+ if (!srcPath.length) {
713
+ result.pathname = null;
714
+ result.path = null;
715
+ } else {
716
+ result.pathname = srcPath.join('/');
717
+ }
718
+
719
+ //to support request.http
720
+ if (!isNull(result.pathname) || !isNull(result.search)) {
721
+ result.path = (result.pathname ? result.pathname : '') +
722
+ (result.search ? result.search : '');
723
+ }
724
+ result.auth = relative.auth || result.auth;
725
+ result.slashes = result.slashes || relative.slashes;
726
+ result.href = result.format();
727
+ return result;
728
+ };
729
+
730
+ Url.prototype.parseHost = function() {
731
+ return parseHost(this);
732
+ };
733
+
734
+ function parseHost(self) {
735
+ var host = self.host;
736
+ var port = portPattern.exec(host);
737
+ if (port) {
738
+ port = port[0];
739
+ if (port !== ':') {
740
+ self.port = port.substr(1);
741
+ }
742
+ host = host.substr(0, host.length - port.length);
743
+ }
744
+ if (host) self.hostname = host;
745
+ }