@gradio/client 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/dist/client.d.ts +2 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/index.js +69 -12
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +82 -15
- package/src/utils.ts +21 -0
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,37 @@
|
|
1
1
|
# @gradio/client
|
2
2
|
|
3
|
+
## 0.5.0
|
4
|
+
|
5
|
+
### Highlights
|
6
|
+
|
7
|
+
#### new `FileExplorer` component ([#5672](https://github.com/gradio-app/gradio/pull/5672) [`e4a307ed6`](https://github.com/gradio-app/gradio/commit/e4a307ed6cde3bbdf4ff2f17655739addeec941e))
|
8
|
+
|
9
|
+
Thanks to a new capability that allows components to communicate directly with the server _without_ passing data via the value, we have created a new `FileExplorer` component.
|
10
|
+
|
11
|
+
This component allows you to populate the explorer by passing a glob, but only provides the selected file(s) in your prediction function.
|
12
|
+
|
13
|
+
Users can then navigate the virtual filesystem and select files which will be accessible in your predict function. This component will allow developers to build more complex spaces, with more flexible input options.
|
14
|
+
|
15
|
+

|
16
|
+
|
17
|
+
For more information check the [`FileExplorer` documentation](https://gradio.app/docs/fileexplorer).
|
18
|
+
|
19
|
+
Thanks [@aliabid94](https://github.com/aliabid94)!
|
20
|
+
|
21
|
+
### Features
|
22
|
+
|
23
|
+
- [#5787](https://github.com/gradio-app/gradio/pull/5787) [`caeee8bf7`](https://github.com/gradio-app/gradio/commit/caeee8bf7821fd5fe2f936ed82483bed00f613ec) - ensure the client does not depend on `window` when running in a node environment. Thanks [@gibiee](https://github.com/gibiee)!
|
24
|
+
|
25
|
+
### Fixes
|
26
|
+
|
27
|
+
- [#5776](https://github.com/gradio-app/gradio/pull/5776) [`c0fef4454`](https://github.com/gradio-app/gradio/commit/c0fef44541bfa61568bdcfcdfc7d7d79869ab1df) - Revert replica proxy logic and instead implement using the `root` variable. Thanks [@freddyaboulton](https://github.com/freddyaboulton)!
|
28
|
+
|
29
|
+
## 0.4.2
|
30
|
+
|
31
|
+
### Features
|
32
|
+
|
33
|
+
- [#5124](https://github.com/gradio-app/gradio/pull/5124) [`6e56a0d9b`](https://github.com/gradio-app/gradio/commit/6e56a0d9b0c863e76c69e1183d9d40196922b4cd) - Lite: Websocket queueing. Thanks [@whitphx](https://github.com/whitphx)!
|
34
|
+
|
3
35
|
## 0.4.1
|
4
36
|
|
5
37
|
### Fixes
|
package/dist/client.d.ts
CHANGED
@@ -7,6 +7,7 @@ type client_return = {
|
|
7
7
|
predict: predict;
|
8
8
|
config: Config;
|
9
9
|
submit: (endpoint: string | number, data?: unknown[], event_data?: unknown) => SubmitReturn;
|
10
|
+
component_server: (component_id: number, fn_name: string, data: unknown[]) => any;
|
10
11
|
view_api: (c?: Config) => Promise<ApiInfo<JsApiData>>;
|
11
12
|
};
|
12
13
|
type SubmitReturn = {
|
@@ -33,7 +34,7 @@ interface Client {
|
|
33
34
|
}) => Promise<client_return>;
|
34
35
|
handle_blob: (endpoint: string, data: unknown[], api_info: ApiInfo<JsApiData>, token?: `hf_${string}`) => Promise<unknown[]>;
|
35
36
|
}
|
36
|
-
export declare function api_factory(fetch_implementation: typeof fetch): Client;
|
37
|
+
export declare function api_factory(fetch_implementation: typeof fetch, WebSocket_factory: (url: URL) => WebSocket): Client;
|
37
38
|
export declare const post_data: (url: string, body: unknown, token?: `hf_${string}`) => Promise<[PostResponse, number]>, upload_files: (root: string, files: File[], token?: `hf_${string}`) => Promise<UploadResponse>, client: (app_reference: string, options: {
|
38
39
|
hf_token?: `hf_${string}`;
|
39
40
|
status_callback?: SpaceStatusCallback;
|
package/dist/client.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OA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,EAAE,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;KAC/B,CAAC;CACF;AAiID,wBAAsB,oBAAoB,CACzC,KAAK,KAAA,EACL,IAAI,MAAY,EAChB,IAAI,QAAK,EACT,IAAI,UAAQ,EACZ,QAAQ,MAAY,GAClB,OAAO,CACT;IACC,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,IAAI,GAAG,KAAK,CAAC;CACnB,EAAE,CACH,CAsEA"}
|
package/dist/index.js
CHANGED
@@ -4,6 +4,12 @@ function semiver(a, b, bool) {
|
|
4
4
|
b = b.split(".");
|
5
5
|
return fn(a[0], b[0]) || fn(a[1], b[1]) || (b[2] = b.slice(2).join("."), bool = /[.-]/.test(a[2] = a.slice(2).join(".")), bool == /[.-]/.test(b[2]) ? fn(a[2], b[2]) : bool ? -1 : 1);
|
6
6
|
}
|
7
|
+
function resolve_root(base_url, root_path, prioritize_base) {
|
8
|
+
if (root_path.startsWith("http://") || root_path.startsWith("https://")) {
|
9
|
+
return prioritize_base ? base_url : root_path;
|
10
|
+
}
|
11
|
+
return base_url + root_path;
|
12
|
+
}
|
7
13
|
function determine_protocol(endpoint) {
|
8
14
|
if (endpoint.startsWith("http")) {
|
9
15
|
const { protocol, host } = new URL(endpoint);
|
@@ -209,7 +215,7 @@ async function duplicate(app_reference, options) {
|
|
209
215
|
throw new Error(e);
|
210
216
|
}
|
211
217
|
}
|
212
|
-
function api_factory(fetch_implementation) {
|
218
|
+
function api_factory(fetch_implementation, WebSocket_factory) {
|
213
219
|
return { post_data: post_data2, upload_files: upload_files2, client: client2, handle_blob: handle_blob2 };
|
214
220
|
async function post_data2(url, body, token) {
|
215
221
|
const headers = { "Content-Type": "application/json" };
|
@@ -261,7 +267,8 @@ function api_factory(fetch_implementation) {
|
|
261
267
|
const return_obj = {
|
262
268
|
predict,
|
263
269
|
submit,
|
264
|
-
view_api
|
270
|
+
view_api,
|
271
|
+
component_server
|
265
272
|
// duplicate
|
266
273
|
};
|
267
274
|
const transform_files = normalise_files ?? true;
|
@@ -408,11 +415,14 @@ function api_factory(fetch_implementation) {
|
|
408
415
|
let payload;
|
409
416
|
let complete = false;
|
410
417
|
const listener_map = {};
|
411
|
-
|
412
|
-
|
413
|
-
|
418
|
+
let url_params = "";
|
419
|
+
if (typeof window !== "undefined") {
|
420
|
+
url_params = new URLSearchParams(
|
421
|
+
window.location.search
|
422
|
+
).toString();
|
423
|
+
}
|
414
424
|
handle_blob2(
|
415
|
-
`${http_protocol}//${host
|
425
|
+
`${http_protocol}//${resolve_root(host, config.path, true)}`,
|
416
426
|
data,
|
417
427
|
api_info,
|
418
428
|
hf_token
|
@@ -428,7 +438,7 @@ function api_factory(fetch_implementation) {
|
|
428
438
|
time: /* @__PURE__ */ new Date()
|
429
439
|
});
|
430
440
|
post_data2(
|
431
|
-
`${http_protocol}//${host
|
441
|
+
`${http_protocol}//${resolve_root(host, config.path, true)}/run${_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`}${url_params ? "?" + url_params : ""}`,
|
432
442
|
{
|
433
443
|
...payload,
|
434
444
|
session_hash
|
@@ -489,12 +499,16 @@ function api_factory(fetch_implementation) {
|
|
489
499
|
fn_index,
|
490
500
|
time: /* @__PURE__ */ new Date()
|
491
501
|
});
|
492
|
-
let url = new URL(`${ws_protocol}://${
|
502
|
+
let url = new URL(`${ws_protocol}://${resolve_root(
|
503
|
+
host,
|
504
|
+
config.path,
|
505
|
+
true
|
506
|
+
)}
|
493
507
|
/queue/join${url_params ? "?" + url_params : ""}`);
|
494
508
|
if (jwt) {
|
495
509
|
url.searchParams.set("__sign", jwt);
|
496
510
|
}
|
497
|
-
websocket =
|
511
|
+
websocket = WebSocket_factory(url);
|
498
512
|
websocket.onclose = (evt) => {
|
499
513
|
if (!evt.wasClean) {
|
500
514
|
fire_event({
|
@@ -628,7 +642,11 @@ function api_factory(fetch_implementation) {
|
|
628
642
|
}
|
629
643
|
try {
|
630
644
|
await fetch_implementation(
|
631
|
-
`${http_protocol}//${
|
645
|
+
`${http_protocol}//${resolve_root(
|
646
|
+
host,
|
647
|
+
config.path,
|
648
|
+
true
|
649
|
+
)}/reset`,
|
632
650
|
{
|
633
651
|
headers: { "Content-Type": "application/json" },
|
634
652
|
method: "POST",
|
@@ -655,6 +673,42 @@ function api_factory(fetch_implementation) {
|
|
655
673
|
destroy
|
656
674
|
};
|
657
675
|
}
|
676
|
+
async function component_server(component_id, fn_name, data) {
|
677
|
+
var _a;
|
678
|
+
const headers = { "Content-Type": "application/json" };
|
679
|
+
if (hf_token) {
|
680
|
+
headers.Authorization = `Bearer ${hf_token}`;
|
681
|
+
}
|
682
|
+
let root_url;
|
683
|
+
let component = config.components.find(
|
684
|
+
(comp) => comp.id === component_id
|
685
|
+
);
|
686
|
+
if ((_a = component == null ? void 0 : component.props) == null ? void 0 : _a.root_url) {
|
687
|
+
root_url = component.props.root_url;
|
688
|
+
} else {
|
689
|
+
root_url = `${http_protocol}//${host + config.path}/`;
|
690
|
+
}
|
691
|
+
const response = await fetch_implementation(
|
692
|
+
`${root_url}component_server/`,
|
693
|
+
{
|
694
|
+
method: "POST",
|
695
|
+
body: JSON.stringify({
|
696
|
+
data,
|
697
|
+
component_id,
|
698
|
+
fn_name,
|
699
|
+
session_hash
|
700
|
+
}),
|
701
|
+
headers
|
702
|
+
}
|
703
|
+
);
|
704
|
+
if (!response.ok) {
|
705
|
+
throw new Error(
|
706
|
+
"Could not connect to component server: " + response.statusText
|
707
|
+
);
|
708
|
+
}
|
709
|
+
const output = await response.json();
|
710
|
+
return output;
|
711
|
+
}
|
658
712
|
async function view_api(config2) {
|
659
713
|
if (api)
|
660
714
|
return api;
|
@@ -731,7 +785,10 @@ function api_factory(fetch_implementation) {
|
|
731
785
|
});
|
732
786
|
}
|
733
787
|
}
|
734
|
-
const { post_data, upload_files, client, handle_blob } = api_factory(
|
788
|
+
const { post_data, upload_files, client, handle_blob } = api_factory(
|
789
|
+
fetch,
|
790
|
+
(...args) => new WebSocket(...args)
|
791
|
+
);
|
735
792
|
function transform_output(data, api_info, root_url, remote_url) {
|
736
793
|
return data.map((d, i) => {
|
737
794
|
var _a, _b, _c, _d;
|
@@ -943,7 +1000,7 @@ async function resolve_config(fetch_implementation, endpoint, token) {
|
|
943
1000
|
if (typeof window !== "undefined" && window.gradio_config && location.origin !== "http://localhost:9876" && !window.gradio_config.dev_mode) {
|
944
1001
|
const path = window.gradio_config.root;
|
945
1002
|
const config = window.gradio_config;
|
946
|
-
config.root = endpoint
|
1003
|
+
config.root = resolve_root(endpoint, config.root, false);
|
947
1004
|
return { ...config, path };
|
948
1005
|
} else if (endpoint) {
|
949
1006
|
let response = await fetch_implementation(`${endpoint}/config`, {
|
package/dist/utils.d.ts
CHANGED
@@ -1,4 +1,15 @@
|
|
1
1
|
import type { Config } from "./types.js";
|
2
|
+
/**
|
3
|
+
* This function is used to resolve the URL for making requests when the app has a root path.
|
4
|
+
* The root path could be a path suffix like "/app" which is appended to the end of the base URL. Or
|
5
|
+
* it could be a full URL like "https://abidlabs-test-client-replica--gqf2x.hf.space" which is used when hosting
|
6
|
+
* Gradio apps on Hugging Face Spaces.
|
7
|
+
* @param {string} base_url The base URL at which the Gradio server is hosted
|
8
|
+
* @param {string} root_path The root path, which could be a path suffix (e.g. mounted in FastAPI app) or a full URL (e.g. hosted on Hugging Face Spaces)
|
9
|
+
* @param {boolean} prioritize_base Whether to prioritize the base URL over the root path. This is used when both the base path and root paths are full URLs. For example, for fetching files the root path should be prioritized, but for making request, the base URL should be prioritized.
|
10
|
+
* @returns {string} the resolved URL
|
11
|
+
*/
|
12
|
+
export declare function resolve_root(base_url: string, root_path: string, prioritize_base: boolean): string;
|
2
13
|
export declare function determine_protocol(endpoint: string): {
|
3
14
|
ws_protocol: "ws" | "wss";
|
4
15
|
http_protocol: "http:" | "https:";
|
package/dist/utils.d.ts.map
CHANGED
@@ -1 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AQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,CAqB1C;AAED,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,EAC7C,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,CAuB1C;AAED,wBAAsB,iBAAiB,CACtC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,MAAM,CAAC,CAuBjB;AAED,eAAO,MAAM,cAAc,0GAQjB,CAAC"}
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
@@ -8,7 +8,8 @@ import {
|
|
8
8
|
get_space_hardware,
|
9
9
|
set_space_hardware,
|
10
10
|
set_space_timeout,
|
11
|
-
hardware_types
|
11
|
+
hardware_types,
|
12
|
+
resolve_root
|
12
13
|
} from "./utils.js";
|
13
14
|
|
14
15
|
import type {
|
@@ -45,6 +46,11 @@ type client_return = {
|
|
45
46
|
data?: unknown[],
|
46
47
|
event_data?: unknown
|
47
48
|
) => SubmitReturn;
|
49
|
+
component_server: (
|
50
|
+
component_id: number,
|
51
|
+
fn_name: string,
|
52
|
+
data: unknown[]
|
53
|
+
) => any;
|
48
54
|
view_api: (c?: Config) => Promise<ApiInfo<JsApiData>>;
|
49
55
|
};
|
50
56
|
|
@@ -165,7 +171,10 @@ interface Client {
|
|
165
171
|
) => Promise<unknown[]>;
|
166
172
|
}
|
167
173
|
|
168
|
-
export function api_factory(
|
174
|
+
export function api_factory(
|
175
|
+
fetch_implementation: typeof fetch,
|
176
|
+
WebSocket_factory: (url: URL) => WebSocket
|
177
|
+
): Client {
|
169
178
|
return { post_data, upload_files, client, handle_blob };
|
170
179
|
|
171
180
|
async function post_data(
|
@@ -240,7 +249,8 @@ export function api_factory(fetch_implementation: typeof fetch): Client {
|
|
240
249
|
const return_obj = {
|
241
250
|
predict,
|
242
251
|
submit,
|
243
|
-
view_api
|
252
|
+
view_api,
|
253
|
+
component_server
|
244
254
|
// duplicate
|
245
255
|
};
|
246
256
|
|
@@ -419,12 +429,15 @@ export function api_factory(fetch_implementation: typeof fetch): Client {
|
|
419
429
|
let payload: Payload;
|
420
430
|
let complete: false | Record<string, any> = false;
|
421
431
|
const listener_map: ListenerMap<EventType> = {};
|
422
|
-
|
423
|
-
|
424
|
-
|
432
|
+
let url_params = ""
|
433
|
+
if (typeof(window) !== "undefined") {
|
434
|
+
url_params = new URLSearchParams(
|
435
|
+
window.location.search
|
436
|
+
).toString();
|
437
|
+
}
|
425
438
|
|
426
439
|
handle_blob(
|
427
|
-
`${http_protocol}//${host
|
440
|
+
`${http_protocol}//${resolve_root(host, config.path, true)}`,
|
428
441
|
data,
|
429
442
|
api_info,
|
430
443
|
hf_token
|
@@ -441,7 +454,7 @@ export function api_factory(fetch_implementation: typeof fetch): Client {
|
|
441
454
|
});
|
442
455
|
|
443
456
|
post_data(
|
444
|
-
`${http_protocol}//${host
|
457
|
+
`${http_protocol}//${resolve_root(host, config.path, true)}/run${
|
445
458
|
_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`
|
446
459
|
}${url_params ? "?" + url_params : ""}`,
|
447
460
|
{
|
@@ -509,15 +522,18 @@ export function api_factory(fetch_implementation: typeof fetch): Client {
|
|
509
522
|
fn_index,
|
510
523
|
time: new Date()
|
511
524
|
});
|
512
|
-
|
513
|
-
|
525
|
+
let url = new URL(`${ws_protocol}://${resolve_root(
|
526
|
+
host,
|
527
|
+
config.path,
|
528
|
+
true
|
529
|
+
)}
|
514
530
|
/queue/join${url_params ? "?" + url_params : ""}`);
|
515
531
|
|
516
532
|
if (jwt) {
|
517
533
|
url.searchParams.set("__sign", jwt);
|
518
534
|
}
|
519
535
|
|
520
|
-
websocket =
|
536
|
+
websocket = WebSocket_factory(url);
|
521
537
|
|
522
538
|
websocket.onclose = (evt) => {
|
523
539
|
if (!evt.wasClean) {
|
@@ -674,7 +690,11 @@ export function api_factory(fetch_implementation: typeof fetch): Client {
|
|
674
690
|
|
675
691
|
try {
|
676
692
|
await fetch_implementation(
|
677
|
-
`${http_protocol}//${
|
693
|
+
`${http_protocol}//${resolve_root(
|
694
|
+
host,
|
695
|
+
config.path,
|
696
|
+
true
|
697
|
+
)}/reset`,
|
678
698
|
{
|
679
699
|
headers: { "Content-Type": "application/json" },
|
680
700
|
method: "POST",
|
@@ -704,6 +724,51 @@ export function api_factory(fetch_implementation: typeof fetch): Client {
|
|
704
724
|
};
|
705
725
|
}
|
706
726
|
|
727
|
+
async function component_server(
|
728
|
+
component_id: number,
|
729
|
+
fn_name: string,
|
730
|
+
data: unknown[]
|
731
|
+
): Promise<any> {
|
732
|
+
const headers: {
|
733
|
+
Authorization?: string;
|
734
|
+
"Content-Type": "application/json";
|
735
|
+
} = { "Content-Type": "application/json" };
|
736
|
+
if (hf_token) {
|
737
|
+
headers.Authorization = `Bearer ${hf_token}`;
|
738
|
+
}
|
739
|
+
let root_url: string;
|
740
|
+
let component = config.components.find(
|
741
|
+
(comp) => comp.id === component_id
|
742
|
+
);
|
743
|
+
if (component?.props?.root_url) {
|
744
|
+
root_url = component.props.root_url;
|
745
|
+
} else {
|
746
|
+
root_url = `${http_protocol}//${host + config.path}/`;
|
747
|
+
}
|
748
|
+
const response = await fetch_implementation(
|
749
|
+
`${root_url}component_server/`,
|
750
|
+
{
|
751
|
+
method: "POST",
|
752
|
+
body: JSON.stringify({
|
753
|
+
data: data,
|
754
|
+
component_id: component_id,
|
755
|
+
fn_name: fn_name,
|
756
|
+
session_hash: session_hash
|
757
|
+
}),
|
758
|
+
headers
|
759
|
+
}
|
760
|
+
);
|
761
|
+
|
762
|
+
if (!response.ok) {
|
763
|
+
throw new Error(
|
764
|
+
"Could not connect to component server: " + response.statusText
|
765
|
+
);
|
766
|
+
}
|
767
|
+
|
768
|
+
const output = await response.json();
|
769
|
+
return output;
|
770
|
+
}
|
771
|
+
|
707
772
|
async function view_api(config?: Config): Promise<ApiInfo<JsApiData>> {
|
708
773
|
if (api) return api;
|
709
774
|
|
@@ -803,8 +868,10 @@ export function api_factory(fetch_implementation: typeof fetch): Client {
|
|
803
868
|
}
|
804
869
|
}
|
805
870
|
|
806
|
-
export const { post_data, upload_files, client, handle_blob } =
|
807
|
-
|
871
|
+
export const { post_data, upload_files, client, handle_blob } = api_factory(
|
872
|
+
fetch,
|
873
|
+
(...args) => new WebSocket(...args)
|
874
|
+
);
|
808
875
|
|
809
876
|
function transform_output(
|
810
877
|
data: any[],
|
@@ -1147,7 +1214,7 @@ async function resolve_config(
|
|
1147
1214
|
) {
|
1148
1215
|
const path = window.gradio_config.root;
|
1149
1216
|
const config = window.gradio_config;
|
1150
|
-
config.root = endpoint
|
1217
|
+
config.root = resolve_root(endpoint, config.root, false);
|
1151
1218
|
return { ...config, path: path };
|
1152
1219
|
} else if (endpoint) {
|
1153
1220
|
let response = await fetch_implementation(`${endpoint}/config`, {
|
package/src/utils.ts
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
import type { Config } from "./types.js";
|
2
2
|
|
3
|
+
/**
|
4
|
+
* This function is used to resolve the URL for making requests when the app has a root path.
|
5
|
+
* The root path could be a path suffix like "/app" which is appended to the end of the base URL. Or
|
6
|
+
* it could be a full URL like "https://abidlabs-test-client-replica--gqf2x.hf.space" which is used when hosting
|
7
|
+
* Gradio apps on Hugging Face Spaces.
|
8
|
+
* @param {string} base_url The base URL at which the Gradio server is hosted
|
9
|
+
* @param {string} root_path The root path, which could be a path suffix (e.g. mounted in FastAPI app) or a full URL (e.g. hosted on Hugging Face Spaces)
|
10
|
+
* @param {boolean} prioritize_base Whether to prioritize the base URL over the root path. This is used when both the base path and root paths are full URLs. For example, for fetching files the root path should be prioritized, but for making request, the base URL should be prioritized.
|
11
|
+
* @returns {string} the resolved URL
|
12
|
+
*/
|
13
|
+
export function resolve_root(
|
14
|
+
base_url: string,
|
15
|
+
root_path: string,
|
16
|
+
prioritize_base: boolean
|
17
|
+
): string {
|
18
|
+
if (root_path.startsWith("http://") || root_path.startsWith("https://")) {
|
19
|
+
return prioritize_base ? base_url : root_path;
|
20
|
+
}
|
21
|
+
return base_url + root_path;
|
22
|
+
}
|
23
|
+
|
3
24
|
export function determine_protocol(endpoint: string): {
|
4
25
|
ws_protocol: "ws" | "wss";
|
5
26
|
http_protocol: "http:" | "https:";
|