UNPKG

@translated/lara

Version:

Official Lara SDK for JavaScript and Node.js

69 lines (68 loc) 3.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AudioTranslator = exports.VoiceGender = exports.AudioStatus = void 0; const errors_1 = require("./errors"); const s3_1 = __importDefault(require("./net/s3")); // biome-ignore format: keep comments aligned var AudioStatus; (function (AudioStatus) { AudioStatus["INITIALIZED"] = "initialized"; AudioStatus["ANALYZING"] = "analyzing"; AudioStatus["PAUSED"] = "paused"; AudioStatus["READY"] = "ready"; AudioStatus["TRANSLATING"] = "translating"; AudioStatus["TRANSLATED"] = "translated"; AudioStatus["ERROR"] = "error"; })(AudioStatus || (exports.AudioStatus = AudioStatus = {})); var VoiceGender; (function (VoiceGender) { VoiceGender["MALE"] = "male"; VoiceGender["FEMALE"] = "female"; })(VoiceGender || (exports.VoiceGender = VoiceGender = {})); class AudioTranslator { constructor(client) { this.client = client; this.s3Client = (0, s3_1.default)(); } async upload(file, filename, source, target, options) { const { url, fields } = await this.client.get(`/v2/audio/upload-url`, { filename }); await this.s3Client.upload(url, fields, file, options === null || options === void 0 ? void 0 : options.contentLength); const headers = (options === null || options === void 0 ? void 0 : options.noTrace) ? { "X-No-Trace": "true" } : {}; return this.client.post(`/v2/audio/translate`, { source, target, s3key: fields.key, adapt_to: options === null || options === void 0 ? void 0 : options.adaptTo, glossaries: options === null || options === void 0 ? void 0 : options.glossaries, style: options === null || options === void 0 ? void 0 : options.style, voice_gender: options === null || options === void 0 ? void 0 : options.voiceGender }, undefined, headers); } async status(id) { return await this.client.get(`/v2/audio/${id}`); } async download(id) { const { url } = await this.client.get(`/v2/audio/${id}/download-url`); return (await this.s3Client.downloadStream(url)); } async translate(file, filename, source, target, options) { const { id } = await this.upload(file, filename, source, target, options); const pollingInterval = 2000; const maxWaitTime = 1000 * 60 * 15; // 15 minutes const start = Date.now(); while (Date.now() - start < maxWaitTime) { await new Promise((resolve) => setTimeout(resolve, pollingInterval)); const { status, errorReason } = await this.status(id); if (status === AudioStatus.TRANSLATED) return await this.download(id); if (status === AudioStatus.ERROR) { throw new errors_1.LaraApiError(500, "AudioError", errorReason); } } throw new errors_1.TimeoutError(); } } exports.AudioTranslator = AudioTranslator;