@nexterm/canvas 0.0.0-stage → 1.2.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.
- package/LICENSE +55 -0
- package/geometry.js +1006 -0
- package/index.d.ts +1467 -0
- package/index.js +761 -0
- package/js-binding.js +466 -0
- package/load-image.js +166 -0
- package/node-canvas.d.ts +249 -0
- package/node-canvas.js +448 -0
- package/package.json +56 -4
- package/README.md +0 -3
package/js-binding.js
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
// prettier-ignore
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* auto-generated by NAPI-RS */
|
|
5
|
+
|
|
6
|
+
const { readFileSync } = require('fs')
|
|
7
|
+
let nativeBinding = null
|
|
8
|
+
const loadErrors = []
|
|
9
|
+
|
|
10
|
+
const isMusl = () => {
|
|
11
|
+
let musl = false
|
|
12
|
+
if (process.platform === 'linux') {
|
|
13
|
+
musl = isMuslFromFilesystem()
|
|
14
|
+
if (musl === null) {
|
|
15
|
+
musl = isMuslFromReport()
|
|
16
|
+
}
|
|
17
|
+
if (musl === null) {
|
|
18
|
+
musl = isMuslFromChildProcess()
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return musl
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-')
|
|
25
|
+
|
|
26
|
+
const isMuslFromFilesystem = () => {
|
|
27
|
+
try {
|
|
28
|
+
return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl')
|
|
29
|
+
} catch {
|
|
30
|
+
return null
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const isMuslFromReport = () => {
|
|
35
|
+
let report = null
|
|
36
|
+
if (typeof process.report?.getReport === 'function') {
|
|
37
|
+
process.report.excludeNetwork = true
|
|
38
|
+
report = process.report.getReport()
|
|
39
|
+
}
|
|
40
|
+
if (!report) {
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
if (report.header && report.header.glibcVersionRuntime) {
|
|
44
|
+
return false
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(report.sharedObjects)) {
|
|
47
|
+
if (report.sharedObjects.some(isFileMusl)) {
|
|
48
|
+
return true
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const isMuslFromChildProcess = () => {
|
|
55
|
+
try {
|
|
56
|
+
return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl')
|
|
57
|
+
} catch (e) {
|
|
58
|
+
// If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function requireNative() {
|
|
64
|
+
if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
|
|
65
|
+
try {
|
|
66
|
+
return require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH)
|
|
67
|
+
} catch (err) {
|
|
68
|
+
loadErrors.push(err)
|
|
69
|
+
}
|
|
70
|
+
} else if (process.platform === 'android') {
|
|
71
|
+
if (process.arch === 'arm64') {
|
|
72
|
+
try {
|
|
73
|
+
return require('./skia.android-arm64.node')
|
|
74
|
+
} catch (e) {
|
|
75
|
+
loadErrors.push(e)
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
return require('@napi-rs/canvas-android-arm64')
|
|
79
|
+
} catch (e) {
|
|
80
|
+
loadErrors.push(e)
|
|
81
|
+
}
|
|
82
|
+
} else if (process.arch === 'arm') {
|
|
83
|
+
try {
|
|
84
|
+
return require('./skia.android-arm-eabi.node')
|
|
85
|
+
} catch (e) {
|
|
86
|
+
loadErrors.push(e)
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
return require('@napi-rs/canvas-android-arm-eabi')
|
|
90
|
+
} catch (e) {
|
|
91
|
+
loadErrors.push(e)
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`))
|
|
95
|
+
}
|
|
96
|
+
} else if (process.platform === 'win32') {
|
|
97
|
+
if (process.arch === 'x64') {
|
|
98
|
+
if (
|
|
99
|
+
process.config?.variables?.shlib_suffix === 'dll.a' ||
|
|
100
|
+
process.config?.variables?.node_target_type === 'shared_library'
|
|
101
|
+
) {
|
|
102
|
+
try {
|
|
103
|
+
return require('./skia.win32-x64-gnu.node')
|
|
104
|
+
} catch (e) {
|
|
105
|
+
loadErrors.push(e)
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
return require('@napi-rs/canvas-win32-x64-gnu')
|
|
109
|
+
} catch (e) {
|
|
110
|
+
loadErrors.push(e)
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
try {
|
|
114
|
+
return require('./skia.win32-x64-msvc.node')
|
|
115
|
+
} catch (e) {
|
|
116
|
+
loadErrors.push(e)
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
return require('@napi-rs/canvas-win32-x64-msvc')
|
|
120
|
+
} catch (e) {
|
|
121
|
+
loadErrors.push(e)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
} else if (process.arch === 'ia32') {
|
|
125
|
+
try {
|
|
126
|
+
return require('./skia.win32-ia32-msvc.node')
|
|
127
|
+
} catch (e) {
|
|
128
|
+
loadErrors.push(e)
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
return require('@napi-rs/canvas-win32-ia32-msvc')
|
|
132
|
+
} catch (e) {
|
|
133
|
+
loadErrors.push(e)
|
|
134
|
+
}
|
|
135
|
+
} else if (process.arch === 'arm64') {
|
|
136
|
+
try {
|
|
137
|
+
return require('./skia.win32-arm64-msvc.node')
|
|
138
|
+
} catch (e) {
|
|
139
|
+
loadErrors.push(e)
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
return require('@napi-rs/canvas-win32-arm64-msvc')
|
|
143
|
+
} catch (e) {
|
|
144
|
+
loadErrors.push(e)
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`))
|
|
148
|
+
}
|
|
149
|
+
} else if (process.platform === 'darwin') {
|
|
150
|
+
try {
|
|
151
|
+
return require('./skia.darwin-universal.node')
|
|
152
|
+
} catch (e) {
|
|
153
|
+
loadErrors.push(e)
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
return require('@napi-rs/canvas-darwin-universal')
|
|
157
|
+
} catch (e) {
|
|
158
|
+
loadErrors.push(e)
|
|
159
|
+
}
|
|
160
|
+
if (process.arch === 'x64') {
|
|
161
|
+
try {
|
|
162
|
+
return require('./skia.darwin-x64.node')
|
|
163
|
+
} catch (e) {
|
|
164
|
+
loadErrors.push(e)
|
|
165
|
+
}
|
|
166
|
+
try {
|
|
167
|
+
return require('@napi-rs/canvas-darwin-x64')
|
|
168
|
+
} catch (e) {
|
|
169
|
+
loadErrors.push(e)
|
|
170
|
+
}
|
|
171
|
+
} else if (process.arch === 'arm64') {
|
|
172
|
+
try {
|
|
173
|
+
return require('./skia.darwin-arm64.node')
|
|
174
|
+
} catch (e) {
|
|
175
|
+
loadErrors.push(e)
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
return require('@napi-rs/canvas-darwin-arm64')
|
|
179
|
+
} catch (e) {
|
|
180
|
+
loadErrors.push(e)
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`))
|
|
184
|
+
}
|
|
185
|
+
} else if (process.platform === 'freebsd') {
|
|
186
|
+
if (process.arch === 'x64') {
|
|
187
|
+
try {
|
|
188
|
+
return require('./skia.freebsd-x64.node')
|
|
189
|
+
} catch (e) {
|
|
190
|
+
loadErrors.push(e)
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
return require('@napi-rs/canvas-freebsd-x64')
|
|
194
|
+
} catch (e) {
|
|
195
|
+
loadErrors.push(e)
|
|
196
|
+
}
|
|
197
|
+
} else if (process.arch === 'arm64') {
|
|
198
|
+
try {
|
|
199
|
+
return require('./skia.freebsd-arm64.node')
|
|
200
|
+
} catch (e) {
|
|
201
|
+
loadErrors.push(e)
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
return require('@napi-rs/canvas-freebsd-arm64')
|
|
205
|
+
} catch (e) {
|
|
206
|
+
loadErrors.push(e)
|
|
207
|
+
}
|
|
208
|
+
} else {
|
|
209
|
+
loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`))
|
|
210
|
+
}
|
|
211
|
+
} else if (process.platform === 'linux') {
|
|
212
|
+
if (process.arch === 'x64') {
|
|
213
|
+
if (isMusl()) {
|
|
214
|
+
try {
|
|
215
|
+
return require('./skia.linux-x64-musl.node')
|
|
216
|
+
} catch (e) {
|
|
217
|
+
loadErrors.push(e)
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
return require('@napi-rs/canvas-linux-x64-musl')
|
|
221
|
+
} catch (e) {
|
|
222
|
+
loadErrors.push(e)
|
|
223
|
+
}
|
|
224
|
+
} else {
|
|
225
|
+
try {
|
|
226
|
+
return require('./skia.linux-x64-gnu.node')
|
|
227
|
+
} catch (e) {
|
|
228
|
+
loadErrors.push(e)
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
return require('@napi-rs/canvas-linux-x64-gnu')
|
|
232
|
+
} catch (e) {
|
|
233
|
+
loadErrors.push(e)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
} else if (process.arch === 'arm64') {
|
|
237
|
+
if (isMusl()) {
|
|
238
|
+
try {
|
|
239
|
+
return require('./skia.linux-arm64-musl.node')
|
|
240
|
+
} catch (e) {
|
|
241
|
+
loadErrors.push(e)
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
return require('@napi-rs/canvas-linux-arm64-musl')
|
|
245
|
+
} catch (e) {
|
|
246
|
+
loadErrors.push(e)
|
|
247
|
+
}
|
|
248
|
+
} else {
|
|
249
|
+
try {
|
|
250
|
+
return require('./skia.linux-arm64-gnu.node')
|
|
251
|
+
} catch (e) {
|
|
252
|
+
loadErrors.push(e)
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
return require('@napi-rs/canvas-linux-arm64-gnu')
|
|
256
|
+
} catch (e) {
|
|
257
|
+
loadErrors.push(e)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
} else if (process.arch === 'arm') {
|
|
261
|
+
if (isMusl()) {
|
|
262
|
+
try {
|
|
263
|
+
return require('./skia.linux-arm-musleabihf.node')
|
|
264
|
+
} catch (e) {
|
|
265
|
+
loadErrors.push(e)
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
return require('@napi-rs/canvas-linux-arm-musleabihf')
|
|
269
|
+
} catch (e) {
|
|
270
|
+
loadErrors.push(e)
|
|
271
|
+
}
|
|
272
|
+
} else {
|
|
273
|
+
try {
|
|
274
|
+
return require('./skia.linux-arm-gnueabihf.node')
|
|
275
|
+
} catch (e) {
|
|
276
|
+
loadErrors.push(e)
|
|
277
|
+
}
|
|
278
|
+
try {
|
|
279
|
+
return require('@napi-rs/canvas-linux-arm-gnueabihf')
|
|
280
|
+
} catch (e) {
|
|
281
|
+
loadErrors.push(e)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
} else if (process.arch === 'loong64') {
|
|
285
|
+
if (isMusl()) {
|
|
286
|
+
try {
|
|
287
|
+
return require('./skia.linux-loong64-musl.node')
|
|
288
|
+
} catch (e) {
|
|
289
|
+
loadErrors.push(e)
|
|
290
|
+
}
|
|
291
|
+
try {
|
|
292
|
+
return require('@napi-rs/canvas-linux-loong64-musl')
|
|
293
|
+
} catch (e) {
|
|
294
|
+
loadErrors.push(e)
|
|
295
|
+
}
|
|
296
|
+
} else {
|
|
297
|
+
try {
|
|
298
|
+
return require('./skia.linux-loong64-gnu.node')
|
|
299
|
+
} catch (e) {
|
|
300
|
+
loadErrors.push(e)
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
return require('@napi-rs/canvas-linux-loong64-gnu')
|
|
304
|
+
} catch (e) {
|
|
305
|
+
loadErrors.push(e)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
} else if (process.arch === 'riscv64') {
|
|
309
|
+
if (isMusl()) {
|
|
310
|
+
try {
|
|
311
|
+
return require('./skia.linux-riscv64-musl.node')
|
|
312
|
+
} catch (e) {
|
|
313
|
+
loadErrors.push(e)
|
|
314
|
+
}
|
|
315
|
+
try {
|
|
316
|
+
return require('@napi-rs/canvas-linux-riscv64-musl')
|
|
317
|
+
} catch (e) {
|
|
318
|
+
loadErrors.push(e)
|
|
319
|
+
}
|
|
320
|
+
} else {
|
|
321
|
+
try {
|
|
322
|
+
return require('./skia.linux-riscv64-gnu.node')
|
|
323
|
+
} catch (e) {
|
|
324
|
+
loadErrors.push(e)
|
|
325
|
+
}
|
|
326
|
+
try {
|
|
327
|
+
return require('@napi-rs/canvas-linux-riscv64-gnu')
|
|
328
|
+
} catch (e) {
|
|
329
|
+
loadErrors.push(e)
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
} else if (process.arch === 'ppc64') {
|
|
333
|
+
try {
|
|
334
|
+
return require('./skia.linux-ppc64-gnu.node')
|
|
335
|
+
} catch (e) {
|
|
336
|
+
loadErrors.push(e)
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
return require('@napi-rs/canvas-linux-ppc64-gnu')
|
|
340
|
+
} catch (e) {
|
|
341
|
+
loadErrors.push(e)
|
|
342
|
+
}
|
|
343
|
+
} else if (process.arch === 's390x') {
|
|
344
|
+
try {
|
|
345
|
+
return require('./skia.linux-s390x-gnu.node')
|
|
346
|
+
} catch (e) {
|
|
347
|
+
loadErrors.push(e)
|
|
348
|
+
}
|
|
349
|
+
try {
|
|
350
|
+
return require('@napi-rs/canvas-linux-s390x-gnu')
|
|
351
|
+
} catch (e) {
|
|
352
|
+
loadErrors.push(e)
|
|
353
|
+
}
|
|
354
|
+
} else {
|
|
355
|
+
loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`))
|
|
356
|
+
}
|
|
357
|
+
} else if (process.platform === 'openharmony') {
|
|
358
|
+
if (process.arch === 'arm64') {
|
|
359
|
+
try {
|
|
360
|
+
return require('./skia.openharmony-arm64.node')
|
|
361
|
+
} catch (e) {
|
|
362
|
+
loadErrors.push(e)
|
|
363
|
+
}
|
|
364
|
+
try {
|
|
365
|
+
return require('@napi-rs/canvas-openharmony-arm64')
|
|
366
|
+
} catch (e) {
|
|
367
|
+
loadErrors.push(e)
|
|
368
|
+
}
|
|
369
|
+
} else if (process.arch === 'x64') {
|
|
370
|
+
try {
|
|
371
|
+
return require('./skia.openharmony-x64.node')
|
|
372
|
+
} catch (e) {
|
|
373
|
+
loadErrors.push(e)
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
return require('@napi-rs/canvas-openharmony-x64')
|
|
377
|
+
} catch (e) {
|
|
378
|
+
loadErrors.push(e)
|
|
379
|
+
}
|
|
380
|
+
} else if (process.arch === 'arm') {
|
|
381
|
+
try {
|
|
382
|
+
return require('./skia.openharmony-arm.node')
|
|
383
|
+
} catch (e) {
|
|
384
|
+
loadErrors.push(e)
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
const binding = require('@napi-rs/canvas-openharmony-arm')
|
|
388
|
+
return binding
|
|
389
|
+
} catch (e) {
|
|
390
|
+
loadErrors.push(e)
|
|
391
|
+
}
|
|
392
|
+
} else {
|
|
393
|
+
loadErrors.push(new Error(`Unsupported architecture on OpenHarmony: ${process.arch}`))
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
loadErrors.push(new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`))
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
nativeBinding = requireNative()
|
|
401
|
+
|
|
402
|
+
if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
|
|
403
|
+
let wasiBinding = null
|
|
404
|
+
let wasiBindingError = null
|
|
405
|
+
try {
|
|
406
|
+
wasiBinding = require('./skia.wasi.cjs')
|
|
407
|
+
nativeBinding = wasiBinding
|
|
408
|
+
} catch (err) {
|
|
409
|
+
if (process.env.NAPI_RS_FORCE_WASI) {
|
|
410
|
+
wasiBindingError = err
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (!nativeBinding) {
|
|
414
|
+
try {
|
|
415
|
+
wasiBinding = require('@napi-rs/canvas-wasm32-wasi')
|
|
416
|
+
nativeBinding = wasiBinding
|
|
417
|
+
} catch (err) {
|
|
418
|
+
if (process.env.NAPI_RS_FORCE_WASI) {
|
|
419
|
+
wasiBindingError.cause = err
|
|
420
|
+
loadErrors.push(err)
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (!nativeBinding) {
|
|
427
|
+
if (loadErrors.length > 0) {
|
|
428
|
+
throw new Error(
|
|
429
|
+
`Cannot find native binding. ` +
|
|
430
|
+
`npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +
|
|
431
|
+
'Please try `npm i` again after removing both package-lock.json and node_modules directory.',
|
|
432
|
+
{
|
|
433
|
+
cause: loadErrors.reduce((err, cur) => {
|
|
434
|
+
cur.cause = err
|
|
435
|
+
return cur
|
|
436
|
+
}),
|
|
437
|
+
},
|
|
438
|
+
)
|
|
439
|
+
}
|
|
440
|
+
throw new Error(`Failed to load native binding`)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
module.exports = nativeBinding
|
|
444
|
+
module.exports.GlobalFonts = nativeBinding.GlobalFonts
|
|
445
|
+
module.exports.CanvasElement = nativeBinding.CanvasElement
|
|
446
|
+
module.exports.CanvasGradient = nativeBinding.CanvasGradient
|
|
447
|
+
module.exports.CanvasPattern = nativeBinding.CanvasPattern
|
|
448
|
+
module.exports.CanvasRenderingContext2D = nativeBinding.CanvasRenderingContext2D
|
|
449
|
+
module.exports.FontKey = nativeBinding.FontKey
|
|
450
|
+
module.exports.Image = nativeBinding.Image
|
|
451
|
+
module.exports.ImageData = nativeBinding.ImageData
|
|
452
|
+
module.exports.Path = nativeBinding.Path
|
|
453
|
+
module.exports.PdfDocument = nativeBinding.PdfDocument
|
|
454
|
+
module.exports.SVGCanvas = nativeBinding.SVGCanvas
|
|
455
|
+
module.exports.ChromaSubsampling = nativeBinding.ChromaSubsampling
|
|
456
|
+
module.exports.clearAllCache = nativeBinding.clearAllCache
|
|
457
|
+
module.exports.convertSVGTextToPath = nativeBinding.convertSVGTextToPath
|
|
458
|
+
module.exports.setTextRenderer = nativeBinding.setTextRenderer
|
|
459
|
+
module.exports.getTextRenderer = nativeBinding.getTextRenderer
|
|
460
|
+
module.exports.FillType = nativeBinding.FillType
|
|
461
|
+
module.exports.PathOp = nativeBinding.PathOp
|
|
462
|
+
module.exports.StrokeCap = nativeBinding.StrokeCap
|
|
463
|
+
module.exports.StrokeJoin = nativeBinding.StrokeJoin
|
|
464
|
+
module.exports.SvgExportFlag = nativeBinding.SvgExportFlag
|
|
465
|
+
module.exports.GifEncoder = nativeBinding.GifEncoder
|
|
466
|
+
module.exports.GifDisposal = nativeBinding.GifDisposal
|
package/load-image.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const { Readable } = require('stream')
|
|
3
|
+
const { URL } = require('url')
|
|
4
|
+
|
|
5
|
+
const { Image } = require('./js-binding')
|
|
6
|
+
|
|
7
|
+
let http, https
|
|
8
|
+
|
|
9
|
+
const MAX_REDIRECTS = 20
|
|
10
|
+
// Image fires onerror with an Event, as browsers do; the Error behind it is
|
|
11
|
+
// under this key.
|
|
12
|
+
const IMAGE_ERROR = Symbol.for('@napi-rs/canvas/image-error')
|
|
13
|
+
const REDIRECT_STATUSES = new Set([301, 302])
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Loads the given source into canvas Image
|
|
17
|
+
* @param {string|URL|Image|Buffer} source The image source to be loaded
|
|
18
|
+
* @param {object} options Options passed to the loader
|
|
19
|
+
*/
|
|
20
|
+
module.exports = async function loadImage(source, options = {}) {
|
|
21
|
+
// use the same buffer without copying if the source is a buffer
|
|
22
|
+
if (Buffer.isBuffer(source) || source instanceof Uint8Array) return createImage(source, options.alt)
|
|
23
|
+
// load readable stream as image
|
|
24
|
+
if (source instanceof Readable) return createImage(await consumeStream(source), options.alt)
|
|
25
|
+
// construct a Uint8Array if the source is ArrayBuffer or SharedArrayBuffer
|
|
26
|
+
if (source instanceof ArrayBuffer || source instanceof SharedArrayBuffer)
|
|
27
|
+
return createImage(new Uint8Array(source), options.alt)
|
|
28
|
+
// construct a buffer if the source is buffer-like
|
|
29
|
+
if (isBufferLike(source)) return createImage(Buffer.from(source), options.alt)
|
|
30
|
+
// if the source is Image instance, copy the image src to new image
|
|
31
|
+
if (source instanceof Image) return createImage(source.src, options.alt)
|
|
32
|
+
// A data: URL (the URL parser drops leading C0 controls and spaces) is
|
|
33
|
+
// the image's src, as in a browser: Image parses it with the Fetch data:
|
|
34
|
+
// URL processor. A URL object with the data: scheme too.
|
|
35
|
+
// An empty body fires onerror, so the promise settles (issue #1255).
|
|
36
|
+
if (typeof source === 'string' && isDataUrl(source)) return createImage(source, options.alt)
|
|
37
|
+
if (source instanceof URL && source.protocol === 'data:') return createImage(source.href, options.alt)
|
|
38
|
+
// if source is a string or URL instance
|
|
39
|
+
if (typeof source === 'string') {
|
|
40
|
+
// if the source exists as a file, construct image from that file
|
|
41
|
+
if (!source.startsWith('http') && !source.startsWith('https') && (await exists(source))) {
|
|
42
|
+
return createImage(source, options.alt)
|
|
43
|
+
} else {
|
|
44
|
+
// the source is a remote url here
|
|
45
|
+
source = new URL(source)
|
|
46
|
+
// attempt to download the remote source and construct image
|
|
47
|
+
const data = await new Promise((resolve, reject) =>
|
|
48
|
+
makeRequest(
|
|
49
|
+
source,
|
|
50
|
+
resolve,
|
|
51
|
+
reject,
|
|
52
|
+
typeof options.maxRedirects === 'number' && options.maxRedirects >= 0 ? options.maxRedirects : MAX_REDIRECTS,
|
|
53
|
+
options.requestOptions,
|
|
54
|
+
),
|
|
55
|
+
)
|
|
56
|
+
return createImage(data, options.alt)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (source instanceof URL) {
|
|
61
|
+
if (source.protocol === 'file:') {
|
|
62
|
+
// remove the leading slash on windows
|
|
63
|
+
return createImage(process.platform === 'win32' ? source.pathname.substring(1) : source.pathname, options.alt)
|
|
64
|
+
} else {
|
|
65
|
+
const data = await new Promise((resolve, reject) =>
|
|
66
|
+
makeRequest(
|
|
67
|
+
source,
|
|
68
|
+
resolve,
|
|
69
|
+
reject,
|
|
70
|
+
typeof options.maxRedirects === 'number' && options.maxRedirects >= 0 ? options.maxRedirects : MAX_REDIRECTS,
|
|
71
|
+
options.requestOptions,
|
|
72
|
+
),
|
|
73
|
+
)
|
|
74
|
+
return createImage(data, options.alt)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// throw error as don't support that source
|
|
79
|
+
throw new TypeError('unsupported image source')
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function makeRequest(url, resolve, reject, redirectCount, requestOptions) {
|
|
83
|
+
const isHttps = url.protocol === 'https:'
|
|
84
|
+
// lazy load the lib
|
|
85
|
+
const lib = isHttps ? (!https ? (https = require('https')) : https) : !http ? (http = require('http')) : http
|
|
86
|
+
|
|
87
|
+
lib
|
|
88
|
+
.get(url.toString(), requestOptions || {}, (res) => {
|
|
89
|
+
try {
|
|
90
|
+
const shouldRedirect = REDIRECT_STATUSES.has(res.statusCode) && typeof res.headers.location === 'string'
|
|
91
|
+
if (shouldRedirect && redirectCount > 0)
|
|
92
|
+
return makeRequest(
|
|
93
|
+
new URL(res.headers.location, url.origin),
|
|
94
|
+
resolve,
|
|
95
|
+
reject,
|
|
96
|
+
redirectCount - 1,
|
|
97
|
+
requestOptions,
|
|
98
|
+
)
|
|
99
|
+
if (typeof res.statusCode === 'number' && (res.statusCode < 200 || res.statusCode >= 300)) {
|
|
100
|
+
return reject(new Error(`remote source rejected with status code ${res.statusCode}`))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
consumeStream(res).then(resolve, reject)
|
|
104
|
+
} catch (err) {
|
|
105
|
+
reject(err)
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
.on('error', reject)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// use stream/consumers in the future?
|
|
112
|
+
function consumeStream(res) {
|
|
113
|
+
return new Promise((resolve, reject) => {
|
|
114
|
+
const chunks = []
|
|
115
|
+
|
|
116
|
+
res.on('data', (chunk) => chunks.push(chunk))
|
|
117
|
+
res.on('end', () => resolve(Buffer.concat(chunks)))
|
|
118
|
+
res.on('error', reject)
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function createImage(src, alt) {
|
|
123
|
+
// Empty Buffer / Uint8Array: Image follows the HTML spec (silent completion,
|
|
124
|
+
// no events) so the promise below would hang. Reject upfront.
|
|
125
|
+
// See https://github.com/Brooooooklyn/canvas/issues/1255
|
|
126
|
+
if ((Buffer.isBuffer(src) || src instanceof Uint8Array) && src.length === 0) {
|
|
127
|
+
throw new Error('loadImage: empty image data')
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const image = new Image()
|
|
131
|
+
if (typeof alt === 'string') image.alt = alt
|
|
132
|
+
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
image.onload = () => {
|
|
135
|
+
// Wait for bitmap decode before resolving. SVG sources fire onload
|
|
136
|
+
// synchronously inside the src setter, where napi >= 3.12 rejects
|
|
137
|
+
// reentrant &mut borrows of the Image, so decode on a microtask.
|
|
138
|
+
Promise.resolve()
|
|
139
|
+
.then(() => image.decode())
|
|
140
|
+
.then(() => resolve(image), reject)
|
|
141
|
+
}
|
|
142
|
+
image.onerror = (event) => reject((event && event[IMAGE_ERROR]) || event)
|
|
143
|
+
image.src = src
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// The URL parser skips leading C0 controls and spaces; the scheme is
|
|
148
|
+
// ASCII case-insensitive.
|
|
149
|
+
function isDataUrl(src) {
|
|
150
|
+
let i = 0
|
|
151
|
+
while (i < src.length && src.charCodeAt(i) <= 0x20) i++
|
|
152
|
+
return src.slice(i, i + 5).toLowerCase() === 'data:'
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function isBufferLike(src) {
|
|
156
|
+
return (src && src.type === 'Buffer') || Array.isArray(src)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function exists(path) {
|
|
160
|
+
try {
|
|
161
|
+
await fs.promises.access(path, fs.constants.F_OK)
|
|
162
|
+
return true
|
|
163
|
+
} catch {
|
|
164
|
+
return false
|
|
165
|
+
}
|
|
166
|
+
}
|