@ac-kit/cmd-exiftool 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 +42 -0
- package/LICENSE +28 -0
- package/README.md +176 -2
- package/dist/_argfile.d.ts +41 -0
- package/dist/_argfile.d.ts.map +1 -0
- package/dist/_argfile.js +99 -0
- package/dist/_argfile.js.map +1 -0
- package/dist/_inspect-file.d.ts +32 -0
- package/dist/_inspect-file.d.ts.map +1 -0
- package/dist/_inspect-file.js +103 -0
- package/dist/_inspect-file.js.map +1 -0
- package/dist/_parse-json-output.d.ts +31 -0
- package/dist/_parse-json-output.d.ts.map +1 -0
- package/dist/_parse-json-output.js +132 -0
- package/dist/_parse-json-output.js.map +1 -0
- package/dist/_print-read-arguments.d.ts +42 -0
- package/dist/_print-read-arguments.d.ts.map +1 -0
- package/dist/_print-read-arguments.js +152 -0
- package/dist/_print-read-arguments.js.map +1 -0
- package/dist/_process-handle.d.ts +7 -0
- package/dist/_process-handle.d.ts.map +1 -0
- package/dist/_process-handle.js +2 -0
- package/dist/_process-handle.js.map +1 -0
- package/dist/_response-reader.d.ts +54 -0
- package/dist/_response-reader.d.ts.map +1 -0
- package/dist/_response-reader.js +129 -0
- package/dist/_response-reader.js.map +1 -0
- package/dist/_worker.d.ts +104 -0
- package/dist/_worker.d.ts.map +1 -0
- package/dist/_worker.js +327 -0
- package/dist/_worker.js.map +1 -0
- package/dist/date-time.d.ts +28 -0
- package/dist/date-time.d.ts.map +1 -0
- package/dist/date-time.js +74 -0
- package/dist/date-time.js.map +1 -0
- package/dist/errors.d.ts +125 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +151 -0
- package/dist/errors.js.map +1 -0
- package/dist/exiftool-pool.d.ts +203 -0
- package/dist/exiftool-pool.d.ts.map +1 -0
- package/dist/exiftool-pool.js +455 -0
- package/dist/exiftool-pool.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/metadata.d.ts +46 -0
- package/dist/metadata.d.ts.map +1 -0
- package/dist/metadata.js +8 -0
- package/dist/metadata.js.map +1 -0
- package/dist/read-options.d.ts +86 -0
- package/dist/read-options.d.ts.map +1 -0
- package/dist/read-options.js +2 -0
- package/dist/read-options.js.map +1 -0
- package/dist/version.d.ts +39 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +53 -0
- package/dist/version.js.map +1 -0
- package/package.json +61 -3
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { BACKSLASH, DOUBLE_QUOTE, LEFT_CURLY_BRACKET, LEFT_SQUARE_BRACKET, RIGHT_CURLY_BRACKET, RIGHT_SQUARE_BRACKET, } from "@ac-kit/core";
|
|
2
|
+
import { ExiftoolLimitError, ExiftoolOutputError } from "./errors.js";
|
|
3
|
+
import { EXIFTOOL_MAX_JSON_DEPTH } from "./metadata.js";
|
|
4
|
+
const DECODER_ = new TextDecoder("utf-8", { fatal: true });
|
|
5
|
+
// ES2025 "JSON.parse source text access" hands primitives their source text.
|
|
6
|
+
const JSON_REVIVER_ = (_key, value, context) => typeof value === "number" &&
|
|
7
|
+
!Number.isFinite(value) &&
|
|
8
|
+
context?.source !== undefined
|
|
9
|
+
? context.source
|
|
10
|
+
: value;
|
|
11
|
+
const TEXT_REVIVER_ = (_key, value, context) => {
|
|
12
|
+
if (typeof value === "number") {
|
|
13
|
+
return context?.source ?? String(value);
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === "boolean") {
|
|
16
|
+
return String(value);
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Parses the stdout of one `-json` command.
|
|
22
|
+
*
|
|
23
|
+
* @param body The bytes before the `{ready}` marker.
|
|
24
|
+
* @param scalars `"text"` turns every number and boolean left by exiftool into
|
|
25
|
+
* its source text.
|
|
26
|
+
* @returns The entries, `[]` for empty output. O(body length).
|
|
27
|
+
* @throws {ExiftoolLimitError} Kind `"nesting-depth"` when arrays and objects
|
|
28
|
+
* nest deeper than {@link EXIFTOOL_MAX_JSON_DEPTH}: `JSON.parse` revives
|
|
29
|
+
* recursively, so the depth is checked first.
|
|
30
|
+
* @throws {ExiftoolOutputError} When the bytes are not UTF-8, not JSON, or not
|
|
31
|
+
* an array of objects with a string `SourceFile`.
|
|
32
|
+
*/
|
|
33
|
+
export function parseJsonOutput(body, scalars) {
|
|
34
|
+
let text;
|
|
35
|
+
try {
|
|
36
|
+
text = DECODER_.decode(body);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new ExiftoolOutputError("exiftool output is not UTF-8", {
|
|
40
|
+
cause: error,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (text.trim() === "") {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
const depth = firstExcessDepth_(text, EXIFTOOL_MAX_JSON_DEPTH);
|
|
47
|
+
if (depth !== null) {
|
|
48
|
+
throw new ExiftoolLimitError("nesting-depth", EXIFTOOL_MAX_JSON_DEPTH, depth);
|
|
49
|
+
}
|
|
50
|
+
let parsed;
|
|
51
|
+
try {
|
|
52
|
+
parsed = JSON.parse(text, (scalars === "text" ? TEXT_REVIVER_ : JSON_REVIVER_));
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
throw new ExiftoolOutputError("exiftool output is not JSON", {
|
|
56
|
+
cause: error,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (!Array.isArray(parsed)) {
|
|
60
|
+
throw new ExiftoolOutputError("exiftool output is not a JSON array");
|
|
61
|
+
}
|
|
62
|
+
for (const entry of parsed) {
|
|
63
|
+
if (typeof entry !== "object" ||
|
|
64
|
+
entry === null ||
|
|
65
|
+
Array.isArray(entry) ||
|
|
66
|
+
typeof entry.SourceFile !== "string") {
|
|
67
|
+
throw new ExiftoolOutputError("exiftool output entry is not an object with a SourceFile");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return parsed;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Scans JSON text for bracket nesting, skipping strings. O(length), no
|
|
74
|
+
* recursion; malformed text is left for `JSON.parse` to reject.
|
|
75
|
+
*
|
|
76
|
+
* @returns `maxDepth + 1` as soon as it is reached, otherwise `null`.
|
|
77
|
+
*/
|
|
78
|
+
function firstExcessDepth_(text, maxDepth) {
|
|
79
|
+
let depth = 0;
|
|
80
|
+
let inString = false;
|
|
81
|
+
for (let index = 0; index < text.length; index++) {
|
|
82
|
+
const character = text.charCodeAt(index);
|
|
83
|
+
if (inString) {
|
|
84
|
+
if (character === BACKSLASH) {
|
|
85
|
+
index++;
|
|
86
|
+
}
|
|
87
|
+
else if (character === DOUBLE_QUOTE) {
|
|
88
|
+
inString = false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else if (character === DOUBLE_QUOTE) {
|
|
92
|
+
inString = true;
|
|
93
|
+
}
|
|
94
|
+
else if (character === LEFT_SQUARE_BRACKET ||
|
|
95
|
+
character === LEFT_CURLY_BRACKET) {
|
|
96
|
+
depth++;
|
|
97
|
+
if (depth > maxDepth) {
|
|
98
|
+
return depth;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else if (character === RIGHT_SQUARE_BRACKET ||
|
|
102
|
+
character === RIGHT_CURLY_BRACKET) {
|
|
103
|
+
depth--;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Finds exiftool's own `Error` or `Warning` tag among an entry's keys.
|
|
110
|
+
*
|
|
111
|
+
* The key ends in `:<name>` (or is `<name>` with no groups). When the families
|
|
112
|
+
* include 0, 1 or 2, one of its groups must be `ExifTool`, which is what those
|
|
113
|
+
* families call exiftool's own tags; otherwise the first match is taken.
|
|
114
|
+
*
|
|
115
|
+
* @returns The value as text, or `null`.
|
|
116
|
+
*/
|
|
117
|
+
export function findExiftoolMessage(tags, name, groups) {
|
|
118
|
+
const needsExiftoolGroup = groups.some((family) => family <= 2);
|
|
119
|
+
for (const key of Object.keys(tags)) {
|
|
120
|
+
const segments = key.split(":");
|
|
121
|
+
if (segments.at(-1) !== name) {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (needsExiftoolGroup && !segments.slice(0, -1).includes("ExifTool")) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const value = tags[key];
|
|
128
|
+
return typeof value === "string" ? value : (JSON.stringify(value) ?? null);
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=_parse-json-output.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_parse-json-output.js","sourceRoot":"","sources":["../src/_parse-json-output.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EACT,YAAY,EACZ,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,GACpB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,uBAAuB,EAAsB,MAAM,eAAe,CAAC;AAe5E,MAAM,QAAQ,GAAG,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAE3D,6EAA6E;AAC7E,MAAM,aAAa,GAAa,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,CACxD,OAAO,KAAK,KAAK,QAAQ;IACzB,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;IACvB,OAAO,EAAE,MAAM,KAAK,SAAS;IAC5B,CAAC,CAAC,OAAO,CAAC,MAAM;IAChB,CAAC,CAAC,KAAK,CAAC;AAEV,MAAM,aAAa,GAAa,CAAC,IAAI,EAAE,KAAK,EAAE,OAA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M,IACN,SAAS,KAAK,oBAAoB;YAClC,SAAS,KAAK,mBAAmB,EAChC,CAAC;YACF,KAAK,EAAE,CAAC;QACT,CAAC;IACF,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAClC,IAAsD,EACtD,IAAyB,EACzB,MAAsC;IAEtC,MAAM,kBAAkB,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC;IAEhE,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACrC,MAAM,QAAQ,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;YAC9B,SAAS;QACV,CAAC;QACD,IAAI,kBAAkB,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;YACvE,SAAS;QACV,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QACxB,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,CAAC;IAC5E,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ExiftoolReadOptions } from "./read-options.js";
|
|
2
|
+
/**
|
|
3
|
+
* Lower-cased `-api` names a read may set; see `ExiftoolReadOptions.api` for
|
|
4
|
+
* the rationale.
|
|
5
|
+
*/
|
|
6
|
+
export declare const READ_API_OPTIONS: ReadonlySet<string>;
|
|
7
|
+
/**
|
|
8
|
+
* Prints the argument lines of one read, everything but the end marker and the
|
|
9
|
+
* file.
|
|
10
|
+
*
|
|
11
|
+
* Every tag selector goes out as `-All:<selector>` (or `--All:<selector>`):
|
|
12
|
+
* `All` matches any group, and no exiftool option starts with `All:`, so a
|
|
13
|
+
* selector can never become an option such as `-delete_original` or `-if`.
|
|
14
|
+
*
|
|
15
|
+
* @returns Argument-file lines, each already encoded.
|
|
16
|
+
* @throws {ExiftoolArgumentError} For an invalid selector, group family or
|
|
17
|
+
* `-api` name, or an `-api` value the argument file cannot carry.
|
|
18
|
+
*/
|
|
19
|
+
export declare function printReadArguments(options?: ExiftoolReadOptions): string[];
|
|
20
|
+
/** The file operand of a read, as sent and as exiftool reports it back. */
|
|
21
|
+
export type PathArgument = {
|
|
22
|
+
/** The argument-file line. */
|
|
23
|
+
readonly line: string;
|
|
24
|
+
/** The operand exiftool receives, and prints back as `SourceFile`. */
|
|
25
|
+
readonly operand: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Encodes the file argument of a read.
|
|
29
|
+
*
|
|
30
|
+
* A relative path is sent as `./<path>`, so nothing it starts with (`-`, U+2212
|
|
31
|
+
* MINUS SIGN, which exiftool also takes as an option prefix, `-execute`) can
|
|
32
|
+
* read as an option. On Windows, exiftool expands `*` and `?` in file names, so
|
|
33
|
+
* those are refused there, as is a drive-relative `C:name`.
|
|
34
|
+
*
|
|
35
|
+
* @param path A local file path, relative to the pool's working directory or
|
|
36
|
+
* absolute.
|
|
37
|
+
* @param platform The platform whose path rules apply (for tests).
|
|
38
|
+
* @throws {ExiftoolArgumentError} For an empty path, NUL, a lone surrogate, a
|
|
39
|
+
* path the argument file cannot carry, or one of the Windows cases above.
|
|
40
|
+
*/
|
|
41
|
+
export declare function printPathArgument(path: string, platform?: NodeJS.Platform): PathArgument;
|
|
42
|
+
//# sourceMappingURL=_print-read-arguments.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_print-read-arguments.d.ts","sourceRoot":"","sources":["../src/_print-read-arguments.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAK7D;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,WAAW,CAAC,MAAM,CAoChD,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,MAAM,EAAE,CA4E1E;AAED,2EAA2E;AAC3E,MAAM,MAAM,YAAY,GAAG;IAC1B,8BAA8B;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,sEAAsE;IACtE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CACzB,CAAC;AAIF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAChC,IAAI,EAAE,MAAM,EACZ,QAAQ,GAAE,MAAM,CAAC,QAA2B,GAC1C,YAAY,CA4Bd"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { posix, win32 } from "node:path";
|
|
2
|
+
import { encodeArgfileLine } from "./_argfile.js";
|
|
3
|
+
import { ExiftoolArgumentError } from "./errors.js";
|
|
4
|
+
const SELECTOR_ = /^(?:\d?[A-Za-z0-9_][A-Za-z0-9_-]*:)*[A-Za-z0-9_?*][A-Za-z0-9_?*-]*#?$/;
|
|
5
|
+
/**
|
|
6
|
+
* Lower-cased `-api` names a read may set; see `ExiftoolReadOptions.api` for
|
|
7
|
+
* the rationale.
|
|
8
|
+
*/
|
|
9
|
+
export const READ_API_OPTIONS = new Set([
|
|
10
|
+
"ByteUnit",
|
|
11
|
+
"CharsetEXIF",
|
|
12
|
+
"CharsetID3",
|
|
13
|
+
"CharsetIPTC",
|
|
14
|
+
"CharsetPhotoshop",
|
|
15
|
+
"CharsetQuickTime",
|
|
16
|
+
"CharsetRIFF",
|
|
17
|
+
"Composite",
|
|
18
|
+
"DateFormat",
|
|
19
|
+
"Duplicates",
|
|
20
|
+
"GPSQuadrant",
|
|
21
|
+
"HexTagIDs",
|
|
22
|
+
"IgnoreGroups",
|
|
23
|
+
"IgnoreMinorErrors",
|
|
24
|
+
"IgnoreTags",
|
|
25
|
+
"ImageHashType",
|
|
26
|
+
"KeepUTCTime",
|
|
27
|
+
"LargeFileSupport",
|
|
28
|
+
"LimitLongValues",
|
|
29
|
+
"MissingTagValue",
|
|
30
|
+
"NoPDFList",
|
|
31
|
+
"QuickTimeHandler",
|
|
32
|
+
"QuickTimeUTC",
|
|
33
|
+
"RequestAll",
|
|
34
|
+
"RequestTags",
|
|
35
|
+
"ScanForXMP",
|
|
36
|
+
"StrictDate",
|
|
37
|
+
"SystemTags",
|
|
38
|
+
"SystemTimeRes",
|
|
39
|
+
"Validate",
|
|
40
|
+
"WindowsLongPath",
|
|
41
|
+
"WindowsWideFile",
|
|
42
|
+
"XMPAutoConv",
|
|
43
|
+
].map((name) => name.toLowerCase()));
|
|
44
|
+
/**
|
|
45
|
+
* Prints the argument lines of one read, everything but the end marker and the
|
|
46
|
+
* file.
|
|
47
|
+
*
|
|
48
|
+
* Every tag selector goes out as `-All:<selector>` (or `--All:<selector>`):
|
|
49
|
+
* `All` matches any group, and no exiftool option starts with `All:`, so a
|
|
50
|
+
* selector can never become an option such as `-delete_original` or `-if`.
|
|
51
|
+
*
|
|
52
|
+
* @returns Argument-file lines, each already encoded.
|
|
53
|
+
* @throws {ExiftoolArgumentError} For an invalid selector, group family or
|
|
54
|
+
* `-api` name, or an `-api` value the argument file cannot carry.
|
|
55
|
+
*/
|
|
56
|
+
export function printReadArguments(options) {
|
|
57
|
+
const lines = ["-json"];
|
|
58
|
+
const groups = options?.groups ?? [1];
|
|
59
|
+
const seen = new Set();
|
|
60
|
+
for (let index = 0; index < groups.length; index++) {
|
|
61
|
+
const family = groups[index];
|
|
62
|
+
if (!Number.isInteger(family) || family < 0 || family > 8) {
|
|
63
|
+
throw new ExiftoolArgumentError("invalid-name", `groups[${index}]`, `not a group family from 0 to 8: ${String(family)}`);
|
|
64
|
+
}
|
|
65
|
+
if (seen.has(family)) {
|
|
66
|
+
throw new ExiftoolArgumentError("invalid-name", `groups[${index}]`, `family ${family} given twice`);
|
|
67
|
+
}
|
|
68
|
+
seen.add(family);
|
|
69
|
+
}
|
|
70
|
+
if (groups.length > 0) {
|
|
71
|
+
lines.push(`-G${groups.join(":")}`);
|
|
72
|
+
}
|
|
73
|
+
if (options?.printConversion !== true) {
|
|
74
|
+
lines.push("-n");
|
|
75
|
+
}
|
|
76
|
+
lines.push("-charset", "filename=utf8", "-api", "LargeFileSupport=1");
|
|
77
|
+
if (options?.scalars === "text") {
|
|
78
|
+
lines.push("-api", "StructFormat=JSONQ");
|
|
79
|
+
}
|
|
80
|
+
if (options?.struct === true) {
|
|
81
|
+
lines.push("-struct");
|
|
82
|
+
}
|
|
83
|
+
pushLevel_(lines, "extractEmbedded", options?.extractEmbedded, 3, (level) => level === 1 ? "-ee" : `-ee${level}`);
|
|
84
|
+
pushLevel_(lines, "unknown", options?.unknown, 2, (level) => level === 1 ? "-u" : "-U");
|
|
85
|
+
pushLevel_(lines, "fast", options?.fast, 5, (level) => level === 1 ? "-fast" : `-fast${level}`);
|
|
86
|
+
const api = options?.api ?? [];
|
|
87
|
+
for (let index = 0; index < api.length; index++) {
|
|
88
|
+
const [name, value] = api[index];
|
|
89
|
+
if (typeof name !== "string" || !READ_API_OPTIONS.has(name.toLowerCase())) {
|
|
90
|
+
throw new ExiftoolArgumentError("invalid-name", `api[${index}].name`, `-api option not allowed for reads: ${String(name)}`);
|
|
91
|
+
}
|
|
92
|
+
if (typeof value !== "string") {
|
|
93
|
+
throw new ExiftoolArgumentError("invalid-value", `api[${index}].value`, "must be a string");
|
|
94
|
+
}
|
|
95
|
+
lines.push("-api", encodeArgfileLine(`${name}=${value}`, `api[${index}].value`));
|
|
96
|
+
}
|
|
97
|
+
pushSelectors_(lines, "tags", options?.tags, "-All:");
|
|
98
|
+
pushSelectors_(lines, "excludeTags", options?.excludeTags, "--All:");
|
|
99
|
+
return lines;
|
|
100
|
+
}
|
|
101
|
+
const WINDOWS_DRIVE_RELATIVE_ = /^[A-Za-z]:(?![\\/])/;
|
|
102
|
+
/**
|
|
103
|
+
* Encodes the file argument of a read.
|
|
104
|
+
*
|
|
105
|
+
* A relative path is sent as `./<path>`, so nothing it starts with (`-`, U+2212
|
|
106
|
+
* MINUS SIGN, which exiftool also takes as an option prefix, `-execute`) can
|
|
107
|
+
* read as an option. On Windows, exiftool expands `*` and `?` in file names, so
|
|
108
|
+
* those are refused there, as is a drive-relative `C:name`.
|
|
109
|
+
*
|
|
110
|
+
* @param path A local file path, relative to the pool's working directory or
|
|
111
|
+
* absolute.
|
|
112
|
+
* @param platform The platform whose path rules apply (for tests).
|
|
113
|
+
* @throws {ExiftoolArgumentError} For an empty path, NUL, a lone surrogate, a
|
|
114
|
+
* path the argument file cannot carry, or one of the Windows cases above.
|
|
115
|
+
*/
|
|
116
|
+
export function printPathArgument(path, platform = process.platform) {
|
|
117
|
+
if (typeof path !== "string" || path === "") {
|
|
118
|
+
throw new ExiftoolArgumentError("invalid-value", "path", "must be a non-empty string");
|
|
119
|
+
}
|
|
120
|
+
const windows = platform === "win32";
|
|
121
|
+
if (windows && /[*?]/.test(path)) {
|
|
122
|
+
throw new ExiftoolArgumentError("invalid-value", "path", "'*' and '?' are wildcards to exiftool on Windows");
|
|
123
|
+
}
|
|
124
|
+
if (windows && WINDOWS_DRIVE_RELATIVE_.test(path)) {
|
|
125
|
+
throw new ExiftoolArgumentError("invalid-value", "path", "a drive-relative path (such as C:name) is ambiguous; use an absolute path");
|
|
126
|
+
}
|
|
127
|
+
const absolute = windows ? win32.isAbsolute(path) : posix.isAbsolute(path);
|
|
128
|
+
const operand = absolute ? path : `./${path}`;
|
|
129
|
+
return { line: encodeArgfileLine(operand, "path"), operand };
|
|
130
|
+
}
|
|
131
|
+
function pushLevel_(lines, field, level, maximum, print) {
|
|
132
|
+
if (level === undefined || level === null || level === 0) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (!Number.isInteger(level) || level < 0 || level > maximum) {
|
|
136
|
+
throw new ExiftoolArgumentError("invalid-value", field, `must be an integer from 0 to ${maximum}: ${String(level)}`);
|
|
137
|
+
}
|
|
138
|
+
lines.push(print(level));
|
|
139
|
+
}
|
|
140
|
+
function pushSelectors_(lines, field, selectors, prefix) {
|
|
141
|
+
if (!selectors) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
for (let index = 0; index < selectors.length; index++) {
|
|
145
|
+
const selector = selectors[index];
|
|
146
|
+
if (typeof selector !== "string" || !SELECTOR_.test(selector)) {
|
|
147
|
+
throw new ExiftoolArgumentError("invalid-name", `${field}[${index}]`, `not a tag selector: ${JSON.stringify(selector)}`);
|
|
148
|
+
}
|
|
149
|
+
lines.push(prefix + selector);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=_print-read-arguments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_print-read-arguments.js","sourceRoot":"","sources":["../src/_print-read-arguments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGpD,MAAM,SAAS,GACd,uEAAuE,CAAC;AAEzE;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAwB,IAAI,GAAG,CAC3D;IACC,UAAU;IACV,aAAa;IACb,YAAY;IACZ,aAAa;IACb,kBAAkB;IAClB,kBAAkB;IAClB,aAAa;IACb,WAAW;IACX,YAAY;IACZ,YAAY;IACZ,aAAa;IACb,WAAW;IACX,cAAc;IACd,mBAAmB;IACnB,YAAY;IACZ,eAAe;IACf,aAAa;IACb,kBAAkB;IAClB,iBAAiB;IACjB,iBAAiB;IAC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B,OAAO;IACR,CAAC;IACD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACvD,MAAM,QAAQ,GAAG,SAAS,CAAC,KAAK,CAAE,CAAC;QACnC,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC/D,MAAM,IAAI,qBAAqB,CAC9B,cAAc,EACd,GAAG,KAAK,IAAI,KAAK,GAAG,EACpB,uBAAuB,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,CACjD,CAAC;QACH,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAC,CAAC;IAC/B,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SpawnedProcess } from "@ac-kit/node";
|
|
2
|
+
/**
|
|
3
|
+
* What the worker uses of a spawned process: the boundary the in-memory fake in
|
|
4
|
+
* `__fixtures__/fake-exiftool.ts` implements.
|
|
5
|
+
*/
|
|
6
|
+
export type ProcessHandle = Pick<SpawnedProcess, "pid" | "stdin" | "stdout" | "stderr" | "waitForExit" | "terminate" | "kill">;
|
|
7
|
+
//# sourceMappingURL=_process-handle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_process-handle.d.ts","sourceRoot":"","sources":["../src/_process-handle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,IAAI,CAC/B,cAAc,EACd,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,aAAa,GAAG,WAAW,GAAG,MAAM,CAC5E,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_process-handle.js","sourceRoot":"","sources":["../src/_process-handle.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/** One stretch of output closed by an end-marker line. */
|
|
2
|
+
export type ResponseFrame = {
|
|
3
|
+
/** The bytes before the marker line, at most `maxBytes` of them. */
|
|
4
|
+
readonly body: Uint8Array<ArrayBuffer>;
|
|
5
|
+
/** How many bytes came before the marker line, kept or not. */
|
|
6
|
+
readonly bodyBytes: number;
|
|
7
|
+
/** The marker line, terminator (LF or CRLF) removed. */
|
|
8
|
+
readonly marker: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Splits a child's output stream into frames closed by marker lines, such as
|
|
12
|
+
* exiftool's `{ready<id>}`.
|
|
13
|
+
*
|
|
14
|
+
* Works on raw bytes with no line-length limit: a JSON line of any length is
|
|
15
|
+
* body, and only lines short enough to be a marker are tested. Memory is the
|
|
16
|
+
* kept body (at most `maxBytes`) plus one marker-sized line; bytes past
|
|
17
|
+
* `maxBytes` are counted and dropped, and the marker is still found. Each input
|
|
18
|
+
* byte is visited a constant number of times (`indexOf` for line ends).
|
|
19
|
+
*/
|
|
20
|
+
export declare class ResponseScanner {
|
|
21
|
+
private readonly maxBytes;
|
|
22
|
+
private readonly isMarker;
|
|
23
|
+
private readonly kept;
|
|
24
|
+
private keptBytes;
|
|
25
|
+
private bodyBytes;
|
|
26
|
+
private readonly line;
|
|
27
|
+
private lineLength;
|
|
28
|
+
/** The current line was too long to be a marker and went to the body. */
|
|
29
|
+
private lineFlushed;
|
|
30
|
+
/**
|
|
31
|
+
* @param maxBytes Body bytes kept per frame.
|
|
32
|
+
* @param isMarker Whether a complete line (terminator removed, decoded as
|
|
33
|
+
* Latin-1) ends a frame.
|
|
34
|
+
*/
|
|
35
|
+
constructor(maxBytes: number, isMarker: (line: string) => boolean);
|
|
36
|
+
/**
|
|
37
|
+
* Feeds one chunk.
|
|
38
|
+
*
|
|
39
|
+
* @param chunk Not retained beyond the frame it belongs to; never mutated.
|
|
40
|
+
* @returns The frames this chunk completed, usually none or one.
|
|
41
|
+
*/
|
|
42
|
+
push(chunk: Uint8Array): ResponseFrame[];
|
|
43
|
+
/** Bytes received since the last frame, kept or not. */
|
|
44
|
+
get pendingBytes(): number;
|
|
45
|
+
/**
|
|
46
|
+
* The bytes received since the last frame, for diagnostics when the stream
|
|
47
|
+
* ends without a marker.
|
|
48
|
+
*/
|
|
49
|
+
pendingBody(): Uint8Array<ArrayBuffer>;
|
|
50
|
+
private markerOf;
|
|
51
|
+
private takeFrame;
|
|
52
|
+
private appendBody;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=_response-reader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_response-reader.d.ts","sourceRoot":"","sources":["../src/_response-reader.ts"],"names":[],"mappings":"AAQA,0DAA0D;AAC1D,MAAM,MAAM,aAAa,GAAG;IAC3B,oEAAoE;IACpE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IACvC,+DAA+D;IAC/D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,wDAAwD;IACxD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;;;;;GASG;AACH,qBAAa,eAAe;IAe1B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAf1B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAoB;IACzC,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAyC;IAC9D,OAAO,CAAC,UAAU,CAAK;IACvB,yEAAyE;IACzE,OAAO,CAAC,WAAW,CAAS;IAE5B;;;;OAIG;IACH,YACkB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,EACjD;IAEJ;;;;;OAKG;IACH,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,aAAa,EAAE,CAuCvC;IAED,wDAAwD;IACxD,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED;;;OAGG;IACH,WAAW,IAAI,UAAU,CAAC,WAAW,CAAC,CAOrC;IAED,OAAO,CAAC,QAAQ;IAYhB,OAAO,CAAC,SAAS;IAYjB,OAAO,CAAC,UAAU;CAUlB"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { concatBytes } from "@ac-kit/core";
|
|
2
|
+
/** The longest end-marker line recognised, terminator excluded. */
|
|
3
|
+
const MAX_MARKER_BYTES_ = 96;
|
|
4
|
+
const LF_ = 0x0a;
|
|
5
|
+
const CR_ = 0x0d;
|
|
6
|
+
const MARKER_DECODER_ = new TextDecoder("latin1");
|
|
7
|
+
/**
|
|
8
|
+
* Splits a child's output stream into frames closed by marker lines, such as
|
|
9
|
+
* exiftool's `{ready<id>}`.
|
|
10
|
+
*
|
|
11
|
+
* Works on raw bytes with no line-length limit: a JSON line of any length is
|
|
12
|
+
* body, and only lines short enough to be a marker are tested. Memory is the
|
|
13
|
+
* kept body (at most `maxBytes`) plus one marker-sized line; bytes past
|
|
14
|
+
* `maxBytes` are counted and dropped, and the marker is still found. Each input
|
|
15
|
+
* byte is visited a constant number of times (`indexOf` for line ends).
|
|
16
|
+
*/
|
|
17
|
+
export class ResponseScanner {
|
|
18
|
+
maxBytes;
|
|
19
|
+
isMarker;
|
|
20
|
+
kept = [];
|
|
21
|
+
keptBytes = 0;
|
|
22
|
+
bodyBytes = 0;
|
|
23
|
+
line = new Uint8Array(MAX_MARKER_BYTES_ + 2);
|
|
24
|
+
lineLength = 0;
|
|
25
|
+
/** The current line was too long to be a marker and went to the body. */
|
|
26
|
+
lineFlushed = false;
|
|
27
|
+
/**
|
|
28
|
+
* @param maxBytes Body bytes kept per frame.
|
|
29
|
+
* @param isMarker Whether a complete line (terminator removed, decoded as
|
|
30
|
+
* Latin-1) ends a frame.
|
|
31
|
+
*/
|
|
32
|
+
constructor(maxBytes, isMarker) {
|
|
33
|
+
this.maxBytes = maxBytes;
|
|
34
|
+
this.isMarker = isMarker;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Feeds one chunk.
|
|
38
|
+
*
|
|
39
|
+
* @param chunk Not retained beyond the frame it belongs to; never mutated.
|
|
40
|
+
* @returns The frames this chunk completed, usually none or one.
|
|
41
|
+
*/
|
|
42
|
+
push(chunk) {
|
|
43
|
+
const frames = [];
|
|
44
|
+
let position = 0;
|
|
45
|
+
while (position < chunk.length) {
|
|
46
|
+
const newline = chunk.indexOf(LF_, position);
|
|
47
|
+
const end = newline === -1 ? chunk.length : newline + 1;
|
|
48
|
+
const segment = chunk.subarray(position, end);
|
|
49
|
+
position = end;
|
|
50
|
+
if (this.lineFlushed) {
|
|
51
|
+
this.appendBody(segment);
|
|
52
|
+
}
|
|
53
|
+
else if (this.lineLength + segment.length > this.line.length) {
|
|
54
|
+
this.appendBody(this.line.slice(0, this.lineLength));
|
|
55
|
+
this.appendBody(segment);
|
|
56
|
+
this.lineLength = 0;
|
|
57
|
+
this.lineFlushed = true;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.line.set(segment, this.lineLength);
|
|
61
|
+
this.lineLength += segment.length;
|
|
62
|
+
}
|
|
63
|
+
if (newline === -1) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (!this.lineFlushed) {
|
|
67
|
+
const marker = this.markerOf();
|
|
68
|
+
if (marker !== null) {
|
|
69
|
+
frames.push(this.takeFrame(marker));
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this.appendBody(this.line.slice(0, this.lineLength));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
this.lineLength = 0;
|
|
76
|
+
this.lineFlushed = false;
|
|
77
|
+
}
|
|
78
|
+
return frames;
|
|
79
|
+
}
|
|
80
|
+
/** Bytes received since the last frame, kept or not. */
|
|
81
|
+
get pendingBytes() {
|
|
82
|
+
return this.bodyBytes + (this.lineFlushed ? 0 : this.lineLength);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The bytes received since the last frame, for diagnostics when the stream
|
|
86
|
+
* ends without a marker.
|
|
87
|
+
*/
|
|
88
|
+
pendingBody() {
|
|
89
|
+
const parts = [...this.kept];
|
|
90
|
+
if (!this.lineFlushed && this.lineLength > 0) {
|
|
91
|
+
parts.push(this.line.subarray(0, this.lineLength));
|
|
92
|
+
}
|
|
93
|
+
const body = concatBytes(parts);
|
|
94
|
+
return body.length <= this.maxBytes ? body : body.slice(0, this.maxBytes);
|
|
95
|
+
}
|
|
96
|
+
markerOf() {
|
|
97
|
+
let length = this.lineLength - 1;
|
|
98
|
+
if (length > 0 && this.line[length - 1] === CR_) {
|
|
99
|
+
length--;
|
|
100
|
+
}
|
|
101
|
+
if (length > MAX_MARKER_BYTES_) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
const text = MARKER_DECODER_.decode(this.line.subarray(0, length));
|
|
105
|
+
return this.isMarker(text) ? text : null;
|
|
106
|
+
}
|
|
107
|
+
takeFrame(marker) {
|
|
108
|
+
const frame = {
|
|
109
|
+
body: concatBytes(this.kept),
|
|
110
|
+
bodyBytes: this.bodyBytes,
|
|
111
|
+
marker,
|
|
112
|
+
};
|
|
113
|
+
this.kept.length = 0;
|
|
114
|
+
this.keptBytes = 0;
|
|
115
|
+
this.bodyBytes = 0;
|
|
116
|
+
return frame;
|
|
117
|
+
}
|
|
118
|
+
appendBody(bytes) {
|
|
119
|
+
this.bodyBytes += bytes.length;
|
|
120
|
+
const room = this.maxBytes - this.keptBytes;
|
|
121
|
+
if (room <= 0 || bytes.length === 0) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const kept = bytes.length <= room ? bytes : bytes.subarray(0, room);
|
|
125
|
+
this.kept.push(kept);
|
|
126
|
+
this.keptBytes += kept.length;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=_response-reader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_response-reader.js","sourceRoot":"","sources":["../src/_response-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,mEAAmE;AACnE,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAC7B,MAAM,GAAG,GAAG,IAAI,CAAC;AACjB,MAAM,GAAG,GAAG,IAAI,CAAC;AACjB,MAAM,eAAe,GAAG,IAAI,WAAW,CAAC,QAAQ,CAAC,CAAC;AAYlD;;;;;;;;;GASG;AACH,MAAM,OAAO,eAAe;IAeT,QAAQ;IACR,QAAQ;IAfT,IAAI,GAAiB,EAAE,CAAC;IACjC,SAAS,GAAG,CAAC,CAAC;IACd,SAAS,GAAG,CAAC,CAAC;IACL,IAAI,GAAG,IAAI,UAAU,CAAC,iBAAiB,GAAG,CAAC,CAAC,CAAC;IACtD,UAAU,GAAG,CAAC,CAAC;IACvB,yEAAyE;IACjE,WAAW,GAAG,KAAK,CAAC;IAE5B;;;;OAIG;IACH,YACkB,QAAgB,EAChB,QAAmC;wBADnC,QAAQ;wBACR,QAAQ;IACvB,CAAC;IAEJ;;;;;OAKG;IACH,IAAI,CAAC,KAAiB;QACrB,MAAM,MAAM,GAAoB,EAAE,CAAC;QACnC,IAAI,QAAQ,GAAG,CAAC,CAAC;QAEjB,OAAO,QAAQ,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;YAChC,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;YAC7C,MAAM,GAAG,GAAG,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC;YACxD,MAAM,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;YAC9C,QAAQ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