You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit introduces two different validation modes:
- Strict (default): Only allows letters, digits, hyphens
- Lax: Allows any octets and just checks for the max lengths
This allows domains to have an underscore character. Closes#134
BREAKING CHANGE: Introduces a dependency on the global `TextEncoder` constructor which should be available in all modern engines
(see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder). The strict validation mode (which is the default) will also be a little bit more strict since it will now also check for hyphens at the beginning or end of a domain label. It also requires top-level domain names not to be all-numeric.
// 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,7 +32,7 @@ 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, 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), [`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
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/)).
38
38
@@ -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`:
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)).
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";
283
297
284
298
constparseResult=parseDomain("www.example.com");
285
299
```
286
300
301
+
Use `Validation.Lax` if you want to allow all characters:
@@ -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
+
<h3id="api-ts-ParseDomainOptions">
324
+
🧬 <code>export type ParseDomainOptions</code>
325
+
</h3>
326
+
327
+
```ts
328
+
exporttypeParseDomainOptions= {
329
+
/**
330
+
* If no validation is specified, Validation.Strict will be used.
331
+
**/
332
+
validation?:Validation;
333
+
};
334
+
```
335
+
336
+
<h3id="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
+
exportconstValidation= {
344
+
/**
345
+
* Allows any octets as labels
346
+
* but still restricts the length of labels and the overall domain.
0 commit comments