@@ -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 */
4543export 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 */
6977export 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 */
89107export 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
0 commit comments