uniweb 1.34.6 → 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 +7 -7
- package/partials/guides/content.md +1 -1
- package/src/backend/prepare-video.js +280 -0
- package/src/backend/site-media.js +143 -9
- package/src/commands/publish.js +5 -1
- package/src/commands/push.js +7 -1
- package/src/framework-index.json +5 -5
- package/src/utils/asset-upload.js +15 -1
- package/src/utils/code-upload.js +4 -0
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": {
|
|
@@ -41,15 +41,15 @@
|
|
|
41
41
|
"js-yaml": "^4.3.2",
|
|
42
42
|
"prompts": "^2.4.2",
|
|
43
43
|
"tar": "^7.5.21",
|
|
44
|
-
"@uniweb/schemas": "~1.7.4",
|
|
45
|
-
"@uniweb/core": "~1.6.12",
|
|
46
44
|
"@uniweb/content-writer": "~1.3.2",
|
|
47
|
-
"@uniweb/semantic-parser": "~1.13.
|
|
45
|
+
"@uniweb/semantic-parser": "~1.13.5",
|
|
46
|
+
"@uniweb/core": "~1.6.13",
|
|
47
|
+
"@uniweb/schemas": "~1.7.4"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@uniweb/build": "~1.23.
|
|
51
|
-
"@uniweb/
|
|
52
|
-
"@uniweb/
|
|
50
|
+
"@uniweb/build": "~1.23.7",
|
|
51
|
+
"@uniweb/content-reader": "~1.5.10",
|
|
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 and adds its own copy of each file a page names there
|
|
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
|
+
}
|
|
@@ -18,6 +18,8 @@ import { existsSync, readFileSync } from 'node:fs'
|
|
|
18
18
|
import { basename } from 'node:path'
|
|
19
19
|
import { resolveAssetPath } from '@uniweb/build/site'
|
|
20
20
|
import { servedFingerprint } from '@uniweb/build/uwx'
|
|
21
|
+
import { prepareImage, isPreparableImage } from '@uniweb/build/images'
|
|
22
|
+
import { prepareVideo, isPreparableVideo } from './prepare-video.js'
|
|
21
23
|
import { contentTypeFor } from '../utils/code-upload.js'
|
|
22
24
|
import { humanBytes } from '../utils/bytes.js'
|
|
23
25
|
|
|
@@ -27,11 +29,14 @@ import { humanBytes } from '../utils/bytes.js'
|
|
|
27
29
|
* @param {string} siteDir - the site root (site-root refs resolve under public/)
|
|
28
30
|
* @param {Array<string|{ ref: string, path: string, contentType?: string }>} refs - site-root local
|
|
29
31
|
* asset refs (`/images/x.png`), or files already located (a file record's)
|
|
30
|
-
* @param {{ siteUuid?: string|null, onProgress?: (m: string) => void, warn?: (m: string) => void }} [opts]
|
|
32
|
+
* @param {{ siteUuid?: string|null, onProgress?: (m: string) => void, warn?: (m: string) => void, recorded?: Record<string, object> }} [opts]
|
|
31
33
|
* `siteUuid` is the owner the uploaded bytes are charged to. Callers create the
|
|
32
34
|
* site before uploading precisely so this is set — an unowned upload is charged
|
|
33
35
|
* and cannot be freed, because freeing means deleting the owning entity.
|
|
34
|
-
*
|
|
36
|
+
* `recorded` is this backend's asset map from `sync.json`, which says what each ref was stored as.
|
|
37
|
+
* `keepFormat` names the pictures other software fetches by their address, which keep their format
|
|
38
|
+
* and lose only their location and camera data (`prepareImage`'s `keepFormat`).
|
|
39
|
+
* @returns {Promise<{ map: Record<string,string>, ids: Record<string,{id:string,ext:string,source?:string,size?:number}>, missing: string[], served: string[], failed: Array<{path:string,status:number,detail?:string}> }>}
|
|
35
40
|
* `map` is ref → serve URL for refs that resolved AND uploaded. The two failure
|
|
36
41
|
* kinds are reported SEPARATELY because callers must treat them differently:
|
|
37
42
|
* `missing` is a ref with no file under the site — an authoring mistake, already
|
|
@@ -47,7 +52,7 @@ export async function uploadSiteMedia(
|
|
|
47
52
|
client,
|
|
48
53
|
siteDir,
|
|
49
54
|
refs,
|
|
50
|
-
{ siteUuid = null, onProgress, warn } = {}
|
|
55
|
+
{ siteUuid = null, onProgress, warn, recorded = {}, keepFormat = new Set() } = {}
|
|
51
56
|
) {
|
|
52
57
|
if (!refs?.length) return { map: {}, ids: {}, missing: [], served: [], failed: [] }
|
|
53
58
|
|
|
@@ -74,18 +79,56 @@ export async function uploadSiteMedia(
|
|
|
74
79
|
continue
|
|
75
80
|
}
|
|
76
81
|
const bytes = readFileSync(resolved)
|
|
77
|
-
|
|
82
|
+
const declared = typeof item === 'object' ? item.contentType : null
|
|
83
|
+
const file = {
|
|
78
84
|
path: ref.replace(/^\/+/, ''), // bookkeeping key into the plan (must be unique)
|
|
79
|
-
content_type:
|
|
85
|
+
content_type: declared || contentTypeFor(basename(resolved)),
|
|
80
86
|
size: bytes.length,
|
|
81
|
-
sha256:
|
|
87
|
+
sha256: sha256(bytes),
|
|
82
88
|
localUrl: ref, // the rewrite key — the original content ref
|
|
83
89
|
diskPath: resolved
|
|
84
|
-
}
|
|
90
|
+
}
|
|
91
|
+
// ⭐ A picture is prepared before it is stored (`prepareImage`). Not a file record's file, which
|
|
92
|
+
// declares its own type: that is a download, kept as its author gave it. A picture other software
|
|
93
|
+
// fetches by its address (`keepFormat`: the favicon, the touch icon, the site's card and its social
|
|
94
|
+
// images) keeps its format, since a home screen takes a PNG and a link preview a JPEG or PNG, and
|
|
95
|
+
// loses only its location and camera data. ⛔ Until 2026-11-12 those went up as they were, so a
|
|
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.
|
|
98
|
+
const ext = extOf(resolved)
|
|
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)
|
|
85
101
|
}
|
|
86
102
|
if (!files.length) return { map: {}, ids: {}, missing, served, failed: [] }
|
|
87
103
|
|
|
88
|
-
|
|
104
|
+
let result = await client.uploadSiteAssets({ files, siteUuid, onProgress })
|
|
105
|
+
// A recorded preparation the backend no longer holds, whose file now prepares to other bytes
|
|
106
|
+
// (the rule changed since): prepared anew, and planned again.
|
|
107
|
+
const stale = (result.failed || []).filter((f) => f.stale)
|
|
108
|
+
if (stale.length) {
|
|
109
|
+
const again = []
|
|
110
|
+
for (const f of stale) {
|
|
111
|
+
const was = files.find((x) => x.path === f.path)
|
|
112
|
+
if (!was) continue
|
|
113
|
+
const bytes = readFileSync(was.diskPath)
|
|
114
|
+
const file = {
|
|
115
|
+
path: was.key,
|
|
116
|
+
content_type: contentTypeFor(basename(was.diskPath)),
|
|
117
|
+
size: bytes.length,
|
|
118
|
+
sha256: sha256(bytes),
|
|
119
|
+
localUrl: was.localUrl,
|
|
120
|
+
diskPath: was.diskPath
|
|
121
|
+
}
|
|
122
|
+
again.push(await preparedFile(file, extOf(was.diskPath), null, warn, was.make))
|
|
123
|
+
}
|
|
124
|
+
const retry = await client.uploadSiteAssets({ files: again, siteUuid, onProgress })
|
|
125
|
+
result = {
|
|
126
|
+
...result,
|
|
127
|
+
failed: [...result.failed.filter((f) => !f.stale), ...(retry.failed || [])],
|
|
128
|
+
assetsByLocalUrl: { ...result.assetsByLocalUrl, ...retry.assetsByLocalUrl }
|
|
129
|
+
}
|
|
130
|
+
for (const f of again) files[files.findIndex((x) => x.localUrl === f.localUrl)] = f
|
|
131
|
+
}
|
|
89
132
|
const failed = [...(result.failed || [])]
|
|
90
133
|
for (const f of failed)
|
|
91
134
|
warn?.(`local-media: upload failed for ${f.path} (HTTP ${f.status})`)
|
|
@@ -104,10 +147,14 @@ export async function uploadSiteMedia(
|
|
|
104
147
|
// id beside it (`info.preview`, `seo.image`, a section param); the header of
|
|
105
148
|
// `@uniweb/build`'s asset-map.js says why a hash and not the URL.
|
|
106
149
|
if (entry.id) {
|
|
150
|
+
// A prepared picture also records the file it was made from and its stored size, so the next
|
|
151
|
+
// push reuses it rather than preparing the file again (`preparedFile`).
|
|
152
|
+
const sent = files.find((f) => f.localUrl === ref)
|
|
107
153
|
ids[ref] = {
|
|
108
154
|
id: entry.id,
|
|
109
155
|
ext: entry.ext || '',
|
|
110
|
-
...(entry.serveUrl ? { served: servedFingerprint(entry.serveUrl) } : {})
|
|
156
|
+
...(entry.serveUrl ? { served: servedFingerprint(entry.serveUrl) } : {}),
|
|
157
|
+
...(sent?.source ? { source: sent.source, size: sent.size } : {})
|
|
111
158
|
}
|
|
112
159
|
}
|
|
113
160
|
// The backend's canonical serve URL, READ — never composed. An entry without
|
|
@@ -135,6 +182,93 @@ export async function uploadSiteMedia(
|
|
|
135
182
|
return { map, ids, missing, served, failed }
|
|
136
183
|
}
|
|
137
184
|
|
|
185
|
+
const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex')
|
|
186
|
+
const extOf = (path) => (basename(path).match(/\.([^.]+)$/)?.[1] || '').toLowerCase()
|
|
187
|
+
|
|
188
|
+
/**
|
|
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:
|
|
204
|
+
*
|
|
205
|
+
* - **Prepared before, from these same bytes** (`source` is their hash): planned by the identity it
|
|
206
|
+
* recorded, with no bytes, so nothing is prepared again. The backend still holding it, the plan
|
|
207
|
+
* says so and nothing moves. If it doesn't, `load` prepares the file again. So a change to the
|
|
208
|
+
* rule never prepares, uploads or charges an unchanged picture again, and its pages don't change.
|
|
209
|
+
* - **Stored as it is, before pictures were prepared** (no `source`, and the id is these bytes'
|
|
210
|
+
* hash, the asset plan's content address): left as it is, and its source recorded.
|
|
211
|
+
* - **New or changed:** prepared now.
|
|
212
|
+
*
|
|
213
|
+
* @param {object} file - the plan entry for the file as it is
|
|
214
|
+
* @param {string} ext - its extension
|
|
215
|
+
* @param {object|undefined|null} prior - its entry in the asset map
|
|
216
|
+
* @param {(m: string) => void} [warn]
|
|
217
|
+
* @param {() => Promise<{ bytes: Buffer, ext: string, unreadable?: boolean, note?: string }>} make
|
|
218
|
+
*/
|
|
219
|
+
async function preparedFile(file, ext, prior, warn, make) {
|
|
220
|
+
const source = file.sha256
|
|
221
|
+
const key = file.path
|
|
222
|
+
if (prior?.source === source && prior.id && prior.ext && Number.isInteger(prior.size)) {
|
|
223
|
+
return {
|
|
224
|
+
...file,
|
|
225
|
+
key,
|
|
226
|
+
path: storedPath(key, ext, prior.ext),
|
|
227
|
+
content_type: contentTypeFor(`x.${prior.ext}`),
|
|
228
|
+
size: prior.size,
|
|
229
|
+
sha256: prior.id,
|
|
230
|
+
source,
|
|
231
|
+
make,
|
|
232
|
+
load: async () => (await make()).bytes
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (prior && !prior.source && prior.id === source) return { ...file, key, source }
|
|
236
|
+
|
|
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}`)
|
|
240
|
+
return {
|
|
241
|
+
...file,
|
|
242
|
+
key,
|
|
243
|
+
path: storedPath(key, ext, prepared.ext),
|
|
244
|
+
content_type: contentTypeFor(`x.${prepared.ext}`),
|
|
245
|
+
size: prepared.bytes.length,
|
|
246
|
+
sha256: sha256(prepared.bytes),
|
|
247
|
+
bytes: prepared.bytes,
|
|
248
|
+
source,
|
|
249
|
+
make
|
|
250
|
+
}
|
|
251
|
+
}
|
|
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
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The plan's key for a prepared file. The backend types an upload by its extension, so a picture
|
|
265
|
+
* stored as a WebP goes by one ending in `.webp`: its own key with the extension added, which keeps
|
|
266
|
+
* it apart from every other file's.
|
|
267
|
+
*/
|
|
268
|
+
function storedPath(key, ext, storedExt) {
|
|
269
|
+
return storedExt === ext ? key : `${key}.${storedExt}`
|
|
270
|
+
}
|
|
271
|
+
|
|
138
272
|
// ─── Asset-plan refusals ──────────────────────────────────────────────────────
|
|
139
273
|
//
|
|
140
274
|
// There is deliberately NO status-based refusal predicate here, and re-adding
|
package/src/commands/publish.js
CHANGED
|
@@ -791,7 +791,11 @@ export async function publish(args = []) {
|
|
|
791
791
|
{
|
|
792
792
|
siteUuid: site.uuid,
|
|
793
793
|
onProgress: (m) => say.dim(` ${m}`),
|
|
794
|
-
warn: (m) => say.dim(`! ${m}`)
|
|
794
|
+
warn: (m) => say.dim(`! ${m}`),
|
|
795
|
+
// What each picture was stored as, so an unchanged one is not prepared again.
|
|
796
|
+
recorded: readBackendState(siteDir, client.origin).assets || {},
|
|
797
|
+
// The pictures other software fetches (favicon, touch icon, card, social images): in their format.
|
|
798
|
+
keepFormat: new Set(probe.outsideAssets || [])
|
|
795
799
|
}
|
|
796
800
|
)
|
|
797
801
|
// A ref whose bytes did not land must NOT be published: the content would go
|
package/src/commands/push.js
CHANGED
|
@@ -516,6 +516,8 @@ export async function push(args = [], deps = {}) {
|
|
|
516
516
|
let created = null
|
|
517
517
|
if (!output && !dryRun) {
|
|
518
518
|
let mediaRefs = []
|
|
519
|
+
// The pictures other software fetches (favicon, touch icon, card, social images): uploaded as they are.
|
|
520
|
+
let outsideRefs = new Set()
|
|
519
521
|
let refusals = []
|
|
520
522
|
let held = null
|
|
521
523
|
try {
|
|
@@ -531,6 +533,7 @@ export async function push(args = [], deps = {}) {
|
|
|
531
533
|
})
|
|
532
534
|
// Site-root media, and each file record's file (`@uniweb/file` — `{ ref, path, contentType }`).
|
|
533
535
|
mediaRefs = [...(probe.localAssets || []), ...(probe.localFiles || [])]
|
|
536
|
+
outsideRefs = new Set(probe.outsideAssets || [])
|
|
534
537
|
refusals = probe.refusals || []
|
|
535
538
|
held = probe
|
|
536
539
|
} catch (err) {
|
|
@@ -573,7 +576,10 @@ export async function push(args = [], deps = {}) {
|
|
|
573
576
|
{
|
|
574
577
|
siteUuid: site.uuid,
|
|
575
578
|
onProgress: (m) => note(` ${m}`),
|
|
576
|
-
warn: (m) => note(`! ${m}`)
|
|
579
|
+
warn: (m) => note(`! ${m}`),
|
|
580
|
+
// What each picture was stored as, so an unchanged one is not prepared again.
|
|
581
|
+
recorded: readBackendState(siteDir, client.origin).assets || {},
|
|
582
|
+
keepFormat: outsideRefs
|
|
577
583
|
}
|
|
578
584
|
)
|
|
579
585
|
// Bytes that did not land must not be pushed around: the content would go
|
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"
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"version": "1.3.2"
|
|
16
16
|
},
|
|
17
17
|
"@uniweb/core": {
|
|
18
|
-
"version": "1.6.
|
|
18
|
+
"version": "1.6.13"
|
|
19
19
|
},
|
|
20
20
|
"@uniweb/frame-bridge": {
|
|
21
21
|
"version": "1.0.4"
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"version": "1.2.17"
|
|
37
37
|
},
|
|
38
38
|
"@uniweb/runtime": {
|
|
39
|
-
"version": "1.6.
|
|
39
|
+
"version": "1.6.5"
|
|
40
40
|
},
|
|
41
41
|
"@uniweb/scene": {
|
|
42
42
|
"version": "1.0.1"
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"version": "1.1.3"
|
|
49
49
|
},
|
|
50
50
|
"@uniweb/semantic-parser": {
|
|
51
|
-
"version": "1.13.
|
|
51
|
+
"version": "1.13.5"
|
|
52
52
|
},
|
|
53
53
|
"@uniweb/snapshot": {
|
|
54
54
|
"version": "1.1.2"
|
|
@@ -206,6 +206,20 @@ export async function uploadSiteAssets({
|
|
|
206
206
|
continue
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
+
// The bytes to send: held, read from the file, or made again. A file a push prepared before
|
|
210
|
+
// is planned by the identity it recorded, with no bytes; when the backend no longer holds them,
|
|
211
|
+
// `load` prepares the file again, which must give the recorded bytes. Bytes that differ (the
|
|
212
|
+
// rule changed since) are `stale`, for the caller to plan as a new preparation.
|
|
213
|
+
let body = src.bytes
|
|
214
|
+
if (!body && typeof src.load === 'function') {
|
|
215
|
+
body = await src.load()
|
|
216
|
+
if (createHash('sha256').update(body).digest('hex') !== src.sha256) {
|
|
217
|
+
failed.push({ path: src.path, status: 0, detail: 'prepares to other bytes than recorded', stale: true })
|
|
218
|
+
continue
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (!body) body = readFileSync(src.diskPath)
|
|
222
|
+
|
|
209
223
|
const headers = { ...(up.headers || {}), 'x-uniweb-sha256': src.sha256 }
|
|
210
224
|
// Direct-mode PUTs are authed requests to the backend; presigned URLs are
|
|
211
225
|
// self-signed, so a foreign auth header can break the SigV4 target. This is
|
|
@@ -228,7 +242,7 @@ export async function uploadSiteAssets({
|
|
|
228
242
|
putRes = await fetchWithRetry(new URL(up.url, origin), {
|
|
229
243
|
method: up.method || 'PUT',
|
|
230
244
|
headers,
|
|
231
|
-
body
|
|
245
|
+
body
|
|
232
246
|
}, { timeoutMs: 120_000, retryOnStatus: isTransientStatus })
|
|
233
247
|
} catch (err) {
|
|
234
248
|
failed.push({ path: src.path, status: 0, detail: err.message })
|
package/src/utils/code-upload.js
CHANGED