@byline/core 1.2.0 → 1.2.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/dist/image/image-processor.d.ts +45 -0
- package/dist/image/image-processor.js +138 -0
- package/dist/image/index.d.ts +16 -0
- package/dist/image/index.js +15 -0
- package/package.json +8 -2
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This Source Code is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) Infonomic Company Limited
|
|
7
|
+
*/
|
|
8
|
+
import type { ImageSize } from '../@types/collection-types.js';
|
|
9
|
+
import type { BylineLogger } from '../logger/index.js';
|
|
10
|
+
export interface ImageMeta {
|
|
11
|
+
width: number | null;
|
|
12
|
+
height: number | null;
|
|
13
|
+
format: string | null;
|
|
14
|
+
}
|
|
15
|
+
export interface ImageVariantResult {
|
|
16
|
+
name: string;
|
|
17
|
+
storagePath: string;
|
|
18
|
+
width: number | undefined;
|
|
19
|
+
height: number | undefined;
|
|
20
|
+
format: string;
|
|
21
|
+
}
|
|
22
|
+
export interface ProcessImageResult {
|
|
23
|
+
meta: ImageMeta;
|
|
24
|
+
variants: ImageVariantResult[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns true for MIME types that should skip Sharp processing.
|
|
28
|
+
* SVGs are vector files — Sharp cannot meaningfully resize or convert them,
|
|
29
|
+
* and they do not benefit from the responsive-image pipeline.
|
|
30
|
+
*/
|
|
31
|
+
export declare function isBypassMimeType(mimeType: string): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Extract basic image metadata (dimensions + format) from a buffer using Sharp.
|
|
34
|
+
* Returns nulls for non-image or unrecognised formats.
|
|
35
|
+
*/
|
|
36
|
+
export declare function extractImageMeta(buffer: Buffer, mimeType: string): Promise<ImageMeta>;
|
|
37
|
+
/**
|
|
38
|
+
* Generate the named image variants (sizes) defined in `UploadConfig.sizes`.
|
|
39
|
+
*
|
|
40
|
+
* - SVG and GIF files are skipped entirely (bypass types).
|
|
41
|
+
* - Each variant is written as a sibling file to the original, using the
|
|
42
|
+
* naming convention: `<basename>-<variantName>.<ext>`
|
|
43
|
+
* - Returns an array of `ImageVariantResult` describing what was created.
|
|
44
|
+
*/
|
|
45
|
+
export declare function generateImageVariants(sourceBuffer: Buffer, mimeType: string, absoluteOriginalPath: string, storageBaseDir: string, sizes: ImageSize[], logger?: BylineLogger): Promise<ImageVariantResult[]>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This Source Code is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) Infonomic Company Limited
|
|
7
|
+
*/
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
// Sharp ships its own types; no @types/sharp needed.
|
|
11
|
+
import sharp from 'sharp';
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// SVG detection
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
/**
|
|
16
|
+
* Returns true for MIME types that should skip Sharp processing.
|
|
17
|
+
* SVGs are vector files — Sharp cannot meaningfully resize or convert them,
|
|
18
|
+
* and they do not benefit from the responsive-image pipeline.
|
|
19
|
+
*/
|
|
20
|
+
export function isBypassMimeType(mimeType) {
|
|
21
|
+
return mimeType === 'image/svg+xml' || mimeType === 'image/gif';
|
|
22
|
+
}
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Metadata extraction
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
/**
|
|
27
|
+
* Extract basic image metadata (dimensions + format) from a buffer using Sharp.
|
|
28
|
+
* Returns nulls for non-image or unrecognised formats.
|
|
29
|
+
*/
|
|
30
|
+
export async function extractImageMeta(buffer, mimeType) {
|
|
31
|
+
if (isBypassMimeType(mimeType)) {
|
|
32
|
+
// For SVGs, attempt a lightweight XML parse for width/height attributes.
|
|
33
|
+
const svgMeta = tryParseSvgDimensions(buffer);
|
|
34
|
+
return { width: svgMeta.width, height: svgMeta.height, format: 'svg' };
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const metadata = await sharp(buffer).metadata();
|
|
38
|
+
return {
|
|
39
|
+
width: metadata.width ?? null,
|
|
40
|
+
height: metadata.height ?? null,
|
|
41
|
+
format: metadata.format ?? null,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return { width: null, height: null, format: null };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A lightweight SVG width/height parser — avoids pulling in a full XML library.
|
|
50
|
+
* Only reads the root `<svg>` element's `width` and `height` attributes.
|
|
51
|
+
*/
|
|
52
|
+
function tryParseSvgDimensions(buffer) {
|
|
53
|
+
try {
|
|
54
|
+
const text = buffer.toString('utf8', 0, Math.min(buffer.length, 2048));
|
|
55
|
+
const widthMatch = text.match(/<svg[^>]*\swidth=["']([0-9.]+)(?:px)?["']/);
|
|
56
|
+
const heightMatch = text.match(/<svg[^>]*\sheight=["']([0-9.]+)(?:px)?["']/);
|
|
57
|
+
return {
|
|
58
|
+
width: widthMatch ? Math.round(Number.parseFloat(widthMatch[1])) : null,
|
|
59
|
+
height: heightMatch ? Math.round(Number.parseFloat(heightMatch[1])) : null,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return { width: null, height: null };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// Variant generation
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
/**
|
|
70
|
+
* Generate the named image variants (sizes) defined in `UploadConfig.sizes`.
|
|
71
|
+
*
|
|
72
|
+
* - SVG and GIF files are skipped entirely (bypass types).
|
|
73
|
+
* - Each variant is written as a sibling file to the original, using the
|
|
74
|
+
* naming convention: `<basename>-<variantName>.<ext>`
|
|
75
|
+
* - Returns an array of `ImageVariantResult` describing what was created.
|
|
76
|
+
*/
|
|
77
|
+
export async function generateImageVariants(sourceBuffer, mimeType, absoluteOriginalPath, storageBaseDir, sizes, logger) {
|
|
78
|
+
if (isBypassMimeType(mimeType) || sizes.length === 0) {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
const originalExt = path.extname(absoluteOriginalPath);
|
|
82
|
+
const originalBase = path.basename(absoluteOriginalPath, originalExt);
|
|
83
|
+
const variantDir = path.dirname(absoluteOriginalPath);
|
|
84
|
+
const variants = [];
|
|
85
|
+
for (const size of sizes) {
|
|
86
|
+
const outputFormat = size.format ?? 'webp';
|
|
87
|
+
const outputExt = `.${outputFormat}`;
|
|
88
|
+
const variantFilename = `${originalBase}-${size.name}${outputExt}`;
|
|
89
|
+
const variantAbsolutePath = path.join(variantDir, variantFilename);
|
|
90
|
+
// Derive the storage-relative path (relative to storageBaseDir).
|
|
91
|
+
const variantStoragePath = path
|
|
92
|
+
.relative(storageBaseDir, variantAbsolutePath)
|
|
93
|
+
.replace(/\\/g, '/');
|
|
94
|
+
try {
|
|
95
|
+
let pipeline = sharp(sourceBuffer);
|
|
96
|
+
const resizeOptions = {
|
|
97
|
+
width: size.width,
|
|
98
|
+
height: size.height,
|
|
99
|
+
fit: size.fit ?? 'cover',
|
|
100
|
+
withoutEnlargement: true,
|
|
101
|
+
};
|
|
102
|
+
pipeline = pipeline.resize(resizeOptions);
|
|
103
|
+
// Apply format + quality.
|
|
104
|
+
switch (outputFormat) {
|
|
105
|
+
case 'jpeg':
|
|
106
|
+
pipeline = pipeline.jpeg({ quality: size.quality ?? 85 });
|
|
107
|
+
break;
|
|
108
|
+
case 'png':
|
|
109
|
+
pipeline = pipeline.png({ quality: size.quality ?? 85 });
|
|
110
|
+
break;
|
|
111
|
+
case 'webp':
|
|
112
|
+
pipeline = pipeline.webp({ quality: size.quality ?? 85 });
|
|
113
|
+
break;
|
|
114
|
+
case 'avif':
|
|
115
|
+
pipeline = pipeline.avif({ quality: size.quality ?? 55 });
|
|
116
|
+
break;
|
|
117
|
+
default:
|
|
118
|
+
pipeline = pipeline.webp({ quality: size.quality ?? 85 });
|
|
119
|
+
}
|
|
120
|
+
const variantBuffer = await pipeline.toBuffer();
|
|
121
|
+
fs.mkdirSync(path.dirname(variantAbsolutePath), { recursive: true });
|
|
122
|
+
await fs.promises.writeFile(variantAbsolutePath, variantBuffer);
|
|
123
|
+
const sharpMeta = await sharp(variantBuffer).metadata();
|
|
124
|
+
variants.push({
|
|
125
|
+
name: size.name,
|
|
126
|
+
storagePath: variantStoragePath,
|
|
127
|
+
width: sharpMeta.width,
|
|
128
|
+
height: sharpMeta.height,
|
|
129
|
+
format: outputFormat,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
logger?.error({ err, variant: size.name }, 'failed to generate image variant');
|
|
134
|
+
// Non-fatal: skip this variant but continue with others.
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return variants;
|
|
138
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This Source Code is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) Infonomic Company Limited
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Storage-agnostic image-processing helpers (sharp-backed). Lives in the
|
|
10
|
+
* neutral `@byline/core/image` subpath so any storage provider —
|
|
11
|
+
* `@byline/storage-local`, `@byline/storage-s3`, future providers — can
|
|
12
|
+
* consume the same metadata extraction and variant generation utilities
|
|
13
|
+
* without taking a dependency on a sibling provider package.
|
|
14
|
+
*/
|
|
15
|
+
export { extractImageMeta, generateImageVariants, isBypassMimeType, } from './image-processor.js';
|
|
16
|
+
export type { ImageMeta, ImageVariantResult, ProcessImageResult, } from './image-processor.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This Source Code is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) Infonomic Company Limited
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Storage-agnostic image-processing helpers (sharp-backed). Lives in the
|
|
10
|
+
* neutral `@byline/core/image` subpath so any storage provider —
|
|
11
|
+
* `@byline/storage-local`, `@byline/storage-s3`, future providers — can
|
|
12
|
+
* consume the same metadata extraction and variant generation utilities
|
|
13
|
+
* without taking a dependency on a sibling provider package.
|
|
14
|
+
*/
|
|
15
|
+
export { extractImageMeta, generateImageVariants, isBypassMimeType, } from './image-processor.js';
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@byline/core",
|
|
3
3
|
"private": false,
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
|
-
"version": "1.2.
|
|
5
|
+
"version": "1.2.1",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=20.9.0"
|
|
8
8
|
},
|
|
@@ -61,6 +61,11 @@
|
|
|
61
61
|
"import": "./dist/validation/index.js",
|
|
62
62
|
"require": "./dist/validation/index.js"
|
|
63
63
|
},
|
|
64
|
+
"./image": {
|
|
65
|
+
"types": "./dist/image/index.d.ts",
|
|
66
|
+
"import": "./dist/image/index.js",
|
|
67
|
+
"require": "./dist/image/index.js"
|
|
68
|
+
},
|
|
64
69
|
"./package.json": "./package.json"
|
|
65
70
|
},
|
|
66
71
|
"files": [
|
|
@@ -71,9 +76,10 @@
|
|
|
71
76
|
"jose": "^6.2.3",
|
|
72
77
|
"npm-run-all": "^4.1.5",
|
|
73
78
|
"pino": "^10.3.1",
|
|
79
|
+
"sharp": "^0.34.5",
|
|
74
80
|
"uuid": "^14.0.0",
|
|
75
81
|
"zod": "^4.4.2",
|
|
76
|
-
"@byline/auth": "1.2.
|
|
82
|
+
"@byline/auth": "1.2.1"
|
|
77
83
|
},
|
|
78
84
|
"devDependencies": {
|
|
79
85
|
"@biomejs/biome": "2.4.14",
|