parse-domain 4.1.0 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/README.md +131 -58
  2. package/bin/update.js +8 -11
  3. package/{build-cjs/src → build}/config.d.ts +0 -0
  4. package/{build-esm/src → build}/config.js +0 -0
  5. package/build/config.js.map +1 -0
  6. package/{build-cjs/src → build}/from-url.d.ts +0 -0
  7. package/{build-esm/src → build}/from-url.js +3 -1
  8. package/build/from-url.js.map +1 -0
  9. package/build/main.d.ts +3 -0
  10. package/build/main.js +6 -0
  11. package/build/main.js.map +1 -0
  12. package/{build-cjs/src → build}/parse-domain.d.ts +9 -3
  13. package/{build-esm/src → build}/parse-domain.js +6 -6
  14. package/build/parse-domain.js.map +1 -0
  15. package/build/psl/build-tries.d.ts +4 -0
  16. package/{build-esm/src → build}/psl/build-tries.js +5 -20
  17. package/build/psl/build-tries.js.map +1 -0
  18. package/{build-cjs/src → build}/psl/fetch-psl.d.ts +0 -0
  19. package/{build-esm/src → build}/psl/fetch-psl.js +1 -1
  20. package/build/psl/fetch-psl.js.map +1 -0
  21. package/build/punycode.d.ts +1 -0
  22. package/build/punycode.js +4 -0
  23. package/build/punycode.js.map +1 -0
  24. package/{build-cjs/src → build}/sanitize.d.ts +28 -6
  25. package/build/sanitize.js +193 -0
  26. package/build/sanitize.js.map +1 -0
  27. package/{build-cjs/src → build}/scripts/update-psl-fixture.d.ts +0 -0
  28. package/build/scripts/update-psl-fixture.js +20 -0
  29. package/build/scripts/update-psl-fixture.js.map +1 -0
  30. package/{build-cjs/src → build}/scripts/update-tries.d.ts +0 -0
  31. package/build/scripts/update-tries.js +39 -0
  32. package/build/scripts/update-tries.js.map +1 -0
  33. package/build/serialized-tries.d.ts +3 -0
  34. package/build/serialized-tries.js +4 -0
  35. package/build/serialized-tries.js.map +1 -0
  36. package/{build-cjs/src → build}/smoke-test.d.ts +0 -0
  37. package/{build-esm/src → build}/smoke-test.js +6 -8
  38. package/build/smoke-test.js.map +1 -0
  39. package/{build-cjs/src → build}/trie/characters.d.ts +0 -0
  40. package/{build-esm/src → build}/trie/characters.js +0 -0
  41. package/build/trie/characters.js.map +1 -0
  42. package/{build-cjs/src → build}/trie/create-trie.d.ts +1 -1
  43. package/{build-esm/src → build}/trie/create-trie.js +1 -1
  44. package/build/trie/create-trie.js.map +1 -0
  45. package/{build-cjs/src → build}/trie/look-up.d.ts +2 -2
  46. package/{build-esm/src → build}/trie/look-up.js +1 -1
  47. package/build/trie/look-up.js.map +1 -0
  48. package/{build-cjs/src → build}/trie/nodes.d.ts +0 -0
  49. package/{build-esm/src → build}/trie/nodes.js +0 -0
  50. package/build/trie/nodes.js.map +1 -0
  51. package/{build-cjs/src → build}/trie/parse-trie.d.ts +1 -1
  52. package/{build-esm/src → build}/trie/parse-trie.js +2 -2
  53. package/build/trie/parse-trie.js.map +1 -0
  54. package/{build-cjs/src → build}/trie/serialize-trie.d.ts +1 -1
  55. package/{build-esm/src → build}/trie/serialize-trie.js +1 -1
  56. package/build/trie/serialize-trie.js.map +1 -0
  57. package/{build-cjs/src → build}/type-util.d.ts +0 -0
  58. package/{build-esm/src → build}/type-util.js +0 -0
  59. package/{build-cjs/src → build}/type-util.js.map +1 -1
  60. package/{build-cjs/src → build}/update-tries.d.ts +0 -0
  61. package/{build-esm/src → build}/update-tries.js +3 -3
  62. package/build/update-tries.js.map +1 -0
  63. package/package.json +34 -43
  64. package/serialized-tries/icann.d.ts +3 -0
  65. package/serialized-tries/icann.js +1 -0
  66. package/serialized-tries/info.json +1 -0
  67. package/serialized-tries/private.d.ts +3 -0
  68. package/serialized-tries/private.js +1 -0
  69. package/build-cjs/serialized-tries/icann.json +0 -1
  70. package/build-cjs/serialized-tries/info.json +0 -1
  71. package/build-cjs/serialized-tries/private.json +0 -1
  72. package/build-cjs/src/config.js +0 -10
  73. package/build-cjs/src/config.js.map +0 -1
  74. package/build-cjs/src/from-url.js +0 -32
  75. package/build-cjs/src/from-url.js.map +0 -1
  76. package/build-cjs/src/main.d.ts +0 -3
  77. package/build-cjs/src/main.js +0 -14
  78. package/build-cjs/src/main.js.map +0 -1
  79. package/build-cjs/src/main.mjs +0 -11
  80. package/build-cjs/src/parse-domain.js +0 -97
  81. package/build-cjs/src/parse-domain.js.map +0 -1
  82. package/build-cjs/src/psl/build-tries.d.ts +0 -4
  83. package/build-cjs/src/psl/build-tries.js +0 -59
  84. package/build-cjs/src/psl/build-tries.js.map +0 -1
  85. package/build-cjs/src/psl/fetch-psl.js +0 -27
  86. package/build-cjs/src/psl/fetch-psl.js.map +0 -1
  87. package/build-cjs/src/sanitize.js +0 -132
  88. package/build-cjs/src/sanitize.js.map +0 -1
  89. package/build-cjs/src/scripts/update-psl-fixture.js +0 -29
  90. package/build-cjs/src/scripts/update-psl-fixture.js.map +0 -1
  91. package/build-cjs/src/scripts/update-tries.js +0 -48
  92. package/build-cjs/src/scripts/update-tries.js.map +0 -1
  93. package/build-cjs/src/serialized-tries.d.ts +0 -3
  94. package/build-cjs/src/serialized-tries.js +0 -11
  95. package/build-cjs/src/serialized-tries.js.map +0 -1
  96. package/build-cjs/src/smoke-test.js +0 -63
  97. package/build-cjs/src/smoke-test.js.map +0 -1
  98. package/build-cjs/src/trie/characters.js +0 -11
  99. package/build-cjs/src/trie/characters.js.map +0 -1
  100. package/build-cjs/src/trie/create-trie.js +0 -16
  101. package/build-cjs/src/trie/create-trie.js.map +0 -1
  102. package/build-cjs/src/trie/look-up.js +0 -48
  103. package/build-cjs/src/trie/look-up.js.map +0 -1
  104. package/build-cjs/src/trie/nodes.js +0 -27
  105. package/build-cjs/src/trie/nodes.js.map +0 -1
  106. package/build-cjs/src/trie/parse-trie.js +0 -70
  107. package/build-cjs/src/trie/parse-trie.js.map +0 -1
  108. package/build-cjs/src/trie/serialize-trie.js +0 -31
  109. package/build-cjs/src/trie/serialize-trie.js.map +0 -1
  110. package/build-cjs/src/type-util.js +0 -3
  111. package/build-cjs/src/update-tries.js +0 -27
  112. package/build-cjs/src/update-tries.js.map +0 -1
  113. package/build-esm/serialized-tries/icann.json +0 -1
  114. package/build-esm/serialized-tries/info.json +0 -1
  115. package/build-esm/serialized-tries/private.json +0 -1
  116. package/build-esm/src/config.d.ts +0 -6
  117. package/build-esm/src/config.js.map +0 -1
  118. package/build-esm/src/from-url.d.ts +0 -2
  119. package/build-esm/src/from-url.js.map +0 -1
  120. package/build-esm/src/main.d.ts +0 -3
  121. package/build-esm/src/main.js +0 -6
  122. package/build-esm/src/main.js.map +0 -1
  123. package/build-esm/src/parse-domain.d.ts +0 -79
  124. package/build-esm/src/parse-domain.js.map +0 -1
  125. package/build-esm/src/psl/build-tries.d.ts +0 -4
  126. package/build-esm/src/psl/build-tries.js.map +0 -1
  127. package/build-esm/src/psl/fetch-psl.d.ts +0 -1
  128. package/build-esm/src/psl/fetch-psl.js.map +0 -1
  129. package/build-esm/src/sanitize.d.ts +0 -36
  130. package/build-esm/src/sanitize.js +0 -125
  131. package/build-esm/src/sanitize.js.map +0 -1
  132. package/build-esm/src/serialized-tries.d.ts +0 -3
  133. package/build-esm/src/serialized-tries.js +0 -4
  134. package/build-esm/src/serialized-tries.js.map +0 -1
  135. package/build-esm/src/smoke-test.d.ts +0 -1
  136. package/build-esm/src/smoke-test.js.map +0 -1
  137. package/build-esm/src/trie/characters.d.ts +0 -6
  138. package/build-esm/src/trie/characters.js.map +0 -1
  139. package/build-esm/src/trie/create-trie.d.ts +0 -1
  140. package/build-esm/src/trie/create-trie.js.map +0 -1
  141. package/build-esm/src/trie/look-up.d.ts +0 -3
  142. package/build-esm/src/trie/look-up.js.map +0 -1
  143. package/build-esm/src/trie/nodes.d.ts +0 -15
  144. package/build-esm/src/trie/nodes.js.map +0 -1
  145. package/build-esm/src/trie/parse-trie.d.ts +0 -1
  146. package/build-esm/src/trie/parse-trie.js.map +0 -1
  147. package/build-esm/src/trie/serialize-trie.d.ts +0 -2
  148. package/build-esm/src/trie/serialize-trie.js.map +0 -1
  149. package/build-esm/src/type-util.d.ts +0 -3
  150. package/build-esm/src/type-util.js.map +0 -1
  151. package/build-esm/src/update-tries.d.ts +0 -4
  152. package/build-esm/src/update-tries.js.map +0 -1
package/README.md CHANGED
@@ -15,7 +15,7 @@ Since domain name registrars organize their namespaces in different ways, it's n
15
15
  import { parseDomain, ParseResultType } from "parse-domain";
16
16
 
17
17
  const parseResult = parseDomain(
18
- // This should be a string with basic latin characters only.
18
+ // This should be a string with basic latin letters only.
19
19
  // More information below.
20
20
  "www.some.example.co.uk"
21
21
  );
@@ -32,9 +32,9 @@ if (parseResult.type === ParseResultType.Listed) {
32
32
  }
33
33
  ```
34
34
 
35
- This package has been designed for modern Node and browser environments, supporting both CommonJS and ECMAScript modules. It assumes an ES2015 environment with [`Symbol()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) and [`URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL) globally available. You need to transpile it down to ES5 (e.g. by using [Babel](https://babeljs.io/)) if you need to support older environments.
35
+ This package has been designed for modern Node and browser environments with ECMAScript modules support. It assumes an ES2015 environment with [`Symbol()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol), [`URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL) and [`TextDecoder()`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) globally available. You need to transpile it down to ES5 (e.g. by using [Babel](https://babeljs.io/)) if you need to support older environments.
36
36
 
37
- The list of top-level domains is stored in a [trie](https://en.wikipedia.org/wiki/Trie) data structure and serialization format to ensure the fastest lookup and the smallest possible library size. The library is side-effect free (this is important for proper [tree-shaking](https://webpack.js.org/guides/tree-shaking/)).
37
+ The list of top-level domains is stored in a [trie](https://en.wikipedia.org/wiki/Trie) data structure and serialization format to ensure the fastest lookup and the smallest possible library size.
38
38
 
39
39
  <br />
40
40
 
@@ -96,7 +96,7 @@ When parsing a hostname there are 5 possible results:
96
96
 
97
97
  ### 👉 Invalid domains
98
98
 
99
- The given input is first validated against [RFC 1034](https://tools.ietf.org/html/rfc1034). If the validation fails, `parseResult.type` will be `ParseResultType.Invalid`:
99
+ The given input is first validated against [RFC 3696](https://datatracker.ietf.org/doc/html/rfc3696#section-2) (the domain labels are limited to basic latin letters, numbers and hyphens). If the validation fails, `parseResult.type` will be `ParseResultType.Invalid`:
100
100
 
101
101
  ```javascript
102
102
  import { parseDomain, ParseResultType } from "parse-domain";
@@ -108,6 +108,20 @@ console.log(parseResult.type === ParseResultType.Invalid); // true
108
108
 
109
109
  Check out the [API](#api-ts-ValidationError) if you need more information about the validation error.
110
110
 
111
+ If you don't want the characters to be validated (e.g. because you need to allow underscores in hostnames), there's also a more relaxed validation mode (according to [RFC 2181](https://www.rfc-editor.org/rfc/rfc2181#section-11)).
112
+
113
+ ```javascript
114
+ import { parseDomain, ParseResultType, Validation } from "parse-domain";
115
+
116
+ const parseResult = parseDomain("_jabber._tcp.gmail.com", {
117
+ validation: Validation.Lax,
118
+ });
119
+
120
+ console.log(parseResult.type === ParseResultType.Listed); // true
121
+ ```
122
+
123
+ See also [#134](https://github.com/peerigon/parse-domain/issues/134) for the discussion.
124
+
111
125
  ### 👉 IP addresses
112
126
 
113
127
  If the given input is an IP address, `parseResult.type` will be `ParseResultType.Ip`:
@@ -273,10 +287,10 @@ console.log(topLevelDomains); // []
273
287
  🧬 = TypeScript export
274
288
 
275
289
  <h3 id="api-js-parseDomain">
276
- 🧩 <code>export parseDomain(hostname: string | typeof <a href="#api-js-NO_HOSTNAME">NO_HOSTNAME</a>): <a href="#api-ts-ParseResult">ParseResult</a></code>
290
+ 🧩 <code>export parseDomain(hostname: string | typeof <a href="#api-js-NO_HOSTNAME">NO_HOSTNAME</a>, options?: <a href="#api-ts-ParseDomainOptions">ParseDomainOptions</a>): <a href="#api-ts-ParseResult">ParseResult</a></code>
277
291
  </h3>
278
292
 
279
- Takes a hostname (e.g. `"www.example.com"`) and returns a [`ParseResult`](#api-ts-ParseResult). The hostname must only contain basic latin characters, digits, hyphens and dots. International hostnames must be puny-encoded. Does not throw an error, even with invalid input.
293
+ Takes a hostname (e.g. `"www.example.com"`) and returns a [`ParseResult`](#api-ts-ParseResult). The hostname must only contain basic latin letters, digits, hyphens and dots. International hostnames must be puny-encoded. Does not throw an error, even with invalid input.
280
294
 
281
295
  ```javascript
282
296
  import { parseDomain } from "parse-domain";
@@ -284,6 +298,16 @@ import { parseDomain } from "parse-domain";
284
298
  const parseResult = parseDomain("www.example.com");
285
299
  ```
286
300
 
301
+ Use `Validation.Lax` if you want to allow all characters:
302
+
303
+ ```javascript
304
+ import { parseDomain, Validation } from "parse-domain";
305
+
306
+ const parseResult = parseDomain("_jabber._tcp.gmail.com", {
307
+ validation: Validation.Lax,
308
+ });
309
+ ```
310
+
287
311
  <h3 id="api-js-fromUrl">
288
312
  🧩 <code>export fromUrl(input: string): string | typeof <a href="#api-js-NO_HOSTNAME">NO_HOSTNAME</a></code>
289
313
  </h3>
@@ -296,6 +320,54 @@ Takes a URL-like string and tries to extract the hostname. Requires the global [
296
320
 
297
321
  `NO_HOSTNAME` is a symbol that is returned by [`fromUrl`](#api-js-fromUrl) when it was not able to extract a hostname from the given string. When passed to [`parseDomain`](#api-js-parseDomain), it will always yield a [`ParseResultInvalid`](#api-ts-ParseResultInvalid).
298
322
 
323
+ <h3 id="api-ts-ParseDomainOptions">
324
+ 🧬 <code>export type ParseDomainOptions</code>
325
+ </h3>
326
+
327
+ ```ts
328
+ export type ParseDomainOptions = {
329
+ /**
330
+ * If no validation is specified, Validation.Strict will be used.
331
+ **/
332
+ validation?: Validation;
333
+ };
334
+ ```
335
+
336
+ <h3 id="api-js-Validation">
337
+ 🧩 <code>export Validation</code>
338
+ </h3>
339
+
340
+ An object that holds all possible [Validation](#api-ts-Validation) `validation` values:
341
+
342
+ ```javascript
343
+ export const Validation = {
344
+ /**
345
+ * Allows any octets as labels
346
+ * but still restricts the length of labels and the overall domain.
347
+ *
348
+ * @see https://www.rfc-editor.org/rfc/rfc2181#section-11
349
+ **/
350
+ Lax: "LAX",
351
+
352
+ /**
353
+ * Only allows ASCII letters, digits and hyphens (aka LDH),
354
+ * forbids hyphens at the beginning or end of a label
355
+ * and requires top-level domain names not to be all-numeric.
356
+ *
357
+ * This is the default if no validation is configured.
358
+ *
359
+ * @see https://datatracker.ietf.org/doc/html/rfc3696#section-2
360
+ */
361
+ Strict: "STRICT",
362
+ };
363
+ ```
364
+
365
+ <h3 id="api-ts-Validation">
366
+ 🧬 <code>export Validation</code>
367
+ </h3>
368
+
369
+ This type represents all possible `validation` values.
370
+
299
371
  <h3 id="api-ts-ParseResult">
300
372
  🧬 <code>export ParseResult</code>
301
373
  </h3>
@@ -342,17 +414,17 @@ Describes the shape of the parse result that is returned when the given hostname
342
414
 
343
415
  ```ts
344
416
  type ParseResultInvalid = {
345
- type: ParseResultType.INVALID;
346
- hostname: string | typeof NO_HOSTNAME;
347
- errors: Array<ValidationError>;
417
+ type: ParseResultType.INVALID;
418
+ hostname: string | typeof NO_HOSTNAME;
419
+ errors: Array<ValidationError>;
348
420
  };
349
421
 
350
422
  // Example
351
423
 
352
424
  {
353
- type: "INVALID",
354
- hostname: ".com",
355
- errors: [...]
425
+ type: "INVALID",
426
+ hostname: ".com",
427
+ errors: [...]
356
428
  }
357
429
  ```
358
430
 
@@ -364,17 +436,17 @@ Describes the shape of a validation error as returned by [`parseDomain`](#api-js
364
436
 
365
437
  ```ts
366
438
  type ValidationError = {
367
- type: ValidationErrorType;
368
- message: string;
369
- column: number;
439
+ type: ValidationErrorType;
440
+ message: string;
441
+ column: number;
370
442
  };
371
443
 
372
444
  // Example
373
445
 
374
446
  {
375
- type: "LABEL_MIN_LENGTH",
376
- message: `Label "" is too short. Label is 0 octets long but should be at least 1.`,
377
- column: 1,
447
+ type: "LABEL_MIN_LENGTH",
448
+ message: `Label "" is too short. Label is 0 octets long but should be at least 1.`,
449
+ column: 1,
378
450
  }
379
451
  ```
380
452
 
@@ -391,6 +463,7 @@ const ValidationErrorType = {
391
463
  LabelMinLength: "LABEL_MIN_LENGTH",
392
464
  LabelMaxLength: "LABEL_MAX_LENGTH",
393
465
  LabelInvalidCharacter: "LABEL_INVALID_CHARACTER",
466
+ LastLabelInvalid: "LAST_LABEL_INVALID",
394
467
  };
395
468
  ```
396
469
 
@@ -408,17 +481,17 @@ This type describes the shape of the parse result that is returned when the give
408
481
 
409
482
  ```ts
410
483
  type ParseResultIp = {
411
- type: ParseResultType.Ip;
412
- hostname: string;
413
- ipVersion: 4 | 6;
484
+ type: ParseResultType.Ip;
485
+ hostname: string;
486
+ ipVersion: 4 | 6;
414
487
  };
415
488
 
416
489
  // Example
417
490
 
418
491
  {
419
- type: "IP",
420
- hostname: "192.168.0.1",
421
- ipVersion: 4
492
+ type: "IP",
493
+ hostname: "192.168.0.1",
494
+ ipVersion: 4
422
495
  }
423
496
  ```
424
497
 
@@ -443,17 +516,17 @@ This type describes the shape of the parse result that is returned when the give
443
516
 
444
517
  ```ts
445
518
  type ParseResultReserved = {
446
- type: ParseResultType.Reserved;
447
- hostname: string;
448
- labels: Array<string>;
519
+ type: ParseResultType.Reserved;
520
+ hostname: string;
521
+ labels: Array<string>;
449
522
  };
450
523
 
451
524
  // Example
452
525
 
453
526
  {
454
- type: "RESERVED",
455
- hostname: "pecorino.local",
456
- labels: ["pecorino", "local"]
527
+ type: "RESERVED",
528
+ hostname: "pecorino.local",
529
+ labels: ["pecorino", "local"]
457
530
  }
458
531
  ```
459
532
 
@@ -467,17 +540,17 @@ Describes the shape of the parse result that is returned when the given hostname
467
540
 
468
541
  ```ts
469
542
  type ParseResultNotListed = {
470
- type: ParseResultType.NotListed;
471
- hostname: string;
472
- labels: Array<string>;
543
+ type: ParseResultType.NotListed;
544
+ hostname: string;
545
+ labels: Array<string>;
473
546
  };
474
547
 
475
548
  // Example
476
549
 
477
550
  {
478
- type: "NOT_LISTED",
479
- hostname: "this.is.not-listed",
480
- labels: ["this", "is", "not-listed"]
551
+ type: "NOT_LISTED",
552
+ hostname: "this.is.not-listed",
553
+ labels: ["this", "is", "not-listed"]
481
554
  }
482
555
  ```
483
556
 
@@ -489,33 +562,33 @@ Describes the shape of the parse result that is returned when the given hostname
489
562
 
490
563
  ```ts
491
564
  type ParseResultListed = {
492
- type: ParseResultType.Listed;
493
- hostname: string;
494
- labels: Array<string>;
495
- subDomains: Array<string>;
496
- domain: string | undefined;
497
- topLevelDomains: Array<string>;
498
- icann: {
499
- subDomains: Array<string>;
500
- domain: string | undefined;
501
- topLevelDomains: Array<string>;
502
- };
565
+ type: ParseResultType.Listed;
566
+ hostname: string;
567
+ labels: Array<string>;
568
+ subDomains: Array<string>;
569
+ domain: string | undefined;
570
+ topLevelDomains: Array<string>;
571
+ icann: {
572
+ subDomains: Array<string>;
573
+ domain: string | undefined;
574
+ topLevelDomains: Array<string>;
575
+ };
503
576
  };
504
577
 
505
578
  // Example
506
579
 
507
580
  {
508
- type: "LISTED",
509
- hostname: "parse-domain.github.io",
510
- labels: ["parse-domain", "github", "io"]
511
- subDomains: [],
512
- domain: "parse-domain",
513
- topLevelDomains: ["github", "io"],
514
- icann: {
515
- subDomains: ["parse-domain"],
516
- domain: "github",
517
- topLevelDomains: ["io"]
518
- }
581
+ type: "LISTED",
582
+ hostname: "parse-domain.github.io",
583
+ labels: ["parse-domain", "github", "io"]
584
+ subDomains: [],
585
+ domain: "parse-domain",
586
+ topLevelDomains: ["github", "io"],
587
+ icann: {
588
+ subDomains: ["parse-domain"],
589
+ domain: "github",
590
+ topLevelDomains: ["io"]
591
+ }
519
592
  }
520
593
  ```
521
594
 
package/bin/update.js CHANGED
@@ -1,25 +1,22 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- "use strict";
4
-
5
- const { EOL } = require("os");
3
+ import { EOL } from "os";
6
4
 
7
5
  (async () => {
8
- process.argv.push(
9
- "--",
10
- "../../serialized-tries",
11
- "../../../build-esm/serialized-tries"
12
- );
6
+ process.argv.push("--", "../../serialized-tries");
7
+
8
+ const updateTries = await import("../build/scripts/update-tries.js");
13
9
 
14
- await require("../build-cjs/src/scripts/update-tries.js").done;
10
+ await updateTries.done;
15
11
 
16
12
  process.stderr.write("Running smoke test... ");
17
13
 
18
- require("../build-cjs/src/smoke-test.js").runSmokeTest();
14
+ const smokeTest = await import("../build/smoke-test.js");
15
+
16
+ smokeTest.runSmokeTest();
19
17
 
20
18
  process.stdout.write("ok" + EOL);
21
19
  })().catch((error) => {
22
20
  console.error(`parse-domain update failed: ${error}`);
23
- // eslint-disable-next-line no-process-exit, node/no-process-exit
24
21
  process.exit(1);
25
22
  });
File without changes
File without changes
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,iBAAiB,GAC5B,sDAAsD,CAAC;AACzD,MAAM,CAAC,MAAM,gCAAgC,GAAG,8BAA8B,CAAC;AAC/E,MAAM,CAAC,MAAM,8BAA8B,GAAG,4BAA4B,CAAC;AAC3E,MAAM,CAAC,MAAM,kCAAkC,GAC7C,gCAAgC,CAAC;AACnC,MAAM,CAAC,MAAM,gCAAgC,GAAG,8BAA8B,CAAC;AAC/E,MAAM,CAAC,MAAM,6BAA6B,GAAG,MAAM,CAAC"}
File without changes
@@ -1,4 +1,5 @@
1
1
  const urlPattern = /^[a-z][*+.a-z-]+:\/\//i;
2
+ const invalidIpv6Pattern = /^([a-z][*+.a-z-]+:\/\/)([^[].*:[^/?]*:[^/?]*)(.*)/i;
2
3
  export const NO_HOSTNAME = Symbol("NO_HOSTNAME");
3
4
  export const fromUrl = (urlLike) => {
4
5
  /* istanbul ignore next */
@@ -10,7 +11,7 @@ export const fromUrl = (urlLike) => {
10
11
  return NO_HOSTNAME;
11
12
  }
12
13
  // URLs that start with // are protocol relative
13
- const url = urlLike.startsWith("//")
14
+ let url = urlLike.startsWith("//")
14
15
  ? `http:${urlLike}`
15
16
  : // URLs that start with / do not have a hostname section
16
17
  urlLike.startsWith("/")
@@ -18,6 +19,7 @@ export const fromUrl = (urlLike) => {
18
19
  : urlPattern.test(urlLike)
19
20
  ? urlLike
20
21
  : `http://${urlLike}`;
22
+ url = url.replace(invalidIpv6Pattern, "$1[$2]$3");
21
23
  try {
22
24
  return new URL(url).hostname;
23
25
  }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-url.js","sourceRoot":"","sources":["../src/from-url.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,GAAG,wBAAwB,CAAC;AAC5C,MAAM,kBAAkB,GAAG,oDAAoD,CAAC;AAEhF,MAAM,CAAC,MAAM,WAAW,GAAkB,MAAM,CAAC,aAAa,CAAC,CAAC;AAEhE,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,OAAe,EAAE,EAAE;IACzC,0BAA0B;IAC1B,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE;QAC7B,MAAM,IAAI,KAAK,CACb,yHAAyH,CAC1H,CAAC;KACH;IAED,uCAAuC;IACvC,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QAC/B,OAAO,WAAW,CAAC;KACpB;IAED,gDAAgD;IAChD,IAAI,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC;QAChC,CAAC,CAAC,QAAQ,OAAO,EAAE;QACnB,CAAC,CAAC,wDAAwD;YAC1D,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC;gBACvB,CAAC,CAAC,OAAO;gBACT,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC;oBAC1B,CAAC,CAAC,OAAO;oBACT,CAAC,CAAC,UAAU,OAAO,EAAE,CAAC;IAExB,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,kBAAkB,EAAE,UAAU,CAAC,CAAC;IAElD,IAAI;QACF,OAAO,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC;KAC9B;IAAC,WAAM;QACN,OAAO,WAAW,CAAC;KACpB;AACH,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { parseDomain, ParseResult, ParseResultType, ParseResultInvalid, ParseResultReserved, ParseResultNotListed, ParseResultListed, } from "./parse-domain.js";
2
+ export { fromUrl, NO_HOSTNAME } from "./from-url.js";
3
+ export { Validation, ValidationError, ValidationErrorType, } from "./sanitize.js";
package/build/main.js ADDED
@@ -0,0 +1,6 @@
1
+ /* istanbul ignore file */
2
+ // Jest will report function coverage errors here otherwise
3
+ export { parseDomain, ParseResultType, } from "./parse-domain.js";
4
+ export { fromUrl, NO_HOSTNAME } from "./from-url.js";
5
+ export { Validation, ValidationErrorType, } from "./sanitize.js";
6
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,0BAA0B;AAC1B,2DAA2D;AAC3D,OAAO,EACL,WAAW,EAEX,eAAe,GAKhB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EACL,UAAU,EAEV,mBAAmB,GACpB,MAAM,eAAe,CAAC"}
@@ -1,5 +1,5 @@
1
- import { ValidationError, SanitizationResultValidIp } from "./sanitize";
2
- import { NO_HOSTNAME } from "./from-url";
1
+ import { ValidationError, SanitizationResultValidIp, Validation } from "./sanitize.js";
2
+ import { NO_HOSTNAME } from "./from-url.js";
3
3
  export declare const RESERVED_TOP_LEVEL_DOMAINS: string[];
4
4
  export declare type Label = string;
5
5
  export declare enum ParseResultType {
@@ -72,8 +72,14 @@ export declare type ParseResultListed = ParseResultCommon<ParseResultType.Listed
72
72
  icann: ParseResultListedDomains;
73
73
  };
74
74
  export declare type ParseResult = ParseResultInvalid | ParseResultIp | ParseResultReserved | ParseResultNotListed | ParseResultListed;
75
+ export declare type ParseDomainOptions = {
76
+ /**
77
+ * If no validation is specified, Validation.Strict will be used.
78
+ **/
79
+ validation?: Validation;
80
+ };
75
81
  /**
76
82
  * Splits the given hostname in topLevelDomains, a domain and subDomains.
77
83
  */
78
- export declare const parseDomain: (hostname: string | typeof NO_HOSTNAME) => ParseResult;
84
+ export declare const parseDomain: (hostname: string | typeof NO_HOSTNAME, options?: ParseDomainOptions | undefined) => ParseResult;
79
85
  export {};
@@ -1,7 +1,7 @@
1
- import { icannTrie, privateTrie } from "./serialized-tries";
2
- import { lookUpTldsInTrie } from "./trie/look-up";
3
- import { sanitize, SanitizationResultType, } from "./sanitize";
4
- import { parseTrie } from "./trie/parse-trie";
1
+ import { icannTrie, privateTrie } from "./serialized-tries.js";
2
+ import { lookUpTldsInTrie } from "./trie/look-up.js";
3
+ import { sanitize, SanitizationResultType, } from "./sanitize.js";
4
+ import { parseTrie } from "./trie/parse-trie.js";
5
5
  export const RESERVED_TOP_LEVEL_DOMAINS = [
6
6
  "localhost",
7
7
  "local",
@@ -49,8 +49,8 @@ let parsedPrivateTrie;
49
49
  /**
50
50
  * Splits the given hostname in topLevelDomains, a domain and subDomains.
51
51
  */
52
- export const parseDomain = (hostname) => {
53
- const sanitizationResult = sanitize(hostname);
52
+ export const parseDomain = (hostname, options) => {
53
+ const sanitizationResult = sanitize(hostname, options);
54
54
  if (sanitizationResult.type === SanitizationResultType.Error) {
55
55
  return {
56
56
  type: ParseResultType.Invalid,
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-domain.js","sourceRoot":"","sources":["../src/parse-domain.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,4 @@
1
+ export declare const buildTries: (psl: string) => {
2
+ icannTrie: import("../trie/nodes.js").TrieRootNode;
3
+ privateTrie: import("../trie/nodes.js").TrieRootNode;
4
+ };
@@ -1,6 +1,6 @@
1
- import { toASCII } from "punycode";
2
- import { PUBLIC_SUFFIX_MARKER_ICANN_START, PUBLIC_SUFFIX_MARKER_ICANN_END, PUBLIC_SUFFIX_MARKER_PRIVATE_START, PUBLIC_SUFFIX_MARKER_PRIVATE_END, } from "../config";
3
- import { createTrieFromList } from "../trie/create-trie";
1
+ import { PUBLIC_SUFFIX_MARKER_ICANN_START, PUBLIC_SUFFIX_MARKER_ICANN_END, PUBLIC_SUFFIX_MARKER_PRIVATE_START, PUBLIC_SUFFIX_MARKER_PRIVATE_END, } from "../config.js";
2
+ import { toASCII } from "../punycode.js";
3
+ import { createTrieFromList } from "../trie/create-trie.js";
4
4
  const matchNewLine = /\r?\n/u;
5
5
  const matchComment = /^\s*\/\//u;
6
6
  const matchWhitespace = /^\s*$/u;
@@ -16,31 +16,16 @@ const extractByMarkers = (listContent, startMarker, endMarker) => {
16
16
  return listContent.slice(start, end);
17
17
  };
18
18
  const containsRule = (line) => matchComment.test(line) === false && matchWhitespace.test(line) === false;
19
- const normalizeRule = (rule) =>
20
- /*
21
- Users of parse-domain should only pass parsed hostnames (e.g. via new URL("...").hostname)
22
- to parseDomain(). This frees us from the burden of URL parsing.
23
- See also https://github.com/peerigon/parse-domain/issues/14
24
-
25
- The problem is that new URL("...").hostname will also translate all non-ASCII characters
26
- into punycode (e.g. 大分.jp becomes xn--kbrq7o.jp). This means for us that our serialized
27
- trie needs to use punyencoded hostnames.
28
-
29
- The downside of this is that punycode hostnames are bigger in terms of file size.
30
- However, that's still better than including a punycode library into the runtime code of parse-domain.
31
- Gzip is also very effective in removing duplicate characters.
32
- */
33
- toASCII(rule).toLowerCase();
34
19
  const parsePsl = (listContent) => {
35
20
  return {
36
21
  icann: extractByMarkers(listContent, PUBLIC_SUFFIX_MARKER_ICANN_START, PUBLIC_SUFFIX_MARKER_ICANN_END)
37
22
  .split(matchNewLine)
38
23
  .filter(containsRule)
39
- .map(normalizeRule),
24
+ .map(toASCII),
40
25
  private: extractByMarkers(listContent, PUBLIC_SUFFIX_MARKER_PRIVATE_START, PUBLIC_SUFFIX_MARKER_PRIVATE_END)
41
26
  .split(matchNewLine)
42
27
  .filter(containsRule)
43
- .map(normalizeRule),
28
+ .map(toASCII),
44
29
  };
45
30
  };
46
31
  export const buildTries = (psl) => {
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build-tries.js","sourceRoot":"","sources":["../../src/psl/build-tries.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gCAAgC,EAChC,8BAA8B,EAC9B,kCAAkC,EAClC,gCAAgC,GACjC,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAE5D,MAAM,YAAY,GAAG,QAAQ,CAAC;AAC9B,MAAM,YAAY,GAAG,WAAW,CAAC;AACjC,MAAM,eAAe,GAAG,QAAQ,CAAC;AAEjC,MAAM,gBAAgB,GAAG,CACvB,WAAmB,EACnB,WAAmB,EACnB,SAAiB,EACjB,EAAE;IACF,MAAM,KAAK,GAAG,WAAW,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IAC/C,MAAM,GAAG,GAAG,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAE3C,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE;QAChB,MAAM,IAAI,KAAK,CACb,wBAAwB,WAAW,wBAAwB,CAC5D,CAAC;KACH;IACD,IAAI,GAAG,KAAK,CAAC,CAAC,EAAE;QACd,MAAM,IAAI,KAAK,CAAC,sBAAsB,SAAS,wBAAwB,CAAC,CAAC;KAC1E;IAED,OAAO,WAAW,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AACvC,CAAC,CAAC;AAEF,MAAM,YAAY,GAAG,CAAC,IAAY,EAAE,EAAE,CACpC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,KAAK,IAAI,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC;AAE5E,MAAM,QAAQ,GAAG,CAAC,WAAmB,EAAE,EAAE;IACvC,OAAO;QACL,KAAK,EAAE,gBAAgB,CACrB,WAAW,EACX,gCAAgC,EAChC,8BAA8B,CAC/B;aACE,KAAK,CAAC,YAAY,CAAC;aACnB,MAAM,CAAC,YAAY,CAAC;aACpB,GAAG,CAAC,OAAO,CAAC;QACf,OAAO,EAAE,gBAAgB,CACvB,WAAW,EACX,kCAAkC,EAClC,gCAAgC,CACjC;aACE,KAAK,CAAC,YAAY,CAAC;aACnB,MAAM,CAAC,YAAY,CAAC;aACpB,GAAG,CAAC,OAAO,CAAC;KAChB,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,EAAE;IACxC,MAAM,SAAS,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;IAChC,MAAM,SAAS,GAAG,kBAAkB,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IACtD,MAAM,WAAW,GAAG,kBAAkB,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAE1D,OAAO;QACL,SAAS;QACT,WAAW;KACZ,CAAC;AACJ,CAAC,CAAC"}
File without changes
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { ok } from "assert";
11
11
  import fetch from "node-fetch";
12
- import { FETCH_PSL_EXPECTED_MIN_LENGTH, PUBLIC_SUFFIX_URL } from "../config";
12
+ import { FETCH_PSL_EXPECTED_MIN_LENGTH, PUBLIC_SUFFIX_URL } from "../config.js";
13
13
  export const fetchPsl = () => __awaiter(void 0, void 0, void 0, function* () {
14
14
  const response = yield fetch(PUBLIC_SUFFIX_URL);
15
15
  const pslContent = yield response.text();
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-psl.js","sourceRoot":"","sources":["../../src/psl/fetch-psl.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC5B,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,EAAE,6BAA6B,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEhF,MAAM,CAAC,MAAM,QAAQ,GAAG,GAAS,EAAE;IACjC,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,iBAAiB,CAAC,CAAC;IAChD,MAAM,UAAU,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAEzC,eAAe;IACf,EAAE,CACA,UAAU,CAAC,MAAM,IAAI,6BAA6B,EAClD,6CAA6C,CAC9C,CAAC;IAEF,OAAO,UAAU,CAAC;AACpB,CAAC,CAAA,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const toASCII: (hostname: string) => string;
@@ -0,0 +1,4 @@
1
+ export const toASCII = (hostname) => {
2
+ return new URL(`http://${hostname}`).hostname;
3
+ };
4
+ //# sourceMappingURL=punycode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"punycode.js","sourceRoot":"","sources":["../src/punycode.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,QAAgB,EAAE,EAAE;IAC1C,OAAO,IAAI,GAAG,CAAC,UAAU,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC;AAChD,CAAC,CAAC"}
@@ -1,12 +1,32 @@
1
- import isIp from "is-ip";
2
- import { Label } from "./parse-domain";
3
- import { NO_HOSTNAME } from "./from-url";
1
+ import { ipVersion } from "is-ip";
2
+ import { Label } from "./parse-domain.js";
3
+ import { NO_HOSTNAME } from "./from-url.js";
4
+ export declare enum Validation {
5
+ /**
6
+ * Allows any octets as labels
7
+ * but still restricts the length of labels and the overall domain.
8
+ *
9
+ * @see https://www.rfc-editor.org/rfc/rfc2181#section-11
10
+ **/
11
+ Lax = "LAX",
12
+ /**
13
+ * Only allows ASCII letters, digits and hyphens (aka LDH),
14
+ * forbids hyphens at the beginning or end of a label
15
+ * and requires top-level domain names not to be all-numeric.
16
+ *
17
+ * This is the default if no validation is configured.
18
+ *
19
+ * @see https://datatracker.ietf.org/doc/html/rfc3696#section-2
20
+ */
21
+ Strict = "STRICT"
22
+ }
4
23
  export declare enum ValidationErrorType {
5
24
  NoHostname = "NO_HOSTNAME",
6
25
  DomainMaxLength = "DOMAIN_MAX_LENGTH",
7
26
  LabelMinLength = "LABEL_MIN_LENGTH",
8
27
  LabelMaxLength = "LABEL_MAX_LENGTH",
9
- LabelInvalidCharacter = "LABEL_INVALID_CHARACTER"
28
+ LabelInvalidCharacter = "LABEL_INVALID_CHARACTER",
29
+ LastLabelInvalid = "LAST_LABEL_INVALID"
10
30
  }
11
31
  export declare type ValidationError = {
12
32
  type: ValidationErrorType;
@@ -21,7 +41,7 @@ export declare enum SanitizationResultType {
21
41
  export declare type SanitizationResultValidIp = {
22
42
  type: SanitizationResultType.ValidIp;
23
43
  ip: string;
24
- ipVersion: Exclude<ReturnType<typeof isIp.version>, undefined>;
44
+ ipVersion: Exclude<ReturnType<typeof ipVersion>, undefined>;
25
45
  };
26
46
  export declare type SanitizationResultValidDomain = {
27
47
  type: SanitizationResultType.ValidDomain;
@@ -33,4 +53,6 @@ export declare type SanitizationResultError = {
33
53
  errors: Array<ValidationError>;
34
54
  };
35
55
  export declare type SanitizationResult = SanitizationResultValidIp | SanitizationResultValidDomain | SanitizationResultError;
36
- export declare const sanitize: (input: string | typeof NO_HOSTNAME) => SanitizationResult;
56
+ export declare const sanitize: (input: string | typeof NO_HOSTNAME, options?: {
57
+ validation?: Validation;
58
+ }) => SanitizationResult;