@langlink/l10n-mcp 0.2.0 → 0.2.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/dist/index.js +52 -12
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
|
9
9
|
|
|
10
10
|
// packages/l10n-mcp/src/http.ts
|
|
11
11
|
import { createServer } from "node:http";
|
|
12
|
+
import { timingSafeEqual } from "node:crypto";
|
|
12
13
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
13
14
|
var MAX_BODY_BYTES = 10 * 1024 * 1024;
|
|
14
15
|
function sendJson(response, status, value) {
|
|
@@ -32,9 +33,20 @@ async function readBody(request) {
|
|
|
32
33
|
if (chunks.length === 0) return void 0;
|
|
33
34
|
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
34
35
|
}
|
|
36
|
+
function timingSafeEqualString(a, b) {
|
|
37
|
+
const aBuf = Buffer.from(a);
|
|
38
|
+
const bBuf = Buffer.from(b);
|
|
39
|
+
if (aBuf.length !== bBuf.length) {
|
|
40
|
+
Buffer.compare(bBuf, bBuf);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return timingSafeEqual(aBuf, bBuf);
|
|
44
|
+
}
|
|
35
45
|
function authorized(request, token) {
|
|
36
46
|
if (!token) return true;
|
|
37
|
-
|
|
47
|
+
const header = request.headers.authorization;
|
|
48
|
+
if (typeof header !== "string" || !header.startsWith("Bearer ")) return false;
|
|
49
|
+
return timingSafeEqualString(header.slice("Bearer ".length), token);
|
|
38
50
|
}
|
|
39
51
|
function createHttpMcpServer(options) {
|
|
40
52
|
return createServer(async (request, response) => {
|
|
@@ -91,9 +103,18 @@ function createHttpMcpServer(options) {
|
|
|
91
103
|
}
|
|
92
104
|
});
|
|
93
105
|
}
|
|
106
|
+
function isLoopbackHost(host) {
|
|
107
|
+
const h = host.toLowerCase();
|
|
108
|
+
return h === "127.0.0.1" || h === "::1" || h === "localhost";
|
|
109
|
+
}
|
|
94
110
|
function listenHttpMcp(options) {
|
|
95
111
|
const host = options.host ?? "127.0.0.1";
|
|
96
112
|
const port = options.port ?? 8765;
|
|
113
|
+
if (!options.token && !isLoopbackHost(host)) {
|
|
114
|
+
return Promise.reject(
|
|
115
|
+
new Error("L10N MCP HTTP token is required when binding a non-loopback host")
|
|
116
|
+
);
|
|
117
|
+
}
|
|
97
118
|
const server = createHttpMcpServer(options);
|
|
98
119
|
return new Promise((resolvePromise, reject) => {
|
|
99
120
|
server.once("error", reject);
|
|
@@ -431,7 +452,7 @@ function sdlState(segDefs) {
|
|
|
431
452
|
}
|
|
432
453
|
return Object.keys(state).length > 0 ? state : void 0;
|
|
433
454
|
}
|
|
434
|
-
function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
|
|
455
|
+
function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
|
|
435
456
|
const sourceTags = sourceContainer ? elementSignature(sourceContainer) : [];
|
|
436
457
|
const targetTags = targetContainer ? elementSignature(targetContainer) : [];
|
|
437
458
|
const id = `${fileIndex}:${unitId}:${mid ?? segmentIndex}`;
|
|
@@ -441,7 +462,7 @@ function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetCon
|
|
|
441
462
|
}
|
|
442
463
|
return {
|
|
443
464
|
id,
|
|
444
|
-
index:
|
|
465
|
+
index: ordinal,
|
|
445
466
|
location: { fileIndex, unitId, ...mid === void 0 ? {} : { mid }, segmentIndex },
|
|
446
467
|
sourceInner: sourceContainer ? xml.slice(sourceContainer.innerStart, sourceContainer.innerEnd) : "",
|
|
447
468
|
targetInner: targetContainer ? xml.slice(targetContainer.innerStart, targetContainer.innerEnd) : "",
|
|
@@ -450,6 +471,7 @@ function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetCon
|
|
|
450
471
|
sourceTags,
|
|
451
472
|
targetTags,
|
|
452
473
|
targetRange: targetContainer ? { start: targetContainer.innerStart, end: targetContainer.innerEnd } : null,
|
|
474
|
+
...sourceContainer === void 0 ? {} : { sourceNode: sourceContainer },
|
|
453
475
|
...targetContainer === void 0 ? {} : { targetNode: targetContainer },
|
|
454
476
|
...sdlNode === void 0 ? {} : { sdlNode },
|
|
455
477
|
...sdl === void 0 ? {} : { sdl }
|
|
@@ -508,6 +530,7 @@ function parseXliff(xml, options = {}) {
|
|
|
508
530
|
fileIndex,
|
|
509
531
|
unitId,
|
|
510
532
|
segmentIndex,
|
|
533
|
+
segments.length,
|
|
511
534
|
source,
|
|
512
535
|
target,
|
|
513
536
|
segmentId,
|
|
@@ -541,6 +564,7 @@ function parseXliff(xml, options = {}) {
|
|
|
541
564
|
fileIndex,
|
|
542
565
|
unitId,
|
|
543
566
|
segmentIndex,
|
|
567
|
+
segments.length,
|
|
544
568
|
sourceMrk,
|
|
545
569
|
targetMrk ?? (targetMrks.length === 0 ? targetElement : void 0),
|
|
546
570
|
mid,
|
|
@@ -555,7 +579,7 @@ function parseXliff(xml, options = {}) {
|
|
|
555
579
|
}
|
|
556
580
|
if (sourceElement || targetElement) {
|
|
557
581
|
segments.push(
|
|
558
|
-
makeSegment(fileIndex, unitId, 0, sourceElement, targetElement, void 0, sdl, sdlNode, xml, warnings)
|
|
582
|
+
makeSegment(fileIndex, unitId, 0, segments.length, sourceElement, targetElement, void 0, sdl, sdlNode, xml, warnings)
|
|
559
583
|
);
|
|
560
584
|
} else {
|
|
561
585
|
warnings.push(`${fileIndex}:${unitId}: no source/target`);
|
|
@@ -2189,18 +2213,23 @@ function checkWhitespace(context) {
|
|
|
2189
2213
|
function checkFullwidthHalfwidth(context) {
|
|
2190
2214
|
const target = context.target;
|
|
2191
2215
|
if (target.trim().length === 0) return;
|
|
2192
|
-
const
|
|
2193
|
-
|
|
2216
|
+
const exempt = new Set(context.locale?.fullwidthExemptChars ?? []);
|
|
2217
|
+
const fullwidthPattern = /[\uff10-\uff19\uff21-\uff3a\uff41-\uff5a]/gu;
|
|
2218
|
+
for (const match of target.matchAll(fullwidthPattern)) {
|
|
2219
|
+
const char = match[0];
|
|
2220
|
+
if (exempt.has(char)) continue;
|
|
2221
|
+
const index = match.index ?? 0;
|
|
2194
2222
|
draft(context, {
|
|
2195
2223
|
ruleId: "FULLWIDTH_HALFWIDTH",
|
|
2196
2224
|
message: "\u8BD1\u6587\u4E2D\u5B57\u6BCD/\u6570\u5B57\u4F7F\u7528\u5168\u89D2\u5F62\u5F0F",
|
|
2197
|
-
evidence: `found '${
|
|
2225
|
+
evidence: `found '${char}'`,
|
|
2198
2226
|
suggestion: "\u5B57\u6BCD\u4E0E\u6570\u5B57\u901A\u5E38\u4F7F\u7528\u534A\u89D2\u5F62\u5F0F",
|
|
2199
|
-
targetRange: { start:
|
|
2227
|
+
targetRange: { start: index, end: index + 1 }
|
|
2200
2228
|
});
|
|
2201
2229
|
return;
|
|
2202
2230
|
}
|
|
2203
|
-
|
|
2231
|
+
const expectFullwidthPunctuation = context.locale?.fullwidthPunctuation ?? context.targetIsCjk;
|
|
2232
|
+
if (!expectFullwidthPunctuation) return;
|
|
2204
2233
|
const mixed = /[\u4e00-\u9fff][,;:!?]|[,;:!?][\u4e00-\u9fff]/u.exec(target);
|
|
2205
2234
|
if (mixed === null || mixed.index === void 0) return;
|
|
2206
2235
|
draft(context, {
|
|
@@ -2338,10 +2367,17 @@ function checkEmptyTargetGroup(context) {
|
|
|
2338
2367
|
function checkUntranslatedCopyGroup(context) {
|
|
2339
2368
|
const { source, target } = context;
|
|
2340
2369
|
if (source.trim().length === 0 || target.trim().length === 0) return;
|
|
2341
|
-
|
|
2370
|
+
const normalizedSource = normalizeForCompare(source);
|
|
2371
|
+
if (normalizedSource !== normalizeForCompare(target)) return;
|
|
2372
|
+
const policy = context.untranslatedCopy;
|
|
2373
|
+
if (policy?.whitelist?.some((entry) => normalizeForCompare(entry) === normalizedSource)) return;
|
|
2342
2374
|
const tokens = wordTokens(source);
|
|
2343
2375
|
const cjkChars = Array.from(source).filter((char) => CJK_RE.test(char)).length;
|
|
2344
|
-
|
|
2376
|
+
const maxShort = policy?.shortSegmentMaxLength ?? 4;
|
|
2377
|
+
if (tokens.length <= 1 && cjkChars < 2 && source.trim().length <= maxShort) return;
|
|
2378
|
+
if (tokens.length <= 1 && policy?.singleTokenExemptions?.some((entry) => normalizeForCompare(entry) === normalizedSource)) {
|
|
2379
|
+
return;
|
|
2380
|
+
}
|
|
2345
2381
|
draft(context, {
|
|
2346
2382
|
ruleId: "UNTRANSLATED_COPY",
|
|
2347
2383
|
message: "\u8BD1\u6587\u4E0E\u6E90\u6587\u5B8C\u5168\u76F8\u540C\uFF08\u672A\u7FFB\u8BD1\uFF09",
|
|
@@ -2419,6 +2455,8 @@ function buildContext(segment, options, glossary) {
|
|
|
2419
2455
|
targetIsCjk: isCjkHeavy(segment.targetText),
|
|
2420
2456
|
glossary,
|
|
2421
2457
|
drafts: [],
|
|
2458
|
+
...options.locale === void 0 ? {} : { locale: options.locale },
|
|
2459
|
+
...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
|
|
2422
2460
|
...termHits === void 0 ? {} : { termHits }
|
|
2423
2461
|
};
|
|
2424
2462
|
}
|
|
@@ -2594,7 +2632,9 @@ function checkDocument(document, options = {}) {
|
|
|
2594
2632
|
const qaOptions = {
|
|
2595
2633
|
...options.glossary === void 0 ? {} : { glossary: options.glossary },
|
|
2596
2634
|
...segmentTermHits.size === 0 ? {} : { segmentTermHits },
|
|
2597
|
-
...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules }
|
|
2635
|
+
...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules },
|
|
2636
|
+
...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
|
|
2637
|
+
...options.locale === void 0 ? {} : { locale: options.locale }
|
|
2598
2638
|
};
|
|
2599
2639
|
const qa = runDeterministicChecks(document.segments, qaOptions);
|
|
2600
2640
|
return { qa, segmentTermHits };
|