//------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ /** * Escapes the control characters of a given string. * @param {string} s - A string to escape. * @returns {string} An escaped string. */ function escape(s) { const isOneChar = s.length === 1; s = lodash.escapeRegExp(s); return isOneChar ? s : `(?:${s})`; }
/** * Gets the index of a given variable name in a given comment. * @param {eslint-scope.Variable} variable - A variable to get. * @param {ASTNode} comment - A comment node which includes the variable name. * @returns {number} The index of the variable name's location. * @private */ function getColumnInComment(variable, comment) { const namePattern = new RegExp(`[\\s,]${lodash.escapeRegExp(variable.name)}(?:$|[\\s,:])`, "g"); // To ignore the first text "global". namePattern.lastIndex = comment.value.indexOf("global") + 6; // Search a given variable name. const match = namePattern.exec(comment.value); return match ? match.index + 1 : 0; }
function checkOrigin(origin, settings) { if (_.isString(settings)) { if (settings.indexOf(origin) !== -1) return true; if (settings.indexOf("*") !== -1) { // Based on: https://github.com/hapijs/hapi // eslint-disable-next-line const wildcard = new RegExp(`^${_.escapeRegExp(settings).replace(/\\\*/g, ".*").replace(/\\\?/g, ".")}$`); return origin.match(wildcard); } } else if (Array.isArray(settings)) { for (let i = 0; i < settings.length; i++) { if (checkOrigin(origin, settings[i])) { return true; } } } return false; }
app.post("/login", async (req, res) => { try { let user = await UsersModel.findOne({ username: { $regex: _.escapeRegExp(req.body.username), $options: "i" } }) .lean() .exec(); if (user && bcrypt.compareSync(req.body.password, user.password)) { const token = createToken({ id: user._id, username: user.username }); res.cookie("token", token, { httpOnly: true }); res.status(200).send({ message: "User login success." }); } else res .status(400) .send({ message: "User not exist or password not correct" }); } catch (e) { console.error("E, login,", e); res.status(500).send({ message: "some error" }); } });
app.post("/register", async (req, res) => { try { let user = await UsersModel.findOne({ username: { $regex: _.escapeRegExp(req.body.username), $options: "i" } }) .lean() .exec(); if (user) { return res.status(400).send({ message: "User already exist" }); } user = await UsersModel.create({ username: req.body.username, password: req.body.password }); const token = createToken({ id: user._id, username: user.username }); res.cookie("token", token, { httpOnly: true }); res.status(200).send({ message: "User created." }); } catch (e) { console.error("E, register,", e); res.status(500).send({ message: "some error" }); } });
const escapeStr = (path) => _.escapeRegExp(path).replace(digitsPlaceholder, '\\d+')
function checkOrigin(origin, settings) { if (_.isString(settings)) { if (settings.indexOf(origin) !== -1) return true; if (settings.indexOf("*") !== -1) { // Based on: https://github.com/hapijs/hapi // eslint-disable-next-line const wildcard = new RegExp(`^${_.escapeRegExp(settings).replace(/\\\*/g, ".*").replace(/\\\?/g, ".")}$`) return origin.match(wildcard); } } else if (Array.isArray(settings)) { for(let i = 0; i < settings.length; i++) { if (checkOrigin(origin, settings[i])) { return true; } } } return false; }