@travetto/image 2.1.1 → 2.2.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 (3) hide show
  1. package/README.md +3 -3
  2. package/package.json +2 -2
  3. package/src/util.ts +13 -10
package/README.md CHANGED
@@ -10,18 +10,18 @@ npm install @travetto/image
10
10
 
11
11
  This module provides functionality for image resizing, and png optimization. This is primarily meant to be used in conjunction with other modules, like the [Asset](https://github.com/travetto/travetto/tree/main/module/asset#readme "Modular library for storing and retrieving binary assets") module or the [Email Templating](https://github.com/travetto/travetto/tree/main/module/email-template#readme "Email templating module") module. It can also be invoked directly as needed (as it can be very handy for batch processing images on the command line).
12
12
 
13
- The utility's primary structure revolves around the [CommandService](https://github.com/travetto/travetto/tree/main/module/command/src/command.ts#L11) from the [Command](https://github.com/travetto/travetto/tree/main/module/command#readme "Support for executing complex commands at runtime.") module. The [CommandService](https://github.com/travetto/travetto/tree/main/module/command/src/command.ts#L11) allows for declaration of a local executable, and a fall-back docker container (mainly meant for development). The [ImageUtil](https://github.com/travetto/travetto/tree/main/module/image/src/util.ts#L30) utilizes [ImageMagick](https://imagemagick.org/index.php), [pngquant](https://pngquant.org/), and [Jpegoptim](https://github.com/tjko/jpegoptim) as the backing for image resizing and png compression, respectively.
13
+ The utility's primary structure revolves around the [CommandService](https://github.com/travetto/travetto/tree/main/module/command/src/command.ts#L11) from the [Command](https://github.com/travetto/travetto/tree/main/module/command#readme "Support for executing complex commands at runtime.") module. The [CommandService](https://github.com/travetto/travetto/tree/main/module/command/src/command.ts#L11) allows for declaration of a local executable, and a fall-back docker container (mainly meant for development). The [ImageUtil](https://github.com/travetto/travetto/tree/main/module/image/src/util.ts#L31) utilizes [ImageMagick](https://imagemagick.org/index.php), [pngquant](https://pngquant.org/), and [Jpegoptim](https://github.com/tjko/jpegoptim) as the backing for image resizing and png compression, respectively.
14
14
 
15
15
  **Code: Simple Image Resize**
16
16
  ```typescript
17
- import * as fs from 'fs';
17
+ import { createReadStream } from 'fs';
18
18
 
19
19
  import { StreamUtil } from '@travetto/boot';
20
20
  import { ImageUtil } from '@travetto/image';
21
21
 
22
22
  export class ResizeService {
23
23
  async resizeImage(imgPath: string, width: number, height: number): Promise<string> {
24
- const stream = await ImageUtil.resize(fs.createReadStream(imgPath), { w: width, h: height });
24
+ const stream = await ImageUtil.resize(createReadStream(imgPath), { w: width, h: height });
25
25
  const out = imgPath.replace(/[.][^.]+$/, (ext) => `.resized${ext}`);
26
26
  await StreamUtil.writeToFile(stream, out);
27
27
  return out;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@travetto/image",
3
3
  "displayName": "Image",
4
- "version": "2.1.1",
4
+ "version": "2.2.0",
5
5
  "description": "Image support, resizing, and optimization",
6
6
  "keywords": [
7
7
  "images",
@@ -24,7 +24,7 @@
24
24
  "directory": "module/image"
25
25
  },
26
26
  "dependencies": {
27
- "@travetto/command": "^2.1.1"
27
+ "@travetto/command": "^2.2.0"
28
28
  },
29
29
  "publishConfig": {
30
30
  "access": "public"
package/src/util.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as fs from 'fs/promises';
2
+ import { Readable } from 'stream';
2
3
 
3
4
  import { CommandService } from '@travetto/command';
4
5
  import { ExecUtil, StreamUtil, AppCache, FsUtil } from '@travetto/boot';
@@ -21,7 +22,7 @@ export interface ImageOptions {
21
22
  optimize?: boolean;
22
23
  }
23
24
 
24
- type ImageType = NodeJS.ReadableStream | Buffer;
25
+ type ImageType = Readable | Buffer;
25
26
 
26
27
  /**
27
28
  * Simple support for image manipulation. Built upon @travetto/command, it can
@@ -50,30 +51,31 @@ export class ImageUtil {
50
51
  */
51
52
  static JPEG_COMPRESSOR = new CommandService({
52
53
  containerImage: 'shomatan/jpegoptim:1.4.4',
53
- localCheck: ['jpegotim', ['-h']]
54
+ localCheck: ['jpegoptim', ['-h']]
54
55
  });
55
56
 
56
57
  /**
57
- * Resize image using image magick
58
+ * Resize image using imagemagick
58
59
  */
59
- static resize(image: NodeJS.ReadableStream, options: ImageOptions): Promise<NodeJS.ReadableStream>;
60
+ static resize(image: Readable, options: ImageOptions): Promise<Readable>;
60
61
  static resize(image: Buffer, options: ImageOptions): Promise<Buffer>;
61
- static async resize(image: ImageType, options: ImageOptions): Promise<NodeJS.ReadableStream | Buffer> {
62
+ static async resize(image: ImageType, options: ImageOptions): Promise<Readable | Buffer> {
62
63
  const state = await this.CONVERTER.exec(
63
64
  'gm', 'convert', '-resize', `${options.w ?? ''}x${options.h ?? ''}`,
64
65
  '-auto-orient',
65
66
  ...(options.optimize ? ['-strip', '-quality', '86'] : []),
66
67
  '-', '-');
67
68
 
69
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
68
70
  return await ExecUtil.pipe(state, image as Buffer);
69
71
  }
70
72
 
71
73
  /**
72
- * Optimize png usng pngquant
74
+ * Optimize png using pngquant
73
75
  */
74
- static optimize(format: 'png' | 'jpeg', image: NodeJS.ReadableStream): Promise<NodeJS.ReadableStream>;
76
+ static optimize(format: 'png' | 'jpeg', image: Readable): Promise<Readable>;
75
77
  static optimize(format: 'png' | 'jpeg', image: Buffer): Promise<Buffer>;
76
- static async optimize(format: 'png' | 'jpeg', image: ImageType): Promise<NodeJS.ReadableStream | Buffer> {
78
+ static async optimize(format: 'png' | 'jpeg', image: ImageType): Promise<Readable | Buffer> {
77
79
  let stream;
78
80
  switch (format) {
79
81
  case 'png': {
@@ -86,20 +88,21 @@ export class ImageUtil {
86
88
  break;
87
89
  }
88
90
  }
91
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
89
92
  return await ExecUtil.pipe(stream, image as Buffer);
90
93
  }
91
94
 
92
95
  /**
93
96
  * Fetch image, compress and return as buffer
94
97
  */
95
- static async optimizeResource(rel: string) {
98
+ static async optimizeResource(rel: string): Promise<Buffer> {
96
99
  const { ResourceManager } = await import('@travetto/base');
97
100
 
98
101
  const pth = await ResourceManager.find(rel);
99
102
  const out = AppCache.toEntryName(pth);
100
103
 
101
104
  if (!(await FsUtil.exists(out))) {
102
- let stream: Buffer | NodeJS.ReadableStream = await ResourceManager.readStream(rel);
105
+ let stream: Buffer | Readable = await ResourceManager.readStream(rel);
103
106
  if (/[.]png$/.test(pth)) {
104
107
  stream = await this.optimize('png', stream);
105
108
  } else if (/[.]jpe?g$/i.test(pth)) {