@travetto/image 5.0.14 → 5.0.16

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.
Files changed (2) hide show
  1. package/package.json +8 -3
  2. package/src/util.ts +45 -47
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/image",
3
- "version": "5.0.14",
3
+ "version": "5.0.16",
4
4
  "description": "Image support, resizing, and optimization",
5
5
  "keywords": [
6
6
  "images",
@@ -23,11 +23,16 @@
23
23
  "directory": "module/image"
24
24
  },
25
25
  "dependencies": {
26
- "@travetto/runtime": "^5.0.11",
26
+ "@travetto/runtime": "^5.0.12",
27
27
  "sharp": "^0.33.5"
28
28
  },
29
29
  "travetto": {
30
- "displayName": "Image"
30
+ "displayName": "Image",
31
+ "build": {
32
+ "binaryDependencies": [
33
+ "sharp"
34
+ ]
35
+ }
31
36
  },
32
37
  "publishConfig": {
33
38
  "access": "public"
package/src/util.ts CHANGED
@@ -1,10 +1,13 @@
1
+ import { createReadStream } from 'node:fs';
1
2
  import { Readable } from 'node:stream';
2
3
  import { ReadableStream } from 'node:stream/web';
3
4
  import { pipeline } from 'node:stream/promises';
5
+ import type { Metadata } from 'sharp';
4
6
 
5
- import { AppError, castTo } from '@travetto/runtime';
7
+ import { castTo } from '@travetto/runtime';
6
8
 
7
- type ImageFormat = 'jpeg' | 'png' | 'avif' | 'webp';
9
+ type ImageFormat = 'jpeg' | 'png' | 'avif' | 'webp' | 'gif' | 'jxl';
10
+ type Input = Buffer | string | ReadableStream | Readable;
8
11
 
9
12
  /**
10
13
  * Image convert options
@@ -28,29 +31,20 @@ export interface ConvertOptions {
28
31
  format?: ImageFormat;
29
32
  }
30
33
 
31
- type ImageType = Readable | Buffer | ReadableStream;
32
-
33
34
  /**
34
35
  * Simple support for image manipulation.
35
36
  */
36
37
  export class ImageUtil {
37
38
 
38
39
  /**
39
- * Resize image
40
+ * Convert image
40
41
  */
41
- static async convert<T extends ImageType>(image: T, options: ConvertOptions): Promise<T> {
42
- if (options.optimize && !options.format) {
43
- if (Buffer.isBuffer(image)) {
44
- options.format = await this.getFileType(image);
45
- }
46
- throw new AppError('Format is required for optimizing');
47
- }
48
-
42
+ static async convert<T extends Input>(image: T, { format, optimize, ...opts }: ConvertOptions): Promise<T extends string ? Readable : T> {
49
43
  const { default: sharp } = await import('sharp');
50
44
 
51
45
  let builder = sharp();
52
- if (options.w || options.h) {
53
- const dims = [options.w, options.h].map(x => x ? Math.trunc(x) : undefined);
46
+ if (opts.w || opts.h) {
47
+ const dims = [opts.w, opts.h].map(x => x ? Math.trunc(x) : undefined);
54
48
  const fluid = dims.some(x => !x);
55
49
  builder = builder.resize({
56
50
  width: dims[0],
@@ -59,44 +53,48 @@ export class ImageUtil {
59
53
  });
60
54
  }
61
55
 
62
- switch (options.format) {
63
- case 'jpeg':
64
- builder = builder.jpeg(options.optimize ? { quality: 80, progressive: true } : {});
65
- break;
66
- case 'png':
67
- builder = builder.png(options.optimize ? { compressionLevel: 9, quality: 80, adaptiveFiltering: true } : {});
68
- break;
69
- case 'avif':
70
- builder = builder.avif(options.optimize ? { quality: 70 } : {});
71
- break;
72
- case 'webp':
73
- builder = builder.webp(options.optimize ? { quality: 80 } : {});
74
- break;
75
- }
56
+ builder = builder
57
+ .avif({ force: format === 'avif', ...optimize ? { quality: 70 } : {} })
58
+ .webp({ force: format === 'webp', ...optimize ? { quality: 80 } : {} })
59
+ .png({ force: format === 'png', ...optimize ? { compressionLevel: 9, quality: 80, adaptiveFiltering: true } : {} })
60
+ .jpeg({ force: format === 'jpeg', ...optimize ? { quality: 80, progressive: true } : {} })
61
+ .jxl({ force: format === 'jxl', ...optimize ? { lossless: false, quality: 80 } : {} })
62
+ .gif({ force: format === 'gif', ...optimize ? { effort: 10 } : {} });
76
63
 
77
- const stream = Buffer.isBuffer(image) ? Readable.from(image) : image;
78
- pipeline(stream, builder);
79
- return castTo('pipeThrough' in image ?
80
- ReadableStream.from(builder) :
81
- Buffer.isBuffer(image) ? builder.toBuffer() : builder);
82
- }
64
+ const stream = Buffer.isBuffer(image) ?
65
+ Readable.from(image) :
66
+ (typeof image === 'string' ? createReadStream(image) : image);
83
67
 
84
- /**
85
- * Get Image Dimensions
86
- */
87
- static async getDimensions(image: Buffer | string): Promise<{ width: number, height: number, aspect: number }> {
88
- const { default: sharp } = await import('sharp');
89
- return sharp(image).metadata().then(v => ({ width: v.width!, height: v.height!, aspect: v.width! / v.height! }));
68
+ pipeline(stream, builder);
69
+ return castTo(
70
+ typeof image === 'string' ?
71
+ builder : Buffer.isBuffer(image) ?
72
+ builder.toBuffer() :
73
+ (image instanceof ReadableStream) ?
74
+ ReadableStream.from(builder) : builder
75
+ );
90
76
  }
91
77
 
92
78
  /**
93
- * Get image type
79
+ * Get Image metadata
94
80
  */
95
- static async getFileType(image: Buffer | string): Promise<ImageFormat> {
81
+ static async getMetadata(image: Input): Promise<{
82
+ width: number;
83
+ height: number;
84
+ aspect: number;
85
+ format: ImageFormat;
86
+ }> {
96
87
  const { default: sharp } = await import('sharp');
97
- return sharp(image).metadata().then(v =>
98
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
99
- v.format?.replace('heif', 'avif')! as ImageFormat
100
- );
88
+ const out = await ((Buffer.isBuffer(image) || typeof image === 'string') ?
89
+ sharp(image).metadata() :
90
+ new Promise<Metadata>((resolve, reject) =>
91
+ pipeline(image, sharp().metadata((err, metadata) => err ? reject(err) : resolve(metadata)))
92
+ ));
93
+ return {
94
+ width: out.width!,
95
+ height: out.height!,
96
+ format: castTo(out.format?.replace('heif', 'avif')!),
97
+ aspect: out.width! / out.height!
98
+ };
101
99
  }
102
100
  }