11import * as sharp from 'sharp' ;
22import { Features , IAction , InvalidArgument , IProcessContext , IProcessor , IProcessResponse } from '../../processor' ;
33import { IBufferStore } from '../../store' ;
4+ import { ActionMask } from './_base' ;
45import { AutoOrientAction } from './auto-orient' ;
56import { BlurAction } from './blur' ;
67import { BrightAction } from './bright' ;
@@ -46,14 +47,16 @@ export class ImageProcessor implements IProcessor {
4647 const ctx : IProcessContext = {
4748 uri,
4849 actions,
50+ mask : new ActionMask ( actions ) ,
4951 bufferStore,
5052 features : {
5153 [ Features . AutoOrient ] : true ,
5254 [ Features . ReadAllAnimatedFrames ] : true ,
5355 } ,
5456 headers : { } ,
5557 } ;
56- for ( const action of actions ) {
58+ for ( let i = 0 ; i < actions . length ; i ++ ) {
59+ const action = actions [ i ] ;
5760 if ( ( this . name === action ) || ( ! action ) ) {
5861 continue ;
5962 }
@@ -64,7 +67,7 @@ export class ImageProcessor implements IProcessor {
6467 if ( ! act ) {
6568 throw new InvalidArgument ( `Unkown action: "${ name } "` ) ;
6669 }
67- act . beforeNewContext . bind ( act ) ( ctx , params ) ;
70+ act . beforeNewContext . bind ( act ) ( ctx , params , i ) ;
6871 }
6972 const { buffer, headers } = await bufferStore . get ( uri ) ;
7073 const image = sharp ( buffer , { failOnError : false , animated : ctx . features [ Features . ReadAllAnimatedFrames ] } ) ;
@@ -77,7 +80,7 @@ export class ImageProcessor implements IProcessor {
7780 return {
7881 uri : ctx . uri ,
7982 actions : ctx . actions ,
80- effectiveActions : ctx . effectiveActions ,
83+ mask : ctx . mask ,
8184 bufferStore : ctx . bufferStore ,
8285 features : ctx . features ,
8386 headers : Object . assign ( ctx . headers , headers ) ,
@@ -96,8 +99,28 @@ export class ImageProcessor implements IProcessor {
9699
97100 if ( ctx . features [ Features . AutoOrient ] ) { ctx . image . rotate ( ) ; }
98101
99- const actions = ( ctx . effectiveActions && ctx . effectiveActions . length ) ? ctx . effectiveActions : ctx . actions ;
100- for ( const action of actions ) {
102+ ctx . mask . forEachAction ( ( action , _ , index ) => {
103+ if ( ( this . name === action ) || ( ! action ) ) {
104+ return ;
105+ }
106+ // "<action-name>,<param-1>,<param-2>,..."
107+ const params = action . split ( ',' ) ;
108+ const name = params [ 0 ] ;
109+ const act = this . action ( name ) ;
110+ if ( ! act ) {
111+ throw new InvalidArgument ( `Unkown action: "${ name } "` ) ;
112+ }
113+ act . beforeProcess . bind ( act ) ( ctx , params , index ) ;
114+ } ) ;
115+ const enabledActions = ctx . mask . filterEnabledActions ( ) ;
116+ const nothing2do = ( enabledActions . length === 1 ) && ( this . name === enabledActions [ 0 ] ) ;
117+
118+ if ( nothing2do && ( ! ctx . features [ Features . AutoWebp ] ) ) {
119+ const { buffer } = await ctx . bufferStore . get ( ctx . uri ) ;
120+ return { data : buffer , type : ctx . metadata . format ! } ;
121+ }
122+
123+ for ( const action of enabledActions ) {
101124 if ( ( this . name === action ) || ( ! action ) ) {
102125 continue ;
103126 }
0 commit comments