@napi-rs/canvas 0.1.57 → 0.1.58
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/load-image.js +8 -9
- package/package.json +10 -10
package/load-image.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
const fs = require('fs')
|
|
2
|
-
const { Image } = require('./js-binding')
|
|
3
2
|
const { Readable } = require('stream')
|
|
4
3
|
|
|
4
|
+
const { Image } = require('./js-binding')
|
|
5
|
+
|
|
5
6
|
let http, https
|
|
6
7
|
|
|
7
8
|
const MAX_REDIRECTS = 20
|
|
@@ -13,10 +14,12 @@ const REDIRECT_STATUSES = new Set([301, 302])
|
|
|
13
14
|
* @param {object} options Options passed to the loader
|
|
14
15
|
*/
|
|
15
16
|
module.exports = async function loadImage(source, options = {}) {
|
|
17
|
+
// use the same buffer without copying if the source is a buffer
|
|
18
|
+
if (Buffer.isBuffer(source) || source instanceof Uint8Array) return createImage(source, options.alt)
|
|
16
19
|
// load readable stream as image
|
|
17
20
|
if (source instanceof Readable) return createImage(await consumeStream(source), options.alt)
|
|
18
|
-
//
|
|
19
|
-
if (
|
|
21
|
+
// construct a Uint8Array if the source is ArrayBuffer or SharedArrayBuffer
|
|
22
|
+
if (source instanceof ArrayBuffer || source instanceof SharedArrayBuffer) return createImage(Uint8Array.from(source), options.alt)
|
|
20
23
|
// construct a buffer if the source is buffer-like
|
|
21
24
|
if (isBufferLike(source)) return createImage(Buffer.from(source), options.alt)
|
|
22
25
|
// if the source is Image instance, copy the image src to new image
|
|
@@ -68,7 +71,7 @@ function makeRequest(url, resolve, reject, redirectCount, requestOptions) {
|
|
|
68
71
|
if (typeof res.statusCode === 'number' && (res.statusCode < 200 || res.statusCode >= 300)) {
|
|
69
72
|
return reject(new Error(`remote source rejected with status code ${res.statusCode}`))
|
|
70
73
|
}
|
|
71
|
-
|
|
74
|
+
|
|
72
75
|
consumeStream(res).then(resolve, reject)
|
|
73
76
|
} catch (err) {
|
|
74
77
|
reject(err)
|
|
@@ -100,11 +103,7 @@ function createImage(src, alt) {
|
|
|
100
103
|
|
|
101
104
|
function isBufferLike(src) {
|
|
102
105
|
return (
|
|
103
|
-
(src && src.type === 'Buffer') ||
|
|
104
|
-
Array.isArray(src) ||
|
|
105
|
-
src instanceof ArrayBuffer ||
|
|
106
|
-
src instanceof SharedArrayBuffer ||
|
|
107
|
-
src instanceof Object.getPrototypeOf(Uint8Array)
|
|
106
|
+
(src && src.type === 'Buffer') || Array.isArray(src)
|
|
108
107
|
)
|
|
109
108
|
}
|
|
110
109
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@napi-rs/canvas",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.58",
|
|
4
4
|
"description": "Canvas for Node.js with skia backend",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -137,14 +137,14 @@
|
|
|
137
137
|
},
|
|
138
138
|
"packageManager": "yarn@4.5.0",
|
|
139
139
|
"optionalDependencies": {
|
|
140
|
-
"@napi-rs/canvas-win32-x64-msvc": "0.1.
|
|
141
|
-
"@napi-rs/canvas-darwin-x64": "0.1.
|
|
142
|
-
"@napi-rs/canvas-linux-x64-gnu": "0.1.
|
|
143
|
-
"@napi-rs/canvas-linux-arm-gnueabihf": "0.1.
|
|
144
|
-
"@napi-rs/canvas-linux-x64-musl": "0.1.
|
|
145
|
-
"@napi-rs/canvas-linux-arm64-gnu": "0.1.
|
|
146
|
-
"@napi-rs/canvas-linux-arm64-musl": "0.1.
|
|
147
|
-
"@napi-rs/canvas-darwin-arm64": "0.1.
|
|
148
|
-
"@napi-rs/canvas-android-arm64": "0.1.
|
|
140
|
+
"@napi-rs/canvas-win32-x64-msvc": "0.1.58",
|
|
141
|
+
"@napi-rs/canvas-darwin-x64": "0.1.58",
|
|
142
|
+
"@napi-rs/canvas-linux-x64-gnu": "0.1.58",
|
|
143
|
+
"@napi-rs/canvas-linux-arm-gnueabihf": "0.1.58",
|
|
144
|
+
"@napi-rs/canvas-linux-x64-musl": "0.1.58",
|
|
145
|
+
"@napi-rs/canvas-linux-arm64-gnu": "0.1.58",
|
|
146
|
+
"@napi-rs/canvas-linux-arm64-musl": "0.1.58",
|
|
147
|
+
"@napi-rs/canvas-darwin-arm64": "0.1.58",
|
|
148
|
+
"@napi-rs/canvas-android-arm64": "0.1.58"
|
|
149
149
|
}
|
|
150
150
|
}
|