@enslo/sd-metadata 1.3.0 → 1.4.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/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.1/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.1/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
  */
@@ -31,6 +14,10 @@ type MetadataSegmentSource = {
31
14
  } | {
32
15
  type: 'exifMake';
33
16
  prefix?: string;
17
+ } | {
18
+ type: 'exifSoftware';
19
+ } | {
20
+ type: 'exifDocumentName';
34
21
  } | {
35
22
  type: 'jpegCom';
36
23
  };
@@ -330,9 +317,16 @@ declare function read(data: Uint8Array): ParseResult;
330
317
  */
331
318
 
332
319
  /**
333
- * Result of the write operation
320
+ * Warning types for write operations
321
+ */
322
+ type WriteWarning = {
323
+ type: 'metadataDropped';
324
+ reason: 'unrecognizedCrossFormat';
325
+ };
326
+ /**
327
+ * Error types for write operations
334
328
  */
335
- type WriteResult = Result<Uint8Array, {
329
+ type WriteError = {
336
330
  type: 'unsupportedFormat';
337
331
  } | {
338
332
  type: 'conversionFailed';
@@ -340,36 +334,32 @@ type WriteResult = Result<Uint8Array, {
340
334
  } | {
341
335
  type: 'writeFailed';
342
336
  message: string;
343
- }>;
337
+ };
344
338
  /**
345
- * Options for write operation
339
+ * Result of the write operation
340
+ *
341
+ * Success case may include a warning when metadata was intentionally dropped.
346
342
  */
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
- }
343
+ type WriteResult = {
344
+ ok: true;
345
+ value: Uint8Array;
346
+ warning?: WriteWarning;
347
+ } | {
348
+ ok: false;
349
+ error: WriteError;
350
+ };
361
351
  /**
362
352
  * Write metadata to an image
363
353
  *
364
354
  * Automatically detects the target image format and converts the metadata
365
- * if necessary.
355
+ * if necessary. For unrecognized metadata with cross-format conversion,
356
+ * metadata is dropped and a warning is returned.
366
357
  *
367
358
  * @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
359
+ * @param metadata - ParseResult from `read()`
360
+ * @returns New image data with embedded metadata (or warning if metadata was dropped)
371
361
  */
372
- declare function write(data: Uint8Array, metadata: ParseResult, options?: WriteOptions): WriteResult;
362
+ declare function write(data: Uint8Array, metadata: ParseResult): WriteResult;
373
363
 
374
364
  /**
375
365
  * WebUI (A1111) format writer for sd-metadata
@@ -491,4 +481,4 @@ declare function formatAsWebUI(metadata: GenerationMetadata): string;
491
481
  */
492
482
  declare function formatRaw(raw: RawMetadata): string;
493
483
 
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 };
484
+ 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 };