File tree 5 files changed +102
-2
lines changed
packages/gatsby-plugin-sharp
5 files changed +102
-2
lines changed Original file line number Diff line number Diff line change 11
11
"async" : " ^2.1.2" ,
12
12
"bluebird" : " ^3.5.0" ,
13
13
"fs-extra" : " ^7.0.0" ,
14
+ "got" : " ^8.3.2" ,
14
15
"imagemin" : " ^6.0.0" ,
15
16
"imagemin-mozjpeg" : " ^8.0.0" ,
16
17
"imagemin-pngquant" : " ^6.0.0" ,
Original file line number Diff line number Diff line change
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports [` processFile should offload sharp transforms to the cloud 1` ] = `
4
+ Array [
5
+ Promise { } ,
6
+ ]
7
+ ` ;
8
+
9
+ exports [` processFile should offload sharp transforms to the cloud 2` ] = `
10
+ [MockFunction] {
11
+ " calls" : Array [
12
+ Array [
13
+ " https://example.com/image-service" ,
14
+ Object {
15
+ " body" : Object {
16
+ " file" : " mypath/file.jpg" ,
17
+ " options" : Object {
18
+ " stripMetadata" : true ,
19
+ },
20
+ " transforms" : Array [
21
+ Object {
22
+ " args" : Object {
23
+ " height" : 100 ,
24
+ " width" : 100 ,
25
+ },
26
+ " outputPath" : " myoutputpath/1234/file.jpg" ,
27
+ },
28
+ ],
29
+ },
30
+ " json" : true ,
31
+ },
32
+ ],
33
+ ],
34
+ " results" : Array [
35
+ Object {
36
+ " type" : " return" ,
37
+ " value" : Promise {},
38
+ },
39
+ ],
40
+ }
41
+ ` ;
Original file line number Diff line number Diff line change 1
- const { createArgsDigest, sortKeys } = require ( `../process-file` )
1
+ jest . mock ( `got` )
2
+ jest . mock ( `../safe-sharp` , ( ) => {
3
+ return {
4
+ simd : jest . fn ( ) ,
5
+ }
6
+ } )
7
+ const { createArgsDigest, processFile, sortKeys } = require ( `../process-file` )
8
+ const got = require ( `got` )
2
9
3
10
describe ( `createArgsDigest` , ( ) => {
4
11
const defaultArgsBaseline = {
@@ -116,3 +123,28 @@ describe(`createArgsDigest`, () => {
116
123
} )
117
124
} )
118
125
} )
126
+
127
+ describe ( `processFile` , ( ) => {
128
+ it ( `should offload sharp transforms to the cloud` , async ( ) => {
129
+ process . env . GATSBY_CLOUD_IMAGE_SERVICE_URL = `https://example.com/image-service`
130
+ const transforms = {
131
+ outputPath : `myoutputpath/1234/file.jpg` ,
132
+ args : {
133
+ width : 100 ,
134
+ height : 100 ,
135
+ } ,
136
+ }
137
+
138
+ got . post . mockImplementation ( jest . fn ( ( ) => Promise . resolve ( ) ) )
139
+
140
+ expect (
141
+ await processFile ( `mypath/file.jpg` , [ transforms ] , {
142
+ stripMetadata : true ,
143
+ } )
144
+ ) . toMatchSnapshot ( )
145
+ expect ( got . post ) . toHaveBeenCalled ( )
146
+ expect ( got . post ) . toMatchSnapshot ( )
147
+
148
+ delete process . env . GATSBY_CLOUD_IMAGE_SERVICE_URL
149
+ } )
150
+ } )
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ const imageminPngquant = require(`imagemin-pngquant`)
8
8
const imageminWebp = require ( `imagemin-webp` )
9
9
const _ = require ( `lodash` )
10
10
const crypto = require ( `crypto` )
11
+ const got = require ( `got` )
11
12
12
13
// Try to enable the use of SIMD instructions. Seems to provide a smallish
13
14
// speedup on resizing heavy loads (~10%). Sharp disables this feature by
@@ -75,6 +76,31 @@ const argsWhitelist = [
75
76
exports . processFile = ( file , transforms , options = { } ) => {
76
77
let pipeline
77
78
try {
79
+ // adds gatsby cloud image service to gatsby-sharp
80
+ // this is an experimental api so it can be removed without any warnings
81
+ if ( process . env . GATSBY_CLOUD_IMAGE_SERVICE_URL ) {
82
+ let cloudPromise
83
+
84
+ return transforms . map ( transform => {
85
+ if ( ! cloudPromise ) {
86
+ cloudPromise = got
87
+ . post ( process . env . GATSBY_CLOUD_IMAGE_SERVICE_URL , {
88
+ body : {
89
+ file,
90
+ transforms,
91
+ options,
92
+ } ,
93
+ json : true ,
94
+ } )
95
+ . then ( ( ) => transform )
96
+
97
+ return cloudPromise
98
+ }
99
+
100
+ return Promise . resolve ( transform )
101
+ } )
102
+ }
103
+
78
104
pipeline = sharp ( file )
79
105
80
106
// Keep Metadata
Original file line number Diff line number Diff line change @@ -10098,7 +10098,7 @@ got@^7.0.0, got@^7.1.0:
10098
10098
url-parse-lax "^1.0.0"
10099
10099
url-to-options "^1.0.1"
10100
10100
10101
- got@^8.0.0, got@^8.3.1:
10101
+ got@^8.0.0, got@^8.3.1, got@^8.3.2 :
10102
10102
version "8.3.2"
10103
10103
resolved "https://registry.yarnpkg.com/got/-/got-8.3.2.tgz#1d23f64390e97f776cac52e5b936e5f514d2e937"
10104
10104
dependencies:
You can’t perform that action at this time.
0 commit comments