Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-sharp): enable gatsby cloud processing #15384

Merged
merged 5 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sharp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"async": "^2.1.2",
"bluebird": "^3.5.0",
"fs-extra": "^7.0.0",
"got": "^8.3.2",
"imagemin": "^6.0.0",
"imagemin-mozjpeg": "^8.0.0",
"imagemin-pngquant": "^6.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`processFile should offload sharp transforms to the cloud 1`] = `
Array [
Promise {},
]
`;

exports[`processFile should offload sharp transforms to the cloud 2`] = `
[MockFunction] {
"calls": Array [
Array [
"https://example.com/image-service",
Object {
"body": Object {
"file": "mypath/file.jpg",
"options": Object {
"stripMetadata": true,
},
"transforms": Array [
Object {
"args": Object {
"height": 100,
"width": 100,
},
"outputPath": "myoutputpath/1234/file.jpg",
},
],
},
"json": true,
},
],
],
"results": Array [
Object {
"type": "return",
"value": Promise {},
},
],
}
`;
34 changes: 33 additions & 1 deletion packages/gatsby-plugin-sharp/src/__tests__/process-file.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const { createArgsDigest, sortKeys } = require(`../process-file`)
jest.mock(`got`)
jest.mock(`../safe-sharp`, () => {
return {
simd: jest.fn(),
}
})
const { createArgsDigest, processFile, sortKeys } = require(`../process-file`)
const got = require(`got`)

describe(`createArgsDigest`, () => {
const defaultArgsBaseline = {
Expand Down Expand Up @@ -116,3 +123,28 @@ describe(`createArgsDigest`, () => {
})
})
})

describe(`processFile`, () => {
it(`should offload sharp transforms to the cloud`, async () => {
process.env.GATSBY_CLOUD_IMAGE_SERVICE_URL = `https://example.com/image-service`
const transforms = {
outputPath: `myoutputpath/1234/file.jpg`,
args: {
width: 100,
height: 100,
},
}

got.post.mockImplementation(jest.fn(() => Promise.resolve()))

expect(
await processFile(`mypath/file.jpg`, [transforms], {
stripMetadata: true,
})
).toMatchSnapshot()
expect(got.post).toHaveBeenCalled()
expect(got.post).toMatchSnapshot()

delete process.env.GATSBY_CLOUD_IMAGE_SERVICE_URL
})
})
26 changes: 26 additions & 0 deletions packages/gatsby-plugin-sharp/src/process-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const imageminPngquant = require(`imagemin-pngquant`)
const imageminWebp = require(`imagemin-webp`)
const _ = require(`lodash`)
const crypto = require(`crypto`)
const got = require(`got`)

// Try to enable the use of SIMD instructions. Seems to provide a smallish
// speedup on resizing heavy loads (~10%). Sharp disables this feature by
Expand Down Expand Up @@ -75,6 +76,31 @@ const argsWhitelist = [
exports.processFile = (file, transforms, options = {}) => {
let pipeline
try {
// adds gatsby cloud image service to gatsby-sharp
// this is an experimental api so it can be removed without any warnings
if (process.env.GATSBY_CLOUD_IMAGE_SERVICE_URL) {
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
let cloudPromise

return transforms.map(transform => {
if (!cloudPromise) {
cloudPromise = got
.post(process.env.GATSBY_CLOUD_IMAGE_SERVICE_URL, {
body: {
file,
transforms,
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
options,
},
json: true,
})
.then(() => transform)

return cloudPromise
}

return Promise.resolve(transform)
})
}

pipeline = sharp(file)

// Keep Metadata
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10098,7 +10098,7 @@ got@^7.0.0, got@^7.1.0:
url-parse-lax "^1.0.0"
url-to-options "^1.0.1"

got@^8.0.0, got@^8.3.1:
got@^8.0.0, got@^8.3.1, got@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/got/-/got-8.3.2.tgz#1d23f64390e97f776cac52e5b936e5f514d2e937"
dependencies:
Expand Down