Skip to content

Commit

Permalink
fix: loadImage memleak and performance issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Oct 14, 2024
1 parent 2f14158 commit a2854fd
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions load-image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require('fs')
const { Image } = require('./js-binding')
const { Readable } = require('stream')

const { Image } = require('./js-binding')

let http, https

const MAX_REDIRECTS = 20
Expand All @@ -13,10 +14,12 @@ const REDIRECT_STATUSES = new Set([301, 302])
* @param {object} options Options passed to the loader
*/
module.exports = async function loadImage(source, options = {}) {
// use the same buffer without copying if the source is a buffer
if (Buffer.isBuffer(source) || source instanceof Uint8Array) return createImage(source, options.alt)
// load readable stream as image
if (source instanceof Readable) return createImage(await consumeStream(source), options.alt)
// use the same buffer without copying if the source is a buffer
if (Buffer.isBuffer(source)) return createImage(source, options.alt)
// construct a Uint8Array if the source is ArrayBuffer or SharedArrayBuffer
if (source instanceof ArrayBuffer || source instanceof SharedArrayBuffer) return createImage(Uint8Array.from(source), options.alt)
// construct a buffer if the source is buffer-like
if (isBufferLike(source)) return createImage(Buffer.from(source), options.alt)
// if the source is Image instance, copy the image src to new image
Expand Down Expand Up @@ -68,7 +71,7 @@ function makeRequest(url, resolve, reject, redirectCount, requestOptions) {
if (typeof res.statusCode === 'number' && (res.statusCode < 200 || res.statusCode >= 300)) {
return reject(new Error(`remote source rejected with status code ${res.statusCode}`))
}

consumeStream(res).then(resolve, reject)
} catch (err) {
reject(err)
Expand Down Expand Up @@ -100,11 +103,7 @@ function createImage(src, alt) {

function isBufferLike(src) {
return (
(src && src.type === 'Buffer') ||
Array.isArray(src) ||
src instanceof ArrayBuffer ||
src instanceof SharedArrayBuffer ||
src instanceof Object.getPrototypeOf(Uint8Array)
(src && src.type === 'Buffer') || Array.isArray(src)
)
}

Expand Down

0 comments on commit a2854fd

Please sign in to comment.