@@ -20,16 +20,36 @@ const duotone = require(`./duotone`)
20
20
const { IMAGE_PROCESSING_JOB_NAME } = require ( `./gatsby-worker` )
21
21
22
22
const imageSizeCache = new Map ( )
23
+
24
+ const getImageSizeAsync = async file => {
25
+ if (
26
+ process . env . NODE_ENV !== `test` &&
27
+ imageSizeCache . has ( file . internal . contentDigest )
28
+ ) {
29
+ return imageSizeCache . get ( file . internal . contentDigest )
30
+ }
31
+ const input = fs . createReadStream ( file . absolutePath )
32
+ const dimensions = await imageSize ( input )
33
+
34
+ if ( ! dimensions ) {
35
+ reportError (
36
+ `gatsby-plugin-sharp couldn't determine dimensions for file:\n${ file . absolutePath } \nThis file is unusable and is most likely corrupt.` ,
37
+ ``
38
+ )
39
+ }
40
+
41
+ imageSizeCache . set ( file . internal . contentDigest , dimensions )
42
+ return dimensions
43
+ }
44
+ // Remove in next major as it's really slow
23
45
const getImageSize = file => {
24
46
if (
25
47
process . env . NODE_ENV !== `test` &&
26
48
imageSizeCache . has ( file . internal . contentDigest )
27
49
) {
28
50
return imageSizeCache . get ( file . internal . contentDigest )
29
51
} else {
30
- const dimensions = imageSize . sync (
31
- toArray ( fs . readFileSync ( file . absolutePath ) )
32
- )
52
+ const dimensions = imageSize . sync ( fs . readFileSync ( file . absolutePath ) )
33
53
34
54
if ( ! dimensions ) {
35
55
reportError (
@@ -657,7 +677,7 @@ async function fixed({ file, args = {}, reporter, cache }) {
657
677
sizes . push ( options [ fixedDimension ] )
658
678
sizes . push ( options [ fixedDimension ] * 1.5 )
659
679
sizes . push ( options [ fixedDimension ] * 2 )
660
- const dimensions = getImageSize ( file )
680
+ const dimensions = await getImageSizeAsync ( file )
661
681
662
682
const filteredSizes = sizes . filter ( size => size <= dimensions [ fixedDimension ] )
663
683
@@ -763,16 +783,6 @@ async function fixed({ file, args = {}, reporter, cache }) {
763
783
}
764
784
}
765
785
766
- function toArray ( buf ) {
767
- var arr = new Array ( buf . length )
768
-
769
- for ( var i = 0 ; i < buf . length ; i ++ ) {
770
- arr [ i ] = buf [ i ]
771
- }
772
-
773
- return arr
774
- }
775
-
776
786
exports . queueImageResizing = queueImageResizing
777
787
exports . resize = queueImageResizing
778
788
exports . base64 = base64
@@ -783,4 +793,5 @@ exports.resolutions = fixed
783
793
exports . fluid = fluid
784
794
exports . fixed = fixed
785
795
exports . getImageSize = getImageSize
796
+ exports . getImageSizeAsync = getImageSizeAsync
786
797
exports . stats = stats
0 commit comments