@uniweb/build 1.14.3 → 1.14.4
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 +6 -6
- package/src/site/asset-processor.js +7 -16
- package/src/site/drawn-size.js +339 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniweb/build",
|
|
3
|
-
"version": "1.14.
|
|
3
|
+
"version": "1.14.4",
|
|
4
4
|
"description": "Build tooling for the Uniweb Component Web Platform",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -56,15 +56,15 @@
|
|
|
56
56
|
"js-yaml": "^4.3.2",
|
|
57
57
|
"sharp": "^0.35.5",
|
|
58
58
|
"yaml": "^2.5.0",
|
|
59
|
-
"@uniweb/content-reader": "~1.5.8",
|
|
60
59
|
"@uniweb/schemas": "~1.6.7",
|
|
60
|
+
"@uniweb/content-reader": "~1.5.8",
|
|
61
61
|
"@uniweb/theming": "~1.2.1",
|
|
62
62
|
"@uniweb/semantic-parser": "~1.13.2",
|
|
63
|
-
"@uniweb/
|
|
64
|
-
"@uniweb/
|
|
63
|
+
"@uniweb/projections": "~1.2.10",
|
|
64
|
+
"@uniweb/content-writer": "~1.3.1"
|
|
65
65
|
},
|
|
66
66
|
"optionalDependencies": {
|
|
67
|
-
"@uniweb/runtime": "~1.3.
|
|
67
|
+
"@uniweb/runtime": "~1.3.14"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
70
|
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"@vitejs/plugin-react": "^4.0.0 || ^5.0.0",
|
|
75
75
|
"vite-plugin-svgr": "^4.0.0",
|
|
76
76
|
"@uniweb/core": "~1.6.1",
|
|
77
|
-
"@uniweb/runtime": "~1.3.
|
|
77
|
+
"@uniweb/runtime": "~1.3.14"
|
|
78
78
|
},
|
|
79
79
|
"peerDependenciesMeta": {
|
|
80
80
|
"vite": {
|
|
@@ -15,6 +15,7 @@ import { join, basename, extname, dirname } from 'node:path'
|
|
|
15
15
|
import { createHash } from 'node:crypto'
|
|
16
16
|
import sharp from 'sharp'
|
|
17
17
|
import { mimeFor } from '@uniweb/semantic-parser'
|
|
18
|
+
import { drawnSize, RAN_OUT } from './drawn-size.js'
|
|
18
19
|
|
|
19
20
|
// Image formats that can be converted to WebP
|
|
20
21
|
const CONVERTIBLE_FORMATS = ['.png', '.jpg', '.jpeg', '.gif']
|
|
@@ -68,22 +69,12 @@ async function convertToWebp(input, options = {}) {
|
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
/**
|
|
71
|
-
* The size
|
|
72
|
-
*
|
|
73
|
-
* sharp can't read every format (`.ico`). Quiet, because a file with no size is still copied.
|
|
74
|
-
*
|
|
75
|
-
* @param {Buffer} bytes
|
|
76
|
-
* @param {string} ext - the extension of the file the site serves
|
|
77
|
-
* @returns {Promise<{ width: number, height: number } | null>}
|
|
72
|
+
* The size of a file the site serves, whole, so running out of it means it has none. Quiet, because a
|
|
73
|
+
* file with no size is still copied.
|
|
78
74
|
*/
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const { autoOrient } = await sharp(bytes).metadata()
|
|
83
|
-
return autoOrient?.width && autoOrient?.height ? { width: autoOrient.width, height: autoOrient.height } : null
|
|
84
|
-
} catch {
|
|
85
|
-
return null
|
|
86
|
-
}
|
|
75
|
+
function sizeOfFile(bytes) {
|
|
76
|
+
const size = drawnSize(bytes)
|
|
77
|
+
return size === RAN_OUT ? null : size
|
|
87
78
|
}
|
|
88
79
|
|
|
89
80
|
/**
|
|
@@ -163,7 +154,7 @@ export async function processAsset(asset, options = {}) {
|
|
|
163
154
|
processed: true,
|
|
164
155
|
converted,
|
|
165
156
|
size: outputBuffer.length,
|
|
166
|
-
...(isImage ?
|
|
157
|
+
...(isImage ? sizeOfFile(outputBuffer) : null)
|
|
167
158
|
}
|
|
168
159
|
} catch (error) {
|
|
169
160
|
console.warn(`[asset-processor] Failed to process ${resolved}:`, error.message)
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The size a browser draws an image at, read from the first bytes of its file.
|
|
3
|
+
*
|
|
4
|
+
* `width` and `height` on an `<img>` hold its place on the page while its file loads, and a size with
|
|
5
|
+
* the wrong proportions moves the page as much as no size does. So this answers with a size only where
|
|
6
|
+
* every browser draws the same one, which isn't every file. Measured on `naturalWidth` and
|
|
7
|
+
* `naturalHeight` in Chromium 156, WebKit 27.2 and Firefox 157, 2026-11-02:
|
|
8
|
+
*
|
|
9
|
+
* JPEG, PNG an EXIF orientation of 5 to 8 swaps the sides, in a PNG only ahead of its data
|
|
10
|
+
* JPEG EXIF's resolution corrects the size where the pixels it states are the file's at that
|
|
11
|
+
* resolution: 16x8 at 144 dpi stating 8x4 draws at 8x4. In a PNG only Firefox does: none
|
|
12
|
+
* JPEG an orientation in any but the first APP1 segment: Firefox doesn't read it: none
|
|
13
|
+
* JPEG, PNG an orientation only on the thumbnail: only WebKit reads it: none
|
|
14
|
+
* WebP an EXIF orientation of 5 to 8: only WebKit turns it: none. Else its canvas
|
|
15
|
+
* GIF its screen, where the first frame fills it. Else they differ: none
|
|
16
|
+
* AVIF its primary item's size, an `irot` swapping the sides. A `clap`: they differ: none
|
|
17
|
+
*
|
|
18
|
+
* It reads headers and never decodes, so it needs only the start of a file, and its answers can't
|
|
19
|
+
* change with an image library's version or the machine it runs on.
|
|
20
|
+
*
|
|
21
|
+
* @module @uniweb/build/site
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** The bytes ended before the answer. A caller holding only the start of a file reads further. */
|
|
25
|
+
export const RAN_OUT = 'ran-out'
|
|
26
|
+
|
|
27
|
+
const OUT = Symbol('out of bytes')
|
|
28
|
+
|
|
29
|
+
const need = (b, end) => {
|
|
30
|
+
if (end > b.length) throw OUT
|
|
31
|
+
}
|
|
32
|
+
const u8 = (b, at) => (need(b, at + 1), b[at])
|
|
33
|
+
const be16 = (b, at) => (need(b, at + 2), b.readUInt16BE(at))
|
|
34
|
+
const be32 = (b, at) => (need(b, at + 4), b.readUInt32BE(at))
|
|
35
|
+
const le16 = (b, at) => (need(b, at + 2), b.readUInt16LE(at))
|
|
36
|
+
const le24 = (b, at) => (need(b, at + 3), b.readUIntLE(at, 3))
|
|
37
|
+
const le32 = (b, at) => (need(b, at + 4), b.readUInt32LE(at))
|
|
38
|
+
const text = (b, at, length) => (need(b, at + length), b.latin1Slice(at, at + length))
|
|
39
|
+
|
|
40
|
+
const sized = (width, height) => (width > 0 && height > 0 ? { width, height } : null)
|
|
41
|
+
|
|
42
|
+
const NO_EXIF = { turns: false, thumbnailTurns: false }
|
|
43
|
+
|
|
44
|
+
/** What `read` answers, or undefined where it runs out of bytes. */
|
|
45
|
+
function attempt(read) {
|
|
46
|
+
try {
|
|
47
|
+
return read()
|
|
48
|
+
} catch (thrown) {
|
|
49
|
+
if (thrown === OUT) return undefined
|
|
50
|
+
throw thrown
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What a block of EXIF says about its picture. The block is held whole by now, so running out of it
|
|
56
|
+
* means that part is malformed, and a browser reads a malformed part as saying nothing.
|
|
57
|
+
*
|
|
58
|
+
* @returns {{ turns: boolean, thumbnailTurns: boolean, density?: Object }} `turns`: its orientation is a
|
|
59
|
+
* quarter turn, which swaps the picture's sides. `thumbnailTurns`: the picture has no orientation of
|
|
60
|
+
* its own and its thumbnail's is one. `density`: the resolution and the pixels it states, if any.
|
|
61
|
+
*/
|
|
62
|
+
function readExif(block) {
|
|
63
|
+
const facts = attempt(() => {
|
|
64
|
+
const order = text(block, 0, 2)
|
|
65
|
+
if (order !== 'II' && order !== 'MM') return null
|
|
66
|
+
const u16 = order === 'II' ? le16 : be16
|
|
67
|
+
const u32 = order === 'II' ? le32 : be32
|
|
68
|
+
if (u16(block, 2) !== 42) return null
|
|
69
|
+
|
|
70
|
+
const entries = (ifd) => {
|
|
71
|
+
const found = new Map()
|
|
72
|
+
const count = u16(block, ifd)
|
|
73
|
+
for (let i = 0, at = ifd + 2; i < count && at + 12 <= block.length; i++, at += 12) found.set(u16(block, at), at)
|
|
74
|
+
return found
|
|
75
|
+
}
|
|
76
|
+
const value = (entry) => {
|
|
77
|
+
if (entry === undefined) return undefined
|
|
78
|
+
const type = u16(block, entry + 2)
|
|
79
|
+
if (type === 3) return u16(block, entry + 8)
|
|
80
|
+
if (type === 4) return u32(block, entry + 8)
|
|
81
|
+
if (type !== 5) return undefined
|
|
82
|
+
const at = u32(block, entry + 8)
|
|
83
|
+
return u32(block, at) / u32(block, at + 4)
|
|
84
|
+
}
|
|
85
|
+
const isTurn = (orientation) => orientation >= 5 && orientation <= 8
|
|
86
|
+
|
|
87
|
+
const first = u32(block, 4)
|
|
88
|
+
const own = entries(first)
|
|
89
|
+
const orientation = value(own.get(0x0112))
|
|
90
|
+
return {
|
|
91
|
+
turns: isTurn(orientation),
|
|
92
|
+
thumbnailTurns:
|
|
93
|
+
orientation === undefined &&
|
|
94
|
+
isTurn(
|
|
95
|
+
attempt(() => {
|
|
96
|
+
const next = u32(block, first + 2 + u16(block, first) * 12)
|
|
97
|
+
return next ? value(entries(next).get(0x0112)) : undefined
|
|
98
|
+
}),
|
|
99
|
+
),
|
|
100
|
+
density: attempt(() => {
|
|
101
|
+
const pixels = entries(value(own.get(0x8769)))
|
|
102
|
+
return {
|
|
103
|
+
x: value(own.get(0x011a)),
|
|
104
|
+
y: value(own.get(0x011b)),
|
|
105
|
+
unit: value(own.get(0x0128)),
|
|
106
|
+
width: value(pixels.get(0xa002)),
|
|
107
|
+
height: value(pixels.get(0xa003)),
|
|
108
|
+
}
|
|
109
|
+
}),
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
return facts ?? NO_EXIF
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const swapped = (size) => sized(size.height, size.width)
|
|
116
|
+
const same = (one, other) => one.width === other.width && one.height === other.height
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The size EXIF's resolution corrects a picture to, which is the HTML standard's rule: in inches, and
|
|
120
|
+
* only where the pixels it states are the file's at that resolution. So a file that was resized and
|
|
121
|
+
* kept a phone's tags isn't corrected, and neither is one at 144 dpi stating its own pixels.
|
|
122
|
+
*/
|
|
123
|
+
function corrected(size, { density }) {
|
|
124
|
+
if (!density || density.unit !== 2) return size
|
|
125
|
+
const { x, y, width, height } = density
|
|
126
|
+
if (!(x > 0 && y > 0 && width > 0 && height > 0)) return size
|
|
127
|
+
return Math.round((size.width * 72) / x) === width && Math.round((size.height * 72) / y) === height
|
|
128
|
+
? { width, height }
|
|
129
|
+
: size
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A JPEG's size, from the headers before its data starts: every browser corrects it, then turns it. */
|
|
133
|
+
function jpeg(b) {
|
|
134
|
+
let size = null
|
|
135
|
+
let exif = null
|
|
136
|
+
let first = true
|
|
137
|
+
let app1 = 0
|
|
138
|
+
let at = 2
|
|
139
|
+
for (;;) {
|
|
140
|
+
if (u8(b, at) !== 0xff) return null
|
|
141
|
+
let marker = u8(b, at + 1)
|
|
142
|
+
while (marker === 0xff) marker = u8(b, ++at + 1)
|
|
143
|
+
at += 2
|
|
144
|
+
if (marker === 0xda || marker === 0xd9) break
|
|
145
|
+
if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) continue
|
|
146
|
+
const length = be16(b, at)
|
|
147
|
+
if (length < 2) return null
|
|
148
|
+
// Each start-of-frame marker, which are C0 to CF but for C4, C8 and CC.
|
|
149
|
+
if (marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker)) {
|
|
150
|
+
size = sized(be16(b, at + 5), be16(b, at + 3))
|
|
151
|
+
} else if (marker === 0xe1) {
|
|
152
|
+
need(b, at + length)
|
|
153
|
+
if (!exif && length >= 8 && text(b, at + 2, 6) === 'Exif\0\0') {
|
|
154
|
+
exif = readExif(b.subarray(at + 8, at + length))
|
|
155
|
+
first = app1 === 0
|
|
156
|
+
}
|
|
157
|
+
app1++
|
|
158
|
+
}
|
|
159
|
+
at += length
|
|
160
|
+
}
|
|
161
|
+
if (!size || !exif) return size
|
|
162
|
+
// Only WebKit reads a thumbnail's orientation.
|
|
163
|
+
if (exif.thumbnailTurns) return null
|
|
164
|
+
const fixed = corrected(size, exif)
|
|
165
|
+
const drawn = exif.turns ? swapped(fixed) : fixed
|
|
166
|
+
// Firefox reads only the first of these segments, and the others read any of them.
|
|
167
|
+
return first || same(drawn, size) ? drawn : null
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** A PNG's size, turned by EXIF ahead of its data and not after it. Only Firefox corrects one. */
|
|
171
|
+
function png(b) {
|
|
172
|
+
if (be32(b, 8) !== 13 || text(b, 12, 4) !== 'IHDR') return null
|
|
173
|
+
const size = sized(be32(b, 16), be32(b, 20))
|
|
174
|
+
if (!size) return null
|
|
175
|
+
let exif = null
|
|
176
|
+
let at = 33
|
|
177
|
+
for (;;) {
|
|
178
|
+
const length = be32(b, at)
|
|
179
|
+
const type = text(b, at + 4, 4)
|
|
180
|
+
if (type === 'IDAT' || type === 'IEND') break
|
|
181
|
+
if (type === 'eXIf' && !exif) {
|
|
182
|
+
need(b, at + 8 + length)
|
|
183
|
+
exif = readExif(b.subarray(at + 8, at + 8 + length))
|
|
184
|
+
}
|
|
185
|
+
at += 12 + length
|
|
186
|
+
}
|
|
187
|
+
if (!exif) return size
|
|
188
|
+
if (exif.thumbnailTurns || !same(corrected(size, exif), size)) return null
|
|
189
|
+
return exif.turns ? swapped(size) : size
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** A GIF's screen, where its first frame fills it. Browsers disagree on one that doesn't. */
|
|
193
|
+
function gif(b) {
|
|
194
|
+
const size = sized(le16(b, 6), le16(b, 8))
|
|
195
|
+
if (!size) return null
|
|
196
|
+
const flags = u8(b, 10)
|
|
197
|
+
let at = 13 + (flags & 0x80 ? 3 * 2 ** ((flags & 7) + 1) : 0)
|
|
198
|
+
for (;;) {
|
|
199
|
+
const block = u8(b, at)
|
|
200
|
+
if (block === 0x2c) {
|
|
201
|
+
const fills =
|
|
202
|
+
le16(b, at + 1) === 0 && le16(b, at + 3) === 0 && le16(b, at + 5) === size.width && le16(b, at + 7) === size.height
|
|
203
|
+
return fills ? size : null
|
|
204
|
+
}
|
|
205
|
+
if (block !== 0x21) return null
|
|
206
|
+
at += 2
|
|
207
|
+
for (let length = u8(b, at); length; length = u8(b, at)) at += length + 1
|
|
208
|
+
at += 1
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** A WebP's canvas. Only WebKit turns one by its EXIF, so one that says to has no size. */
|
|
213
|
+
function webp(b) {
|
|
214
|
+
if (text(b, 8, 4) !== 'WEBP') return null
|
|
215
|
+
const first = text(b, 12, 4)
|
|
216
|
+
if (first === 'VP8 ') {
|
|
217
|
+
if (be32(b, 23) >>> 8 !== 0x9d012a) return null
|
|
218
|
+
return sized(le16(b, 26) & 0x3fff, le16(b, 28) & 0x3fff)
|
|
219
|
+
}
|
|
220
|
+
if (first === 'VP8L') {
|
|
221
|
+
if (u8(b, 20) !== 0x2f) return null
|
|
222
|
+
const bits = le32(b, 21)
|
|
223
|
+
return sized((bits & 0x3fff) + 1, ((bits >>> 14) & 0x3fff) + 1)
|
|
224
|
+
}
|
|
225
|
+
if (first !== 'VP8X') return null
|
|
226
|
+
const size = sized(le24(b, 24) + 1, le24(b, 27) + 1)
|
|
227
|
+
if (!(u8(b, 20) & 0x08)) return size
|
|
228
|
+
const end = 8 + le32(b, 4)
|
|
229
|
+
for (let at = 30; at < end; ) {
|
|
230
|
+
const type = text(b, at, 4)
|
|
231
|
+
const length = le32(b, at + 4)
|
|
232
|
+
if (type === 'EXIF') {
|
|
233
|
+
need(b, at + 8 + length)
|
|
234
|
+
const start = text(b, at + 8, 6) === 'Exif\0\0' ? at + 14 : at + 8
|
|
235
|
+
return readExif(b.subarray(start, at + 8 + length)).turns ? null : size
|
|
236
|
+
}
|
|
237
|
+
at += 8 + length + (length & 1)
|
|
238
|
+
}
|
|
239
|
+
return size
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** The boxes in `[at, end)` of a file in the format an AVIF uses, as `[type, start, end]`. */
|
|
243
|
+
function* boxes(b, at, end) {
|
|
244
|
+
while (at < end) {
|
|
245
|
+
let size = be32(b, at)
|
|
246
|
+
const type = text(b, at + 4, 4)
|
|
247
|
+
let start = at + 8
|
|
248
|
+
if (size === 1) {
|
|
249
|
+
size = be32(b, at + 8) * 2 ** 32 + be32(b, at + 12)
|
|
250
|
+
start = at + 16
|
|
251
|
+
} else if (size === 0) {
|
|
252
|
+
size = end - at
|
|
253
|
+
}
|
|
254
|
+
if (size < start - at) throw OUT
|
|
255
|
+
yield [type, start, at + size]
|
|
256
|
+
at += size
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* An AVIF's size, which is its primary item's, turned by its `irot`. Browsers disagree on a crop.
|
|
262
|
+
*/
|
|
263
|
+
function avif(b) {
|
|
264
|
+
const ftyp = be32(b, 0)
|
|
265
|
+
need(b, ftyp)
|
|
266
|
+
const brands = [text(b, 8, 4)]
|
|
267
|
+
for (let at = 16; at + 4 <= ftyp; at += 4) brands.push(text(b, at, 4))
|
|
268
|
+
if (!brands.includes('avif')) return null
|
|
269
|
+
|
|
270
|
+
for (const [type, start, end] of boxes(b, ftyp, Infinity)) {
|
|
271
|
+
if (type !== 'meta') continue
|
|
272
|
+
need(b, end)
|
|
273
|
+
let primary = null
|
|
274
|
+
let properties = null
|
|
275
|
+
let links = null
|
|
276
|
+
for (const [inner, from, to] of boxes(b, start + 4, end)) {
|
|
277
|
+
if (inner === 'pitm') primary = u8(b, from) === 0 ? be16(b, from + 4) : be32(b, from + 4)
|
|
278
|
+
if (inner !== 'iprp') continue
|
|
279
|
+
for (const [box, boxFrom, boxTo] of boxes(b, from, to)) {
|
|
280
|
+
if (box === 'ipco') properties = [...boxes(b, boxFrom, boxTo)]
|
|
281
|
+
if (box === 'ipma') links = [boxFrom, boxTo]
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (primary === null || !properties || !links) return null
|
|
285
|
+
|
|
286
|
+
const [ipma] = links
|
|
287
|
+
const version = u8(b, ipma)
|
|
288
|
+
const wide = u8(b, ipma + 3) & 1
|
|
289
|
+
let at = ipma + 8
|
|
290
|
+
for (let i = be32(b, ipma + 4); i > 0; i--) {
|
|
291
|
+
const item = version < 1 ? be16(b, at) : be32(b, at)
|
|
292
|
+
at += version < 1 ? 2 : 4
|
|
293
|
+
const count = u8(b, at++)
|
|
294
|
+
const own = []
|
|
295
|
+
for (let n = 0; n < count; n++) {
|
|
296
|
+
own.push(wide ? be16(b, at) & 0x7fff : u8(b, at) & 0x7f)
|
|
297
|
+
at += wide ? 2 : 1
|
|
298
|
+
}
|
|
299
|
+
if (item !== primary) continue
|
|
300
|
+
const held = own.map((index) => properties[index - 1]).filter(Boolean)
|
|
301
|
+
if (held.some(([box]) => box === 'clap')) return null
|
|
302
|
+
const spatial = held.find(([box]) => box === 'ispe')
|
|
303
|
+
if (!spatial) return null
|
|
304
|
+
const size = sized(be32(b, spatial[1] + 4), be32(b, spatial[1] + 8))
|
|
305
|
+
const rotation = held.find(([box]) => box === 'irot')
|
|
306
|
+
return size && rotation && u8(b, rotation[1]) & 1 ? swapped(size) : size
|
|
307
|
+
}
|
|
308
|
+
return null
|
|
309
|
+
}
|
|
310
|
+
return null
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const READERS = [
|
|
314
|
+
[Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), png],
|
|
315
|
+
[Buffer.from([0xff, 0xd8]), jpeg],
|
|
316
|
+
[Buffer.from('GIF87a', 'latin1'), gif],
|
|
317
|
+
[Buffer.from('GIF89a', 'latin1'), gif],
|
|
318
|
+
[Buffer.from('RIFF', 'latin1'), webp],
|
|
319
|
+
]
|
|
320
|
+
|
|
321
|
+
function read(b) {
|
|
322
|
+
for (const [signature, reader] of READERS) {
|
|
323
|
+
const head = b.subarray(0, signature.length)
|
|
324
|
+
if (head.equals(signature)) return reader(b)
|
|
325
|
+
if (head.length < signature.length && head.equals(signature.subarray(0, head.length))) throw OUT
|
|
326
|
+
}
|
|
327
|
+
return text(b, 4, 4) === 'ftyp' ? avif(b) : null
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* @param {Uint8Array} bytes - a file, or the start of one
|
|
332
|
+
* @returns {{ width: number, height: number } | null | 'ran-out'} the size every browser draws it at;
|
|
333
|
+
* null when it has none to give, however much more of it there is (a vector, or a file browsers
|
|
334
|
+
* draw differently); or `RAN_OUT`
|
|
335
|
+
*/
|
|
336
|
+
export function drawnSize(bytes) {
|
|
337
|
+
const answer = attempt(() => read(Buffer.from(bytes.buffer, bytes.byteOffset, bytes.length)))
|
|
338
|
+
return answer === undefined ? RAN_OUT : answer
|
|
339
|
+
}
|