@zthun/webigail-url 4.0.9 → 5.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.
- package/dist/index.cjs +318 -535
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +318 -535
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -92,69 +92,17 @@ import URLParse from 'url-parse';
|
|
|
92
92
|
return ZMimeTypeText;
|
|
93
93
|
}({});
|
|
94
94
|
|
|
95
|
-
function _define_property$2(obj, key, value) {
|
|
96
|
-
if (key in obj) {
|
|
97
|
-
Object.defineProperty(obj, key, {
|
|
98
|
-
value: value,
|
|
99
|
-
enumerable: true,
|
|
100
|
-
configurable: true,
|
|
101
|
-
writable: true
|
|
102
|
-
});
|
|
103
|
-
} else {
|
|
104
|
-
obj[key] = value;
|
|
105
|
-
}
|
|
106
|
-
return obj;
|
|
107
|
-
}
|
|
108
|
-
function _object_spread$1(target) {
|
|
109
|
-
for(var i = 1; i < arguments.length; i++){
|
|
110
|
-
var source = arguments[i] != null ? arguments[i] : {};
|
|
111
|
-
var ownKeys = Object.keys(source);
|
|
112
|
-
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
113
|
-
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
114
|
-
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
115
|
-
}));
|
|
116
|
-
}
|
|
117
|
-
ownKeys.forEach(function(key) {
|
|
118
|
-
_define_property$2(target, key, source[key]);
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
return target;
|
|
122
|
-
}
|
|
123
95
|
/**
|
|
124
96
|
* A mapping of supported mime types.
|
|
125
|
-
*/
|
|
126
|
-
|
|
127
|
-
|
|
97
|
+
*/ const ZSupportedMimeTypes = Object.freeze({
|
|
98
|
+
...{
|
|
99
|
+
"": "text/plain;charset=ASCII"
|
|
100
|
+
},
|
|
101
|
+
...keyBy(Object.values(ZMimeTypeApplication)),
|
|
102
|
+
...keyBy(Object.values(ZMimeTypeText)),
|
|
103
|
+
...keyBy(Object.values(ZMimeTypeImage))
|
|
104
|
+
});
|
|
128
105
|
|
|
129
|
-
function _array_like_to_array(arr, len) {
|
|
130
|
-
if (len == null || len > arr.length) len = arr.length;
|
|
131
|
-
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
132
|
-
return arr2;
|
|
133
|
-
}
|
|
134
|
-
function _array_with_holes(arr) {
|
|
135
|
-
if (Array.isArray(arr)) return arr;
|
|
136
|
-
}
|
|
137
|
-
function _array_without_holes(arr) {
|
|
138
|
-
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
139
|
-
}
|
|
140
|
-
function _class_call_check$1(instance, Constructor) {
|
|
141
|
-
if (!(instance instanceof Constructor)) {
|
|
142
|
-
throw new TypeError("Cannot call a class as a function");
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
function _defineProperties$1(target, props) {
|
|
146
|
-
for(var i = 0; i < props.length; i++){
|
|
147
|
-
var descriptor = props[i];
|
|
148
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
149
|
-
descriptor.configurable = true;
|
|
150
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
151
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
function _create_class$1(Constructor, protoProps, staticProps) {
|
|
155
|
-
if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
|
|
156
|
-
return Constructor;
|
|
157
|
-
}
|
|
158
106
|
function _define_property$1(obj, key, value) {
|
|
159
107
|
if (key in obj) {
|
|
160
108
|
Object.defineProperty(obj, key, {
|
|
@@ -168,63 +116,11 @@ function _define_property$1(obj, key, value) {
|
|
|
168
116
|
}
|
|
169
117
|
return obj;
|
|
170
118
|
}
|
|
171
|
-
function _iterable_to_array(iter) {
|
|
172
|
-
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
173
|
-
}
|
|
174
|
-
function _non_iterable_rest() {
|
|
175
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
176
|
-
}
|
|
177
|
-
function _non_iterable_spread() {
|
|
178
|
-
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
179
|
-
}
|
|
180
|
-
function _object_spread(target) {
|
|
181
|
-
for(var i = 1; i < arguments.length; i++){
|
|
182
|
-
var source = arguments[i] != null ? arguments[i] : {};
|
|
183
|
-
var ownKeys = Object.keys(source);
|
|
184
|
-
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
185
|
-
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
186
|
-
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
187
|
-
}));
|
|
188
|
-
}
|
|
189
|
-
ownKeys.forEach(function(key) {
|
|
190
|
-
_define_property$1(target, key, source[key]);
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
return target;
|
|
194
|
-
}
|
|
195
|
-
function _to_array(arr) {
|
|
196
|
-
return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
|
|
197
|
-
}
|
|
198
|
-
function _to_consumable_array(arr) {
|
|
199
|
-
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
200
|
-
}
|
|
201
|
-
function _unsupported_iterable_to_array(o, minLen) {
|
|
202
|
-
if (!o) return;
|
|
203
|
-
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
204
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
205
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
206
|
-
if (n === "Map" || n === "Set") return Array.from(n);
|
|
207
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
208
|
-
}
|
|
209
119
|
/**
|
|
210
120
|
* Represents an object that is helpful in building a data url with support
|
|
211
121
|
* for data encoding.
|
|
212
|
-
*/
|
|
213
|
-
|
|
214
|
-
_class_call_check$1(this, ZDataUrlBuilder);
|
|
215
|
-
/**
|
|
216
|
-
* The representation of the data url object.
|
|
217
|
-
*/ _define_property$1(this, "_data", void 0);
|
|
218
|
-
this._data = {
|
|
219
|
-
mimeType: "",
|
|
220
|
-
encoding: "utf8",
|
|
221
|
-
buffer: new Uint8Array([])
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
_create_class$1(ZDataUrlBuilder, [
|
|
225
|
-
{
|
|
226
|
-
key: "parse",
|
|
227
|
-
value: /**
|
|
122
|
+
*/ class ZDataUrlBuilder {
|
|
123
|
+
/**
|
|
228
124
|
* Parses an existing data url and sets all properties.
|
|
229
125
|
*
|
|
230
126
|
* @param url -
|
|
@@ -232,55 +128,53 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
232
128
|
*
|
|
233
129
|
* @returns
|
|
234
130
|
* This object.
|
|
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
|
-
} else {
|
|
275
|
-
body = decodeURIComponent(body);
|
|
276
|
-
}
|
|
277
|
-
this._data.buffer = new TextEncoder().encode(body);
|
|
278
|
-
return this;
|
|
131
|
+
*/ parse(url) {
|
|
132
|
+
this._data = {
|
|
133
|
+
mimeType: "",
|
|
134
|
+
encoding: "utf8",
|
|
135
|
+
buffer: new Uint8Array([])
|
|
136
|
+
};
|
|
137
|
+
if (!url.startsWith("data:")) {
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
url = url.substring(5);
|
|
141
|
+
const parts = url.split(",");
|
|
142
|
+
if (parts.length < 2) {
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
const [mimeType, ...bodyParts] = parts;
|
|
146
|
+
let [type, ...params] = mimeType.split(";");
|
|
147
|
+
const isBase64 = last(params) === "base64";
|
|
148
|
+
this._data.encoding = isBase64 ? "base64" : "utf8";
|
|
149
|
+
if (isBase64) {
|
|
150
|
+
params.pop();
|
|
151
|
+
}
|
|
152
|
+
if (!Object.hasOwnProperty.call(ZSupportedMimeTypes, type)) {
|
|
153
|
+
type = ZMimeTypeApplication.OctetStream;
|
|
154
|
+
params = [];
|
|
155
|
+
}
|
|
156
|
+
type = [
|
|
157
|
+
type,
|
|
158
|
+
...params
|
|
159
|
+
].join(";");
|
|
160
|
+
this._data.mimeType = type;
|
|
161
|
+
// Commas can be in the body. Type this into chrome and you can
|
|
162
|
+
// see that chrome actually parses it: data:text/plain,cat,,,
|
|
163
|
+
// We will support this here, but we're going to properly encode it.
|
|
164
|
+
let body = bodyParts.join("%2C");
|
|
165
|
+
if (isBase64) {
|
|
166
|
+
try {
|
|
167
|
+
body = atob(body);
|
|
168
|
+
} catch {
|
|
169
|
+
body = "";
|
|
279
170
|
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
171
|
+
} else {
|
|
172
|
+
body = decodeURIComponent(body);
|
|
173
|
+
}
|
|
174
|
+
this._data.buffer = new TextEncoder().encode(body);
|
|
175
|
+
return this;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
284
178
|
* Sets the mime type.
|
|
285
179
|
*
|
|
286
180
|
* @param type -
|
|
@@ -288,14 +182,11 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
288
182
|
*
|
|
289
183
|
* @returns
|
|
290
184
|
* This object.
|
|
291
|
-
*/
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
{
|
|
297
|
-
key: "buffer",
|
|
298
|
-
value: /**
|
|
185
|
+
*/ mimeType(type) {
|
|
186
|
+
this._data.mimeType = type;
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
299
190
|
* Sets the data buffer.
|
|
300
191
|
*
|
|
301
192
|
* @param data -
|
|
@@ -303,14 +194,11 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
303
194
|
* is expected to be utf8.
|
|
304
195
|
* @returns
|
|
305
196
|
* This object.
|
|
306
|
-
*/
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
{
|
|
312
|
-
key: "encode",
|
|
313
|
-
value: /**
|
|
197
|
+
*/ buffer(data) {
|
|
198
|
+
this._data.buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
199
|
+
return this;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
314
202
|
* Sets the output encoding.
|
|
315
203
|
*
|
|
316
204
|
* If you output encode the data as utf8, then it will be properly
|
|
@@ -321,69 +209,55 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
321
209
|
*
|
|
322
210
|
* @returns
|
|
323
211
|
* This object.
|
|
324
|
-
*/
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
{
|
|
330
|
-
key: "build",
|
|
331
|
-
value: /**
|
|
212
|
+
*/ encode(encoding) {
|
|
213
|
+
this._data.encoding = encoding;
|
|
214
|
+
return this;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
332
217
|
* Builds the url string and returns it.
|
|
333
218
|
*
|
|
334
219
|
* @returns The url string.
|
|
335
|
-
*/
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
{
|
|
353
|
-
key: "info",
|
|
354
|
-
value: /**
|
|
220
|
+
*/ build() {
|
|
221
|
+
const protocol = "data";
|
|
222
|
+
const modifier = this._data.encoding === "base64" ? ";base64" : "";
|
|
223
|
+
let raw = new TextDecoder("utf8").decode(this._data.buffer);
|
|
224
|
+
if (this._data.encoding === "utf8") {
|
|
225
|
+
raw = encodeURIComponent(raw);
|
|
226
|
+
// Note ! should be encoded as %21, but for uri's..it's not because
|
|
227
|
+
// it's actually valid, but some servers don't accept ! as a character
|
|
228
|
+
// and it must be encoded. Just fix it here.
|
|
229
|
+
raw = raw.split("!").join("%21");
|
|
230
|
+
}
|
|
231
|
+
if (this._data.encoding === "base64") {
|
|
232
|
+
raw = btoa(raw);
|
|
233
|
+
}
|
|
234
|
+
return `${protocol}:${this._data.mimeType}${modifier},${raw}`;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
355
237
|
* Gets the current information about the url being built.
|
|
356
238
|
*
|
|
357
239
|
* @returns The current information about the url being built.
|
|
358
|
-
*/
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
]);
|
|
365
|
-
return ZDataUrlBuilder;
|
|
366
|
-
}();
|
|
367
|
-
|
|
368
|
-
function _class_call_check(instance, Constructor) {
|
|
369
|
-
if (!(instance instanceof Constructor)) {
|
|
370
|
-
throw new TypeError("Cannot call a class as a function");
|
|
240
|
+
*/ info() {
|
|
241
|
+
const other = {
|
|
242
|
+
...this._data
|
|
243
|
+
};
|
|
244
|
+
other.buffer = this._data.buffer.slice();
|
|
245
|
+
return other;
|
|
371
246
|
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
247
|
+
/**
|
|
248
|
+
* Initializes a new instance of this object.
|
|
249
|
+
*/ constructor(){
|
|
250
|
+
/**
|
|
251
|
+
* The representation of the data url object.
|
|
252
|
+
*/ _define_property$1(this, "_data", void 0);
|
|
253
|
+
this._data = {
|
|
254
|
+
mimeType: "",
|
|
255
|
+
encoding: "utf8",
|
|
256
|
+
buffer: new Uint8Array([])
|
|
257
|
+
};
|
|
380
258
|
}
|
|
381
259
|
}
|
|
382
|
-
|
|
383
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
384
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
385
|
-
return Constructor;
|
|
386
|
-
}
|
|
260
|
+
|
|
387
261
|
function _define_property(obj, key, value) {
|
|
388
262
|
if (key in obj) {
|
|
389
263
|
Object.defineProperty(obj, key, {
|
|
@@ -410,27 +284,34 @@ function _define_property(obj, key, value) {
|
|
|
410
284
|
}({});
|
|
411
285
|
/**
|
|
412
286
|
* Represents an object that is helpful in building a url.
|
|
413
|
-
*/
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
*
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
287
|
+
*/ class ZUrlBuilder {
|
|
288
|
+
/**
|
|
289
|
+
* Gets whether the given protocols default port is port.
|
|
290
|
+
*
|
|
291
|
+
* The main purpose of this method is to determine if a url requires
|
|
292
|
+
* a port section. Therefore, there are some special behaviors which may
|
|
293
|
+
* not be obvious:
|
|
294
|
+
*
|
|
295
|
+
* 1. If the port is falsy then this method returns true.
|
|
296
|
+
* 2. If the port is NaN, then this method returns true.
|
|
297
|
+
* 3. If the port is a string that evaluates to 0, then this method returns true.
|
|
298
|
+
* 4. If port is less than 0, then this method returns true.
|
|
299
|
+
*
|
|
300
|
+
* @param protocol -
|
|
301
|
+
* The protocol to check.
|
|
302
|
+
* @param port -
|
|
303
|
+
* The port to compare.
|
|
304
|
+
*
|
|
305
|
+
* @returns
|
|
306
|
+
* True if the default port for protocol is port.
|
|
307
|
+
*/ static defaults(protocol, port) {
|
|
308
|
+
const numericPort = +port;
|
|
309
|
+
if (isNaN(numericPort) || numericPort < 1) {
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
return ZUrlBuilder.ProtocolPorts[protocol] === +port;
|
|
429
313
|
}
|
|
430
|
-
|
|
431
|
-
{
|
|
432
|
-
key: "location",
|
|
433
|
-
value: /**
|
|
314
|
+
/**
|
|
434
315
|
* Fills the information from the current location data.
|
|
435
316
|
*
|
|
436
317
|
* @param loc -
|
|
@@ -438,25 +319,17 @@ function _define_property(obj, key, value) {
|
|
|
438
319
|
*
|
|
439
320
|
* @returns
|
|
440
321
|
* This object.
|
|
441
|
-
*/
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
});
|
|
453
|
-
}
|
|
454
|
-
return this;
|
|
455
|
-
}
|
|
456
|
-
},
|
|
457
|
-
{
|
|
458
|
-
key: "api",
|
|
459
|
-
value: /**
|
|
322
|
+
*/ location(loc) {
|
|
323
|
+
this.protocol(loc.protocol).hostname(loc.hostname).hash(loc.hash).path(loc.pathname).port(+loc.port);
|
|
324
|
+
let search = loc.search;
|
|
325
|
+
if (search.startsWith("?")) {
|
|
326
|
+
search = search.slice(1);
|
|
327
|
+
const pairs = search.split("&");
|
|
328
|
+
pairs.map((pair)=>pair.split("=")).forEach((matrix)=>this.param(matrix[0], matrix[1]));
|
|
329
|
+
}
|
|
330
|
+
return this;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
460
333
|
* Fills the information for an api path call.
|
|
461
334
|
*
|
|
462
335
|
* This is a combination of location, hash with an empty string, and an
|
|
@@ -471,14 +344,10 @@ function _define_property(obj, key, value) {
|
|
|
471
344
|
*
|
|
472
345
|
* @returns
|
|
473
346
|
* This object.
|
|
474
|
-
*/
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
},
|
|
479
|
-
{
|
|
480
|
-
key: "parse",
|
|
481
|
-
value: /**
|
|
347
|
+
*/ api(loc, basePath = "api") {
|
|
348
|
+
return this.location(loc).hash("").path(basePath);
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
482
351
|
* Parses an existing url and sets all properties.
|
|
483
352
|
*
|
|
484
353
|
* If you give this a path without the protocol and hostname, then it will
|
|
@@ -493,19 +362,13 @@ function _define_property(obj, key, value) {
|
|
|
493
362
|
*
|
|
494
363
|
* @returns
|
|
495
364
|
* This object.
|
|
496
|
-
*/
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
return this;
|
|
504
|
-
}
|
|
505
|
-
},
|
|
506
|
-
{
|
|
507
|
-
key: "gravatar",
|
|
508
|
-
value: /**
|
|
365
|
+
*/ parse(url) {
|
|
366
|
+
const current = new URLParse(url, true);
|
|
367
|
+
this.protocol(current.protocol).username(current.username).password(current.password).hostname(current.hostname).hash(current.hash).path(current.pathname).port(current.port ? +current.port : undefined);
|
|
368
|
+
Object.keys(current.query).forEach((key)=>this.param(key, current.query[key]));
|
|
369
|
+
return this;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
509
372
|
* Sets the url for a user gravatar.
|
|
510
373
|
*
|
|
511
374
|
* @param hash -
|
|
@@ -515,16 +378,13 @@ function _define_property(obj, key, value) {
|
|
|
515
378
|
*
|
|
516
379
|
* @returns
|
|
517
380
|
* This object.
|
|
518
|
-
*/
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
{
|
|
526
|
-
key: "youTube",
|
|
527
|
-
value: /**
|
|
381
|
+
*/ gravatar(hash, size) {
|
|
382
|
+
let current = this.parse(ZUrlBuilder.UrlGravatar);
|
|
383
|
+
current = hash ? current.append(hash) : current;
|
|
384
|
+
current = size ? current.param("s", String(size)) : current;
|
|
385
|
+
return current;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
528
388
|
* Sets the url target to something on YouTube.
|
|
529
389
|
*
|
|
530
390
|
* @param api -
|
|
@@ -537,20 +397,17 @@ function _define_property(obj, key, value) {
|
|
|
537
397
|
* The url for YouTube. If you set the api and
|
|
538
398
|
* id, then the url will be targeted to a specific video,
|
|
539
399
|
* otherwise, the base domain url of YouTube will be returned.
|
|
540
|
-
*/
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
{
|
|
552
|
-
key: "protocol",
|
|
553
|
-
value: /**
|
|
400
|
+
*/ youTube(api, id) {
|
|
401
|
+
let current = this.parse(ZUrlBuilder.UrlYouTube);
|
|
402
|
+
current = api ? current.path(`${api}/${id}`) : current;
|
|
403
|
+
// The watch api is a little bizarre that they don't actually
|
|
404
|
+
// use the same format as their other apis. So we will handle this here.
|
|
405
|
+
if (api === "watch") {
|
|
406
|
+
current = current.path(api).param("v", id);
|
|
407
|
+
}
|
|
408
|
+
return current;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
554
411
|
* Sets the protocol.
|
|
555
412
|
*
|
|
556
413
|
* @param protocol -
|
|
@@ -558,14 +415,11 @@ function _define_property(obj, key, value) {
|
|
|
558
415
|
*
|
|
559
416
|
* @returns
|
|
560
417
|
* This object.
|
|
561
|
-
*/
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
{
|
|
567
|
-
key: "username",
|
|
568
|
-
value: /**
|
|
418
|
+
*/ protocol(protocol) {
|
|
419
|
+
this._url.protocol = protocol;
|
|
420
|
+
return this;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
569
423
|
* Sets the user name.
|
|
570
424
|
*
|
|
571
425
|
* Used for things like ssh and ftp.
|
|
@@ -575,14 +429,11 @@ function _define_property(obj, key, value) {
|
|
|
575
429
|
*
|
|
576
430
|
* @returns
|
|
577
431
|
* This object.
|
|
578
|
-
*/
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
{
|
|
584
|
-
key: "password",
|
|
585
|
-
value: /**
|
|
432
|
+
*/ username(user) {
|
|
433
|
+
this._url.username = user;
|
|
434
|
+
return this;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
586
437
|
* Sets the password.
|
|
587
438
|
*
|
|
588
439
|
* This is only valid if the username is set.
|
|
@@ -592,14 +443,11 @@ function _define_property(obj, key, value) {
|
|
|
592
443
|
*
|
|
593
444
|
* @returns
|
|
594
445
|
* This object.
|
|
595
|
-
*/
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
{
|
|
601
|
-
key: "hostname",
|
|
602
|
-
value: /**
|
|
446
|
+
*/ password(pwd) {
|
|
447
|
+
this._url.password = pwd;
|
|
448
|
+
return this;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
603
451
|
* Sets the host name.
|
|
604
452
|
*
|
|
605
453
|
* This sets the entire hostname as the root domain. This
|
|
@@ -611,14 +459,11 @@ function _define_property(obj, key, value) {
|
|
|
611
459
|
*
|
|
612
460
|
* @returns
|
|
613
461
|
* This object.
|
|
614
|
-
*/
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
{
|
|
620
|
-
key: "subdomain",
|
|
621
|
-
value: /**
|
|
462
|
+
*/ hostname(host) {
|
|
463
|
+
this._url.hostname = host;
|
|
464
|
+
return this;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
622
467
|
* Adds a subdomain in front of the hostname.
|
|
623
468
|
*
|
|
624
469
|
* If a hostname was never set, then domain becomes the hostname.
|
|
@@ -628,28 +473,22 @@ function _define_property(obj, key, value) {
|
|
|
628
473
|
*
|
|
629
474
|
* @returns
|
|
630
475
|
* This object.
|
|
631
|
-
*/
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
{
|
|
637
|
-
key: "popSubdomain",
|
|
638
|
-
value: /**
|
|
476
|
+
*/ subdomain(domain) {
|
|
477
|
+
this._url.hostname = this._url.hostname ? `${domain}.${this._url.hostname}` : domain;
|
|
478
|
+
return this;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
639
481
|
* Removes a subdomain from the current domain.
|
|
640
482
|
*
|
|
641
483
|
* @returns
|
|
642
484
|
* This object.
|
|
643
|
-
*/
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
{
|
|
651
|
-
key: "port",
|
|
652
|
-
value: /**
|
|
485
|
+
*/ popSubdomain() {
|
|
486
|
+
const parts = this._url.hostname.split(".");
|
|
487
|
+
parts.splice(0, 1);
|
|
488
|
+
this._url.hostname = parts.join(".");
|
|
489
|
+
return this;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
653
492
|
* Sets the port.
|
|
654
493
|
*
|
|
655
494
|
* @param port -
|
|
@@ -657,14 +496,11 @@ function _define_property(obj, key, value) {
|
|
|
657
496
|
*
|
|
658
497
|
* @returns
|
|
659
498
|
* This object.
|
|
660
|
-
*/
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
{
|
|
666
|
-
key: "path",
|
|
667
|
-
value: /**
|
|
499
|
+
*/ port(port) {
|
|
500
|
+
this._url.port = port;
|
|
501
|
+
return this;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
668
504
|
* Removes all existing path segments and restarts the path.
|
|
669
505
|
*
|
|
670
506
|
* @param path -
|
|
@@ -672,16 +508,13 @@ function _define_property(obj, key, value) {
|
|
|
672
508
|
*
|
|
673
509
|
* @returns
|
|
674
510
|
* This object.
|
|
675
|
-
*/
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
{
|
|
683
|
-
key: "append",
|
|
684
|
-
value: /**
|
|
511
|
+
*/ path(path) {
|
|
512
|
+
this._url.path = [
|
|
513
|
+
path
|
|
514
|
+
];
|
|
515
|
+
return this;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
685
518
|
* Appends a path segment.
|
|
686
519
|
*
|
|
687
520
|
* @param path -
|
|
@@ -689,14 +522,11 @@ function _define_property(obj, key, value) {
|
|
|
689
522
|
*
|
|
690
523
|
* @returns
|
|
691
524
|
* This object.
|
|
692
|
-
*/
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
{
|
|
698
|
-
key: "hash",
|
|
699
|
-
value: /**
|
|
525
|
+
*/ append(path) {
|
|
526
|
+
this._url.path.push(path);
|
|
527
|
+
return this;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
700
530
|
* Sets the hash section.
|
|
701
531
|
*
|
|
702
532
|
* @param hash -
|
|
@@ -704,14 +534,11 @@ function _define_property(obj, key, value) {
|
|
|
704
534
|
*
|
|
705
535
|
* @returns
|
|
706
536
|
* This object.
|
|
707
|
-
*/
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
{
|
|
713
|
-
key: "param",
|
|
714
|
-
value: /**
|
|
537
|
+
*/ hash(hash) {
|
|
538
|
+
this._url.hash = hash;
|
|
539
|
+
return this;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
715
542
|
* Adds a search parameter.
|
|
716
543
|
*
|
|
717
544
|
* This version assumes that value is not null.
|
|
@@ -723,17 +550,14 @@ function _define_property(obj, key, value) {
|
|
|
723
550
|
*
|
|
724
551
|
* @returns
|
|
725
552
|
* This object.
|
|
726
|
-
*/
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
{
|
|
735
|
-
key: "onlyParam",
|
|
736
|
-
value: /**
|
|
553
|
+
*/ param(key, val) {
|
|
554
|
+
this._url.params.push({
|
|
555
|
+
key,
|
|
556
|
+
val
|
|
557
|
+
});
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
737
561
|
* Removes all duplicate params and adds one with the key to the value.
|
|
738
562
|
*
|
|
739
563
|
* @param key -
|
|
@@ -741,16 +565,11 @@ function _define_property(obj, key, value) {
|
|
|
741
565
|
* @param val -
|
|
742
566
|
* The parameter value. If this is falsy, then
|
|
743
567
|
* the key is deleted.
|
|
744
|
-
*/
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
}
|
|
750
|
-
},
|
|
751
|
-
{
|
|
752
|
-
key: "page",
|
|
753
|
-
value: /**
|
|
568
|
+
*/ onlyParam(key, val) {
|
|
569
|
+
this._url.params = this._url.params.filter((p)=>p.key !== key);
|
|
570
|
+
return val ? this.param(key, val) : this;
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
754
573
|
* Adds a page param.
|
|
755
574
|
*
|
|
756
575
|
* @param page -
|
|
@@ -759,13 +578,10 @@ function _define_property(obj, key, value) {
|
|
|
759
578
|
*
|
|
760
579
|
* @returns
|
|
761
580
|
* This object.
|
|
762
|
-
*/
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
{
|
|
767
|
-
key: "size",
|
|
768
|
-
value: /**
|
|
581
|
+
*/ page(page) {
|
|
582
|
+
return this.onlyParam("page", page == null || page < 1 ? undefined : String(page));
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
769
585
|
* Adds a size param.
|
|
770
586
|
*
|
|
771
587
|
* @param size -
|
|
@@ -774,13 +590,10 @@ function _define_property(obj, key, value) {
|
|
|
774
590
|
*
|
|
775
591
|
* @returns
|
|
776
592
|
* This object.
|
|
777
|
-
*/
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
{
|
|
782
|
-
key: "search",
|
|
783
|
-
value: /**
|
|
593
|
+
*/ size(size) {
|
|
594
|
+
return this.onlyParam("size", size == null || size < 0 ? undefined : String(size));
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
784
597
|
* Adds a search param.
|
|
785
598
|
*
|
|
786
599
|
* @param search -
|
|
@@ -789,13 +602,10 @@ function _define_property(obj, key, value) {
|
|
|
789
602
|
*
|
|
790
603
|
* @returns
|
|
791
604
|
* This object.
|
|
792
|
-
*/
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
{
|
|
797
|
-
key: "filter",
|
|
798
|
-
value: /**
|
|
605
|
+
*/ search(search) {
|
|
606
|
+
return this.onlyParam("search", search || undefined);
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
799
609
|
* Adds a filter param.
|
|
800
610
|
*
|
|
801
611
|
* @param filter -
|
|
@@ -804,13 +614,10 @@ function _define_property(obj, key, value) {
|
|
|
804
614
|
*
|
|
805
615
|
* @returns
|
|
806
616
|
* This object.
|
|
807
|
-
*/
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
{
|
|
812
|
-
key: "sort",
|
|
813
|
-
value: /**
|
|
617
|
+
*/ filter(filter) {
|
|
618
|
+
return this.onlyParam("filter", filter || undefined);
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
814
621
|
* Adds a sort param.
|
|
815
622
|
*
|
|
816
623
|
* @param sort -
|
|
@@ -819,101 +626,77 @@ function _define_property(obj, key, value) {
|
|
|
819
626
|
*
|
|
820
627
|
* @returns
|
|
821
628
|
* This object.
|
|
822
|
-
*/
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
{
|
|
827
|
-
key: "build",
|
|
828
|
-
value: /**
|
|
629
|
+
*/ sort(sort) {
|
|
630
|
+
return this.onlyParam("sort", sort || undefined);
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
829
633
|
* Builds the url string and returns it.
|
|
830
634
|
*
|
|
831
635
|
* @returns
|
|
832
636
|
* The url string.
|
|
833
|
-
*/
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
if (hash) {
|
|
869
|
-
url = "".concat(url, "#").concat(hash);
|
|
870
|
-
}
|
|
871
|
-
return url;
|
|
872
|
-
}
|
|
873
|
-
},
|
|
874
|
-
{
|
|
875
|
-
key: "info",
|
|
876
|
-
value: /**
|
|
637
|
+
*/ build() {
|
|
638
|
+
const search = sortBy(this._url.params, (p)=>p.key).map((param)=>`${param.key}=${encodeURIComponent(param.val)}`).join("&");
|
|
639
|
+
const user = trim(this._url.username);
|
|
640
|
+
const password = trim(this._url.password);
|
|
641
|
+
let protocol = trim(this._url.protocol);
|
|
642
|
+
let host = trim(this._url.hostname);
|
|
643
|
+
let port = String(this._url.port);
|
|
644
|
+
let hash = trim(this._url.hash);
|
|
645
|
+
let path = this._url.path.map((segment)=>trim(segment, "/")).join("/");
|
|
646
|
+
let credentials = "";
|
|
647
|
+
protocol = trimEnd(protocol, "/:");
|
|
648
|
+
host = trim(host, "/");
|
|
649
|
+
hash = trimStart(hash, "#");
|
|
650
|
+
path = trim(path, "/");
|
|
651
|
+
if (ZUrlBuilder.defaults(protocol, port)) {
|
|
652
|
+
port = "";
|
|
653
|
+
} else {
|
|
654
|
+
port = `:${port}`;
|
|
655
|
+
}
|
|
656
|
+
if (user) {
|
|
657
|
+
credentials = password ? `${user}:${password}@` : `${user}@`;
|
|
658
|
+
}
|
|
659
|
+
let url = `${protocol}://${credentials}${host}${port}`;
|
|
660
|
+
if (path) {
|
|
661
|
+
url = `${url}/${path}`;
|
|
662
|
+
}
|
|
663
|
+
if (search) {
|
|
664
|
+
url = `${url}/?${search}`;
|
|
665
|
+
}
|
|
666
|
+
if (hash) {
|
|
667
|
+
url = `${url}#${hash}`;
|
|
668
|
+
}
|
|
669
|
+
return url;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
877
672
|
* Gets the current information about the uri being built.
|
|
878
673
|
*
|
|
879
674
|
* @returns The current uri information.
|
|
880
|
-
*/
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
{
|
|
886
|
-
key: "defaults",
|
|
887
|
-
value: /**
|
|
888
|
-
* Gets whether the given protocols default port is port.
|
|
889
|
-
*
|
|
890
|
-
* The main purpose of this method is to determine if a url requires
|
|
891
|
-
* a port section. Therefore, there are some special behaviors which may
|
|
892
|
-
* not be obvious:
|
|
893
|
-
*
|
|
894
|
-
* 1. If the port is falsy then this method returns true.
|
|
895
|
-
* 2. If the port is NaN, then this method returns true.
|
|
896
|
-
* 3. If the port is a string that evaluates to 0, then this method returns true.
|
|
897
|
-
* 4. If port is less than 0, then this method returns true.
|
|
675
|
+
*/ info() {
|
|
676
|
+
return JSON.parse(JSON.stringify(this._url));
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Initializes a new instance of this object.
|
|
898
680
|
*
|
|
899
681
|
* @param protocol -
|
|
900
|
-
* The protocol to
|
|
901
|
-
* @param
|
|
902
|
-
* The
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
*
|
|
906
|
-
*/
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
}
|
|
682
|
+
* The protocol to use.
|
|
683
|
+
* @param hostname -
|
|
684
|
+
* The hostname to connect with.
|
|
685
|
+
*/ constructor(protocol = "http", hostname = "localhost"){
|
|
686
|
+
/**
|
|
687
|
+
* The representation of the url object.
|
|
688
|
+
*/ _define_property(this, "_url", void 0);
|
|
689
|
+
this._url = {
|
|
690
|
+
protocol,
|
|
691
|
+
hostname,
|
|
692
|
+
path: [
|
|
693
|
+
"/"
|
|
694
|
+
],
|
|
695
|
+
hash: "",
|
|
696
|
+
params: []
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
}
|
|
917
700
|
/**
|
|
918
701
|
* The url to the gravatar api.
|
|
919
702
|
*/ _define_property(ZUrlBuilder, "UrlGravatar", "https://s.gravatar.com/avatar");
|