translate is working
This commit is contained in:
parent
974be37b3b
commit
f37e215c22
@ -4,7 +4,7 @@
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import OperationConfig from "./config/OperationConfig.json" assert {type: "json"};
|
||||
import OperationConfig from "./config/OperationConfig.json" with {type: "json"};
|
||||
import OperationError from "./errors/OperationError.mjs";
|
||||
import Operation from "./Operation.mjs";
|
||||
import DishError from "./errors/DishError.mjs";
|
||||
@ -17,7 +17,7 @@ let modules = null;
|
||||
/**
|
||||
* The Recipe controls a list of Operations and the Dish they operate on.
|
||||
*/
|
||||
class Recipe {
|
||||
class Recipe {
|
||||
|
||||
/**
|
||||
* Recipe constructor
|
||||
@ -176,7 +176,7 @@ class Recipe {
|
||||
* @returns {number}
|
||||
* - The final progress through the recipe
|
||||
*/
|
||||
async execute(dish, startFrom=0, forkState={}) {
|
||||
async execute(dish, startFrom = 0, forkState = {}) {
|
||||
let op, input, output,
|
||||
numJumps = 0,
|
||||
numRegisters = forkState.numRegisters || 0;
|
||||
@ -204,19 +204,19 @@ class Recipe {
|
||||
log.debug(`Executing operation '${op.name}'`);
|
||||
|
||||
if (isWorkerEnvironment()) {
|
||||
self.sendStatusMessage(`Baking... (${i+1}/${this.opList.length})`);
|
||||
self.sendStatusMessage(`Baking... (${i + 1}/${this.opList.length})`);
|
||||
self.sendProgressMessage(i + 1, this.opList.length);
|
||||
}
|
||||
|
||||
if (op.flowControl) {
|
||||
// Package up the current state
|
||||
let state = {
|
||||
"progress": i,
|
||||
"dish": dish,
|
||||
"opList": this.opList,
|
||||
"numJumps": numJumps,
|
||||
"progress": i,
|
||||
"dish": dish,
|
||||
"opList": this.opList,
|
||||
"numJumps": numJumps,
|
||||
"numRegisters": numRegisters,
|
||||
"forkOffset": forkState.forkOffset || 0
|
||||
"forkOffset": forkState.forkOffset || 0
|
||||
};
|
||||
|
||||
state = await op.run(state);
|
||||
@ -339,7 +339,7 @@ class Recipe {
|
||||
*/
|
||||
lastOpPresented(progress) {
|
||||
if (progress < 1) return false;
|
||||
return this.opList[progress-1].presentType !== this.opList[progress-1].outputType;
|
||||
return this.opList[progress - 1].presentType !== this.opList[progress - 1].outputType;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import OperationConfig from "../config/OperationConfig.json" assert {type: "json"};
|
||||
import OperationConfig from "../config/OperationConfig.json" with {type: "json"};
|
||||
import Utils, { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import Recipe from "../Recipe.mjs";
|
||||
import Dish from "../Dish.mjs";
|
||||
|
||||
@ -59,7 +59,7 @@ class GoogleTranslate extends Operation {
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
async run(input, args) {
|
||||
const [sourceLanguage, targetLanguage, authType, authString] = args;
|
||||
|
||||
if (input.length === 0) return "";
|
||||
@ -90,29 +90,31 @@ class GoogleTranslate extends Operation {
|
||||
cache: "no-cache",
|
||||
};
|
||||
|
||||
return fetch(url, config)
|
||||
.then(r => {
|
||||
if (!r.ok) {
|
||||
return r.json().then(err => {
|
||||
let msg = err?.error?.message || r.statusText;
|
||||
throw new OperationError(`Google Translation API Error (${r.status}): ${msg}`);
|
||||
}).catch(() => {
|
||||
throw new OperationError(`Google Translation API Error: ${r.status} ${r.statusText}`);
|
||||
});
|
||||
}
|
||||
return r.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data && data.data && data.data.translations && data.data.translations.length > 0) {
|
||||
return data.data.translations[0].translatedText;
|
||||
}
|
||||
throw new OperationError("Error: Unexpected response format from Google Translation API.");
|
||||
})
|
||||
.catch(e => {
|
||||
if (e instanceof OperationError) throw e;
|
||||
throw new OperationError(e.toString() +
|
||||
"\n\nThis error could be caused by a network issue or invalid authentication.");
|
||||
});
|
||||
try {
|
||||
const response = await fetch(url, config);
|
||||
let responseData;
|
||||
|
||||
try {
|
||||
responseData = await response.json();
|
||||
} catch (err) {
|
||||
throw new OperationError("Error: Failed to parse response from Google Translation API.");
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const msg = responseData?.error?.message || response.statusText;
|
||||
throw new OperationError(`Google Translation API Error (${response.status}): ${msg}`);
|
||||
}
|
||||
|
||||
if (responseData && responseData.data && responseData.data.translations && responseData.data.translations.length > 0) {
|
||||
return responseData.data.translations[0].translatedText;
|
||||
}
|
||||
|
||||
throw new OperationError("Error: Unexpected response format from Google Translation API.");
|
||||
} catch (e) {
|
||||
if (e.name === "OperationError") throw e;
|
||||
throw new OperationError(e.message || e.toString() +
|
||||
"\n\nThis error could be caused by a network issue or invalid authentication.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
import NodeDish from "./NodeDish.mjs";
|
||||
import NodeRecipe from "./NodeRecipe.mjs";
|
||||
import OperationConfig from "../core/config/OperationConfig.json" assert {type: "json"};
|
||||
import OperationConfig from "../core/config/OperationConfig.json" with {type: "json"};
|
||||
import { sanitise, removeSubheadingsFromArray, sentenceToCamelCase } from "./apiUtils.mjs";
|
||||
import ExcludedOperationError from "../core/errors/ExcludedOperationError.mjs";
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import sm from "sitemap";
|
||||
import OperationConfig from "../../core/config/OperationConfig.json" assert { type: "json" };
|
||||
import OperationConfig from "../../core/config/OperationConfig.json" with {type: "json"};
|
||||
|
||||
/**
|
||||
* Generates an XML sitemap for all CyberChef operations and a number of recipes.
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import TestRegister from "../../lib/TestRegister.mjs";
|
||||
import Categories from "../../../src/core/config/Categories.json" assert {type: "json"};
|
||||
import OperationConfig from "../../../src/core/config/OperationConfig.json" assert {type: "json"};
|
||||
import Categories from "../../../src/core/config/Categories.json" with {type: "json"};
|
||||
import OperationConfig from "../../../src/core/config/OperationConfig.json" with {type: "json"};
|
||||
import it from "../assertionHandler.mjs";
|
||||
import assert from "assert";
|
||||
|
||||
|
||||
20
tests/operations/tests/GoogleTranslate.mjs
Normal file
20
tests/operations/tests/GoogleTranslate.mjs
Normal file
@ -0,0 +1,20 @@
|
||||
import TestRegister from "../../lib/TestRegister.mjs";
|
||||
|
||||
TestRegister.addTests([
|
||||
{
|
||||
name: "Google Translate: Missing Auth String",
|
||||
input: "Hello world",
|
||||
expectedError: "Error: Please provide a valid GCP Auth String (API Key or OAuth Token).",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Google Translate",
|
||||
"args": [
|
||||
"en",
|
||||
"es",
|
||||
"API Key",
|
||||
""
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]);
|
||||
Loading…
x
Reference in New Issue
Block a user