Fix uuidv4 on Node 18

Closes #2718
This commit is contained in:
Cohee 2024-08-27 09:35:00 +00:00
parent a0a1847634
commit 3806dcf9fb
2 changed files with 22 additions and 22 deletions

View File

@ -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,

View File

@ -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;