1 | 'use strict';
|
2 |
|
3 | const { URL } = require('url');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = function (urlString) {
|
13 | let protocol = null;
|
14 |
|
15 | try {
|
16 | protocol = new URL(urlString).protocol;
|
17 | } catch (err) {
|
18 | return null;
|
19 | }
|
20 |
|
21 | if (protocol === null || typeof protocol === 'undefined') {
|
22 | return null;
|
23 | }
|
24 |
|
25 | const scheme = protocol.slice(0, -1);
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const slashIndex = protocol.length;
|
31 | const expectedSlashes = urlString.slice(slashIndex, slashIndex + 2);
|
32 | const isSchemeLessUrl = expectedSlashes !== '//' && scheme !== 'data';
|
33 |
|
34 | if (isSchemeLessUrl) {
|
35 | return null;
|
36 | }
|
37 |
|
38 | return scheme;
|
39 | };
|