From 157b849868906d3fa7b94d787becb51336676473 Mon Sep 17 00:00:00 2001 From: JounQin Date: Mon, 30 May 2022 21:50:55 +0800 Subject: [PATCH] feat: upgrade to got v12 close #2 --- api/_deepl.js | 67 +++++++++++++++++++++++++------------------- api/package.json | 2 +- package.json | 2 +- pnpm-lock.yaml | 52 ++++++++++++++++++++-------------- src/api.ts | 71 ++++++++++++++++++++++++++++------------------- src/cli.ts | 2 +- src/extractors.ts | 2 +- src/generators.ts | 4 +-- src/utils.ts | 9 +++++- 9 files changed, 126 insertions(+), 85 deletions(-) diff --git a/api/_deepl.js b/api/_deepl.js index 4864995..dd7f579 100644 --- a/api/_deepl.js +++ b/api/_deepl.js @@ -2,12 +2,6 @@ Object.defineProperty(exports, '__esModule', { value: true }); -var _got = require('got'); - -function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } - -var _got__default = /*#__PURE__*/_interopDefaultLegacy(_got); - function extractTranslatedSentences(response) { return response.result.translations.reduce((sentences, translation) => { sentences.push(translation.beams[0].postprocessed_sentence); @@ -135,6 +129,9 @@ function createAbbreviationsDictionary(languages = SUPPORTED_LANGUAGES) { function abbreviateLanguage(language) { return createAbbreviationsDictionary()[language.toLowerCase()]; } +function importEsm(modulePath) { + return new Function("modulePath", "return import(modulePath)")(modulePath); +} var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { @@ -156,32 +153,42 @@ var __async = (__this, __arguments, generator) => { step((generator = generator.apply(__this, __arguments)).next()); }); }; -const got = _got__default["default"].extend({ - headers: { - accept: "*/*", - "accept-language": "en-US;q=0.8,en;q=0.7", - authority: "www2.deepl.com", - "content-type": "application/json", - origin: "https://www.deepl.com", - referer: "https://www.deepl.com/translator", - "sec-fetch-dest": "empty", - "sec-fetch-mode": "cors", - "sec-fetch-site": "same-site", - "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36" - }, - stringifyJson(object) { - return JSON.stringify(object).replace('"method":"', () => { - const self = object; - if ((self.id + 3) % 13 === 0 || (self.id + 5) % 29 === 0) { - return '"method" : "'; - } - return '"method": "'; - }); - } -}); +let _got; +function getGot() { + return __async(this, null, function* () { + if (!_got) { + const got = yield importEsm("got"); + _got = got.default.extend({ + headers: { + accept: "*/*", + "accept-language": "en-US;q=0.8,en;q=0.7", + authority: "www2.deepl.com", + "content-type": "application/json", + origin: "https://www.deepl.com", + referer: "https://www.deepl.com/translator", + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "same-site", + "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36" + }, + stringifyJson(object) { + return JSON.stringify(object).replace('"method":"', () => { + const self = object; + if ((self.id + 3) % 13 === 0 || (self.id + 5) % 29 === 0) { + return '"method" : "'; + } + return '"method": "'; + }); + } + }); + } + return _got; + }); +} function splitSentences(text, sourceLanguage, identifier) { return __async(this, null, function* () { const data = generateSplitSentencesRequestData(text, sourceLanguage, identifier); + const got = yield getGot(); return yield got.post(API_URL, { json: data }).json(); @@ -196,6 +203,7 @@ function requestTranslation(text, targetLanguage, sourceLanguage, identifier, al return __async(this, null, function* () { const res = yield splitSentences(text, sourceLanguage, identifier); const data = generateTranslationRequestData(sourceLanguage === "auto" ? res.result.lang : sourceLanguage, targetLanguage, extractSplitSentences(res), identifier, alternatives, formalityTone); + const got = yield getGot(); return yield got.post(API_URL, { json: data }).json(); @@ -223,6 +231,7 @@ exports.generateJobs = generateJobs; exports.generateSplitSentencesRequestData = generateSplitSentencesRequestData; exports.generateTimestamp = generateTimestamp; exports.generateTranslationRequestData = generateTranslationRequestData; +exports.importEsm = importEsm; exports.randRange = randRange; exports.requestTranslation = requestTranslation; exports.splitIntoSentences = splitIntoSentences; diff --git a/api/package.json b/api/package.json index 0129d33..19c1f82 100644 --- a/api/package.json +++ b/api/package.json @@ -2,6 +2,6 @@ "name": "deepl-api", "type": "commonjs", "dependencies": { - "got": "^11.8.5" + "got": "^12.1.0" } } diff --git a/package.json b/package.json index de4b2c4..7f59251 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ }, "dependencies": { "commander": "^9.3.0", - "got": "^11.8.5", + "got": "^12.1.0", "tslib": "^2.4.0" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 64d4731..95ae179 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,7 +9,7 @@ specifiers: '@vercel/node': ^1.15.3 commander: ^9.3.0 esno: ^0.16.3 - got: ^11.8.5 + got: ^12.1.0 ts-jest: ^28.0.3 tslib: ^2.4.0 type-coverage: ^2.21.1 @@ -17,7 +17,7 @@ specifiers: dependencies: commander: 9.3.0 - got: 11.8.5 + got: 12.1.0 tslib: 2.4.0 devDependencies: @@ -3005,9 +3005,9 @@ packages: '@sinonjs/commons': 1.8.3 dev: true - /@szmarczak/http-timer/4.0.6: - resolution: {integrity: sha512-4BAffykYOgO+5nzBWYwE3W90sBgLJoUPRWWcL8wlyiM8IB8ipJz3UMJ9KXQd1RKQXpKp8Tutn80HZtWsu2u76w==} - engines: {node: '>=10'} + /@szmarczak/http-timer/5.0.1: + resolution: {integrity: sha512-+PmQX0PiAYPMeVYe237LJAYvOMYW1j2rH5YROyS3b4CTVJum34HfRvKvAzozHAQG0TnHNdUfY9nCeUyRAs//cw==} + engines: {node: '>=14.16'} dependencies: defer-to-connect: 2.0.1 dev: false @@ -4096,8 +4096,8 @@ packages: engines: {node: '>= 0.8'} dev: true - /cacheable-lookup/5.0.4: - resolution: {integrity: sha512-2/kNscPhpcxrOigMZzbiWF7dz8ilhb/nIHU3EyZiXWXpeq/au8qJ8VhdftMkty3n7Gj6HIGalQG8oiBNB3AJgA==} + /cacheable-lookup/6.0.4: + resolution: {integrity: sha512-mbcDEZCkv2CZF4G01kr8eBd/5agkt9oCqz75tJMSIsquvRZ2sL6Hi5zGVKi/0OSC9oO1GHfJ2AV0ZIOY9vye0A==} engines: {node: '>=10.6.0'} dev: false @@ -6373,6 +6373,10 @@ packages: resolution: {integrity: sha512-WIWGi2L3DyTUvUrwRKgGi9TwxQMUEqPOPQBVi71R96jZXJdFskXEmf54BoZaS1kknGODoIGASGEzBUYdyMCBJg==} dev: true + /form-data-encoder/1.7.1: + resolution: {integrity: sha512-EFRDrsMm/kyqbTQocNvRXMLjc7Es2Vk+IQFx/YW7hkUH1eBl4J1fqiP34l74Yt0pFLCNpc06fkbVk00008mzjg==} + dev: false + /format/0.2.2: resolution: {integrity: sha512-wzsgA6WOq+09wrU1tsJ09udeR/YZRaeArL9e1wPbFg3GG2yDnC2ldKpxs4xunpFF9DgqCqOIra3bc1HWrJ37Ww==} engines: {node: '>=0.4.x'} @@ -6488,7 +6492,6 @@ packages: /get-stream/6.0.1: resolution: {integrity: sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==} engines: {node: '>=10'} - dev: true /get-symbol-description/1.0.0: resolution: {integrity: sha512-2EmdH1YvIQiZpltCNgkuiUnyukzxM/R6NDJX31Ke3BG1Nq5b0S2PhX59UKi9vZpPDQVdqn+1IcaAwnzTT5vCjw==} @@ -6616,20 +6619,22 @@ packages: resolution: {integrity: sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg==} dev: true - /got/11.8.5: - resolution: {integrity: sha512-o0Je4NvQObAuZPHLFoRSkdG2lTgtcynqymzg2Vupdx6PorhaT5MCbIyXG6d4D94kk8ZG57QeosgdiqfJWhEhlQ==} - engines: {node: '>=10.19.0'} + /got/12.1.0: + resolution: {integrity: sha512-hBv2ty9QN2RdbJJMK3hesmSkFTjVIHyIDDbssCKnSmq62edGgImJWD10Eb1k77TiV1bxloxqcFAVK8+9pkhOig==} + engines: {node: '>=14.16'} dependencies: '@sindresorhus/is': 4.6.0 - '@szmarczak/http-timer': 4.0.6 + '@szmarczak/http-timer': 5.0.1 '@types/cacheable-request': 6.0.2 '@types/responselike': 1.0.0 - cacheable-lookup: 5.0.4 + cacheable-lookup: 6.0.4 cacheable-request: 7.0.2 decompress-response: 6.0.0 - http2-wrapper: 1.0.3 - lowercase-keys: 2.0.0 - p-cancelable: 2.1.1 + form-data-encoder: 1.7.1 + get-stream: 6.0.1 + http2-wrapper: 2.1.11 + lowercase-keys: 3.0.0 + p-cancelable: 3.0.0 responselike: 2.0.0 dev: false @@ -6734,8 +6739,8 @@ packages: resolution: {integrity: sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==} dev: false - /http2-wrapper/1.0.3: - resolution: {integrity: sha512-V+23sDMr12Wnz7iTcDeJr3O6AIxlnvT/bmaAAAP/Xda35C90p9599p0F1eHR/N1KILWSoWVAiOMFjBBXaXSMxg==} + /http2-wrapper/2.1.11: + resolution: {integrity: sha512-aNAk5JzLturWEUiuhAN73Jcbq96R7rTitAoXV54FYMatvihnpD2+6PUgU4ce3D/m5VDbw+F5CsyKSF176ptitQ==} engines: {node: '>=10.19.0'} dependencies: quick-lru: 5.1.1 @@ -8031,6 +8036,11 @@ packages: engines: {node: '>=8'} dev: false + /lowercase-keys/3.0.0: + resolution: {integrity: sha512-ozCC6gdQ+glXOQsveKD0YsDy8DSQFjDTz4zyzEHNV5+JP5D62LmfDZ6o1cycFx9ouG940M5dE8C8CTewdj2YWQ==} + engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} + dev: false + /lru-cache/4.1.5: resolution: {integrity: sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==} dependencies: @@ -9105,9 +9115,9 @@ packages: resolution: {integrity: sha512-/jHxFIzoMXdqPzTaCpFzAAWhpkSjZPF4Vsn6jAfNpmbH/ymsmd7Qc6VE9BGn0L6YMj6uwpQLxCECpus4ukKS9Q==} dev: true - /p-cancelable/2.1.1: - resolution: {integrity: sha512-BZOr3nRQHOntUjTrH8+Lh54smKHoHyur8We1V8DSMVrl5A2malOOwuJRnKRDjSnkoeBh4at6BwEnb5I7Jl31wg==} - engines: {node: '>=8'} + /p-cancelable/3.0.0: + resolution: {integrity: sha512-mlVgR3PGuzlo0MmTdk4cXqXWlwQDLnONTAg6sm62XkMJEiRxN3GL3SffkYvqwonbkJBcrI7Uvv5Zh9yjvn2iUw==} + engines: {node: '>=12.20'} dev: false /p-defer/1.0.0: diff --git a/src/api.ts b/src/api.ts index d273681..10e9c5d 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,43 +1,56 @@ -import _got from 'got' +import type { Got } from 'got' import { extractTranslatedSentences, extractSplitSentences, SplittedSentences, - TranslatedSentences, + type TranslatedSentences, } from './extractors.js' import { generateTranslationRequestData, generateSplitSentencesRequestData, } from './generators.js' -import { API_URL, AUTO, SourceLanguage, TargetLanguage } from './settings.js' -import { abbreviateLanguage } from './utils.js' +import { + API_URL, + AUTO, + type SourceLanguage, + type TargetLanguage, +} from './settings.js' +import { abbreviateLanguage, importEsm } from './utils.js' + +let _got: Got | undefined -const got = _got.extend({ - headers: { - accept: '*/*', - 'accept-language': 'en-US;q=0.8,en;q=0.7', - authority: 'www2.deepl.com', - 'content-type': 'application/json', - origin: 'https://www.deepl.com', - referer: 'https://www.deepl.com/translator', - 'sec-fetch-dest': 'empty', - 'sec-fetch-mode': 'cors', - 'sec-fetch-site': 'same-site', - 'user-agent': - 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36', - }, - stringifyJson(object: unknown) { - return JSON.stringify(object).replace('"method":"', () => { - const self = object as { id: number } - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - if ((self.id + 3) % 13 === 0 || (self.id + 5) % 29 === 0) { - return '"method" : "' - } - return '"method": "' +async function getGot() { + if (!_got) { + const got = await importEsm('got') + _got = got.default.extend({ + headers: { + accept: '*/*', + 'accept-language': 'en-US;q=0.8,en;q=0.7', + authority: 'www2.deepl.com', + 'content-type': 'application/json', + origin: 'https://www.deepl.com', + referer: 'https://www.deepl.com/translator', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'same-site', + 'user-agent': + 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36', + }, + stringifyJson(object: unknown) { + return JSON.stringify(object).replace('"method":"', () => { + const self = object as { id: number } + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + if ((self.id + 3) % 13 === 0 || (self.id + 5) % 29 === 0) { + return '"method" : "' + } + return '"method": "' + }) + }, }) - }, -}) + } + return _got +} export async function splitSentences( text: string, @@ -49,6 +62,7 @@ export async function splitSentences( sourceLanguage, identifier, ) + const got = await getGot() return await got .post(API_URL, { json: data, @@ -83,6 +97,7 @@ export async function requestTranslation( alternatives, formalityTone, ) + const got = await getGot() return await got .post(API_URL, { json: data, diff --git a/src/cli.ts b/src/cli.ts index 15569f6..a9ccb39 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -7,7 +7,7 @@ import { URL } from 'node:url' import { program } from 'commander' import { translate } from './api.js' -import { SourceLanguage, TargetLanguage } from './settings.js' +import { type SourceLanguage, type TargetLanguage } from './settings.js' const __dirname = new URL('.', import.meta.url).pathname diff --git a/src/extractors.ts b/src/extractors.ts index 4e0efcb..d0df933 100644 --- a/src/extractors.ts +++ b/src/extractors.ts @@ -1,4 +1,4 @@ -import { SourceLanguage } from './settings.js' +import { type SourceLanguage } from './settings.js' export interface TranslatedSentences { result: { diff --git a/src/generators.ts b/src/generators.ts index a9af26a..22d164a 100644 --- a/src/generators.ts +++ b/src/generators.ts @@ -1,9 +1,9 @@ import { generateId, generateTimestamp } from './hacks.js' import { AUTO, - SourceLanguage, + type SourceLanguage, SUPPORTED_FORMALITY_TONES, - TargetLanguage, + type TargetLanguage, } from './settings.js' export function generateSplitSentencesRequestData( diff --git a/src/utils.ts b/src/utils.ts index ea0ba61..6a0942f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import { SUPPORTED_LANGUAGES, TargetLanguage } from './settings.js' +import { SUPPORTED_LANGUAGES, type TargetLanguage } from './settings.js' export function createAbbreviationsDictionary(languages = SUPPORTED_LANGUAGES) { return languages.reduce>((acc, lang) => { @@ -13,3 +13,10 @@ export function abbreviateLanguage(language: string) { | TargetLanguage | undefined } + +export function importEsm(modulePath: string) { + // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func + return new Function('modulePath', 'return import(modulePath)')( + modulePath, + ) as Promise +}