@travetto/image 4.1.1 → 5.0.0-rc.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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 ArcSine Technologies
3
+ Copyright (c) 2023 ArcSine Technologies
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -19,16 +19,16 @@ The utility's primary structure revolves around the [CommandOperation](https://g
19
19
 
20
20
  **Code: Simple Image Resize**
21
21
  ```typescript
22
- import { createReadStream } from 'node:fs';
22
+ import { createReadStream, createWriteStream } from 'node:fs';
23
+ import { pipeline } from 'node:stream/promises';
23
24
 
24
- import { StreamUtil } from '@travetto/base';
25
25
  import { ImageConverter } from '@travetto/image';
26
26
 
27
27
  export class ResizeService {
28
28
  async resizeImage(imgPath: string, width: number, height: number): Promise<string> {
29
29
  const stream = await ImageConverter.resize(createReadStream(imgPath), { w: width, h: height });
30
30
  const out = imgPath.replace(/[.][^.]+$/, (ext) => `.resized${ext}`);
31
- await StreamUtil.writeToFile(stream, out);
31
+ await pipeline(stream, createWriteStream(out));
32
32
  return out;
33
33
  }
34
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/image",
3
- "version": "4.1.1",
3
+ "version": "5.0.0-rc.0",
4
4
  "description": "Image support, resizing, and optimization",
5
5
  "keywords": [
6
6
  "images",
@@ -23,8 +23,8 @@
23
23
  "directory": "module/image"
24
24
  },
25
25
  "dependencies": {
26
- "@travetto/base": "^4.1.1",
27
- "@travetto/command": "^4.1.1"
26
+ "@travetto/base": "^5.0.0-rc.0",
27
+ "@travetto/command": "^5.0.0-rc.0"
28
28
  },
29
29
  "travetto": {
30
30
  "displayName": "Image"
package/src/convert.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { Readable } from 'node:stream';
2
+ import { buffer as toBuffer } from 'node:stream/consumers';
2
3
  import { createReadStream } from 'node:fs';
3
4
  import { ChildProcess } from 'node:child_process';
4
5
  import { pipeline } from 'node:stream/promises';
5
6
 
6
7
  import { CommandOperation } from '@travetto/command';
7
- import { MemoryWritable, StreamUtil } from '@travetto/base';
8
8
 
9
9
  /**
10
10
  * Image output options
@@ -62,13 +62,12 @@ export class ImageConverter {
62
62
 
63
63
  static async #stream<T extends ImageType>(proc: ChildProcess, input: T): Promise<T> {
64
64
  if (Buffer.isBuffer(input)) {
65
- const buffer = new MemoryWritable();
66
- await Promise.all([
67
- pipeline(await StreamUtil.toStream(input), proc.stdin!),
68
- pipeline(proc.stdout!, buffer)
65
+ const [_, output] = await Promise.all([
66
+ pipeline(Readable.from(input), proc.stdin!),
67
+ toBuffer(proc.stdout!)
69
68
  ]);
70
69
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
71
- return buffer.toBuffer() as T;
70
+ return output as T;
72
71
  } else {
73
72
  input.pipe(proc.stdin!); // Start the process
74
73
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
@@ -122,13 +121,12 @@ export class ImageConverter {
122
121
  image = createReadStream(image);
123
122
  }
124
123
 
125
- const buffer = new MemoryWritable();
126
- await Promise.all([
124
+ const [_, output] = await Promise.all([
127
125
  pipeline(image, proc.stdin!),
128
- pipeline(proc.stdout!, buffer)
126
+ toBuffer(proc.stdout!)
129
127
  ]);
130
128
 
131
- const text = buffer.toBuffer('utf8');
129
+ const text = output.toString('utf8');
132
130
  const [w, h] = text.split('X').map(x => parseFloat(x));
133
131
 
134
132
  return { width: w, height: h };
package/src/resource.ts CHANGED
@@ -1,21 +1,22 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import { Readable } from 'node:stream';
3
3
  import { pipeline } from 'node:stream/promises';
4
+ import path from 'node:path';
4
5
 
5
- import { RuntimeContext, path } from '@travetto/manifest';
6
- import { Env, ResourceLoader } from '@travetto/base';
6
+ import { RuntimeContext } from '@travetto/manifest';
7
+ import { Env, FileLoader } from '@travetto/base';
7
8
 
8
9
  import { ImageConverter } from './convert';
9
10
 
10
11
  /**
11
12
  * Resource provider for images that allows for real-time optimization
12
13
  */
13
- export class ImageOptimizingResourceLoader extends ResourceLoader {
14
+ export class ImageOptimizingResourceLoader extends FileLoader {
14
15
 
15
16
  #cacheRoot: string;
16
17
 
17
18
  constructor(paths: string[] = [], cacheRoot?: string) {
18
- super(paths);
19
+ super([...paths, ...Env.resourcePaths]);
19
20
 
20
21
  this.#cacheRoot = cacheRoot ?? path.resolve(Env.TRV_IMAGE_CACHE.val || RuntimeContext.toolPath('image_cache'));
21
22
  }