Newer
Older
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
writable: true,
value: 0
});
Cookie.serializableProperties = Object.keys(Cookie.prototype)
.filter(function(prop) {
return !(
Cookie.prototype[prop] instanceof Function ||
prop === 'creationIndex' ||
prop.substr(0,1) === '_'
);
});
Cookie.prototype.inspect = function inspect() {
var now = Date.now();
return 'Cookie="'+this.toString() +
'; hostOnly='+(this.hostOnly != null ? this.hostOnly : '?') +
'; aAge='+(this.lastAccessed ? (now-this.lastAccessed.getTime())+'ms' : '?') +
'; cAge='+(this.creation ? (now-this.creation.getTime())+'ms' : '?') +
'"';
};
// Use the new custom inspection symbol to add the custom inspect function if
// available.
if (util.inspect.custom) {
Cookie.prototype[util.inspect.custom] = Cookie.prototype.inspect;
}
Cookie.prototype.toJSON = function() {
var obj = {};
var props = Cookie.serializableProperties;
for (var i=0; i<props.length; i++) {
var prop = props[i];
if (this[prop] === Cookie.prototype[prop]) {
continue; // leave as prototype default
}
if (prop === 'expires' ||
prop === 'creation' ||
prop === 'lastAccessed')
{
if (this[prop] === null) {
obj[prop] = null;
} else {
obj[prop] = this[prop] == "Infinity" ? // intentionally not ===
"Infinity" : this[prop].toISOString();
}
} else if (prop === 'maxAge') {
if (this[prop] !== null) {
// again, intentionally not ===
obj[prop] = (this[prop] == Infinity || this[prop] == -Infinity) ?
this[prop].toString() : this[prop];
}
} else {
if (this[prop] !== Cookie.prototype[prop]) {
obj[prop] = this[prop];
}
}
}
return obj;
};
Cookie.prototype.clone = function() {
return fromJSON(this.toJSON());
};
Cookie.prototype.validate = function validate() {
if (!COOKIE_OCTETS.test(this.value)) {
return false;
}
if (this.expires != Infinity && !(this.expires instanceof Date) && !parseDate(this.expires)) {
return false;
}
if (this.maxAge != null && this.maxAge <= 0) {
return false; // "Max-Age=" non-zero-digit *DIGIT
}
if (this.path != null && !PATH_VALUE.test(this.path)) {
return false;
}
var cdomain = this.cdomain();
if (cdomain) {
if (cdomain.match(/\.$/)) {
return false; // S4.1.2.3 suggests that this is bad. domainMatch() tests confirm this
}
var suffix = pubsuffix.getPublicSuffix(cdomain);
if (suffix == null) { // it's a public suffix
return false;
}
}
return true;
};
Cookie.prototype.setExpires = function setExpires(exp) {
if (exp instanceof Date) {
this.expires = exp;
} else {
this.expires = parseDate(exp) || "Infinity";
}
};
Cookie.prototype.setMaxAge = function setMaxAge(age) {
if (age === Infinity || age === -Infinity) {
this.maxAge = age.toString(); // so JSON.stringify() works
} else {
this.maxAge = age;
}
};
// gives Cookie header format
Cookie.prototype.cookieString = function cookieString() {
var val = this.value;
if (val == null) {
val = '';
}
if (this.key === '') {
return val;
}
return this.key+'='+val;
};
// gives Set-Cookie header format
Cookie.prototype.toString = function toString() {
var str = this.cookieString();
if (this.expires != Infinity) {
if (this.expires instanceof Date) {
str += '; Expires='+formatDate(this.expires);
} else {
str += '; Expires='+this.expires;
}
}
if (this.maxAge != null && this.maxAge != Infinity) {
str += '; Max-Age='+this.maxAge;
}
if (this.domain && !this.hostOnly) {
str += '; Domain='+this.domain;
}
if (this.path) {
str += '; Path='+this.path;
}
if (this.secure) {
str += '; Secure';
}
if (this.httpOnly) {
str += '; HttpOnly';
}
if (this.extensions) {
this.extensions.forEach(function(ext) {
str += '; '+ext;
});
}
return str;
};
// TTL() partially replaces the "expiry-time" parts of S5.3 step 3 (setCookie()
// elsewhere)
// S5.3 says to give the "latest representable date" for which we use Infinity
// For "expired" we use 0
Cookie.prototype.TTL = function TTL(now) {
/* RFC6265 S4.1.2.2 If a cookie has both the Max-Age and the Expires
* attribute, the Max-Age attribute has precedence and controls the
* expiration date of the cookie.
* (Concurs with S5.3 step 3)
*/
if (this.maxAge != null) {
return this.maxAge<=0 ? 0 : this.maxAge*1000;
}
var expires = this.expires;
if (expires != Infinity) {
if (!(expires instanceof Date)) {
expires = parseDate(expires) || Infinity;
}
if (expires == Infinity) {
return Infinity;
}
return expires.getTime() - (now || Date.now());
}
return Infinity;
};
// expiryTime() replaces the "expiry-time" parts of S5.3 step 3 (setCookie()
// elsewhere)
Cookie.prototype.expiryTime = function expiryTime(now) {
if (this.maxAge != null) {
var relativeTo = now || this.creation || new Date();
var age = (this.maxAge <= 0) ? -Infinity : this.maxAge*1000;
return relativeTo.getTime() + age;
}
if (this.expires == Infinity) {
return Infinity;
}
return this.expires.getTime();
};
// expiryDate() replaces the "expiry-time" parts of S5.3 step 3 (setCookie()
// elsewhere), except it returns a Date
Cookie.prototype.expiryDate = function expiryDate(now) {
var millisec = this.expiryTime(now);
if (millisec == Infinity) {
return new Date(MAX_TIME);
} else if (millisec == -Infinity) {
return new Date(MIN_TIME);
} else {
return new Date(millisec);
}
};
// This replaces the "persistent-flag" parts of S5.3 step 3
Cookie.prototype.isPersistent = function isPersistent() {
return (this.maxAge != null || this.expires != Infinity);
};
// Mostly S5.1.2 and S5.2.3:
Cookie.prototype.cdomain =
Cookie.prototype.canonicalizedDomain = function canonicalizedDomain() {
if (this.domain == null) {
return null;
}
return canonicalDomain(this.domain);
};
function CookieJar(store, options) {
if (typeof options === "boolean") {
options = {rejectPublicSuffixes: options};
} else if (options == null) {
options = {};
}
if (options.rejectPublicSuffixes != null) {
this.rejectPublicSuffixes = options.rejectPublicSuffixes;
}
if (options.looseMode != null) {
this.enableLooseMode = options.looseMode;
}
if (!store) {
store = new MemoryCookieStore();
}
this.store = store;
}
CookieJar.prototype.store = null;
CookieJar.prototype.rejectPublicSuffixes = true;
CookieJar.prototype.enableLooseMode = false;
var CAN_BE_SYNC = [];
CAN_BE_SYNC.push('setCookie');
CookieJar.prototype.setCookie = function(cookie, url, options, cb) {
var err;
var context = getCookieContext(url);
if (options instanceof Function) {
cb = options;
options = {};
}
var host = canonicalDomain(context.hostname);
var loose = this.enableLooseMode;
if (options.loose != null) {
loose = options.loose;
}
// S5.3 step 1
if (!(cookie instanceof Cookie)) {
cookie = Cookie.parse(cookie, { loose: loose });
}
if (!cookie) {
err = new Error("Cookie failed to parse");
return cb(options.ignoreError ? null : err);
}
// S5.3 step 2
var now = options.now || new Date(); // will assign later to save effort in the face of errors
// S5.3 step 3: NOOP; persistent-flag and expiry-time is handled by getCookie()
// S5.3 step 4: NOOP; domain is null by default
// S5.3 step 5: public suffixes
if (this.rejectPublicSuffixes && cookie.domain) {
var suffix = pubsuffix.getPublicSuffix(cookie.cdomain());
if (suffix == null) { // e.g. "com"
err = new Error("Cookie has domain set to a public suffix");
return cb(options.ignoreError ? null : err);
}
}
// S5.3 step 6:
if (cookie.domain) {
if (!domainMatch(host, cookie.cdomain(), false)) {
err = new Error("Cookie not in this host's domain. Cookie:"+cookie.cdomain()+" Request:"+host);
return cb(options.ignoreError ? null : err);
}
if (cookie.hostOnly == null) { // don't reset if already set
cookie.hostOnly = false;
}
} else {
cookie.hostOnly = true;
cookie.domain = host;
}
//S5.2.4 If the attribute-value is empty or if the first character of the
//attribute-value is not %x2F ("/"):
//Let cookie-path be the default-path.
if (!cookie.path || cookie.path[0] !== '/') {
cookie.path = defaultPath(context.pathname);
cookie.pathIsDefault = true;
}
// S5.3 step 8: NOOP; secure attribute
// S5.3 step 9: NOOP; httpOnly attribute
// S5.3 step 10
if (options.http === false && cookie.httpOnly) {
err = new Error("Cookie is HttpOnly and this isn't an HTTP API");
return cb(options.ignoreError ? null : err);
}
var store = this.store;
if (!store.updateCookie) {
store.updateCookie = function(oldCookie, newCookie, cb) {
this.putCookie(newCookie, cb);
};
}
function withCookie(err, oldCookie) {
if (err) {
return cb(err);
}
var next = function(err) {
if (err) {
return cb(err);
} else {
cb(null, cookie);
}
};
if (oldCookie) {
// S5.3 step 11 - "If the cookie store contains a cookie with the same name,
// domain, and path as the newly created cookie:"
if (options.http === false && oldCookie.httpOnly) { // step 11.2
err = new Error("old Cookie is HttpOnly and this isn't an HTTP API");
return cb(options.ignoreError ? null : err);
}
cookie.creation = oldCookie.creation; // step 11.3
cookie.creationIndex = oldCookie.creationIndex; // preserve tie-breaker
cookie.lastAccessed = now;
// Step 11.4 (delete cookie) is implied by just setting the new one:
store.updateCookie(oldCookie, cookie, next); // step 12
} else {
cookie.creation = cookie.lastAccessed = now;
store.putCookie(cookie, next); // step 12
}
}
store.findCookie(cookie.domain, cookie.path, cookie.key, withCookie);
};
// RFC6365 S5.4
CAN_BE_SYNC.push('getCookies');
CookieJar.prototype.getCookies = function(url, options, cb) {
var context = getCookieContext(url);
if (options instanceof Function) {
cb = options;
options = {};
}
var host = canonicalDomain(context.hostname);
var path = context.pathname || '/';
var secure = options.secure;
if (secure == null && context.protocol &&
(context.protocol == 'https:' || context.protocol == 'wss:'))
{
secure = true;
}
var http = options.http;
if (http == null) {
http = true;
}
var now = options.now || Date.now();
var expireCheck = options.expire !== false;
var allPaths = !!options.allPaths;
var store = this.store;
function matchingCookie(c) {
// "Either:
// The cookie's host-only-flag is true and the canonicalized
// request-host is identical to the cookie's domain.
// Or:
// The cookie's host-only-flag is false and the canonicalized
// request-host domain-matches the cookie's domain."
if (c.hostOnly) {
if (c.domain != host) {
return false;
}
} else {
if (!domainMatch(host, c.domain, false)) {
return false;
}
}
// "The request-uri's path path-matches the cookie's path."
if (!allPaths && !pathMatch(path, c.path)) {
return false;
}
// "If the cookie's secure-only-flag is true, then the request-uri's
// scheme must denote a "secure" protocol"
if (c.secure && !secure) {
return false;
}
// "If the cookie's http-only-flag is true, then exclude the cookie if the
// cookie-string is being generated for a "non-HTTP" API"
if (c.httpOnly && !http) {
return false;
}
// deferred from S5.3
// non-RFC: allow retention of expired cookies by choice
if (expireCheck && c.expiryTime() <= now) {
store.removeCookie(c.domain, c.path, c.key, function(){}); // result ignored
return false;
}
return true;
}
store.findCookies(host, allPaths ? null : path, function(err,cookies) {
if (err) {
return cb(err);
}
cookies = cookies.filter(matchingCookie);
// sorting of S5.4 part 2
if (options.sort !== false) {
cookies = cookies.sort(cookieCompare);
}
// S5.4 part 3
var now = new Date();
cookies.forEach(function(c) {
c.lastAccessed = now;
});
// TODO persist lastAccessed
cb(null,cookies);
});
};
CAN_BE_SYNC.push('getCookieString');
CookieJar.prototype.getCookieString = function(/*..., cb*/) {
var args = Array.prototype.slice.call(arguments,0);
var cb = args.pop();
var next = function(err,cookies) {
if (err) {
cb(err);
} else {
cb(null, cookies
.sort(cookieCompare)
.map(function(c){
return c.cookieString();
})
.join('; '));
}
};
args.push(next);
this.getCookies.apply(this,args);
};
CAN_BE_SYNC.push('getSetCookieStrings');
CookieJar.prototype.getSetCookieStrings = function(/*..., cb*/) {
var args = Array.prototype.slice.call(arguments,0);
var cb = args.pop();
var next = function(err,cookies) {
if (err) {
cb(err);
} else {
cb(null, cookies.map(function(c){
return c.toString();
}));
}
};
args.push(next);
this.getCookies.apply(this,args);
};
CAN_BE_SYNC.push('serialize');
CookieJar.prototype.serialize = function(cb) {
var type = this.store.constructor.name;
if (type === 'Object') {
type = null;
}
// update README.md "Serialization Format" if you change this, please!
var serialized = {
// The version of tough-cookie that serialized this jar. Generally a good
// practice since future versions can make data import decisions based on
// known past behavior. When/if this matters, use `semver`.
version: 'tough-cookie@'+VERSION,
// add the store type, to make humans happy:
storeType: type,
// CookieJar configuration:
rejectPublicSuffixes: !!this.rejectPublicSuffixes,
// this gets filled from getAllCookies:
cookies: []
};
if (!(this.store.getAllCookies &&
typeof this.store.getAllCookies === 'function'))
{
return cb(new Error('store does not support getAllCookies and cannot be serialized'));
}
this.store.getAllCookies(function(err,cookies) {
if (err) {
return cb(err);
}
serialized.cookies = cookies.map(function(cookie) {
// convert to serialized 'raw' cookies
cookie = (cookie instanceof Cookie) ? cookie.toJSON() : cookie;
// Remove the index so new ones get assigned during deserialization
delete cookie.creationIndex;
return cookie;
});
return cb(null, serialized);
});
};
// well-known name that JSON.stringify calls
CookieJar.prototype.toJSON = function() {
return this.serializeSync();
};
// use the class method CookieJar.deserialize instead of calling this directly
CAN_BE_SYNC.push('_importCookies');
CookieJar.prototype._importCookies = function(serialized, cb) {
var jar = this;
var cookies = serialized.cookies;
if (!cookies || !Array.isArray(cookies)) {
return cb(new Error('serialized jar has no cookies array'));
}
cookies = cookies.slice(); // do not modify the original
function putNext(err) {
if (err) {
return cb(err);
}
if (!cookies.length) {
return cb(err, jar);
}
var cookie;
try {
cookie = fromJSON(cookies.shift());
} catch (e) {
return cb(e);
}
if (cookie === null) {
return putNext(null); // skip this cookie
}
jar.store.putCookie(cookie, putNext);
}
putNext();
};
CookieJar.deserialize = function(strOrObj, store, cb) {
if (arguments.length !== 3) {
// store is optional
cb = store;
store = null;
}
var serialized;
if (typeof strOrObj === 'string') {
serialized = jsonParse(strOrObj);
if (serialized instanceof Error) {
return cb(serialized);
}
} else {
serialized = strOrObj;
}
var jar = new CookieJar(store, serialized.rejectPublicSuffixes);
jar._importCookies(serialized, function(err) {
if (err) {
return cb(err);
}
cb(null, jar);
});
};
CookieJar.deserializeSync = function(strOrObj, store) {
var serialized = typeof strOrObj === 'string' ?
JSON.parse(strOrObj) : strOrObj;
var jar = new CookieJar(store, serialized.rejectPublicSuffixes);
// catch this mistake early:
if (!jar.store.synchronous) {
throw new Error('CookieJar store is not synchronous; use async API instead.');
}
jar._importCookiesSync(serialized);
return jar;
};
CookieJar.fromJSON = CookieJar.deserializeSync;
CAN_BE_SYNC.push('clone');
CookieJar.prototype.clone = function(newStore, cb) {
if (arguments.length === 1) {
cb = newStore;
newStore = null;
}
this.serialize(function(err,serialized) {
if (err) {
return cb(err);
}
CookieJar.deserialize(newStore, serialized, cb);
});
};
// Use a closure to provide a true imperative API for synchronous stores.
function syncWrap(method) {
return function() {
if (!this.store.synchronous) {
throw new Error('CookieJar store is not synchronous; use async API instead.');
}
var args = Array.prototype.slice.call(arguments);
var syncErr, syncResult;
args.push(function syncCb(err, result) {
syncErr = err;
syncResult = result;
});
this[method].apply(this, args);
if (syncErr) {
throw syncErr;
}
return syncResult;
};
}
// wrap all declared CAN_BE_SYNC methods in the sync wrapper
CAN_BE_SYNC.forEach(function(method) {
CookieJar.prototype[method+'Sync'] = syncWrap(method);
});
exports.CookieJar = CookieJar;
exports.Cookie = Cookie;
exports.Store = Store;
exports.MemoryCookieStore = MemoryCookieStore;
exports.parseDate = parseDate;
exports.formatDate = formatDate;
exports.parse = parse;
exports.fromJSON = fromJSON;
exports.domainMatch = domainMatch;
exports.defaultPath = defaultPath;
exports.pathMatch = pathMatch;
exports.getPublicSuffix = pubsuffix.getPublicSuffix;
exports.cookieCompare = cookieCompare;
exports.permuteDomain = __webpack_require__(90).permuteDomain;
exports.permutePath = permutePath;
exports.canonicalDomain = canonicalDomain;
/***/ }),
/* 82 */
/***/ (function(module, exports) {
module.exports = require("net");
/***/ }),
/* 83 */
/***/ (function(module, exports) {
module.exports = require("url");
/***/ }),
/* 84 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/*!
* Copyright (c) 2018, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of Salesforce.com nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
var psl = __webpack_require__(85);
function getPublicSuffix(domain) {
return psl.get(domain);
}
exports.getPublicSuffix = getPublicSuffix;
/***/ }),
/* 85 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/*eslint no-var:0, prefer-arrow-callback: 0, object-shorthand: 0 */
var Punycode = __webpack_require__(86);
var internals = {};
//
// Read rules from file.
//
internals.rules = __webpack_require__(87).map(function (rule) {
return {
rule: rule,
suffix: rule.replace(/^(\*\.|\!)/, ''),
punySuffix: -1,
wildcard: rule.charAt(0) === '*',
exception: rule.charAt(0) === '!'
};
});
//
// Check is given string ends with `suffix`.
//
internals.endsWith = function (str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
};
//
// Find rule for a given domain.
//
internals.findRule = function (domain) {
var punyDomain = Punycode.toASCII(domain);
return internals.rules.reduce(function (memo, rule) {
if (rule.punySuffix === -1){
rule.punySuffix = Punycode.toASCII(rule.suffix);
}
if (!internals.endsWith(punyDomain, '.' + rule.punySuffix) && punyDomain !== rule.punySuffix) {
return memo;
}
// This has been commented out as it never seems to run. This is because
// sub tlds always appear after their parents and we never find a shorter
// match.
//if (memo) {
// var memoSuffix = Punycode.toASCII(memo.suffix);
// if (memoSuffix.length >= punySuffix.length) {
// return memo;
// }
//}
return rule;
}, null);
};
//
// Error codes and messages.
//
exports.errorCodes = {
DOMAIN_TOO_SHORT: 'Domain name too short.',
DOMAIN_TOO_LONG: 'Domain name too long. It should be no more than 255 chars.',
LABEL_STARTS_WITH_DASH: 'Domain name label can not start with a dash.',
LABEL_ENDS_WITH_DASH: 'Domain name label can not end with a dash.',
LABEL_TOO_LONG: 'Domain name label should be at most 63 chars long.',
LABEL_TOO_SHORT: 'Domain name label should be at least 1 character long.',
LABEL_INVALID_CHARS: 'Domain name label can only contain alphanumeric characters or dashes.'
};
//
// Validate domain name and throw if not valid.
//
// From wikipedia:
//
// Hostnames are composed of series of labels concatenated with dots, as are all
// domain names. Each label must be between 1 and 63 characters long, and the
// entire hostname (including the delimiting dots) has a maximum of 255 chars.
//
// Allowed chars:
//
// * `a-z`
// * `0-9`
// * `-` but not as a starting or ending character
// * `.` as a separator for the textual portions of a domain name
//
// * http://en.wikipedia.org/wiki/Domain_name
// * http://en.wikipedia.org/wiki/Hostname
//
internals.validate = function (input) {
// Before we can validate we need to take care of IDNs with unicode chars.
var ascii = Punycode.toASCII(input);
if (ascii.length < 1) {
return 'DOMAIN_TOO_SHORT';
}
if (ascii.length > 255) {
return 'DOMAIN_TOO_LONG';
}
// Check each part's length and allowed chars.
var labels = ascii.split('.');
var label;
for (var i = 0; i < labels.length; ++i) {
label = labels[i];
if (!label.length) {
return 'LABEL_TOO_SHORT';
}
if (label.length > 63) {
return 'LABEL_TOO_LONG';
}
if (label.charAt(0) === '-') {
return 'LABEL_STARTS_WITH_DASH';
}
if (label.charAt(label.length - 1) === '-') {
return 'LABEL_ENDS_WITH_DASH';
}
if (!/^[a-z0-9\-]+$/.test(label)) {
return 'LABEL_INVALID_CHARS';
}
}
};
//
// Public API
//
//
// Parse domain.
//
exports.parse = function (input) {
if (typeof input !== 'string') {
throw new TypeError('Domain name must be a string.');
}
// Force domain to lowercase.
var domain = input.slice(0).toLowerCase();
// Handle FQDN.
// TODO: Simply remove trailing dot?
if (domain.charAt(domain.length - 1) === '.') {
domain = domain.slice(0, domain.length - 1);
}
// Validate and sanitise input.
var error = internals.validate(domain);
if (error) {
return {
input: input,
error: {
message: exports.errorCodes[error],
code: error
}
};
}
var parsed = {
input: input,
tld: null,
sld: null,
domain: null,
subdomain: null,
listed: false
};
var domainParts = domain.split('.');
// Non-Internet TLD
if (domainParts[domainParts.length - 1] === 'local') {
return parsed;
}
var handlePunycode = function () {
if (!/xn--/.test(domain)) {
return parsed;
}
if (parsed.domain) {
parsed.domain = Punycode.toASCII(parsed.domain);
}
if (parsed.subdomain) {
parsed.subdomain = Punycode.toASCII(parsed.subdomain);
}
return parsed;
};
var rule = internals.findRule(domain);
// Unlisted tld.
if (!rule) {
if (domainParts.length < 2) {
return parsed;
}
parsed.tld = domainParts.pop();
parsed.sld = domainParts.pop();
parsed.domain = [parsed.sld, parsed.tld].join('.');
if (domainParts.length) {
parsed.subdomain = domainParts.pop();
}
return handlePunycode();
}
// At this point we know the public suffix is listed.
parsed.listed = true;
var tldParts = rule.suffix.split('.');
var privateParts = domainParts.slice(0, domainParts.length - tldParts.length);
if (rule.exception) {
privateParts.push(tldParts.shift());
}
parsed.tld = tldParts.join('.');
if (!privateParts.length) {
return handlePunycode();
}
if (rule.wildcard) {
tldParts.unshift(privateParts.pop());
parsed.tld = tldParts.join('.');
}
if (!privateParts.length) {
return handlePunycode();
}
parsed.sld = privateParts.pop();
parsed.domain = [parsed.sld, parsed.tld].join('.');
if (privateParts.length) {