@@ -2,6 +2,8 @@ const errors = require('@tryghost/errors');
2
2
const fs = require ( 'fs-extra' ) ;
3
3
const path = require ( 'path' ) ;
4
4
5
+ const DEFAULT_PROCESSING_TIMEOUT_SECONDS = 0 ; // 0 means no timeout
6
+
5
7
/**
6
8
* Check if this tool can handle any file transformations as Sharp is an optional dependency
7
9
*/
@@ -58,13 +60,14 @@ const canTransformToFormat = format => [
58
60
* https://github.com/lovell/sharp/issues/1360.
59
61
*
60
62
* Resize an image referenced by the `in` path and write it to the `out` path
61
- * @param {{in, out, width} } options
63
+ * @param {{in, out, width, timeout } } options
62
64
*/
63
65
const unsafeResizeFromPath = ( options = { } ) => {
64
66
return fs . readFile ( options . in )
65
67
. then ( ( data ) => {
66
68
return unsafeResizeFromBuffer ( data , {
67
- width : options . width
69
+ width : options . width ,
70
+ timeout : options . timeout
68
71
} ) ;
69
72
} )
70
73
. then ( ( data ) => {
@@ -76,7 +79,7 @@ const unsafeResizeFromPath = (options = {}) => {
76
79
* Resize an image
77
80
*
78
81
* @param {Buffer } originalBuffer image to resize
79
- * @param {{width?: number, height?: number, format?: keyof import('sharp').FormatEnum, animated?: boolean, withoutEnlargement?: boolean} } [options]
82
+ * @param {{width?: number, height?: number, format?: keyof import('sharp').FormatEnum, animated?: boolean, withoutEnlargement?: boolean, timeout:? number } } [options]
80
83
* options.animated defaults to true for file formats where animation is supported (will always maintain animation if possible)
81
84
* @returns {Promise<Buffer> } the resizedBuffer
82
85
*/
@@ -105,7 +108,8 @@ const unsafeResizeFromBuffer = async (originalBuffer, options = {}) => {
105
108
withoutEnlargement : options . withoutEnlargement ?? true
106
109
} )
107
110
// CASE: Automatically remove metadata and rotate based on the orientation.
108
- . rotate ( ) ;
111
+ . rotate ( )
112
+ . timeout ( { seconds : options . timeout || DEFAULT_PROCESSING_TIMEOUT_SECONDS } ) ;
109
113
110
114
const metadata = await s . metadata ( ) ;
111
115
@@ -162,3 +166,4 @@ module.exports.canTransformToFormat = canTransformToFormat;
162
166
module . exports . generateOriginalImageName = generateOriginalImageName ;
163
167
module . exports . resizeFromPath = makeSafe ( unsafeResizeFromPath ) ;
164
168
module . exports . resizeFromBuffer = makeSafe ( unsafeResizeFromBuffer ) ;
169
+ module . exports . DEFAULT_PROCESSING_TIMEOUT_SECONDS = DEFAULT_PROCESSING_TIMEOUT_SECONDS ;
0 commit comments