@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,104 @@
|
|
|
1
|
+
import { type ProcessExit } from "@ac-kit/node";
|
|
2
|
+
import { type ExiftoolVersion } from "./version.js";
|
|
3
|
+
/** How one exiftool process is started and bounded. */
|
|
4
|
+
export type WorkerConfig = {
|
|
5
|
+
readonly command: string;
|
|
6
|
+
/** Everything before `-stay_open True -@ -`. */
|
|
7
|
+
readonly leadingArgs: readonly string[];
|
|
8
|
+
readonly cwd: string | null;
|
|
9
|
+
readonly env: NodeJS.ProcessEnv | null;
|
|
10
|
+
readonly maxResponseBytes: number;
|
|
11
|
+
readonly maxStderrBytes: number;
|
|
12
|
+
readonly graceMs: number;
|
|
13
|
+
};
|
|
14
|
+
/** What exiftool answered to one command. */
|
|
15
|
+
export type WorkerResponse = {
|
|
16
|
+
/** Stdout before `{ready<id>}`, at most `maxResponseBytes`. */
|
|
17
|
+
readonly stdout: Uint8Array<ArrayBuffer>;
|
|
18
|
+
/** How many stdout bytes the command produced, kept or not. */
|
|
19
|
+
readonly stdoutBytes: number;
|
|
20
|
+
/** Stderr before the end marker, at most `maxStderrBytes`, decoded. */
|
|
21
|
+
readonly stderr: string;
|
|
22
|
+
/** Exiftool's exit status for the command. */
|
|
23
|
+
readonly status: number;
|
|
24
|
+
};
|
|
25
|
+
/** @returns How many exiftool processes of any pool are alive (for tests). */
|
|
26
|
+
export declare function liveProcessCount(): number;
|
|
27
|
+
/**
|
|
28
|
+
* One `exiftool -stay_open True -@ -` process running one command at a time.
|
|
29
|
+
*
|
|
30
|
+
* The only file that touches a process. Each command is one write of argument
|
|
31
|
+
* lines ending in `-echo4 {<nonce>:<id>:${status}}` and `-execute<id>`; the
|
|
32
|
+
* response is complete when stdout has printed `{ready<id>}` and stderr the
|
|
33
|
+
* echo line. The nonce, 16 random hex digits, keeps a crafted file name in an
|
|
34
|
+
* error message from faking the stderr marker; `-json` escapes line breaks, so
|
|
35
|
+
* no value can fake the stdout one.
|
|
36
|
+
*/
|
|
37
|
+
export declare class ExiftoolWorker {
|
|
38
|
+
private readonly handle;
|
|
39
|
+
private readonly config;
|
|
40
|
+
/** Set by the handshake; {@link start} returns only once it is. */
|
|
41
|
+
version: ExiftoolVersion;
|
|
42
|
+
/** Commands sent, the handshake excluded. */
|
|
43
|
+
requestCount: number;
|
|
44
|
+
private readonly nonce;
|
|
45
|
+
private readonly stderrMarker;
|
|
46
|
+
private readonly stdoutScanner;
|
|
47
|
+
private readonly stderrScanner;
|
|
48
|
+
private readonly writer;
|
|
49
|
+
private nextId;
|
|
50
|
+
private pending;
|
|
51
|
+
private exitValue;
|
|
52
|
+
private closing;
|
|
53
|
+
private readonly exitListeners;
|
|
54
|
+
/** Resolves once the process has exited and its streams are drained. */
|
|
55
|
+
readonly exited: Promise<ProcessExit>;
|
|
56
|
+
private constructor();
|
|
57
|
+
/**
|
|
58
|
+
* Spawns a process and checks its version with `-ver`.
|
|
59
|
+
*
|
|
60
|
+
* @param signal Aborting terminates the process and rejects with its reason.
|
|
61
|
+
* @throws What spawning throws (`ENOENT` and the like).
|
|
62
|
+
* @throws {ExiftoolVersionError} Below {@link EXIFTOOL_MINIMUM_VERSION}.
|
|
63
|
+
* @throws {ExiftoolOutputError} When `-ver` printed no version.
|
|
64
|
+
* @throws {ExiftoolProcessError} When the process died first.
|
|
65
|
+
*/
|
|
66
|
+
static start(config: WorkerConfig, signal?: AbortSignal | null): Promise<ExiftoolWorker>;
|
|
67
|
+
get pid(): number | undefined;
|
|
68
|
+
get busy(): boolean;
|
|
69
|
+
/** The process exited, or is being closed or terminated. */
|
|
70
|
+
get retired(): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* @param listener Called once, after the process has exited (in a microtask
|
|
73
|
+
* when it already has).
|
|
74
|
+
*/
|
|
75
|
+
onExited(listener: (worker: ExiftoolWorker) => void): void;
|
|
76
|
+
/**
|
|
77
|
+
* Runs one read command.
|
|
78
|
+
*
|
|
79
|
+
* @param before Encoded argument lines before the end-marker echo.
|
|
80
|
+
* @param after Encoded lines after it (the file).
|
|
81
|
+
* @throws {ExiftoolProcessError} When the process exits or its stdin breaks
|
|
82
|
+
* before both markers arrived.
|
|
83
|
+
* @throws {ExiftoolOutputError} For a marker of another command; the process
|
|
84
|
+
* is terminated.
|
|
85
|
+
*/
|
|
86
|
+
run(before: readonly string[], after: readonly string[]): Promise<WorkerResponse>;
|
|
87
|
+
/**
|
|
88
|
+
* Asks exiftool to exit (`-stay_open False`), closes its stdin, and
|
|
89
|
+
* terminates it after the grace period. Idempotent.
|
|
90
|
+
*/
|
|
91
|
+
close(): Promise<void>;
|
|
92
|
+
/** SIGTERM, then SIGKILL after the grace period. Idempotent. */
|
|
93
|
+
terminate(): Promise<void>;
|
|
94
|
+
private send;
|
|
95
|
+
private closeGracefully;
|
|
96
|
+
private pump;
|
|
97
|
+
private onStdoutFrame;
|
|
98
|
+
private onStderrFrame;
|
|
99
|
+
private settleIfComplete;
|
|
100
|
+
private protocolError;
|
|
101
|
+
private fail;
|
|
102
|
+
private onExit;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=_worker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_worker.d.ts","sourceRoot":"","sources":["../src/_worker.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,WAAW,EAAkB,MAAM,cAAc,CAAC;AAUhE,OAAO,EAGN,KAAK,eAAe,EAEpB,MAAM,cAAc,CAAC;AAEtB,uDAAu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CAAK;IACnB,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,SAAS,CAA4B;IAC7C,OAAO,CAAC,OAAO,CAA8B;IAC7C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA4C;IAE1E,wEAAwE;IACxE,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IAEtC,OAAO,eAoCN;IAED;;;;;;;;OAQG;IACH,OAAa,KAAK,CACjB,MAAM,EAAE,YAAY,EACpB,MAAM,CAAC,EAAE,WAAW,GAAG,IAAI,GACzB,OAAO,CAAC,cAAc,CAAC,CAmCzB;IAED,IAAI,GAAG,IAAI,MAAM,GAAG,SAAS,CAE5B;IAED,IAAI,IAAI,IAAI,OAAO,CAElB;IAED,4DAA4D;IAC5D,IAAI,OAAO,IAAI,OAAO,CAErB;IAED;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,GAAG,IAAI,CAMzD;IAED;;;;;;;;;OASG;IACH,GAAG,CACF,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,KAAK,EAAE,SAAS,MAAM,EAAE,GACtB,OAAO,CAAC,cAAc,CAAC,CAGzB;IAED;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAGrB;IAED,gEAAgE;IAC1D,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC,CAG/B;IAED,OAAO,CAAC,IAAI;YA2CE,eAAe;YAwBf,IAAI;IAgBlB,OAAO,CAAC,aAAa;IAUrB,OAAO,CAAC,aAAa;IAWrB,OAAO,CAAC,gBAAgB;IAcxB,OAAO,CAAC,aAAa;IAKrB,OAAO,CAAC,IAAI;IASZ,OAAO,CAAC,MAAM;CAYd"}
|
package/dist/_worker.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { setLongTimeout } from "@ac-kit/core";
|
|
3
|
+
import { SpawnedProcess } from "@ac-kit/node";
|
|
4
|
+
import { encodeArgfileCommand } from "./_argfile.js";
|
|
5
|
+
import { ResponseScanner } from "./_response-reader.js";
|
|
6
|
+
import { ExiftoolOutputError, ExiftoolProcessError, ExiftoolVersionError, } from "./errors.js";
|
|
7
|
+
import { compareExiftoolVersions, EXIFTOOL_MINIMUM_VERSION, parseExiftoolVersion, } from "./version.js";
|
|
8
|
+
const STDOUT_MARKER_ = /^\{ready\d*\}$/;
|
|
9
|
+
// Processes still alive when Node exits: exiftool never exits on end of input
|
|
10
|
+
// (it polls stdin forever), so a parent leaving without close() would orphan
|
|
11
|
+
// them. Children are registered synchronously at spawn, before anything can
|
|
12
|
+
// await, and the one listener for every pool is present only while one lives.
|
|
13
|
+
const liveChildren_ = new Set();
|
|
14
|
+
const killLiveChildren_ = () => {
|
|
15
|
+
for (const child of liveChildren_) {
|
|
16
|
+
child.kill("SIGKILL");
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
function trackChild_(child) {
|
|
20
|
+
if (liveChildren_.size === 0) {
|
|
21
|
+
process.on("exit", killLiveChildren_);
|
|
22
|
+
}
|
|
23
|
+
liveChildren_.add(child);
|
|
24
|
+
}
|
|
25
|
+
function untrackChild_(child) {
|
|
26
|
+
if (!liveChildren_.delete(child)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (liveChildren_.size === 0) {
|
|
30
|
+
process.off("exit", killLiveChildren_);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** @returns How many exiftool processes of any pool are alive (for tests). */
|
|
34
|
+
export function liveProcessCount() {
|
|
35
|
+
return liveChildren_.size;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Spawns a child and registers it before returning, so that a `process.exit` in
|
|
39
|
+
* the same tick still kills it. `startProcess` would hand the child over only
|
|
40
|
+
* after its `spawn` event.
|
|
41
|
+
*/
|
|
42
|
+
async function spawnTracked_(config) {
|
|
43
|
+
const startedAt = performance.now();
|
|
44
|
+
const child = spawn(config.command, [...config.leadingArgs, "-stay_open", "True", "-@", "-"], {
|
|
45
|
+
cwd: config.cwd ?? undefined,
|
|
46
|
+
env: config.env ?? undefined,
|
|
47
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
48
|
+
});
|
|
49
|
+
trackChild_(child);
|
|
50
|
+
child.once("exit", () => untrackChild_(child));
|
|
51
|
+
// Attached before anything can await, as startProcess does.
|
|
52
|
+
child.on("error", () => { });
|
|
53
|
+
try {
|
|
54
|
+
await new Promise((resolve, reject) => {
|
|
55
|
+
child.once("error", reject);
|
|
56
|
+
child.once("spawn", () => {
|
|
57
|
+
child.removeListener("error", reject);
|
|
58
|
+
resolve();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
untrackChild_(child);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
return new SpawnedProcess(child, startedAt, {
|
|
67
|
+
graceMs: config.graceMs,
|
|
68
|
+
exposeStdin: true,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* One `exiftool -stay_open True -@ -` process running one command at a time.
|
|
73
|
+
*
|
|
74
|
+
* The only file that touches a process. Each command is one write of argument
|
|
75
|
+
* lines ending in `-echo4 {<nonce>:<id>:${status}}` and `-execute<id>`; the
|
|
76
|
+
* response is complete when stdout has printed `{ready<id>}` and stderr the
|
|
77
|
+
* echo line. The nonce, 16 random hex digits, keeps a crafted file name in an
|
|
78
|
+
* error message from faking the stderr marker; `-json` escapes line breaks, so
|
|
79
|
+
* no value can fake the stdout one.
|
|
80
|
+
*/
|
|
81
|
+
export class ExiftoolWorker {
|
|
82
|
+
handle;
|
|
83
|
+
config;
|
|
84
|
+
/** Set by the handshake; {@link start} returns only once it is. */
|
|
85
|
+
version = EXIFTOOL_MINIMUM_VERSION;
|
|
86
|
+
/** Commands sent, the handshake excluded. */
|
|
87
|
+
requestCount = 0;
|
|
88
|
+
nonce = crypto.getRandomValues(new Uint8Array(8)).toHex();
|
|
89
|
+
stderrMarker;
|
|
90
|
+
stdoutScanner;
|
|
91
|
+
stderrScanner;
|
|
92
|
+
writer;
|
|
93
|
+
nextId = 0;
|
|
94
|
+
pending = null;
|
|
95
|
+
exitValue = null;
|
|
96
|
+
closing = null;
|
|
97
|
+
exitListeners = [];
|
|
98
|
+
/** Resolves once the process has exited and its streams are drained. */
|
|
99
|
+
exited;
|
|
100
|
+
constructor(handle, config) {
|
|
101
|
+
this.handle = handle;
|
|
102
|
+
this.config = config;
|
|
103
|
+
this.stderrMarker = new RegExp(`^\\{${this.nonce}:(\\d+):(-?\\d+)\\}$`);
|
|
104
|
+
this.stdoutScanner = new ResponseScanner(config.maxResponseBytes, (line) => STDOUT_MARKER_.test(line));
|
|
105
|
+
this.stderrScanner = new ResponseScanner(config.maxStderrBytes, (line) => this.stderrMarker.test(line));
|
|
106
|
+
const stdin = handle.stdin;
|
|
107
|
+
if (!stdin || !handle.stdout || !handle.stderr) {
|
|
108
|
+
void handle.terminate();
|
|
109
|
+
throw new ExiftoolProcessError(null, "", "The exiftool process has no piped stdin, stdout or stderr");
|
|
110
|
+
}
|
|
111
|
+
this.writer = stdin.getWriter();
|
|
112
|
+
// A failed write is reported through the request that made it.
|
|
113
|
+
this.writer.closed.catch(() => { });
|
|
114
|
+
const stdoutDone = this.pump(handle.stdout, this.stdoutScanner, (frame) => this.onStdoutFrame(frame));
|
|
115
|
+
const stderrDone = this.pump(handle.stderr, this.stderrScanner, (frame) => this.onStderrFrame(frame));
|
|
116
|
+
this.exited = handle.waitForExit().then(async (exit) => {
|
|
117
|
+
await Promise.allSettled([stdoutDone, stderrDone]);
|
|
118
|
+
this.onExit(exit);
|
|
119
|
+
return exit;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Spawns a process and checks its version with `-ver`.
|
|
124
|
+
*
|
|
125
|
+
* @param signal Aborting terminates the process and rejects with its reason.
|
|
126
|
+
* @throws What spawning throws (`ENOENT` and the like).
|
|
127
|
+
* @throws {ExiftoolVersionError} Below {@link EXIFTOOL_MINIMUM_VERSION}.
|
|
128
|
+
* @throws {ExiftoolOutputError} When `-ver` printed no version.
|
|
129
|
+
* @throws {ExiftoolProcessError} When the process died first.
|
|
130
|
+
*/
|
|
131
|
+
static async start(config, signal) {
|
|
132
|
+
signal?.throwIfAborted();
|
|
133
|
+
const handle = await spawnTracked_(config);
|
|
134
|
+
const worker = new ExiftoolWorker(handle, config);
|
|
135
|
+
const onAbort = () => {
|
|
136
|
+
void worker.terminate();
|
|
137
|
+
};
|
|
138
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
139
|
+
try {
|
|
140
|
+
if (signal?.aborted === true) {
|
|
141
|
+
onAbort();
|
|
142
|
+
}
|
|
143
|
+
const response = await worker.send(["-ver"], []);
|
|
144
|
+
signal?.throwIfAborted();
|
|
145
|
+
const version = parseExiftoolVersion(new TextDecoder().decode(response.stdout));
|
|
146
|
+
if (version === null) {
|
|
147
|
+
throw new ExiftoolOutputError("exiftool -ver printed no version number");
|
|
148
|
+
}
|
|
149
|
+
if (compareExiftoolVersions(version, EXIFTOOL_MINIMUM_VERSION) < 0) {
|
|
150
|
+
throw new ExiftoolVersionError(version, EXIFTOOL_MINIMUM_VERSION);
|
|
151
|
+
}
|
|
152
|
+
worker.version = version;
|
|
153
|
+
return worker;
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
await worker.terminate();
|
|
157
|
+
signal?.throwIfAborted();
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
signal?.removeEventListener("abort", onAbort);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
get pid() {
|
|
165
|
+
return this.handle.pid;
|
|
166
|
+
}
|
|
167
|
+
get busy() {
|
|
168
|
+
return this.pending !== null;
|
|
169
|
+
}
|
|
170
|
+
/** The process exited, or is being closed or terminated. */
|
|
171
|
+
get retired() {
|
|
172
|
+
return this.exitValue !== null || this.closing !== null;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* @param listener Called once, after the process has exited (in a microtask
|
|
176
|
+
* when it already has).
|
|
177
|
+
*/
|
|
178
|
+
onExited(listener) {
|
|
179
|
+
if (this.exitValue !== null) {
|
|
180
|
+
queueMicrotask(() => listener(this));
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
this.exitListeners.push(listener);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Runs one read command.
|
|
188
|
+
*
|
|
189
|
+
* @param before Encoded argument lines before the end-marker echo.
|
|
190
|
+
* @param after Encoded lines after it (the file).
|
|
191
|
+
* @throws {ExiftoolProcessError} When the process exits or its stdin breaks
|
|
192
|
+
* before both markers arrived.
|
|
193
|
+
* @throws {ExiftoolOutputError} For a marker of another command; the process
|
|
194
|
+
* is terminated.
|
|
195
|
+
*/
|
|
196
|
+
run(before, after) {
|
|
197
|
+
this.requestCount++;
|
|
198
|
+
return this.send(before, after);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Asks exiftool to exit (`-stay_open False`), closes its stdin, and
|
|
202
|
+
* terminates it after the grace period. Idempotent.
|
|
203
|
+
*/
|
|
204
|
+
close() {
|
|
205
|
+
this.closing ??= this.closeGracefully();
|
|
206
|
+
return this.closing;
|
|
207
|
+
}
|
|
208
|
+
/** SIGTERM, then SIGKILL after the grace period. Idempotent. */
|
|
209
|
+
async terminate() {
|
|
210
|
+
this.closing ??= this.exited.then(() => { });
|
|
211
|
+
await this.handle.terminate();
|
|
212
|
+
}
|
|
213
|
+
send(before, after) {
|
|
214
|
+
if (this.pending !== null) {
|
|
215
|
+
return Promise.reject(new Error("exiftool worker is busy"));
|
|
216
|
+
}
|
|
217
|
+
if (this.exitValue !== null || this.closing !== null) {
|
|
218
|
+
return Promise.reject(new ExiftoolProcessError(this.exitValue, "", "The exiftool process is no longer running"));
|
|
219
|
+
}
|
|
220
|
+
const id = this.nextId++;
|
|
221
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
222
|
+
this.pending = { id, stdout: null, stderr: null, resolve, reject };
|
|
223
|
+
const command = encodeArgfileCommand([
|
|
224
|
+
...before,
|
|
225
|
+
"-echo4",
|
|
226
|
+
`{${this.nonce}:${id}:\${status}}`,
|
|
227
|
+
...after,
|
|
228
|
+
`-execute${id}`,
|
|
229
|
+
]);
|
|
230
|
+
this.writer.write(command).catch((error) => {
|
|
231
|
+
this.fail(new ExiftoolProcessError(this.exitValue, "", "Writing to exiftool failed", { cause: error }));
|
|
232
|
+
});
|
|
233
|
+
return promise;
|
|
234
|
+
}
|
|
235
|
+
async closeGracefully() {
|
|
236
|
+
if (this.exitValue === null) {
|
|
237
|
+
try {
|
|
238
|
+
await this.writer.write(encodeArgfileCommand(["-stay_open", "False"]));
|
|
239
|
+
await this.writer.close();
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
// Already gone; the exit below settles it.
|
|
243
|
+
}
|
|
244
|
+
const grace = new AbortController();
|
|
245
|
+
const expired = new Promise((resolve) => {
|
|
246
|
+
setLongTimeout(() => resolve(false), this.config.graceMs, grace.signal);
|
|
247
|
+
});
|
|
248
|
+
const exitedInTime = await Promise.race([
|
|
249
|
+
this.handle.waitForExit().then(() => true),
|
|
250
|
+
expired,
|
|
251
|
+
]);
|
|
252
|
+
grace.abort();
|
|
253
|
+
if (!exitedInTime) {
|
|
254
|
+
await this.handle.terminate();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
await this.exited;
|
|
258
|
+
}
|
|
259
|
+
async pump(stream, scanner, onFrame) {
|
|
260
|
+
try {
|
|
261
|
+
for await (const chunk of stream) {
|
|
262
|
+
for (const frame of scanner.push(chunk)) {
|
|
263
|
+
onFrame(frame);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
// A broken pipe ends the process too; its exit reports the failure.
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
onStdoutFrame(frame) {
|
|
272
|
+
const pending = this.pending;
|
|
273
|
+
if (pending === null || frame.marker !== `{ready${pending.id}}`) {
|
|
274
|
+
this.protocolError(`unexpected end marker ${frame.marker}`);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
pending.stdout = frame;
|
|
278
|
+
this.settleIfComplete(pending);
|
|
279
|
+
}
|
|
280
|
+
onStderrFrame(frame) {
|
|
281
|
+
const pending = this.pending;
|
|
282
|
+
const match = this.stderrMarker.exec(frame.marker);
|
|
283
|
+
if (pending === null || match === null || Number(match[1]) !== pending.id) {
|
|
284
|
+
this.protocolError("unexpected stderr end marker");
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
pending.stderr = frame;
|
|
288
|
+
this.settleIfComplete(pending);
|
|
289
|
+
}
|
|
290
|
+
settleIfComplete(pending) {
|
|
291
|
+
if (pending.stdout === null || pending.stderr === null) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const match = this.stderrMarker.exec(pending.stderr.marker);
|
|
295
|
+
this.pending = null;
|
|
296
|
+
pending.resolve({
|
|
297
|
+
stdout: pending.stdout.body,
|
|
298
|
+
stdoutBytes: pending.stdout.bodyBytes,
|
|
299
|
+
stderr: decodeStderr_(pending.stderr.body),
|
|
300
|
+
status: Number(match[2]),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
protocolError(message) {
|
|
304
|
+
this.fail(new ExiftoolOutputError(`exiftool protocol error: ${message}`));
|
|
305
|
+
void this.terminate();
|
|
306
|
+
}
|
|
307
|
+
fail(error) {
|
|
308
|
+
const pending = this.pending;
|
|
309
|
+
if (pending === null) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
this.pending = null;
|
|
313
|
+
pending.reject(error);
|
|
314
|
+
}
|
|
315
|
+
onExit(exit) {
|
|
316
|
+
this.exitValue = exit;
|
|
317
|
+
this.fail(new ExiftoolProcessError(exit, decodeStderr_(this.stderrScanner.pendingBody())));
|
|
318
|
+
for (const listener of this.exitListeners.splice(0)) {
|
|
319
|
+
listener(this);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
/** Decodes kept stderr, dropping a character cut by the byte limit. */
|
|
324
|
+
function decodeStderr_(bytes) {
|
|
325
|
+
return new TextDecoder().decode(bytes, { stream: true });
|
|
326
|
+
}
|
|
327
|
+
//# sourceMappingURL=_worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_worker.js","sourceRoot":"","sources":["../src/_worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE9D,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAoB,cAAc,EAAE,MAAM,cAAc,CAAC;AAEhE,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAErD,OAAO,EAAsB,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC5E,OAAO,EACN,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EACN,uBAAuB,EACvB,wBAAwB,EAExB,oBAAoB,GACpB,MAAM,cAAc,CAAC;AAkCtB,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAExC,8EAA8E;AAC9E,6EAA6E;AAC7E,4EAA4E;AAC5E,8EAA8E;AAC9E,MAAM,aAAa,GAAG,IAAI,GAAG,EAAgB,CAAC;AAC9C,MAAM,iBAAiB,GAAG,GAAS,EAAE;IACpC,KAAK,MAAM,KAAK,IAAI,aAAa,EAAE,CAAC;QACnC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACvB,CAAC;AACF,CAAC,CAAC;AAEF,SAAS,WAAW,CAAC,KAAmB;IACvC,IAAI,aAAa,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QAC9B,OAAO,CAAC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
|
|
1
|
+
/** A date or date-time exiftool printed, as `Temporal` values. */
|
|
2
|
+
export type ExiftoolDateTime = {
|
|
3
|
+
readonly kind: "date-time";
|
|
4
|
+
/** The wall-clock reading, as written in the file. */
|
|
5
|
+
readonly value: Temporal.PlainDateTime;
|
|
6
|
+
/** The UTC offset as `±HH:MM`, `null` when the text carried none. */
|
|
7
|
+
readonly offset: string | null;
|
|
8
|
+
/** The instant, only when an offset was present. */
|
|
9
|
+
readonly instant: Temporal.Instant | null;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: "date";
|
|
12
|
+
readonly value: Temporal.PlainDate;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Parses exiftool's date text: `YYYY:MM:DD HH:MM:SS[.fraction][Z|±HH:MM|±HHMM]`
|
|
16
|
+
* or `YYYY:MM:DD`.
|
|
17
|
+
*
|
|
18
|
+
* Fraction digits past nanoseconds are dropped. QuickTime's ambiguity between
|
|
19
|
+
* UTC and local time stays with the caller (`-api QuickTimeUTC`). Needs the
|
|
20
|
+
* `Temporal` global (Node 26, or a polyfill).
|
|
21
|
+
*
|
|
22
|
+
* @param text One value, such as `ExifIFD:DateTimeOriginal`.
|
|
23
|
+
* @returns The date or date-time, or `null` for anything else: zero dates
|
|
24
|
+
* (`0000:00:00 00:00:00`), impossible calendar values (`2023:02:30`), a leap
|
|
25
|
+
* second, an offset beyond ±23:59, other formats. O(length).
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseExiftoolDateTime(text: string): ExiftoolDateTime | null;
|
|
28
|
+
//# sourceMappingURL=date-time.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-time.d.ts","sourceRoot":"","sources":["../src/date-time.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,MAAM,MAAM,gBAAgB,GACzB;IACA,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAC3B,sDAAsD;IACtD,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,aAAa,CAAC;IACvC,qEAAqE;IACrE,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,oDAAoD;IACpD,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,OAAO,GAAG,IAAI,CAAC;CACzC,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,SAAS,CAAA;CAAE,CAAC;AAKjE;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAkD3E"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
const DATE_TIME_ = /^(\d{4}):(\d{2}):(\d{2})(?: (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
2
|
+
/**
|
|
3
|
+
* Parses exiftool's date text: `YYYY:MM:DD HH:MM:SS[.fraction][Z|±HH:MM|±HHMM]`
|
|
4
|
+
* or `YYYY:MM:DD`.
|
|
5
|
+
*
|
|
6
|
+
* Fraction digits past nanoseconds are dropped. QuickTime's ambiguity between
|
|
7
|
+
* UTC and local time stays with the caller (`-api QuickTimeUTC`). Needs the
|
|
8
|
+
* `Temporal` global (Node 26, or a polyfill).
|
|
9
|
+
*
|
|
10
|
+
* @param text One value, such as `ExifIFD:DateTimeOriginal`.
|
|
11
|
+
* @returns The date or date-time, or `null` for anything else: zero dates
|
|
12
|
+
* (`0000:00:00 00:00:00`), impossible calendar values (`2023:02:30`), a leap
|
|
13
|
+
* second, an offset beyond ±23:59, other formats. O(length).
|
|
14
|
+
*/
|
|
15
|
+
export function parseExiftoolDateTime(text) {
|
|
16
|
+
const match = DATE_TIME_.exec(text);
|
|
17
|
+
if (match === null) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
const date = {
|
|
21
|
+
year: Number(match[1]),
|
|
22
|
+
month: Number(match[2]),
|
|
23
|
+
day: Number(match[3]),
|
|
24
|
+
};
|
|
25
|
+
try {
|
|
26
|
+
if (match[4] === undefined) {
|
|
27
|
+
return {
|
|
28
|
+
kind: "date",
|
|
29
|
+
value: Temporal.PlainDate.from(date, { overflow: "reject" }),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const fraction = (match[7] ?? "").slice(0, 9).padEnd(9, "0");
|
|
33
|
+
const value = Temporal.PlainDateTime.from({
|
|
34
|
+
...date,
|
|
35
|
+
hour: Number(match[4]),
|
|
36
|
+
minute: Number(match[5]),
|
|
37
|
+
second: Number(match[6]),
|
|
38
|
+
millisecond: Number(fraction.slice(0, 3)),
|
|
39
|
+
microsecond: Number(fraction.slice(3, 6)),
|
|
40
|
+
nanosecond: Number(fraction.slice(6, 9)),
|
|
41
|
+
}, { overflow: "reject" });
|
|
42
|
+
const offset = normalizeOffset_(match[8]);
|
|
43
|
+
if (offset === undefined) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
kind: "date-time",
|
|
48
|
+
value,
|
|
49
|
+
offset,
|
|
50
|
+
instant: offset === null ? null : value.toZonedDateTime(offset).toInstant(),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// Temporal rejects impossible calendar values with a RangeError.
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** @returns `±HH:MM`, `null` for no offset, `undefined` when out of range. */
|
|
59
|
+
function normalizeOffset_(offset) {
|
|
60
|
+
if (offset === undefined) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
if (offset === "Z") {
|
|
64
|
+
return "+00:00";
|
|
65
|
+
}
|
|
66
|
+
const digits = offset.replace(":", "");
|
|
67
|
+
const hours = Number(digits.slice(1, 3));
|
|
68
|
+
const minutes = Number(digits.slice(3, 5));
|
|
69
|
+
if (hours > 23 || minutes > 59) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
return `${digits[0]}${digits.slice(1, 3)}:${digits.slice(3, 5)}`;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=date-time.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-time.js","sourceRoot":"","sources":["../src/date-time.ts"],"names":[],"mappings":"AAaA,MAAM,UAAU,GACf,yFAAyF,CAAC;AAE3F;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY;IACjD,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,IAAI,GAAG;QACZ,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACtB,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACvB,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KACrB,CAAC;IAEF,IAAI,CAAC;QACJ,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC;YAC5B,OAAO;gBACN,IAAI,EAAE,MAAM;gBACZ,KAAK,EAAE,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC;aAC5D,CAAC;QACH,CAAC;QAED,MAAM,QAAQ,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAC7D,MAAM,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,IAAI,CACxC;YACC,GAAG,IAAI;YACP,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACtB,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