@tryghost/image-transform 1.2.10 → 1.3.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/lib/transform.js +18 -5
- package/package.json +5 -5
package/lib/transform.js
CHANGED
|
@@ -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,10 +108,19 @@ 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});
|
|
113
|
+
|
|
114
|
+
const metadata = await s.metadata();
|
|
109
115
|
|
|
110
116
|
if (options.format) {
|
|
111
|
-
|
|
117
|
+
if (options.format === 'jpeg') {
|
|
118
|
+
s.jpeg({mozjpeg: true}); // .jpeg sets format
|
|
119
|
+
} else {
|
|
120
|
+
s = s.toFormat(options.format);
|
|
121
|
+
}
|
|
122
|
+
} else if (metadata.format === 'jpeg') {
|
|
123
|
+
s.jpeg({mozjpeg: true}); // .jpeg sets format
|
|
112
124
|
}
|
|
113
125
|
|
|
114
126
|
const resizedBuffer = await s.toBuffer();
|
|
@@ -154,3 +166,4 @@ module.exports.canTransformToFormat = canTransformToFormat;
|
|
|
154
166
|
module.exports.generateOriginalImageName = generateOriginalImageName;
|
|
155
167
|
module.exports.resizeFromPath = makeSafe(unsafeResizeFromPath);
|
|
156
168
|
module.exports.resizeFromBuffer = makeSafe(unsafeResizeFromBuffer);
|
|
169
|
+
module.exports.DEFAULT_PROCESSING_TIMEOUT_SECONDS = DEFAULT_PROCESSING_TIMEOUT_SECONDS;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/image-transform",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"repository": "https://github.com/TryGhost/SDK/tree/main/packages/image-transform",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"c8": "
|
|
23
|
-
"mocha": "10.
|
|
22
|
+
"c8": "9.1.0",
|
|
23
|
+
"mocha": "10.4.0",
|
|
24
24
|
"should": "13.2.3",
|
|
25
|
-
"sinon": "17.0.
|
|
25
|
+
"sinon": "17.0.1"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@tryghost/errors": "^1.2.26",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"optionalDependencies": {
|
|
32
32
|
"sharp": "^0.32.0"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "048ccde4bd78d2dcd60e778d03eb8dc3227cece5"
|
|
35
35
|
}
|