@zubyjs/image 1.0.78 → 1.0.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/handler.d.ts +1 -1
- package/handler.js +12 -6
- package/imageLoader.d.ts +2 -2
- package/imageLoader.js +2 -1
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/optimizeImage.d.ts +1 -1
- package/optimizeImage.js +2 -2
- package/options/optionsToPath.d.ts +1 -1
- package/options/pathToOptions.d.ts +1 -1
- package/package.json +46 -45
- package/types.d.ts +3 -2
package/handler.d.ts
CHANGED
package/handler.js
CHANGED
|
@@ -2,6 +2,7 @@ import optimizeImage from './optimizeImage.js';
|
|
|
2
2
|
import { HTTP_HEADERS } from 'zuby/constants.js';
|
|
3
3
|
import { IMAGE_FORMATS_ARRAY } from 'zuby/types.js';
|
|
4
4
|
import { pathToOptions } from './options/pathToOptions.js';
|
|
5
|
+
import { getErrorMessage } from 'zuby/utils/errorUtils.js';
|
|
5
6
|
/**
|
|
6
7
|
* The image handler that optimizes images
|
|
7
8
|
* on /_image/:path* endpoint.
|
|
@@ -17,9 +18,14 @@ export default async function Handler(context, _next) {
|
|
|
17
18
|
if (src.startsWith('http://') || src.startsWith('https://')) {
|
|
18
19
|
return new Response(`ERROR: The 'src' attribute needs to be relative`, { status: 400 });
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return new Response(`ERROR:
|
|
21
|
+
// Validate against path traversal attacks
|
|
22
|
+
if (src.includes('..') || src.startsWith('/')) {
|
|
23
|
+
return new Response(`ERROR: Invalid 'src' path - path traversal not allowed`, { status: 400 });
|
|
24
|
+
}
|
|
25
|
+
if ((format && !IMAGE_FORMATS_ARRAY.includes(format)) || (src && !IMAGE_FORMATS_ARRAY.some((ext) => src.endsWith(ext)))) {
|
|
26
|
+
return new Response(`ERROR: Unsupported 'format' or 'src' file extension. Supported formats are: ${IMAGE_FORMATS_ARRAY.join(', ')}`, {
|
|
27
|
+
status: 400,
|
|
28
|
+
});
|
|
23
29
|
}
|
|
24
30
|
const origin = context.url.origin.replace('localhost', '127.0.0.1');
|
|
25
31
|
const imageUrl = new URL(src, origin);
|
|
@@ -57,8 +63,8 @@ export default async function Handler(context, _next) {
|
|
|
57
63
|
},
|
|
58
64
|
});
|
|
59
65
|
}
|
|
60
|
-
catch (
|
|
61
|
-
|
|
62
|
-
return new Response(
|
|
66
|
+
catch (error) {
|
|
67
|
+
const errorMessage = getErrorMessage(error);
|
|
68
|
+
return new Response(`ERROR: An error occurred while optimizing your image: ${errorMessage}`, { status: 500 });
|
|
63
69
|
}
|
|
64
70
|
}
|
package/imageLoader.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { ImageLoaderOptions } from 'zuby/types.js';
|
|
1
|
+
import type { ImageLoaderOptions } from 'zuby/types.js';
|
|
2
2
|
/**
|
|
3
3
|
* The ImageLoader for @zubyjs/image
|
|
4
4
|
* that is used to generate image URLs
|
|
5
5
|
*/
|
|
6
|
-
export default function imageLoader({ context, src, isAbsolute, format, quality, width, height
|
|
6
|
+
export default function imageLoader({ context, src, isAbsolute, format, quality, width, height }: ImageLoaderOptions): string;
|
|
7
7
|
/**
|
|
8
8
|
* Returns shared global image URLs set
|
|
9
9
|
* available across the whole project
|
package/imageLoader.js
CHANGED
|
@@ -4,7 +4,7 @@ let warningShown = false;
|
|
|
4
4
|
* The ImageLoader for @zubyjs/image
|
|
5
5
|
* that is used to generate image URLs
|
|
6
6
|
*/
|
|
7
|
-
export default function imageLoader({ context, src, isAbsolute, format, quality, width = 1920, height
|
|
7
|
+
export default function imageLoader({ context, src, isAbsolute, format, quality, width = 1920, height }) {
|
|
8
8
|
const { buildId } = context;
|
|
9
9
|
if (isAbsolute) {
|
|
10
10
|
// Just add buildId to the image URL
|
|
@@ -13,6 +13,7 @@ export default function imageLoader({ context, src, isAbsolute, format, quality,
|
|
|
13
13
|
return `${src}?${buildId}`;
|
|
14
14
|
}
|
|
15
15
|
if (!width && !height && !warningShown) {
|
|
16
|
+
// Using console.warn as this runs in browser context without logger
|
|
16
17
|
console.warn('WARNING: You should always provide at least width or height to the Image component to avoid layout shift.');
|
|
17
18
|
warningShown = true;
|
|
18
19
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ZubyPlugin } from 'zuby/types.js';
|
|
1
|
+
import type { ZubyPlugin } from 'zuby/types.js';
|
|
2
2
|
export interface ImagePluginOptions {
|
|
3
3
|
/**
|
|
4
4
|
* Set this option to false to disable
|
|
@@ -19,5 +19,5 @@ export interface ImagePluginOptions {
|
|
|
19
19
|
* optimize images in your project
|
|
20
20
|
* @returns ZubyPlugin
|
|
21
21
|
*/
|
|
22
|
-
declare const _default: ({ buildTime, maxBuildTime
|
|
22
|
+
declare const _default: ({ buildTime, maxBuildTime }?: ImagePluginOptions) => ZubyPlugin;
|
|
23
23
|
export default _default;
|
package/index.js
CHANGED
|
@@ -14,7 +14,7 @@ const __dirname = dirname(__filename);
|
|
|
14
14
|
* optimize images in your project
|
|
15
15
|
* @returns ZubyPlugin
|
|
16
16
|
*/
|
|
17
|
-
export default ({ buildTime = true, maxBuildTime = 1000
|
|
17
|
+
export default ({ buildTime = true, maxBuildTime = 1000 } = {}) => ({
|
|
18
18
|
name: 'zuby-image-plugin',
|
|
19
19
|
description: 'optimizing images...',
|
|
20
20
|
buildStep: true,
|
package/optimizeImage.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { OptimizeImageOptions, OptimizeImageOutput } from './types.js';
|
|
1
|
+
import type { OptimizeImageOptions, OptimizeImageOutput } from './types.js';
|
|
2
2
|
export default function optimizeImage({ input, inputFile, outputFile, quality, width, height, format, }: OptimizeImageOptions): Promise<OptimizeImageOutput>;
|
package/optimizeImage.js
CHANGED
|
@@ -2,7 +2,7 @@ import sharp from 'sharp';
|
|
|
2
2
|
export default async function optimizeImage({ input, inputFile, outputFile, quality = 75, width, height, format, }) {
|
|
3
3
|
// Read the image from buffer or filesystem
|
|
4
4
|
const image = sharp(input || inputFile);
|
|
5
|
-
const { format: originalFormat = 'webp', width: originalWidth = 0, height: originalHeight = 0, size: inputSize
|
|
5
|
+
const { format: originalFormat = 'webp', width: originalWidth = 0, height: originalHeight = 0, size: inputSize } = await image.metadata();
|
|
6
6
|
const widthRatio = originalWidth / originalHeight;
|
|
7
7
|
const heightRatio = originalHeight / originalWidth;
|
|
8
8
|
format = format || originalFormat;
|
|
@@ -26,7 +26,7 @@ export default async function optimizeImage({ input, inputFile, outputFile, qual
|
|
|
26
26
|
quality,
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
-
let outputFileSize
|
|
29
|
+
let outputFileSize;
|
|
30
30
|
// Save the image to filesystem
|
|
31
31
|
if (outputFile) {
|
|
32
32
|
const outputInfo = await image.toFile(outputFile);
|
package/package.json
CHANGED
|
@@ -1,47 +1,48 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
|
|
2
|
+
"name": "@zubyjs/image",
|
|
3
|
+
"version": "1.0.80",
|
|
4
|
+
"description": "Zuby.js image plugin",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"release": "cd ./dist && npm publish --access public && cd ..",
|
|
9
|
+
"bump-version": "npm version patch",
|
|
10
|
+
"build": "rm -rf dist/ stage/ && mkdir dist && tsc && cp -rf package.json README.md stage/image/src/* dist/ && rm -rf stage/",
|
|
11
|
+
"push-build": "npm run build && cd dist && yalc push --force && cd ..",
|
|
12
|
+
"test": "vitest run",
|
|
13
|
+
"test:coverage": "vitest run --coverage"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"directory": "dist",
|
|
17
|
+
"linkDirectory": true
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"zuby": "^1.0.0"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"chalk": "5.3.0",
|
|
24
|
+
"sharp": "0.32.6"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://gitlab.com/futrou/zuby.js/-/issues",
|
|
28
|
+
"email": "zuby@futrou.com"
|
|
29
|
+
},
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://gitlab.com/futrou/zuby.js.git"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://zubyjs.com",
|
|
36
|
+
"keywords": [
|
|
37
|
+
"zuby-plugin",
|
|
38
|
+
"zuby",
|
|
39
|
+
"image",
|
|
40
|
+
"optimization"
|
|
41
|
+
],
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/sharp": "0.32.0"
|
|
47
|
+
}
|
|
47
48
|
}
|
package/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
+
import type { ImageFormat } from 'zuby/types.js';
|
|
3
4
|
export interface ImageOptions {
|
|
4
5
|
src: string;
|
|
5
6
|
quality?: number;
|