@enslo/sd-metadata-lite 1.1.0 → 1.2.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/CHANGELOG.md +15 -0
- package/README.ja.md +4 -4
- package/README.md +4 -4
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.global.js +1 -1
- package/dist/index.js +483 -405
- package/dist/index.js.map +1 -1
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -1,456 +1,534 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
//#region src/extract.ts
|
|
2
|
+
const CT = "class_type";
|
|
3
|
+
const PARAMS = "parameters";
|
|
4
|
+
const EASY_DIFF_KEY = "use_stable_diffusion_model";
|
|
5
|
+
/**
|
|
6
|
+
* Channel tool-specific entry keys into `entries.parameters`.
|
|
7
|
+
*
|
|
8
|
+
* Each key string appears exactly once in this loop to minimize IIFE size
|
|
9
|
+
* (esbuild cannot mangle object property names).
|
|
10
|
+
*
|
|
11
|
+
* Matched keys and their tools:
|
|
12
|
+
* - "extraMetadata" → Civitai (JPEG UserComment JSON)
|
|
13
|
+
* - "invokeai_metadata" → InvokeAI (PNG tEXt / JPEG UserComment JSON)
|
|
14
|
+
* - "generation_data" → TensorArt (JPEG UserComment JSON)
|
|
15
|
+
*
|
|
16
|
+
* Returns a NEW Entries with the matched value routed to `parameters`.
|
|
17
|
+
* If no key matches, returns the original `entries` reference unchanged
|
|
18
|
+
* (enables identity check with `!==`).
|
|
19
|
+
*/
|
|
5
20
|
function channelKnownKeys(src, entries) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
for (const key of [
|
|
22
|
+
"extraMetadata",
|
|
23
|
+
"invokeai_metadata",
|
|
24
|
+
"generation_data"
|
|
25
|
+
]) if (src[key]) {
|
|
26
|
+
const result = {
|
|
27
|
+
...entries,
|
|
28
|
+
[PARAMS]: entries[PARAMS] ?? stringifyValue(src[key]).replace(/\0+$/, "")
|
|
29
|
+
};
|
|
30
|
+
if (src.prompt) result.prompt ??= stringifyValue(src.prompt);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
return entries;
|
|
18
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Normalize heterogeneous entry formats into a uniform shape.
|
|
37
|
+
*
|
|
38
|
+
* Pure function — returns a new Entries without mutating the input.
|
|
39
|
+
* Only identifies WHERE data lives and channels it to `entries.parameters`.
|
|
40
|
+
* All actual parsing/extraction is deferred to extract().
|
|
41
|
+
*
|
|
42
|
+
* Entry points by image format:
|
|
43
|
+
* PNG → entries already has named keys (parameters, prompt, Comment, etc.)
|
|
44
|
+
* JPEG → metadata is in EXIF UserComment (plain text or JSON blob)
|
|
45
|
+
* WebP → same as JPEG (EXIF-based)
|
|
46
|
+
*/
|
|
19
47
|
function normalize(entries) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
48
|
+
if (entries.Software?.startsWith("NovelAI")) return {
|
|
49
|
+
...entries,
|
|
50
|
+
[PARAMS]: entries[PARAMS] ?? entries.Comment
|
|
51
|
+
};
|
|
52
|
+
const uc = entries.UserComment;
|
|
53
|
+
if (!uc) return channelKnownKeys(entries, entries);
|
|
54
|
+
if (!uc.startsWith("{")) {
|
|
55
|
+
if (uc.includes("Steps:")) return {
|
|
56
|
+
...entries,
|
|
57
|
+
[PARAMS]: entries[PARAMS] ?? uc
|
|
58
|
+
};
|
|
59
|
+
return entries;
|
|
60
|
+
}
|
|
61
|
+
const obj = parseJson(uc);
|
|
62
|
+
if (!obj) return entries;
|
|
63
|
+
const channeled = channelKnownKeys(obj, entries);
|
|
64
|
+
if (channeled !== entries) return channeled;
|
|
65
|
+
if ("prompt" in obj && "workflow" in obj) return {
|
|
66
|
+
...entries,
|
|
67
|
+
prompt: entries.prompt ?? stringifyValue(obj.prompt)
|
|
68
|
+
};
|
|
69
|
+
if (PARAMS in obj) return {
|
|
70
|
+
...entries,
|
|
71
|
+
[PARAMS]: entries[PARAMS] ?? stringifyValue(obj[PARAMS])
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
...entries,
|
|
75
|
+
[PARAMS]: entries[PARAMS] ?? uc
|
|
76
|
+
};
|
|
41
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Main entry point: detect the originating tool, extract metadata from
|
|
80
|
+
* normalized entries, and return human-readable A1111-format text.
|
|
81
|
+
*/
|
|
42
82
|
function extract(rawEntries) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (entries[EASY_DIFF_KEY]) return buildA1111(extractCommon(entries));
|
|
73
|
-
if (prompt?.includes(CT)) return buildA1111(flatScan(prompt));
|
|
74
|
-
return params ?? entries.UserComment ?? entries.Comment ?? entries.ImageDescription ?? "";
|
|
83
|
+
const entries = normalize(rawEntries);
|
|
84
|
+
const params = entries[PARAMS];
|
|
85
|
+
const prompt = entries.prompt ?? entries.Prompt;
|
|
86
|
+
if (params?.includes("sui_image_params")) {
|
|
87
|
+
const obj = parseJson(params);
|
|
88
|
+
return buildA1111(extractCommon(obj.sui_image_params ?? obj));
|
|
89
|
+
}
|
|
90
|
+
if (params && !params.startsWith("{")) return params;
|
|
91
|
+
if (params) {
|
|
92
|
+
const raw = parseJson(params);
|
|
93
|
+
const obj = typeof raw === "string" ? parseJson(raw) : raw;
|
|
94
|
+
if (obj) {
|
|
95
|
+
const result = tryJsonTool(obj, params);
|
|
96
|
+
if (result) return result;
|
|
97
|
+
if (params.includes(CT)) return buildA1111(flatScan(params));
|
|
98
|
+
const base = extractCommon(obj);
|
|
99
|
+
if (prompt?.includes(CT)) return buildA1111({
|
|
100
|
+
...flatScan(prompt),
|
|
101
|
+
p: base.p,
|
|
102
|
+
n: base.n,
|
|
103
|
+
md: base.md,
|
|
104
|
+
mh: base.mh
|
|
105
|
+
});
|
|
106
|
+
return buildA1111(base);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (entries[EASY_DIFF_KEY]) return buildA1111(extractCommon(entries));
|
|
110
|
+
if (prompt?.includes(CT)) return buildA1111(flatScan(prompt));
|
|
111
|
+
return params ?? entries.UserComment ?? entries.Comment ?? entries.ImageDescription ?? "";
|
|
75
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Attempt to identify specific tools from a parsed JSON object.
|
|
115
|
+
* Returns A1111-format text if a tool is recognized, null otherwise.
|
|
116
|
+
*
|
|
117
|
+
* Detection is ordered from most specific to least specific signatures.
|
|
118
|
+
*/
|
|
76
119
|
function tryJsonTool(obj, text) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
120
|
+
if (asString(obj.software).includes("RuinedFooocus")) return buildA1111(extractCommon(obj));
|
|
121
|
+
if (EASY_DIFF_KEY in obj) return buildA1111(extractCommon(obj));
|
|
122
|
+
if (text.includes("\"Model\"") && text.includes("\"resolution\"")) {
|
|
123
|
+
const [w, h] = parseDimensions(asString(obj.resolution), /(\d+)\s*x\s*(\d+)/);
|
|
124
|
+
return buildA1111({
|
|
125
|
+
...extractCommon(obj),
|
|
126
|
+
w,
|
|
127
|
+
h
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if ("base_model" in obj) {
|
|
131
|
+
const [w, h] = parseDimensions(asString(obj.resolution), /\(\s*(\d+)\s*,\s*(\d+)\s*\)/);
|
|
132
|
+
return buildA1111({
|
|
133
|
+
...extractCommon(obj),
|
|
134
|
+
w,
|
|
135
|
+
h
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if ("noise_schedule" in obj) return buildNovelAI(obj);
|
|
139
|
+
return null;
|
|
96
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Extract GenerationParams from a JSON object using field name fallback chains.
|
|
143
|
+
*
|
|
144
|
+
* Each field tries multiple property names to cover all known JSON-based tools.
|
|
145
|
+
* The fallback order generally goes: most common name → tool-specific aliases.
|
|
146
|
+
*
|
|
147
|
+
* Covered tools: A1111, NovelAI, SwarmUI, InvokeAI, Civitai, TensorArt,
|
|
148
|
+
* Fooocus, RuinedFooocus, EasyDiffusion, HF Space.
|
|
149
|
+
*/
|
|
97
150
|
function extractCommon(obj) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
sa: sa || sc,
|
|
122
|
-
sc: sa ? sc : void 0,
|
|
123
|
-
w: asNumber(obj.width),
|
|
124
|
-
h: asNumber(obj.height),
|
|
125
|
-
// Model name: extensive fallback chain covering all known patterns.
|
|
126
|
-
// "model" (string) → InvokeAI, SwarmUI, Civitai
|
|
127
|
-
// "Model" (capital) → Fooocus
|
|
128
|
-
// "base_model" → HF Space
|
|
129
|
-
// "base_model_name" → Civitai generator
|
|
130
|
-
// "baseModel" (string) → TensorArt (when stored as string)
|
|
131
|
-
// "model.name" → Civitai (nested object)
|
|
132
|
-
// "baseModel.modelFileName" → TensorArt (nested object)
|
|
133
|
-
// "use_stable_diffusion_model" → EasyDiffusion (full path, take filename)
|
|
134
|
-
md: asString(mod) || asString(obj.Model) || asString(obj.base_model) || asString(obj.base_model_name) || asString(base) || asString(mod?.name) || asString(base?.modelFileName) || asString(obj[EASY_DIFF_KEY]).split(/[\\/]/).pop(),
|
|
135
|
-
// Model hash: similar multi-tool fallback chain.
|
|
136
|
-
// "Model hash" → Fooocus
|
|
137
|
-
// "model.hash" → Civitai (nested)
|
|
138
|
-
// "baseModel.hash" → TensorArt (nested)
|
|
139
|
-
mh: asString(obj.base_model_hash) || asString(obj["Model hash"]) || asString(mod?.hash) || asString(base?.hash),
|
|
140
|
-
cs: asNumber(obj.clip_skip ?? obj.clipSkip),
|
|
141
|
-
// Hires/upscaler: use_upscaler (Fooocus, HF Space) → refiner* (SwarmUI)
|
|
142
|
-
hs: asNumber(up?.upscale_by) ?? asNumber(obj.refinerupscale),
|
|
143
|
-
hu: asString(up?.upscale_method) || asString(obj.refinerupscalemethod),
|
|
144
|
-
hd: asNumber(up?.upscaler_strength) ?? asNumber(obj.refinercontrolpercentage),
|
|
145
|
-
ht: asNumber(up?.upscale_steps)
|
|
146
|
-
};
|
|
151
|
+
const sa = asString(obj.sampler ?? obj.sampler_name);
|
|
152
|
+
const sc = asString(obj.scheduler ?? obj.noise_schedule);
|
|
153
|
+
const mod = obj.model;
|
|
154
|
+
const base = obj.baseModel;
|
|
155
|
+
const up = obj.use_upscaler;
|
|
156
|
+
return {
|
|
157
|
+
p: asString(obj.prompt ?? obj.Prompt ?? obj.positive_prompt),
|
|
158
|
+
n: asString(obj.negative_prompt ?? obj.Negative ?? obj.negativeprompt ?? obj.negativePrompt ?? obj.uc),
|
|
159
|
+
se: asNumber(obj.seed),
|
|
160
|
+
st: asNumber(obj.steps ?? obj.num_inference_steps),
|
|
161
|
+
cf: asNumber(obj.cfg ?? obj.cfg_scale ?? obj.cfgScale ?? obj.cfgscale ?? obj.guidance_scale ?? obj.scale),
|
|
162
|
+
sa: sa || sc,
|
|
163
|
+
sc: sa ? sc : void 0,
|
|
164
|
+
w: asNumber(obj.width),
|
|
165
|
+
h: asNumber(obj.height),
|
|
166
|
+
md: asString(mod) || asString(obj.Model) || asString(obj.base_model) || asString(obj.base_model_name) || asString(base) || asString(mod?.name) || asString(base?.modelFileName) || asString(obj[EASY_DIFF_KEY]).split(/[\\/]/).pop(),
|
|
167
|
+
mh: asString(obj.base_model_hash) || asString(obj["Model hash"]) || asString(mod?.hash) || asString(base?.hash),
|
|
168
|
+
cs: asNumber(obj.clip_skip ?? obj.clipSkip),
|
|
169
|
+
hs: asNumber(up?.upscale_by) ?? asNumber(obj.refinerupscale),
|
|
170
|
+
hu: asString(up?.upscale_method) || asString(obj.refinerupscalemethod),
|
|
171
|
+
hd: asNumber(up?.upscaler_strength) ?? asNumber(obj.refinercontrolpercentage),
|
|
172
|
+
ht: asNumber(up?.upscale_steps)
|
|
173
|
+
};
|
|
147
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Build A1111 text from NovelAI JSON metadata.
|
|
177
|
+
*
|
|
178
|
+
* NovelAI v4+ uses a nested prompt structure:
|
|
179
|
+
* { v4_prompt: { caption: { base_caption: "..." } } }
|
|
180
|
+
* Falls back to flat "prompt" / "uc" fields for older versions.
|
|
181
|
+
*/
|
|
148
182
|
function buildNovelAI(obj) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
});
|
|
183
|
+
const v4Caption = obj.v4_prompt?.caption;
|
|
184
|
+
const v4NegCaption = obj.v4_negative_prompt?.caption;
|
|
185
|
+
return buildA1111({
|
|
186
|
+
...extractCommon(obj),
|
|
187
|
+
p: asString(v4Caption?.base_caption) || asString(obj.prompt),
|
|
188
|
+
n: asString(v4NegCaption?.base_caption) || asString(obj.uc)
|
|
189
|
+
});
|
|
157
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Scan a ComfyUI node graph (flat JSON) and extract generation parameters.
|
|
193
|
+
*
|
|
194
|
+
* ComfyUI stores metadata as a dictionary of nodes, each with a "class_type"
|
|
195
|
+
* and "inputs" object. This function iterates all nodes and extracts
|
|
196
|
+
* relevant parameters based on the node's class type.
|
|
197
|
+
*
|
|
198
|
+
* The "first match wins" strategy (??= / ||=) ensures we pick the primary
|
|
199
|
+
* node when multiple nodes of the same type exist (e.g. two KSamplers).
|
|
200
|
+
*/
|
|
158
201
|
function flatScan(json) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
result.h ??= asNumber(inputs.height);
|
|
189
|
-
}
|
|
190
|
-
return result;
|
|
202
|
+
const result = {};
|
|
203
|
+
const nodes = parseJson(json.replace(/\0+$/, "").replace(/\bNaN\b/g, "null"));
|
|
204
|
+
if (!nodes) return result;
|
|
205
|
+
for (const key in nodes) {
|
|
206
|
+
const node = nodes[key];
|
|
207
|
+
if (!node || typeof node !== "object") continue;
|
|
208
|
+
const classType = asString(node[CT]);
|
|
209
|
+
const inputs = node.inputs;
|
|
210
|
+
if (!inputs) continue;
|
|
211
|
+
if (/Text(Encode|.?Box)|ShowText|PromptStash/.test(classType)) {
|
|
212
|
+
const txt = asString(inputs.text) || asString(inputs.prompt) || asString(inputs.Text) || asString(inputs.prompt_text) || asString(inputs.text_0);
|
|
213
|
+
if (txt) {
|
|
214
|
+
if (/neg/i.test(classType)) result.n ??= txt;
|
|
215
|
+
else if (!result.p) result.p = txt;
|
|
216
|
+
else if (!result.n) result.n = txt;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (classType.includes("Sampler")) {
|
|
220
|
+
result.se ??= asNumber(inputs.seed) ?? asNumber(inputs.noise_seed);
|
|
221
|
+
result.st ??= asNumber(inputs.steps);
|
|
222
|
+
result.cf ??= asNumber(inputs.cfg);
|
|
223
|
+
result.sa ||= asString(inputs.sampler_name);
|
|
224
|
+
result.sc ||= asString(inputs.scheduler);
|
|
225
|
+
}
|
|
226
|
+
result.md ||= asString(inputs.ckpt_name) || asString(inputs.unet_name);
|
|
227
|
+
result.w ??= asNumber(inputs.width);
|
|
228
|
+
result.h ??= asNumber(inputs.height);
|
|
229
|
+
}
|
|
230
|
+
return result;
|
|
191
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Build human-readable text in A1111 (Stable Diffusion WebUI) format.
|
|
234
|
+
*
|
|
235
|
+
* Output structure:
|
|
236
|
+
* Line 1: positive prompt
|
|
237
|
+
* Line 2: "Negative prompt: <negative prompt>"
|
|
238
|
+
* Line 3: comma-separated key-value pairs (Steps, Sampler, CFG, etc.)
|
|
239
|
+
*/
|
|
192
240
|
function buildA1111(params) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
241
|
+
const lines = [];
|
|
242
|
+
if (params.p) lines.push(params.p);
|
|
243
|
+
if (params.n) lines.push(`Negative prompt: ${params.n}`);
|
|
244
|
+
const fields = [];
|
|
245
|
+
const addField = (key, value) => {
|
|
246
|
+
if (value) fields.push(`${key}: ${value}`);
|
|
247
|
+
};
|
|
248
|
+
addField("Steps", params.st);
|
|
249
|
+
addField("Sampler", params.sa);
|
|
250
|
+
addField("Schedule type", params.sc);
|
|
251
|
+
addField("CFG scale", params.cf);
|
|
252
|
+
addField("Seed", params.se);
|
|
253
|
+
if (params.w && params.h) addField("Size", `${params.w}x${params.h}`);
|
|
254
|
+
addField("Model hash", params.mh);
|
|
255
|
+
addField("Model", params.md);
|
|
256
|
+
addField("Clip skip", params.cs);
|
|
257
|
+
addField("Denoising strength", params.dn ?? params.hd);
|
|
258
|
+
addField("Hires upscale", params.hs);
|
|
259
|
+
addField("Hires steps", params.ht);
|
|
260
|
+
addField("Hires upscaler", params.hu);
|
|
261
|
+
if (fields.length) lines.push(fields.join(", "));
|
|
262
|
+
return lines.join("\n");
|
|
215
263
|
}
|
|
264
|
+
/** Safely parse JSON, returning null on failure. */
|
|
216
265
|
function parseJson(text) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
266
|
+
if (!text) return null;
|
|
267
|
+
try {
|
|
268
|
+
return JSON.parse(text);
|
|
269
|
+
} catch {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
223
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Coerce unknown value to string. Returns "" for non-strings.
|
|
275
|
+
* Returning "" (falsy) enables `||` fallback chains: `asString(a) || asString(b)`.
|
|
276
|
+
*/
|
|
224
277
|
function asString(v) {
|
|
225
|
-
|
|
278
|
+
return typeof v === "string" ? v : "";
|
|
226
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Convert a value to its string representation for channeling.
|
|
282
|
+
* Strings pass through unchanged; objects are JSON-serialized.
|
|
283
|
+
* Used by channelKnownKeys to convert nested JSON objects to strings
|
|
284
|
+
* that extract() can later re-parse.
|
|
285
|
+
*/
|
|
227
286
|
function stringifyValue(v) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
287
|
+
if (typeof v === "string") return v;
|
|
288
|
+
if (v && typeof v === "object") return JSON.stringify(v);
|
|
289
|
+
return "";
|
|
231
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Coerce unknown value to number. Returns undefined for non-numeric values.
|
|
293
|
+
* Accepts both number and numeric string inputs (e.g. "42" → 42).
|
|
294
|
+
* NaN values are filtered out and returned as undefined.
|
|
295
|
+
*/
|
|
232
296
|
function asNumber(v) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
return void 0;
|
|
297
|
+
if (typeof v === "number") return v === v ? v : void 0;
|
|
298
|
+
if (typeof v === "string") {
|
|
299
|
+
const n = +v;
|
|
300
|
+
return n === n ? n : void 0;
|
|
301
|
+
}
|
|
239
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Extract width/height from a dimension string using the given regex.
|
|
305
|
+
* Different tools use different formats:
|
|
306
|
+
* - Fooocus: "1024x1024" → /(\d+)\s*x\s*(\d+)/
|
|
307
|
+
* - HF Space: "(1024, 1024)" → /\(\s*(\d+)\s*,\s*(\d+)\s*\)/
|
|
308
|
+
*/
|
|
240
309
|
function parseDimensions(text, pattern) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
310
|
+
if (!text) return [];
|
|
311
|
+
const match = text.match(pattern);
|
|
312
|
+
return match ? [Number(match[1]), Number(match[2])] : [];
|
|
244
313
|
}
|
|
245
|
-
|
|
246
|
-
|
|
314
|
+
//#endregion
|
|
315
|
+
//#region src/read.ts
|
|
316
|
+
/**
|
|
317
|
+
* Extract text chunks from PNG data as keyword→text entries.
|
|
318
|
+
*/
|
|
247
319
|
function readPng(data) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}
|
|
278
|
-
return entries;
|
|
320
|
+
const entries = {};
|
|
321
|
+
const dv = dataview(data);
|
|
322
|
+
let offset = 8;
|
|
323
|
+
while (offset + 8 <= data.length) {
|
|
324
|
+
const length = dv.getUint32(offset);
|
|
325
|
+
const type = str(data, offset + 4, 4);
|
|
326
|
+
offset += 8;
|
|
327
|
+
if (offset + length > data.length) break;
|
|
328
|
+
if (type === "tEXt" || type === "iTXt") {
|
|
329
|
+
const nullIdx = data.indexOf(0, offset);
|
|
330
|
+
if (nullIdx > offset && nullIdx < offset + length) {
|
|
331
|
+
const keyword = str(data, offset, nullIdx - offset);
|
|
332
|
+
if (type === "tEXt") entries[keyword] = tryUtf8(data.subarray(nullIdx + 1, offset + length)) ?? str(data, nullIdx + 1, offset + length - nullIdx - 1);
|
|
333
|
+
else {
|
|
334
|
+
const pos = nullIdx + 3;
|
|
335
|
+
const langEnd = data.indexOf(0, pos);
|
|
336
|
+
const transEnd = langEnd < 0 ? -1 : data.indexOf(0, langEnd + 1);
|
|
337
|
+
if (langEnd < 0 || transEnd < 0) {
|
|
338
|
+
offset += length + 4;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
entries[keyword] = tryUtf8(data.subarray(transEnd + 1, offset + length)) ?? "";
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (type === "IEND") break;
|
|
346
|
+
offset += length + 4;
|
|
347
|
+
}
|
|
348
|
+
return entries;
|
|
279
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* Extract metadata from JPEG: EXIF UserComment/ImageDescription/Make + COM segment.
|
|
352
|
+
*/
|
|
280
353
|
function readJpeg(data) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const text = tryUtf8(comData);
|
|
300
|
-
if (text) entries.Comment = text;
|
|
301
|
-
}
|
|
302
|
-
offset += 2 + segLen;
|
|
303
|
-
}
|
|
304
|
-
return entries;
|
|
354
|
+
const entries = {};
|
|
355
|
+
let offset = 2;
|
|
356
|
+
while (offset + 4 <= data.length) {
|
|
357
|
+
const marker = data[offset + 1];
|
|
358
|
+
if (data[offset] !== 255 || marker === 255) {
|
|
359
|
+
offset++;
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (marker > 216 && marker < 219) break;
|
|
363
|
+
const segLen = data[offset + 2] << 8 | data[offset + 3];
|
|
364
|
+
if (marker === 225 && offset + 10 <= data.length && str(data, offset + 4, 6) === "Exif\0\0") Object.assign(entries, readExif(data.subarray(offset + 10, offset + 2 + segLen)));
|
|
365
|
+
if (marker === 254) {
|
|
366
|
+
const text = tryUtf8(data.subarray(offset + 4, offset + 2 + segLen));
|
|
367
|
+
if (text) entries.Comment = text;
|
|
368
|
+
}
|
|
369
|
+
offset += 2 + segLen;
|
|
370
|
+
}
|
|
371
|
+
return entries;
|
|
305
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* Extract metadata from WebP: EXIF chunk → same EXIF parsing as JPEG.
|
|
375
|
+
*/
|
|
306
376
|
function readWebp(data) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}
|
|
318
|
-
return {};
|
|
377
|
+
const dv = dataview(data);
|
|
378
|
+
let offset = 12;
|
|
379
|
+
while (offset + 8 <= data.length) {
|
|
380
|
+
const type = str(data, offset, 4);
|
|
381
|
+
const chunkSize = dv.getUint32(offset + 4, true);
|
|
382
|
+
offset += 8;
|
|
383
|
+
if (type === "EXIF" && offset + chunkSize <= data.length) return readExif(data.subarray(offset, offset + chunkSize));
|
|
384
|
+
offset += chunkSize + chunkSize % 2;
|
|
385
|
+
}
|
|
386
|
+
return {};
|
|
319
387
|
}
|
|
388
|
+
/**
|
|
389
|
+
* Parse EXIF/TIFF data and extract metadata entries.
|
|
390
|
+
*/
|
|
320
391
|
function readExif(data) {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
} else if (tag === 34665) {
|
|
364
|
-
exifIfdOffset = dv.getUint32(tagData.byteOffset - data.byteOffset, le);
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
if (exifIfdOffset && !entries.UserComment && !entries.Software) {
|
|
368
|
-
scanIfd(data, dv, exifIfdOffset, le, (tag, tagData) => {
|
|
369
|
-
if (tag === 37510) handleUserComment(tagData);
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
return entries;
|
|
392
|
+
if (data.length < 8) return {};
|
|
393
|
+
const firstByte = data[0];
|
|
394
|
+
const le = firstByte === 73;
|
|
395
|
+
if (!le && firstByte !== 77) return {};
|
|
396
|
+
const dv = dataview(data);
|
|
397
|
+
if (dv.getUint16(2, le) !== 42) return {};
|
|
398
|
+
const entries = {};
|
|
399
|
+
const ifd0 = dv.getUint32(4, le);
|
|
400
|
+
let exifIfdOffset = 0;
|
|
401
|
+
const handleUserComment = (tagData) => {
|
|
402
|
+
const text = decodeUserComment(tagData);
|
|
403
|
+
if (text) {
|
|
404
|
+
if (text.startsWith("{")) try {
|
|
405
|
+
const obj = JSON.parse(text);
|
|
406
|
+
const software = obj.Software;
|
|
407
|
+
const comment = obj.Comment;
|
|
408
|
+
if (typeof software === "string" && software.startsWith("NovelAI") && typeof comment === "string") {
|
|
409
|
+
entries.Software = software;
|
|
410
|
+
entries.Comment = comment;
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
} catch {}
|
|
414
|
+
entries.UserComment = text;
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
scanIfd(data, dv, ifd0, le, (tag, tagData) => {
|
|
418
|
+
if (tag === 270 || tag === 271) {
|
|
419
|
+
const text = decodeAscii(tagData);
|
|
420
|
+
if (text) {
|
|
421
|
+
const prefixMatch = text.match(/^([A-Za-z]+):\s/);
|
|
422
|
+
if (prefixMatch) {
|
|
423
|
+
const key = prefixMatch[1];
|
|
424
|
+
if (key) entries[key] = text.slice(prefixMatch[0].length);
|
|
425
|
+
} else entries[tag === 270 ? "ImageDescription" : "Make"] = text;
|
|
426
|
+
}
|
|
427
|
+
} else if (tag === 37510) handleUserComment(tagData);
|
|
428
|
+
else if (tag === 34665) exifIfdOffset = dv.getUint32(tagData.byteOffset - data.byteOffset, le);
|
|
429
|
+
});
|
|
430
|
+
if (exifIfdOffset && !entries.UserComment && !entries.Software) scanIfd(data, dv, exifIfdOffset, le, (tag, tagData) => {
|
|
431
|
+
if (tag === 37510) handleUserComment(tagData);
|
|
432
|
+
});
|
|
433
|
+
return entries;
|
|
373
434
|
}
|
|
435
|
+
/**
|
|
436
|
+
* Scan IFD entries and call handler for each tag.
|
|
437
|
+
*/
|
|
374
438
|
function scanIfd(data, dv, ifdOffset, le, handler) {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
439
|
+
const u16 = (o) => dv.getUint16(o, le);
|
|
440
|
+
const u32 = (o) => dv.getUint32(o, le);
|
|
441
|
+
if (ifdOffset + 2 > data.length) return;
|
|
442
|
+
const count = u16(ifdOffset);
|
|
443
|
+
let pos = ifdOffset + 2;
|
|
444
|
+
for (let i = 0; i < count; i++) {
|
|
445
|
+
if (pos + 12 > data.length) return;
|
|
446
|
+
const tag = u16(pos);
|
|
447
|
+
const type = u16(pos + 2);
|
|
448
|
+
const size = u32(pos + 4) * ([
|
|
449
|
+
0,
|
|
450
|
+
1,
|
|
451
|
+
1,
|
|
452
|
+
2,
|
|
453
|
+
4,
|
|
454
|
+
8,
|
|
455
|
+
0,
|
|
456
|
+
1
|
|
457
|
+
][type] ?? 1);
|
|
458
|
+
const valOffset = size <= 4 ? pos + 8 : u32(pos + 8);
|
|
459
|
+
if (valOffset + size <= data.length) handler(tag, data.subarray(valOffset, valOffset + size));
|
|
460
|
+
pos += 12;
|
|
461
|
+
}
|
|
393
462
|
}
|
|
394
463
|
function dataview(data) {
|
|
395
|
-
|
|
464
|
+
return new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
396
465
|
}
|
|
397
|
-
|
|
398
|
-
|
|
466
|
+
const utf8Dec = new TextDecoder("utf-8", { fatal: true });
|
|
467
|
+
const fromCharCode = String.fromCharCode;
|
|
399
468
|
function str(data, offset, len) {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
469
|
+
let s = "";
|
|
470
|
+
for (let i = 0; i < len; i++) s += fromCharCode(data[offset + i]);
|
|
471
|
+
return s;
|
|
403
472
|
}
|
|
404
473
|
function tryUtf8(data) {
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
474
|
+
try {
|
|
475
|
+
return utf8Dec.decode(data).replace(/\0$/, "");
|
|
476
|
+
} catch {
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
410
479
|
}
|
|
411
480
|
function decodeAscii(data) {
|
|
412
|
-
|
|
413
|
-
|
|
481
|
+
const nullIdx = data.indexOf(0);
|
|
482
|
+
return str(data, 0, nullIdx < 0 ? data.length : nullIdx).trim() || null;
|
|
414
483
|
}
|
|
484
|
+
/**
|
|
485
|
+
* Decode UserComment with encoding prefix detection.
|
|
486
|
+
*/
|
|
415
487
|
function decodeUserComment(data) {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
return decodeAscii(payload);
|
|
426
|
-
}
|
|
427
|
-
return tryUtf8(data);
|
|
488
|
+
if (data.length < 8) return null;
|
|
489
|
+
const prefix = str(data, 0, 8);
|
|
490
|
+
const payload = data.subarray(8);
|
|
491
|
+
if (prefix === "UNICODE\0") {
|
|
492
|
+
if (payload.length < 2) return null;
|
|
493
|
+
return decodeUtf16(payload, payload[0] !== 0 && payload[1] === 0);
|
|
494
|
+
}
|
|
495
|
+
if (prefix === "ASCII\0\0\0") return decodeAscii(payload);
|
|
496
|
+
return tryUtf8(data);
|
|
428
497
|
}
|
|
429
498
|
function decodeUtf16(data, le) {
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
499
|
+
let s = "";
|
|
500
|
+
for (let i = 0; i + 1 < data.length; i += 2) {
|
|
501
|
+
const code = le ? data[i] | data[i + 1] << 8 : data[i] << 8 | data[i + 1];
|
|
502
|
+
if (!code) break;
|
|
503
|
+
s += fromCharCode(code);
|
|
504
|
+
}
|
|
505
|
+
return s;
|
|
437
506
|
}
|
|
438
|
-
|
|
439
|
-
|
|
507
|
+
//#endregion
|
|
508
|
+
//#region src/index.ts
|
|
509
|
+
/**
|
|
510
|
+
* @enslo/sd-metadata-lite
|
|
511
|
+
*
|
|
512
|
+
* Lightweight AI image metadata reader for bookmarklets.
|
|
513
|
+
* Reads PNG/JPEG/WebP files and returns A1111-format plain text.
|
|
514
|
+
*/
|
|
515
|
+
/**
|
|
516
|
+
* Parse AI-generated image metadata and return A1111-format text.
|
|
517
|
+
*
|
|
518
|
+
* Supports PNG, JPEG, and WebP formats across 18+ generation tools.
|
|
519
|
+
* Returns empty string if no metadata is found.
|
|
520
|
+
*
|
|
521
|
+
* @param input - Image file data
|
|
522
|
+
* @returns A1111-format metadata text, or empty string
|
|
523
|
+
*/
|
|
440
524
|
function parse(input) {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
return extract(readJpeg(data));
|
|
447
|
-
}
|
|
448
|
-
if (data[0] === 82 && data[1] === 73) {
|
|
449
|
-
return extract(readWebp(data));
|
|
450
|
-
}
|
|
451
|
-
return "";
|
|
525
|
+
const data = input instanceof ArrayBuffer ? new Uint8Array(input) : input;
|
|
526
|
+
if (data[0] === 137 && data[1] === 80) return extract(readPng(data));
|
|
527
|
+
if (data[0] === 255 && data[1] === 216) return extract(readJpeg(data));
|
|
528
|
+
if (data[0] === 82 && data[1] === 73) return extract(readWebp(data));
|
|
529
|
+
return "";
|
|
452
530
|
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
531
|
+
//#endregion
|
|
532
|
+
export { parse };
|
|
533
|
+
|
|
456
534
|
//# sourceMappingURL=index.js.map
|