i-repo 2.5.0 → 2.6.0
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/commands/reports/download.js +31 -6
- package/dist/sdk/api/report.d.ts +3 -1
- package/dist/sdk/api/report.js +2 -1
- package/dist/sdk/core/base-api.d.ts +1 -0
- package/dist/sdk/core/base-api.js +1 -1
- package/dist/sdk/core/http-client.d.ts +17 -8
- package/dist/sdk/core/http-client.js +37 -2
- package/dist/sdk/resources/reports.d.ts +1 -0
- package/dist/utils/file.d.ts +9 -0
- package/dist/utils/file.js +28 -0
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { assertNumericId, handleError } from "../../core/errors.js";
|
|
2
|
-
import { saveBinaryDownload, inferFilename } from "../../utils/file.js";
|
|
1
|
+
import { assertNumericId, handleError, ValidationError } from "../../core/errors.js";
|
|
2
|
+
import { saveBinaryDownload, inferFilename, sanitizeServerFilename } from "../../utils/file.js";
|
|
3
3
|
import { withSpinner } from "../../ui/spinner.js";
|
|
4
4
|
import { choiceOption, parsePageSpec } from "../shared-options.js";
|
|
5
5
|
export function registerReportsDownload(parent, getClient) {
|
|
@@ -8,12 +8,19 @@ export function registerReportsDownload(parent, getClient) {
|
|
|
8
8
|
.description("Download report file (PDF/Excel)")
|
|
9
9
|
.argument("<reportId>", "Report ID (numeric)")
|
|
10
10
|
.addOption(choiceOption("--file-type <type>", "File type", ["pdf", "pdfLayer", "excel"]).makeOptionMandatory())
|
|
11
|
-
.option("-o, --output <path>", "Output file path")
|
|
11
|
+
.option("-o, --output <path>", "Output file path (default: server-provided filename, else report-<id>)")
|
|
12
12
|
.option("--page <pages>", 'Pages (pdf/pdfLayer only; e.g. "1,3" or "2-4")', parsePageSpec)
|
|
13
|
-
.option("--file-name <name>", "Output file name")
|
|
13
|
+
.option("--file-name <name>", "Output file name (also sent to the server's fileName parameter)")
|
|
14
14
|
.action(async (reportId, options, cmd) => {
|
|
15
15
|
try {
|
|
16
16
|
assertNumericId(reportId, "reportId");
|
|
17
|
+
// --file-name は「ファイル名」専用 — パスは -o/--output の仕事。
|
|
18
|
+
// 空白のみ (→ 隠しファイル ".pdf") やパス区切り入りは黙って変形せず
|
|
19
|
+
// ここで明示的に拒否する (API 呼び出し前に早期失敗)。
|
|
20
|
+
const requestedName = options.fileName?.trim();
|
|
21
|
+
if (requestedName !== undefined && (requestedName === "" || /[/\\]/.test(requestedName))) {
|
|
22
|
+
throw new ValidationError(`Invalid --file-name "${options.fileName}": must be a plain file name without path separators (use --output for paths).`);
|
|
23
|
+
}
|
|
17
24
|
const globalOpts = cmd.optsWithGlobals();
|
|
18
25
|
const client = await getClient(cmd);
|
|
19
26
|
const result = await withSpinner("downloading", async () => {
|
|
@@ -21,11 +28,29 @@ export function registerReportsDownload(parent, getClient) {
|
|
|
21
28
|
fileType: options.fileType,
|
|
22
29
|
report: reportId,
|
|
23
30
|
pageNo: options.page,
|
|
24
|
-
fileName:
|
|
31
|
+
fileName: requestedName,
|
|
25
32
|
});
|
|
26
33
|
});
|
|
27
34
|
const ext = options.fileType === "excel" ? "xlsx" : "pdf";
|
|
28
|
-
|
|
35
|
+
// 保存名の優先順位: -o/--output (パスごと明示) > --file-name (明示) >
|
|
36
|
+
// サーバ申告名 (Content-Disposition = ファイル自動出力設定の名称) >
|
|
37
|
+
// 従来の report-<ID>.<ext>。ユーザーの明示指定は、サーバが fileName
|
|
38
|
+
// パラメータを無視/正規化して別名を申告してきても負けない。
|
|
39
|
+
// --file-name / サーバ名とも、拡張子付きならそのまま・無ければ .<ext>
|
|
40
|
+
// を補う ("foo.pdf" を "foo.pdf.pdf" にしない。--file-type で拡張子は
|
|
41
|
+
// 確定しているので、octet-stream 応答でも拡張子なし保存にしない)。
|
|
42
|
+
// サーバ名は無害化してから使う。
|
|
43
|
+
// 末尾のドット/空白は Windows のファイルシステムが黙って剥がす — 先に
|
|
44
|
+
// 剥がしてから拡張子判定する ("foo." / "foo.pdf " を誤判定して、要求と
|
|
45
|
+
// 違う名前でディスクに載るのを防ぐ。"foo." → "foo.pdf")
|
|
46
|
+
const withExt = (name) => {
|
|
47
|
+
const base = name.replace(/[. ]+$/, "");
|
|
48
|
+
return base.lastIndexOf(".") > 0 ? base : `${base}.${ext}`;
|
|
49
|
+
};
|
|
50
|
+
const serverBase = sanitizeServerFilename(result.fileName);
|
|
51
|
+
const serverName = serverBase ? withExt(serverBase) : undefined;
|
|
52
|
+
const explicitName = requestedName ? withExt(requestedName) : undefined;
|
|
53
|
+
const defaultName = explicitName ?? serverName ?? `report-${reportId}.${ext}`;
|
|
29
54
|
const filename = inferFilename(result.contentType, defaultName);
|
|
30
55
|
await saveBinaryDownload(result, options.output, filename, globalOpts.quiet);
|
|
31
56
|
}
|
package/dist/sdk/api/report.d.ts
CHANGED
|
@@ -53,11 +53,13 @@ export declare class ReportApi extends BaseApi {
|
|
|
53
53
|
copyReport(params: CopyReportParams): Promise<CopyReportResponse>;
|
|
54
54
|
/**
|
|
55
55
|
* 帳票ファイル (PDF/Excel) をバイナリとして取得する
|
|
56
|
-
* @returns バイナリデータとContentType
|
|
56
|
+
* @returns バイナリデータとContentType。fileName はサーバが Content-Disposition で
|
|
57
|
+
* 申告した出力名(ファイル自動出力設定の名称。無ければ undefined)
|
|
57
58
|
*/
|
|
58
59
|
getReportFile(params: GetReportFileParams): Promise<{
|
|
59
60
|
data: ArrayBuffer;
|
|
60
61
|
contentType: string;
|
|
62
|
+
fileName?: string;
|
|
61
63
|
}>;
|
|
62
64
|
/**
|
|
63
65
|
* クラスター値を取得する。
|
package/dist/sdk/api/report.js
CHANGED
|
@@ -132,7 +132,8 @@ export class ReportApi extends BaseApi {
|
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
134
|
* 帳票ファイル (PDF/Excel) をバイナリとして取得する
|
|
135
|
-
* @returns バイナリデータとContentType
|
|
135
|
+
* @returns バイナリデータとContentType。fileName はサーバが Content-Disposition で
|
|
136
|
+
* 申告した出力名(ファイル自動出力設定の名称。無ければ undefined)
|
|
136
137
|
*/
|
|
137
138
|
async getReportFile(params) {
|
|
138
139
|
const reqParams = buildParams({ command: "GetReportFile", fileType: params.fileType }, {
|
|
@@ -58,7 +58,7 @@ export class BaseApi {
|
|
|
58
58
|
}
|
|
59
59
|
// Content-Type が xml でなくても、本文が XML エラーなら沈黙保存させない
|
|
60
60
|
this.checkBinaryErrorEnvelope(result.data, params.command);
|
|
61
|
-
return { data: result.data, contentType: result.contentType };
|
|
61
|
+
return { data: result.data, contentType: result.contentType, fileName: result.fileName };
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
64
64
|
* クラスタ値取得用。XMLレスポンスで `conmas.value` があればテキスト値を
|
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
import type { IReporterConfig } from "../types/common.js";
|
|
2
|
+
/** バイナリ応答。fileName は Content-Disposition から得たサーバ提供名 (無ければ undefined) */
|
|
3
|
+
export interface BinaryResponse {
|
|
4
|
+
data: ArrayBuffer;
|
|
5
|
+
contentType: string;
|
|
6
|
+
fileName?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Content-Disposition からファイル名を取り出す (RFC 6266)。
|
|
10
|
+
* `filename*=charset''pct-encoded` (RFC 5987) を優先し、無ければ
|
|
11
|
+
* `filename="..."` / `filename=token`。パースできない・壊れた値は undefined —
|
|
12
|
+
* 呼び出し側がフォールバック名を使う。値はサーバ申告のまま返す (パス区切り等の
|
|
13
|
+
* 無害化は保存する側の責務 — SDK はワイヤ忠実)。
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseContentDispositionFilename(header: string | null | undefined): string | undefined;
|
|
2
16
|
/** HTTPクライアント: セッション(Cookie)管理 + POSTリクエスト */
|
|
3
17
|
export declare class HttpClient {
|
|
4
18
|
private readonly baseUrl;
|
|
@@ -41,19 +55,14 @@ export declare class HttpClient {
|
|
|
41
55
|
filename: string;
|
|
42
56
|
}): Promise<string>;
|
|
43
57
|
/** バイナリレスポンスでPOSTリクエスト(ダウンロード用)。画像等を破損させないため arraybuffer で受信 */
|
|
44
|
-
postForBinary(params: Record<string, string>): Promise<
|
|
45
|
-
data: ArrayBuffer;
|
|
46
|
-
contentType: string;
|
|
47
|
-
}>;
|
|
58
|
+
postForBinary(params: Record<string, string>): Promise<BinaryResponse>;
|
|
48
59
|
/** レスポンスがXMLかどうかを判定してXML/バイナリを返す。画像等は arraybuffer で取得するため専用経路を使用 */
|
|
49
60
|
postAutoDetect(params: Record<string, string>): Promise<{
|
|
50
61
|
type: "xml";
|
|
51
62
|
xml: string;
|
|
52
|
-
} | {
|
|
63
|
+
} | ({
|
|
53
64
|
type: "binary";
|
|
54
|
-
|
|
55
|
-
contentType: string;
|
|
56
|
-
}>;
|
|
65
|
+
} & BinaryResponse)>;
|
|
57
66
|
/** multipart/form-data でPOSTし、Content-Typeを判定してXML/バイナリを返す */
|
|
58
67
|
postMultipartForBinary(params: Record<string, string>, file: {
|
|
59
68
|
name: string;
|
|
@@ -2,6 +2,39 @@ import http from "node:http";
|
|
|
2
2
|
import https from "node:https";
|
|
3
3
|
import { URL } from "node:url";
|
|
4
4
|
import { HttpError, NetworkError } from "./errors.js";
|
|
5
|
+
/**
|
|
6
|
+
* Content-Disposition からファイル名を取り出す (RFC 6266)。
|
|
7
|
+
* `filename*=charset''pct-encoded` (RFC 5987) を優先し、無ければ
|
|
8
|
+
* `filename="..."` / `filename=token`。パースできない・壊れた値は undefined —
|
|
9
|
+
* 呼び出し側がフォールバック名を使う。値はサーバ申告のまま返す (パス区切り等の
|
|
10
|
+
* 無害化は保存する側の責務 — SDK はワイヤ忠実)。
|
|
11
|
+
*/
|
|
12
|
+
export function parseContentDispositionFilename(header) {
|
|
13
|
+
if (!header)
|
|
14
|
+
return undefined;
|
|
15
|
+
// パラメータ名と charset は大文字小文字を区別しない (RFC 6266/5987 — copilot 指摘)
|
|
16
|
+
// filename*=UTF-8''%E5%B8%B3%E7%A5%A8.pdf (charset は UTF-8 のみ対応)
|
|
17
|
+
const ext = /filename\*\s*=\s*utf-8''([^;]+)/i.exec(header);
|
|
18
|
+
if (ext) {
|
|
19
|
+
try {
|
|
20
|
+
const decoded = decodeURIComponent(ext[1].trim());
|
|
21
|
+
if (decoded.length > 0)
|
|
22
|
+
return decoded;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// 壊れた % エンコード → 素の filename= にフォールバック
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const quoted = /filename\s*=\s*"((?:[^"\\]|\\.)*)"/i.exec(header);
|
|
29
|
+
if (quoted) {
|
|
30
|
+
const unescaped = quoted[1].replace(/\\(.)/g, "$1");
|
|
31
|
+
return unescaped.length > 0 ? unescaped : undefined;
|
|
32
|
+
}
|
|
33
|
+
const token = /filename\s*=\s*([^;\s]+)/i.exec(header);
|
|
34
|
+
if (token && token[1].length > 0)
|
|
35
|
+
return token[1];
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
5
38
|
/** HTTPクライアント: セッション(Cookie)管理 + POSTリクエスト */
|
|
6
39
|
export class HttpClient {
|
|
7
40
|
baseUrl;
|
|
@@ -142,13 +175,13 @@ export class HttpClient {
|
|
|
142
175
|
}
|
|
143
176
|
/** レスポンスがXMLかどうかを判定してXML/バイナリを返す。画像等は arraybuffer で取得するため専用経路を使用 */
|
|
144
177
|
async postAutoDetect(params) {
|
|
145
|
-
const { data, contentType } = await this.postForBinary(params);
|
|
178
|
+
const { data, contentType, fileName } = await this.postForBinary(params);
|
|
146
179
|
const rawContentType = contentType ?? "";
|
|
147
180
|
if (rawContentType.includes("xml") && !isZipMagic(data)) {
|
|
148
181
|
const xml = new TextDecoder("utf-8").decode(data);
|
|
149
182
|
return { type: "xml", xml };
|
|
150
183
|
}
|
|
151
|
-
return { type: "binary", data, contentType: rawContentType };
|
|
184
|
+
return { type: "binary", data, contentType: rawContentType, fileName };
|
|
152
185
|
}
|
|
153
186
|
/** multipart/form-data でPOSTし、Content-Typeを判定してXML/バイナリを返す */
|
|
154
187
|
async postMultipartForBinary(params, file) {
|
|
@@ -206,6 +239,7 @@ export class HttpClient {
|
|
|
206
239
|
return {
|
|
207
240
|
data: await this.readArrayBuffer(response),
|
|
208
241
|
contentType: response.headers.get("content-type") ?? "application/octet-stream",
|
|
242
|
+
fileName: parseContentDispositionFilename(response.headers.get("content-disposition")),
|
|
209
243
|
};
|
|
210
244
|
}
|
|
211
245
|
/** Node `http`/`https` で URL-encoded を送る(ASP.NET と相性が良い) */
|
|
@@ -228,6 +262,7 @@ export class HttpClient {
|
|
|
228
262
|
return {
|
|
229
263
|
data: u8.buffer.slice(u8.byteOffset, u8.byteOffset + u8.byteLength),
|
|
230
264
|
contentType,
|
|
265
|
+
fileName: parseContentDispositionFilename(getIncomingHeader(headers, "content-disposition")),
|
|
231
266
|
};
|
|
232
267
|
}
|
|
233
268
|
async nodePostUrlEncoded(startUrl, bodyString) {
|
package/dist/utils/file.d.ts
CHANGED
|
@@ -27,6 +27,15 @@ export declare function saveBinaryDownload(result: {
|
|
|
27
27
|
data: ArrayBuffer;
|
|
28
28
|
contentType: string;
|
|
29
29
|
}, outputPath: string | undefined, defaultFilename: string, quiet?: boolean): Promise<DownloadResult>;
|
|
30
|
+
/**
|
|
31
|
+
* サーバ申告のダウンロードファイル名 (Content-Disposition) を保存に安全な
|
|
32
|
+
* 単一ファイル名へ無害化する。サーバ (または途中の何か) が "../../evil.pdf" や
|
|
33
|
+
* 制御文字入りの名前を申告しても、保存先ディレクトリの外へ書かせない。
|
|
34
|
+
* 日本語等の Unicode は保持する (帳票名がそのまま使えることが目的)。
|
|
35
|
+
* 無害化の結果ファイル名として成立しない場合は undefined — 呼び出し側が
|
|
36
|
+
* フォールバック名を使う。
|
|
37
|
+
*/
|
|
38
|
+
export declare function sanitizeServerFilename(name: string | undefined): string | undefined;
|
|
30
39
|
/**
|
|
31
40
|
* Infer a filename from content-type header.
|
|
32
41
|
* Falls back to the provided default.
|
package/dist/utils/file.js
CHANGED
|
@@ -109,6 +109,34 @@ export async function saveBinaryDownload(result, outputPath, defaultFilename, qu
|
|
|
109
109
|
size: buffer.byteLength,
|
|
110
110
|
};
|
|
111
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* サーバ申告のダウンロードファイル名 (Content-Disposition) を保存に安全な
|
|
114
|
+
* 単一ファイル名へ無害化する。サーバ (または途中の何か) が "../../evil.pdf" や
|
|
115
|
+
* 制御文字入りの名前を申告しても、保存先ディレクトリの外へ書かせない。
|
|
116
|
+
* 日本語等の Unicode は保持する (帳票名がそのまま使えることが目的)。
|
|
117
|
+
* 無害化の結果ファイル名として成立しない場合は undefined — 呼び出し側が
|
|
118
|
+
* フォールバック名を使う。
|
|
119
|
+
*/
|
|
120
|
+
export function sanitizeServerFilename(name) {
|
|
121
|
+
if (!name)
|
|
122
|
+
return undefined;
|
|
123
|
+
// パス区切りを越えさせない: 最後のセグメントだけを採用 (\ は Windows 区切り)
|
|
124
|
+
const lastSegment = name.split(/[/\\]/).pop() ?? "";
|
|
125
|
+
const cleaned = lastSegment
|
|
126
|
+
.replace(/[\u0000-\u001f\u007f]/g, "") // 制御文字 (端末・FS 両方に有害)
|
|
127
|
+
.replace(/[<>:"|?*]/g, "_") // Windows で使えない文字
|
|
128
|
+
.trim()
|
|
129
|
+
// Windows は末尾のドット/空白を黙って剥がす — 先にこちらで剥がして
|
|
130
|
+
// 「指定と違う名前で保存される」を起こさない (copilot 指摘)
|
|
131
|
+
.replace(/[. ]+$/, "");
|
|
132
|
+
if (cleaned === "")
|
|
133
|
+
return undefined;
|
|
134
|
+
// Windows 予約デバイス名 (NUL / CON.pdf 等) はどの OS でも採用しない —
|
|
135
|
+
// 保存物が後で Windows に渡ったとき開けない名前を作らない (可搬性)
|
|
136
|
+
if (isWindowsReservedName(cleaned))
|
|
137
|
+
return undefined;
|
|
138
|
+
return cleaned;
|
|
139
|
+
}
|
|
112
140
|
/**
|
|
113
141
|
* Infer a filename from content-type header.
|
|
114
142
|
* Falls back to the provided default.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "i-repo",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "Modern CLI for ConMas i-Reporter - Built for humans and AI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@types/gradient-string": "1.1.6",
|
|
64
|
-
"@types/node": "
|
|
64
|
+
"@types/node": "25.9.1",
|
|
65
65
|
"@types/react": "19.2.17",
|
|
66
66
|
"typescript": "5.9.3",
|
|
67
67
|
"vitest": "4.1.8"
|