From 3806dcf9fbd767819398acb8890f8bad6483cce7 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 27 Aug 2024 09:35:00 +0000 Subject: [PATCH] Fix uuidv4 on Node 18 Closes #2718 --- src/endpoints/translate.js | 40 +++++++++++++++++++------------------- src/util.js | 4 ++-- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/endpoints/translate.js b/src/endpoints/translate.js index 7260647d9..6b272e27e 100644 --- a/src/endpoints/translate.js +++ b/src/endpoints/translate.js @@ -110,27 +110,27 @@ router.post('/google', jsonParser, async (request, response) => { }); router.post('/yandex', jsonParser, async (request, response) => { - const chunks = request.body.chunks; - const lang = request.body.lang; - - if (!chunks || !lang) { - return response.sendStatus(400); - } - - // reconstruct original text to log - let inputText = ''; - - const params = new URLSearchParams(); - for (const chunk of chunks) { - params.append('text', chunk); - inputText += chunk; - } - params.append('lang', lang); - const ucid = uuidv4().replaceAll('-', ''); - - console.log('Input text: ' + inputText); - try { + const chunks = request.body.chunks; + const lang = request.body.lang; + + if (!chunks || !lang) { + return response.sendStatus(400); + } + + // reconstruct original text to log + let inputText = ''; + + const params = new URLSearchParams(); + for (const chunk of chunks) { + params.append('text', chunk); + inputText += chunk; + } + params.append('lang', lang); + const ucid = uuidv4().replaceAll('-', ''); + + console.log('Input text: ' + inputText); + const result = await fetch(`https://translate.yandex.net/api/v1/tr.json/translate?ucid=${ucid}&srv=android&format=text`, { method: 'POST', body: params, diff --git a/src/util.js b/src/util.js index 278e37498..609f8cecb 100644 --- a/src/util.js +++ b/src/util.js @@ -299,8 +299,8 @@ const color = { * @returns {string} A UUIDv4 string */ function uuidv4() { - if ('randomUUID' in crypto) { - return crypto.randomUUID(); + if ('crypto' in global && 'randomUUID' in global.crypto) { + return global.crypto.randomUUID(); } return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = Math.random() * 16 | 0;