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