@gradio/client 0.17.0 → 0.19.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 (69) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +10 -6
  3. package/dist/client.d.ts +18 -7
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/constants.d.ts +8 -2
  6. package/dist/constants.d.ts.map +1 -1
  7. package/dist/helpers/api_info.d.ts +22 -0
  8. package/dist/helpers/api_info.d.ts.map +1 -1
  9. package/dist/helpers/data.d.ts +2 -2
  10. package/dist/helpers/data.d.ts.map +1 -1
  11. package/dist/helpers/init_helpers.d.ts.map +1 -1
  12. package/dist/helpers/spaces.d.ts.map +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +275 -183
  16. package/dist/test/handlers.d.ts +3 -0
  17. package/dist/test/handlers.d.ts.map +1 -0
  18. package/dist/test/mock_eventsource.d.ts +2 -0
  19. package/dist/test/mock_eventsource.d.ts.map +1 -0
  20. package/dist/test/server.d.ts +2 -0
  21. package/dist/test/server.d.ts.map +1 -0
  22. package/dist/test/test_data.d.ts +76 -0
  23. package/dist/test/test_data.d.ts.map +1 -0
  24. package/dist/types.d.ts +23 -7
  25. package/dist/types.d.ts.map +1 -1
  26. package/dist/upload.d.ts +2 -2
  27. package/dist/upload.d.ts.map +1 -1
  28. package/dist/utils/duplicate.d.ts.map +1 -1
  29. package/dist/utils/predict.d.ts +1 -1
  30. package/dist/utils/predict.d.ts.map +1 -1
  31. package/dist/utils/stream.d.ts +2 -2
  32. package/dist/utils/stream.d.ts.map +1 -1
  33. package/dist/utils/submit.d.ts +1 -1
  34. package/dist/utils/submit.d.ts.map +1 -1
  35. package/dist/utils/upload_files.d.ts.map +1 -1
  36. package/dist/utils/view_api.d.ts.map +1 -1
  37. package/package.json +8 -2
  38. package/src/client.ts +53 -28
  39. package/src/constants.ts +9 -2
  40. package/src/helpers/api_info.ts +75 -4
  41. package/src/helpers/data.ts +46 -28
  42. package/src/helpers/init_helpers.ts +7 -11
  43. package/src/helpers/spaces.ts +8 -3
  44. package/src/index.ts +1 -1
  45. package/src/test/api_info.test.ts +567 -0
  46. package/src/test/data.test.ts +281 -0
  47. package/src/test/handlers.ts +438 -0
  48. package/src/test/init.test.ts +139 -0
  49. package/src/test/init_helpers.test.ts +94 -0
  50. package/src/test/mock_eventsource.ts +13 -0
  51. package/src/test/post_data.test.ts +45 -0
  52. package/src/test/server.ts +6 -0
  53. package/src/test/spaces.test.ts +145 -0
  54. package/src/test/stream.test.ts +79 -0
  55. package/src/test/test_data.ts +557 -0
  56. package/src/test/upload_files.test.ts +42 -0
  57. package/src/test/view_api.test.ts +53 -0
  58. package/src/types.ts +36 -17
  59. package/src/upload.ts +4 -8
  60. package/src/utils/duplicate.ts +20 -3
  61. package/src/utils/handle_blob.ts +1 -1
  62. package/src/utils/post_data.ts +1 -1
  63. package/src/utils/predict.ts +2 -2
  64. package/src/utils/stream.ts +30 -21
  65. package/src/utils/submit.ts +42 -19
  66. package/src/utils/upload_files.ts +11 -6
  67. package/src/utils/view_api.ts +4 -7
  68. package/vite.config.js +7 -0
  69. package/src/utils/client.node-test.ts +0 -173
@@ -5,7 +5,7 @@ import type {
5
5
  Config,
6
6
  EndpointInfo,
7
7
  JsApiData,
8
- ParamType
8
+ DataType
9
9
  } from "../types";
10
10
 
11
11
  export function update_object(
@@ -31,22 +31,22 @@ export function update_object(
31
31
  }
32
32
 
33
33
  export async function walk_and_store_blobs(
34
- param: ParamType,
34
+ data: DataType,
35
35
  type: string | undefined = undefined,
36
36
  path: string[] = [],
37
37
  root = false,
38
38
  endpoint_info: EndpointInfo<ApiData | JsApiData> | undefined = undefined
39
39
  ): Promise<BlobRef[]> {
40
- if (Array.isArray(param)) {
40
+ if (Array.isArray(data)) {
41
41
  let blob_refs: BlobRef[] = [];
42
42
 
43
43
  await Promise.all(
44
- param.map(async (item) => {
44
+ data.map(async (item) => {
45
45
  let new_path = path.slice();
46
46
  new_path.push(item);
47
47
 
48
48
  const array_refs = await walk_and_store_blobs(
49
- param[item],
49
+ data[item],
50
50
  root ? endpoint_info?.parameters[item]?.component || undefined : type,
51
51
  new_path,
52
52
  false,
@@ -58,44 +58,62 @@ export async function walk_and_store_blobs(
58
58
  );
59
59
 
60
60
  return blob_refs;
61
- } else if (globalThis.Buffer && param instanceof globalThis.Buffer) {
61
+ } else if (
62
+ (globalThis.Buffer && data instanceof globalThis.Buffer) ||
63
+ data instanceof Blob
64
+ ) {
62
65
  const is_image = type === "Image";
63
66
  return [
64
67
  {
65
68
  path: path,
66
- blob: is_image ? false : new NodeBlob([param]),
69
+ blob: is_image ? false : new NodeBlob([data]),
67
70
  type
68
71
  }
69
72
  ];
70
- } else if (typeof param === "object") {
73
+ } else if (typeof data === "object" && data !== null) {
71
74
  let blob_refs: BlobRef[] = [];
72
- for (let key in param) {
73
- if (param.hasOwnProperty(key)) {
74
- let new_path = path.slice();
75
- new_path.push(key);
76
- blob_refs = blob_refs.concat(
77
- await walk_and_store_blobs(
78
- // @ts-ignore
79
- param[key],
80
- undefined,
81
- new_path,
82
- false,
83
- endpoint_info
84
- )
85
- );
86
- }
75
+ for (const key of Object.keys(data) as (keyof typeof data)[]) {
76
+ const new_path = [...path, key];
77
+ const value = data[key];
78
+
79
+ blob_refs = blob_refs.concat(
80
+ await walk_and_store_blobs(
81
+ value,
82
+ undefined,
83
+ new_path,
84
+ false,
85
+ endpoint_info
86
+ )
87
+ );
88
+ }
89
+
90
+ if (
91
+ !blob_refs.length &&
92
+ !(
93
+ data instanceof Blob ||
94
+ data instanceof ArrayBuffer ||
95
+ data instanceof Uint8Array
96
+ )
97
+ ) {
98
+ return [
99
+ {
100
+ path: path,
101
+ blob: new NodeBlob([JSON.stringify(data)]),
102
+ type: typeof data
103
+ }
104
+ ];
87
105
  }
88
106
  return blob_refs;
89
107
  }
108
+
90
109
  return [];
91
110
  }
92
111
 
93
112
  export function skip_queue(id: number, config: Config): boolean {
94
- return (
95
- !(config?.dependencies?.[id]?.queue === null
96
- ? config.enable_queue
97
- : config?.dependencies?.[id]?.queue) || false
98
- );
113
+ if (config?.dependencies?.[id]?.queue !== null) {
114
+ return !config.dependencies[id].queue;
115
+ }
116
+ return !config.enable_queue;
99
117
  }
100
118
 
101
119
  // todo: add jsdoc for this function
@@ -1,5 +1,5 @@
1
1
  import type { Config } from "../types";
2
- import { CONFIG_URL } from "../constants";
2
+ import { CONFIG_ERROR_MSG, CONFIG_URL } from "../constants";
3
3
  import { Client } from "..";
4
4
 
5
5
  /**
@@ -38,7 +38,6 @@ export async function get_jwt(
38
38
 
39
39
  return jwt || false;
40
40
  } catch (e) {
41
- console.error(e);
42
41
  return false;
43
42
  }
44
43
  }
@@ -74,14 +73,11 @@ export async function resolve_config(
74
73
  const config = window.gradio_config;
75
74
  let config_root = resolve_root(endpoint, config.root, false);
76
75
  config.root = config_root;
77
- return { ...config, path };
76
+ return { ...config, path } as Config;
78
77
  } else if (endpoint) {
79
- const response = await this.fetch_implementation(
80
- `${endpoint}/${CONFIG_URL}`,
81
- {
82
- headers
83
- }
84
- );
78
+ const response = await this.fetch(`${endpoint}/${CONFIG_URL}`, {
79
+ headers
80
+ });
85
81
 
86
82
  if (response?.status === 200) {
87
83
  let config = await response.json();
@@ -89,10 +85,10 @@ export async function resolve_config(
89
85
  config.root = endpoint;
90
86
  return config;
91
87
  }
92
- throw new Error("Could not get config.");
88
+ throw new Error(CONFIG_ERROR_MSG);
93
89
  }
94
90
 
95
- throw new Error("No config or app endpoint found");
91
+ throw new Error(CONFIG_ERROR_MSG);
96
92
  }
97
93
 
98
94
  export function determine_protocol(endpoint: string): {
@@ -1,3 +1,8 @@
1
+ import {
2
+ RUNTIME_URL,
3
+ SLEEPTIME_URL,
4
+ SPACE_STATUS_ERROR_MSG
5
+ } from "../constants";
1
6
  import type { SpaceStatusCallback } from "../types";
2
7
 
3
8
  export async function check_space_status(
@@ -22,7 +27,7 @@ export async function check_space_status(
22
27
  status_callback({
23
28
  status: "error",
24
29
  load_status: "error",
25
- message: "Could not get space status",
30
+ message: SPACE_STATUS_ERROR_MSG,
26
31
  detail: "NOT_FOUND"
27
32
  });
28
33
  return;
@@ -121,7 +126,7 @@ export async function get_space_hardware(
121
126
 
122
127
  try {
123
128
  const res = await fetch(
124
- `https://huggingface.co/api/spaces/${space_id}/runtime`,
129
+ `https://huggingface.co/api/spaces/${space_id}/${RUNTIME_URL}`,
125
130
  { headers }
126
131
  );
127
132
 
@@ -154,7 +159,7 @@ export async function set_space_timeout(
154
159
 
155
160
  try {
156
161
  const res = await fetch(
157
- `https://huggingface.co/api/spaces/${space_id}/sleeptime`,
162
+ `https://huggingface.co/api/spaces/${space_id}/${SLEEPTIME_URL}`,
158
163
  {
159
164
  method: "POST",
160
165
  headers: { "Content-Type": "application/json", ...headers },
package/src/index.ts CHANGED
@@ -14,4 +14,4 @@ export type {
14
14
 
15
15
  // todo: remove in @gradio/client v1.0
16
16
  export { client } from "./client";
17
- export { duplicate } from "./utils/duplicate";
17
+ export { duplicate_space as duplicate } from "./client";