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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
|
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.Mock = global.Mock || {})));
}(this, (function (exports) { 'use strict';
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
/**
* Check if we're required to add a port number.
*
* @see https://url.spec.whatwg.org/#default-port
* @param {Number|String} port Port number we need to check
* @param {String} protocol Protocol we need to check against.
* @returns {Boolean} Is it a default port for the given protocol
* @api private
*/
var requiresPort = function required(port, protocol) {
protocol = protocol.split(':')[0];
port = +port;
if (!port) { return false; }
switch (protocol) {
case 'http':
case 'ws':
return port !== 80;
case 'https':
case 'wss':
return port !== 443;
case 'ftp':
return port !== 21;
case 'gopher':
return port !== 70;
case 'file':
return false;
}
return port !== 0;
};
var has = Object.prototype.hasOwnProperty;
var undef;
/**
* Decode a URI encoded string.
*
* @param {String} input The URI encoded string.
* @returns {String|Null} The decoded string.
* @api private
*/
function decode(input) {
try {
return decodeURIComponent(input.replace(/\+/g, ' '));
} catch (e) {
return null;
}
}
/**
* Attempts to encode a given input.
*
* @param {String} input The string that needs to be encoded.
* @returns {String|Null} The encoded string.
* @api private
*/
function encode(input) {
try {
return encodeURIComponent(input);
} catch (e) {
return null;
}
}
/**
* Simple query string parser.
*
* @param {String} query The query string that needs to be parsed.
* @returns {Object}
* @api public
*/
function querystring(query) {
var parser = /([^=?#&]+)=?([^&]*)/g
, result = {}
, part;
while (part = parser.exec(query)) {
var key = decode(part[1])
, value = decode(part[2]);
//
// Prevent overriding of existing properties. This ensures that build-in
// methods like `toString` or __proto__ are not overriden by malicious
// querystrings.
//
// In the case if failed decoding, we want to omit the key/value pairs
// from the result.
//
if (key === null || value === null || key in result) { continue; }
result[key] = value;
}
return result;
}
/**
* Transform a query string to an object.
*
* @param {Object} obj Object that should be transformed.
* @param {String} prefix Optional prefix.
* @returns {String}
* @api public
*/
function querystringify(obj, prefix) {
prefix = prefix || '';
var pairs = []
, value
, key;
//
// Optionally prefix with a '?' if needed
//
if ('string' !== typeof prefix) { prefix = '?'; }
for (key in obj) {
if (has.call(obj, key)) {
value = obj[key];
//
// Edge cases where we actually want to encode the value to an empty
// string instead of the stringified value.
//
if (!value && (value === null || value === undef || isNaN(value))) {
value = '';
}
key = encode(key);
value = encode(value);
//
// If we failed to encode the strings, we should bail out as we don't
// want to add invalid strings to the query.
//
if (key === null || value === null) { continue; }
pairs.push(key +'='+ value);
}
}
return pairs.length ? prefix + pairs.join('&') : '';
}
//
// Expose the module.
//
var stringify = querystringify;
var parse = querystring;
var querystringify_1 = {
stringify: stringify,
parse: parse
};
var CRHTLF = /[\n\r\t]/g;
var slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\//;
var protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\\/]+)?([\S\s]*)/i;
var windowsDriveLetter = /^[a-zA-Z]:/;
var whitespace = /^[\x00-\x20\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+/;
/**
* Trim a given string.
*
* @param {String} str String to trim.
* @public
*/
function trimLeft(str) {
return (str ? str : '').toString().replace(whitespace, '');
}
/**
* These are the parse rules for the URL parser, it informs the parser
* about:
*
* 0. The char it Needs to parse, if it's a string it should be done using
* indexOf, RegExp using exec and NaN means set as current value.
* 1. The property we should set when parsing this value.
* 2. Indication if it's backwards or forward parsing, when set as number it's
* the value of extra chars that should be split off.
* 3. Inherit from location if non existing in the parser.
* 4. `toLowerCase` the resulting value.
*/
var rules = [
['#', 'hash'], // Extract from the back.
['?', 'query'], // Extract from the back.
function sanitize(address, url) { // Sanitize what is left of the address
return isSpecial(url.protocol) ? address.replace(/\\/g, '/') : address;
},
['/', 'pathname'], // Extract from the back.
['@', 'auth', 1], // Extract from the front.
[NaN, 'host', undefined, 1, 1], // Set left over value.
[/:(\d*)$/, 'port', undefined, 1], // RegExp the back.
[NaN, 'hostname', undefined, 1, 1] // Set left over.
];
/**
* These properties should not be copied or inherited from. This is only needed
* for all non blob URL's as a blob URL does not include a hash, only the
* origin.
*
* @type {Object}
* @private
*/
var ignore = { hash: 1, query: 1 };
/**
* The location object differs when your code is loaded through a normal page,
* Worker or through a worker using a blob. And with the blobble begins the
* trouble as the location object will contain the URL of the blob, not the
* location of the page where our code is loaded in. The actual origin is
* encoded in the `pathname` so we can thankfully generate a good "default"
* location from it so we can generate proper relative URL's again.
*
* @param {Object|String} loc Optional default location object.
* @returns {Object} lolcation object.
* @public
*/
function lolcation(loc) {
var globalVar;
if (typeof window !== 'undefined') { globalVar = window; }
else if (typeof commonjsGlobal !== 'undefined') { globalVar = commonjsGlobal; }
else if (typeof self !== 'undefined') { globalVar = self; }
else { globalVar = {}; }
var location = globalVar.location || {};
loc = loc || location;
var finaldestination = {}
, type = typeof loc
, key;
if ('blob:' === loc.protocol) {
finaldestination = new Url(unescape(loc.pathname), {});
} else if ('string' === type) {
finaldestination = new Url(loc, {});
for (key in ignore) { delete finaldestination[key]; }
} else if ('object' === type) {
for (key in loc) {
if (key in ignore) { continue; }
finaldestination[key] = loc[key];
}
if (finaldestination.slashes === undefined) {
finaldestination.slashes = slashes.test(loc.href);
}
}
return finaldestination;
}
/**
* Check whether a protocol scheme is special.
*
* @param {String} The protocol scheme of the URL
* @return {Boolean} `true` if the protocol scheme is special, else `false`
* @private
*/
function isSpecial(scheme) {
return (
scheme === 'file:' ||
scheme === 'ftp:' ||
scheme === 'http:' ||
scheme === 'https:' ||
scheme === 'ws:' ||
scheme === 'wss:'
);
}
/**
* @typedef ProtocolExtract
* @type Object
* @property {String} protocol Protocol matched in the URL, in lowercase.
* @property {Boolean} slashes `true` if protocol is followed by "//", else `false`.
* @property {String} rest Rest of the URL that is not part of the protocol.
*/
/**
* Extract protocol information from a URL with/without double slash ("//").
*
* @param {String} address URL we want to extract from.
* @param {Object} location
* @return {ProtocolExtract} Extracted information.
* @private
*/
function extractProtocol(address, location) {
address = trimLeft(address);
address = address.replace(CRHTLF, '');
location = location || {};
var match = protocolre.exec(address);
var protocol = match[1] ? match[1].toLowerCase() : '';
var forwardSlashes = !!match[2];
var otherSlashes = !!match[3];
var slashesCount = 0;
var rest;
if (forwardSlashes) {
if (otherSlashes) {
rest = match[2] + match[3] + match[4];
slashesCount = match[2].length + match[3].length;
} else {
rest = match[2] + match[4];
slashesCount = match[2].length;
}
} else {
if (otherSlashes) {
rest = match[3] + match[4];
slashesCount = match[3].length;
} else {
rest = match[4];
}
}
if (protocol === 'file:') {
if (slashesCount >= 2) {
rest = rest.slice(2);
}
} else if (isSpecial(protocol)) {
rest = match[4];
} else if (protocol) {
if (forwardSlashes) {
rest = rest.slice(2);
}
} else if (slashesCount >= 2 && isSpecial(location.protocol)) {
rest = match[4];
}
return {
protocol: protocol,
slashes: forwardSlashes || isSpecial(protocol),
slashesCount: slashesCount,
rest: rest
};
}
/**
* Resolve a relative URL pathname against a base URL pathname.
*
* @param {String} relative Pathname of the relative URL.
* @param {String} base Pathname of the base URL.
* @return {String} Resolved pathname.
* @private
*/
function resolve(relative, base) {
if (relative === '') { return base; }
var path = (base || '/').split('/').slice(0, -1).concat(relative.split('/'))
, i = path.length
, last = path[i - 1]
, unshift = false
, up = 0;
while (i--) {
if (path[i] === '.') {
path.splice(i, 1);
} else if (path[i] === '..') {
path.splice(i, 1);
up++;
} else if (up) {
if (i === 0) { unshift = true; }
path.splice(i, 1);
up--;
}
}
if (unshift) { path.unshift(''); }
if (last === '.' || last === '..') { path.push(''); }
return path.join('/');
}
/**
* The actual URL instance. Instead of returning an object we've opted-in to
* create an actual constructor as it's much more memory efficient and
* faster and it pleases my OCD.
*
* It is worth noting that we should not use `URL` as class name to prevent
* clashes with the global URL instance that got introduced in browsers.
*
* @constructor
* @param {String} address URL we want to parse.
* @param {Object|String} [location] Location defaults for relative paths.
* @param {Boolean|Function} [parser] Parser for the query string.
* @private
*/
function Url(address, location, parser) {
address = trimLeft(address);
address = address.replace(CRHTLF, '');
if (!(this instanceof Url)) {
return new Url(address, location, parser);
}
var relative, extracted, parse, instruction, index, key
, instructions = rules.slice()
, type = typeof location
, url = this
, i = 0;
//
// The following if statements allows this module two have compatibility with
// 2 different API:
//
// 1. Node.js's `url.parse` api which accepts a URL, boolean as arguments
// where the boolean indicates that the query string should also be parsed.
//
// 2. The `URL` interface of the browser which accepts a URL, object as
// arguments. The supplied object will be used as default values / fall-back
// for relative paths.
//
if ('object' !== type && 'string' !== type) {
parser = location;
location = null;
}
if (parser && 'function' !== typeof parser) { parser = querystringify_1.parse; }
location = lolcation(location);
//
// Extract protocol information before running the instructions.
//
extracted = extractProtocol(address || '', location);
relative = !extracted.protocol && !extracted.slashes;
url.slashes = extracted.slashes || relative && location.slashes;
url.protocol = extracted.protocol || location.protocol || '';
address = extracted.rest;
//
// When the authority component is absent the URL starts with a path
// component.
//
if (
extracted.protocol === 'file:' && (
extracted.slashesCount !== 2 || windowsDriveLetter.test(address)) ||
(!extracted.slashes &&
(extracted.protocol ||
extracted.slashesCount < 2 ||
!isSpecial(url.protocol)))
) {
instructions[3] = [/(.*)/, 'pathname'];
}
for (; i < instructions.length; i++) {
instruction = instructions[i];
if (typeof instruction === 'function') {
address = instruction(address, url);
continue;
}
parse = instruction[0];
key = instruction[1];
if (parse !== parse) {
url[key] = address;
} else if ('string' === typeof parse) {
index = parse === '@'
? address.lastIndexOf(parse)
: address.indexOf(parse);
if (~index) {
if ('number' === typeof instruction[2]) {
url[key] = address.slice(0, index);
address = address.slice(index + instruction[2]);
} else {
url[key] = address.slice(index);
address = address.slice(0, index);
}
}
} else if ((index = parse.exec(address))) {
url[key] = index[1];
address = address.slice(0, index.index);
}
url[key] = url[key] || (
relative && instruction[3] ? location[key] || '' : ''
);
//
// Hostname, host and protocol should be lowercased so they can be used to
// create a proper `origin`.
//
if (instruction[4]) { url[key] = url[key].toLowerCase(); }
}
//
// Also parse the supplied query string in to an object. If we're supplied
// with a custom parser as function use that instead of the default build-in
// parser.
//
if (parser) { url.query = parser(url.query); }
//
// If the URL is relative, resolve the pathname against the base URL.
//
if (
relative
&& location.slashes
&& url.pathname.charAt(0) !== '/'
&& (url.pathname !== '' || location.pathname !== '')
) {
url.pathname = resolve(url.pathname, location.pathname);
}
//
// Default to a / for pathname if none exists. This normalizes the URL
// to always have a /
//
if (url.pathname.charAt(0) !== '/' && isSpecial(url.protocol)) {
url.pathname = '/' + url.pathname;
}
//
// We should not add port numbers if they are already the default port number
// for a given protocol. As the host also contains the port number we're going
// override it with the hostname which contains no port number.
//
if (!requiresPort(url.port, url.protocol)) {
url.host = url.hostname;
url.port = '';
}
//
// Parse down the `auth` for the username and password.
//
url.username = url.password = '';
if (url.auth) {
index = url.auth.indexOf(':');
if (~index) {
url.username = url.auth.slice(0, index);
url.username = encodeURIComponent(decodeURIComponent(url.username));
url.password = url.auth.slice(index + 1);
url.password = encodeURIComponent(decodeURIComponent(url.password));
} else {
url.username = encodeURIComponent(decodeURIComponent(url.auth));
}
url.auth = url.password ? url.username +':'+ url.password : url.username;
}
url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host
? url.protocol +'//'+ url.host
: 'null';
//
// The href is just the compiled result.
//
url.href = url.toString();
}
/**
* This is convenience method for changing properties in the URL instance to
* insure that they all propagate correctly.
*
* @param {String} part Property we need to adjust.
* @param {Mixed} value The newly assigned value.
* @param {Boolean|Function} fn When setting the query, it will be the function
* used to parse the query.
* When setting the protocol, double slash will be
* removed from the final url if it is true.
* @returns {URL} URL instance for chaining.
* @public
*/
function set(part, value, fn) {
var url = this;
switch (part) {
case 'query':
if ('string' === typeof value && value.length) {
value = (fn || querystringify_1.parse)(value);
}
url[part] = value;
break;
case 'port':
url[part] = value;
if (!requiresPort(value, url.protocol)) {
url.host = url.hostname;
url[part] = '';
} else if (value) {
url.host = url.hostname +':'+ value;
}
break;
case 'hostname':
url[part] = value;
if (url.port) { value += ':'+ url.port; }
url.host = value;
break;
case 'host':
url[part] = value;
if (/:\d+$/.test(value)) {
value = value.split(':');
url.port = value.pop();
url.hostname = value.join(':');
} else {
url.hostname = value;
url.port = '';
}
break;
case 'protocol':
url.protocol = value.toLowerCase();
url.slashes = !fn;
break;
case 'pathname':
case 'hash':
if (value) {
var char = part === 'pathname' ? '/' : '#';
url[part] = value.charAt(0) !== char ? char + value : value;
} else {
url[part] = value;
}
break;
case 'username':
case 'password':
url[part] = encodeURIComponent(value);
break;
case 'auth':
var index = value.indexOf(':');
if (~index) {
url.username = value.slice(0, index);
url.username = encodeURIComponent(decodeURIComponent(url.username));
url.password = value.slice(index + 1);
url.password = encodeURIComponent(decodeURIComponent(url.password));
} else {
url.username = encodeURIComponent(decodeURIComponent(value));
}
}
for (var i = 0; i < rules.length; i++) {
var ins = rules[i];
if (ins[4]) { url[ins[1]] = url[ins[1]].toLowerCase(); }
}
url.auth = url.password ? url.username +':'+ url.password : url.username;
url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host
? url.protocol +'//'+ url.host
: 'null';
url.href = url.toString();
return url;
}
/**
* Transform the properties back in to a valid and full URL string.
*
* @param {Function} stringify Optional query stringify function.
* @returns {String} Compiled version of the URL.
* @public
*/
function toString(stringify) {
if (!stringify || 'function' !== typeof stringify) { stringify = querystringify_1.stringify; }
var query
, url = this
, host = url.host
, protocol = url.protocol;
if (protocol && protocol.charAt(protocol.length - 1) !== ':') { protocol += ':'; }
var result =
protocol +
((url.protocol && url.slashes) || isSpecial(url.protocol) ? '//' : '');
if (url.username) {
result += url.username;
if (url.password) { result += ':'+ url.password; }
result += '@';
} else if (url.password) {
result += ':'+ url.password;
result += '@';
} else if (
url.protocol !== 'file:' &&
isSpecial(url.protocol) &&
!host &&
url.pathname !== '/'
) {
//
// Add back the empty userinfo, otherwise the original invalid URL
// might be transformed into a valid one with `url.pathname` as host.
//
result += '@';
}
//
// Trailing colon is removed from `url.host` when it is parsed. If it still
// ends with a colon, then add back the trailing colon that was removed. This
// prevents an invalid URL from being transformed into a valid one.
//
if (host[host.length - 1] === ':') { host += ':'; }
result += host + url.pathname;
query = 'object' === typeof url.query ? stringify(url.query) : url.query;
if (query) { result += '?' !== query.charAt(0) ? '?'+ query : query; }
if (url.hash) { result += url.hash; }
return result;
}
Url.prototype = { set: set, toString: toString };
//
// Expose the URL parser and some additional properties that might be useful for
// others or testing.
//
Url.extractProtocol = extractProtocol;
Url.location = lolcation;
Url.trimLeft = trimLeft;
Url.qs = querystringify_1;
var urlParse = Url;
/*
* This delay allows the thread to finish assigning its on* methods
* before invoking the delay callback. This is purely a timing hack.
* http://geekabyte.blogspot.com/2014/01/javascript-effect-of-setting-settimeout.html
*
* @param {callback: function} the callback which will be invoked after the timeout
* @parma {context: object} the context in which to invoke the function
*/
function delay(callback, context) {
setTimeout(function (timeoutContext) { return callback.call(timeoutContext); }, 4, context);
}
function log(method, message) {
/* eslint-disable no-console */
if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'test') {
console[method].call(null, message);
}
/* eslint-enable no-console */
}
function reject(array, callback) {
if ( array === void 0 ) array = [];
var results = [];
array.forEach(function (itemInArray) {
if (!callback(itemInArray)) {
results.push(itemInArray);
}
});
return results;
}
function filter(array, callback) {
if ( array === void 0 ) array = [];
var results = [];
array.forEach(function (itemInArray) {
if (callback(itemInArray)) {
results.push(itemInArray);
}
});
return results;
}
/*
* EventTarget is an interface implemented by objects that can
* receive events and may have listeners for them.
*
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
*/
var EventTarget = function EventTarget() {
this.listeners = {};
};
/*
* Ties a listener function to an event type which can later be invoked via the
* dispatchEvent method.
*
* @param {string} type - the type of event (ie: 'open', 'message', etc.)
* @param {function} listener - callback function to invoke when an event is dispatched matching the type
* @param {boolean} useCapture - N/A TODO: implement useCapture functionality
*/
EventTarget.prototype.addEventListener = function addEventListener (type, listener /* , useCapture */) {
if (typeof listener === 'function') {
if (!Array.isArray(this.listeners[type])) {
this.listeners[type] = [];
}
// Only add the same function once
if (filter(this.listeners[type], function (item) { return item === listener; }).length === 0) {
this.listeners[type].push(listener);
}
}
};
/*
* Removes the listener so it will no longer be invoked via the dispatchEvent method.
*
* @param {string} type - the type of event (ie: 'open', 'message', etc.)
* @param {function} listener - callback function to invoke when an event is dispatched matching the type
* @param {boolean} useCapture - N/A TODO: implement useCapture functionality
*/
EventTarget.prototype.removeEventListener = function removeEventListener (type, removingListener /* , useCapture */) {
var arrayOfListeners = this.listeners[type];
this.listeners[type] = reject(arrayOfListeners, function (listener) { return listener === removingListener; });
};
/*
* Invokes all listener functions that are listening to the given event.type property. Each
* listener will be passed the event as the first argument.
*
* @param {object} event - event object which will be passed to all listeners of the event.type property
*/
EventTarget.prototype.dispatchEvent = function dispatchEvent (event) {
var this$1 = this;
var customArguments = [], len = arguments.length - 1;
while ( len-- > 0 ) customArguments[ len ] = arguments[ len + 1 ];
var eventName = event.type;
var listeners = this.listeners[eventName];
if (!Array.isArray(listeners)) {
return false;
}
listeners.forEach(function (listener) {
if (customArguments.length > 0) {
listener.apply(this$1, customArguments);
} else {
listener.call(this$1, event);
}
});
return true;
};
function trimQueryPartFromURL(url) {
var queryIndex = url.indexOf('?');
return queryIndex >= 0 ? url.slice(0, queryIndex) : url;
}
/*
* The network bridge is a way for the mock websocket object to 'communicate' with
* all available servers. This is a singleton object so it is important that you
* clean up urlMap whenever you are finished.
*/
var NetworkBridge = function NetworkBridge() {
this.urlMap = {};
};
/*
* Attaches a websocket object to the urlMap hash so that it can find the server
* it is connected to and the server in turn can find it.
*
* @param {object} websocket - websocket object to add to the urlMap hash
* @param {string} url
*/
NetworkBridge.prototype.attachWebSocket = function attachWebSocket (websocket, url) {
var serverURL = trimQueryPartFromURL(url);
var connectionLookup = this.urlMap[serverURL];
if (connectionLookup && connectionLookup.server && connectionLookup.websockets.indexOf(websocket) === -1) {
connectionLookup.websockets.push(websocket);
return connectionLookup.server;
}
};
/*
* Attaches a websocket to a room
*/
NetworkBridge.prototype.addMembershipToRoom = function addMembershipToRoom (websocket, room) {
var connectionLookup = this.urlMap[trimQueryPartFromURL(websocket.url)];
if (connectionLookup && connectionLookup.server && connectionLookup.websockets.indexOf(websocket) !== -1) {
if (!connectionLookup.roomMemberships[room]) {
connectionLookup.roomMemberships[room] = [];
}
connectionLookup.roomMemberships[room].push(websocket);
}
};
/*
* Attaches a server object to the urlMap hash so that it can find a websockets
* which are connected to it and so that websockets can in turn can find it.
*
* @param {object} server - server object to add to the urlMap hash
* @param {string} url
*/
NetworkBridge.prototype.attachServer = function attachServer (server, url) {
var serverUrl = trimQueryPartFromURL(url);
var connectionLookup = this.urlMap[serverUrl];
if (!connectionLookup) {
this.urlMap[serverUrl] = {
server: server,
websockets: [],
roomMemberships: {}
};
return server;
}
};
/*
* Finds the server which is 'running' on the given url.
*
* @param {string} url - the url to use to find which server is running on it
*/
NetworkBridge.prototype.serverLookup = function serverLookup (url) {
var serverURL = trimQueryPartFromURL(url);
var connectionLookup = this.urlMap[serverURL];
if (connectionLookup) {
return connectionLookup.server;
}
};
/*
* Finds all websockets which is 'listening' on the given url.
*
* @param {string} url - the url to use to find all websockets which are associated with it
* @param {string} room - if a room is provided, will only return sockets in this room
* @param {class} broadcaster - socket that is broadcasting and is to be excluded from the lookup
*/
NetworkBridge.prototype.websocketsLookup = function websocketsLookup (url, room, broadcaster) {
var serverURL = trimQueryPartFromURL(url);
var websockets;
var connectionLookup = this.urlMap[serverURL];
websockets = connectionLookup ? connectionLookup.websockets : [];
if (room) {
var members = connectionLookup.roomMemberships[room];
websockets = members || [];
}
return broadcaster ? websockets.filter(function (websocket) { return websocket !== broadcaster; }) : websockets;
};
/*
* Removes the entry associated with the url.
*
* @param {string} url
*/
NetworkBridge.prototype.removeServer = function removeServer (url) {
delete this.urlMap[trimQueryPartFromURL(url)];
};
/*
* Removes the individual websocket from the map of associated websockets.
*
* @param {object} websocket - websocket object to remove from the url map
* @param {string} url
*/
NetworkBridge.prototype.removeWebSocket = function removeWebSocket (websocket, url) {
var serverURL = trimQueryPartFromURL(url);
var connectionLookup = this.urlMap[serverURL];
if (connectionLookup) {
connectionLookup.websockets = reject(connectionLookup.websockets, function (socket) { return socket === websocket; });
}
};
/*
* Removes a websocket from a room
*/
NetworkBridge.prototype.removeMembershipFromRoom = function removeMembershipFromRoom (websocket, room) {
var connectionLookup = this.urlMap[trimQueryPartFromURL(websocket.url)];
var memberships = connectionLookup.roomMemberships[room];
if (connectionLookup && memberships !== null) {
connectionLookup.roomMemberships[room] = reject(memberships, function (socket) { return socket === websocket; });
}
};
var networkBridge = new NetworkBridge(); // Note: this is a singleton
/*
* https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
*/
var CLOSE_CODES = {
CLOSE_NORMAL: 1000,
CLOSE_GOING_AWAY: 1001,
CLOSE_PROTOCOL_ERROR: 1002,
CLOSE_UNSUPPORTED: 1003,
CLOSE_NO_STATUS: 1005,
CLOSE_ABNORMAL: 1006,
UNSUPPORTED_DATA: 1007,
POLICY_VIOLATION: 1008,
CLOSE_TOO_LARGE: 1009,
MISSING_EXTENSION: 1010,
INTERNAL_ERROR: 1011,
SERVICE_RESTART: 1012,
TRY_AGAIN_LATER: 1013,
TLS_HANDSHAKE: 1015
};
var ERROR_PREFIX = {
CONSTRUCTOR_ERROR: "Failed to construct 'WebSocket':",
CLOSE_ERROR: "Failed to execute 'close' on 'WebSocket':",
EVENT: {
CONSTRUCT: "Failed to construct 'Event':",
MESSAGE: "Failed to construct 'MessageEvent':",
CLOSE: "Failed to construct 'CloseEvent':"
}
};
var EventPrototype = function EventPrototype () {};
EventPrototype.prototype.stopPropagation = function stopPropagation () {};
EventPrototype.prototype.stopImmediatePropagation = function stopImmediatePropagation () {};
// if no arguments are passed then the type is set to "undefined" on
// chrome and safari.
EventPrototype.prototype.initEvent = function initEvent (type, bubbles, cancelable) {
if ( type === void 0 ) type = 'undefined';
if ( bubbles === void 0 ) bubbles = false;
if ( cancelable === void 0 ) cancelable = false;
this.type = "" + type;
this.bubbles = Boolean(bubbles);
this.cancelable = Boolean(cancelable);
};
var Event = (function (EventPrototype$$1) {
function Event(type, eventInitConfig) {
if ( eventInitConfig === void 0 ) eventInitConfig = {};
EventPrototype$$1.call(this);
if (!type) {
throw new TypeError(((ERROR_PREFIX.EVENT_ERROR) + " 1 argument required, but only 0 present."));
}
if (typeof eventInitConfig !== 'object') {
throw new TypeError(((ERROR_PREFIX.EVENT_ERROR) + " parameter 2 ('eventInitDict') is not an object."));
}
var bubbles = eventInitConfig.bubbles;
var cancelable = eventInitConfig.cancelable;
this.type = "" + type;
this.timeStamp = Date.now();
this.target = null;
this.srcElement = null;
this.returnValue = true;
this.isTrusted = false;
this.eventPhase = 0;
this.defaultPrevented = false;
this.currentTarget = null;
this.cancelable = cancelable ? Boolean(cancelable) : false;
this.cancelBubble = false;
this.bubbles = bubbles ? Boolean(bubbles) : false;
}
if ( EventPrototype$$1 ) Event.__proto__ = EventPrototype$$1;
Event.prototype = Object.create( EventPrototype$$1 && EventPrototype$$1.prototype );
Event.prototype.constructor = Event;
return Event;
}(EventPrototype));
var MessageEvent = (function (EventPrototype$$1) {
function MessageEvent(type, eventInitConfig) {
if ( eventInitConfig === void 0 ) eventInitConfig = {};
EventPrototype$$1.call(this);
if (!type) {
throw new TypeError(((ERROR_PREFIX.EVENT.MESSAGE) + " 1 argument required, but only 0 present."));
}
if (typeof eventInitConfig !== 'object') {
throw new TypeError(((ERROR_PREFIX.EVENT.MESSAGE) + " parameter 2 ('eventInitDict') is not an object"));
}
var bubbles = eventInitConfig.bubbles;
var cancelable = eventInitConfig.cancelable;
var data = eventInitConfig.data;
var origin = eventInitConfig.origin;
var lastEventId = eventInitConfig.lastEventId;
var ports = eventInitConfig.ports;
this.type = "" + type;
this.timeStamp = Date.now();
this.target = null;
this.srcElement = null;
this.returnValue = true;
this.isTrusted = false;
this.eventPhase = 0;
this.defaultPrevented = false;
this.currentTarget = null;
this.cancelable = cancelable ? Boolean(cancelable) : false;
this.canncelBubble = false;
this.bubbles = bubbles ? Boolean(bubbles) : false;
this.origin = "" + origin;
this.ports = typeof ports === 'undefined' ? null : ports;
this.data = typeof data === 'undefined' ? null : data;
this.lastEventId = "" + (lastEventId || '');
}
if ( EventPrototype$$1 ) MessageEvent.__proto__ = EventPrototype$$1;
MessageEvent.prototype = Object.create( EventPrototype$$1 && EventPrototype$$1.prototype );
MessageEvent.prototype.constructor = MessageEvent;
return MessageEvent;
}(EventPrototype));
var CloseEvent = (function (EventPrototype$$1) {
function CloseEvent(type, eventInitConfig) {
if ( eventInitConfig === void 0 ) eventInitConfig = {};
EventPrototype$$1.call(this);
if (!type) {
throw new TypeError(((ERROR_PREFIX.EVENT.CLOSE) + " 1 argument required, but only 0 present."));
}
if (typeof eventInitConfig !== 'object') {
throw new TypeError(((ERROR_PREFIX.EVENT.CLOSE) + " parameter 2 ('eventInitDict') is not an object"));
}
var bubbles = eventInitConfig.bubbles;
var cancelable = eventInitConfig.cancelable;
var code = eventInitConfig.code;
var reason = eventInitConfig.reason;
var wasClean = eventInitConfig.wasClean;
this.type = "" + type;
this.timeStamp = Date.now();
this.target = null;
this.srcElement = null;
this.returnValue = true;
this.isTrusted = false;
this.eventPhase = 0;
this.defaultPrevented = false;
this.currentTarget = null;
this.cancelable = cancelable ? Boolean(cancelable) : false;
this.cancelBubble = false;
this.bubbles = bubbles ? Boolean(bubbles) : false;
this.code = typeof code === 'number' ? parseInt(code, 10) : 0;
this.reason = "" + (reason || '');
this.wasClean = wasClean ? Boolean(wasClean) : false;
}
if ( EventPrototype$$1 ) CloseEvent.__proto__ = EventPrototype$$1;
CloseEvent.prototype = Object.create( EventPrototype$$1 && EventPrototype$$1.prototype );
CloseEvent.prototype.constructor = CloseEvent;
return CloseEvent;
}(EventPrototype));
/*
* Creates an Event object and extends it to allow full modification of
* its properties.
*
* @param {object} config - within config you will need to pass type and optionally target
*/
function createEvent(config) {
var type = config.type;
var target = config.target;
var eventObject = new Event(type);
if (target) {
eventObject.target = target;
eventObject.srcElement = target;
eventObject.currentTarget = target;
}
return eventObject;
}
/*
* Creates a MessageEvent object and extends it to allow full modification of
* its properties.
*
* @param {object} config - within config: type, origin, data and optionally target
*/
function createMessageEvent(config) {
var type = config.type;
var origin = config.origin;
var data = config.data;
var target = config.target;
var messageEvent = new MessageEvent(type, {
data: data,
origin: origin
});
if (target) {
messageEvent.target = target;
messageEvent.srcElement = target;
messageEvent.currentTarget = target;
}
return messageEvent;
}
/*
* Creates a CloseEvent object and extends it to allow full modification of
* its properties.
*
* @param {object} config - within config: type and optionally target, code, and reason
*/
function createCloseEvent(config) {
var code = config.code;
var reason = config.reason;
var type = config.type;
var target = config.target;
var wasClean = config.wasClean;
if (!wasClean) {
wasClean = code === CLOSE_CODES.CLOSE_NORMAL || code === CLOSE_CODES.CLOSE_NO_STATUS;
}
var closeEvent = new CloseEvent(type, {
code: code,
reason: reason,
wasClean: wasClean
});
if (target) {
closeEvent.target = target;
closeEvent.srcElement = target;
closeEvent.currentTarget = target;
}
return closeEvent;
}
function closeWebSocketConnection(context, code, reason) {
context.readyState = WebSocket$1.CLOSING;
var server = networkBridge.serverLookup(context.url);
var closeEvent = createCloseEvent({
type: 'close',
target: context.target,
code: code,
reason: reason
});
delay(function () {
networkBridge.removeWebSocket(context, context.url);
context.readyState = WebSocket$1.CLOSED;
context.dispatchEvent(closeEvent);
if (server) {
server.dispatchEvent(closeEvent, server);
}
}, context);
}
function failWebSocketConnection(context, code, reason) {
context.readyState = WebSocket$1.CLOSING;
var server = networkBridge.serverLookup(context.url);
var closeEvent = createCloseEvent({
type: 'close',
target: context.target,
code: code,
reason: reason,
wasClean: false
});
var errorEvent = createEvent({
type: 'error',
target: context.target
});
delay(function () {
networkBridge.removeWebSocket(context, context.url);
context.readyState = WebSocket$1.CLOSED;
context.dispatchEvent(errorEvent);
context.dispatchEvent(closeEvent);
if (server) {
server.dispatchEvent(closeEvent, server);
}
}, context);
}
function normalizeSendData(data) {
if (Object.prototype.toString.call(data) !== '[object Blob]' && !(data instanceof ArrayBuffer)) {
data = String(data);
}
return data;
}
var proxies = new WeakMap();
function proxyFactory(target) {
if (proxies.has(target)) {
return proxies.get(target);
}
var proxy = new Proxy(target, {
get: function get(obj, prop) {
if (prop === 'close') {
return function close(options) {
if ( options === void 0 ) options = {};
var code = options.code || CLOSE_CODES.CLOSE_NORMAL;
var reason = options.reason || '';
closeWebSocketConnection(proxy, code, reason);
};
}
if (prop === 'send') {
return function send(data) {
data = normalizeSendData(data);
target.dispatchEvent(
createMessageEvent({
type: 'message',
data: data,
origin: this.url,
target: target
})
);
};
}
var toSocketName = function (type) { return (type === 'message' ? ("server::" + type) : type); };
if (prop === 'on') {
return function onWrapper(type, cb) {
target.addEventListener(toSocketName(type), cb);
};
}
if (prop === 'off') {
return function offWrapper(type, cb) {
target.removeEventListener(toSocketName(type), cb);
};
}
if (prop === 'target') {
return target;
}
return obj[prop];
}
});
proxies.set(target, proxy);
return proxy;
}
function lengthInUtf8Bytes(str) {
// Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.
var m = encodeURIComponent(str).match(/%[89ABab]/g);
return str.length + (m ? m.length : 0);
}
function urlVerification(url) {
var urlRecord = new urlParse(url);
var pathname = urlRecord.pathname;
var protocol = urlRecord.protocol;
var hash = urlRecord.hash;
if (!url) {
throw new TypeError(((ERROR_PREFIX.CONSTRUCTOR_ERROR) + " 1 argument required, but only 0 present."));
}
if (!pathname) {
urlRecord.pathname = '/';
}
if (protocol === '') {
throw new SyntaxError(((ERROR_PREFIX.CONSTRUCTOR_ERROR) + " The URL '" + (urlRecord.toString()) + "' is invalid."));
}
if (protocol !== 'ws:' && protocol !== 'wss:') {
throw new SyntaxError(
((ERROR_PREFIX.CONSTRUCTOR_ERROR) + " The URL's scheme must be either 'ws' or 'wss'. '" + protocol + "' is not allowed.")
);
}
if (hash !== '') {
/* eslint-disable max-len */
throw new SyntaxError(
((ERROR_PREFIX.CONSTRUCTOR_ERROR) + " The URL contains a fragment identifier ('" + hash + "'). Fragment identifiers are not allowed in WebSocket URLs.")
);
/* eslint-enable max-len */
}
return urlRecord.toString();
}
function protocolVerification(protocols) {
if ( protocols === void 0 ) protocols = [];
if (!Array.isArray(protocols) && typeof protocols !== 'string') {
throw new SyntaxError(((ERROR_PREFIX.CONSTRUCTOR_ERROR) + " The subprotocol '" + (protocols.toString()) + "' is invalid."));
}
if (typeof protocols === 'string') {
protocols = [protocols];
}
var uniq = protocols
.map(function (p) { return ({ count: 1, protocol: p }); })
.reduce(function (a, b) {
a[b.protocol] = (a[b.protocol] || 0) + b.count;
return a;
}, {});
var duplicates = Object.keys(uniq).filter(function (a) { return uniq[a] > 1; });
if (duplicates.length > 0) {
throw new SyntaxError(((ERROR_PREFIX.CONSTRUCTOR_ERROR) + " The subprotocol '" + (duplicates[0]) + "' is duplicated."));
}
return protocols;
}
/*
* The main websocket class which is designed to mimick the native WebSocket class as close
* as possible.
*
* https://html.spec.whatwg.org/multipage/web-sockets.html
*/
var WebSocket$1 = (function (EventTarget$$1) {
function WebSocket(url, protocols) {
EventTarget$$1.call(this);
this._onopen = null;
this._onmessage = null;
this._onerror = null;
this._onclose = null;
this.url = urlVerification(url);
protocols = protocolVerification(protocols);
this.protocol = protocols[0] || '';
this.binaryType = 'blob';
this.readyState = WebSocket.CONNECTING;
var client = proxyFactory(this);
var server = networkBridge.attachWebSocket(client, this.url);
/*
* This delay is needed so that we dont trigger an event before the callbacks have been
* setup. For example:
*
* var socket = new WebSocket('ws://localhost');
*
* If we dont have the delay then the event would be triggered right here and this is
* before the onopen had a chance to register itself.
*
* socket.onopen = () => { // this would never be called };
*
* and with the delay the event gets triggered here after all of the callbacks have been
* registered :-)
*/
delay(function delayCallback() {
if (server) {
if (
server.options.verifyClient &&
typeof server.options.verifyClient === 'function' &&
!server.options.verifyClient()
) {
this.readyState = WebSocket.CLOSED;
log(
'error',
("WebSocket connection to '" + (this.url) + "' failed: HTTP Authentication failed; no valid credentials available")
);
networkBridge.removeWebSocket(client, this.url);
this.dispatchEvent(createEvent({ type: 'error', target: this }));
this.dispatchEvent(createCloseEvent({ type: 'close', target: this, code: CLOSE_CODES.CLOSE_NORMAL }));
} else {
if (server.options.selectProtocol && typeof server.options.selectProtocol === 'function') {
var selectedProtocol = server.options.selectProtocol(protocols);
var isFilled = selectedProtocol !== '';
var isRequested = protocols.indexOf(selectedProtocol) !== -1;
if (isFilled && !isRequested) {
this.readyState = WebSocket.CLOSED;
log('error', ("WebSocket connection to '" + (this.url) + "' failed: Invalid Sub-Protocol"));
networkBridge.removeWebSocket(client, this.url);
this.dispatchEvent(createEvent({ type: 'error', target: this }));
this.dispatchEvent(createCloseEvent({ type: 'close', target: this, code: CLOSE_CODES.CLOSE_NORMAL }));
return;
}
this.protocol = selectedProtocol;
}
this.readyState = WebSocket.OPEN;
this.dispatchEvent(createEvent({ type: 'open', target: this }));
server.dispatchEvent(createEvent({ type: 'connection' }), client);
}
} else {
this.readyState = WebSocket.CLOSED;
this.dispatchEvent(createEvent({ type: 'error', target: this }));
this.dispatchEvent(createCloseEvent({ type: 'close', target: this, code: CLOSE_CODES.CLOSE_NORMAL }));
log('error', ("WebSocket connection to '" + (this.url) + "' failed"));
}
}, this);
}
if ( EventTarget$$1 ) WebSocket.__proto__ = EventTarget$$1;
WebSocket.prototype = Object.create( EventTarget$$1 && EventTarget$$1.prototype );
WebSocket.prototype.constructor = WebSocket;
var prototypeAccessors = { onopen: {},onmessage: {},onclose: {},onerror: {} };
prototypeAccessors.onopen.get = function () {
return this._onopen;
};
prototypeAccessors.onmessage.get = function () {
return this._onmessage;
};
prototypeAccessors.onclose.get = function () {
return this._onclose;
};
prototypeAccessors.onerror.get = function () {
return this._onerror;
};
prototypeAccessors.onopen.set = function (listener) {
this.removeEventListener('open', this._onopen);
this._onopen = listener;
this.addEventListener('open', listener);
};
prototypeAccessors.onmessage.set = function (listener) {
this.removeEventListener('message', this._onmessage);
this._onmessage = listener;
this.addEventListener('message', listener);
};
prototypeAccessors.onclose.set = function (listener) {
this.removeEventListener('close', this._onclose);
this._onclose = listener;
this.addEventListener('close', listener);
};
prototypeAccessors.onerror.set = function (listener) {
this.removeEventListener('error', this._onerror);
this._onerror = listener;
this.addEventListener('error', listener);
};
WebSocket.prototype.send = function send (data) {
var this$1 = this;
if (this.readyState === WebSocket.CLOSING || this.readyState === WebSocket.CLOSED) {
throw new Error('WebSocket is already in CLOSING or CLOSED state');
}
// TODO: handle bufferedAmount
var messageEvent = createMessageEvent({
type: 'server::message',
origin: this.url,
data: normalizeSendData(data)
});
var server = networkBridge.serverLookup(this.url);
if (server) {
delay(function () {
this$1.dispatchEvent(messageEvent, data);
}, server);
}
};
WebSocket.prototype.close = function close (code, reason) {
if (code !== undefined) {
if (typeof code !== 'number' || (code !== 1000 && (code < 3000 || code > 4999))) {
throw new TypeError(
((ERROR_PREFIX.CLOSE_ERROR) + " The code must be either 1000, or between 3000 and 4999. " + code + " is neither.")
);
}
}
if (reason !== undefined) {
var length = lengthInUtf8Bytes(reason);
if (length > 123) {
throw new SyntaxError(((ERROR_PREFIX.CLOSE_ERROR) + " The message must not be greater than 123 bytes."));
}
}
if (this.readyState === WebSocket.CLOSING || this.readyState === WebSocket.CLOSED) {
return;
}
var client = proxyFactory(this);
if (this.readyState === WebSocket.CONNECTING) {
failWebSocketConnection(client, code || CLOSE_CODES.CLOSE_ABNORMAL, reason);
} else {
closeWebSocketConnection(client, code || CLOSE_CODES.CLOSE_NO_STATUS, reason);
}
};
Object.defineProperties( WebSocket.prototype, prototypeAccessors );
return WebSocket;
}(EventTarget));
WebSocket$1.CONNECTING = 0;
WebSocket$1.prototype.CONNECTING = WebSocket$1.CONNECTING;
WebSocket$1.OPEN = 1;
WebSocket$1.prototype.OPEN = WebSocket$1.OPEN;
WebSocket$1.CLOSING = 2;
WebSocket$1.prototype.CLOSING = WebSocket$1.CLOSING;
WebSocket$1.CLOSED = 3;
WebSocket$1.prototype.CLOSED = WebSocket$1.CLOSED;
/*
* The socket-io class is designed to mimick the real API as closely as possible.
*
* http://socket.io/docs/
*/
var SocketIO$1 = (function (EventTarget$$1) {
function SocketIO(url, protocol) {
var this$1 = this;
if ( url === void 0 ) url = 'socket.io';
if ( protocol === void 0 ) protocol = '';
EventTarget$$1.call(this);
this.binaryType = 'blob';
var urlRecord = new urlParse(url);
if (!urlRecord.pathname) {
urlRecord.pathname = '/';
}
this.url = urlRecord.toString();
this.readyState = SocketIO.CONNECTING;
this.protocol = '';
this.target = this;
if (typeof protocol === 'string' || (typeof protocol === 'object' && protocol !== null)) {
this.protocol = protocol;
} else if (Array.isArray(protocol) && protocol.length > 0) {
this.protocol = protocol[0];
}
var server = networkBridge.attachWebSocket(this, this.url);
/*
* Delay triggering the connection events so they can be defined in time.
*/
delay(function delayCallback() {
if (server) {
this.readyState = SocketIO.OPEN;
server.dispatchEvent(createEvent({ type: 'connection' }), server, this);
server.dispatchEvent(createEvent({ type: 'connect' }), server, this); // alias
this.dispatchEvent(createEvent({ type: 'connect', target: this }));
} else {
this.readyState = SocketIO.CLOSED;
this.dispatchEvent(createEvent({ type: 'error', target: this }));
this.dispatchEvent(
createCloseEvent({
type: 'close',
target: this,
code: CLOSE_CODES.CLOSE_NORMAL
})
);
log('error', ("Socket.io connection to '" + (this.url) + "' failed"));
}
}, this);
/**
Add an aliased event listener for close / disconnect
*/
this.addEventListener('close', function (event) {
this$1.dispatchEvent(
createCloseEvent({
type: 'disconnect',
target: event.target,
code: event.code
})
);
});
}
if ( EventTarget$$1 ) SocketIO.__proto__ = EventTarget$$1;
SocketIO.prototype = Object.create( EventTarget$$1 && EventTarget$$1.prototype );
SocketIO.prototype.constructor = SocketIO;
var prototypeAccessors = { broadcast: {} };
/*
* Closes the SocketIO connection or connection attempt, if any.
* If the connection is already CLOSED, this method does nothing.
*/
SocketIO.prototype.close = function close () {
if (this.readyState !== SocketIO.OPEN) {
return undefined;
}
var server = networkBridge.serverLookup(this.url);
networkBridge.removeWebSocket(this, this.url);
this.readyState = SocketIO.CLOSED;
this.dispatchEvent(
createCloseEvent({
type: 'close',
target: this,
code: CLOSE_CODES.CLOSE_NORMAL
})
);
if (server) {
server.dispatchEvent(
createCloseEvent({
type: 'disconnect',
target: this,
code: CLOSE_CODES.CLOSE_NORMAL
}),
server
);
}
return this;
};
/*
* Alias for Socket#close
*
* https://github.com/socketio/socket.io-client/blob/master/lib/socket.js#L383
*/
SocketIO.prototype.disconnect = function disconnect () {
return this.close();
};
/*
* Submits an event to the server with a payload
*/
SocketIO.prototype.emit = function emit (event) {
var data = [], len = arguments.length - 1;
while ( len-- > 0 ) data[ len ] = arguments[ len + 1 ];
if (this.readyState !== SocketIO.OPEN) {
throw new Error('SocketIO is already in CLOSING or CLOSED state');
}
var messageEvent = createMessageEvent({
type: event,
origin: this.url,
data: data
});
var server = networkBridge.serverLookup(this.url);
if (server) {
server.dispatchEvent.apply(server, [ messageEvent ].concat( data ));
}
return this;
};
/*
* Submits a 'message' event to the server.
*
* Should behave exactly like WebSocket#send
*
* https://github.com/socketio/socket.io-client/blob/master/lib/socket.js#L113
*/
SocketIO.prototype.send = function send (data) {
this.emit('message', data);
return this;
};
/*
* For broadcasting events to other connected sockets.
*
* e.g. socket.broadcast.emit('hi!');
* e.g. socket.broadcast.to('my-room').emit('hi!');
*/
prototypeAccessors.broadcast.get = function () {
if (this.readyState !== SocketIO.OPEN) {
throw new Error('SocketIO is already in CLOSING or CLOSED state');
}
var self = this;
var server = networkBridge.serverLookup(this.url);
if (!server) {
throw new Error(("SocketIO can not find a server at the specified URL (" + (this.url) + ")"));
}
return {
emit: function emit(event, data) {
server.emit(event, data, { websockets: networkBridge.websocketsLookup(self.url, null, self) });
return self;
},
to: function to(room) {
return server.to(room, self);
},
in: function in$1(room) {
return server.in(room, self);
}
};
};
/*
* For registering events to be received from the server
*/
SocketIO.prototype.on = function on (type, callback) {
this.addEventListener(type, callback);
return this;
};
/*
* Remove event listener
*
* https://github.com/component/emitter#emitteroffevent-fn
*/
SocketIO.prototype.off = function off (type, callback) {
this.removeEventListener(type, callback);
};
/*
* Check if listeners have already been added for an event
*
* https://github.com/component/emitter#emitterhaslistenersevent
*/
SocketIO.prototype.hasListeners = function hasListeners (type) {
var listeners = this.listeners[type];
if (!Array.isArray(listeners)) {
return false;
}
return !!listeners.length;
};
/*
* Join a room on a server
*
* http://socket.io/docs/rooms-and-namespaces/#joining-and-leaving
*/
SocketIO.prototype.join = function join (room) {
networkBridge.addMembershipToRoom(this, room);
};
/*
* Get the websocket to leave the room
*
* http://socket.io/docs/rooms-and-namespaces/#joining-and-leaving
*/
SocketIO.prototype.leave = function leave (room) {
networkBridge.removeMembershipFromRoom(this, room);
};
SocketIO.prototype.to = function to (room) {
return this.broadcast.to(room);
};
SocketIO.prototype.in = function in$1 () {
return this.to.apply(null, arguments);
};
/*
* Invokes all listener functions that are listening to the given event.type property. Each
* listener will be passed the event as the first argument.
*
* @param {object} event - event object which will be passed to all listeners of the event.type property
*/
SocketIO.prototype.dispatchEvent = function dispatchEvent (event) {
var this$1 = this;
var customArguments = [], len = arguments.length - 1;
while ( len-- > 0 ) customArguments[ len ] = arguments[ len + 1 ];
var eventName = event.type;
var listeners = this.listeners[eventName];
if (!Array.isArray(listeners)) {
return false;
}
listeners.forEach(function (listener) {
if (customArguments.length > 0) {
listener.apply(this$1, customArguments);
} else {
// Regular WebSockets expect a MessageEvent but Socketio.io just wants raw data
// payload instanceof MessageEvent works, but you can't isntance of NodeEvent
// for now we detect if the output has data defined on it
listener.call(this$1, event.data ? event.data : event);
}
});
};
Object.defineProperties( SocketIO.prototype, prototypeAccessors );
return SocketIO;
}(EventTarget));
SocketIO$1.CONNECTING = 0;
SocketIO$1.OPEN = 1;
SocketIO$1.CLOSING = 2;
SocketIO$1.CLOSED = 3;
/*
* Static constructor methods for the IO Socket
*/
var IO = function ioConstructor(url, protocol) {
return new SocketIO$1(url, protocol);
};
/*
* Alias the raw IO() constructor
*/
IO.connect = function ioConnect(url, protocol) {
/* eslint-disable new-cap */
return IO(url, protocol);
/* eslint-enable new-cap */
};
var dedupe = function (arr) { return arr.reduce(function (deduped, b) {
if (deduped.indexOf(b) > -1) { return deduped; }
return deduped.concat(b);
}, []); };
function retrieveGlobalObject() {
if (typeof window !== 'undefined') {
return window;
}
return typeof process === 'object' && typeof require === 'function' && typeof global === 'object' ? global : this;
}
var defaultOptions = {
mock: true,
verifyClient: null,
selectProtocol: null
};
var Server$1 = (function (EventTarget$$1) {
function Server(url, options) {
if ( options === void 0 ) options = defaultOptions;
EventTarget$$1.call(this);
var urlRecord = new urlParse(url);
if (!urlRecord.pathname) {
urlRecord.pathname = '/';
}
this.url = urlRecord.toString();
this.originalWebSocket = null;
var server = networkBridge.attachServer(this, this.url);
if (!server) {
this.dispatchEvent(createEvent({ type: 'error' }));
throw new Error('A mock server is already listening on this url');
}
this.options = Object.assign({}, defaultOptions, options);
if (this.options.mock) {
this.mockWebsocket();
}
}
if ( EventTarget$$1 ) Server.__proto__ = EventTarget$$1;
Server.prototype = Object.create( EventTarget$$1 && EventTarget$$1.prototype );
Server.prototype.constructor = Server;
/*
* Attaches the mock websocket object to the global object
*/
Server.prototype.mockWebsocket = function mockWebsocket () {
var globalObj = retrieveGlobalObject();
this.originalWebSocket = globalObj.WebSocket;
globalObj.WebSocket = WebSocket$1;
};
/*
* Removes the mock websocket object from the global object
*/
Server.prototype.restoreWebsocket = function restoreWebsocket () {
var globalObj = retrieveGlobalObject();
if (this.originalWebSocket !== null) {
globalObj.WebSocket = this.originalWebSocket;
}
this.originalWebSocket = null;
};
/**
* Removes itself from the urlMap so another server could add itself to the url.
* @param {function} callback - The callback is called when the server is stopped
*/
Server.prototype.stop = function stop (callback) {
if ( callback === void 0 ) callback = function () {};
if (this.options.mock) {
this.restoreWebsocket();
}
networkBridge.removeServer(this.url);
if (typeof callback === 'function') {
callback();
}
};
/*
* This is the main function for the mock server to subscribe to the on events.
*
* ie: mockServer.on('connection', function() { console.log('a mock client connected'); });
*
* @param {string} type - The event key to subscribe to. Valid keys are: connection, message, and close.
* @param {function} callback - The callback which should be called when a certain event is fired.
*/
Server.prototype.on = function on (type, callback) {
this.addEventListener(type, callback);
};
/*
* Remove event listener
*/
Server.prototype.off = function off (type, callback) {
this.removeEventListener(type, callback);
};
/*
* Closes the connection and triggers the onclose method of all listening
* websockets. After that it removes itself from the urlMap so another server
* could add itself to the url.
*
* @param {object} options
*/
Server.prototype.close = function close (options) {
if ( options === void 0 ) options = {};
var code = options.code;
var reason = options.reason;
var wasClean = options.wasClean;
var listeners = networkBridge.websocketsLookup(this.url);
// Remove server before notifications to prevent immediate reconnects from
// socket onclose handlers
networkBridge.removeServer(this.url);
listeners.forEach(function (socket) {
socket.readyState = WebSocket$1.CLOSED;
socket.dispatchEvent(
createCloseEvent({
type: 'close',
target: socket.target,
code: code || CLOSE_CODES.CLOSE_NORMAL,
reason: reason || '',
wasClean: wasClean
})
);
});
this.dispatchEvent(createCloseEvent({ type: 'close' }), this);
};
/*
* Sends a generic message event to all mock clients.
*/
Server.prototype.emit = function emit (event, data, options) {
var this$1 = this;
if ( options === void 0 ) options = {};
var websockets = options.websockets;
if (!websockets) {
websockets = networkBridge.websocketsLookup(this.url);
}
var normalizedData;
if (typeof options !== 'object' || arguments.length > 3) {
data = Array.prototype.slice.call(arguments, 1, arguments.length);
normalizedData = data.map(function (item) { return normalizeSendData(item); });
} else {
normalizedData = normalizeSendData(data);
}
websockets.forEach(function (socket) {
var messageData = socket instanceof SocketIO$1 ? data : normalizedData;
if (Array.isArray(messageData)) {
socket.dispatchEvent.apply(
socket, [ createMessageEvent({
type: event,
data: messageData,
origin: this$1.url,
target: socket.target
}) ].concat( messageData )
);
} else {
socket.dispatchEvent(
createMessageEvent({
type: event,
data: messageData,
origin: this$1.url,
target: socket.target
})
);
}
});
};
/*
* Returns an array of websockets which are listening to this server
* TOOD: this should return a set and not be a method
*/
Server.prototype.clients = function clients () {
return networkBridge.websocketsLookup(this.url);
};
/*
* Prepares a method to submit an event to members of the room
*
* e.g. server.to('my-room').emit('hi!');
*/
Server.prototype.to = function to (room, broadcaster, broadcastList) {
var this$1 = this;
if ( broadcastList === void 0 ) broadcastList = [];
var self = this;
var websockets = dedupe(broadcastList.concat(networkBridge.websocketsLookup(this.url, room, broadcaster)));
return {
to: function (chainedRoom, chainedBroadcaster) { return this$1.to.call(this$1, chainedRoom, chainedBroadcaster, websockets); },
emit: function emit(event, data) {
self.emit(event, data, { websockets: websockets });
}
};
};
/*
* Alias for Server.to
*/
Server.prototype.in = function in$1 () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return this.to.apply(null, args);
};
/*
* Simulate an event from the server to the clients. Useful for
* simulating errors.
*/
Server.prototype.simulate = function simulate (event) {
var listeners = networkBridge.websocketsLookup(this.url);
if (event === 'error') {
listeners.forEach(function (socket) {
socket.readyState = WebSocket$1.CLOSED;
socket.dispatchEvent(createEvent({ type: 'error', target: socket.target }));
});
}
};
return Server;
}(EventTarget));
/*
* Alternative constructor to support namespaces in socket.io
*
* http://socket.io/docs/rooms-and-namespaces/#custom-namespaces
*/
Server$1.of = function of(url) {
return new Server$1(url);
};
var Server = Server$1;
var WebSocket = WebSocket$1;
var SocketIO$$1 = IO;
exports.Server = Server;
exports.WebSocket = WebSocket;
exports.SocketIO = SocketIO$$1;
Object.defineProperty(exports, '__esModule', { value: true });
})));
|