@gradio/client 2.3.0 → 2.4.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +4 -2
  3. package/dist/__vite-browser-external-Cgmn0awE.js +7 -0
  4. package/dist/browser.js +1672 -1902
  5. package/dist/client.d.ts +5 -0
  6. package/dist/client.d.ts.map +1 -1
  7. package/dist/constants.d.ts +4 -0
  8. package/dist/constants.d.ts.map +1 -1
  9. package/dist/helpers/api_info.d.ts.map +1 -1
  10. package/dist/helpers/data.d.ts.map +1 -1
  11. package/dist/helpers/init_helpers.d.ts +12 -3
  12. package/dist/helpers/init_helpers.d.ts.map +1 -1
  13. package/dist/index.cjs +2274 -0
  14. package/dist/index.js +2155 -2449
  15. package/dist/index.min.js +1735 -0
  16. package/dist/types.d.ts +16 -0
  17. package/dist/types.d.ts.map +1 -1
  18. package/dist/utils/duplicate.d.ts.map +1 -1
  19. package/dist/utils/predict.d.ts.map +1 -1
  20. package/dist/utils/stream.d.ts.map +1 -1
  21. package/dist/utils/submit.d.ts.map +1 -1
  22. package/dist/utils/view_api.d.ts.map +1 -1
  23. package/package.json +11 -4
  24. package/scripts/verify_dist.mjs +46 -0
  25. package/src/client.ts +46 -7
  26. package/src/constants.ts +10 -0
  27. package/src/helpers/api_info.ts +47 -19
  28. package/src/helpers/data.ts +8 -3
  29. package/src/helpers/init_helpers.ts +61 -14
  30. package/src/test/api_info.test.ts +105 -12
  31. package/src/test/data.test.ts +26 -3
  32. package/src/test/init.test.ts +21 -3
  33. package/src/test/init_helpers.test.ts +94 -1
  34. package/src/test/post_data.test.ts +21 -0
  35. package/src/test/refresh.test.ts +31 -0
  36. package/src/test/submit.test.ts +14 -0
  37. package/src/types.ts +16 -0
  38. package/src/utils/duplicate.ts +5 -1
  39. package/src/utils/post_data.ts +1 -1
  40. package/src/utils/predict.ts +34 -19
  41. package/src/utils/stream.ts +17 -4
  42. package/src/utils/submit.ts +43 -10
  43. package/src/utils/upload_files.ts +1 -1
  44. package/src/utils/view_api.ts +2 -3
  45. package/vite.config.ts +20 -4
  46. package/dist/__vite-browser-external-DYxpcVy9.js +0 -4
@@ -19,11 +19,13 @@ import {
19
19
  } from "../helpers/api_info";
20
20
  import {
21
21
  BROKEN_CONNECTION_MSG,
22
+ NO_API_INFO_MSG,
22
23
  QUEUE_FULL_MSG,
23
24
  SSE_URL,
24
25
  SSE_DATA_URL,
25
26
  RESET_URL,
26
- CANCEL_URL
27
+ CANCEL_URL,
28
+ WS_PROTOCOL_MSG
27
29
  } from "../constants";
28
30
  import { apply_diff_stream, close_stream } from "./stream";
29
31
  import { Client } from "../client";
@@ -60,7 +62,7 @@ export function submit(
60
62
 
61
63
  const that = this;
62
64
 
63
- if (!api_info) throw new Error("No API found");
65
+ if (!api_info) throw new Error(NO_API_INFO_MSG);
64
66
  if (!config) throw new Error("Could not resolve app config");
65
67
 
66
68
  let { fn_index, endpoint_info, dependency } = get_endpoint_info(
@@ -75,7 +77,7 @@ export function submit(
75
77
  let stream: EventSource | null;
76
78
  let protocol = config.protocol ?? "ws";
77
79
  if (protocol === "ws") {
78
- throw new Error("WebSocket protocol is not supported in this version");
80
+ throw new Error(WS_PROTOCOL_MSG);
79
81
  }
80
82
  let event_id_final = "";
81
83
  let event_id_cb: () => string = () => event_id_final;
@@ -182,7 +184,10 @@ export function submit(
182
184
  data: input_data || [],
183
185
  event_data,
184
186
  fn_index,
185
- trigger_id
187
+ trigger_id,
188
+ ...(options.oauth_token && endpoint_info?.oauth_token
189
+ ? { oauth_token: options.oauth_token }
190
+ : {})
186
191
  };
187
192
  if (skip_queue(fn_index, config)) {
188
193
  fire_event({
@@ -630,6 +635,22 @@ export function submit(
630
635
  }
631
636
  });
632
637
 
638
+ // Surface internal failures (e.g. malformed payloads or configs) as an
639
+ // error event instead of leaving the returned iterator hanging forever
640
+ // with an unhandled promise rejection.
641
+ job.catch((e) => {
642
+ fire_event({
643
+ type: "status",
644
+ stage: "error",
645
+ message: e instanceof Error ? e.message : String(e),
646
+ queue: !skip_queue(fn_index, config),
647
+ endpoint: _endpoint,
648
+ fn_index,
649
+ time: new Date()
650
+ });
651
+ close();
652
+ });
653
+
633
654
  let done = false;
634
655
  const values: (IteratorResult<GradioEvent> | PromiseLike<never>)[] = [];
635
656
  const resolvers: ((
@@ -735,25 +756,37 @@ function get_endpoint_info(
735
756
  } {
736
757
  let fn_index: number;
737
758
  let endpoint_info: EndpointInfo<JsApiData>;
738
- let dependency: Dependency;
759
+ let dependency: Dependency | undefined;
739
760
 
740
761
  if (typeof endpoint === "number") {
741
762
  fn_index = endpoint;
742
763
  endpoint_info = api_info.unnamed_endpoints[fn_index];
743
- dependency = config.dependencies.find((dep) => dep.id == endpoint)!;
764
+ dependency = config.dependencies.find((dep) => dep.id == endpoint);
744
765
  } else {
745
766
  const trimmed_endpoint = endpoint.replace(/^\//, "");
746
767
 
747
768
  fn_index = api_map[trimmed_endpoint];
748
- endpoint_info = api_info.named_endpoints[endpoint.trim()];
769
+ // named endpoints are keyed with a leading slash in the API info, but
770
+ // accept endpoint names passed without one (e.g. "predict")
771
+ endpoint_info =
772
+ api_info.named_endpoints[endpoint.trim()] ??
773
+ api_info.named_endpoints[`/${trimmed_endpoint}`];
749
774
  dependency = config.dependencies.find(
750
775
  (dep) => dep.id == api_map[trimmed_endpoint]
751
- )!;
776
+ );
752
777
  }
753
778
 
754
- if (typeof fn_index !== "number") {
779
+ if (typeof fn_index !== "number" || !dependency) {
780
+ const valid_endpoints = config.dependencies
781
+ .filter((dep) => dep.api_name)
782
+ .map((dep) => `"/${dep.api_name}"`)
783
+ .join(", ");
755
784
  throw new Error(
756
- "There is no endpoint matching that name of fn_index matching that number."
785
+ `No endpoint matching ${JSON.stringify(endpoint)} was found. ` +
786
+ (valid_endpoints
787
+ ? `Valid named endpoints are: ${valid_endpoints}. `
788
+ : "This app exposes no named endpoints. ") +
789
+ "An fn_index (number) of an existing dependency can also be used."
757
790
  );
758
791
  }
759
792
  return { fn_index, endpoint_info, dependency };
@@ -34,7 +34,7 @@ export async function upload_files(
34
34
  method: "POST",
35
35
  body: formData,
36
36
  headers,
37
- credentials: "include"
37
+ credentials: this.options.credentials ?? "same-origin"
38
38
  });
39
39
  } catch (e) {
40
40
  throw new Error(BROKEN_CONNECTION_MSG + (e as Error).message);
@@ -12,8 +12,7 @@ export async function view_api(this: Client): Promise<any> {
12
12
 
13
13
  const headers: {
14
14
  Authorization?: string;
15
- "Content-Type": "application/json";
16
- } = { "Content-Type": "application/json" };
15
+ } = {};
17
16
 
18
17
  if (token) {
19
18
  headers.Authorization = `Bearer ${token}`;
@@ -32,7 +31,7 @@ export async function view_api(this: Client): Promise<any> {
32
31
  const url = join_urls(config.root, this.api_prefix, API_INFO_URL);
33
32
  response = await this.fetch(url, {
34
33
  headers,
35
- credentials: "include"
34
+ credentials: this.options.credentials ?? "same-origin"
36
35
  });
37
36
  if (!response.ok) {
38
37
  throw new Error(BROKEN_CONNECTION_MSG);
package/vite.config.ts CHANGED
@@ -38,7 +38,12 @@ logger.info = (log, options) => {
38
38
  };
39
39
  export default defineConfig(({ mode }) => {
40
40
  const production = mode === "production";
41
- const isBrowserBuild = process.env.BROWSER_BUILD === "true";
41
+ // Single-file, minified browser bundle served from CDNs at
42
+ // dist/index.min.js (see https://github.com/gradio-app/gradio/issues/10028)
43
+ const isCdnBuild = process.env.CDN_BUILD === "true";
44
+ // a CDN build is always a browser build (guards against CDN_BUILD=true
45
+ // without BROWSER_BUILD=true emitting es+cjs into the same file name)
46
+ const isBrowserBuild = process.env.BROWSER_BUILD === "true" || isCdnBuild;
42
47
 
43
48
  if (mode === "preview") {
44
49
  return {
@@ -50,15 +55,26 @@ export default defineConfig(({ mode }) => {
50
55
  customLogger: logger,
51
56
  build: {
52
57
  emptyOutDir: false,
58
+ ...(isCdnBuild ? { minify: true } : {}),
53
59
  lib: {
54
60
  entry: "src/index.ts",
55
- formats: ["es"],
56
- fileName: isBrowserBuild ? `browser` : `index`
61
+ // the node build also emits a CommonJS bundle (dist/index.cjs)
62
+ // so that require("@gradio/client") works on all supported
63
+ // node versions (see https://github.com/gradio-app/gradio/issues/9214)
64
+ formats: isBrowserBuild ? ["es"] : ["es", "cjs"],
65
+ fileName: (format) => {
66
+ if (isCdnBuild) return "index.min.js";
67
+ if (isBrowserBuild) return "browser.js";
68
+ return format === "cjs" ? "index.cjs" : "index.js";
69
+ }
57
70
  },
58
71
  rollupOptions: {
59
72
  input: "src/index.ts",
60
73
  output: {
61
- dir: "dist"
74
+ dir: "dist",
75
+ // keep the CDN bundle as a single file so the documented
76
+ // <script type="module"> import works without extra chunks
77
+ inlineDynamicImports: isCdnBuild
62
78
  }
63
79
  }
64
80
  },
@@ -1,4 +0,0 @@
1
- const e = {};
2
- export {
3
- e as default
4
- };