From 0f63c50c063e072f034ead0d6a94ff31b0e3fd5d Mon Sep 17 00:00:00 2001 From: Kyra Date: Fri, 10 Aug 2018 11:23:22 +0200 Subject: [PATCH] fix: Util.basename being unreliable (#2679) * fix: Util.basename being unreliable new URL for WHATWG parsing was not chosen because it only works for URLs and threw in local pathes, path.basename is unreliable (according to the devs' note), and path.parse seems to work well. * docs: Update Util.basename's description --- src/util/Util.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/util/Util.js b/src/util/Util.js index 154903486..a214695a0 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -2,7 +2,7 @@ const { Colors, DefaultOptions, Endpoints } = require('./Constants'); const fetch = require('node-fetch'); const { Error: DiscordError, RangeError, TypeError } = require('../errors'); const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k); -const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/; +const { parse } = require('path'); /** * Contains various general-purpose utility methods. These functions are also available on the base `Discord` object. @@ -323,16 +323,15 @@ class Util { } /** - * Alternative to Node's `path.basename` that we have for some (probably stupid) reason. + * Alternative to Node's `path.basename`, removing query string after the extension if it exists. * @param {string} path Path to get the basename of * @param {string} [ext] File extension to remove * @returns {string} Basename of the path * @private */ static basename(path, ext) { - let f = splitPathRe.exec(path)[3]; - if (ext && f.endsWith(ext)) f = f.slice(0, -ext.length); - return f; + let res = parse(path); + return ext && res.ext.startsWith(ext) ? res.name : res.base.split('?')[0]; } /**