Skip to content

Commit b410d7f

Browse files
authored
Feature/400/fetch images from url (#401)
* (#400) Implement fetchFromUrl + tests * (#400) Re-export fetchFromUrl as part of the public API * (#400) Added docstring for fetchFromUrl
1 parent 4183d97 commit b410d7f

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const loadImage = providerRegistry.getImageReader().load;
5252
const saveImage = providerRegistry.getImageWriter().store;
5353

5454
const imageResource = (fileName: string) => loadImageResource(providerRegistry, screen.config.resourceDirectory, fileName);
55+
export {fetchFromUrl} from "./lib/imageResources.function";
5556

5657
export {
5758
clipboard,

lib/imageResources.function.spec.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {loadImageResource} from "./imageResources.function";
1+
import {fetchFromUrl, loadImageResource} from "./imageResources.function";
22
import {mockPartial} from "sneer";
33
import {ProviderRegistry} from "./provider/provider-registry.class";
44
import {ImageReader} from "./provider";
55
import {join} from "path";
6+
import {ColorMode} from "./colormode.enum";
67

78
const loadMock = jest.fn();
89
const providerRegistryMock = mockPartial<ProviderRegistry>({
@@ -25,4 +26,46 @@ describe('imageResources', () => {
2526
// THEN
2627
expect(loadMock).toBeCalledWith(join(resourceDirectoryPath, imageFileName));
2728
});
29+
});
30+
31+
describe('fetchFromUrl', () => {
32+
it('should throw on malformed URLs', async () => {
33+
// GIVEN
34+
const malformedUrl = "foo";
35+
36+
// WHEN
37+
const SUT = () => fetchFromUrl(malformedUrl);
38+
39+
// THEN
40+
await expect(SUT).rejects.toThrowError("Invalid URL");
41+
});
42+
43+
it('should throw on non-image URLs', async () => {
44+
// GIVEN
45+
const nonImageUrl = 'https://www.npmjs.com/package/jimp';
46+
47+
// WHEN
48+
const SUT = () => fetchFromUrl(nonImageUrl);
49+
50+
// THEN
51+
await expect(SUT).rejects.toThrowError('Could not find MIME for Buffer');
52+
});
53+
54+
it('should return an RGB image from a valid URL', async () => {
55+
// GIVEN
56+
const validImageUrl = 'https://github.com/nut-tree/nut.js/raw/master/.gfx/nut.png';
57+
const expectedDimensions = {
58+
width: 502,
59+
height: 411
60+
};
61+
const expectedColorMode = ColorMode.RGB;
62+
63+
// WHEN
64+
const rgbImage = await fetchFromUrl(validImageUrl);
65+
66+
// THEN
67+
expect(rgbImage.colorMode).toBe(expectedColorMode);
68+
expect(rgbImage.width).toBe(expectedDimensions.width);
69+
expect(rgbImage.height).toBe(expectedDimensions.height);
70+
});
2871
});

lib/imageResources.function.ts

+36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
import {join, normalize} from "path";
22
import {ProviderRegistry} from "./provider/provider-registry.class";
3+
import {URL} from "url";
4+
import {Image} from "./image.class";
5+
import Jimp from "jimp";
6+
import {ColorMode} from "./colormode.enum";
37

48
export function loadImageResource(providerRegistry: ProviderRegistry, resourceDirectory: string, fileName: string) {
59
const fullPath = normalize(join(resourceDirectory, fileName));
610
return providerRegistry.getImageReader().load(fullPath);
11+
}
12+
13+
/**
14+
* fetchFromUrl loads remote image content at runtime to provide it for further use in on-screen image search
15+
* @param url The remote URl to fetch an image from as string or {@link URL}
16+
* @throws On malformed URL input or in case of non-image remote content
17+
*/
18+
export async function fetchFromUrl(url: string | URL): Promise<Image> {
19+
let imageUrl: URL;
20+
if (url instanceof URL) {
21+
imageUrl = url;
22+
} else {
23+
try {
24+
imageUrl = new URL(url);
25+
} catch (e) {
26+
throw e;
27+
}
28+
}
29+
return Jimp.read(imageUrl.href)
30+
.then((image) => {
31+
return new Image(
32+
image.bitmap.width,
33+
image.bitmap.height,
34+
image.bitmap.data,
35+
4,
36+
imageUrl.href,
37+
ColorMode.RGB
38+
);
39+
})
40+
.catch(err => {
41+
throw err;
42+
});
743
}

0 commit comments

Comments
 (0)