@enslo/sd-metadata 1.4.2 → 1.5.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
@@ -117,9 +117,7 @@ fileInput.addEventListener('change', async (e) => {
117
117
  if (!file) return;
118
118
 
119
119
  const arrayBuffer = await file.arrayBuffer();
120
- const imageData = new Uint8Array(arrayBuffer);
121
-
122
- const result = read(imageData);
120
+ const result = read(arrayBuffer);
123
121
 
124
122
  if (result.status === 'success') {
125
123
  document.getElementById('tool').textContent = result.metadata.software;
@@ -137,12 +135,10 @@ fileInput.addEventListener('change', async (e) => {
137
135
  // CDNからインポート
138
136
  import { read } from 'https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@latest/dist/index.js';
139
137
 
140
- // 画像を取得してメタデータを読み込み
141
138
  const response = await fetch(imageUrl);
142
139
  const arrayBuffer = await response.arrayBuffer();
143
- const imageData = new Uint8Array(arrayBuffer);
140
+ const result = read(arrayBuffer);
144
141
 
145
- const result = read(imageData);
146
142
  if (result.status === 'success') {
147
143
  console.log('Tool:', result.metadata.software);
148
144
  console.log('Prompt:', result.metadata.prompt);
@@ -153,7 +149,7 @@ if (result.status === 'success') {
153
149
  > 本番環境では `@latest` の代わりに特定のバージョンを指定してください:
154
150
  >
155
151
  > ```text
156
- > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.4.1/dist/index.js
152
+ > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.5.0/dist/index.js
157
153
  > ```
158
154
 
159
155
  ### 応用例
@@ -335,7 +331,7 @@ if (result.status === 'success') {
335
331
 
336
332
  ## APIリファレンス
337
333
 
338
- ### `read(data: Uint8Array): ParseResult`
334
+ ### `read(input: Uint8Array | ArrayBuffer): ParseResult`
339
335
 
340
336
  画像ファイルからメタデータを読み込み、パースします。
341
337
 
@@ -350,13 +346,13 @@ if (result.status === 'success') {
350
346
  - `{ status: 'invalid', message? }` - 破損または非対応の画像フォーマット
351
347
  - `message`: オプションのエラー説明
352
348
 
353
- ### `write(data: Uint8Array, metadata: ParseResult): WriteResult`
349
+ ### `write(input: Uint8Array | ArrayBuffer, metadata: ParseResult): WriteResult`
354
350
 
355
351
  画像ファイルにメタデータを書き込みます。
356
352
 
357
353
  **パラメータ:**
358
354
 
359
- - `data` - ターゲット画像ファイルデータ(PNG、JPEG、またはWebP)
355
+ - `input` - ターゲット画像ファイルデータ(PNG、JPEG、またはWebP)
360
356
  - `metadata` - `read()` から得られた `ParseResult`
361
357
  - `status: 'success'` または `'empty'` - 直接書き込み可能
362
358
  - `status: 'unrecognized'` - 同じフォーマット:そのまま書き込み、異なるフォーマット:warning付きでメタデータ削除
@@ -370,13 +366,13 @@ if (result.status === 'success') {
370
366
  - `'conversionFailed'`: メタデータ変換に失敗(例:互換性のないフォーマット)
371
367
  - `'writeFailed'`: 画像へのメタデータ埋め込みに失敗
372
368
 
373
- ### `writeAsWebUI(data: Uint8Array, metadata: GenerationMetadata): WriteResult`
369
+ ### `writeAsWebUI(input: Uint8Array | ArrayBuffer, metadata: GenerationMetadata): WriteResult`
374
370
 
375
371
  SD WebUI (A1111) フォーマットで画像にメタデータを書き込みます。
376
372
 
377
373
  **パラメータ:**
378
374
 
379
- - `data` - ターゲット画像ファイルデータ(PNG、JPEG、またはWebP)
375
+ - `input` - ターゲット画像ファイルデータ(PNG、JPEG、またはWebP)
380
376
  - `metadata` - 埋め込む生成メタデータ
381
377
  - 任意のツールからでも、カスタム作成でも可能
382
378
  - 自動的にWebUIフォーマットに変換される
package/README.md CHANGED
@@ -93,7 +93,6 @@ const { read } = require('@enslo/sd-metadata');
93
93
  import { read, write } from '@enslo/sd-metadata';
94
94
  import { readFileSync, writeFileSync } from 'fs';
95
95
 
96
- // Read metadata from any supported format
97
96
  const imageData = readFileSync('image.png');
98
97
  const result = read(imageData);
99
98
 
@@ -117,9 +116,7 @@ fileInput.addEventListener('change', async (e) => {
117
116
  if (!file) return;
118
117
 
119
118
  const arrayBuffer = await file.arrayBuffer();
120
- const imageData = new Uint8Array(arrayBuffer);
121
-
122
- const result = read(imageData);
119
+ const result = read(arrayBuffer);
123
120
 
124
121
  if (result.status === 'success') {
125
122
  document.getElementById('tool').textContent = result.metadata.software;
@@ -137,12 +134,10 @@ For bookmarklets or userscripts (Tampermonkey, Violentmonkey, etc.), load from j
137
134
  // Import from CDN
138
135
  import { read } from 'https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@latest/dist/index.js';
139
136
 
140
- // Fetch image and read metadata
141
137
  const response = await fetch(imageUrl);
142
138
  const arrayBuffer = await response.arrayBuffer();
143
- const imageData = new Uint8Array(arrayBuffer);
139
+ const result = read(arrayBuffer);
144
140
 
145
- const result = read(imageData);
146
141
  if (result.status === 'success') {
147
142
  console.log('Tool:', result.metadata.software);
148
143
  console.log('Prompt:', result.metadata.prompt);
@@ -153,7 +148,7 @@ if (result.status === 'success') {
153
148
  > For production use, pin to a specific version instead of `@latest`:
154
149
  >
155
150
  > ```text
156
- > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.4.2/dist/index.js
151
+ > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.5.0/dist/index.js
157
152
  > ```
158
153
 
159
154
  ### Advanced Examples
@@ -335,7 +330,7 @@ if (result.status === 'success') {
335
330
 
336
331
  ## API Reference
337
332
 
338
- ### `read(data: Uint8Array): ParseResult`
333
+ ### `read(input: Uint8Array | ArrayBuffer): ParseResult`
339
334
 
340
335
  Reads and parses metadata from an image file.
341
336
 
@@ -350,13 +345,13 @@ Reads and parses metadata from an image file.
350
345
  - `{ status: 'invalid', message? }` - Corrupted or unsupported image format
351
346
  - `message`: Optional error description
352
347
 
353
- ### `write(data: Uint8Array, metadata: ParseResult): WriteResult`
348
+ ### `write(input: Uint8Array | ArrayBuffer, metadata: ParseResult): WriteResult`
354
349
 
355
350
  Writes metadata to an image file.
356
351
 
357
352
  **Parameters:**
358
353
 
359
- - `data` - Target image file data (PNG, JPEG, or WebP)
354
+ - `input` - Target image file data (PNG, JPEG, or WebP)
360
355
  - `metadata` - `ParseResult` from `read()`
361
356
  - `status: 'success'` or `'empty'` - Can write directly
362
357
  - `status: 'unrecognized'` - Same format: writes as-is; Cross-format: drops metadata with warning
@@ -370,13 +365,13 @@ Writes metadata to an image file.
370
365
  - `'conversionFailed'`: Metadata conversion failed (e.g., incompatible format)
371
366
  - `'writeFailed'`: Failed to embed metadata into the image
372
367
 
373
- ### `writeAsWebUI(data: Uint8Array, metadata: GenerationMetadata): WriteResult`
368
+ ### `writeAsWebUI(input: Uint8Array | ArrayBuffer, metadata: GenerationMetadata): WriteResult`
374
369
 
375
370
  Writes metadata to an image in SD WebUI (A1111) format.
376
371
 
377
372
  **Parameters:**
378
373
 
379
- - `data` - Target image file data (PNG, JPEG, or WebP)
374
+ - `input` - Target image file data (PNG, JPEG, or WebP)
380
375
  - `metadata` - Generation metadata to embed
381
376
  - Can be from any tool or custom-created
382
377
  - Automatically converted to WebUI format
package/dist/index.d.ts CHANGED
@@ -300,10 +300,10 @@ type ParseResult = {
300
300
  * Automatically detects the image format (PNG, JPEG, WebP) and parses
301
301
  * any embedded generation metadata.
302
302
  *
303
- * @param data - Image file data
303
+ * @param input - Image file data (Uint8Array or ArrayBuffer)
304
304
  * @returns Parse result containing metadata and raw data
305
305
  */
306
- declare function read(data: Uint8Array): ParseResult;
306
+ declare function read(input: Uint8Array | ArrayBuffer): ParseResult;
307
307
 
308
308
  /**
309
309
  * Write API for sd-metadata
@@ -351,11 +351,11 @@ type WriteResult = {
351
351
  * if necessary. For unrecognized metadata with cross-format conversion,
352
352
  * metadata is dropped and a warning is returned.
353
353
  *
354
- * @param data - Target image file data
354
+ * @param input - Target image file data (Uint8Array or ArrayBuffer)
355
355
  * @param metadata - ParseResult from `read()`
356
356
  * @returns New image data with embedded metadata (or warning if metadata was dropped)
357
357
  */
358
- declare function write(data: Uint8Array, metadata: ParseResult): WriteResult;
358
+ declare function write(input: Uint8Array | ArrayBuffer, metadata: ParseResult): WriteResult;
359
359
 
360
360
  /**
361
361
  * WebUI (A1111) format writer for sd-metadata
@@ -377,7 +377,7 @@ declare function write(data: Uint8Array, metadata: ParseResult): WriteResult;
377
377
  * - PNG: `parameters` tEXt/iTXt chunk (encoding auto-selected based on content)
378
378
  * - JPEG/WebP: Exif UserComment field
379
379
  *
380
- * @param data - Target image file data (PNG, JPEG, or WebP)
380
+ * @param input - Target image file data (Uint8Array or ArrayBuffer)
381
381
  * @param metadata - Generation metadata to embed
382
382
  * @returns New image data with embedded metadata, or error
383
383
  *
@@ -403,7 +403,7 @@ declare function write(data: Uint8Array, metadata: ParseResult): WriteResult;
403
403
  * }
404
404
  * ```
405
405
  */
406
- declare function writeAsWebUI(data: Uint8Array, metadata: GenerationMetadata): WriteResult;
406
+ declare function writeAsWebUI(input: Uint8Array | ArrayBuffer, metadata: GenerationMetadata): WriteResult;
407
407
 
408
408
  /**
409
409
  * A1111-format metadata serialization utilities
package/dist/index.js CHANGED
@@ -1030,6 +1030,9 @@ function parseMetadata(entries) {
1030
1030
  }
1031
1031
 
1032
1032
  // src/utils/binary.ts
1033
+ function toUint8Array(input) {
1034
+ return input instanceof ArrayBuffer ? new Uint8Array(input) : input;
1035
+ }
1033
1036
  function readUint24LE(data, offset) {
1034
1037
  return (data[offset] ?? 0) | (data[offset + 1] ?? 0) << 8 | (data[offset + 2] ?? 0) << 16;
1035
1038
  }
@@ -1641,7 +1644,8 @@ function sourceToKeyword(source) {
1641
1644
  }
1642
1645
 
1643
1646
  // src/api/read.ts
1644
- function read(data) {
1647
+ function read(input) {
1648
+ const data = toUint8Array(input);
1645
1649
  const format = detectFormat(data);
1646
1650
  if (!format) {
1647
1651
  return { status: "invalid", message: "Unknown image format" };
@@ -2715,7 +2719,8 @@ function buildExifChunk(segments) {
2715
2719
  }
2716
2720
 
2717
2721
  // src/api/write.ts
2718
- function write(data, metadata) {
2722
+ function write(input, metadata) {
2723
+ const data = toUint8Array(input);
2719
2724
  const targetFormat = detectFormat(data);
2720
2725
  if (!targetFormat) {
2721
2726
  return { ok: false, error: { type: "unsupportedFormat" } };
@@ -2915,7 +2920,8 @@ function formatAsWebUI(metadata) {
2915
2920
  }
2916
2921
 
2917
2922
  // src/api/write-webui.ts
2918
- function writeAsWebUI(data, metadata) {
2923
+ function writeAsWebUI(input, metadata) {
2924
+ const data = toUint8Array(input);
2919
2925
  const format = detectFormat(data);
2920
2926
  if (!format) {
2921
2927
  return Result.error({ type: "unsupportedFormat" });