@chen_hu_hu/pi-qq 1.3.2 → 1.3.4

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.
@@ -1,4 +1,5 @@
1
1
  import { formatBytes } from "../infra/display-format";
2
+ import { t } from "../infra/i18n";
2
3
 
3
4
  export interface InboundAttachmentLike {
4
5
  content_type: string;
@@ -19,7 +20,9 @@ async function readBodyLimited(response: Response, limit: number, kind: string):
19
20
  if (!response.body) {
20
21
  const body = Buffer.from(await response.arrayBuffer());
21
22
  if (body.byteLength > limit) {
22
- throw new Error(`${kind} too large (${formatBytes(body.byteLength)} > ${formatBytes(limit)})`);
23
+ throw new Error(
24
+ t("attachments.tooLarge", { kind, size: formatBytes(body.byteLength), limit: formatBytes(limit) }),
25
+ );
23
26
  }
24
27
  return body;
25
28
  }
@@ -35,14 +38,14 @@ async function readBodyLimited(response: Response, limit: number, kind: string):
35
38
  total += value.byteLength;
36
39
  if (total > limit) {
37
40
  await reader.cancel();
38
- throw new Error(`${kind} too large (>${formatBytes(limit)})`);
41
+ throw new Error(t("attachments.tooLargeShort", { kind, limit: formatBytes(limit) }));
39
42
  }
40
43
  chunks.push(value);
41
44
  }
42
45
  } finally {
43
46
  reader.releaseLock();
44
47
  }
45
- if (total === 0) throw new Error(`empty ${kind} content`);
48
+ if (total === 0) throw new Error(t("attachments.empty", { kind }));
46
49
  return Buffer.concat(chunks, total);
47
50
  }
48
51
 
@@ -52,10 +55,10 @@ export async function downloadLimited(opts: DownloadOptions): Promise<Buffer> {
52
55
  try {
53
56
  parsed = new URL(opts.url);
54
57
  } catch {
55
- throw new Error(`invalid ${opts.kind} URL`);
58
+ throw new Error(t("attachments.invalidUrl", { kind: opts.kind }));
56
59
  }
57
60
  if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
58
- throw new Error(`unsupported ${opts.kind} URL protocol`);
61
+ throw new Error(t("attachments.unsupportedProtocol", { kind: opts.kind }));
59
62
  }
60
63
 
61
64
  const controller = new AbortController();
@@ -64,19 +67,21 @@ export async function downloadLimited(opts: DownloadOptions): Promise<Buffer> {
64
67
  try {
65
68
  const response = await fetchImpl(parsed.toString(), { signal: controller.signal, redirect: "follow" });
66
69
  if (!response.ok) {
67
- throw new Error(`download failed (HTTP ${response.status})`);
70
+ throw new Error(t("attachments.httpFailed", { status: response.status }));
68
71
  }
69
72
 
70
73
  const lengthHeader = response.headers.get("content-length");
71
74
  const contentLength = lengthHeader ? Number(lengthHeader) : NaN;
72
75
  if (Number.isFinite(contentLength) && contentLength > limit) {
73
76
  await response.body?.cancel();
74
- throw new Error(`${opts.kind} too large (${formatBytes(contentLength)} > ${formatBytes(limit)})`);
77
+ throw new Error(
78
+ t("attachments.tooLarge", { kind: opts.kind, size: formatBytes(contentLength), limit: formatBytes(limit) }),
79
+ );
75
80
  }
76
81
  return await readBodyLimited(response, limit, opts.kind);
77
82
  } catch (err) {
78
83
  if (controller.signal.aborted) {
79
- throw new Error(`download timed out after ${opts.timeoutMs / 1000}s`, { cause: err });
84
+ throw new Error(t("attachments.timeout", { seconds: opts.timeoutMs / 1000 }), { cause: err });
80
85
  }
81
86
  throw err;
82
87
  } finally {
@@ -1,3 +1,5 @@
1
+ import { t } from "../infra/i18n";
2
+
1
3
  export interface AttachmentFailure {
2
4
  filename: string;
3
5
  reason: string;
@@ -6,7 +8,7 @@ export interface AttachmentFailure {
6
8
  export function formatAttachmentFailures(failures: readonly AttachmentFailure[], successCount: number): string {
7
9
  const title =
8
10
  successCount > 0
9
- ? `⚠️ ${failures.length} attachment(s) failed (${successCount} succeeded):`
10
- : "⚠️ Attachment failed:";
11
+ ? t("attachments.failedMany", { failed: failures.length, success: successCount })
12
+ : t("attachments.failedNone");
11
13
  return [title, ...failures.map((failure) => `• ${failure.filename}: ${failure.reason}`)].join("\n");
12
14
  }
@@ -5,6 +5,7 @@ import { downloadLimited, type InboundAttachmentLike } from "./attachment-downlo
5
5
  import type { AttachmentFailure } from "./attachment-failure";
6
6
  import { formatBytes } from "../infra/display-format";
7
7
  import { log } from "../infra/file-logger";
8
+ import { t } from "../infra/i18n";
8
9
 
9
10
  const DOWNLOAD_TIMEOUT_MS = 60_000;
10
11
  const UPLOAD_INDEX_FILENAME = ".index.json";
@@ -179,7 +180,7 @@ async function saveOrReuse(
179
180
  return { file: { path: candidatePath }, changed: true };
180
181
  }
181
182
 
182
- throw new Error("Could not allocate a unique filename");
183
+ throw new Error(t("attachments.noUniqueName"));
183
184
  }
184
185
 
185
186
  export async function saveInboundFiles(
@@ -207,7 +208,13 @@ export async function saveInboundFiles(
207
208
  const fallback = `file-${position + 1}`;
208
209
  const filename = sanitizeFilename(attachment.filename, fallback);
209
210
  if (typeof attachment.size === "number" && attachment.size > limit) {
210
- failures.push({ filename, reason: `file too large (${formatBytes(attachment.size)} > ${formatBytes(limit)})` });
211
+ failures.push({
212
+ filename,
213
+ reason: t("attachments.fileTooLarge", {
214
+ size: formatBytes(attachment.size),
215
+ limit: formatBytes(limit),
216
+ }),
217
+ });
211
218
  continue;
212
219
  }
213
220
 
@@ -216,14 +223,14 @@ export async function saveInboundFiles(
216
223
  url: attachment.url,
217
224
  maxSize: limit,
218
225
  timeoutMs: DOWNLOAD_TIMEOUT_MS,
219
- kind: "file",
226
+ kind: t("attachments.kindFile"),
220
227
  fetchImpl,
221
228
  });
222
229
  const result = await saveOrReuse(uploadDir, filename, body, index);
223
230
  indexChanged ||= result.changed;
224
231
  addFile(result.file);
225
232
  } catch (err) {
226
- failures.push({ filename, reason: err instanceof Error ? err.message : "Download failed" });
233
+ failures.push({ filename, reason: err instanceof Error ? err.message : t("attachments.downloadFailed") });
227
234
  }
228
235
  }
229
236
 
@@ -1,5 +1,6 @@
1
1
  import { appendFile, mkdir } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
+ import { localizeLogMessage } from "./i18n";
3
4
 
4
5
  export type LogLevel = "silent" | "error" | "warn" | "info" | "debug";
5
6
  type LogFields = Record<string, unknown>;
@@ -78,11 +79,13 @@ function stringifyError(err: unknown): string {
78
79
  function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message: string, data?: unknown): void {
79
80
  if (!isLevelEnabled(activeLevel, level)) return;
80
81
 
82
+ const localized = localizeLogMessage(message);
81
83
  const record: LogFields = {
82
84
  time: new Date().toISOString(),
83
85
  level,
84
86
  scope,
85
- msg: message,
87
+ ...(localized.msgKey ? { msgKey: localized.msgKey } : {}),
88
+ msg: localized.msg,
86
89
  };
87
90
  const fields = normalizeData(data);
88
91
  if (fields) {
@@ -105,7 +108,8 @@ function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message:
105
108
  time: record.time,
106
109
  level,
107
110
  scope,
108
- msg: message,
111
+ ...(localized.msgKey ? { msgKey: localized.msgKey } : {}),
112
+ msg: localized.msg,
109
113
  serializationError: stringifyError(err),
110
114
  });
111
115
  }