File: index.js

package info (click to toggle)
node-urlgrey 0.4.4+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 156 kB
  • sloc: makefile: 42; sh: 2
file content (443 lines) | stat: -rw-r--r-- 10,809 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
var urlParse = require('url').parse;
var querystring = require('querystring');
var isBrowser = (typeof window !== "undefined");

var getDefaults = function(){
  var defaultUrl = "http://localhost:80";
  if (isBrowser){
    defaultUrl = window.location.href.toString();
  }
  var defaults = urlParse(defaultUrl);
  return defaults;
};

if (!Array.isArray) {
  Array.isArray = function (arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };
}

var objectEach = function(obj, cb){
  for (var k in obj){
    if (Object.prototype.hasOwnProperty.call(obj, k)) {
      cb(obj[k], k);  
    }
  }
};

var argsArray = function(obj){
    if (!obj) { return []; }
    if (Array.isArray(obj)) { return obj.slice() ; }
    var args = [];
    objectEach(obj, function(v, k){
      args[k] = v;
    });
    return args;
  };

var arrLast = function(arr){
  return arr[arr.length-1];
};

var arrFlatten = function(input, output) {
  if (!output) { output = []; }
  for (var i = 0; i < input.length; i++){
    var value = input[i];
    if (Array.isArray(value)) {
        arrFlatten(value, output);
    } else {
      output.push(value);
    }
  }
  return output;
};


var UrlGrey = function(url){
  if (!url && isBrowser){
    url = window.location.href.toString();
  }
  this.url = url;
  this._parsed = null;
};

UrlGrey.prototype.parsed = function(){
  if (!this._parsed){
    this._parsed = urlParse(this.url);
    var defaults = getDefaults();
    var p = this._parsed;
    p.protocol = p.protocol || defaults.protocol;
    p.protocol = p.protocol.slice(0,-1);
    if (p.hash){
      p.hash = p.hash.substring(1);
    }
    p.username = ''; 
    p.password = '';
    if (p.protocol !== 'file'){
      p.port = parseInt(p.port, 10);
      if (p.auth){
        var auth = p.auth.split(':');
        p.username = auth[0]; 
        p.password = auth[1];
      }
    }
    if (isBrowser){
      p.hostname = p.hostname || defaults.hostname;
    }
  }

  // enforce only returning these properties
  this._parsed = {
   protocol : this._parsed.protocol,
   auth:        this._parsed.auth,
   host:        this._parsed.host,
   port:        this._parsed.port,
   hostname:    this._parsed.hostname,
   hash:        this._parsed.hash,
   search:      this._parsed.search,
   query:       this._parsed.query,
   pathname:    this._parsed.pathname,
   path:        this._parsed.path,
   href:        this._parsed.href,
   username:    this._parsed.username,
   password:    this._parsed.password
  };


  return this._parsed;
};


UrlGrey.prototype.extendedPath = function(url){
  if (url){
    var p = urlParse(url);
    var obj = new UrlGrey(this.toString());
    if (p.hash){
      p.hash = p.hash.substring(1);
    }
    obj.parsed().hash = p.hash;
    obj.parsed().query = p.query;
    obj = obj.path(p.pathname);
    return obj;
  } else {
    var href = this.path();
    href += this.queryString() ? '?' + this.queryString() : '';
    href += this.hash() ? '#' + this.hash() : '';
    return href;
  }
};


UrlGrey.prototype.port = function(num){
  var hostname = this.parsed().hostname;

  // setter
  if (num){
    if (this.protocol() === 'file'){
      throw new Error("file urls don't have ports");
    }
    var obj = new UrlGrey(this.toString());
    obj.parsed().port = parseInt(num, 10);
    return obj;
  }

  // getter
  var output = this._parsed.port;
  if (!output){
      switch(this.protocol()){
        case 'http' : return 80;
        case 'https' : return 443;
        default : return null;
      }
  }
  return parseInt(output, 10);
};

UrlGrey.prototype.query = function(mergeObject){
  if (arguments.length === 0) {
    return querystring.parse(this.queryString());
  } else if (mergeObject === null || mergeObject === false){
    return this.queryString('');
  } else {
    // read the object out
    var oldQuery = querystring.parse(this.queryString());
    objectEach(mergeObject, function(v, k){
      if (v === null || v === false){
        delete oldQuery[k];
      } else {
        oldQuery[k] = v;
      }
    });
    var newString = querystring.stringify(oldQuery);
    var ret = this.queryString(newString);
    return ret;
  }
};

  
UrlGrey.prototype.rawQuery = function(mergeObject){
  if (arguments.length === 0) {
    if (this.queryString().length === 0) { return {}; }
    
    return this.queryString().split("&").reduce(function(obj, pair) {
      pair = pair.split("=");
      var key = pair[0];
      var val = pair[1];
      obj[key] = val;
      return obj;
    }, {});
  } else if (mergeObject === null || mergeObject === false){
    return this.queryString('');
  } else {
    // read the object out
    var oldQuery = querystring.parse(this.queryString());
    objectEach(mergeObject, function(v, k){
      if (v === null){
        delete oldQuery[k];
      } else {
        oldQuery[k] = v;
      }
    });
    var pairs = [];
    objectEach(oldQuery, function(v, k){
      pairs.push(k + '=' + v);
    });
    var newString = pairs.join('&');

    return this.queryString(newString);
  }
};

addPropertyGetterSetter('protocol');
addPropertyGetterSetter('username');
addPropertyGetterSetter('password');
addPropertyGetterSetter('hostname');
addPropertyGetterSetter('hash');
// add a method called queryString that manipulates 'query'
addPropertyGetterSetter('query', 'queryString');  
addPropertyGetterSetter('pathname', 'path');  

UrlGrey.prototype.path = function(){
  var args = arrFlatten(argsArray(arguments));
  if (args.length !== 0){
    var obj = new UrlGrey(this.toString());
    var str = args.join('/');
    str = str.replace(/\/+/g, '/'); // remove double slashes
    str = str.replace(/\/$/, '');  // remove all trailing slashes
    args = str.split('/');
    for(var i = 0; i < args.length; i++){
      args[i] = this.encode(args[i]);
    }
    str = args.join('/');
    if (str[0] !== '/'){ str = '/' + str; }
    obj.parsed().pathname = str;
    return obj;
  }
  return this.parsed().pathname;
};

UrlGrey.prototype.rawPath = function(){
  var args = arrFlatten(argsArray(arguments));
  if (args.length !== 0){
    var obj = new UrlGrey(this.toString());
    var str = args.join('/');
    str = str.replace(/\/+/g, '/'); // remove double slashes
    str = str.replace(/\/$/, '');  // remove all trailing slashes
    if (str[0] !== '/'){ str = '/' + str; }
    obj.parsed().pathname = str;
    return obj;
  }
  return this.parsed().pathname;
};

UrlGrey.prototype.encode = function(str){
  try {
    return encodeURIComponent(str);
  } catch (ex) {
    return querystring.escape(str);
  }
};

UrlGrey.prototype.decode = function(str){
  return decode(str);
};

UrlGrey.prototype.parent = function(){
  // read-only.  (can't SET parent)
  var pieces = this.path().split("/");
  var popped = pieces.pop();
  if (popped === ''){  // ignore trailing slash
    popped = pieces.pop();
  }
  if (!popped){
    throw new Error("The current path has no parent path");
  }
  return this.query(false).hash('').path(pieces.join("/"));
};

UrlGrey.prototype.rawChild = function(suffixes){
  if (suffixes){
    suffixes = argsArray(arguments);
    return this.query(false).hash('').rawPath(this.path(), suffixes);
  } else {
    // if no suffix, return the child
    var pieces = this.path().split("/");
    var last = arrLast(pieces);
    if ((pieces.length > 1) && (last === '')){
      // ignore trailing slashes
      pieces.pop();
      last = arrLast(pieces);
    }
    return last;
  }
};

UrlGrey.prototype.child = function(suffixes){
  suffixes = argsArray(arguments);
  if (suffixes.length > 0){
    return this.query(false).hash('').path(this.path(), suffixes);
  }

  // if no suffix, return the child
  var pieces = pathPieces(this.path());
  var last = arrLast(pieces);
  if ((pieces.length > 1) && (last === '')){
    // ignore trailing slashes
    pieces.pop();
    last = arrLast(pieces);
  }
  return last;
};

UrlGrey.prototype.toJSON = function(){
  return this.toString();
};

UrlGrey.prototype.toString = function(){
  var p = this.parsed();
  var retval = this.protocol() + '://';
  if (this.protocol() !== 'file'){
    var userinfo = p.username + ':' + p.password;
    if (userinfo !== ':'){
      retval += userinfo + '@';
    }
    retval += p.hostname;
    var port = portString(this);
    if (port !== ''){
      retval += ':' + port;
    }
  }
  retval += this.path() === '/' ? '' : this.path();
  var qs = this.queryString();
  if (qs){
    retval += '?' + qs;
  }
  if (p.hash){
    retval += '#' + p.hash;
  }
  return retval;
};

var pathPieces = function(path){
  var pieces = path.split('/');
  for(var i = 0; i < pieces.length; i++){
    pieces[i] = decode(pieces[i]);
  }
  return pieces;
};

var decode = function(str){
  try {
    return decodeURIComponent(str);
  } catch (ex) {
    return querystring.unescape(str);
  }
};

var portString = function(o){
  if (o.protocol() === 'https'){
    if (o.port() === 443){
      return '';
    }
  }
  if (o.protocol() === 'http'){
    if (o.port() === 80){
      return '';
    }
  }
  return '' + o.port();
};


/*
UrlGrey.prototype.absolute = function(path){
  if (path[0] == '/'){
    path = path.substring(1);
  }
  var parsed = urlParse(path);
  if (!!parsed.protocol){  // if it's already absolute, just return it
    return path;
  }
  return this._protocol + "://" + this._host + '/' + path;
};

// TODO make this interpolate vars into the url.   both sinatra style and url-tempates
// TODO name this: 
UrlGrey.prototype.get = function(nameOrPath, varDict){
  if (!!nameOrPath){
    if (!!varDict){
      return this.absolute(this._router.getUrl(nameOrPath, varDict));
    }
    return this.absolute(this._router.getUrl(nameOrPath));
  }
  return this.url;
};*/

/*
// TODO needs to take a template as an input
UrlGrey.prototype.param = function(key, defaultValue){
  var value = this.params()[key];
  if (!!value) { 
    return value; 
  }
  return defaultValue;
};

// TODO extract params, given a template?
// TODO needs to take a template as an input
UrlGrey.prototype.params = function(inUrl){
  if (!!inUrl){
    return this._router.pathVariables(inUrl);
  }
  if (!!this._params){
    return this._params;
  }
  return this._router.pathVariables(this.url);
};
*/

// TODO relative()  // takes an absolutepath and returns a relative one
// TODO absolute() // takes a relative path and returns an absolute one.



module.exports = function(url){ return new UrlGrey(url); };

function addPropertyGetterSetter(propertyName, methodName){
  if (!methodName){
    methodName = propertyName;
  }
  UrlGrey.prototype[methodName] = function(str){
    if (!str && str !== "") {
      return this.parsed()[propertyName];  
    } else {
      var obj = new UrlGrey(this.toString());
      obj.parsed()[propertyName] = str;
      return obj;
    }
  };
}