Skip to content
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
10 changes: 9 additions & 1 deletion js/polliLib/src/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ export async function image(prompt, {
const data = await r.json();
if (json) return data;
if (data?.url) {
const ir = await fetch(data.url);
const ir = await client.get(data.url);
if (ir.ok) return await ir.blob();
if (retries > 0) {
await sleep(retryDelayMs);
return await image(prompt, {
model, seed, width, height, image, nologo, private: priv,
enhance, safe, referrer, json, retries: retries - 1, retryDelayMs,
}, client);
}
throw new Error(`image error ${ir.status}`);
}
if (retries > 0) {
await sleep(retryDelayMs);
Expand Down
3 changes: 2 additions & 1 deletion tests/pollilib-async-image.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const client = {
}
};

global.fetch = async () => ({ ok: true, blob: async () => new Blob(['x'], { type: 'image/png' }) });
global.fetch = () => { throw new Error('fetch should not be called'); };

const blob = await image('test', {}, client);
assert(blob instanceof Blob);
assert.equal(calls, 2);
32 changes: 32 additions & 0 deletions tests/pollilib-image-error.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import assert from 'assert/strict';
import fs from 'fs/promises';
import { image } from '../js/polliLib/src/image.js';

let calls = 0;
const pending = JSON.parse(await fs.readFile(new URL('./fixtures/pending-image.json', import.meta.url), 'utf8'));
const client = {
imageBase: 'https://example.com',
async get(url, opts = {}) {
calls++;
if (calls === 1) {
return {
ok: true,
headers: { get: () => 'application/json' },
async json() { return pending; },
};
}
return { ok: false, status: 500, headers: { get: () => 'text/plain' } };
}
};

global.fetch = () => { throw new Error('fetch should not be called'); };

let caught = null;
try {
await image('test', { retries: 0 }, client);
} catch (err) {
caught = err;
}
assert(caught);
assert.equal(caught.message, 'image error 500');
assert.equal(calls, 2);