@fullstackunicorn/websites 2.0.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,18 +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
+ recipe = 'webp-lossless-responsive-v2'
7
+ sizesVersion = 2
8
+ minimumWidth = 40
6
9
  widths = {
7
- S1: 64,
8
- S2: 128,
9
- S3: 256,
10
- M1: 320,
11
- M2: 640,
12
- M3: 960,
13
- L1: 1280,
14
- L2: 1920,
15
- 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
16
25
  }
17
26
 
18
27
  appendFile = (obj, name, data) => {
@@ -29,21 +38,16 @@ class Images {
29
38
  }
30
39
 
31
40
  getPicture = async ({ file, folder }) => {
32
-
33
41
  const picture = {}
34
42
  const pathParsed = path.posix.parse(file.path)
35
43
 
36
44
  picture.dir = pathParsed.dir || ''
37
45
  picture.folder = folder
38
-
39
- picture.original = pathParsed.base
40
46
  picture.ext = this.getExtension(pathParsed)
41
47
  picture.base = sharp(file.buffer)
42
48
  picture.name = pathParsed.name
43
- picture.src = path.posix.join(folder, picture.dir, 'S1', `${picture.name}.webp`)
44
-
49
+ picture.src = path.posix.join(folder, picture.dir, 'S40', `${picture.name}.webp`)
45
50
  picture.pathJson = path.posix.join(picture.dir, `${picture.name}.json`)
46
- picture.pathImg = path.posix.join(picture.dir, `${picture.name}${picture.ext}`)
47
51
 
48
52
  return picture
49
53
  }
@@ -57,20 +61,20 @@ class Images {
57
61
 
58
62
  if (!size.width || isNaN(size.width)) F3.throw('no-width-height', picture.name)
59
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
+ })
60
69
 
61
- const pictureData = {}
62
- pictureData.original = picture.original
63
- pictureData.name = `${picture.name}.webp`
64
- pictureData.x = size.width
65
- pictureData.y = size.height
66
- pictureData.placeholder = await this.getBlurhash({ picture })
67
- pictureData.src = picture.src
68
- pictureData.ratio = size.width / size.height
69
- pictureData.max = this.getMaxSize({ size })
70
- pictureData.full = path.posix.join(picture.folder, picture.dir, `${picture.name}.webp`)
71
- pictureData.widths = Object.fromEntries(Object.entries(this.widths).filter(([, width]) => width <= size.width))
72
-
73
- 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
+ }
74
78
  }
75
79
 
76
80
  getBlurhash = async ({ picture }) => {
@@ -84,71 +88,53 @@ class Images {
84
88
  return blurhash.encode(pixels, info.width, info.height, 4, 4)
85
89
  }
86
90
 
87
- getMaxSize = ({ size }) => {
88
- const sortedSizes = Object.entries(this.widths).sort(([, a], [, b]) => a - b)
89
- let maxSize = null
90
- for (const [key, value] of sortedSizes) {
91
- if (size.width >= value) maxSize = key
92
- else break
93
- }
94
- return maxSize
95
- }
96
-
97
91
  getBuffer = async ({ picture, entry }) => {
92
+ if (entry[0] === 'OO') return picture.base.clone().webp({ lossless: true, effort: 4 }).toBuffer()
98
93
  return picture.base.clone()
99
94
  .resize({ width: entry[1], withoutEnlargement: true })
100
95
  .webp({ lossless: true, effort: 4 })
101
96
  .toBuffer()
102
97
  }
103
98
 
104
- 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')
105
109
 
110
+ generate = async ({ file, folder, sizes }) => {
106
111
  const picture = await this.getPicture({ file, folder })
107
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
+ })))
108
118
 
109
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
+ }
110
129
 
111
- // json
112
130
  this.appendFile(result, 'json', {
113
131
  path: picture.pathJson,
114
- file: pictureData
132
+ file: { ...pictureData, sizes: hashes }
115
133
  })
116
134
 
117
- // original
118
- const originalBuffer = Buffer.from(file.buffer)
119
- this.appendFile(result, 'original', {
120
- path: picture.pathImg,
121
- file: originalBuffer
122
- })
123
-
124
- const fullBuffer = await picture.base.clone().webp({ lossless: true, effort: 4 }).toBuffer()
125
- this.appendFile(result, 'full', {
126
- path: path.posix.join(picture.dir, `${picture.name}.webp`),
127
- file: fullBuffer
128
- })
129
-
130
- const created = []
131
- for (const entry of Object.entries(this.widths)) {
132
-
133
- const key = entry[0]
134
- const w = entry[1]
135
-
136
- if (pictureData.x < w) continue
137
-
138
- const thumbPath = this.getPathThumbnail({ picture, entry })
139
-
140
- let buffer
141
- if (pictureData.x === w) buffer = fullBuffer
142
- else buffer = await this.getBuffer({ picture, entry })
143
-
144
- this.appendFile(result, key, { path: thumbPath, file: buffer })
145
- created.push(key)
146
- }
147
-
148
- F3.logs(`✅ Created: ${created.join(' ')}\n`)
149
-
135
+ F3.logs(`✅ Created: ${Object.keys(hashes).join(' ')}\n`)
150
136
  return result
151
137
  }
152
138
  }
153
139
 
154
- export const images = new Images()
140
+ export const images = new Images()
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.0.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: `1.0.10`. 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; larghezze S1 64, S2 128, S3 256, M1 320, M2 640, M3 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 `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 versions 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.