Skip to content

Commit

Permalink
fix: loadImage convert ArrayBuffer issue (#920)
Browse files Browse the repository at this point in the history
- Close #919
  • Loading branch information
Brooooooklyn authored Oct 27, 2024
1 parent b099c0a commit 0636775
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 11 additions & 2 deletions __test__/loadimage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { join } from 'path'
import { join } from 'node:path'
import fs from 'node:fs'
import { readFile } from 'node:fs/promises'

import test from 'ava'
import fs from 'fs'

import { createCanvas, Image, loadImage } from '../index'

Expand Down Expand Up @@ -30,6 +32,13 @@ test('should load remote url', async (t) => {
t.is(img instanceof Image, true)
})

test('should load arrayBuffer', async (t) => {
const imageBuffer = await readFile(join(__dirname, '../example/simple.png'))
const img = await loadImage(imageBuffer.buffer)
t.is(img instanceof Image, true)
t.true(img.width > 0)
})

test('should load data uri', async (t) => {
const img = await loadImage(
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII',
Expand Down
15 changes: 10 additions & 5 deletions load-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = async function loadImage(source, options = {}) {
// load readable stream as image
if (source instanceof Readable) return createImage(await consumeStream(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)
if (source instanceof ArrayBuffer || source instanceof SharedArrayBuffer)
return createImage(new Uint8Array(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 @@ -67,7 +68,13 @@ function makeRequest(url, resolve, reject, redirectCount, requestOptions) {
try {
const shouldRedirect = REDIRECT_STATUSES.has(res.statusCode) && typeof res.headers.location === 'string'
if (shouldRedirect && redirectCount > 0)
return makeRequest(new URL(res.headers.location, url.origin), resolve, reject, redirectCount - 1, requestOptions)
return makeRequest(
new URL(res.headers.location, url.origin),
resolve,
reject,
redirectCount - 1,
requestOptions,
)
if (typeof res.statusCode === 'number' && (res.statusCode < 200 || res.statusCode >= 300)) {
return reject(new Error(`remote source rejected with status code ${res.statusCode}`))
}
Expand Down Expand Up @@ -102,9 +109,7 @@ function createImage(src, alt) {
}

function isBufferLike(src) {
return (
(src && src.type === 'Buffer') || Array.isArray(src)
)
return (src && src.type === 'Buffer') || Array.isArray(src)
}

async function exists(path) {
Expand Down

0 comments on commit 0636775

Please sign in to comment.