Skip to content

Commit

Permalink
use url-regex
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Oct 15, 2021
1 parent 5d30eac commit 0e9a164
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
11 changes: 2 additions & 9 deletions .babelrc.js
@@ -1,17 +1,10 @@
console.log('Load babel config');

module.exports = api => {
api.cache(false);
return {
presets: [
[
'@babel/preset-env',
api.env('test')
? { targets: { node: true } }
: {
loose: true,
modules: false,
},
],
['@babel/preset-env', { targets: { node: true } }],

This comment has been minimized.

Copy link
@afc163

This comment has been minimized.

Copy link
@yiminghe

yiminghe Oct 18, 2021

Author Owner

4.0.6

'@babel/preset-typescript',
],
};
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -89,5 +89,8 @@
},
"pre-commit": [
"lint-staged"
]
],
"dependencies": {
"url-regex": "^5.0.0"

This comment has been minimized.

Copy link
@afc163

afc163 Oct 18, 2021

Contributor

https://bundlephobia.com/package/url-regex@5.0.0

这个有 6.7kB,感觉不是很划算。

This comment has been minimized.

Copy link
@yiminghe

yiminghe via email Oct 18, 2021

Author Owner
}
}
8 changes: 3 additions & 5 deletions src/rule/type.ts
@@ -1,16 +1,14 @@
import { ExecuteRule, Value } from '../interface';
import { format } from '../util';
import required from './required';
import urlRegex from 'url-regex';

/* eslint max-len:0 */

const pattern = {
// http://emailregex.com/
email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/,
url: new RegExp(
'^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
'i',
),
url: urlRegex({ exact: true }),
hex: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i,
};

Expand Down Expand Up @@ -58,7 +56,7 @@ const types = {
return typeof value === 'string' && !!value.match(pattern.email);
},
url(value: Value) {
return typeof value === 'string' && !!value.match(pattern.url);
return typeof value === 'string' && value.length <= 2048 && !!value.match(pattern.url);
},
hex(value: Value) {
return typeof value === 'string' && !!value.match(pattern.hex);
Expand Down
19 changes: 19 additions & 0 deletions tests/url.js
@@ -0,0 +1,19 @@
import AsyncValidator from '../src/';

const validator = new AsyncValidator({
v: {
type: 'url',
},
});

for (var i = 1; i <= 50000; i++) {
var time = Date.now();
var attack_str = '//' + ':'.repeat(i * 10000) + '@';
validator.validate({
v: attack_str,
});
var time_cost = Date.now() - time;
console.log(
'attack_str.length: ' + attack_str.length + ': ' + time_cost + ' ms',
);
}

0 comments on commit 0e9a164

Please sign in to comment.