@enslo/sd-metadata 1.3.0 → 1.4.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/README.ja.md CHANGED
@@ -153,7 +153,7 @@ if (result.status === 'success') {
153
153
  > 本番環境では `@latest` の代わりに特定のバージョンを指定してください:
154
154
  >
155
155
  > ```text
156
- > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.3.0/dist/index.js
156
+ > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.4.0/dist/index.js
157
157
  > ```
158
158
 
159
159
  ### 応用例
@@ -226,9 +226,9 @@ switch (result.status) {
226
226
  </details>
227
227
 
228
228
  <details>
229
- <summary>未対応メタデータの保持</summary>
229
+ <summary>未対応メタデータの扱い</summary>
230
230
 
231
- 未対応ツールのメタデータを含む画像を変換する際も、元のメタデータを保持できます:
231
+ 未対応ツールのメタデータを含む画像を扱う場合:
232
232
 
233
233
  ```typescript
234
234
  import { read, write } from '@enslo/sd-metadata';
@@ -236,12 +236,16 @@ import { read, write } from '@enslo/sd-metadata';
236
236
  const source = read(unknownImage);
237
237
  // source.status === 'unrecognized'
238
238
 
239
- // 元のメタデータチャンク/セグメントをそのまま保持
240
- const result = write(targetImage, source, { force: true });
241
-
239
+ // ターゲット画像に書き込み
240
+ // - 同じフォーマット(例:PNG PNG):メタデータそのまま保持
241
+ // - 異なるフォーマット(例:PNG → JPEG):warning付きでメタデータ削除
242
+ const result = write(targetImage, source);
242
243
  if (result.ok) {
243
- // 元のメタデータが新しい画像に保持される
244
- console.log('メタデータの保持に成功しました');
244
+ saveFile('output.png', result.value);
245
+ if (result.warning) {
246
+ // クロスフォーマット変換によりメタデータが削除された場合
247
+ console.warn('メタデータが削除されました:', result.warning.reason);
248
+ }
245
249
  }
246
250
  ```
247
251
 
@@ -346,7 +350,7 @@ if (result.status === 'success') {
346
350
  - `{ status: 'invalid', message? }` - 破損または非対応の画像フォーマット
347
351
  - `message`: オプションのエラー説明
348
352
 
349
- ### `write(data: Uint8Array, metadata: ParseResult, options?: WriteOptions): WriteResult`
353
+ ### `write(data: Uint8Array, metadata: ParseResult): WriteResult`
350
354
 
351
355
  画像ファイルにメタデータを書き込みます。
352
356
 
@@ -355,13 +359,12 @@ if (result.status === 'success') {
355
359
  - `data` - ターゲット画像ファイルデータ(PNG、JPEG、またはWebP)
356
360
  - `metadata` - `read()` から得られた `ParseResult`
357
361
  - `status: 'success'` または `'empty'` - 直接書き込み可能
358
- - `status: 'unrecognized'` - `force: true` オプションが必要
359
- - `options` - オプション設定:
360
- - `force?: boolean` - 未対応メタデータの書き込みを有効化(元データをそのまま保持)
362
+ - `status: 'unrecognized'` - 同じフォーマット:そのまま書き込み、異なるフォーマット:warning付きでメタデータ削除
361
363
 
362
364
  **戻り値:**
363
365
 
364
- - `{ ok: true, value: Uint8Array }` - 書き込み成功(新しい画像データを返す)
366
+ - `{ ok: true, value: Uint8Array, warning?: WriteWarning }` - 書き込み成功
367
+ - `warning` はメタデータが意図的に削除された場合に設定される(例:未対応のクロスフォーマット変換)
365
368
  - `{ ok: false, error: { type, message? } }` - 失敗。`type` は以下のいずれか:
366
369
  - `'unsupportedFormat'`: 対象画像がPNG、JPEG、WebP以外の場合
367
370
  - `'conversionFailed'`: メタデータ変換に失敗(例:互換性のないフォーマット)
package/README.md CHANGED
@@ -153,7 +153,7 @@ if (result.status === 'success') {
153
153
  > For production use, pin to a specific version instead of `@latest`:
154
154
  >
155
155
  > ```text
156
- > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.3.0/dist/index.js
156
+ > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.4.0/dist/index.js
157
157
  > ```
158
158
 
159
159
  ### Advanced Examples
@@ -226,9 +226,9 @@ switch (result.status) {
226
226
  </details>
227
227
 
228
228
  <details>
229
- <summary>Preserving Unrecognized Metadata</summary>
229
+ <summary>Handling Unrecognized Metadata</summary>
230
230
 
231
- When converting images with metadata from unsupported tools, you can still preserve the original metadata:
231
+ When working with metadata from unsupported tools:
232
232
 
233
233
  ```typescript
234
234
  import { read, write } from '@enslo/sd-metadata';
@@ -236,12 +236,16 @@ import { read, write } from '@enslo/sd-metadata';
236
236
  const source = read(unknownImage);
237
237
  // source.status === 'unrecognized'
238
238
 
239
- // Preserve all original metadata chunks/segments
240
- const result = write(targetImage, source, { force: true });
241
-
239
+ // Write to target image
240
+ // - Same format (e.g., PNG PNG): metadata preserved as-is
241
+ // - Cross-format (e.g., PNG → JPEG): metadata dropped with warning
242
+ const result = write(targetImage, source);
242
243
  if (result.ok) {
243
- // Original metadata preserved in the new image
244
- console.log('Metadata preserved successfully');
244
+ saveFile('output.png', result.value);
245
+ if (result.warning) {
246
+ // Metadata was dropped during cross-format conversion
247
+ console.warn('Metadata was dropped:', result.warning.reason);
248
+ }
245
249
  }
246
250
  ```
247
251
 
@@ -346,7 +350,7 @@ Reads and parses metadata from an image file.
346
350
  - `{ status: 'invalid', message? }` - Corrupted or unsupported image format
347
351
  - `message`: Optional error description
348
352
 
349
- ### `write(data: Uint8Array, metadata: ParseResult, options?: WriteOptions): WriteResult`
353
+ ### `write(data: Uint8Array, metadata: ParseResult): WriteResult`
350
354
 
351
355
  Writes metadata to an image file.
352
356
 
@@ -355,13 +359,12 @@ Writes metadata to an image file.
355
359
  - `data` - Target image file data (PNG, JPEG, or WebP)
356
360
  - `metadata` - `ParseResult` from `read()`
357
361
  - `status: 'success'` or `'empty'` - Can write directly
358
- - `status: 'unrecognized'` - Requires `force: true` option
359
- - `options` - Optional settings:
360
- - `force?: boolean` - Enables writing unrecognized metadata (preserves original data as-is)
362
+ - `status: 'unrecognized'` - Same format: writes as-is; Cross-format: drops metadata with warning
361
363
 
362
364
  **Returns:**
363
365
 
364
- - `{ ok: true, value: Uint8Array }` - Successfully written (returns new image data)
366
+ - `{ ok: true, value: Uint8Array, warning?: WriteWarning }` - Successfully written
367
+ - `warning` is set when metadata was intentionally dropped (e.g., unrecognized cross-format)
365
368
  - `{ ok: false, error: { type, message? } }` - Failed. `type` is one of:
366
369
  - `'unsupportedFormat'`: Target image is not PNG, JPEG, or WebP
367
370
  - `'conversionFailed'`: Metadata conversion failed (e.g., incompatible format)
package/dist/index.d.ts CHANGED
@@ -1,20 +1,3 @@
1
- /**
2
- * Result type for explicit error handling
3
- */
4
- type Result<T, E> = {
5
- ok: true;
6
- value: T;
7
- } | {
8
- ok: false;
9
- error: E;
10
- };
11
- /**
12
- * Helper functions for Result type
13
- */
14
- declare const Result: {
15
- ok: <T, E>(value: T) => Result<T, E>;
16
- error: <T, E>(error: E) => Result<T, E>;
17
- };
18
1
  /**
19
2
  * PNG text chunk (tEXt or iTXt)
20
3
  */
@@ -330,9 +313,16 @@ declare function read(data: Uint8Array): ParseResult;
330
313
  */
331
314
 
332
315
  /**
333
- * Result of the write operation
316
+ * Warning types for write operations
317
+ */
318
+ type WriteWarning = {
319
+ type: 'metadataDropped';
320
+ reason: 'unrecognizedCrossFormat';
321
+ };
322
+ /**
323
+ * Error types for write operations
334
324
  */
335
- type WriteResult = Result<Uint8Array, {
325
+ type WriteError = {
336
326
  type: 'unsupportedFormat';
337
327
  } | {
338
328
  type: 'conversionFailed';
@@ -340,36 +330,32 @@ type WriteResult = Result<Uint8Array, {
340
330
  } | {
341
331
  type: 'writeFailed';
342
332
  message: string;
343
- }>;
333
+ };
344
334
  /**
345
- * Options for write operation
335
+ * Result of the write operation
336
+ *
337
+ * Success case may include a warning when metadata was intentionally dropped.
346
338
  */
347
- interface WriteOptions {
348
- /**
349
- * Force blind conversion for unrecognized formats
350
- *
351
- * When true, converts raw chunks/segments between formats even when
352
- * the generating software is unknown. Enables format conversion for
353
- * unknown/future tools without parser implementation.
354
- *
355
- * When false (default), returns error for unrecognized formats.
356
- *
357
- * @default false
358
- */
359
- force?: boolean;
360
- }
339
+ type WriteResult = {
340
+ ok: true;
341
+ value: Uint8Array;
342
+ warning?: WriteWarning;
343
+ } | {
344
+ ok: false;
345
+ error: WriteError;
346
+ };
361
347
  /**
362
348
  * Write metadata to an image
363
349
  *
364
350
  * Automatically detects the target image format and converts the metadata
365
- * if necessary.
351
+ * if necessary. For unrecognized metadata with cross-format conversion,
352
+ * metadata is dropped and a warning is returned.
366
353
  *
367
354
  * @param data - Target image file data
368
- * @param metadata - ParseResult from `read()` (must be 'success' or contain raw data)
369
- * @param options - Write options (e.g., { force: true } for blind conversion)
370
- * @returns New image data with embedded metadata
355
+ * @param metadata - ParseResult from `read()`
356
+ * @returns New image data with embedded metadata (or warning if metadata was dropped)
371
357
  */
372
- declare function write(data: Uint8Array, metadata: ParseResult, options?: WriteOptions): WriteResult;
358
+ declare function write(data: Uint8Array, metadata: ParseResult): WriteResult;
373
359
 
374
360
  /**
375
361
  * WebUI (A1111) format writer for sd-metadata
@@ -491,4 +477,4 @@ declare function formatAsWebUI(metadata: GenerationMetadata): string;
491
477
  */
492
478
  declare function formatRaw(raw: RawMetadata): string;
493
479
 
494
- export { type CharacterPrompt, type GenerationMetadata, type HiresSettings, type ITXtChunk, type MetadataSegment, type MetadataSegmentSource, type ModelSettings, type ParseResult, type PngTextChunk, type RawMetadata, type SamplingSettings, type TExtChunk, type UpscaleSettings, type WriteOptions, type WriteResult, formatAsWebUI, formatRaw, read, write, writeAsWebUI };
480
+ export { type CharacterPrompt, type GenerationMetadata, type HiresSettings, type ITXtChunk, type MetadataSegment, type MetadataSegmentSource, type ModelSettings, type ParseResult, type PngTextChunk, type RawMetadata, type SamplingSettings, type TExtChunk, type UpscaleSettings, type WriteResult, type WriteWarning, formatAsWebUI, formatRaw, read, write, writeAsWebUI };
package/dist/index.js CHANGED
@@ -1768,7 +1768,6 @@ var CHUNK_ENCODING_STRATEGIES = {
1768
1768
  novelai: "dynamic",
1769
1769
  "sd-next": "dynamic",
1770
1770
  easydiffusion: "dynamic",
1771
- blind: "dynamic",
1772
1771
  // Unicode escape tools (spec-compliant)
1773
1772
  comfyui: "text-unicode-escape",
1774
1773
  swarmui: "text-unicode-escape",
@@ -1833,37 +1832,6 @@ function convertA1111SegmentsToPng(segments) {
1833
1832
  );
1834
1833
  }
1835
1834
 
1836
- // src/converters/blind.ts
1837
- function blindPngToSegments(chunks) {
1838
- if (chunks.length === 0) return [];
1839
- const chunkMap = Object.fromEntries(
1840
- chunks.map((chunk) => [chunk.keyword, chunk.text])
1841
- );
1842
- return [
1843
- {
1844
- source: { type: "exifUserComment" },
1845
- data: JSON.stringify(chunkMap)
1846
- }
1847
- ];
1848
- }
1849
- function blindSegmentsToPng(segments) {
1850
- const userComment = segments.find((s) => s.source.type === "exifUserComment");
1851
- if (!userComment) return [];
1852
- const parsed = parseJson(userComment.data);
1853
- if (parsed.ok) {
1854
- return Object.entries(parsed.value).flatMap(([keyword, value]) => {
1855
- const text = typeof value === "string" ? value : JSON.stringify(value);
1856
- if (!text) return [];
1857
- return createEncodedChunk(keyword, text, getEncodingStrategy("blind"));
1858
- });
1859
- }
1860
- return createEncodedChunk(
1861
- "metadata",
1862
- userComment.data,
1863
- getEncodingStrategy("blind")
1864
- );
1865
- }
1866
-
1867
1835
  // src/converters/comfyui.ts
1868
1836
  function convertComfyUIPngToSegments(chunks) {
1869
1837
  const data = {};
@@ -2171,7 +2139,7 @@ function convertSwarmUISegmentsToPng(segments) {
2171
2139
  }
2172
2140
 
2173
2141
  // src/converters/index.ts
2174
- function convertMetadata(parseResult, targetFormat, force = false) {
2142
+ function convertMetadata(parseResult, targetFormat) {
2175
2143
  if (parseResult.status === "empty") {
2176
2144
  return Result.error({ type: "missingRawData" });
2177
2145
  }
@@ -2181,17 +2149,17 @@ function convertMetadata(parseResult, targetFormat, force = false) {
2181
2149
  status: parseResult.status
2182
2150
  });
2183
2151
  }
2184
- const raw = parseResult.raw;
2185
- if (raw.format === "png" && targetFormat === "png" || raw.format === "jpeg" && targetFormat === "jpeg" || raw.format === "webp" && targetFormat === "webp") {
2186
- return Result.ok(raw);
2187
- }
2188
- const software = parseResult.status === "success" ? parseResult.metadata.software : null;
2189
- if (!software) {
2190
- return force ? convertBlind(raw, targetFormat) : Result.error({
2152
+ if (parseResult.status === "unrecognized") {
2153
+ return Result.error({
2191
2154
  type: "unsupportedSoftware",
2192
2155
  software: "unknown"
2193
2156
  });
2194
2157
  }
2158
+ const raw = parseResult.raw;
2159
+ if (raw.format === "png" && targetFormat === "png" || raw.format === "jpeg" && targetFormat === "jpeg" || raw.format === "webp" && targetFormat === "webp") {
2160
+ return Result.ok(raw);
2161
+ }
2162
+ const software = parseResult.metadata.software;
2195
2163
  const converter = softwareConverters[software];
2196
2164
  if (!converter) {
2197
2165
  return Result.error({
@@ -2253,10 +2221,6 @@ var convertHfSpace = createFormatConverter(
2253
2221
  createPngToSegments("parameters"),
2254
2222
  createSegmentsToPng("parameters")
2255
2223
  );
2256
- var convertBlind = createFormatConverter(
2257
- blindPngToSegments,
2258
- blindSegmentsToPng
2259
- );
2260
2224
  var softwareConverters = {
2261
2225
  // NovelAI
2262
2226
  novelai: convertNovelai,
@@ -2810,58 +2774,95 @@ function buildExifChunk(segments) {
2810
2774
  }
2811
2775
 
2812
2776
  // src/api/write.ts
2813
- function write(data, metadata, options) {
2777
+ function write(data, metadata) {
2814
2778
  const targetFormat = detectFormat(data);
2815
2779
  if (!targetFormat) {
2816
- return Result.error({ type: "unsupportedFormat" });
2780
+ return { ok: false, error: { type: "unsupportedFormat" } };
2817
2781
  }
2818
2782
  if (metadata.status === "empty") {
2819
2783
  const result = HELPERS2[targetFormat].writeEmpty(data, []);
2820
2784
  if (!result.ok) {
2821
- return Result.error({ type: "writeFailed", message: result.error.type });
2785
+ return {
2786
+ ok: false,
2787
+ error: { type: "writeFailed", message: result.error.type }
2788
+ };
2822
2789
  }
2823
- return Result.ok(result.value);
2790
+ return { ok: true, value: result.value };
2824
2791
  }
2825
2792
  if (metadata.status === "invalid") {
2826
- return Result.error({
2827
- type: "writeFailed",
2828
- message: "Cannot write invalid metadata"
2829
- });
2793
+ return {
2794
+ ok: false,
2795
+ error: { type: "writeFailed", message: "Cannot write invalid metadata" }
2796
+ };
2830
2797
  }
2831
- const conversionResult = convertMetadata(
2832
- metadata,
2833
- targetFormat,
2834
- options?.force ?? false
2835
- );
2798
+ if (metadata.status === "unrecognized") {
2799
+ const sourceFormat = metadata.raw.format;
2800
+ if (sourceFormat === targetFormat) {
2801
+ return writeRaw(data, targetFormat, metadata.raw);
2802
+ }
2803
+ const result = HELPERS2[targetFormat].writeEmpty(data, []);
2804
+ if (!result.ok) {
2805
+ return {
2806
+ ok: false,
2807
+ error: { type: "writeFailed", message: result.error.type }
2808
+ };
2809
+ }
2810
+ return {
2811
+ ok: true,
2812
+ value: result.value,
2813
+ warning: { type: "metadataDropped", reason: "unrecognizedCrossFormat" }
2814
+ };
2815
+ }
2816
+ const conversionResult = convertMetadata(metadata, targetFormat);
2836
2817
  if (!conversionResult.ok) {
2837
- return Result.error({
2838
- type: "conversionFailed",
2839
- message: `Failed to convert metadata: ${conversionResult.error.type}`
2840
- });
2818
+ return {
2819
+ ok: false,
2820
+ error: {
2821
+ type: "conversionFailed",
2822
+ message: `Failed to convert metadata: ${conversionResult.error.type}`
2823
+ }
2824
+ };
2841
2825
  }
2842
- const newRaw = conversionResult.value;
2843
- if (targetFormat === "png" && newRaw.format === "png") {
2844
- const result = writePngMetadata(data, newRaw.chunks);
2845
- if (!result.ok)
2846
- return Result.error({ type: "writeFailed", message: result.error.type });
2847
- return Result.ok(result.value);
2848
- }
2849
- if (targetFormat === "jpeg" && newRaw.format === "jpeg") {
2850
- const result = writeJpegMetadata(data, newRaw.segments);
2851
- if (!result.ok)
2852
- return Result.error({ type: "writeFailed", message: result.error.type });
2853
- return Result.ok(result.value);
2854
- }
2855
- if (targetFormat === "webp" && newRaw.format === "webp") {
2856
- const result = writeWebpMetadata(data, newRaw.segments);
2857
- if (!result.ok)
2858
- return Result.error({ type: "writeFailed", message: result.error.type });
2859
- return Result.ok(result.value);
2826
+ return writeRaw(data, targetFormat, conversionResult.value);
2827
+ }
2828
+ function writeRaw(data, targetFormat, raw) {
2829
+ if (targetFormat === "png" && raw.format === "png") {
2830
+ const result = writePngMetadata(data, raw.chunks);
2831
+ if (!result.ok) {
2832
+ return {
2833
+ ok: false,
2834
+ error: { type: "writeFailed", message: result.error.type }
2835
+ };
2836
+ }
2837
+ return { ok: true, value: result.value };
2860
2838
  }
2861
- return Result.error({
2862
- type: "writeFailed",
2863
- message: "Internal error: format mismatch after conversion"
2864
- });
2839
+ if (targetFormat === "jpeg" && raw.format === "jpeg") {
2840
+ const result = writeJpegMetadata(data, raw.segments);
2841
+ if (!result.ok) {
2842
+ return {
2843
+ ok: false,
2844
+ error: { type: "writeFailed", message: result.error.type }
2845
+ };
2846
+ }
2847
+ return { ok: true, value: result.value };
2848
+ }
2849
+ if (targetFormat === "webp" && raw.format === "webp") {
2850
+ const result = writeWebpMetadata(data, raw.segments);
2851
+ if (!result.ok) {
2852
+ return {
2853
+ ok: false,
2854
+ error: { type: "writeFailed", message: result.error.type }
2855
+ };
2856
+ }
2857
+ return { ok: true, value: result.value };
2858
+ }
2859
+ return {
2860
+ ok: false,
2861
+ error: {
2862
+ type: "writeFailed",
2863
+ message: "Internal error: format mismatch after conversion"
2864
+ }
2865
+ };
2865
2866
  }
2866
2867
  var HELPERS2 = {
2867
2868
  png: {