Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const srcNode = async (cwd, attributes) => {
const { src, alt = '', ...attrs } = attributes
const url = normalizeUrl(normalizeSrc(src, attributes.srcset))
let size = { width: null, height: null }
if (url.startsWith('http')) {
const result = await probe(url)
size = { width: result.width, height: result.height }
} else if (url.startsWith('data:')) {
const buffer = uriToBuffer(url)
const result = probe.sync(buffer)
size = { width: result.width, height: result.height }
} else {
const fileStream = fs.createReadStream(path.join(cwd, url))
const result = await probe(fileStream)
size = { width: result.width, height: result.height }
fileStream.destroy()
}
// remove loading attribute
// https://github.com/tomoyukikashiro/html2amp/issues/80
let { loading, ..._attrs } = attrs
_attrs = { src: url, alt, width: size.width, height: size.height, layout: 'responsive', ..._attrs }
const _attrsStr = Object.keys(_attrs).map(key => `${key}="${_attrs[key]}"`).join(' ')
return ``
}
meta: Meta,
}) {
if (meta && meta.asset && data) {
const { asset, dimensions, square, contentTypePattern, contentTypeHuman }: Meta = meta;
// filePath could be an URL
const filePath = path.resolve(this.rootDir, data);
try {
// Assumption: All assets are images. This may change in the future.
// NOTE(nikki): The '4100' below should be enough for most file types, though we
// could probably go shorter....
// http://www.garykessler.net/library/file_sigs.html
// The metadata content for .jpgs might be located a lot farther down the file, so this
// may pose problems in the future.
// This cases on whether filePath is a remote URL or located on the machine
const probeResult = fs.existsSync(filePath)
? imageProbe.sync(await readChunk(filePath, 0, 4100))
: await imageProbe(data, { useElectronNet: false });
const { width, height, type, mime, wUnits, hUnits } = probeResult;
if (contentTypePattern && !mime.match(new RegExp(contentTypePattern))) {
this.manualValidationErrors.push(
new ValidationError({
errorCode: ErrorCodes.INVALID_CONTENT_TYPE,
fieldPath,
message: `field '${fieldPath}' should point to ${meta.contentTypeHuman} but the file at '${data}' has type ${type}`,
data,
meta,
})
);
}
async _validateImageAsync({ fieldPath, data, meta }: AssetField) {
if (meta && meta.asset && data) {
const { dimensions, square, contentTypePattern }: Meta = meta;
// filePath could be an URL
const filePath = path.resolve(this.rootDir, data);
try {
// NOTE(nikki): The '4100' below should be enough for most file types, though we
// could probably go shorter....
// http://www.garykessler.net/library/file_sigs.html
// The metadata content for .jpgs might be located a lot farther down the file, so this
// may pose problems in the future.
// This cases on whether filePath is a remote URL or located on the machine
const probeResult = fs.existsSync(filePath)
? imageProbe.sync(await readChunk(filePath, 0, 4100))
: await imageProbe(data, { useElectronNet: false });
if (!probeResult) {
return;
}
const { width, height, type, mime } = probeResult;
if (contentTypePattern && !mime.match(new RegExp(contentTypePattern))) {
this.manualValidationErrors.push(
new ValidationError({
errorCode: 'INVALID_CONTENT_TYPE',
fieldPath,
message: `field '${fieldPath}' should point to ${meta.contentTypeHuman} but the file at '${data}' has type ${type}`,
data,
meta,
})
const defaultBlur = this.config.images.defaultBlur
if (!imageExtensions.includes(ext)) {
throw new Error(
`${ext} is not a supported image format. ` +
`Supported extensions are ${imageExtensions.join(', ')}.`
)
}
if (!await fs.exists(filePath)) {
throw new Error(`${filePath} was not found.`)
}
const hash = await md5File(filePath)
const buffer = await fs.readFile(filePath)
const originalSize = imageSize.sync(buffer) || { width: 1, height: 1 }
const { imageWidth, imageHeight } = computeScaledImageSize(originalSize, options, maxImageWidth)
let imageWidths = options.imageWidths || [480, 1024, 1920, 2560]
if (typeof imageWidths === 'string') {
imageWidths = imageWidths.split(',')
}
let imageSizes = imageWidths.filter(size => size <= imageWidth)
const maxWidth = Math.max(...imageSizes, 0)
if (!options.imageWidths) {
if (imageWidth > maxWidth || imageSizes.length === 0) {
imageSizes.push(imageWidth)
}
export default function(contentBuffer) {
const data = fs.readFileSync(this.resourcePath);
const size = probe.sync(data);
let content = contentBuffer.toString('utf8');
if (content.includes('= {')) {
content = splice(
content,
content.indexOf('};') - 1,
`, height: ${size.height}`
);
content = splice(
content,
content.indexOf('};') - 1,
`, width: ${size.width}`
);
} else {
const src = content.substring(