Skip to content

Commit bfc2891

Browse files
wardpeetGatsbyJS Bot
authored and
GatsbyJS Bot
committed
feat(gatsby-plugin-sharp): enable gatsby cloud processing (#15384)
* feat(gatsby-plugin-sharp): enable gatsby cloud processing * fix snapshot * Downgrade got to version 8.3.2 to support node 8.0.0 * remove debug info
1 parent bb31a34 commit bfc2891

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

packages/gatsby-plugin-sharp/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"async": "^2.1.2",
1212
"bluebird": "^3.5.0",
1313
"fs-extra": "^7.0.0",
14+
"got": "^8.3.2",
1415
"imagemin": "^6.0.0",
1516
"imagemin-mozjpeg": "^8.0.0",
1617
"imagemin-pngquant": "^6.0.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
`;

packages/gatsby-plugin-sharp/src/__tests__/process-file.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
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`)
29

310
describe(`createArgsDigest`, () => {
411
const defaultArgsBaseline = {
@@ -116,3 +123,28 @@ describe(`createArgsDigest`, () => {
116123
})
117124
})
118125
})
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+
})

packages/gatsby-plugin-sharp/src/process-file.js

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const imageminPngquant = require(`imagemin-pngquant`)
88
const imageminWebp = require(`imagemin-webp`)
99
const _ = require(`lodash`)
1010
const crypto = require(`crypto`)
11+
const got = require(`got`)
1112

1213
// Try to enable the use of SIMD instructions. Seems to provide a smallish
1314
// speedup on resizing heavy loads (~10%). Sharp disables this feature by
@@ -75,6 +76,31 @@ const argsWhitelist = [
7576
exports.processFile = (file, transforms, options = {}) => {
7677
let pipeline
7778
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+
78104
pipeline = sharp(file)
79105

80106
// Keep Metadata

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -10098,7 +10098,7 @@ got@^7.0.0, got@^7.1.0:
1009810098
url-parse-lax "^1.0.0"
1009910099
url-to-options "^1.0.1"
1010010100

10101-
got@^8.0.0, got@^8.3.1:
10101+
got@^8.0.0, got@^8.3.1, got@^8.3.2:
1010210102
version "8.3.2"
1010310103
resolved "https://registry.yarnpkg.com/got/-/got-8.3.2.tgz#1d23f64390e97f776cac52e5b936e5f514d2e937"
1010410104
dependencies:

0 commit comments

Comments
 (0)