Skip to content

Commit

Permalink
fix(server): add checkUrl util & unistall is-absolute-url
Browse files Browse the repository at this point in the history
  • Loading branch information
EslamHiko committed Aug 16, 2019
1 parent a1dea72 commit 3a3fb3d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
15 changes: 5 additions & 10 deletions lib/Server.js
Expand Up @@ -27,6 +27,7 @@ const normalizeOptions = require('./utils/normalizeOptions');
const updateCompiler = require('./utils/updateCompiler');
const createLogger = require('./utils/createLogger');
const getCertificate = require('./utils/getCertificate');
const checkUrl = require('./utils/checkUrl');
const status = require('./utils/status');
const createDomain = require('./utils/createDomain');
const runBonjour = require('./utils/runBonjour');
Expand Down Expand Up @@ -338,7 +339,7 @@ class Server {
contentBase.forEach((item) => {
this.app.get('*', express.static(item));
});
} else if (/^(https?:)?\/\//.test(contentBase)) {
} else if (checkUrl(contentBase)) {
this.log.warn(
'Using a URL as contentBase is deprecated and will be removed in the next major version. Please use the proxy option instead.'
);
Expand Down Expand Up @@ -389,25 +390,19 @@ class Server {
contentBase.forEach((item) => {
this.app.get('*', serveIndex(item));
});
} else if (
typeof contentBase !== 'number' &&
!/^(https?:)?\/\//.test(contentBase)
) {
} else if (typeof contentBase !== 'number' && !checkUrl(contentBase)) {
this.app.get('*', serveIndex(contentBase));
}
}

setupWatchStaticFeature() {
const contentBase = this.options.contentBase;

if (
/^(https?:)?\/\//.test(contentBase) ||
typeof contentBase === 'number'
) {
if (checkUrl(contentBase) || typeof contentBase === 'number') {
throw new Error('Watching remote files is not supported.');
} else if (Array.isArray(contentBase)) {
contentBase.forEach((item) => {
if (/^(https?:)?\/\//.test(item) || typeof item === 'number') {
if (checkUrl(item) || typeof item === 'number') {
throw new Error('Watching remote files is not supported.');
}
this._watch(item);
Expand Down
7 changes: 7 additions & 0 deletions lib/utils/checkUrl.js
@@ -0,0 +1,7 @@
'use strict';

function checkUrl(url) {
return /^(https?:)?\/\//.test(url);
}

module.exports = checkUrl;
8 changes: 3 additions & 5 deletions lib/utils/createConfig.js
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const defaultTo = require('./defaultTo');
const checkUrl = require('./checkUrl');

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

if (
!/^(https?:)?\/\//.test(options.publicPath) &&
options.publicPath[0] !== '/'
) {
if (!checkUrl(options.publicPath) && options.publicPath[0] !== '/') {
options.publicPath = `/${options.publicPath}`;
}
}
Expand Down Expand Up @@ -113,7 +111,7 @@ function createConfig(config, argv, { port }) {
options.contentBase = options.contentBase.map((p) => path.resolve(p));
} else if (/^[0-9]$/.test(options.contentBase)) {
options.contentBase = +options.contentBase;
} else if (!/^(https?:)?\/\//.test(options.contentBase)) {
} else if (!checkUrl(options.contentBase)) {
options.contentBase = path.resolve(options.contentBase);
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/runOpen.js
@@ -1,6 +1,7 @@
'use strict';

const open = require('opn');
const checkUrl = require('./checkUrl');

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

const pageUrl =
options.openPage && /^(https?:)?\/\//.test(options.openPage)
options.openPage && checkUrl(options.openPage)
? options.openPage
: `${uri}${options.openPage || ''}`;

Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -49,7 +49,6 @@
"import-local": "^2.0.0",
"internal-ip": "^4.3.0",
"ip": "^1.1.5",
"is-absolute-url": "^3.0.0",
"killable": "^1.0.1",
"loglevel": "^1.6.3",
"opn": "^5.5.0",
Expand Down

0 comments on commit 3a3fb3d

Please sign in to comment.