@fullstackunicorn/websites 2.1.0 → 4.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/index.js +3 -1
- package/modules/covers.js +31 -0
- package/modules/images.js +65 -81
- package/package.json +44 -45
- package/package.md +22 -6
package/index.js
CHANGED
|
@@ -2,9 +2,11 @@ import '@fullstackunicorn/initialize'
|
|
|
2
2
|
F3.service = '@fullstackunicorn/websites'
|
|
3
3
|
import { icons } from '#@/modules/icons.js'
|
|
4
4
|
import { images } from '#@/modules/images.js'
|
|
5
|
+
import { covers } from '#@/modules/covers.js'
|
|
5
6
|
|
|
6
7
|
export const websites = {
|
|
7
8
|
icons,
|
|
8
|
-
images
|
|
9
|
+
images,
|
|
10
|
+
covers
|
|
9
11
|
}
|
|
10
12
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { sharp } from '#@/index.dep.js'
|
|
2
|
+
|
|
3
|
+
class Covers {
|
|
4
|
+
|
|
5
|
+
width = 1200
|
|
6
|
+
height = 630
|
|
7
|
+
|
|
8
|
+
generate = async ({ trademark, background = '#ffffff' }) => {
|
|
9
|
+
if (!trademark) throw new Error('cover_trademark_missing')
|
|
10
|
+
const source = Buffer.isBuffer(trademark) ? trademark : Buffer.from(await trademark.arrayBuffer())
|
|
11
|
+
const mark = await sharp(source)
|
|
12
|
+
.resize(720, 180, { fit: 'inside', withoutEnlargement: true })
|
|
13
|
+
.png()
|
|
14
|
+
.toBuffer()
|
|
15
|
+
return await sharp({ create: { width: this.width, height: this.height, channels: 3, background } })
|
|
16
|
+
.composite([{ input: mark, gravity: 'centre' }])
|
|
17
|
+
.jpeg({ quality: 90, chromaSubsampling: '4:4:4' })
|
|
18
|
+
.toBuffer()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
fromImage = async ({ image }) => {
|
|
22
|
+
if (!image) throw new Error('cover_image_missing')
|
|
23
|
+
const source = Buffer.isBuffer(image) ? image : Buffer.from(await image.arrayBuffer())
|
|
24
|
+
return await sharp(source)
|
|
25
|
+
.resize(this.width, this.height, { fit: 'cover', position: 'attention' })
|
|
26
|
+
.jpeg({ quality: 90, chromaSubsampling: '4:4:4' })
|
|
27
|
+
.toBuffer()
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const covers = new Covers()
|
package/modules/images.js
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
1
2
|
import { path, sharp, blurhash } from '#@/index.dep.js'
|
|
2
3
|
|
|
3
4
|
class Images {
|
|
4
5
|
extensions = ['.jpg', '.png']
|
|
5
|
-
recipe = 'webp-lossless-
|
|
6
|
-
sizesVersion =
|
|
6
|
+
recipe = 'webp-q85-e4-smart-original-lossless-v3'
|
|
7
|
+
sizesVersion = 3
|
|
8
|
+
minimumWidth = 40
|
|
7
9
|
widths = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
S40: 40,
|
|
11
|
+
S80: 80,
|
|
12
|
+
S160: 160,
|
|
13
|
+
S320: 320,
|
|
14
|
+
S400: 400,
|
|
15
|
+
S480: 480,
|
|
16
|
+
S640: 640,
|
|
17
|
+
S800: 800,
|
|
18
|
+
S960: 960,
|
|
19
|
+
S1280: 1280,
|
|
20
|
+
S1600: 1600,
|
|
21
|
+
S1920: 1920,
|
|
22
|
+
S2560: 2560,
|
|
23
|
+
S3200: 3200,
|
|
24
|
+
S3840: 3840
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
appendFile = (obj, name, data) => {
|
|
@@ -33,21 +38,16 @@ class Images {
|
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
getPicture = async ({ file, folder }) => {
|
|
36
|
-
|
|
37
41
|
const picture = {}
|
|
38
42
|
const pathParsed = path.posix.parse(file.path)
|
|
39
43
|
|
|
40
44
|
picture.dir = pathParsed.dir || ''
|
|
41
45
|
picture.folder = folder
|
|
42
|
-
|
|
43
|
-
picture.original = pathParsed.base
|
|
44
46
|
picture.ext = this.getExtension(pathParsed)
|
|
45
47
|
picture.base = sharp(file.buffer)
|
|
46
48
|
picture.name = pathParsed.name
|
|
47
|
-
picture.src = path.posix.join(folder, picture.dir, '
|
|
48
|
-
|
|
49
|
+
picture.src = path.posix.join(folder, picture.dir, 'S40', `${picture.name}.webp`)
|
|
49
50
|
picture.pathJson = path.posix.join(picture.dir, `${picture.name}.json`)
|
|
50
|
-
picture.pathImg = path.posix.join(picture.dir, `${picture.name}${picture.ext}`)
|
|
51
51
|
|
|
52
52
|
return picture
|
|
53
53
|
}
|
|
@@ -61,21 +61,20 @@ class Images {
|
|
|
61
61
|
|
|
62
62
|
if (!size.width || isNaN(size.width)) F3.throw('no-width-height', picture.name)
|
|
63
63
|
if (!size.height || isNaN(size.height)) F3.throw('no-height-height', picture.name)
|
|
64
|
+
if (size.width < this.minimumWidth) F3.throw('image-below-minimum-width', {
|
|
65
|
+
name: picture.name,
|
|
66
|
+
width: size.width,
|
|
67
|
+
minimumWidth: this.minimumWidth
|
|
68
|
+
})
|
|
64
69
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
pictureData.max = this.getMaxSize({ size })
|
|
74
|
-
pictureData.sizesVersion = this.sizesVersion
|
|
75
|
-
pictureData.full = path.posix.join(picture.folder, picture.dir, `${picture.name}.webp`)
|
|
76
|
-
pictureData.widths = Object.fromEntries(Object.entries(this.widths).filter(([, width]) => width <= size.width))
|
|
77
|
-
|
|
78
|
-
return pictureData
|
|
70
|
+
return {
|
|
71
|
+
x: size.width,
|
|
72
|
+
y: size.height,
|
|
73
|
+
placeholder: await this.getBlurhash({ picture }),
|
|
74
|
+
src: picture.src,
|
|
75
|
+
ratio: size.width / size.height,
|
|
76
|
+
sizesVersion: this.sizesVersion
|
|
77
|
+
}
|
|
79
78
|
}
|
|
80
79
|
|
|
81
80
|
getBlurhash = async ({ picture }) => {
|
|
@@ -89,69 +88,54 @@ class Images {
|
|
|
89
88
|
return blurhash.encode(pixels, info.width, info.height, 4, 4)
|
|
90
89
|
}
|
|
91
90
|
|
|
92
|
-
getMaxSize = ({ size }) => {
|
|
93
|
-
const sortedSizes = Object.entries(this.widths).sort(([, a], [, b]) => a - b)
|
|
94
|
-
let maxSize = null
|
|
95
|
-
for (const [key, value] of sortedSizes) {
|
|
96
|
-
if (size.width >= value) maxSize = key
|
|
97
|
-
else break
|
|
98
|
-
}
|
|
99
|
-
return maxSize
|
|
100
|
-
}
|
|
101
|
-
|
|
102
91
|
getBuffer = async ({ picture, entry }) => {
|
|
92
|
+
if (entry[0] === 'OO') return picture.base.clone().webp({ lossless: true, effort: 4 }).toBuffer()
|
|
103
93
|
return picture.base.clone()
|
|
104
94
|
.resize({ width: entry[1], withoutEnlargement: true })
|
|
105
|
-
.webp({ lossless:
|
|
95
|
+
.webp({ lossless: false, quality: 85, effort: 4, smartSubsample: true })
|
|
106
96
|
.toBuffer()
|
|
107
97
|
}
|
|
108
98
|
|
|
109
|
-
|
|
99
|
+
getSizes = sizes => {
|
|
100
|
+
if (!Array.isArray(sizes)) F3.throw('image-sizes-required', { sizes })
|
|
101
|
+
const selected = new Set(['S40', ...sizes])
|
|
102
|
+
for (const size of selected) {
|
|
103
|
+
if (!this.widths[size]) F3.throw('unknown-image-size', { size, sizes: [...selected] })
|
|
104
|
+
}
|
|
105
|
+
return Object.entries(this.widths).filter(([size]) => selected.has(size))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
hash = buffer => createHash('sha256').update(buffer).digest('hex')
|
|
110
109
|
|
|
110
|
+
generate = async ({ file, folder, sizes }) => {
|
|
111
111
|
const picture = await this.getPicture({ file, folder })
|
|
112
112
|
const pictureData = await this.getPictureData({ picture })
|
|
113
|
+
// A lossy display original covers small/non-ladder sources without
|
|
114
|
+
// making clients fall back to the download-only lossless OO.
|
|
115
|
+
const displaySizes = new Map(this.getSizes(sizes).filter(([, width]) => width <= pictureData.x))
|
|
116
|
+
displaySizes.set(`S${pictureData.x}`, pictureData.x)
|
|
117
|
+
const entries = [['OO', pictureData.x], ...[...displaySizes].sort((a, b) => a[1] - b[1])]
|
|
118
|
+
const generated = await Promise.all(entries.map(async entry => ({
|
|
119
|
+
entry,
|
|
120
|
+
buffer: await this.getBuffer({ picture, entry })
|
|
121
|
+
})))
|
|
113
122
|
|
|
114
123
|
const result = {}
|
|
124
|
+
const hashes = {}
|
|
125
|
+
for (const { entry, buffer } of generated) {
|
|
126
|
+
const key = entry[0]
|
|
127
|
+
hashes[key] = this.hash(buffer)
|
|
128
|
+
this.appendFile(result, key, {
|
|
129
|
+
path: this.getPathThumbnail({ picture, entry }),
|
|
130
|
+
file: buffer
|
|
131
|
+
})
|
|
132
|
+
}
|
|
115
133
|
|
|
116
|
-
// json
|
|
117
134
|
this.appendFile(result, 'json', {
|
|
118
135
|
path: picture.pathJson,
|
|
119
|
-
file: pictureData
|
|
136
|
+
file: { ...pictureData, sizes: hashes }
|
|
120
137
|
})
|
|
121
138
|
|
|
122
|
-
// original
|
|
123
|
-
const originalBuffer = Buffer.from(file.buffer)
|
|
124
|
-
this.appendFile(result, 'original', {
|
|
125
|
-
path: picture.pathImg,
|
|
126
|
-
file: originalBuffer
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
const fullBuffer = await picture.base.clone().webp({ lossless: true, effort: 4 }).toBuffer()
|
|
130
|
-
this.appendFile(result, 'full', {
|
|
131
|
-
path: path.posix.join(picture.dir, `${picture.name}.webp`),
|
|
132
|
-
file: fullBuffer
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
const created = []
|
|
136
|
-
for (const entry of Object.entries(this.widths)) {
|
|
137
|
-
|
|
138
|
-
const key = entry[0]
|
|
139
|
-
const w = entry[1]
|
|
140
|
-
|
|
141
|
-
if (pictureData.x < w) continue
|
|
142
|
-
|
|
143
|
-
const thumbPath = this.getPathThumbnail({ picture, entry })
|
|
144
|
-
|
|
145
|
-
let buffer
|
|
146
|
-
if (pictureData.x === w) buffer = fullBuffer
|
|
147
|
-
else buffer = await this.getBuffer({ picture, entry })
|
|
148
|
-
|
|
149
|
-
this.appendFile(result, key, { path: thumbPath, file: buffer })
|
|
150
|
-
created.push(key)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
F3.logs(`✅ Created: ${created.join(' ')}\n`)
|
|
154
|
-
|
|
155
139
|
return result
|
|
156
140
|
}
|
|
157
141
|
}
|
package/package.json
CHANGED
|
@@ -1,46 +1,45 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
2
|
+
"version": "4.0.0",
|
|
3
|
+
"name": "@fullstackunicorn/websites",
|
|
4
|
+
"author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
|
|
5
|
+
"description": "A development utility for generating icons, favicons, thumbnails and all what is needed for websites",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"packageManager": "yarn@4.18.1",
|
|
9
|
+
"readme": "package.md",
|
|
10
|
+
"main": "index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"index.dep.js",
|
|
14
|
+
"modules/",
|
|
15
|
+
"package.md"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://gitlab.com/fullstackunicorn/websites.git"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"icon-generator",
|
|
23
|
+
"favicon-generator",
|
|
24
|
+
"react-native",
|
|
25
|
+
"react",
|
|
26
|
+
"fullstackunicorn"
|
|
27
|
+
],
|
|
28
|
+
"imports": {
|
|
29
|
+
"#@": "./",
|
|
30
|
+
"#@/*": "./*"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"test": "node --test tests/*.test.js"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@fullstackunicorn/filesystem": "^1.0.4",
|
|
37
|
+
"@fullstackunicorn/initialize": "^1.0.7",
|
|
38
|
+
"blurhash": "^2.0.5",
|
|
39
|
+
"sharp": "^0.34.5"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@fullstackunicorn/cli": "^1.0.3",
|
|
43
|
+
"@fullstackunicorn/reduce": "^1.0.19"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/package.md
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
# @fullstackunicorn/websites
|
|
2
2
|
|
|
3
|
-
Versione sorgente: `
|
|
3
|
+
Versione sorgente: `4.0.0`. Installazione: `yarn add @fullstackunicorn/websites` (registro/permessi del pacchetto richiesti).
|
|
4
4
|
|
|
5
|
-
Utility Node per generare immagini e
|
|
5
|
+
Utility Node per generare immagini, icone e cover sociali condivise con sharp.
|
|
6
|
+
|
|
7
|
+
`websites.covers.generate({ trademark, background })` produce una cover JPEG deterministica `1200×630`, con il trademark esistente centrato. Serve come fallback dichiarato quando il sito non dispone di cover specifiche; non aggiunge testo o identità inventati.
|
|
8
|
+
|
|
9
|
+
`websites.covers.fromImage({ image })` normalizza una fotografia già approvata a `1200×630` per i renderer importati che possiedono immagini ma non un trademark grafico.
|
|
6
10
|
|
|
7
11
|
## API
|
|
8
12
|
```js
|
|
@@ -11,14 +15,26 @@ import { websites } from '@fullstackunicorn/websites'
|
|
|
11
15
|
| Metodo | Input → risultato |
|
|
12
16
|
|---|---|
|
|
13
17
|
| websites.icons.generate({ favicon, icon }) | Oggetti con name/buffer PNG o SVG → mappa di file generati |
|
|
14
|
-
| websites.images.
|
|
18
|
+
| websites.images.generate({ file, folder, sizes }) | file.path/file.buffer, destinazione e larghezze richieste → sole varianti disponibili e metadata v3 |
|
|
15
19
|
|
|
16
20
|
## Formati
|
|
17
|
-
Icone: favicon 32, icone 180/192/512. Immagini: jpg/jpeg/png; media sizes version
|
|
21
|
+
Icone: favicon 32, icone 180/192/512. Immagini: jpg/jpeg/png; media sizes version 3 con chiavi che dichiarano direttamente la larghezza: S40, S80, S160, S320, S400, S480, S640, S800, S960, S1280, S1600, S1920, S2560, S3200 e S3840. Estensione non supportata o originale più stretto di 40 px: errore. Helper interni non sono un contratto stabile separato.
|
|
18
22
|
|
|
19
23
|
## Requisiti e limiti
|
|
20
24
|
Node con dipendenze native sharp; non importare nel bundle browser. L’entrypoint carica initialize e modifica F3.service nel codice legacy. Verificare file reali e output prima di adozione; nessun nuovo collaudo implicito in questa documentazione.
|
|
21
25
|
|
|
22
|
-
## Images
|
|
26
|
+
## Images 4.0
|
|
27
|
+
|
|
28
|
+
`generate({ file, folder, sizes })` richiede l'elenco delle sole varianti necessarie al progetto. `S40` è sempre aggiunta e garantita; `OO` è sempre il WebP lossless alla risoluzione originale. Ogni derivata nasce direttamente dalla sorgente con Sharp e `withoutEnlargement`; le varianti richieste più larghe dell'originale non vengono create.
|
|
29
|
+
|
|
30
|
+
I metadata pubblici contengono `sizesVersion: 3`, `src` con `/S40/` come template stabile, `sizes` come mappa diretta `{ OO: hash, S40: hash, ... }`, `x`, `y`, `ratio` e `placeholder`. L'assenza di una chiave in `sizes` significa che la variante non esiste. Non esistono più i campi pubblici `max`, `full`, `widths`, `hashes` o `original`. Il chiamante ricava disponibilità e hash dalla stessa mappa e sostituisce `/S40/` nel path soltanto dopo avere scelto una chiave presente.
|
|
31
|
+
|
|
32
|
+
## Ricetta e visualizzazione
|
|
33
|
+
|
|
34
|
+
Ricetta `webp-q85-e4-smart-original-lossless-v3`: il sorgente resta invariato, `OO` usa `lossless:true, effort:4`; ogni `S<n>` usa `lossless:false, quality:85, effort:4, smartSubsample:true`. Ogni variante deriva direttamente dal sorgente. Q85 non significa conservare l’85% della qualità.
|
|
35
|
+
|
|
36
|
+
Oltre a `S40` e alla selezione responsive, il generatore produce sempre `S<larghezza sorgente>` lossy, deduplicandola se è già richiesta. Esempi: una sorgente da 600 px produce `S600`; una da 661 px produce `S661`. Questa aggiunta automatica non estende la scala accettata da `getSizes`: la selezione responsive resta calcolata dalla Geometry sulla scala canonica. Nessun upscale.
|
|
37
|
+
|
|
38
|
+
`OO` è riservato al download esplicito, non è un fallback per display, zoom, preload o riuso di un’immagine già caricata. I selettori condivisi accettano le chiavi numeriche `S<n>` realmente presenti e non superiori a `picture.x`. Per target maggiori del sorgente scelgono il più grande `S<n>` disponibile. Gli ingrandimenti a schermo restano lossy; il download dell’originale offre il sorgente o `OO`.
|
|
23
39
|
|
|
24
|
-
|
|
40
|
+
La ricetta e `sizesVersion` partecipano alla cache di conversione. Il cambio da v2 richiede rigenerazione dei descrittori e allineamento del selettore condiviso prima dell’attivazione. La versione sorgente non prova la pubblicazione NPM né l’attivazione dei siti.
|