@travetto/image 4.1.1 → 5.0.0-rc.1
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 +1 -1
- package/README.md +3 -3
- package/package.json +3 -3
- package/src/convert.ts +8 -10
- package/src/resource.ts +4 -4
package/LICENSE
CHANGED
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
|
|
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": "
|
|
3
|
+
"version": "5.0.0-rc.1",
|
|
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": "^
|
|
27
|
-
"@travetto/command": "^
|
|
26
|
+
"@travetto/base": "^5.0.0-rc.1",
|
|
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
|
|
66
|
-
|
|
67
|
-
|
|
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
|
|
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
|
|
126
|
-
await Promise.all([
|
|
124
|
+
const [_, output] = await Promise.all([
|
|
127
125
|
pipeline(image, proc.stdin!),
|
|
128
|
-
|
|
126
|
+
toBuffer(proc.stdout!)
|
|
129
127
|
]);
|
|
130
128
|
|
|
131
|
-
const text =
|
|
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,21 @@
|
|
|
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 {
|
|
6
|
-
import { Env, ResourceLoader } from '@travetto/base';
|
|
6
|
+
import { Env, FileLoader, RuntimeContext } from '@travetto/base';
|
|
7
7
|
|
|
8
8
|
import { ImageConverter } from './convert';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Resource provider for images that allows for real-time optimization
|
|
12
12
|
*/
|
|
13
|
-
export class ImageOptimizingResourceLoader extends
|
|
13
|
+
export class ImageOptimizingResourceLoader extends FileLoader {
|
|
14
14
|
|
|
15
15
|
#cacheRoot: string;
|
|
16
16
|
|
|
17
17
|
constructor(paths: string[] = [], cacheRoot?: string) {
|
|
18
|
-
super(paths);
|
|
18
|
+
super([...paths, ...Env.resourcePaths]);
|
|
19
19
|
|
|
20
20
|
this.#cacheRoot = cacheRoot ?? path.resolve(Env.TRV_IMAGE_CACHE.val || RuntimeContext.toolPath('image_cache'));
|
|
21
21
|
}
|