Skip to content

Commit

Permalink
refactor: migrate from got to undici
Browse files Browse the repository at this point in the history
Signed-off-by: TRACTION <19631364+iamtraction@users.noreply.github.com>
  • Loading branch information
iamtraction committed Oct 14, 2022
1 parent 62a2ccf commit 08e93b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const querystring = require("querystring");
const got = require("got");
const { request } = require("undici");

const languages = require("./languages");
const tokenGenerator = require("./tokenGenerator");
Expand Down Expand Up @@ -79,7 +79,8 @@ async function translate(text, options) {
}

// Request translation from Google Translate.
let response = await got(...requestOptions);
let response = await request(...requestOptions);
let body = await response.body.json();

let result = {
text: "",
Expand All @@ -99,12 +100,10 @@ async function translate(text, options) {

// If user requested a raw output, add the raw response to the result
if (options.raw) {
result.raw = response.body;
result.raw = body;
}

// Parse string body to JSON and add it to result object.

let body = JSON.parse(response.body);
// Parse body and add it to the result object.
body[0].forEach((obj) => {
if (obj[0]) {
result.text += obj[0];
Expand Down
35 changes: 12 additions & 23 deletions src/tokenGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Everything between 'BEGIN' and 'END' was copied from the script above.
*/

const got = require("got");
const { request } = require("undici");

/* eslint-disable */
// BEGIN
Expand Down Expand Up @@ -66,33 +66,22 @@ const window = {

// eslint-disable-next-line require-jsdoc
async function updateTKK() {
try {
let now = Math.floor(Date.now() / 3600000);
let now = Math.floor(Date.now() / 3600000);

if (Number(window.TKK.split(".")[0]) !== now) {
let res = await got("https://translate.google.com");
if (Number(window.TKK.split(".")[0]) !== now) {
const response = await request("https://translate.google.com");
const body = await response.body.text();

// code will extract something like tkk:'1232135.131231321312', we need only value
const code = res.body.match(/tkk:'\d+.\d+'/g);
// code will extract something like tkk:'1232135.131231321312', we need only value
const code = body.match(/tkk:'\d+.\d+'/g);

if (code.length > 0) {
// extracting value tkk:'1232135.131231321312', this will extract only token: 1232135.131231321312
const xt = code[0].split(":")[1].replace(/'/g, "");
if (code.length > 0) {
// extracting value tkk:'1232135.131231321312', this will extract only token: 1232135.131231321312
const xt = code[0].split(":")[1].replace(/'/g, "");

window.TKK = xt;
config.set("TKK", xt);
}
}
}
catch (e) {
if (e.name === "HTTPError") {
let error = new Error();
error.name = e.name;
error.statusCode = e.statusCode;
error.statusMessage = e.statusMessage;
throw error;
window.TKK = xt;
config.set("TKK", xt);
}
throw e;
}
}

Expand Down

0 comments on commit 08e93b3

Please sign in to comment.