@textunited/sanity-plugin 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/LICENSE.md +21 -0
- package/README.md +182 -2
- package/dist/chunk-BC676THN.cjs +1059 -0
- package/dist/chunk-BC676THN.cjs.map +1 -0
- package/dist/chunk-BEHPJRZM.cjs +870 -0
- package/dist/chunk-BEHPJRZM.cjs.map +1 -0
- package/dist/chunk-L37ROFN6.js +1052 -0
- package/dist/chunk-L37ROFN6.js.map +1 -0
- package/dist/chunk-TA5JUFXP.js +833 -0
- package/dist/chunk-TA5JUFXP.js.map +1 -0
- package/dist/config-Dcl8jU4S.d.cts +43 -0
- package/dist/config-Dcl8jU4S.d.ts +43 -0
- package/dist/next/index.cjs +18 -0
- package/dist/next/index.cjs.map +1 -0
- package/dist/next/index.d.cts +21 -0
- package/dist/next/index.d.ts +21 -0
- package/dist/next/index.js +16 -0
- package/dist/next/index.js.map +1 -0
- package/dist/server/index.cjs +33 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +329 -0
- package/dist/server/index.d.ts +329 -0
- package/dist/server/index.js +4 -0
- package/dist/server/index.js.map +1 -0
- package/dist/studio/index.cjs +1005 -0
- package/dist/studio/index.cjs.map +1 -0
- package/dist/studio/index.d.cts +41 -0
- package/dist/studio/index.d.ts +41 -0
- package/dist/studio/index.js +1003 -0
- package/dist/studio/index.js.map +1 -0
- package/package.json +89 -4
|
@@ -0,0 +1,1059 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkBEHPJRZM_cjs = require('./chunk-BEHPJRZM.cjs');
|
|
4
|
+
|
|
5
|
+
// src/core/textunited/client.ts
|
|
6
|
+
var TextUnitedTimeoutError = class extends Error {
|
|
7
|
+
constructor(method, path, timeoutMs) {
|
|
8
|
+
super(`TextUnited did not answer ${method} ${path} within ${Math.round(timeoutMs / 1e3)} seconds`);
|
|
9
|
+
this.name = "TextUnitedTimeoutError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var DEFAULT_CAPABILITIES = { appendSegments: true };
|
|
13
|
+
var TextUnitedApiError = class extends Error {
|
|
14
|
+
status;
|
|
15
|
+
body;
|
|
16
|
+
constructor(status, body) {
|
|
17
|
+
super(describeStatus(status, body));
|
|
18
|
+
this.name = "TextUnitedApiError";
|
|
19
|
+
this.status = status;
|
|
20
|
+
this.body = body;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
function describeStatus(status, body) {
|
|
24
|
+
if (status === 401) {
|
|
25
|
+
return "TextUnited rejected the access token. It may have expired or been revoked: create a new one in the TextUnited portal and update the server configuration.";
|
|
26
|
+
}
|
|
27
|
+
if (status === 403) {
|
|
28
|
+
return "The TextUnited access token lacks the required scopes. It needs projects:read and projects:write.";
|
|
29
|
+
}
|
|
30
|
+
const detail = body.trim().slice(0, 500);
|
|
31
|
+
return `TextUnited request failed with status ${status}${detail ? `: ${detail}` : ""}`;
|
|
32
|
+
}
|
|
33
|
+
function encodeBasic(value) {
|
|
34
|
+
if (typeof btoa === "function") return btoa(value);
|
|
35
|
+
return Buffer.from(value).toString("base64");
|
|
36
|
+
}
|
|
37
|
+
var TextUnitedClient = class {
|
|
38
|
+
baseUrl;
|
|
39
|
+
authHeader;
|
|
40
|
+
fetchImpl;
|
|
41
|
+
timeoutMs;
|
|
42
|
+
constructor(options) {
|
|
43
|
+
this.baseUrl = (options.baseUrl ?? "https://api.textunited.com").replace(/\/+$/, "");
|
|
44
|
+
this.fetchImpl = options.fetch ?? fetch;
|
|
45
|
+
this.timeoutMs = options.timeoutMs ?? 9e4;
|
|
46
|
+
this.authHeader = options.auth.type === "pat" ? `Bearer ${options.auth.token}` : `Basic ${encodeBasic(`${options.auth.companyId}:${options.auth.apiKey}`)}`;
|
|
47
|
+
}
|
|
48
|
+
async request(method, path, body) {
|
|
49
|
+
const controller = new AbortController();
|
|
50
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
51
|
+
let response;
|
|
52
|
+
let text;
|
|
53
|
+
try {
|
|
54
|
+
response = await this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
55
|
+
method,
|
|
56
|
+
headers: {
|
|
57
|
+
Authorization: this.authHeader,
|
|
58
|
+
Accept: "application/json",
|
|
59
|
+
...body === void 0 ? {} : { "Content-Type": "application/json" }
|
|
60
|
+
},
|
|
61
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
62
|
+
signal: controller.signal
|
|
63
|
+
});
|
|
64
|
+
text = await response.text();
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (controller.signal.aborted) throw new TextUnitedTimeoutError(method, path, this.timeoutMs);
|
|
67
|
+
throw error;
|
|
68
|
+
} finally {
|
|
69
|
+
clearTimeout(timer);
|
|
70
|
+
}
|
|
71
|
+
if (response.status === 404 && method === "GET") return null;
|
|
72
|
+
if (!response.ok) throw new TextUnitedApiError(response.status, text);
|
|
73
|
+
return text.trim() ? JSON.parse(text) : null;
|
|
74
|
+
}
|
|
75
|
+
/** Returns the multilingual project with this custom id, or null when it does not exist. */
|
|
76
|
+
getProject(customId) {
|
|
77
|
+
return this.request(
|
|
78
|
+
"GET",
|
|
79
|
+
`/segments/projects/custom/${encodeURIComponent(customId)}`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
async createProject(request) {
|
|
83
|
+
const result = await this.request("POST", "/segments/projects", request);
|
|
84
|
+
return result ?? { customId: request.customId };
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Replaces the content of one task (virtual file) in every language project of the
|
|
88
|
+
* multilingual project. With `instantTranslation`, unchanged segments get their text back
|
|
89
|
+
* from TextUnited's translation memory, but with the status `automatic`.
|
|
90
|
+
*/
|
|
91
|
+
async replaceTask(args) {
|
|
92
|
+
const result = await this.request("PUT", "/segments/projects", {
|
|
93
|
+
customId: args.customId,
|
|
94
|
+
taskId: args.taskId,
|
|
95
|
+
segments: args.segments,
|
|
96
|
+
appendContent: false,
|
|
97
|
+
instantTranslation: true,
|
|
98
|
+
eventUrl: args.eventUrl
|
|
99
|
+
});
|
|
100
|
+
return result ?? { customId: args.customId };
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* People who can join an in-house team: active company users and confirmed collaborators.
|
|
104
|
+
* Needs the same token scopes as projects (projects:read).
|
|
105
|
+
*/
|
|
106
|
+
async listTeamCandidates() {
|
|
107
|
+
const [users, collaborators] = await Promise.all([
|
|
108
|
+
this.request("GET", "/accounts/Users"),
|
|
109
|
+
this.request("GET", "/accounts/Collaborators")
|
|
110
|
+
]);
|
|
111
|
+
const name = (first, last, fallback = "") => [first, last].filter(Boolean).join(" ").trim() || fallback;
|
|
112
|
+
const candidates = [];
|
|
113
|
+
for (const user of users ?? []) {
|
|
114
|
+
const username = user.login || user.email;
|
|
115
|
+
if (!user.id || !username || user.stateName && user.stateName !== "Normal") continue;
|
|
116
|
+
candidates.push({ id: user.id, username, name: name(user.firstName, user.lastName, username), kind: "user" });
|
|
117
|
+
}
|
|
118
|
+
for (const collaborator of collaborators ?? []) {
|
|
119
|
+
const id = collaborator.userId || collaborator.id;
|
|
120
|
+
if (!id || !collaborator.email || collaborator.confirmed === false) continue;
|
|
121
|
+
candidates.push({
|
|
122
|
+
id,
|
|
123
|
+
username: collaborator.email,
|
|
124
|
+
name: name(collaborator.firstName, collaborator.lastName, collaborator.email),
|
|
125
|
+
kind: "collaborator"
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return candidates.sort((a, b) => a.name.localeCompare(b.name));
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Adds the segments as a new task in every language project of the multilingual project;
|
|
132
|
+
* existing tasks stay as they are. The task id must not exist in the project yet (HTTP 400).
|
|
133
|
+
*/
|
|
134
|
+
async appendTask(args) {
|
|
135
|
+
const result = await this.request("PUT", "/segments/projects", {
|
|
136
|
+
customId: args.customId,
|
|
137
|
+
taskId: args.taskId,
|
|
138
|
+
segments: args.segments,
|
|
139
|
+
appendContent: true,
|
|
140
|
+
instantTranslation: true,
|
|
141
|
+
eventUrl: args.eventUrl
|
|
142
|
+
});
|
|
143
|
+
return result ?? { customId: args.customId, taskId: args.taskId };
|
|
144
|
+
}
|
|
145
|
+
/** Languages TextUnited supports (`GET /integrations/languages`, no token needed). */
|
|
146
|
+
async listLanguages() {
|
|
147
|
+
return await this.request("GET", "/integrations/languages") ?? [];
|
|
148
|
+
}
|
|
149
|
+
/** Source segments with their translations in every target language. */
|
|
150
|
+
async getSegments(customId, taskId) {
|
|
151
|
+
const query = taskId ? `?taskId=${encodeURIComponent(taskId)}` : "";
|
|
152
|
+
const result = await this.request(
|
|
153
|
+
"GET",
|
|
154
|
+
`/segments/projects/custom/${encodeURIComponent(customId)}/segments${query}`
|
|
155
|
+
);
|
|
156
|
+
return result ?? [];
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// src/core/document/apply.ts
|
|
161
|
+
var NOT_TRANSLATED = /* @__PURE__ */ new Set(["untranslated"]);
|
|
162
|
+
function translatedValue(unit, language, translations) {
|
|
163
|
+
const chunks = [];
|
|
164
|
+
for (const segment of unit.segments) {
|
|
165
|
+
const translation = translations.get(segment.customId)?.get(language);
|
|
166
|
+
if (!translation || !translation.content || NOT_TRANSLATED.has(translation.status ?? "")) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
chunks.push(translation);
|
|
170
|
+
}
|
|
171
|
+
const status = chunks.map((chunk) => chunk.status).find((s) => s !== "proofread") ?? chunks[0]?.status;
|
|
172
|
+
const contents = chunks.map((chunk) => chunk.content);
|
|
173
|
+
if (unit.kind === "text") {
|
|
174
|
+
return { value: chunkBEHPJRZM_cjs.decodeHtmlEntities(chunkBEHPJRZM_cjs.joinTextChunks(contents)), status };
|
|
175
|
+
}
|
|
176
|
+
return { value: chunkBEHPJRZM_cjs.applyBlockTranslation(unit.source, contents), status };
|
|
177
|
+
}
|
|
178
|
+
function writeValue(unit, language, value, targets, sourceDocument, sourceLanguage) {
|
|
179
|
+
if (targets.model === "documentLevel") {
|
|
180
|
+
const document = targets.documents[language];
|
|
181
|
+
if (!document) throw new Error(`No ${language} document to write the translation to`);
|
|
182
|
+
chunkBEHPJRZM_cjs.setAtPath(document, sourceDocument, [...unit.fieldPath, ...unit.innerPath], value);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const intlArray = chunkBEHPJRZM_cjs.getAtPath(targets.document, unit.fieldPath);
|
|
186
|
+
if (!Array.isArray(intlArray)) throw new Error(`No internationalized field at ${unit.id}`);
|
|
187
|
+
const sourceItem = chunkBEHPJRZM_cjs.getIntlItem(chunkBEHPJRZM_cjs.getAtPath(sourceDocument, unit.fieldPath), sourceLanguage);
|
|
188
|
+
let item = chunkBEHPJRZM_cjs.getIntlItem(intlArray, language);
|
|
189
|
+
if (!item) {
|
|
190
|
+
item = {
|
|
191
|
+
_key: language,
|
|
192
|
+
...chunkBEHPJRZM_cjs.isPlainObject(sourceItem) && typeof sourceItem._type === "string" ? { _type: sourceItem._type } : {},
|
|
193
|
+
// Rich text starts as a copy of the source, so images and blocks without text are kept;
|
|
194
|
+
// translated blocks replace their originals.
|
|
195
|
+
value: unit.kind === "text" ? "" : Array.isArray(sourceItem?.value) ? chunkBEHPJRZM_cjs.cloneForTranslation(sourceItem.value) : []
|
|
196
|
+
};
|
|
197
|
+
intlArray.push(item);
|
|
198
|
+
}
|
|
199
|
+
if (unit.innerPath.length === 0) {
|
|
200
|
+
item.value = value;
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (!Array.isArray(item.value)) item.value = [];
|
|
204
|
+
chunkBEHPJRZM_cjs.setAtPath(item, { value: sourceItem?.value ?? [] }, ["value", ...unit.innerPath], value);
|
|
205
|
+
}
|
|
206
|
+
function applyTranslations(args) {
|
|
207
|
+
const targets = chunkBEHPJRZM_cjs.cloneValue(args.targets);
|
|
208
|
+
const state = chunkBEHPJRZM_cjs.cloneValue(args.state);
|
|
209
|
+
const changedFields = /* @__PURE__ */ new Map();
|
|
210
|
+
const outcomes = [];
|
|
211
|
+
for (const unit of args.units) {
|
|
212
|
+
for (const language of args.languages) {
|
|
213
|
+
const translated = translatedValue(unit, language, args.translations);
|
|
214
|
+
if (!translated) {
|
|
215
|
+
outcomes.push({ unitId: unit.id, language, outcome: "incomplete" });
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const current = chunkBEHPJRZM_cjs.readTargetValue(unit, language, targets);
|
|
219
|
+
const record = state.units[unit.id];
|
|
220
|
+
const appliedHash = record?.languages[language]?.appliedHash;
|
|
221
|
+
const editedByHand = !chunkBEHPJRZM_cjs.isMissingValue(unit, current) && appliedHash !== chunkBEHPJRZM_cjs.valueHash(current);
|
|
222
|
+
if (editedByHand && !args.overwrite?.has(unit.id)) {
|
|
223
|
+
outcomes.push({ unitId: unit.id, language, outcome: "keptManualEdit" });
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
const unitState = state.units[unit.id] ??= { sourceHash: unit.sourceHash, languages: {} };
|
|
227
|
+
unitState.languages[language] = {
|
|
228
|
+
appliedHash: chunkBEHPJRZM_cjs.valueHash(translated.value),
|
|
229
|
+
status: translated.status
|
|
230
|
+
};
|
|
231
|
+
if (chunkBEHPJRZM_cjs.valueHash(current) === chunkBEHPJRZM_cjs.valueHash(translated.value)) {
|
|
232
|
+
outcomes.push({ unitId: unit.id, language, outcome: "unchanged" });
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
writeValue(unit, language, translated.value, targets, args.sourceDocument, args.sourceLanguage);
|
|
236
|
+
const documentKey = targets.model === "fieldLevel" ? "" : language;
|
|
237
|
+
const fields = changedFields.get(documentKey) ?? /* @__PURE__ */ new Set();
|
|
238
|
+
fields.add(String(unit.fieldPath[0]));
|
|
239
|
+
changedFields.set(documentKey, fields);
|
|
240
|
+
outcomes.push({ unitId: unit.id, language, outcome: "applied" });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return { targets, state, changedFields, outcomes };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/core/languages.ts
|
|
247
|
+
var ALIASES = {
|
|
248
|
+
"en-gb": "en-en",
|
|
249
|
+
"en-uk": "en-en",
|
|
250
|
+
"zh-cn": "cn-cn",
|
|
251
|
+
"zh-tw": "ct-ct"
|
|
252
|
+
};
|
|
253
|
+
function canonicalLanguageCode(code) {
|
|
254
|
+
const lower = String(code ?? "").trim().toLowerCase();
|
|
255
|
+
return ALIASES[lower] ?? lower;
|
|
256
|
+
}
|
|
257
|
+
function findUnknownLanguageCodes(codes, known) {
|
|
258
|
+
const knownCodes = new Set(known.map((language) => language.langCode.toLowerCase()));
|
|
259
|
+
const unknown = [];
|
|
260
|
+
for (const { label, code } of codes) {
|
|
261
|
+
if (knownCodes.has(canonicalLanguageCode(code))) continue;
|
|
262
|
+
const general = canonicalLanguageCode(code).split("-")[0];
|
|
263
|
+
const suggestions = known.filter((language) => language.langCode.toLowerCase().split("-")[0] === general).slice(0, 5).map((language) => `${language.langCode.toLowerCase()} (${language.systemName})`);
|
|
264
|
+
unknown.push({ label, code, suggestions });
|
|
265
|
+
}
|
|
266
|
+
return unknown;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// src/server/translations.ts
|
|
270
|
+
function toTranslationsBySegment(entries, config, taskOf) {
|
|
271
|
+
const languageByCode = new Map(
|
|
272
|
+
config.targetLanguages.map((language) => [canonicalLanguageCode(language.textUnitedCode), language.id])
|
|
273
|
+
);
|
|
274
|
+
const result = /* @__PURE__ */ new Map();
|
|
275
|
+
for (const entry of entries) {
|
|
276
|
+
const customId = entry.source?.customId;
|
|
277
|
+
if (!customId) continue;
|
|
278
|
+
const wantedTask = taskOf?.get(customId);
|
|
279
|
+
if (wantedTask !== void 0 && (entry.source?.taskId ?? "") !== wantedTask) continue;
|
|
280
|
+
const perLanguage = result.get(customId) ?? /* @__PURE__ */ new Map();
|
|
281
|
+
for (const translation of entry.translations ?? []) {
|
|
282
|
+
const language = languageByCode.get(canonicalLanguageCode(translation.languageCode));
|
|
283
|
+
if (!language || typeof translation.content !== "string") continue;
|
|
284
|
+
perLanguage.set(language, { content: translation.content, status: translation.status });
|
|
285
|
+
}
|
|
286
|
+
result.set(customId, perLanguage);
|
|
287
|
+
}
|
|
288
|
+
return result;
|
|
289
|
+
}
|
|
290
|
+
var FINAL_STATUSES = /* @__PURE__ */ new Set(["translated", "proofread"]);
|
|
291
|
+
function isFinalStatus(status) {
|
|
292
|
+
return FINAL_STATUSES.has(String(status ?? "").toLowerCase());
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/server/jobs.ts
|
|
296
|
+
var DEFAULT_PROJECT_SETTINGS = {
|
|
297
|
+
managedProject: true,
|
|
298
|
+
serviceTranslation: true,
|
|
299
|
+
serviceProofreading: false,
|
|
300
|
+
serviceAutomaticTranslation: true,
|
|
301
|
+
serviceAdaptiveTranslation: false,
|
|
302
|
+
serviceScoring: false
|
|
303
|
+
};
|
|
304
|
+
var DEFAULT_IN_HOUSE_SETTINGS = {
|
|
305
|
+
managedProject: false,
|
|
306
|
+
serviceTranslation: true,
|
|
307
|
+
serviceProofreading: false,
|
|
308
|
+
serviceAutomaticTranslation: true,
|
|
309
|
+
serviceAdaptiveTranslation: false,
|
|
310
|
+
serviceScoring: false
|
|
311
|
+
};
|
|
312
|
+
function teamFromRequest(value) {
|
|
313
|
+
if (!Array.isArray(value)) return [];
|
|
314
|
+
return value.filter((member) => typeof member?.username === "string" && member.username !== "").map((member) => ({
|
|
315
|
+
...typeof member.id === "number" ? { id: member.id } : {},
|
|
316
|
+
username: member.username,
|
|
317
|
+
isTranslator: member.translator === true,
|
|
318
|
+
isProofreader: member.proofreader === true,
|
|
319
|
+
isProjectManager: member.projectManager === true,
|
|
320
|
+
isInCountryReviewer: false
|
|
321
|
+
})).filter((member) => member.isTranslator || member.isProofreader || member.isProjectManager);
|
|
322
|
+
}
|
|
323
|
+
function projectSettingsFor(request, context) {
|
|
324
|
+
if (request.projectType !== "inHouse") return { projectType: "outsourced", settings: context.projectSettings };
|
|
325
|
+
const team = teamFromRequest(request.team);
|
|
326
|
+
if (!team.some((member) => member.isTranslator)) {
|
|
327
|
+
throw new Error("An in-house project needs at least one translator in the team.");
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
projectType: "inHouse",
|
|
331
|
+
settings: {
|
|
332
|
+
...context.inHouseSettings,
|
|
333
|
+
serviceProofreading: team.some((member) => member.isProofreader),
|
|
334
|
+
team
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
var VALID_MODES = /* @__PURE__ */ new Set(["missing", "changed", "all"]);
|
|
339
|
+
function documentTitle(source, sourceLanguage) {
|
|
340
|
+
const title = source.title;
|
|
341
|
+
if (typeof title === "string") return title;
|
|
342
|
+
const localized = chunkBEHPJRZM_cjs.getIntlItem(title, sourceLanguage)?.value;
|
|
343
|
+
return typeof localized === "string" ? localized : "";
|
|
344
|
+
}
|
|
345
|
+
function projectName(source, baseId, sourceLanguage, languages) {
|
|
346
|
+
const title = documentTitle(source, sourceLanguage).replace(/\s+/g, " ").trim();
|
|
347
|
+
const suffix = languages?.length ? ` \u2013 ${languages.map((language) => language.toUpperCase()).join(", ")}` : "";
|
|
348
|
+
return title ? `${title.slice(0, 160)}${suffix} (${baseId})` : `${baseId}${suffix}`;
|
|
349
|
+
}
|
|
350
|
+
function collectInstantTranslations(responses, config) {
|
|
351
|
+
return toTranslationsBySegment(
|
|
352
|
+
responses.flatMap((response) => response.instantTranslationSegments ?? []),
|
|
353
|
+
config
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
function countOutcomes(outcomes) {
|
|
357
|
+
const counts = {};
|
|
358
|
+
for (const { outcome } of outcomes) counts[outcome] = (counts[outcome] ?? 0) + 1;
|
|
359
|
+
return counts;
|
|
360
|
+
}
|
|
361
|
+
async function processTranslationRequest(requestId, context) {
|
|
362
|
+
const now = context.now ?? (() => /* @__PURE__ */ new Date());
|
|
363
|
+
const newId = context.newId ?? (() => crypto.randomUUID());
|
|
364
|
+
const request = await context.store.getDocument(requestId);
|
|
365
|
+
if (!request) return { status: "ignored", reason: "Request not found" };
|
|
366
|
+
if (request.status !== "queued") return { status: "ignored", reason: `Request is ${String(request.status)}` };
|
|
367
|
+
try {
|
|
368
|
+
await context.store.mutate([
|
|
369
|
+
{
|
|
370
|
+
patch: {
|
|
371
|
+
id: requestId,
|
|
372
|
+
ifRevisionID: String(request._rev),
|
|
373
|
+
set: { status: "running", startedAt: now().toISOString() }
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
]);
|
|
377
|
+
} catch (error) {
|
|
378
|
+
if (error instanceof chunkBEHPJRZM_cjs.SanityConflictError) return { status: "ignored", reason: "Already being processed" };
|
|
379
|
+
throw error;
|
|
380
|
+
}
|
|
381
|
+
try {
|
|
382
|
+
const summary = await runRequest(request, context, newId);
|
|
383
|
+
await context.store.mutate([
|
|
384
|
+
{
|
|
385
|
+
patch: {
|
|
386
|
+
id: requestId,
|
|
387
|
+
set: { status: "done", finishedAt: now().toISOString(), summaryJson: JSON.stringify(summary), error: null }
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
]);
|
|
391
|
+
return { status: "done", summary };
|
|
392
|
+
} catch (error) {
|
|
393
|
+
const message = error instanceof Error ? error.message : "Translation request failed";
|
|
394
|
+
await context.store.mutate([
|
|
395
|
+
{ patch: { id: requestId, set: { status: "failed", finishedAt: now().toISOString(), error: message } } }
|
|
396
|
+
]);
|
|
397
|
+
return { status: "failed", error: message };
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
async function runRequest(request, context, newId) {
|
|
401
|
+
const { config } = context;
|
|
402
|
+
const now = context.now ?? (() => /* @__PURE__ */ new Date());
|
|
403
|
+
const baseId = chunkBEHPJRZM_cjs.getBaseDocumentId(String(request.documentId ?? ""));
|
|
404
|
+
const mode = String(request.mode ?? "missing");
|
|
405
|
+
const reset = request.reset === true;
|
|
406
|
+
if (!baseId) throw new Error("The request has no document id");
|
|
407
|
+
if (!VALID_MODES.has(mode)) throw new Error(`Unknown translation mode: ${mode}`);
|
|
408
|
+
const configured = new Set(config.targetLanguages.map((language) => language.id));
|
|
409
|
+
const requestedLanguages = (Array.isArray(request.languages) ? request.languages : []).map(String).filter((language) => configured.has(language));
|
|
410
|
+
if (requestedLanguages.length === 0) throw new Error("No configured target language was requested");
|
|
411
|
+
if (request.action === "stop" || request.action === "resume") {
|
|
412
|
+
return changeStoppedLanguages(baseId, requestedLanguages, request.action, context);
|
|
413
|
+
}
|
|
414
|
+
const stored = await chunkBEHPJRZM_cjs.loadState(context.store, baseId);
|
|
415
|
+
const languages = requestedLanguages.filter((language) => !stored.stopped.includes(language));
|
|
416
|
+
if (languages.length === 0) {
|
|
417
|
+
throw new Error("Translation is stopped for the requested languages. Resume them in the TextUnited panel first.");
|
|
418
|
+
}
|
|
419
|
+
const source = await chunkBEHPJRZM_cjs.getPreferredDocument(context.store, baseId);
|
|
420
|
+
if (!source) throw new Error(`Document ${baseId} not found`);
|
|
421
|
+
const typeConfig = chunkBEHPJRZM_cjs.getDocumentTypeConfig(config, String(source._type));
|
|
422
|
+
if (!typeConfig) throw new Error(`Documents of type ${String(source._type)} are not configured for translation`);
|
|
423
|
+
const units = chunkBEHPJRZM_cjs.extractTranslationUnits(source, typeConfig, config);
|
|
424
|
+
const loaded = await chunkBEHPJRZM_cjs.loadTargets({ store: context.store, source, typeConfig, languages });
|
|
425
|
+
const baseState = chunkBEHPJRZM_cjs.recordBaseline(stored.state, units);
|
|
426
|
+
const currentProjects = reset ? [] : stored.projects;
|
|
427
|
+
const archived = reset ? [...stored.archived, ...stored.projects] : stored.archived;
|
|
428
|
+
const groups = planGroups({
|
|
429
|
+
units,
|
|
430
|
+
languages,
|
|
431
|
+
targets: loaded.targets,
|
|
432
|
+
state: baseState,
|
|
433
|
+
mode,
|
|
434
|
+
reset,
|
|
435
|
+
append: context.capabilities?.appendSegments === true,
|
|
436
|
+
projects: currentProjects,
|
|
437
|
+
newCustomId: () => projectCustomIdFor(context, baseId, currentProjects.length + archived.length)
|
|
438
|
+
});
|
|
439
|
+
const selectedIds = new Set(groups.flatMap((group) => group.apply.selected.map(({ unit }) => unit.id)));
|
|
440
|
+
const summary = {
|
|
441
|
+
tasksSent: [],
|
|
442
|
+
unitsSelected: selectedIds.size,
|
|
443
|
+
outcomes: {},
|
|
444
|
+
projectCreated: false
|
|
445
|
+
};
|
|
446
|
+
summary.textsSent = new Set(groups.flatMap((group) => group.sendUnits.map((unit) => unit.id))).size;
|
|
447
|
+
if (groups.every((group) => group.sendUnits.length === 0)) {
|
|
448
|
+
try {
|
|
449
|
+
const pending = chunkBEHPJRZM_cjs.hasPendingTranslations(baseState, chunkBEHPJRZM_cjs.activeProjects(stored.projects, stored.stopped), isFinalStatus);
|
|
450
|
+
await context.store.mutate([chunkBEHPJRZM_cjs.saveStateMutation(baseId, { ...stored, state: baseState }, pending)]);
|
|
451
|
+
} catch (error) {
|
|
452
|
+
if (!(error instanceof chunkBEHPJRZM_cjs.SanityConflictError)) throw error;
|
|
453
|
+
}
|
|
454
|
+
return summary;
|
|
455
|
+
}
|
|
456
|
+
await assertKnownLanguageCodes(context, languages);
|
|
457
|
+
const eventUrl = await context.eventUrl(baseId);
|
|
458
|
+
const sentAt = now().toISOString();
|
|
459
|
+
const isFirst = currentProjects.length === 0 && archived.length === 0;
|
|
460
|
+
const results = await Promise.all(
|
|
461
|
+
groups.filter((group) => group.sendUnits.length > 0).map((group) => sendToProject({ context, request, group, source, baseId, eventUrl, newId, now, isFirst }))
|
|
462
|
+
);
|
|
463
|
+
let sentState = baseState;
|
|
464
|
+
const projects = [...currentProjects];
|
|
465
|
+
const responses = [];
|
|
466
|
+
for (const result of results) {
|
|
467
|
+
responses.push(...result.responses);
|
|
468
|
+
summary.tasksSent.push(...result.tasksSent);
|
|
469
|
+
if (result.awaiting.length > 0) {
|
|
470
|
+
summary.tasksAwaitingTranslation = [...summary.tasksAwaitingTranslation ?? [], ...result.awaiting];
|
|
471
|
+
}
|
|
472
|
+
if (result.created) summary.projectCreated = true;
|
|
473
|
+
const index = projects.findIndex((project) => project.customId === result.project.customId);
|
|
474
|
+
if (index >= 0) projects[index] = result.project;
|
|
475
|
+
else projects.push(result.project);
|
|
476
|
+
for (const sent of result.sent) {
|
|
477
|
+
sentState = chunkBEHPJRZM_cjs.recordSentUnits(sentState, sent.units, sentAt, sent.taskId, result.project.customId);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
const selected = /* @__PURE__ */ new Map();
|
|
481
|
+
const overwrite = /* @__PURE__ */ new Set();
|
|
482
|
+
for (const group of groups) {
|
|
483
|
+
for (const { unit, reason } of group.apply.selected) {
|
|
484
|
+
selected.set(unit.id, unit);
|
|
485
|
+
if (mode === "all" || reason === "sourceChanged") overwrite.add(unit.id);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return finishRequest({
|
|
489
|
+
context,
|
|
490
|
+
source,
|
|
491
|
+
typeConfig,
|
|
492
|
+
loaded,
|
|
493
|
+
languages,
|
|
494
|
+
units,
|
|
495
|
+
stored,
|
|
496
|
+
baseId,
|
|
497
|
+
newId,
|
|
498
|
+
summary,
|
|
499
|
+
selectedUnits: [...selected.values()],
|
|
500
|
+
overwrite,
|
|
501
|
+
responses,
|
|
502
|
+
sentState,
|
|
503
|
+
projects,
|
|
504
|
+
archived
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
function planGroups(args) {
|
|
508
|
+
const groups = [];
|
|
509
|
+
const plan = (languages, projectCustomId, mode) => chunkBEHPJRZM_cjs.planTranslation({ units: args.units, languages, targets: args.targets, state: args.state, mode, projectCustomId });
|
|
510
|
+
const unitsToSend = (sendPlan) => {
|
|
511
|
+
if (args.append) return sendPlan.selected.map(({ unit }) => unit);
|
|
512
|
+
const taskIds = new Set(sendPlan.tasks.map((task) => task.taskId));
|
|
513
|
+
return args.units.filter((unit) => taskIds.has(unit.taskId));
|
|
514
|
+
};
|
|
515
|
+
for (const project of args.projects) {
|
|
516
|
+
const languages = args.languages.filter((language) => project.languages.includes(language));
|
|
517
|
+
if (languages.length === 0) continue;
|
|
518
|
+
const apply = plan(languages, project.customId, args.mode);
|
|
519
|
+
groups.push({ project, languages, apply, sendPlan: apply, sendUnits: unitsToSend(apply) });
|
|
520
|
+
}
|
|
521
|
+
const covered = new Set(args.projects.flatMap((project) => project.languages));
|
|
522
|
+
const newLanguages = args.languages.filter((language) => !covered.has(language));
|
|
523
|
+
if (newLanguages.length > 0) {
|
|
524
|
+
const project = { customId: args.newCustomId(), languages: newLanguages };
|
|
525
|
+
const apply = plan(newLanguages, void 0, args.mode);
|
|
526
|
+
const sendPlan = args.reset ? plan(newLanguages, void 0, "all") : apply;
|
|
527
|
+
groups.push({ project, languages: newLanguages, apply, sendPlan, sendUnits: unitsToSend(sendPlan) });
|
|
528
|
+
}
|
|
529
|
+
return groups;
|
|
530
|
+
}
|
|
531
|
+
function projectCustomIdFor(context, baseId, existingCount) {
|
|
532
|
+
const customId = chunkBEHPJRZM_cjs.createProjectCustomId({
|
|
533
|
+
sanityProjectId: context.sanityProjectId,
|
|
534
|
+
dataset: context.dataset,
|
|
535
|
+
documentId: baseId
|
|
536
|
+
});
|
|
537
|
+
return existingCount === 0 ? customId : `${customId}-${existingCount + 1}`;
|
|
538
|
+
}
|
|
539
|
+
async function sendToProject(args) {
|
|
540
|
+
const { context, request, group, source, baseId, eventUrl, newId, now } = args;
|
|
541
|
+
const { config } = context;
|
|
542
|
+
const codeOf = new Map(config.targetLanguages.map((language) => [language.id, language.textUnitedCode]));
|
|
543
|
+
const customId = group.project.customId;
|
|
544
|
+
const result = {
|
|
545
|
+
project: { ...group.project },
|
|
546
|
+
created: false,
|
|
547
|
+
responses: [],
|
|
548
|
+
tasksSent: [],
|
|
549
|
+
awaiting: [],
|
|
550
|
+
sent: []
|
|
551
|
+
};
|
|
552
|
+
const existing = await context.textUnited.getProject(customId);
|
|
553
|
+
if (typeof existing?.id === "number") result.project.projectId = existing.id;
|
|
554
|
+
if (existing) assertProjectLanguages(existing, group.languages, codeOf);
|
|
555
|
+
const create = async (taskId, segments) => {
|
|
556
|
+
const chosen = projectSettingsFor(request, context);
|
|
557
|
+
result.project.projectType = chosen.projectType;
|
|
558
|
+
result.project.createdAt = now().toISOString();
|
|
559
|
+
const created = await context.textUnited.createProject({
|
|
560
|
+
customId,
|
|
561
|
+
taskId,
|
|
562
|
+
name: projectName(source, baseId, config.sourceLanguage.id, args.isFirst ? void 0 : group.project.languages),
|
|
563
|
+
sourceLanguageCode: config.sourceLanguage.textUnitedCode,
|
|
564
|
+
targetLanguages: group.project.languages.map((language) => ({
|
|
565
|
+
...chosen.settings,
|
|
566
|
+
targetLanguageCode: codeOf.get(language)
|
|
567
|
+
})),
|
|
568
|
+
segments,
|
|
569
|
+
instantTranslation: true,
|
|
570
|
+
eventUrl,
|
|
571
|
+
referenceNumber: baseId
|
|
572
|
+
}).catch(async (error) => {
|
|
573
|
+
if (error instanceof TextUnitedTimeoutError && await context.textUnited.getProject(customId)) {
|
|
574
|
+
result.awaiting.push(taskId);
|
|
575
|
+
return null;
|
|
576
|
+
}
|
|
577
|
+
throw error;
|
|
578
|
+
});
|
|
579
|
+
if (created) {
|
|
580
|
+
result.responses.push(created);
|
|
581
|
+
if (typeof created.id === "number") result.project.projectId = created.id;
|
|
582
|
+
}
|
|
583
|
+
result.created = true;
|
|
584
|
+
};
|
|
585
|
+
if (context.capabilities?.appendSegments) {
|
|
586
|
+
const segments = group.sendUnits.flatMap((unit) => unit.segments.map((segment) => ({ ...segment })));
|
|
587
|
+
let taskId = existing ? appendTaskId(now(), newId) : "content";
|
|
588
|
+
if (!existing) {
|
|
589
|
+
await create(taskId, segments);
|
|
590
|
+
} else {
|
|
591
|
+
try {
|
|
592
|
+
const response = await context.textUnited.appendTask({ customId, taskId, segments, eventUrl });
|
|
593
|
+
if (response.taskId) taskId = response.taskId;
|
|
594
|
+
result.responses.push(response);
|
|
595
|
+
} catch (error) {
|
|
596
|
+
if (!(error instanceof TextUnitedTimeoutError)) throw error;
|
|
597
|
+
result.awaiting.push(taskId);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
result.tasksSent.push(taskId);
|
|
601
|
+
result.sent.push({ units: group.sendUnits, taskId });
|
|
602
|
+
return result;
|
|
603
|
+
}
|
|
604
|
+
let tasks = group.sendPlan.tasks;
|
|
605
|
+
if (!existing) {
|
|
606
|
+
const formatted = tasks.findIndex((task) => task.segments.some((s) => s.content.includes("data-tu-id")));
|
|
607
|
+
if (formatted > 0) tasks = [tasks[formatted], ...tasks.filter((_, index) => index !== formatted)];
|
|
608
|
+
const [first, ...rest] = tasks;
|
|
609
|
+
if (first) {
|
|
610
|
+
await create(first.taskId, first.segments);
|
|
611
|
+
result.tasksSent.push(first.taskId);
|
|
612
|
+
}
|
|
613
|
+
tasks = rest;
|
|
614
|
+
}
|
|
615
|
+
for (const task of tasks) {
|
|
616
|
+
try {
|
|
617
|
+
result.responses.push(
|
|
618
|
+
await context.textUnited.replaceTask({ customId, taskId: task.taskId, segments: task.segments, eventUrl })
|
|
619
|
+
);
|
|
620
|
+
} catch (error) {
|
|
621
|
+
if (!(error instanceof TextUnitedTimeoutError)) throw error;
|
|
622
|
+
result.awaiting.push(task.taskId);
|
|
623
|
+
}
|
|
624
|
+
result.tasksSent.push(task.taskId);
|
|
625
|
+
}
|
|
626
|
+
result.sent.push({ units: group.sendUnits });
|
|
627
|
+
return result;
|
|
628
|
+
}
|
|
629
|
+
async function changeStoppedLanguages(baseId, languages, action, context) {
|
|
630
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
631
|
+
const stored = await chunkBEHPJRZM_cjs.loadState(context.store, baseId);
|
|
632
|
+
const stopped = action === "stop" ? [.../* @__PURE__ */ new Set([...stored.stopped, ...languages])] : stored.stopped.filter((language) => !languages.includes(language));
|
|
633
|
+
try {
|
|
634
|
+
await context.store.mutate([
|
|
635
|
+
chunkBEHPJRZM_cjs.saveStateMutation(
|
|
636
|
+
baseId,
|
|
637
|
+
{ ...stored, stopped },
|
|
638
|
+
chunkBEHPJRZM_cjs.hasPendingTranslations(stored.state, chunkBEHPJRZM_cjs.activeProjects(stored.projects, stopped), isFinalStatus)
|
|
639
|
+
)
|
|
640
|
+
]);
|
|
641
|
+
return { tasksSent: [], unitsSelected: 0, textsSent: 0, outcomes: {}, projectCreated: false, action, languages };
|
|
642
|
+
} catch (error) {
|
|
643
|
+
if (!(error instanceof chunkBEHPJRZM_cjs.SanityConflictError) || attempt >= 3) throw error;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
function appendTaskId(date, newId) {
|
|
648
|
+
const stamp = date.toISOString().replace(/[-:]/g, "").replace("T", "-").slice(0, 15);
|
|
649
|
+
const suffix = newId().replace(/[^a-z0-9]/gi, "").slice(0, 6).toLowerCase();
|
|
650
|
+
return `update-${stamp}-${suffix}`;
|
|
651
|
+
}
|
|
652
|
+
async function assertKnownLanguageCodes(context, languages) {
|
|
653
|
+
const known = await context.knownLanguages?.();
|
|
654
|
+
if (!known?.length) return;
|
|
655
|
+
const { config } = context;
|
|
656
|
+
const codes = [
|
|
657
|
+
{ label: config.sourceLanguage.title, code: config.sourceLanguage.textUnitedCode },
|
|
658
|
+
...config.targetLanguages.filter((language) => languages.includes(language.id)).map((language) => ({ label: language.title, code: language.textUnitedCode }))
|
|
659
|
+
];
|
|
660
|
+
const unknown = findUnknownLanguageCodes(codes, known);
|
|
661
|
+
if (unknown.length === 0) return;
|
|
662
|
+
const details = unknown.map(
|
|
663
|
+
({ label, code, suggestions }) => `"${code}" (${label})` + (suggestions.length ? ` \u2013 known codes: ${suggestions.join(", ")}` : "")
|
|
664
|
+
).join("; ");
|
|
665
|
+
throw new Error(
|
|
666
|
+
`TextUnited does not know these language codes: ${details}. Fix textUnitedCode in the translation configuration.`
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
function assertProjectLanguages(existing, languages, codeOf) {
|
|
670
|
+
const inProject = new Set(
|
|
671
|
+
(existing.targetLanguages ?? []).map((target) => canonicalLanguageCode(target.targetLanguageCode))
|
|
672
|
+
);
|
|
673
|
+
const missing = languages.filter((language) => !inProject.has(canonicalLanguageCode(codeOf.get(language))));
|
|
674
|
+
if (missing.length > 0) {
|
|
675
|
+
throw new Error(
|
|
676
|
+
`The TextUnited project for this document has no ${missing.join(", ")} translation. Adding a language to an existing project is not supported by the TextUnited API yet.`
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
async function finishRequest(args) {
|
|
681
|
+
const { context, source, typeConfig, loaded, languages, units, stored, baseId, newId, summary } = args;
|
|
682
|
+
const { config } = context;
|
|
683
|
+
const createdLanguages = chunkBEHPJRZM_cjs.createMissingTranslationDocuments({
|
|
684
|
+
targets: loaded.targets,
|
|
685
|
+
source,
|
|
686
|
+
typeConfig,
|
|
687
|
+
languages,
|
|
688
|
+
newId
|
|
689
|
+
});
|
|
690
|
+
const applied = applyTranslations({
|
|
691
|
+
units: args.selectedUnits,
|
|
692
|
+
languages,
|
|
693
|
+
sourceLanguage: config.sourceLanguage.id,
|
|
694
|
+
sourceDocument: source,
|
|
695
|
+
targets: loaded.targets,
|
|
696
|
+
translations: collectInstantTranslations(args.responses, config),
|
|
697
|
+
state: args.sentState,
|
|
698
|
+
overwrite: args.overwrite
|
|
699
|
+
});
|
|
700
|
+
summary.outcomes = countOutcomes(applied.outcomes);
|
|
701
|
+
const targetMutations = chunkBEHPJRZM_cjs.writeTargetsMutations({
|
|
702
|
+
loaded,
|
|
703
|
+
targets: applied.targets,
|
|
704
|
+
source,
|
|
705
|
+
typeConfig,
|
|
706
|
+
sourceLanguage: config.sourceLanguage.id,
|
|
707
|
+
changedFields: applied.changedFields,
|
|
708
|
+
createdLanguages,
|
|
709
|
+
newId
|
|
710
|
+
});
|
|
711
|
+
const touchedUnits = units.map((unit) => unit.id);
|
|
712
|
+
let current = stored;
|
|
713
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
714
|
+
const state = chunkBEHPJRZM_cjs.mergeState(current.state, applied.state, touchedUnits);
|
|
715
|
+
try {
|
|
716
|
+
await context.store.mutate([
|
|
717
|
+
...targetMutations,
|
|
718
|
+
chunkBEHPJRZM_cjs.saveStateMutation(
|
|
719
|
+
baseId,
|
|
720
|
+
{ state, projects: args.projects, archived: args.archived, stopped: current.stopped, revision: current.revision },
|
|
721
|
+
chunkBEHPJRZM_cjs.hasPendingTranslations(state, chunkBEHPJRZM_cjs.activeProjects(args.projects, current.stopped), isFinalStatus)
|
|
722
|
+
)
|
|
723
|
+
]);
|
|
724
|
+
return summary;
|
|
725
|
+
} catch (error) {
|
|
726
|
+
if (!(error instanceof chunkBEHPJRZM_cjs.SanityConflictError) || attempt >= 3) throw error;
|
|
727
|
+
current = await chunkBEHPJRZM_cjs.loadState(context.store, baseId);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// src/server/signing.ts
|
|
733
|
+
var encoder = new TextEncoder();
|
|
734
|
+
async function hmac(secret, data) {
|
|
735
|
+
const key = await crypto.subtle.importKey(
|
|
736
|
+
"raw",
|
|
737
|
+
encoder.encode(secret),
|
|
738
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
739
|
+
false,
|
|
740
|
+
["sign"]
|
|
741
|
+
);
|
|
742
|
+
return new Uint8Array(await crypto.subtle.sign("HMAC", key, encoder.encode(data)));
|
|
743
|
+
}
|
|
744
|
+
function toHex(bytes) {
|
|
745
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
746
|
+
}
|
|
747
|
+
function toBase64Url(bytes) {
|
|
748
|
+
let binary = "";
|
|
749
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
750
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
751
|
+
}
|
|
752
|
+
function safeEqual(a, b) {
|
|
753
|
+
if (a.length !== b.length) return false;
|
|
754
|
+
let difference = 0;
|
|
755
|
+
for (let i = 0; i < a.length; i += 1) difference |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
756
|
+
return difference === 0;
|
|
757
|
+
}
|
|
758
|
+
async function signDocumentId(secret, baseDocumentId) {
|
|
759
|
+
return toHex(await hmac(secret, `textunited-events:${baseDocumentId}`));
|
|
760
|
+
}
|
|
761
|
+
async function verifyDocumentSignature(secret, baseDocumentId, signature) {
|
|
762
|
+
return safeEqual(await signDocumentId(secret, baseDocumentId), signature);
|
|
763
|
+
}
|
|
764
|
+
async function verifySanityWebhookSignature(secret, header, body, maxAgeMs = 5 * 60 * 1e3) {
|
|
765
|
+
if (!header) return false;
|
|
766
|
+
const parts = Object.fromEntries(
|
|
767
|
+
header.split(",").map((part) => {
|
|
768
|
+
const index = part.indexOf("=");
|
|
769
|
+
return [part.slice(0, index).trim(), part.slice(index + 1).trim()];
|
|
770
|
+
})
|
|
771
|
+
);
|
|
772
|
+
const timestamp = Number(parts.t);
|
|
773
|
+
if (!parts.v1 || !Number.isFinite(timestamp)) return false;
|
|
774
|
+
if (Math.abs(Date.now() - timestamp) > maxAgeMs) return false;
|
|
775
|
+
const expected = toBase64Url(await hmac(secret, `${timestamp}.${body}`));
|
|
776
|
+
return safeEqual(expected, parts.v1);
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// src/server/sync.ts
|
|
780
|
+
var MAX_ATTEMPTS = 3;
|
|
781
|
+
async function syncDocument(baseId, context) {
|
|
782
|
+
const newId = context.newId ?? (() => crypto.randomUUID());
|
|
783
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
784
|
+
const stored = await chunkBEHPJRZM_cjs.loadState(context.store, baseId);
|
|
785
|
+
if (stored.projects.length === 0) return { status: "ignored", reason: "No TextUnited project for this document" };
|
|
786
|
+
const projects = chunkBEHPJRZM_cjs.activeProjects(stored.projects, stored.stopped);
|
|
787
|
+
const languages = chunkBEHPJRZM_cjs.projectLanguages(projects);
|
|
788
|
+
const source = await chunkBEHPJRZM_cjs.getPreferredDocument(context.store, baseId);
|
|
789
|
+
if (!source) return { status: "ignored", reason: "Document not found" };
|
|
790
|
+
const typeConfig = chunkBEHPJRZM_cjs.getDocumentTypeConfig(context.config, String(source._type));
|
|
791
|
+
if (!typeConfig) return { status: "ignored", reason: "Document type is not configured" };
|
|
792
|
+
const units = chunkBEHPJRZM_cjs.extractTranslationUnits(source, typeConfig, context.config);
|
|
793
|
+
const loaded = await chunkBEHPJRZM_cjs.loadTargets({ store: context.store, source, typeConfig, languages });
|
|
794
|
+
const createdLanguages = chunkBEHPJRZM_cjs.createMissingTranslationDocuments({
|
|
795
|
+
targets: loaded.targets,
|
|
796
|
+
source,
|
|
797
|
+
typeConfig,
|
|
798
|
+
languages,
|
|
799
|
+
newId
|
|
800
|
+
});
|
|
801
|
+
let targets = loaded.targets;
|
|
802
|
+
let state = stored.state;
|
|
803
|
+
const changedFields = /* @__PURE__ */ new Map();
|
|
804
|
+
const outcomes = [];
|
|
805
|
+
const changedUnits = /* @__PURE__ */ new Set();
|
|
806
|
+
for (const project of projects) {
|
|
807
|
+
const sentOf = (unit) => chunkBEHPJRZM_cjs.sentRecord(state.units[unit.id], project.customId);
|
|
808
|
+
const taskOf = /* @__PURE__ */ new Map();
|
|
809
|
+
for (const unit of units) {
|
|
810
|
+
const taskId = sentOf(unit)?.taskId;
|
|
811
|
+
if (taskId) for (const segment of unit.segments) taskOf.set(segment.customId, taskId);
|
|
812
|
+
}
|
|
813
|
+
const translations = toTranslationsBySegment(
|
|
814
|
+
await context.textUnited.getSegments(project.customId),
|
|
815
|
+
context.config,
|
|
816
|
+
taskOf
|
|
817
|
+
);
|
|
818
|
+
const eligible = units.filter((unit) => {
|
|
819
|
+
const sent = sentOf(unit);
|
|
820
|
+
if (!sent) return false;
|
|
821
|
+
if (sent.sourceHash !== unit.sourceHash) {
|
|
822
|
+
changedUnits.add(unit.id);
|
|
823
|
+
return false;
|
|
824
|
+
}
|
|
825
|
+
return unit.segments.every((segment) => translations.has(segment.customId));
|
|
826
|
+
});
|
|
827
|
+
const applied = applyTranslations({
|
|
828
|
+
units: eligible,
|
|
829
|
+
languages: project.languages,
|
|
830
|
+
sourceLanguage: context.config.sourceLanguage.id,
|
|
831
|
+
sourceDocument: source,
|
|
832
|
+
targets,
|
|
833
|
+
translations,
|
|
834
|
+
state
|
|
835
|
+
});
|
|
836
|
+
targets = applied.targets;
|
|
837
|
+
state = applied.state;
|
|
838
|
+
outcomes.push(...applied.outcomes);
|
|
839
|
+
for (const [key, fields] of applied.changedFields) {
|
|
840
|
+
const all = changedFields.get(key) ?? /* @__PURE__ */ new Set();
|
|
841
|
+
for (const field of fields) all.add(field);
|
|
842
|
+
changedFields.set(key, all);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
const pending = chunkBEHPJRZM_cjs.hasPendingTranslations(state, projects, isFinalStatus);
|
|
846
|
+
try {
|
|
847
|
+
await context.store.mutate([
|
|
848
|
+
...chunkBEHPJRZM_cjs.writeTargetsMutations({
|
|
849
|
+
loaded,
|
|
850
|
+
targets,
|
|
851
|
+
source,
|
|
852
|
+
typeConfig,
|
|
853
|
+
sourceLanguage: context.config.sourceLanguage.id,
|
|
854
|
+
changedFields,
|
|
855
|
+
createdLanguages,
|
|
856
|
+
newId
|
|
857
|
+
}),
|
|
858
|
+
chunkBEHPJRZM_cjs.saveStateMutation(baseId, { ...stored, state }, pending)
|
|
859
|
+
]);
|
|
860
|
+
return { status: "synced", outcomes: countOutcomes(outcomes), sourceChanged: changedUnits.size, pending };
|
|
861
|
+
} catch (error) {
|
|
862
|
+
if (!(error instanceof chunkBEHPJRZM_cjs.SanityConflictError) || attempt >= MAX_ATTEMPTS) throw error;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
async function syncPendingDocuments(context, limit = 20) {
|
|
867
|
+
const results = [];
|
|
868
|
+
for (const documentId of await context.store.listPendingDocuments(limit)) {
|
|
869
|
+
try {
|
|
870
|
+
results.push({ documentId, result: await syncDocument(documentId, context) });
|
|
871
|
+
} catch (error) {
|
|
872
|
+
results.push({
|
|
873
|
+
documentId,
|
|
874
|
+
result: { status: "failed", error: error instanceof Error ? error.message : "Sync failed" }
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
return results;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// src/server/index.ts
|
|
882
|
+
var REQUEST_ID_PREFIX = "textunited.request.";
|
|
883
|
+
var TEAM_QUERY_ID_PREFIX = "textunited.teamQuery.";
|
|
884
|
+
var TEAM_QUERY_MAX_AGE_MS = 10 * 60 * 1e3;
|
|
885
|
+
function json(body, status = 200, headers = {}) {
|
|
886
|
+
return new Response(JSON.stringify(body), {
|
|
887
|
+
status,
|
|
888
|
+
headers: { "Content-Type": "application/json", ...headers }
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
function createTextUnitedRequestHandler(options) {
|
|
892
|
+
const basePath = options.basePath.replace(/\/+$/, "");
|
|
893
|
+
const publicBaseUrl = options.publicBaseUrl.replace(/\/+$/, "");
|
|
894
|
+
const isLocal = /^http:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/.test(publicBaseUrl);
|
|
895
|
+
if (!publicBaseUrl.startsWith("https://") && !isLocal) {
|
|
896
|
+
throw new Error("publicBaseUrl must start with https:// (TextUnited only calls HTTPS addresses)");
|
|
897
|
+
}
|
|
898
|
+
const textUnited = new TextUnitedClient({
|
|
899
|
+
auth: options.textUnited.auth,
|
|
900
|
+
baseUrl: options.textUnited.baseUrl,
|
|
901
|
+
fetch: options.fetch,
|
|
902
|
+
timeoutMs: options.textUnited.timeoutMs
|
|
903
|
+
});
|
|
904
|
+
const capabilities = options.textUnited.capabilities ?? DEFAULT_CAPABILITIES;
|
|
905
|
+
let languagesPromise = null;
|
|
906
|
+
const knownLanguages = () => {
|
|
907
|
+
languagesPromise ??= textUnited.listLanguages().catch(() => {
|
|
908
|
+
languagesPromise = null;
|
|
909
|
+
return null;
|
|
910
|
+
});
|
|
911
|
+
return languagesPromise;
|
|
912
|
+
};
|
|
913
|
+
const store = options.store ?? new chunkBEHPJRZM_cjs.HttpSanityStore({
|
|
914
|
+
projectId: options.sanity.projectId,
|
|
915
|
+
dataset: options.sanity.dataset,
|
|
916
|
+
token: options.sanity.token,
|
|
917
|
+
apiVersion: options.sanity.apiVersion
|
|
918
|
+
});
|
|
919
|
+
const studioOrigins = new Set(options.studioOrigins ?? []);
|
|
920
|
+
const jobContext = {
|
|
921
|
+
config: options.translation,
|
|
922
|
+
store,
|
|
923
|
+
textUnited,
|
|
924
|
+
sanityProjectId: options.sanity.projectId,
|
|
925
|
+
dataset: options.sanity.dataset,
|
|
926
|
+
projectSettings: { ...DEFAULT_PROJECT_SETTINGS, ...options.textUnited.project },
|
|
927
|
+
inHouseSettings: {
|
|
928
|
+
...DEFAULT_IN_HOUSE_SETTINGS,
|
|
929
|
+
// The style guide is a property of the content, whoever translates it.
|
|
930
|
+
...options.textUnited.project?.styleGuideId ? { styleGuideId: options.textUnited.project.styleGuideId } : {},
|
|
931
|
+
...options.textUnited.inHouse
|
|
932
|
+
},
|
|
933
|
+
capabilities,
|
|
934
|
+
knownLanguages,
|
|
935
|
+
eventUrl: async (baseDocumentId) => {
|
|
936
|
+
if (isLocal) return void 0;
|
|
937
|
+
const url = new URL(`${publicBaseUrl}${basePath}/events`);
|
|
938
|
+
url.searchParams.set("doc", baseDocumentId);
|
|
939
|
+
url.searchParams.set("sig", await signDocumentId(options.secret, baseDocumentId));
|
|
940
|
+
return url.toString();
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
function corsHeaders(request) {
|
|
944
|
+
const origin = request.headers.get("origin");
|
|
945
|
+
if (!origin || !studioOrigins.has(origin)) return {};
|
|
946
|
+
return {
|
|
947
|
+
"Access-Control-Allow-Origin": origin,
|
|
948
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
949
|
+
"Access-Control-Allow-Headers": "Content-Type",
|
|
950
|
+
Vary: "Origin"
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
async function handleJobs(request) {
|
|
954
|
+
const cors = corsHeaders(request);
|
|
955
|
+
const body = await request.text();
|
|
956
|
+
const signature = request.headers.get("sanity-webhook-signature");
|
|
957
|
+
let requestId;
|
|
958
|
+
if (signature) {
|
|
959
|
+
if (!options.webhookSecret || !await verifySanityWebhookSignature(options.webhookSecret, signature, body)) {
|
|
960
|
+
return json({ message: "Invalid webhook signature" }, 401);
|
|
961
|
+
}
|
|
962
|
+
requestId = safeParse(body)?._id;
|
|
963
|
+
} else {
|
|
964
|
+
if (request.headers.get("origin") && Object.keys(cors).length === 0) {
|
|
965
|
+
return json({ message: "Origin not allowed" }, 403);
|
|
966
|
+
}
|
|
967
|
+
requestId = safeParse(body)?.requestId;
|
|
968
|
+
}
|
|
969
|
+
if (typeof requestId !== "string" || !requestId.startsWith(REQUEST_ID_PREFIX)) {
|
|
970
|
+
return json({ message: "Missing or invalid requestId" }, 400, cors);
|
|
971
|
+
}
|
|
972
|
+
const result = await processTranslationRequest(requestId, jobContext);
|
|
973
|
+
return json(result, result.status === "failed" ? 500 : 200, cors);
|
|
974
|
+
}
|
|
975
|
+
return async function handle(request) {
|
|
976
|
+
const { pathname } = new URL(request.url);
|
|
977
|
+
const route = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
|
|
978
|
+
if (route === "/jobs") {
|
|
979
|
+
if (request.method === "OPTIONS") {
|
|
980
|
+
const cors = corsHeaders(request);
|
|
981
|
+
return new Response(null, { status: Object.keys(cors).length ? 204 : 403, headers: cors });
|
|
982
|
+
}
|
|
983
|
+
if (request.method === "POST") return handleJobs(request);
|
|
984
|
+
}
|
|
985
|
+
if (route === "/team") {
|
|
986
|
+
if (request.method === "OPTIONS") {
|
|
987
|
+
const cors = corsHeaders(request);
|
|
988
|
+
return new Response(null, { status: Object.keys(cors).length ? 204 : 403, headers: cors });
|
|
989
|
+
}
|
|
990
|
+
if (request.method === "POST") return handleTeam(request);
|
|
991
|
+
}
|
|
992
|
+
if (route === "/events" && (request.method === "GET" || request.method === "POST")) {
|
|
993
|
+
return handleEvent(request);
|
|
994
|
+
}
|
|
995
|
+
if (route === "/sync" && (request.method === "GET" || request.method === "POST")) {
|
|
996
|
+
return handleSync(request);
|
|
997
|
+
}
|
|
998
|
+
return json({ message: "Not found" }, 404);
|
|
999
|
+
};
|
|
1000
|
+
async function handleEvent(request) {
|
|
1001
|
+
const params = new URL(request.url).searchParams;
|
|
1002
|
+
const documentId = params.get("doc");
|
|
1003
|
+
const signature = params.get("sig");
|
|
1004
|
+
if (!documentId || !signature) return json({ message: "Missing doc or sig" }, 400);
|
|
1005
|
+
if (!await verifyDocumentSignature(options.secret, documentId, signature)) {
|
|
1006
|
+
return json({ message: "Invalid signature" }, 401);
|
|
1007
|
+
}
|
|
1008
|
+
try {
|
|
1009
|
+
return json(await syncDocument(documentId, jobContext));
|
|
1010
|
+
} catch (error) {
|
|
1011
|
+
return json({ message: error instanceof Error ? error.message : "Sync failed" }, 500);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
async function handleTeam(request) {
|
|
1015
|
+
const cors = corsHeaders(request);
|
|
1016
|
+
if (request.headers.get("origin") && Object.keys(cors).length === 0) {
|
|
1017
|
+
return json({ message: "Origin not allowed" }, 403);
|
|
1018
|
+
}
|
|
1019
|
+
const queryId = safeParse(await request.text())?.queryId;
|
|
1020
|
+
if (typeof queryId !== "string" || !queryId.startsWith(TEAM_QUERY_ID_PREFIX)) {
|
|
1021
|
+
return json({ message: "Missing or invalid queryId" }, 400, cors);
|
|
1022
|
+
}
|
|
1023
|
+
const query = await store.getDocument(queryId);
|
|
1024
|
+
const createdAt = Date.parse(String(query?._createdAt ?? query?.requestedAt ?? ""));
|
|
1025
|
+
if (!query || !Number.isFinite(createdAt) || Date.now() - createdAt > TEAM_QUERY_MAX_AGE_MS) {
|
|
1026
|
+
return json({ message: "Unknown or expired team query" }, 403, cors);
|
|
1027
|
+
}
|
|
1028
|
+
await store.mutate([{ delete: { id: queryId } }]);
|
|
1029
|
+
try {
|
|
1030
|
+
return json({ members: await textUnited.listTeamCandidates() }, 200, cors);
|
|
1031
|
+
} catch (error) {
|
|
1032
|
+
return json({ message: error instanceof Error ? error.message : "Could not load the team" }, 502, cors);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
async function handleSync(request) {
|
|
1036
|
+
const expected = `Bearer ${options.cronSecret ?? options.secret}`;
|
|
1037
|
+
if (request.headers.get("authorization") !== expected) return json({ message: "Unauthorized" }, 401);
|
|
1038
|
+
const limit = Number(new URL(request.url).searchParams.get("limit") ?? 20);
|
|
1039
|
+
const results = await syncPendingDocuments(jobContext, Number.isFinite(limit) ? Math.min(limit, 100) : 20);
|
|
1040
|
+
return json({ checked: results.length, results });
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
function safeParse(body) {
|
|
1044
|
+
try {
|
|
1045
|
+
const value = JSON.parse(body);
|
|
1046
|
+
return typeof value === "object" && value !== null ? value : null;
|
|
1047
|
+
} catch {
|
|
1048
|
+
return null;
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
exports.REQUEST_ID_PREFIX = REQUEST_ID_PREFIX;
|
|
1053
|
+
exports.TEAM_QUERY_ID_PREFIX = TEAM_QUERY_ID_PREFIX;
|
|
1054
|
+
exports.TextUnitedApiError = TextUnitedApiError;
|
|
1055
|
+
exports.TextUnitedClient = TextUnitedClient;
|
|
1056
|
+
exports.TextUnitedTimeoutError = TextUnitedTimeoutError;
|
|
1057
|
+
exports.createTextUnitedRequestHandler = createTextUnitedRequestHandler;
|
|
1058
|
+
//# sourceMappingURL=chunk-BC676THN.cjs.map
|
|
1059
|
+
//# sourceMappingURL=chunk-BC676THN.cjs.map
|