Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function normalize(obj) {
// remove all empty keys
Object.keys(obj).forEach(function(key) {
var val = obj[key];
if (val == "" || val == null || (typeof val == 'number' && isNaN(val))) delete obj[key];
});
if (!obj.ext) {
if (obj.mime) {
obj.ext = mime.getExtension(obj.mime);
if (!obj.ext) {
// eslint-disable-next-line no-console
console.warn("No extension found for mime type", obj.mime, obj.url);
}
}
if (!obj.ext) {
// eslint-disable-next-line no-console
console.warn("Using extname", obj.pathname);
obj.ext = Path.extname(obj.pathname).substring(1);
}
}
delete obj.pathname;
if (obj.ext) {
obj.ext = obj.ext.toLowerCase();
switch (obj.ext) {
case "jpeg":
size: AnySize,
icon: Icon,
publicPath: string,
cacheKey: string
): Promise<{ manifestIcon: ManifestIcon; webpackAsset: WebpackAsset }> {
const { width, height } = toSize(size);
if (width <= 0 || height <= 0) {
throw Error(`Failed to process image with invalid size: { width: ${width}, height: ${height}}`);
}
const mimeType = mime.getType(icon.src);
if (!mimeType) {
throw new Error(`Invalid mimeType for image with source: ${icon.src}`);
}
const dimensions = `${width}x${height}`;
const fileName = `icon_${dimensions}.${mime.getExtension(mimeType)}`;
let imageBuffer: Buffer | null = await getImageFromCacheAsync(fileName, cacheKey);
if (!imageBuffer) {
// Putting the warning here will prevent the warning from showing if all images were reused from the cache
if (!hasWarned && !(await isAvailableAsync())) {
hasWarned = true;
// TODO: Bacon: Fallback to nodejs image resizing as native doesn't work in the host environment.
console.log('ff', cacheKey, fileName, dimensions);
console.log();
console.log(
chalk.bgYellow.black(
`PWA Images: Using node to generate images. This is much slower than using native packages.`
)
);
console.log(
chalk.yellow(
export default function getMediaType(contentType, fileName) {
const extName = mime.getExtension(contentType);
const fileExt = extName ? `.${extName}` : null;
const testString = fileName || fileExt;
// Get mediaType from file extension
if (testString) {
const res = formats.reduce((ret, testpair) => {
const [regex, mediaType] = testpair;
return regex.test(ret) ? mediaType : ret;
}, testString);
if (res !== testString) return res;
}
// Get mediaType from contentType
if (contentType) {
if (!fs.existsSync(filePath)) {
return;
}
// Setting file size.
const stat = fs.statSync(filePath);
res.setHeader('Content-Length', stat.size);
// Setting last modified date.
const modified = stat.mtime.toUTCString();
res.setHeader('Last-Modified', modified);
// Determining mime type.
const type = mime.getType(filePath);
if (type) {
const charset = mime.getExtension(type);
res.setHeader('Content-Type', type + (charset ? `; charset=${charset}` : ''));
} else {
res.setHeader('Content-Type', 'application/octet-stream');
}
res.setHeader('Connection', 'close');
res.setStream(fs.createReadStream(filePath));
res.setStatusCode(200);
beforeFinalize();
res.finalize();
}
});
}
if (res.result.status !== 200) {
const dump = await common.outcome(res.result.json());
return emit(progress, {
error: (!dump.success) ? res.result.statusText : dump.result.errors.shift().title,
source: options.source
});
}
let streamChain = [];
let dimensions = res.result.headers.get('x-width') + 'x' + res.result.headers.get('x-height');
const ext = mime.getExtension(res.result.headers.get('content-type'));
const fileDestination = path.parse(workflow.destination);
if (fileDestination.ext !== '.txt') {
workflow.destination = (fileDestination.ext === `.${ext}`) ? workflow.destination : `${fileDestination.dir}/${fileDestination.name}.${ext}`;
}
const original = workflow.destination;
workflow.destination = path.dirname(workflow.destination) + '/' + uuid() + path.extname(workflow.destination);
if (options.workflow === 'base64' && workflow.detail.ext === '.txt') {
const dataUrl = (workflow.detail.dataUrl && !workflow.b64ToImage) ? {
prefix: `data:${res.result.headers.get('content-type')};base64,`
} : {};
function processIcon (currentSize, icon, buffer, mimeType, publicPath, shouldFingerprint) {
const dimensions = `${currentSize}x${currentSize}`
const fileName = shouldFingerprint ? `icon_${dimensions}.${generateFingerprint(buffer)}.${mime.getExtension(mimeType)}` : `icon_${dimensions}.${mime.getExtension(mimeType)}`
const iconOutputDir = icon.destination ? joinURI(icon.destination, fileName) : fileName
const iconPublicUrl = joinURI(publicPath, iconOutputDir)
return {
manifestIcon: {
src: iconPublicUrl,
sizes: dimensions,
type: mimeType
},
webpackAsset: {
output: iconOutputDir,
url: iconPublicUrl,
source: buffer,
size: buffer.length,
ios: icon.ios ? { valid: icon.ios, size: dimensions, href: iconPublicUrl } : false,
color: icon.color
}
createFile(config, filename, data, contentType) {
const extname = path.extname(filename);
const hasExtension = extname.length > 0;
if (!hasExtension && contentType && mime.getExtension(contentType)) {
filename = filename + '.' + mime.getExtension(contentType);
} else if (hasExtension && !contentType) {
contentType = mime.getType(filename);
}
if (!this.options.preserveFileName) {
filename = randomHexString(32) + '_' + filename;
}
const location = this.adapter.getFileLocation(config, filename);
return this.adapter.createFile(filename, data, contentType).then(() => {
return Promise.resolve({
url: location,
name: filename,
});
});