-
-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(encode/decode_url): replace decodeURI with unescape #214
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
I will update the highlight test cases in another PR. |
I'm doing it. |
Ops, I have done it in #215. |
before merging this, let me benchmark it first. |
no regression when using const Benchmark = require('benchmark');
const Suite = new Benchmark.Suite;
const normal = 'http://example.com/foo-bar/'
const safe = 'http://example.com/%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%83%86%e3%82%b9%e3%83%88/'
const unsafe = 'http://example.com/100%%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%83%86%e3%82%b9%e3%83%88/'
const safeDecodeURI = str => {
try {
return decodeURI(str);
} catch (err) {
return str;
}
};
const { unescape } = require('querystring');
Suite.add('normal - safedecodeuri', () => {
safeDecodeURI(normal)
}).add('normal - unescape', () => {
unescape(normal)
}).add('safe - safedecodeuri', () => {
safeDecodeURI(safe)
}).add('safe - unescape', () => {
unescape(safe)
}).add('unsafe - safedecodeuri', () => {
safeDecodeURI(unsafe)
}).add('unsafe - unescape', () => {
unescape(unsafe)
}).on('cycle', function(event) {
console.info(String(event.target));
}).run();
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that Node.js is handling unsafe query string with try ... catch ...
syntax as well, thus there will be no performance gained from this PR. However, I do agree utilizing the Node.js API.
unescape()
is a safer alternative that doesn't throw error when encounter malformed URI.