@travetto/image 7.1.3 → 8.0.0-alpha.0

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 +2 -2
  2. package/src/util.ts +30 -36
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/image",
3
- "version": "7.1.3",
3
+ "version": "8.0.0-alpha.0",
4
4
  "type": "module",
5
5
  "description": "Image support, resizing, and optimization",
6
6
  "keywords": [
@@ -24,7 +24,7 @@
24
24
  "directory": "module/image"
25
25
  },
26
26
  "dependencies": {
27
- "@travetto/runtime": "^7.1.3",
27
+ "@travetto/runtime": "^8.0.0-alpha.0",
28
28
  "sharp": "^0.34.5"
29
29
  },
30
30
  "travetto": {
package/src/util.ts CHANGED
@@ -1,13 +1,16 @@
1
- import { createReadStream } from 'node:fs';
2
- import { Readable } from 'node:stream';
3
- import { ReadableStream } from 'node:stream/web';
4
- import { pipeline } from 'node:stream/promises';
5
- import type { Metadata } from 'sharp';
1
+ import type { Metadata, Sharp } from 'sharp';
6
2
 
7
- import { castTo } from '@travetto/runtime';
3
+ import { BinaryUtil, castTo, type BinaryStream, type BinaryType } from '@travetto/runtime';
8
4
 
9
- type ImageFormat = 'jpeg' | 'png' | 'avif' | 'webp' | 'gif' | 'jxl';
10
- type Input = Buffer | string | ReadableStream | Readable;
5
+ const VALID_EXTENSIONS = ['jpeg', 'jpg', 'png', 'avif', 'webp', 'gif', 'jxl'] as const;
6
+
7
+ type ImageFormat = typeof VALID_EXTENSIONS[number];
8
+ type ImageMetadata = {
9
+ width: number;
10
+ height: number;
11
+ aspect: number;
12
+ format: ImageFormat;
13
+ };
11
14
 
12
15
  /**
13
16
  * Image convert options
@@ -36,10 +39,11 @@ export interface ConvertOptions {
36
39
  */
37
40
  export class ImageUtil {
38
41
 
39
- /**
40
- * Convert image
41
- */
42
- static async convert<T extends Input>(image: T, { format, optimize, ...options }: ConvertOptions): Promise<T extends string ? Readable : T> {
42
+ static isKnownExtension(ext: string): ext is ImageFormat {
43
+ return VALID_EXTENSIONS.includes(castTo(ext));
44
+ }
45
+
46
+ static async #getBuilder({ format, optimize, ...options }: ConvertOptions): Promise<Sharp> {
43
47
  const { default: sharp } = await import('sharp');
44
48
 
45
49
  let builder = sharp();
@@ -53,43 +57,33 @@ export class ImageUtil {
53
57
  });
54
58
  }
55
59
 
56
- builder = builder
60
+ return builder
57
61
  .avif({ force: format === 'avif', ...optimize ? { quality: 70 } : {} })
58
62
  .webp({ force: format === 'webp', ...optimize ? { quality: 80 } : {} })
59
63
  .png({ force: format === 'png', ...optimize ? { compressionLevel: 9, quality: 80, adaptiveFiltering: true } : {} })
60
- .jpeg({ force: format === 'jpeg', ...optimize ? { quality: 80, progressive: true } : {} })
64
+ .jpeg({ force: format === 'jpeg' || format === 'jpg', ...optimize ? { quality: 80, progressive: true } : {} })
61
65
  .jxl({ force: format === 'jxl', ...optimize ? { lossless: false, quality: 80 } : {} })
62
66
  .gif({ force: format === 'gif', ...optimize ? { effort: 10 } : {} });
63
67
 
64
- const stream = Buffer.isBuffer(image) ?
65
- Readable.from(image) :
66
- (typeof image === 'string' ? createReadStream(image) : image);
68
+ }
67
69
 
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
- );
70
+ /**
71
+ * Convert image as readable stream
72
+ */
73
+ static async convert(image: BinaryType, options: ConvertOptions): Promise<BinaryStream> {
74
+ const builder = await this.#getBuilder(options);
75
+ BinaryUtil.pipeline(image, builder);
76
+ return builder;
76
77
  }
77
78
 
78
79
  /**
79
80
  * Get Image metadata
80
81
  */
81
- static async getMetadata(image: Input): Promise<{
82
- width: number;
83
- height: number;
84
- aspect: number;
85
- format: ImageFormat;
86
- }> {
82
+ static async getMetadata(image: BinaryType): Promise<ImageMetadata> {
87
83
  const { default: sharp } = await import('sharp');
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((error, metadata) => error ? reject(error) : resolve(metadata)))
92
- ));
84
+ const out = await new Promise<Metadata>((resolve, reject) =>
85
+ BinaryUtil.pipeline(image, sharp().metadata((error, metadata) => error ? reject(error) : resolve(metadata)))
86
+ );
93
87
  return {
94
88
  width: out.width!,
95
89
  height: out.height!,