@fullstackunicorn/websites 2.1.0 → 3.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 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-sml-v1'
6
- sizesVersion = 1
6
+ recipe = 'webp-lossless-responsive-v2'
7
+ sizesVersion = 2
8
+ minimumWidth = 40
7
9
  widths = {
8
- S1: 64,
9
- S2: 128,
10
- S3: 256,
11
- M1: 320,
12
- M2: 400,
13
- M3: 480,
14
- M4: 640,
15
- M5: 800,
16
- M6: 960,
17
- L1: 1280,
18
- L2: 1920,
19
- L3: 2560
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, 'S1', `${picture.name}.webp`)
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
- const pictureData = {}
66
- pictureData.original = picture.original
67
- pictureData.name = `${picture.name}.webp`
68
- pictureData.x = size.width
69
- pictureData.y = size.height
70
- pictureData.placeholder = await this.getBlurhash({ picture })
71
- pictureData.src = picture.src
72
- pictureData.ratio = size.width / size.height
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,51 @@ 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
95
  .webp({ lossless: true, effort: 4 })
106
96
  .toBuffer()
107
97
  }
108
98
 
109
- generate = async ({ file, folder }) => {
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
+ const entries = [['OO', pictureData.x], ...this.getSizes(sizes).filter(([, width]) => width <= pictureData.x)]
114
+ const generated = await Promise.all(entries.map(async entry => ({
115
+ entry,
116
+ buffer: await this.getBuffer({ picture, entry })
117
+ })))
113
118
 
114
119
  const result = {}
120
+ const hashes = {}
121
+ for (const { entry, buffer } of generated) {
122
+ const key = entry[0]
123
+ hashes[key] = this.hash(buffer)
124
+ this.appendFile(result, key, {
125
+ path: this.getPathThumbnail({ picture, entry }),
126
+ file: buffer
127
+ })
128
+ }
115
129
 
116
- // json
117
130
  this.appendFile(result, 'json', {
118
131
  path: picture.pathJson,
119
- file: pictureData
132
+ file: { ...pictureData, sizes: hashes }
120
133
  })
121
134
 
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
-
135
+ F3.logs(`✅ Created: ${Object.keys(hashes).join(' ')}\n`)
155
136
  return result
156
137
  }
157
138
  }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.1.0",
2
+ "version": "3.0.0",
3
3
  "name": "@fullstackunicorn/websites",
4
4
  "author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
5
5
  "description": "A development utility for generating icons, favicons, thumbnails and all what is needed for websites",
@@ -30,7 +30,6 @@
30
30
  "#@/*": "./*"
31
31
  },
32
32
  "scripts": {
33
- "cmd": "chmod +x ./config.cmd.sh && ./config.cmd.sh",
34
33
  "test": "node --test tests/*.test.js"
35
34
  },
36
35
  "dependencies": {
package/package.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # @fullstackunicorn/websites
2
2
 
3
- Versione sorgente: `2.1.0`. Installazione: `npm install @fullstackunicorn/websites` (registro/permessi del pacchetto richiesti).
3
+ Versione sorgente: `3.0.0`. Installazione: `npm install @fullstackunicorn/websites` (registro/permessi del pacchetto richiesti).
4
4
 
5
- Utility Node per generare immagini e icone con sharp.
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,16 @@ 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.getPicture({ file, folder }) | file.path/file.buffer e destinazione → elaborazione immagine e metadati |
18
+ | websites.images.generate({ file, folder, sizes }) | file.path/file.buffer, destinazione e larghezze richieste → sole varianti disponibili e metadata v2 |
15
19
 
16
20
  ## Formati
17
- Icone: favicon 32, icone 180/192/512. Immagini: jpg/jpeg/png; media sizes version 1 con larghezze S1 64, S2 128, S3 256, M1 320, M2 400, M3 480, M4 640, M5 800, M6 960, L1 1280, L2 1920, L3 2560. Estensione non supportata: errore. Helper interni non sono un contratto stabile separato.
21
+ Icone: favicon 32, icone 180/192/512. Immagini: jpg/jpeg/png; media sizes version 2 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 2.0
26
+ ## Images 3.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.
23
29
 
24
- Original bytes are preserved. Full-resolution and resized display files use lossless WebP, preserving alpha. Each derivative is resized from the source with Sharp (no enlargement). Metadata contains `sizesVersion: 1`, `src` with `/S1/` as a reference template, `full` for the full-resolution WebP, `widths` for available variants, `x`, `y`, `ratio`, `max` (null below 64px), and `placeholder`. Resolve the selected variant before issuing a request. `S1` is not an intermediate download. The image recipe identifier and the sizes version together identify processing caches.
30
+ I metadata pubblici contengono `sizesVersion: 2`, `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.