Skip to content

Commit 3a3fb3d

Browse files
committed
fix(server): add checkUrl util & unistall is-absolute-url
1 parent a1dea72 commit 3a3fb3d

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

lib/Server.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const normalizeOptions = require('./utils/normalizeOptions');
2727
const updateCompiler = require('./utils/updateCompiler');
2828
const createLogger = require('./utils/createLogger');
2929
const getCertificate = require('./utils/getCertificate');
30+
const checkUrl = require('./utils/checkUrl');
3031
const status = require('./utils/status');
3132
const createDomain = require('./utils/createDomain');
3233
const runBonjour = require('./utils/runBonjour');
@@ -338,7 +339,7 @@ class Server {
338339
contentBase.forEach((item) => {
339340
this.app.get('*', express.static(item));
340341
});
341-
} else if (/^(https?:)?\/\//.test(contentBase)) {
342+
} else if (checkUrl(contentBase)) {
342343
this.log.warn(
343344
'Using a URL as contentBase is deprecated and will be removed in the next major version. Please use the proxy option instead.'
344345
);
@@ -389,25 +390,19 @@ class Server {
389390
contentBase.forEach((item) => {
390391
this.app.get('*', serveIndex(item));
391392
});
392-
} else if (
393-
typeof contentBase !== 'number' &&
394-
!/^(https?:)?\/\//.test(contentBase)
395-
) {
393+
} else if (typeof contentBase !== 'number' && !checkUrl(contentBase)) {
396394
this.app.get('*', serveIndex(contentBase));
397395
}
398396
}
399397

400398
setupWatchStaticFeature() {
401399
const contentBase = this.options.contentBase;
402400

403-
if (
404-
/^(https?:)?\/\//.test(contentBase) ||
405-
typeof contentBase === 'number'
406-
) {
401+
if (checkUrl(contentBase) || typeof contentBase === 'number') {
407402
throw new Error('Watching remote files is not supported.');
408403
} else if (Array.isArray(contentBase)) {
409404
contentBase.forEach((item) => {
410-
if (/^(https?:)?\/\//.test(item) || typeof item === 'number') {
405+
if (checkUrl(item) || typeof item === 'number') {
411406
throw new Error('Watching remote files is not supported.');
412407
}
413408
this._watch(item);

lib/utils/checkUrl.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
function checkUrl(url) {
4+
return /^(https?:)?\/\//.test(url);
5+
}
6+
7+
module.exports = checkUrl;

lib/utils/createConfig.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('path');
44
const defaultTo = require('./defaultTo');
5+
const checkUrl = require('./checkUrl');
56

67
function createConfig(config, argv, { port }) {
78
const firstWpOpt = Array.isArray(config) ? config[0] : config;
@@ -63,10 +64,7 @@ function createConfig(config, argv, { port }) {
6364
options.publicPath =
6465
(firstWpOpt.output && firstWpOpt.output.publicPath) || '';
6566

66-
if (
67-
!/^(https?:)?\/\//.test(options.publicPath) &&
68-
options.publicPath[0] !== '/'
69-
) {
67+
if (!checkUrl(options.publicPath) && options.publicPath[0] !== '/') {
7068
options.publicPath = `/${options.publicPath}`;
7169
}
7270
}
@@ -113,7 +111,7 @@ function createConfig(config, argv, { port }) {
113111
options.contentBase = options.contentBase.map((p) => path.resolve(p));
114112
} else if (/^[0-9]$/.test(options.contentBase)) {
115113
options.contentBase = +options.contentBase;
116-
} else if (!/^(https?:)?\/\//.test(options.contentBase)) {
114+
} else if (!checkUrl(options.contentBase)) {
117115
options.contentBase = path.resolve(options.contentBase);
118116
}
119117
}

lib/utils/runOpen.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const open = require('opn');
4+
const checkUrl = require('./checkUrl');
45

56
function runOpen(uri, options, log) {
67
// https://github.com/webpack/webpack-dev-server/issues/1990
@@ -13,7 +14,7 @@ function runOpen(uri, options, log) {
1314
}
1415

1516
const pageUrl =
16-
options.openPage && /^(https?:)?\/\//.test(options.openPage)
17+
options.openPage && checkUrl(options.openPage)
1718
? options.openPage
1819
: `${uri}${options.openPage || ''}`;
1920

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"import-local": "^2.0.0",
5050
"internal-ip": "^4.3.0",
5151
"ip": "^1.1.5",
52-
"is-absolute-url": "^3.0.0",
5352
"killable": "^1.0.1",
5453
"loglevel": "^1.6.3",
5554
"opn": "^5.5.0",

0 commit comments

Comments
 (0)