uniweb 1.34.7 → 1.34.8
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uniweb",
|
|
3
|
-
"version": "1.34.
|
|
3
|
+
"version": "1.34.8",
|
|
4
4
|
"description": "Create structured Vite + React sites with content/code separation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -42,14 +42,14 @@
|
|
|
42
42
|
"prompts": "^2.4.2",
|
|
43
43
|
"tar": "^7.5.21",
|
|
44
44
|
"@uniweb/content-writer": "~1.3.2",
|
|
45
|
+
"@uniweb/semantic-parser": "~1.13.5",
|
|
45
46
|
"@uniweb/core": "~1.6.13",
|
|
46
|
-
"@uniweb/schemas": "~1.7.4"
|
|
47
|
-
"@uniweb/semantic-parser": "~1.13.5"
|
|
47
|
+
"@uniweb/schemas": "~1.7.4"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
+
"@uniweb/build": "~1.23.7",
|
|
50
51
|
"@uniweb/content-reader": "~1.5.10",
|
|
51
|
-
"@uniweb/semantic-parser": "~1.13.5"
|
|
52
|
-
"@uniweb/build": "~1.23.6"
|
|
52
|
+
"@uniweb/semantic-parser": "~1.13.5"
|
|
53
53
|
},
|
|
54
54
|
"peerDependenciesMeta": {
|
|
55
55
|
"@uniweb/build": {
|
|
@@ -131,7 +131,7 @@ Image attributes: `{width=800 height=600 loading=lazy fit=cover position=center
|
|
|
131
131
|
|
|
132
132
|
**Section links:** `page:about#team` → `/about#section-team`; `page:/#features` → a homepage section. The id is the file name without its number (`2-features.md` → `features`) or `id:` in frontmatter.
|
|
133
133
|
|
|
134
|
-
**Where media lives.** `./x` is beside the page, `/x` is in `public/`; both reach a static build and a published site. Keep a page's media beside it, and in `public/` what belongs to the whole site: the favicon, fonts. **Only a static host gives a file a fixed address** at the site root. A site published with Uniweb serves a file that something names from an address the upload gives it, and nothing at `/x` — so for a file other sites or scripts fetch by URL, keep it somewhere with its own address and add a page whose `redirect:` goes there. A static build copies `public/` whole, a photo's location included, and adds its own copy of each file a page names there: keep a phone's photos beside their page. **A picture is made ready before it is stored or shipped:** `uniweb push` and `uniweb publish` upload, and a static build copies, a PNG, JPEG or GIF as a WebP when that is at least 10% smaller (a TIFF or HEIC always), turned upright and without its location and camera data; a WebP stays one, without that data. The favicon, the touch icon, the site's card and its social images go up in their own format, without their location and camera data; a static build serves them from `public/` as they are. A picture keeps its pixels, so add one no wider than a page shows it; a HEIC from a phone that can't be converted here goes up as it is, with a note: export it as JPEG. `uniweb push` uploads a file beside a page wherever the page is (mounted directories included); a file outside the site and its mounts is not uploaded — put that in `public/`. There is no `assets/` directory for media; a build stops on a `/x` only `assets/` holds.
|
|
134
|
+
**Where media lives.** `./x` is beside the page, `/x` is in `public/`; both reach a static build and a published site. Keep a page's media beside it, and in `public/` what belongs to the whole site: the favicon, fonts. **Only a static host gives a file a fixed address** at the site root. A site published with Uniweb serves a file that something names from an address the upload gives it, and nothing at `/x` — so for a file other sites or scripts fetch by URL, keep it somewhere with its own address and add a page whose `redirect:` goes there. A static build copies `public/` whole, a photo's location included, and adds its own copy of each file a page names there: keep a phone's photos beside their page. **A picture is made ready before it is stored or shipped:** `uniweb push` and `uniweb publish` upload, and a static build copies, a PNG, JPEG or GIF as a WebP when that is at least 10% smaller (a TIFF or HEIC always), turned upright and without its location and camera data; a WebP stays one, without that data. The favicon, the touch icon, the site's card and its social images go up in their own format, without their location and camera data; a static build serves them from `public/` as they are. **A video** goes up as H.264 and AAC in an MP4, at most 1920 on its long edge and about 5 Mbps at 1080p, when ffmpeg with libx264 is installed (one already within that is only repackaged). It loses its location and camera data either way, an HDR video goes up as it is with a note, and a static build copies videos as they are. A picture keeps its pixels, so add one no wider than a page shows it; a HEIC from a phone that can't be converted here goes up as it is, with a note: export it as JPEG. `uniweb push` uploads a file beside a page wherever the page is (mounted directories included); a file outside the site and its mounts is not uploaded — put that in `public/`. There is no `assets/` directory for media; a build stops on a `/x` only `assets/` holds.
|
|
135
135
|
|
|
136
136
|
**Placement** (`{placement=left|right|wide|full}`) on an image or video, or on a table's caption line, which follows the table after a blank line (`: Plans and prices {placement=wide #tbl-plans}`, which also sets caption and id; with no blank line it becomes a row). It's the author's intent, honoured by kit's `<Render>`, `<Prose>` and `<Article>` and ignored by other components; a section type declares which it honours in `meta.js` (`sequence: { placement: true }`) and the build warns on a mismatch.
|
|
137
137
|
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A VIDEO MADE READY BEFORE IT IS UPLOADED — the video half of the rule a push applies to what it
|
|
3
|
+
* uploads, which only a client applies, since a host converts no video:
|
|
4
|
+
*
|
|
5
|
+
* 1. **H.264 at CRF 23**, 8-bit 4:2:0, with AAC-LC at 128 kbps in stereo, in an MP4 whose index comes
|
|
6
|
+
* first, so a page can start playing it before it has all of it.
|
|
7
|
+
* 2. **A long edge of at most 1920**, never enlarged, with a phone's rotation applied to the pixels.
|
|
8
|
+
* 3. **A bitrate ceiling** of 5 Mbps at 1080p, scaled by the frame's size (about 2.2 Mbps at 720p). An
|
|
9
|
+
* encode that comes out over it is made again at the ceiling.
|
|
10
|
+
* 4. **Location and camera data are dropped**, as from a picture.
|
|
11
|
+
* 5. **A video already within the rule** (H.264 with AAC or no sound, at most 1920 on its long edge, at
|
|
12
|
+
* or under the ceiling) is remuxed only: into an MP4, without its private data, its index first.
|
|
13
|
+
* 6. **An HDR video** (HLG, PQ or Dolby Vision) is remuxed in its own container and kept as it is,
|
|
14
|
+
* with a note. Making one SDR needs tone mapping, and no chain for it is measured here yet.
|
|
15
|
+
* 7. **With no `ffmpeg` that has `libx264`**, an MP4 or QuickTime file still loses its location and
|
|
16
|
+
* camera data: its metadata boxes are blanked in place, so nothing else in it moves
|
|
17
|
+
* (`blankVideoMetadata`). It goes up otherwise as it is, with a note when it is heavy.
|
|
18
|
+
*
|
|
19
|
+
* The quality target repeats byte for byte, and so does an encode at the ceiling, so a prepared video
|
|
20
|
+
* has one identity wherever the same `ffmpeg` makes it. A cap during the encode (`-maxrate`) did not
|
|
21
|
+
* repeat with more than one thread (measured with Homebrew's ffmpeg 8.1, 2026-11-12).
|
|
22
|
+
*
|
|
23
|
+
* `ffmpeg` and `ffprobe` run with their output captured, never handed ours, so a reader that leaves
|
|
24
|
+
* can't stop them (`utils/closed-reader.js`).
|
|
25
|
+
*/
|
|
26
|
+
import { execFile } from 'node:child_process'
|
|
27
|
+
import { mkdtemp, readFile, rm } from 'node:fs/promises'
|
|
28
|
+
import { tmpdir } from 'node:os'
|
|
29
|
+
import { join } from 'node:path'
|
|
30
|
+
import { promisify } from 'node:util'
|
|
31
|
+
|
|
32
|
+
const run = promisify(execFile)
|
|
33
|
+
|
|
34
|
+
/** The rule's numbers. */
|
|
35
|
+
export const VIDEO_RULE = Object.freeze({
|
|
36
|
+
crf: 23,
|
|
37
|
+
longEdge: 1920,
|
|
38
|
+
/** Bits a second at 1920×1080; the ceiling scales with the frame's area. */
|
|
39
|
+
ceilingAt1080p: 5_000_000,
|
|
40
|
+
audioBitrate: '128k',
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
/** Containers the rule prepares, by extension. */
|
|
44
|
+
const PREPARED = new Set(['mp4', 'm4v', 'mov', 'webm'])
|
|
45
|
+
|
|
46
|
+
/** Containers whose metadata boxes can be blanked in place. */
|
|
47
|
+
const BOXED = new Set(['mp4', 'm4v', 'mov'])
|
|
48
|
+
|
|
49
|
+
const extOf = (ext) => String(ext || '').toLowerCase().replace(/^\./, '')
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether the rule prepares a video with this extension.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} ext - `mp4`, `.mov` or `MP4`
|
|
55
|
+
* @returns {boolean}
|
|
56
|
+
*/
|
|
57
|
+
export function isPreparableVideo(ext) {
|
|
58
|
+
return PREPARED.has(extOf(ext))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The ceiling for a frame of this size, in bits a second. */
|
|
62
|
+
export function ceilingFor(width, height) {
|
|
63
|
+
return Math.round((VIDEO_RULE.ceilingAt1080p * width * height) / (1920 * 1080))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The frame a video is encoded to: at most `longEdge` on its long edge, its sides even. */
|
|
67
|
+
function encodedSize(width, height) {
|
|
68
|
+
const scale = Math.min(1, VIDEO_RULE.longEdge / Math.max(width, height))
|
|
69
|
+
if (scale === 1) return [width, height]
|
|
70
|
+
const even = (n) => Math.max(2, Math.round((n * scale) / 2) * 2)
|
|
71
|
+
return [even(width), even(height)]
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let tools // undefined: not looked for yet; null: none here
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The `ffmpeg` and `ffprobe` this machine has, when its `ffmpeg` has `libx264` and `aac`. Some Linux
|
|
78
|
+
* packages leave `libx264` out, and such a build counts as none. Looked for once.
|
|
79
|
+
*
|
|
80
|
+
* @returns {Promise<{ ffmpeg: string, ffprobe: string } | null>}
|
|
81
|
+
*/
|
|
82
|
+
export async function findVideoTools() {
|
|
83
|
+
if (tools !== undefined) return tools
|
|
84
|
+
try {
|
|
85
|
+
const { stdout } = await run('ffmpeg', ['-hide_banner', '-encoders'], { maxBuffer: 4 << 20 })
|
|
86
|
+
await run('ffprobe', ['-version'])
|
|
87
|
+
tools = / libx264 /.test(stdout) && / aac /.test(stdout) ? { ffmpeg: 'ffmpeg', ffprobe: 'ffprobe' } : null
|
|
88
|
+
} catch {
|
|
89
|
+
tools = null
|
|
90
|
+
}
|
|
91
|
+
return tools
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** What `ffprobe` says of a file: its first video stream, its first audio stream and its format. */
|
|
95
|
+
async function probe(ffprobe, path) {
|
|
96
|
+
const { stdout } = await run(
|
|
97
|
+
ffprobe,
|
|
98
|
+
['-v', 'error', '-print_format', 'json', '-show_format', '-show_streams', path],
|
|
99
|
+
{ maxBuffer: 16 << 20 }
|
|
100
|
+
)
|
|
101
|
+
const info = JSON.parse(stdout)
|
|
102
|
+
const streams = info.streams || []
|
|
103
|
+
return {
|
|
104
|
+
video: streams.find((s) => s.codec_type === 'video' && !s.disposition?.attached_pic),
|
|
105
|
+
audio: streams.find((s) => s.codec_type === 'audio'),
|
|
106
|
+
format: info.format || {},
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const HDR_TRANSFERS = new Set(['arib-std-b67', 'smpte2084'])
|
|
111
|
+
|
|
112
|
+
function isHdr(video) {
|
|
113
|
+
if (HDR_TRANSFERS.has(video.color_transfer)) return true
|
|
114
|
+
return (video.side_data_list || []).some((d) => /DOVI/i.test(d.side_data_type || ''))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A stream's bitrate, or the format's less the sound's when the stream states none. */
|
|
118
|
+
function videoBitrate({ video, audio, format }) {
|
|
119
|
+
const own = Number(video.bit_rate)
|
|
120
|
+
if (own > 0) return own
|
|
121
|
+
return Math.max(0, (Number(format.bit_rate) || 0) - (Number(audio?.bit_rate) || 0))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Whether a probed video is within the rule, its container apart. */
|
|
125
|
+
function withinRule(probed) {
|
|
126
|
+
const { video, audio } = probed
|
|
127
|
+
const width = Number(video.width)
|
|
128
|
+
const height = Number(video.height)
|
|
129
|
+
return (
|
|
130
|
+
video.codec_name === 'h264' &&
|
|
131
|
+
(video.pix_fmt === 'yuv420p' || video.pix_fmt === 'yuvj420p') &&
|
|
132
|
+
(!audio || audio.codec_name === 'aac') &&
|
|
133
|
+
Math.max(width, height) <= VIDEO_RULE.longEdge &&
|
|
134
|
+
videoBitrate(probed) <= ceilingFor(width, height)
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const QUIET = ['-hide_banner', '-loglevel', 'error', '-nostdin', '-y']
|
|
139
|
+
// Only the picture and the sound go, without the file's metadata, chapters or other tracks, and with
|
|
140
|
+
// no encoder version written, so the same input gives the same bytes.
|
|
141
|
+
const KEEP = ['-map', '0:v:0', '-map', '0:a:0?', '-map_metadata', '-1', '-map_chapters', '-1']
|
|
142
|
+
const FINISH = ['-movflags', '+faststart', '-fflags', '+bitexact']
|
|
143
|
+
|
|
144
|
+
// WebM has no index to move first.
|
|
145
|
+
const remux = (input, output, { webm = false } = {}) => [
|
|
146
|
+
...QUIET, '-i', input, ...KEEP, '-c', 'copy', ...(webm ? ['-fflags', '+bitexact'] : FINISH), output,
|
|
147
|
+
]
|
|
148
|
+
|
|
149
|
+
function encode(input, output, rate) {
|
|
150
|
+
const edge = VIDEO_RULE.longEdge
|
|
151
|
+
const fit = `scale=w=if(gte(iw\\,ih)\\,min(${edge}\\,iw)\\,-2):h=if(gte(iw\\,ih)\\,-2\\,min(${edge}\\,ih)),format=yuv420p`
|
|
152
|
+
return [
|
|
153
|
+
...QUIET, '-i', input, ...KEEP,
|
|
154
|
+
'-vf', fit,
|
|
155
|
+
'-c:v', 'libx264', '-preset', 'medium', '-profile:v', 'high', ...rate,
|
|
156
|
+
'-c:a', 'aac', '-b:a', VIDEO_RULE.audioBitrate, '-ac', '2',
|
|
157
|
+
...FINISH, output,
|
|
158
|
+
]
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Prepare a video under the rule.
|
|
163
|
+
*
|
|
164
|
+
* @param {string} path - the file on disk, which `ffmpeg` reads
|
|
165
|
+
* @param {Buffer} bytes - the same file, for when there is no `ffmpeg`
|
|
166
|
+
* @param {string} ext - its extension
|
|
167
|
+
* @param {{ tools?: { ffmpeg: string, ffprobe: string } | null, onProgress?: (m: string) => void }} [options]
|
|
168
|
+
* `tools`: what `findVideoTools` found, or `null` to prepare without them
|
|
169
|
+
* @returns {Promise<{ bytes: Buffer, ext: string, converted: boolean, note?: string }>} `converted`:
|
|
170
|
+
* encoded again. `note`: what to tell the author.
|
|
171
|
+
*/
|
|
172
|
+
export async function prepareVideo(path, bytes, ext, { tools: found, onProgress } = {}) {
|
|
173
|
+
const kind = extOf(ext)
|
|
174
|
+
const available = found === undefined ? await findVideoTools() : found
|
|
175
|
+
if (!available) return withoutTools(bytes, kind)
|
|
176
|
+
|
|
177
|
+
let probed
|
|
178
|
+
try {
|
|
179
|
+
probed = await probe(available.ffprobe, path)
|
|
180
|
+
} catch {
|
|
181
|
+
return { ...withoutTools(bytes, kind), note: "is uploaded as it is: ffmpeg couldn't read it, so check that it plays." }
|
|
182
|
+
}
|
|
183
|
+
// Sound alone: nothing to encode, and its metadata blanked where its container allows.
|
|
184
|
+
if (!probed.video) return { bytes: (BOXED.has(kind) && blankVideoMetadata(bytes)?.bytes) || bytes, ext: kind, converted: false }
|
|
185
|
+
|
|
186
|
+
const dir = await mkdtemp(join(tmpdir(), 'uniweb-video-'))
|
|
187
|
+
try {
|
|
188
|
+
if (isHdr(probed.video)) {
|
|
189
|
+
const out = join(dir, `remuxed.${kind}`)
|
|
190
|
+
await run(available.ffmpeg, remux(path, out, { webm: kind === 'webm' }))
|
|
191
|
+
return {
|
|
192
|
+
bytes: await readFile(out),
|
|
193
|
+
ext: kind,
|
|
194
|
+
converted: false,
|
|
195
|
+
note: "is HDR, so it goes up as it is: it can't be made SDR here. Export it as SDR (HD or 1080p) for a smaller file most browsers play.",
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const out = join(dir, 'prepared.mp4')
|
|
199
|
+
if (withinRule(probed)) {
|
|
200
|
+
await run(available.ffmpeg, remux(path, out))
|
|
201
|
+
return { bytes: await readFile(out), ext: 'mp4', converted: false }
|
|
202
|
+
}
|
|
203
|
+
onProgress?.('making a video smaller…')
|
|
204
|
+
await run(available.ffmpeg, encode(path, out, ['-crf', String(VIDEO_RULE.crf)]), { maxBuffer: 4 << 20 })
|
|
205
|
+
const [width, height] = encodedSize(Number(probed.video.width), Number(probed.video.height))
|
|
206
|
+
const ceiling = ceilingFor(width, height)
|
|
207
|
+
const made = await probe(available.ffprobe, out)
|
|
208
|
+
if (videoBitrate(made) > ceiling) {
|
|
209
|
+
await run(available.ffmpeg, encode(path, out, ['-b:v', String(ceiling)]), { maxBuffer: 4 << 20 })
|
|
210
|
+
}
|
|
211
|
+
return { bytes: await readFile(out), ext: 'mp4', converted: true }
|
|
212
|
+
} catch {
|
|
213
|
+
return { ...withoutTools(bytes, kind), note: "is uploaded as it is: ffmpeg couldn't convert it, so check that it plays." }
|
|
214
|
+
} finally {
|
|
215
|
+
await rm(dir, { recursive: true, force: true })
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Without `ffmpeg`: blanked in place where its container allows, and a note when it is heavy. */
|
|
220
|
+
function withoutTools(bytes, kind) {
|
|
221
|
+
const blanked = BOXED.has(kind) ? blankVideoMetadata(bytes) : null
|
|
222
|
+
const seconds = blanked?.seconds
|
|
223
|
+
const heavy = seconds > 0 ? (bytes.length * 8) / seconds > VIDEO_RULE.ceilingAt1080p : !blanked
|
|
224
|
+
return {
|
|
225
|
+
bytes: blanked?.bytes ?? bytes,
|
|
226
|
+
ext: kind,
|
|
227
|
+
converted: false,
|
|
228
|
+
...(heavy
|
|
229
|
+
? { note: "is uploaded as it is: a push makes a video smaller only with ffmpeg installed, with libx264 (on a Mac: brew install ffmpeg)." }
|
|
230
|
+
: {}),
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* An MP4 or QuickTime file without its location and camera data, nothing else in it moved. The
|
|
236
|
+
* metadata boxes in `moov` and in each `trak` (`udta`, `meta`) become `free` boxes with their bytes
|
|
237
|
+
* zeroed, so no size or offset changes and every player skips them. A timed metadata track's samples
|
|
238
|
+
* are not reached, which a remux with `ffmpeg` drops.
|
|
239
|
+
*
|
|
240
|
+
* @param {Buffer} bytes
|
|
241
|
+
* @returns {{ bytes: Buffer, seconds?: number } | null} null when the file doesn't parse as boxes.
|
|
242
|
+
* `seconds`: its duration, from `mvhd`.
|
|
243
|
+
*/
|
|
244
|
+
export function blankVideoMetadata(bytes) {
|
|
245
|
+
const out = Buffer.from(bytes)
|
|
246
|
+
let seconds
|
|
247
|
+
const box = (at, end) => {
|
|
248
|
+
if (at + 8 > end) return null
|
|
249
|
+
let size = out.readUInt32BE(at)
|
|
250
|
+
let head = 8
|
|
251
|
+
if (size === 1) {
|
|
252
|
+
if (at + 16 > end) return null
|
|
253
|
+
size = Number(out.readBigUInt64BE(at + 8))
|
|
254
|
+
head = 16
|
|
255
|
+
} else if (size === 0) size = end - at
|
|
256
|
+
if (size < head || at + size > end) return null
|
|
257
|
+
return { type: out.toString('latin1', at + 4, at + 8), start: at + head, end: at + size }
|
|
258
|
+
}
|
|
259
|
+
const walk = (from, end, parent) => {
|
|
260
|
+
for (let at = from; at < end; ) {
|
|
261
|
+
const b = box(at, end)
|
|
262
|
+
if (!b) return false
|
|
263
|
+
if ((parent === 'moov' || parent === 'trak') && (b.type === 'udta' || b.type === 'meta')) {
|
|
264
|
+
out.write('free', at + 4, 'latin1')
|
|
265
|
+
out.fill(0, b.start, b.end)
|
|
266
|
+
} else if (b.type === 'moov' || b.type === 'trak') {
|
|
267
|
+
if (!walk(b.start, b.end, b.type)) return false
|
|
268
|
+
} else if (b.type === 'mvhd' && parent === 'moov') {
|
|
269
|
+
const v1 = out[b.start] === 1
|
|
270
|
+
const scale = out.readUInt32BE(b.start + (v1 ? 20 : 12))
|
|
271
|
+
const length = v1 ? Number(out.readBigUInt64BE(b.start + 24)) : out.readUInt32BE(b.start + 16)
|
|
272
|
+
if (scale > 0) seconds = length / scale
|
|
273
|
+
}
|
|
274
|
+
at = b.end
|
|
275
|
+
}
|
|
276
|
+
return true
|
|
277
|
+
}
|
|
278
|
+
if (out.length < 8 || !walk(0, out.length, null)) return null
|
|
279
|
+
return { bytes: out, ...(seconds ? { seconds } : {}) }
|
|
280
|
+
}
|
|
@@ -19,6 +19,7 @@ import { basename } from 'node:path'
|
|
|
19
19
|
import { resolveAssetPath } from '@uniweb/build/site'
|
|
20
20
|
import { servedFingerprint } from '@uniweb/build/uwx'
|
|
21
21
|
import { prepareImage, isPreparableImage } from '@uniweb/build/images'
|
|
22
|
+
import { prepareVideo, isPreparableVideo } from './prepare-video.js'
|
|
22
23
|
import { contentTypeFor } from '../utils/code-upload.js'
|
|
23
24
|
import { humanBytes } from '../utils/bytes.js'
|
|
24
25
|
|
|
@@ -93,10 +94,10 @@ export async function uploadSiteMedia(
|
|
|
93
94
|
// images) keeps its format, since a home screen takes a PNG and a link preview a JPEG or PNG, and
|
|
94
95
|
// loses only its location and camera data. ⛔ Until 2026-11-12 those went up as they were, so a
|
|
95
96
|
// phone's photo made a social image kept where it was taken, on its page too.
|
|
97
|
+
// A video a page plays is prepared too (`prepareVideo`), under the video half of the rule.
|
|
96
98
|
const ext = extOf(resolved)
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
files.push(prepare ? await preparedFile(file, bytes, ext, recorded?.[ref], warn, options) : file)
|
|
99
|
+
const make = declared ? null : makerFor(ref, resolved, bytes, ext, { keepFormat: keepFormat.has(ref), onProgress })
|
|
100
|
+
files.push(make ? await preparedFile(file, ext, recorded?.[ref], warn, make) : file)
|
|
100
101
|
}
|
|
101
102
|
if (!files.length) return { map: {}, ids: {}, missing, served, failed: [] }
|
|
102
103
|
|
|
@@ -118,7 +119,7 @@ export async function uploadSiteMedia(
|
|
|
118
119
|
localUrl: was.localUrl,
|
|
119
120
|
diskPath: was.diskPath
|
|
120
121
|
}
|
|
121
|
-
again.push(await preparedFile(file,
|
|
122
|
+
again.push(await preparedFile(file, extOf(was.diskPath), null, warn, was.make))
|
|
122
123
|
}
|
|
123
124
|
const retry = await client.uploadSiteAssets({ files: again, siteUuid, onProgress })
|
|
124
125
|
result = {
|
|
@@ -185,8 +186,21 @@ const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex')
|
|
|
185
186
|
const extOf = (path) => (basename(path).match(/\.([^.]+)$/)?.[1] || '').toLowerCase()
|
|
186
187
|
|
|
187
188
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
189
|
+
* How a file is prepared before it is uploaded, or null when it goes up as it is.
|
|
190
|
+
*
|
|
191
|
+
* @returns {null | (() => Promise<{ bytes: Buffer, ext: string, unreadable?: boolean, note?: string }>)}
|
|
192
|
+
*/
|
|
193
|
+
function makerFor(ref, path, bytes, ext, { keepFormat, onProgress }) {
|
|
194
|
+
if (isPreparableImage(ext)) return () => prepareImage(bytes, ext, { keepFormat })
|
|
195
|
+
if (isPreparableVideo(ext)) {
|
|
196
|
+
return () => prepareVideo(path, bytes, ext, { onProgress: (m) => onProgress?.(`${ref}: ${m}`) })
|
|
197
|
+
}
|
|
198
|
+
return null
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* The plan entry for a picture or a video a page shows, stored as `make` prepares it. Three cases, by
|
|
203
|
+
* what this backend's asset map records for the ref:
|
|
190
204
|
*
|
|
191
205
|
* - **Prepared before, from these same bytes** (`source` is their hash): planned by the identity it
|
|
192
206
|
* recorded, with no bytes, so nothing is prepared again. The backend still holding it, the plan
|
|
@@ -197,13 +211,12 @@ const extOf = (path) => (basename(path).match(/\.([^.]+)$/)?.[1] || '').toLowerC
|
|
|
197
211
|
* - **New or changed:** prepared now.
|
|
198
212
|
*
|
|
199
213
|
* @param {object} file - the plan entry for the file as it is
|
|
200
|
-
* @param {Buffer} bytes - the file
|
|
201
214
|
* @param {string} ext - its extension
|
|
202
215
|
* @param {object|undefined|null} prior - its entry in the asset map
|
|
203
216
|
* @param {(m: string) => void} [warn]
|
|
204
|
-
* @param {
|
|
217
|
+
* @param {() => Promise<{ bytes: Buffer, ext: string, unreadable?: boolean, note?: string }>} make
|
|
205
218
|
*/
|
|
206
|
-
async function preparedFile(file,
|
|
219
|
+
async function preparedFile(file, ext, prior, warn, make) {
|
|
207
220
|
const source = file.sha256
|
|
208
221
|
const key = file.path
|
|
209
222
|
if (prior?.source === source && prior.id && prior.ext && Number.isInteger(prior.size)) {
|
|
@@ -215,16 +228,15 @@ async function preparedFile(file, bytes, ext, prior, warn, { keepFormat = false
|
|
|
215
228
|
size: prior.size,
|
|
216
229
|
sha256: prior.id,
|
|
217
230
|
source,
|
|
218
|
-
|
|
219
|
-
load: async () => (await
|
|
231
|
+
make,
|
|
232
|
+
load: async () => (await make()).bytes
|
|
220
233
|
}
|
|
221
234
|
}
|
|
222
235
|
if (prior && !prior.source && prior.id === source) return { ...file, key, source }
|
|
223
236
|
|
|
224
|
-
const prepared = await
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
237
|
+
const prepared = await make()
|
|
238
|
+
const note = prepared.note ?? (prepared.unreadable ? `is uploaded as it is: ${unreadable(ext)}` : null)
|
|
239
|
+
if (note) warn?.(`local-media: ${file.localUrl} ${note}`)
|
|
228
240
|
return {
|
|
229
241
|
...file,
|
|
230
242
|
key,
|
|
@@ -234,10 +246,20 @@ async function preparedFile(file, bytes, ext, prior, warn, { keepFormat = false
|
|
|
234
246
|
sha256: sha256(prepared.bytes),
|
|
235
247
|
bytes: prepared.bytes,
|
|
236
248
|
source,
|
|
237
|
-
|
|
249
|
+
make
|
|
238
250
|
}
|
|
239
251
|
}
|
|
240
252
|
|
|
253
|
+
/**
|
|
254
|
+
* What to tell an author whose picture couldn't be read here, by its format. A BMP is drawn by every
|
|
255
|
+
* browser, so it only stays as large as it is; a HEIC is drawn by few.
|
|
256
|
+
*/
|
|
257
|
+
function unreadable(ext) {
|
|
258
|
+
if (ext === 'heic' || ext === 'heif') return "it can't be converted here, and most browsers won't show it. Export it as JPEG."
|
|
259
|
+
if (ext === 'bmp') return "it can't be made smaller here. Export it as PNG or JPEG for a smaller file."
|
|
260
|
+
return "it can't be read here, so check that it opens."
|
|
261
|
+
}
|
|
262
|
+
|
|
241
263
|
/**
|
|
242
264
|
* The plan's key for a prepared file. The backend types an upload by its extension, so a picture
|
|
243
265
|
* stored as a WebP goes by one ending in `.webp`: its own key with the extension added, which keeps
|
package/src/framework-index.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
|
-
"generatedAt": "2026-11-
|
|
3
|
+
"generatedAt": "2026-11-13T01:28:21.231Z",
|
|
4
4
|
"packages": {
|
|
5
5
|
"@uniweb/api": {
|
|
6
6
|
"version": "1.7.0"
|
|
7
7
|
},
|
|
8
8
|
"@uniweb/build": {
|
|
9
|
-
"version": "1.23.
|
|
9
|
+
"version": "1.23.7"
|
|
10
10
|
},
|
|
11
11
|
"@uniweb/content-reader": {
|
|
12
12
|
"version": "1.5.10"
|
package/src/utils/code-upload.js
CHANGED