gatsby-source-prismic
Version:
Gatsby source plugin for building websites using Prismic as a data source
1 lines • 8.55 kB
Source Map (JSON)
{"version":3,"file":"resolveGatsbyImageData.server.cjs","sources":["../../../src/lib/resolveGatsbyImageData.server.ts"],"sourcesContent":["import { Buffer } from \"buffer\";\nimport type { GatsbyCache } from \"gatsby\";\nimport type {\n\tIGatsbyImageData,\n\tIGatsbyImageHelperArgs,\n\tImageFormat,\n} from \"gatsby-plugin-image\";\nimport {\n\tgenerateImageData,\n\tgetLowResolutionImageURL,\n} from \"gatsby-plugin-image\";\nimport type { ImgixURLParams } from \"imgix-url-builder\";\nimport { buildURL } from \"imgix-url-builder\";\nimport PQueue from \"p-queue\";\nimport { extname } from \"path\";\n\nimport { name as packageName } from \"../../package.json\";\n\nimport {\n\tDEFAULT_IMGIX_PARAMS,\n\tGatsbyImageDataLayoutKind,\n\tGatsbyImageDataPlaceholderKind,\n} from \"../constants\";\n\nconst imgixRequestQueue = new PQueue({ concurrency: 5 });\n\nexport const generateImageSource: IGatsbyImageHelperArgs[\"generateImageSource\"] =\n\t(sourceUrl, width, height, format, _fit, options?: GatsbyImageDataArgs) => {\n\t\tconst imgixParams: ImgixURLParams = {\n\t\t\t...DEFAULT_IMGIX_PARAMS,\n\t\t\t...options?.imgixParams,\n\t\t\tw: width,\n\t\t\th: height,\n\t\t};\n\n\t\tif (format && format !== \"auto\") {\n\t\t\timgixParams.fm = format;\n\t\t}\n\n\t\treturn {\n\t\t\tsrc: buildURL(sourceUrl, imgixParams),\n\t\t\twidth,\n\t\t\theight,\n\t\t\tformat,\n\t\t};\n\t};\n\ntype FetchBase64ImageConfig = {\n\turl: string;\n\tcache: GatsbyCache;\n};\n\nconst fetchBase64Image = async (\n\tconfig: FetchBase64ImageConfig,\n): Promise<string | undefined> => {\n\tconst cacheKey = `base64___${config.url}`;\n\tconst cacheValue: string | undefined = await config.cache.get(cacheKey);\n\n\tif (cacheValue) {\n\t\treturn cacheValue;\n\t} else {\n\t\tconst fetch = (await import(\"node-fetch\")).default;\n\t\tconst res = await imgixRequestQueue.add(async () => {\n\t\t\treturn await fetch(config.url);\n\t\t});\n\n\t\tif (res) {\n\t\t\tconst arrayBuffer = await res.arrayBuffer();\n\t\t\tconst buffer = Buffer.from(new Uint8Array(arrayBuffer));\n\t\t\tconst contentType = res.headers.get(\"content-type\");\n\t\t\tconst base64 = `data:${contentType};base64,${buffer.toString(\"base64\")}`;\n\n\t\t\tconfig.cache.set(cacheKey, base64);\n\n\t\t\treturn base64;\n\t\t}\n\t}\n};\n\n/**\n * The minimal data used when querying an image's pallete data using Imgix's\n * API.\n *\n * @see Imgix Color Pallete Extration: https://docs.imgix.com/apis/rendering/color-palette/palette\n */\nexport interface ImgixPalleteLike {\n\tcolors: { hex: string }[];\n\tdominant_colors?: {\n\t\tvibrant?: { hex: string };\n\t\tmuted?: { hex: string };\n\t};\n}\n\n/**\n * Metadata that defines an image. This data is used to resolve Gatsby image\n * objects.\n */\nexport interface ImageSource {\n\t/**\n\t * The image's Imgix URL.\n\t */\n\turl: string;\n\n\t/**\n\t * The width of the image.\n\t */\n\twidth: number;\n\n\t/**\n\t * The height of the image.\n\t */\n\theight: number;\n}\n\nexport type GatsbyImageDataArgs = {\n\tplaceholder?: GatsbyImageDataPlaceholderKind;\n\timgixParams?: ImgixURLParams;\n\tplaceholderImgixParams?: ImgixURLParams;\n\n\taspectRatio?: number;\n\tbackgroundColor?: string;\n\tbreakpoints?: number[];\n\tformats?: ImageFormat[];\n\tlayout?: GatsbyImageDataLayoutKind;\n\twidth?: number;\n\theight?: number;\n\tsizes?: string;\n};\n\ntype ResolveGatsbyImageDataConfig = {\n\tcache: GatsbyCache;\n\tpluginName?: string;\n\tbuildURL?: typeof buildURL;\n};\n\nexport const resolveGatsbyImageData = async (\n\timage: ImageSource,\n\toptions: GatsbyImageDataArgs = {},\n\tconfig: ResolveGatsbyImageDataConfig,\n): Promise<IGatsbyImageData | null> => {\n\tconst imageDataArgs: IGatsbyImageHelperArgs = {\n\t\tpluginName: config.pluginName || packageName,\n\t\tsourceMetadata: {\n\t\t\twidth: image.width,\n\t\t\theight: image.height,\n\t\t\tformat: \"auto\",\n\t\t},\n\t\tfilename: image.url,\n\t\tgenerateImageSource,\n\t\toptions,\n\t\tlayout: options.layout,\n\t\twidth: options.width,\n\t\theight: options.height,\n\t\taspectRatio: options.aspectRatio,\n\t\tbackgroundColor: options.backgroundColor,\n\t\tbreakpoints: options.breakpoints,\n\t\tformats: options.formats,\n\t\tsizes: options.sizes,\n\t};\n\n\tconst resolvedBuildURL = config.buildURL || buildURL;\n\tconst placeholderURL = resolvedBuildURL(imageDataArgs.filename, {\n\t\t...DEFAULT_IMGIX_PARAMS,\n\t\t...options.imgixParams,\n\t\t...options.placeholderImgixParams,\n\t});\n\n\tif (options.placeholder === GatsbyImageDataPlaceholderKind.Blurred) {\n\t\timageDataArgs.placeholderURL = await fetchBase64Image({\n\t\t\turl: getLowResolutionImageURL({\n\t\t\t\t...imageDataArgs,\n\t\t\t\tfilename: placeholderURL,\n\t\t\t}),\n\t\t\tcache: config.cache,\n\t\t});\n\t}\n\n\tif (options.placeholder === GatsbyImageDataPlaceholderKind.DominantColor) {\n\t\tconst cacheKey = `${GatsbyImageDataPlaceholderKind.DominantColor}___${placeholderURL}`;\n\t\tconst cacheValue: string | undefined = await config.cache.get(cacheKey);\n\n\t\tif (cacheValue) {\n\t\t\timageDataArgs.backgroundColor = cacheValue;\n\t\t} else {\n\t\t\tconst fileExtension = extname(new URL(placeholderURL).pathname);\n\n\t\t\t// Imgix does not support `palette=json` for SVGs.\n\t\t\tif (fileExtension !== \".svg\") {\n\t\t\t\tconst palleteUrl = resolvedBuildURL(placeholderURL, {\n\t\t\t\t\tpalette: \"json\",\n\t\t\t\t\tcolors: 1,\n\t\t\t\t});\n\t\t\t\tconst fetch = (await import(\"node-fetch\")).default;\n\t\t\t\tconst res = await imgixRequestQueue.add(async () => {\n\t\t\t\t\treturn await fetch(palleteUrl);\n\t\t\t\t});\n\n\t\t\t\tif (res) {\n\t\t\t\t\tconst json = (await res.json()) as ImgixPalleteLike;\n\n\t\t\t\t\tconst dominantColor =\n\t\t\t\t\t\tjson.dominant_colors?.muted?.hex ||\n\t\t\t\t\t\tjson.dominant_colors?.vibrant?.hex ||\n\t\t\t\t\t\tjson.colors[0].hex;\n\n\t\t\t\t\tconfig.cache.set(cacheKey, dominantColor);\n\n\t\t\t\t\timageDataArgs.backgroundColor = dominantColor;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn generateImageData(imageDataArgs);\n};\n"],"names":["PQueue","DEFAULT_IMGIX_PARAMS","buildURL","buffer","Buffer","packageName","GatsbyImageDataPlaceholderKind","getLowResolutionImageURL","extname","generateImageData"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBA,MAAM,oBAAoB,IAAIA,MAAA,QAAO,EAAE,aAAa,EAAG,CAAA;AAEhD,MAAM,sBACZ,CAAC,WAAW,OAAO,QAAQ,QAAQ,MAAM,YAAiC;AACzE,QAAM,cAA8B;AAAA,IACnC,GAAGC,UAAA;AAAA,IACH,GAAG,mCAAS;AAAA,IACZ,GAAG;AAAA,IACH,GAAG;AAAA,EAAA;AAGA,MAAA,UAAU,WAAW,QAAQ;AAChC,gBAAY,KAAK;AAAA,EACjB;AAEM,SAAA;AAAA,IACN,KAAKC,gBAAAA,SAAS,WAAW,WAAW;AAAA,IACpC;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEF;AAOD,MAAM,mBAAmB,OACxB,WACgC;AAC1B,QAAA,WAAW,YAAY,OAAO;AACpC,QAAM,aAAiC,MAAM,OAAO,MAAM,IAAI,QAAQ;AAEtE,MAAI,YAAY;AACR,WAAA;AAAA,EAAA,OACD;AACN,UAAM,SAAS,MAAM,OAAO,YAAY,GAAG;AAC3C,UAAM,MAAM,MAAM,kBAAkB,IAAI,YAAW;AAC3C,aAAA,MAAM,MAAM,OAAO,GAAG;AAAA,IAAA,CAC7B;AAED,QAAI,KA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