-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
228 lines (182 loc) · 5.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const fs = require('fs')
const { performance } = require('perf_hooks')
const base64Decode = require('fast-base64-decode')
const base64Length = require('fast-base64-length')
const ImageData = require('@canvas/image-data')
const simpleGet = require('simple-get')
const bmp = require('@cwasm/nsbmp')
const gif = require('@cwasm/nsgif')
const jpeg = require('@cwasm/jpeg-turbo')
const png = require('@cwasm/lodepng')
const webp = require('@cwasm/webp')
const decodeIco = require('decode-ico')
const heightMap = new WeakMap()
const imageDataMap = new WeakMap()
const srcMap = new WeakMap()
const widthMap = new WeakMap()
const headers = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36' }
function decodeImage (data) {
if (data[0] === 0xFF && data[1] === 0xD8 && data[2] === 0xFF) {
return jpeg.decode(data)
}
if (data[0] === 0x89 && data[1] === 0x50 && data[2] === 0x4E && data[3] === 0x47 && data[4] === 0x0D && data[5] === 0x0A && data[6] === 0x1A && data[7] === 0x0A) {
return png.decode(data)
}
if (data[0] === 0x47 && data[1] === 0x49 && data[2] === 0x46) {
return gif.decode(data)
}
if (data[8] === 0x57 && data[9] === 0x45 && data[10] === 0x42 && data[11] === 0x50) {
return webp.decode(data)
}
if (data[0] === 0x42 && data[1] === 0x4D) {
return bmp.decode(data)
}
if (data[0] === 0x00 && data[1] === 0x00 && data[2] === 0x01 && data[3] === 0x00) {
const images = decodeIco(data)
let biggestEntry = images[0]
// ref: https://github.com/mozilla/gecko-dev/blob/10a46f9dacc39a9305ef9cbfb27f8b68e25eccc9/image/decoders/nsICODecoder.cpp#L249-L258
for (const image of images.slice(1)) {
if (
image.bpp >= biggestEntry.bpp &&
image.width * image.height >= biggestEntry.width * biggestEntry.height
) {
biggestEntry = image
}
}
if (biggestEntry.type === 'png') {
return png.decode(biggestEntry.data)
} else {
return new ImageData(biggestEntry.data, biggestEntry.width, biggestEntry.height)
}
}
throw new Error('Unknown file format')
}
function fireError (img, err) {
img.complete = true
process.nextTick(() => {
if (!img.onerror) throw err
img.onerror({ type: 'error', bubbles: false, cancelable: false, currentTarget: img, defaultPrevented: false, eventPhase: 2, target: img, timeStamp: performance.now() })
})
}
function fireLoad (img, data) {
imageDataMap.set(img, data)
img.complete = true
process.nextTick(() => {
if (!img.onload) return
img.onload({ type: 'load', bubbles: false, cancelable: false, currentTarget: img, defaultPrevented: false, eventPhase: 2, target: img, timeStamp: performance.now() })
})
}
const reDataURL = /^data:([^;,]*)(;base64)?,/
function decodeDataURL (url) {
const m = reDataURL.exec(url)
if (m && m[2] === ';base64') {
const source = url.slice(m[0].length)
const length = base64Length(source)
const data = new Uint8Array(length)
base64Decode(source, data)
return decodeImage(data)
}
throw new Error('Invalid data url')
}
async function readDataURL (url, img) {
await Promise.resolve()
let data
try {
data = decodeDataURL(url)
} catch (err) {
return fireError(img, err)
}
fireLoad(img, data)
}
async function readWeb (url, img) {
let data
try {
const [res, encoded] = await new Promise((resolve, reject) => simpleGet.concat({ headers, url }, (err, res, data) => err ? reject(err) : resolve([res, data])))
if (res.statusCode < 200 || res.statusCode >= 300) throw new Error(`Server responded with ${res.statusCode}`)
data = decodeImage(encoded)
} catch (err) {
return fireError(img, err)
}
fireLoad(img, data)
}
async function readFile (url, img) {
const queryIndex = url.indexOf('?')
if (queryIndex !== -1) url = url.slice(0, queryIndex)
if (url.startsWith('file://')) url = url.slice(7)
let data
try {
data = decodeImage(await new Promise((resolve, reject) => fs.readFile(url, (err, raw) => err ? reject(err) : resolve(raw))))
} catch (err) {
return fireError(img, err)
}
fireLoad(img, data)
}
class Image {
constructor (width, height) {
this.complete = true
this.onerror = null
this.onload = null
srcMap.set(this, '')
if (width != null) this.width = width
if (height != null) this.height = height
}
get src () {
return srcMap.get(this)
}
set src (url) {
url = String(url)
this.complete = false
imageDataMap.delete(this)
srcMap.set(this, url)
if (url.startsWith('data:')) {
readDataURL(url, this)
} else if (url.startsWith('http:') || url.startsWith('https:')) {
readWeb(url, this)
} else {
readFile(url, this)
}
}
get height () {
return heightMap.has(this) ? heightMap.get(this) : this.naturalHeight
}
set height (value) {
value = Number(value)
if (value < 0) value = 0
if (value > 0x7fffffff) value = 0
heightMap.set(this, value >>> 0)
}
get naturalHeight () {
return imageDataMap.has(this) ? imageDataMap.get(this).height : 0
}
get naturalWidth () {
return imageDataMap.has(this) ? imageDataMap.get(this).width : 0
}
get width () {
return widthMap.has(this) ? widthMap.get(this) : this.naturalWidth
}
set width (value) {
value = Number(value)
if (value < 0) value = 0
if (value > 0x7fffffff) value = 0
widthMap.set(this, value >>> 0)
}
}
function getImageData (image) {
return imageDataMap.get(image)
}
function imageFromBuffer (buffer) {
return new Promise((resolve) => {
const imageData = decodeImage(buffer)
resolve(imageFromImageData(imageData))
})
}
function imageFromImageData (imageData) {
const img = new Image()
fireLoad(img, imageData)
return Promise.resolve(img)
}
exports.getImageData = getImageData
exports.imageFromBuffer = imageFromBuffer
exports.imageFromImageData = imageFromImageData
exports.Image = Image
exports.default = Image