@basaltkit/image-sharp 1.0.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 +21 -0
- package/README.md +75 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +74 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Basalt Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# @basaltkit/image-sharp
|
|
2
|
+
|
|
3
|
+
**Image processing engine** for [`@basaltkit/storage`](https://www.npmjs.com/package/@basaltkit/storage), backed by [sharp](https://sharp.pixelplumbing.com). It implements the storage `ImageProcessor` contract so `disk.image(...)` can resize, rotate, and re-encode to WebP/AVIF/JPEG/PNG.
|
|
4
|
+
|
|
5
|
+
You need this module only when you actually process images. The core `@basaltkit/storage` ships the fluent pipeline and the contract, but **no native dependency** — the heavy `sharp`/`libvips` binary lives here, as an opt-in satellite (see the framework's ARCHITECTURE §9.1).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @basaltkit/image-sharp sharp
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`sharp` is a **peer dependency** (it carries the native `libvips` binary). It's loaded lazily on first use, so a missing install fails fast with a clear message instead of at import time.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
Wire the engine once, then use `disk.image(...)` anywhere:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { storagePlugin } from '@basaltkit/storage'
|
|
21
|
+
import { SharpImageProcessor } from '@basaltkit/image-sharp'
|
|
22
|
+
|
|
23
|
+
storagePlugin({
|
|
24
|
+
imageProcessor: new SharpImageProcessor(),
|
|
25
|
+
disks: { uploads: { driver: 'local', root: './storage' } },
|
|
26
|
+
})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// resize + re-encode + write back to the disk (tenant scope + key guard apply)
|
|
31
|
+
await storage.disk('uploads')
|
|
32
|
+
.image('avatars/1.png')
|
|
33
|
+
.resize(256, 256, { fit: 'cover' })
|
|
34
|
+
.webp(80)
|
|
35
|
+
.save('avatars/1.webp')
|
|
36
|
+
|
|
37
|
+
// or just get the bytes
|
|
38
|
+
const thumb = await storage.disk('uploads').image('hero.jpg').resize(320).jpeg().toBuffer()
|
|
39
|
+
|
|
40
|
+
// read dimensions without re-encoding
|
|
41
|
+
const { width, height, format } = await storage.disk('uploads').image('hero.jpg').metadata()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Do heavy work inside a `@basaltkit/queue` job so it never blocks the request.
|
|
45
|
+
|
|
46
|
+
## Pipeline operations
|
|
47
|
+
|
|
48
|
+
| Method | sharp call | Notes |
|
|
49
|
+
|---|---|---|
|
|
50
|
+
| `.resize(width?, height?, { fit?, position? })` | `resize()` | Omit a dimension to scale by aspect ratio |
|
|
51
|
+
| `.rotate(degrees?)` | `rotate()` | No argument → auto-orient from EXIF |
|
|
52
|
+
| `.blur(sigma?)` | `blur()` | |
|
|
53
|
+
| `.grayscale()` / `.flip()` / `.flop()` | same | |
|
|
54
|
+
| `.webp(q?)` `.jpeg(q?)` `.png(q?)` `.avif(q?)` | encoder | Sets the output format (+ quality) |
|
|
55
|
+
| `.format(fmt, q?)` | encoder | Dynamic form of the above |
|
|
56
|
+
|
|
57
|
+
Terminals: `.toBuffer()`, `.save(path, options?)`, `.metadata()`.
|
|
58
|
+
|
|
59
|
+
## API
|
|
60
|
+
|
|
61
|
+
### `class SharpImageProcessor`
|
|
62
|
+
|
|
63
|
+
`new SharpImageProcessor({ sharp? })` — implements `ImageProcessor` from `@basaltkit/storage`. Pass a `sharp` factory to inject a fake in tests; by default the real `sharp` is lazy-loaded.
|
|
64
|
+
|
|
65
|
+
### `applyOps(image, ops)`
|
|
66
|
+
|
|
67
|
+
The pure op-list → sharp-call translator, exported for testing and advanced embedding.
|
|
68
|
+
|
|
69
|
+
## How it connects
|
|
70
|
+
|
|
71
|
+
`@basaltkit/storage` owns `disk.image(...)`, the fluent `ImagePipeline`, and the `ImageProcessor` interface. This package is one implementation of that interface — swap it for another engine without touching app code.
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ImageProcessor, ImageOp, ImageMetadata } from '@basaltkit/storage';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The slice of sharp's fluent API this driver uses. Declared structurally so the
|
|
5
|
+
* op translator is testable with a fake and the core never imports sharp's types.
|
|
6
|
+
*/
|
|
7
|
+
interface SharpLike {
|
|
8
|
+
resize(options: {
|
|
9
|
+
width?: number;
|
|
10
|
+
height?: number;
|
|
11
|
+
fit?: string;
|
|
12
|
+
position?: string;
|
|
13
|
+
}): SharpLike;
|
|
14
|
+
rotate(degrees?: number): SharpLike;
|
|
15
|
+
blur(sigma?: number): SharpLike;
|
|
16
|
+
grayscale(): SharpLike;
|
|
17
|
+
flip(): SharpLike;
|
|
18
|
+
flop(): SharpLike;
|
|
19
|
+
jpeg(options?: {
|
|
20
|
+
quality?: number;
|
|
21
|
+
}): SharpLike;
|
|
22
|
+
png(options?: {
|
|
23
|
+
quality?: number;
|
|
24
|
+
}): SharpLike;
|
|
25
|
+
webp(options?: {
|
|
26
|
+
quality?: number;
|
|
27
|
+
}): SharpLike;
|
|
28
|
+
avif(options?: {
|
|
29
|
+
quality?: number;
|
|
30
|
+
}): SharpLike;
|
|
31
|
+
toBuffer(): Promise<Buffer>;
|
|
32
|
+
metadata(): Promise<{
|
|
33
|
+
format?: string;
|
|
34
|
+
width?: number;
|
|
35
|
+
height?: number;
|
|
36
|
+
size?: number;
|
|
37
|
+
}>;
|
|
38
|
+
}
|
|
39
|
+
/** `sharp(input)` — the module's callable default export. */
|
|
40
|
+
type SharpFactory = (input: Buffer) => SharpLike;
|
|
41
|
+
/**
|
|
42
|
+
* Applies the engine-neutral op list to a sharp instance, in order. Pure and
|
|
43
|
+
* synchronous over the chainable — unit-tested without the native binary.
|
|
44
|
+
*/
|
|
45
|
+
declare function applyOps(image: SharpLike, ops: readonly ImageOp[]): SharpLike;
|
|
46
|
+
interface SharpImageProcessorOptions {
|
|
47
|
+
/**
|
|
48
|
+
* Injectable sharp factory — the real `sharp` by default (lazy-loaded). Tests
|
|
49
|
+
* pass a fake so no native binary is required.
|
|
50
|
+
*/
|
|
51
|
+
sharp?: SharpFactory;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A {@link ImageProcessor} backed by [sharp](https://sharp.pixelplumbing.com).
|
|
55
|
+
* Wire it into storage: `storagePlugin({ imageProcessor: new SharpImageProcessor(), ... })`.
|
|
56
|
+
* `sharp` is a **peer dependency** (native `libvips`), loaded on first use so the
|
|
57
|
+
* app fails fast with a clear message if it isn't installed.
|
|
58
|
+
*/
|
|
59
|
+
declare class SharpImageProcessor implements ImageProcessor {
|
|
60
|
+
readonly name = "sharp";
|
|
61
|
+
private factory;
|
|
62
|
+
constructor(options?: SharpImageProcessorOptions);
|
|
63
|
+
run(input: Buffer, ops: ImageOp[]): Promise<Buffer>;
|
|
64
|
+
metadata(input: Buffer): Promise<ImageMetadata>;
|
|
65
|
+
private load;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { type SharpFactory, SharpImageProcessor, type SharpImageProcessorOptions, type SharpLike, applyOps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function applyOps(image, ops) {
|
|
3
|
+
let out = image;
|
|
4
|
+
for (const op of ops) {
|
|
5
|
+
switch (op.op) {
|
|
6
|
+
case "resize":
|
|
7
|
+
out = out.resize({
|
|
8
|
+
...op.width !== void 0 ? { width: op.width } : {},
|
|
9
|
+
...op.height !== void 0 ? { height: op.height } : {},
|
|
10
|
+
...op.fit ? { fit: op.fit } : {},
|
|
11
|
+
...op.position ? { position: op.position } : {}
|
|
12
|
+
});
|
|
13
|
+
break;
|
|
14
|
+
case "rotate":
|
|
15
|
+
out = op.degrees === void 0 ? out.rotate() : out.rotate(op.degrees);
|
|
16
|
+
break;
|
|
17
|
+
case "blur":
|
|
18
|
+
out = op.sigma === void 0 ? out.blur() : out.blur(op.sigma);
|
|
19
|
+
break;
|
|
20
|
+
case "grayscale":
|
|
21
|
+
out = out.grayscale();
|
|
22
|
+
break;
|
|
23
|
+
case "flip":
|
|
24
|
+
out = out.flip();
|
|
25
|
+
break;
|
|
26
|
+
case "flop":
|
|
27
|
+
out = out.flop();
|
|
28
|
+
break;
|
|
29
|
+
case "format":
|
|
30
|
+
out = out[op.format](op.quality === void 0 ? {} : { quality: op.quality });
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
var SharpImageProcessor = class {
|
|
37
|
+
name = "sharp";
|
|
38
|
+
factory;
|
|
39
|
+
constructor(options = {}) {
|
|
40
|
+
this.factory = options.sharp;
|
|
41
|
+
}
|
|
42
|
+
async run(input, ops) {
|
|
43
|
+
const sharp = await this.load();
|
|
44
|
+
return applyOps(sharp(input), ops).toBuffer();
|
|
45
|
+
}
|
|
46
|
+
async metadata(input) {
|
|
47
|
+
const sharp = await this.load();
|
|
48
|
+
const m = await sharp(input).metadata();
|
|
49
|
+
return {
|
|
50
|
+
...m.format ? { format: m.format } : {},
|
|
51
|
+
...m.width !== void 0 ? { width: m.width } : {},
|
|
52
|
+
...m.height !== void 0 ? { height: m.height } : {},
|
|
53
|
+
...m.size !== void 0 ? { size: m.size } : {}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async load() {
|
|
57
|
+
if (this.factory) return this.factory;
|
|
58
|
+
try {
|
|
59
|
+
const specifier = "sharp";
|
|
60
|
+
const mod = await import(specifier);
|
|
61
|
+
const factory = typeof mod === "function" ? mod : mod.default ?? mod;
|
|
62
|
+
this.factory = factory;
|
|
63
|
+
return factory;
|
|
64
|
+
} catch {
|
|
65
|
+
throw new Error(
|
|
66
|
+
"@basaltkit/image-sharp requires the 'sharp' peer dependency. Install it with `pnpm add sharp`."
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
export {
|
|
72
|
+
SharpImageProcessor,
|
|
73
|
+
applyOps
|
|
74
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@basaltkit/image-sharp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "sharp-backed image processor for @basaltkit/storage — resize/rotate/blur and JPEG/PNG/WebP/AVIF encoding behind the engine-neutral ImageProcessor contract.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@basaltkit/storage": "^1.2.0"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"sharp": "^0.33.0 || ^0.34.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^22.15.0",
|
|
24
|
+
"tsup": "^8.4.0",
|
|
25
|
+
"typescript": "^5.8.0",
|
|
26
|
+
"vitest": "^3.1.0",
|
|
27
|
+
"@basaltkit/tsconfig": "^0.24.0"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/Zebedeu/basalt.git",
|
|
35
|
+
"directory": "packages/image-sharp"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/Zebedeu/basalt/tree/main/packages/image-sharp#readme",
|
|
38
|
+
"bugs": "https://github.com/Zebedeu/basalt/issues",
|
|
39
|
+
"keywords": [
|
|
40
|
+
"basalt",
|
|
41
|
+
"typescript",
|
|
42
|
+
"image",
|
|
43
|
+
"sharp",
|
|
44
|
+
"resize",
|
|
45
|
+
"webp",
|
|
46
|
+
"avif"
|
|
47
|
+
],
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"typecheck": "tsc --noEmit"
|
|
52
|
+
}
|
|
53
|
+
}
|