Skip to content

Commit 6a31150

Browse files
mtuchiclaude
andcommitted
image-utils: rename base64 option to parseAs
Replace { base64: true } with { parseAs: 'base64' } on resize, compress, and strip to match the parseAs convention used in language-common http. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 0767604 commit 6a31150

2 files changed

Lines changed: 47 additions & 22 deletions

File tree

packages/image-utils/src/Adaptor.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ function resolveInput(raw) {
1212
return Buffer.isBuffer(raw) ? raw : decodeBase64Image(raw);
1313
}
1414

15-
// util.encode from language-common is text/JSON-oriented (utf-8 encoding).
16-
// For binary image buffers we encode directly with buffer.toString('base64').
17-
function applyBase64Option(result, encode) {
18-
if (!encode) return result;
15+
function applyParseAs(result, parseAs) {
16+
if (parseAs !== 'base64') return result;
1917
const { buffer, ...rest } = result;
2018
return { ...rest, base64: buffer.toString('base64') };
2119
}
@@ -38,15 +36,25 @@ function applyBase64Option(result, encode) {
3836
* @param {object} [options={}]
3937
* @param {number} [options.width=1200] - Output width in pixels
4038
* @param {number} [options.height=1600] - Output height in pixels
41-
* @param {boolean} [options.base64=false] - When true, returns `{ base64, width, height }` instead of `{ buffer, width, height }`
39+
* @param {'buffer'|'base64'} [options.parseAs='buffer'] - Return format: `'buffer'` (default) or `'base64'`
4240
* @state {ImageState}
4341
* @returns {Operation}
4442
*/
4543
export function resize(base64ImgOrBuffer, options = {}) {
4644
return async state => {
47-
const [resolvedImg, resolvedOptions] = expandReferences(state, base64ImgOrBuffer, options);
48-
const result = await resizeImage(resolveInput(resolvedImg), resolvedOptions);
49-
return composeNextState(state, applyBase64Option(result, resolvedOptions.base64));
45+
const [resolvedImg, resolvedOptions] = expandReferences(
46+
state,
47+
base64ImgOrBuffer,
48+
options,
49+
);
50+
const result = await resizeImage(
51+
resolveInput(resolvedImg),
52+
resolvedOptions,
53+
);
54+
return composeNextState(
55+
state,
56+
applyParseAs(result, resolvedOptions.parseAs),
57+
);
5058
};
5159
}
5260

@@ -62,15 +70,25 @@ export function resize(base64ImgOrBuffer, options = {}) {
6270
* @param {number} [options.maxBytes=716800] - Maximum output file size in bytes
6371
* @param {number} [options.minQuality=20] - JPEG quality floor (1–100); compression stops here even if maxBytes is not met
6472
* @param {string} [options.comment] - String to embed in the EXIF UserComment field
65-
* @param {boolean} [options.base64=false] - When true, returns `{ base64, size, quality }` instead of `{ buffer, size, quality }`
73+
* @param {'buffer'|'base64'} [options.parseAs='buffer'] - Return format: `'buffer'` (default) or `'base64'`
6674
* @state {ImageState}
6775
* @returns {Operation}
6876
*/
6977
export function compress(base64ImgOrBuffer, options = {}) {
7078
return async state => {
71-
const [resolvedImg, resolvedOptions] = expandReferences(state, base64ImgOrBuffer, options);
72-
const result = await compressImage(resolveInput(resolvedImg), resolvedOptions);
73-
return composeNextState(state, applyBase64Option(result, resolvedOptions.base64));
79+
const [resolvedImg, resolvedOptions] = expandReferences(
80+
state,
81+
base64ImgOrBuffer,
82+
options,
83+
);
84+
const result = await compressImage(
85+
resolveInput(resolvedImg),
86+
resolvedOptions,
87+
);
88+
return composeNextState(
89+
state,
90+
applyParseAs(result, resolvedOptions.parseAs),
91+
);
7492
};
7593
}
7694

@@ -82,15 +100,22 @@ export function compress(base64ImgOrBuffer, options = {}) {
82100
* @function
83101
* @param {string|Buffer|Function} base64ImgOrBuffer - Base64 string, data URL, Buffer, or resolver fn
84102
* @param {object} [options={}]
85-
* @param {boolean} [options.base64=false] - When true, returns `{ base64 }` instead of `{ buffer }`
103+
* @param {'buffer'|'base64'} [options.parseAs='buffer'] - Return format: `'buffer'` (default) or `'base64'`
86104
* @state {ImageState}
87105
* @returns {Operation}
88106
*/
89107
export function strip(base64ImgOrBuffer, options = {}) {
90108
return async state => {
91-
const [resolvedImg, resolvedOptions] = expandReferences(state, base64ImgOrBuffer, options);
109+
const [resolvedImg, resolvedOptions] = expandReferences(
110+
state,
111+
base64ImgOrBuffer,
112+
options,
113+
);
92114
const result = await stripImage(resolveInput(resolvedImg));
93-
return composeNextState(state, applyBase64Option(result, resolvedOptions.base64));
115+
return composeNextState(
116+
state,
117+
applyParseAs(result, resolvedOptions.parseAs),
118+
);
94119
};
95120
}
96121

packages/image-utils/test/Adaptor.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ describe('resize', () => {
150150
});
151151

152152
describe('base64 option', () => {
153-
it('returns { base64, width, height } when base64: true', async () => {
153+
it("returns { base64, width, height } when parseAs: 'base64'", async () => {
154154
const finalState = await resize(
155155
toBase64Str('portrait-small.jpg'),
156-
{ ...DEFAULT_RESIZE_OPTS, base64: true },
156+
{ ...DEFAULT_RESIZE_OPTS, parseAs: 'base64' },
157157
)(state);
158158

159159
expect(finalState.data).to.have.keys(['base64', 'width', 'height']);
@@ -287,11 +287,11 @@ describe('compress', () => {
287287
});
288288

289289
describe('base64 option', () => {
290-
it('returns { base64, size, quality } when base64: true', async () => {
290+
it("returns { base64, size, quality } when parseAs: 'base64'", async () => {
291291
const inputBuffer = await getResizedBuffer('portrait-small.jpg');
292292
const finalState = await compress(
293293
inputBuffer,
294-
{ ...DEFAULT_COMPRESS_OPTS, base64: true },
294+
{ ...DEFAULT_COMPRESS_OPTS, parseAs: 'base64' },
295295
)(state);
296296

297297
expect(finalState.data).to.have.keys(['base64', 'size', 'quality']);
@@ -356,8 +356,8 @@ describe('strip', () => {
356356
expect(finalState.data.buffer).to.be.instanceOf(Buffer);
357357
});
358358

359-
it('returns { base64 } when base64: true', async () => {
360-
const finalState = await strip(toBase64Str('portrait-small.jpg'), { base64: true })(state);
359+
it("returns { base64 } when parseAs: 'base64'", async () => {
360+
const finalState = await strip(toBase64Str('portrait-small.jpg'), { parseAs: 'base64' })(state);
361361

362362
expect(finalState.data).to.have.keys(['base64']);
363363
expect(finalState.data.base64).to.be.a('string');
@@ -440,7 +440,7 @@ describe('buffer passing between steps', () => {
440440
it('base64 output from resize() can be fed directly into compress()', async () => {
441441
const resizeState = await resize(
442442
toBase64Str('portrait-large.jpg'),
443-
{ ...DEFAULT_RESIZE_OPTS, base64: true },
443+
{ ...DEFAULT_RESIZE_OPTS, parseAs: 'base64' },
444444
)(state);
445445
expect(resizeState.data.base64).to.be.a('string');
446446

0 commit comments

Comments
 (0)