@prunellejs/images 0.7.0 → 0.7.1

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/metadata.ts +37 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/images",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Build-time responsive AVIF/WebP image optimization plugin for Prunelle",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -22,6 +22,6 @@
22
22
  "directory": "packages/plugin-images"
23
23
  },
24
24
  "peerDependencies": {
25
- "prunelle": "^0.7.0"
25
+ "prunelle": "^0.7.1"
26
26
  }
27
27
  }
package/src/metadata.ts CHANGED
@@ -64,35 +64,63 @@ function jpegSegmentKind(marker: number, payload: Uint8Array): ImageMetadataKind
64
64
  return undefined
65
65
  }
66
66
 
67
+ function isStandaloneJpegMarker(marker: number): boolean {
68
+ return marker === 0x01 || (marker >= 0xD0 && marker <= 0xD7) || marker === 0xD9
69
+ }
70
+
67
71
  function stripJpeg(bytes: Uint8Array<ArrayBuffer>): StrippedImage {
68
72
  const kept: Uint8Array[] = [bytes.subarray(0, 2)]
69
73
  const removed = new Set<ImageMetadataKind>()
70
74
  let orientation: number | undefined
75
+ let insertAt = 1
71
76
  let at = 2
72
- while (at + 4 <= bytes.length && bytes[at] === 0xFF) {
73
- const marker = bytes[at + 1]!
77
+ while (at < bytes.length) {
78
+ if (bytes[at] !== 0xFF)
79
+ return { bytes, removed: [] }
80
+ const start = at
81
+ while (at < bytes.length && bytes[at] === 0xFF)
82
+ at++
83
+ if (at >= bytes.length)
84
+ return { bytes, removed: [] }
85
+ const marker = bytes[at]!
86
+ if (marker === 0x00)
87
+ return { bytes, removed: [] }
74
88
  if (marker === 0xDA) {
75
- kept.push(bytes.subarray(at))
89
+ kept.push(bytes.subarray(start))
76
90
  at = bytes.length
77
91
  break
78
92
  }
79
- const length = (bytes[at + 2]! << 8) | bytes[at + 3]!
80
- const end = at + 2 + length
81
- const kind = jpegSegmentKind(marker, bytes.subarray(at + 4, end))
93
+ if (isStandaloneJpegMarker(marker)) {
94
+ kept.push(bytes.subarray(start, at + 1))
95
+ at += 1
96
+ if (marker === 0xD9)
97
+ break
98
+ continue
99
+ }
100
+ if (at + 3 > bytes.length)
101
+ return { bytes, removed: [] }
102
+ const length = (bytes[at + 1]! << 8) | bytes[at + 2]!
103
+ const end = at + 1 + length
104
+ if (length < 2 || end > bytes.length)
105
+ return { bytes, removed: [] }
106
+ const payload = bytes.subarray(at + 3, end)
107
+ const kind = jpegSegmentKind(marker, payload)
82
108
  if (kind === undefined) {
83
- kept.push(bytes.subarray(at, end))
109
+ if (kept.length === 1 && marker === 0xE0)
110
+ insertAt = 2
111
+ kept.push(bytes.subarray(start, end))
84
112
  }
85
113
  else {
86
114
  removed.add(kind)
87
115
  if (kind === 'exif')
88
- orientation ??= exifOrientation(bytes.subarray(at + 4, end))
116
+ orientation ??= exifOrientation(payload)
89
117
  }
90
118
  at = end
91
119
  }
92
120
  if (at !== bytes.length || removed.size === 0)
93
121
  return { bytes, removed: [] }
94
122
  if (orientation !== undefined && orientation > 1 && orientation <= 8)
95
- kept.splice(kept[1]?.[1] === 0xE0 ? 2 : 1, 0, orientationSegment(orientation))
123
+ kept.splice(insertAt, 0, orientationSegment(orientation))
96
124
  return { bytes: concat(kept), removed: [...removed] }
97
125
  }
98
126