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

fix(contentful): pass reporter to retry function #31608

Merged
merged 2 commits into from
May 28, 2021
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
13 changes: 8 additions & 5 deletions packages/gatsby-source-contentful/src/cache-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const downloadWithRetry = require(`./download-with-retry`).default

const inFlightImageCache = new Map()

module.exports = async function cacheImage(store, image, options) {
module.exports = async function cacheImage(store, image, options, reporter) {
const program = store.getState().program
const CACHE_DIR = resolve(`${program.directory}/.cache/contentful/assets/`)
const {
Expand Down Expand Up @@ -59,10 +59,13 @@ module.exports = async function cacheImage(store, image, options) {
const downloadPromise = new Promise((resolve, reject) => {
const previewUrl = `http:${url}?${params.join(`&`)}`

downloadWithRetry({
url: previewUrl,
responseType: `stream`,
})
downloadWithRetry(
{
url: previewUrl,
responseType: `stream`,
},
reporter
)
.then(response => {
const file = createWriteStream(absolutePath)
response.data.pipe(file)
Expand Down
50 changes: 32 additions & 18 deletions packages/gatsby-source-contentful/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const isImage = image =>
)

// Note: this may return a Promise<body>, body (sync), or null
const getBase64Image = imageProps => {
const getBase64Image = (imageProps, reporter) => {
if (!imageProps) {
return null
}
Expand Down Expand Up @@ -111,10 +111,13 @@ const getBase64Image = imageProps => {
}

const loadImage = async () => {
const imageResponse = await downloadWithRetry({
url: requestUrl,
responseType: `arraybuffer`,
})
const imageResponse = await downloadWithRetry(
{
url: requestUrl,
responseType: `arraybuffer`,
},
reporter
)

const base64 = Buffer.from(imageResponse.data, `binary`).toString(`base64`)

Expand Down Expand Up @@ -470,14 +473,14 @@ const resolveResize = (image, options) => {

exports.resolveResize = resolveResize

const fixedNodeType = ({ name, getTracedSVG }) => {
const fixedNodeType = ({ name, getTracedSVG, reporter }) => {
return {
type: new GraphQLObjectType({
name: name,
fields: {
base64: {
type: GraphQLString,
resolve: getBase64Image,
resolve: imageProps => getBase64Image(imageProps, reporter),
},
tracedSVG: {
type: GraphQLString,
Expand Down Expand Up @@ -565,14 +568,14 @@ const fixedNodeType = ({ name, getTracedSVG }) => {
}
}

const fluidNodeType = ({ name, getTracedSVG }) => {
const fluidNodeType = ({ name, getTracedSVG, reporter }) => {
return {
type: new GraphQLObjectType({
name: name,
fields: {
base64: {
type: GraphQLString,
resolve: getBase64Image,
resolve: imageProps => getBase64Image(imageProps, reporter),
},
tracedSVG: {
type: GraphQLString,
Expand Down Expand Up @@ -662,7 +665,7 @@ const fluidNodeType = ({ name, getTracedSVG }) => {
}
}

exports.extendNodeType = ({ type, store }) => {
exports.extendNodeType = ({ type, store, reporter }) => {
if (type.name !== `ContentfulAsset`) {
return {}
}
Expand All @@ -679,7 +682,7 @@ exports.extendNodeType = ({ type, store }) => {
return null
}

const absolutePath = await cacheImage(store, image, options)
const absolutePath = await cacheImage(store, image, options, reporter)
const extension = path.extname(absolutePath)

return traceSVG({
Expand All @@ -696,7 +699,7 @@ exports.extendNodeType = ({ type, store }) => {

const getDominantColor = async ({ image, options }) => {
try {
const absolutePath = await cacheImage(store, image, options)
const absolutePath = await cacheImage(store, image, options, reporter)

const pluginSharp = require(`gatsby-plugin-sharp`)
if (!(`getDominantColor` in pluginSharp)) {
Expand Down Expand Up @@ -748,9 +751,12 @@ exports.extendNodeType = ({ type, store }) => {
}

if (options.placeholder === `blurred`) {
placeholderDataURI = await getBase64Image({
baseUrl,
})
placeholderDataURI = await getBase64Image(
{
baseUrl,
},
reporter
)
}

if (options.placeholder === `tracedSVG`) {
Expand All @@ -767,9 +773,17 @@ exports.extendNodeType = ({ type, store }) => {
return imageProps
}

const fixedNode = fixedNodeType({ name: `ContentfulFixed`, getTracedSVG })
const fixedNode = fixedNodeType({
name: `ContentfulFixed`,
getTracedSVG,
reporter,
})

const fluidNode = fluidNodeType({ name: `ContentfulFluid`, getTracedSVG })
const fluidNode = fluidNodeType({
name: `ContentfulFluid`,
getTracedSVG,
reporter,
})

// gatsby-plugin-image
const getGatsbyImageData = () => {
Expand Down Expand Up @@ -841,7 +855,7 @@ exports.extendNodeType = ({ type, store }) => {
fields: {
base64: {
type: GraphQLString,
resolve: getBase64Image,
resolve: imageProps => getBase64Image(imageProps, reporter),
},
tracedSVG: {
type: GraphQLString,
Expand Down