Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loadImage should handle file URL #959

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions __test__/loadimage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from 'node:path'
import fs from 'node:fs'
import { readFile } from 'node:fs/promises'
import { URL, pathToFileURL } from 'node:url'

import test from 'ava'

Expand Down Expand Up @@ -30,6 +31,14 @@ test('should load remote url', async (t) => {
'https://raw.githubusercontent.com/Brooooooklyn/canvas/462fce53afeaee6d6b4ae5d1b407c17e2359ff7e/example/anime-girl.png',
)
t.is(img instanceof Image, true)
t.is(
(await loadImage(
new URL(
'https://raw.githubusercontent.com/Brooooooklyn/canvas/462fce53afeaee6d6b4ae5d1b407c17e2359ff7e/example/anime-girl.png',
),
)) instanceof Image,
true,
)
})

test('should load arrayBuffer', async (t) => {
Expand Down Expand Up @@ -68,3 +77,10 @@ test('should load issue-672 img', async (t) => {
t.is(img.width, 297)
t.is(img.height, 465)
})

test('should load file url', async (t) => {
const url = new URL('__test__/javascript.png', pathToFileURL(__dirname))
const img = await loadImage(url)
t.is(img.width, 512)
t.is(img.height, 512)
})
25 changes: 22 additions & 3 deletions load-image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs')
const { Readable } = require('stream')
const { URL } = require('url')

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

Expand Down Expand Up @@ -33,13 +34,13 @@ module.exports = async function loadImage(source, options = {}) {
return createImage(data, options.alt)
}
// if source is a string or URL instance
if (typeof source === 'string' || source instanceof URL) {
if (typeof source === 'string') {
// if the source exists as a file, construct image from that file
if (await exists(source)) {
if ((!source.startsWith('http') && !source.startsWith('https')) && await exists(source)) {
return createImage(source, options.alt)
} else {
// the source is a remote url here
source = !(source instanceof URL) ? new URL(source) : source
source = new URL(source)
// attempt to download the remote source and construct image
const data = await new Promise((resolve, reject) =>
makeRequest(
Expand All @@ -54,6 +55,24 @@ module.exports = async function loadImage(source, options = {}) {
}
}

if (source instanceof URL) {
if (source.protocol === 'file:') {
// remove the leading slash on windows
return createImage(process.platform === 'win32' ? source.pathname.substring(1) : source.pathname, options.alt)
} else {
const data = await new Promise((resolve, reject) =>
makeRequest(
source,
resolve,
reject,
typeof options.maxRedirects === 'number' && options.maxRedirects >= 0 ? options.maxRedirects : MAX_REDIRECTS,
options.requestOptions,
),
)
return createImage(data, options.alt)
}
}

// throw error as don't support that source
throw new TypeError('unsupported image source')
}
Expand Down