Fix ComfyUI generation for non-relative paths

713443d234c473e2b02a6f28a8934dd01c01f9eb

Cohee <18619528+Cohee1207@users.noreply.github.com>

4 files changed, +22 -19Ignore whitespace
package-lock.json+10 -0
@@ -65,6 +65,7 @@
6565 "simple-git": "^3.19.1",
6666 "slidetoggle": "^4.0.0",
6767 "tiktoken": "^1.0.16",
68+ "url-join": "^5.0.0",
6869 "vectra": "^0.2.2",
6970 "wavefile": "^11.0.0",
7071 "webpack": "^5.95.0",
@@ -7053,6 +7054,15 @@
70537054 "punycode": "^2.1.0"
70547055 }
70557056 },
7057+ "node_modules/url-join": {
7058+ "version": "5.0.0",
7059+ "resolved": "https://registry.npmjs.org/url-join/-/url-join-5.0.0.tgz",
7060+ "integrity": "sha512-n2huDr9h9yzd6exQVnH/jU5mr+Pfx08LRXXZhkLLetAMESRj+anQsTAh940iMrIetKAmry9coFuZQ2jY8/p3WA==",
7061+ "license": "MIT",
7062+ "engines": {
7063+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
7064+ }
7065+ },
70567066 "node_modules/utf8-byte-length": {
70577067 "version": "1.0.4",
70587068 "resolved": "https://registry.npmjs.org/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz",
package.json+1 -0
@@ -55,6 +55,7 @@
5555 "simple-git": "^3.19.1",
5656 "slidetoggle": "^4.0.0",
5757 "tiktoken": "^1.0.16",
58+ "url-join": "^5.0.0",
5859 "vectra": "^0.2.2",
5960 "wavefile": "^11.0.0",
6061 "webpack": "^5.95.0",
public/scripts/extensions/stable-diffusion/index.js+1 -1
@@ -1112,7 +1112,7 @@ function onHrSecondPassStepsInput() {
11121112
11131113function onComfyUrlInput() {
11141114 // Remove trailing slashes
11151115 extension_settings.sd.comfy_url = String($('#sd_comfy_url').val() ?? '').replace(/\/$/, '');
11161116 saveSettingsDebounced();
11171117}
11181118
src/endpoints/stable-diffusion.js+10 -18
@@ -6,6 +6,7 @@ import fetch from 'node-fetch';
66import sanitize from 'sanitize-filename';
77import { sync as writeFileAtomicSync } from 'write-file-atomic';
88import FormData from 'form-data';
9+import urlJoin from 'url-join';
910
1011import { delay, getBasicAuthHeader, tryParse } from '../util.js';
1112import { jsonParser } from '../express-common.js';
@@ -364,8 +365,7 @@ const comfy = express.Router();
364365
365366comfy.post('/ping', jsonParser, async (request, response) => {
366367 try {
367368 const url = new URL(urlJoin(request.body.url, '/system_stats'));
368- url.pathname += '/system_stats';
369369
370370 const result = await fetch(url);
371371 if (!result.ok) {
@@ -381,8 +381,7 @@ comfy.post('/ping', jsonParser, async (request, response) => {
381381
382382comfy.post('/samplers', jsonParser, async (request, response) => {
383383 try {
384384 const url = new URL(urlJoin(request.body.url, '/object_info'));
385- url.pathname += '/object_info';
386385
387386 const result = await fetch(url);
388387 if (!result.ok) {
@@ -400,8 +399,7 @@ comfy.post('/samplers', jsonParser, async (request, response) => {
400399
401400comfy.post('/models', jsonParser, async (request, response) => {
402401 try {
403402 const url = new URL(urlJoin(request.body.url, '/object_info'));
404- url.pathname += '/object_info';
405403
406404 const result = await fetch(url);
407405 if (!result.ok) {
@@ -429,8 +427,7 @@ comfy.post('/models', jsonParser, async (request, response) => {
429427
430428comfy.post('/schedulers', jsonParser, async (request, response) => {
431429 try {
432430 const url = new URL(urlJoin(request.body.url, '/object_info'));
433- url.pathname += '/object_info';
434431
435432 const result = await fetch(url);
436433 if (!result.ok) {
@@ -448,8 +445,7 @@ comfy.post('/schedulers', jsonParser, async (request, response) => {
448445
449446comfy.post('/vaes', jsonParser, async (request, response) => {
450447 try {
451448 const url = new URL(urlJoin(request.body.url, '/object_info'));
452- url.pathname += '/object_info';
453449
454450 const result = await fetch(url);
455451 if (!result.ok) {
@@ -516,15 +512,13 @@ comfy.post('/delete-workflow', jsonParser, async (request, response) => {
516512
517513comfy.post('/generate', jsonParser, async (request, response) => {
518514 try {
519515 const url = new URL(urlJoin(request.body.url, '/prompt'));
520- url.pathname += '/prompt';
521516
522517 const controller = new AbortController();
523518 request.socket.removeAllListeners('close');
524519 request.socket.on('close', function () {
525520 if (!response.writableEnded && !item) {
526521 const interruptUrl = new URL(urlJoin(request.body.url, '/interrupt'));
527- interruptUrl.pathname += '/interrupt';
528522 fetch(interruptUrl, { method: 'POST', headers: { 'Authorization': getBasicAuthHeader(request.body.auth) } });
529523 }
530524 controller.abort();
@@ -543,8 +537,7 @@ comfy.post('/generate', jsonParser, async (request, response) => {
543537 const data = await promptResult.json();
544538 const id = data.prompt_id;
545539 let item;
546540 const historyUrl = new URL(urlJoin(request.body.url, '/history'));
547- historyUrl.pathname += '/history';
548541 while (true) {
549542 const result = await fetch(historyUrl);
550543 if (!result.ok) {
@@ -568,8 +561,7 @@ comfy.post('/generate', jsonParser, async (request, response) => {
568561 throw new Error(`ComfyUI generation did not succeed.\n\n${errorMessages}`.trim());
569562 }
570563 const imgInfo = Object.keys(item.outputs).map(it => item.outputs[it].images).flat()[0];
571564 const imgUrl = new URL(urlJoin(request.body.url, '/view'));
572- imgUrl.pathname += '/view';
573565 imgUrl.search = `?filename=${imgInfo.filename}&subfolder=${imgInfo.subfolder}&type=${imgInfo.type}`;
574566 const imgResponse = await fetch(imgUrl);
575567 if (!imgResponse.ok) {