@gradio/client 0.19.3 → 0.20.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 (41) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +8 -1
  3. package/dist/client.d.ts +4 -0
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/constants.d.ts +4 -0
  6. package/dist/constants.d.ts.map +1 -1
  7. package/dist/helpers/api_info.d.ts +1 -0
  8. package/dist/helpers/api_info.d.ts.map +1 -1
  9. package/dist/helpers/data.d.ts.map +1 -1
  10. package/dist/helpers/init_helpers.d.ts +4 -1
  11. package/dist/helpers/init_helpers.d.ts.map +1 -1
  12. package/dist/index.js +236 -75
  13. package/dist/test/handlers.d.ts.map +1 -1
  14. package/dist/test/test_data.d.ts.map +1 -1
  15. package/dist/types.d.ts +6 -0
  16. package/dist/types.d.ts.map +1 -1
  17. package/dist/utils/duplicate.d.ts.map +1 -1
  18. package/dist/utils/post_data.d.ts.map +1 -1
  19. package/dist/utils/predict.d.ts.map +1 -1
  20. package/dist/utils/submit.d.ts.map +1 -1
  21. package/dist/utils/upload_files.d.ts.map +1 -1
  22. package/dist/utils/view_api.d.ts.map +1 -1
  23. package/package.json +1 -1
  24. package/src/client.ts +70 -28
  25. package/src/constants.ts +5 -0
  26. package/src/helpers/api_info.ts +44 -17
  27. package/src/helpers/data.ts +9 -22
  28. package/src/helpers/init_helpers.ts +98 -9
  29. package/src/test/api_info.test.ts +69 -4
  30. package/src/test/data.test.ts +13 -16
  31. package/src/test/handlers.ts +249 -2
  32. package/src/test/init.test.ts +2 -2
  33. package/src/test/init_helpers.test.ts +53 -1
  34. package/src/test/test_data.ts +3 -0
  35. package/src/types.ts +6 -0
  36. package/src/utils/duplicate.ts +27 -2
  37. package/src/utils/post_data.ts +2 -1
  38. package/src/utils/predict.ts +4 -2
  39. package/src/utils/submit.ts +42 -9
  40. package/src/utils/upload_files.ts +2 -1
  41. package/src/utils/view_api.ts +7 -4
package/dist/index.js CHANGED
@@ -10,7 +10,9 @@ function semiver(a, b, bool) {
10
10
  b = b.split(".");
11
11
  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);
12
12
  }
13
+ const HOST_URL = "host";
13
14
  const UPLOAD_URL = "upload";
15
+ const LOGIN_URL = "login";
14
16
  const CONFIG_URL = "config";
15
17
  const API_INFO_URL = "info";
16
18
  const RUNTIME_URL = "runtime";
@@ -22,17 +24,22 @@ const CONFIG_ERROR_MSG = "Could not resolve app config. ";
22
24
  const SPACE_STATUS_ERROR_MSG = "Could not get space status. ";
23
25
  const API_INFO_ERROR_MSG = "Could not get API info. ";
24
26
  const SPACE_METADATA_ERROR_MSG = "Space metadata could not be loaded. ";
27
+ const INVALID_URL_MSG = "Invalid URL. A full URL path is required.";
28
+ const UNAUTHORIZED_MSG = "Not authorized to access this space. ";
29
+ const INVALID_CREDENTIALS_MSG = "Invalid credentials. Could not login. ";
30
+ const MISSING_CREDENTIALS_MSG = "Login credentials are required to access this space.";
25
31
  function resolve_root(base_url, root_path, prioritize_base) {
26
32
  if (root_path.startsWith("http://") || root_path.startsWith("https://")) {
27
33
  return prioritize_base ? base_url : root_path;
28
34
  }
29
35
  return base_url + root_path;
30
36
  }
31
- async function get_jwt(space, token) {
37
+ async function get_jwt(space, token, cookies) {
32
38
  try {
33
39
  const r = await fetch(`https://huggingface.co/api/spaces/${space}/jwt`, {
34
40
  headers: {
35
- Authorization: `Bearer ${token}`
41
+ Authorization: `Bearer ${token}`,
42
+ ...cookies ? { Cookie: cookies } : {}
36
43
  }
37
44
  });
38
45
  const jwt = (await r.json()).token;
@@ -43,9 +50,9 @@ async function get_jwt(space, token) {
43
50
  }
44
51
  function map_names_to_ids(fns) {
45
52
  let apis = {};
46
- fns.forEach(({ api_name }, i) => {
53
+ fns.forEach(({ api_name, id }) => {
47
54
  if (api_name)
48
- apis[api_name] = i;
55
+ apis[api_name] = id;
49
56
  });
50
57
  return apis;
51
58
  }
@@ -59,22 +66,74 @@ async function resolve_config(endpoint) {
59
66
  config.root = config_root;
60
67
  return { ...config, path };
61
68
  } else if (endpoint) {
62
- const response = await this.fetch(`${endpoint}/${CONFIG_URL}`, {
63
- headers
69
+ const config_url = join_urls(endpoint, CONFIG_URL);
70
+ const response = await this.fetch(config_url, {
71
+ headers,
72
+ credentials: "include"
64
73
  });
74
+ if ((response == null ? void 0 : response.status) === 401 && !this.options.auth) {
75
+ throw new Error(MISSING_CREDENTIALS_MSG);
76
+ } else if ((response == null ? void 0 : response.status) === 401 && this.options.auth) {
77
+ throw new Error(INVALID_CREDENTIALS_MSG);
78
+ }
65
79
  if ((response == null ? void 0 : response.status) === 200) {
66
80
  let config = await response.json();
67
81
  config.path = config.path ?? "";
68
82
  config.root = endpoint;
69
83
  return config;
84
+ } else if ((response == null ? void 0 : response.status) === 401) {
85
+ throw new Error(UNAUTHORIZED_MSG);
70
86
  }
71
87
  throw new Error(CONFIG_ERROR_MSG);
72
88
  }
73
89
  throw new Error(CONFIG_ERROR_MSG);
74
90
  }
91
+ async function resolve_cookies() {
92
+ const { http_protocol, host } = await process_endpoint(
93
+ this.app_reference,
94
+ this.options.hf_token
95
+ );
96
+ try {
97
+ if (this.options.auth) {
98
+ const cookie_header = await get_cookie_header(
99
+ http_protocol,
100
+ host,
101
+ this.options.auth,
102
+ this.fetch,
103
+ this.options.hf_token
104
+ );
105
+ if (cookie_header)
106
+ this.set_cookies(cookie_header);
107
+ }
108
+ } catch (e) {
109
+ throw Error(e.message);
110
+ }
111
+ }
112
+ async function get_cookie_header(http_protocol, host, auth, _fetch, hf_token) {
113
+ const formData = new FormData();
114
+ formData.append("username", auth == null ? void 0 : auth[0]);
115
+ formData.append("password", auth == null ? void 0 : auth[1]);
116
+ let headers = {};
117
+ if (hf_token) {
118
+ headers.Authorization = `Bearer ${hf_token}`;
119
+ }
120
+ const res = await _fetch(`${http_protocol}//${host}/${LOGIN_URL}`, {
121
+ headers,
122
+ method: "POST",
123
+ body: formData,
124
+ credentials: "include"
125
+ });
126
+ if (res.status === 200) {
127
+ return res.headers.get("set-cookie");
128
+ } else if (res.status === 401) {
129
+ throw new Error(INVALID_CREDENTIALS_MSG);
130
+ } else {
131
+ throw new Error(SPACE_METADATA_ERROR_MSG);
132
+ }
133
+ }
75
134
  function determine_protocol(endpoint) {
76
135
  if (endpoint.startsWith("http")) {
77
- const { protocol, host } = new URL(endpoint);
136
+ const { protocol, host, pathname } = new URL(endpoint);
78
137
  if (host.endsWith("hf.space")) {
79
138
  return {
80
139
  ws_protocol: "wss",
@@ -85,7 +144,7 @@ function determine_protocol(endpoint) {
85
144
  return {
86
145
  ws_protocol: protocol === "https:" ? "wss" : "ws",
87
146
  http_protocol: protocol,
88
- host
147
+ host: host + (pathname !== "/" ? pathname : "")
89
148
  };
90
149
  } else if (endpoint.startsWith("file:")) {
91
150
  return {
@@ -101,18 +160,29 @@ function determine_protocol(endpoint) {
101
160
  host: endpoint
102
161
  };
103
162
  }
104
- const RE_SPACE_NAME = /^[^\/]*\/[^\/]*$/;
163
+ const parse_and_set_cookies = (cookie_header) => {
164
+ let cookies = [];
165
+ const parts = cookie_header.split(/,(?=\s*[^\s=;]+=[^\s=;]+)/);
166
+ parts.forEach((cookie) => {
167
+ const [cookie_name, cookie_value] = cookie.split(";")[0].split("=");
168
+ if (cookie_name && cookie_value) {
169
+ cookies.push(`${cookie_name.trim()}=${cookie_value.trim()}`);
170
+ }
171
+ });
172
+ return cookies;
173
+ };
174
+ const RE_SPACE_NAME = /^[a-zA-Z0-9_\-\.]+\/[a-zA-Z0-9_\-\.]+$/;
105
175
  const RE_SPACE_DOMAIN = /.*hf\.space\/{0,1}$/;
106
176
  async function process_endpoint(app_reference, hf_token) {
107
177
  const headers = {};
108
178
  if (hf_token) {
109
179
  headers.Authorization = `Bearer ${hf_token}`;
110
180
  }
111
- const _app_reference = app_reference.trim();
181
+ const _app_reference = app_reference.trim().replace(/\/$/, "");
112
182
  if (RE_SPACE_NAME.test(_app_reference)) {
113
183
  try {
114
184
  const res = await fetch(
115
- `https://huggingface.co/api/spaces/${_app_reference}/host`,
185
+ `https://huggingface.co/api/spaces/${_app_reference}/${HOST_URL}`,
116
186
  { headers }
117
187
  );
118
188
  const _host = (await res.json()).host;
@@ -121,9 +191,7 @@ async function process_endpoint(app_reference, hf_token) {
121
191
  ...determine_protocol(_host)
122
192
  };
123
193
  } catch (e) {
124
- throw new Error(
125
- "Space metadata could not be loaded. " + e.message
126
- );
194
+ throw new Error(SPACE_METADATA_ERROR_MSG);
127
195
  }
128
196
  }
129
197
  if (RE_SPACE_DOMAIN.test(_app_reference)) {
@@ -140,6 +208,17 @@ async function process_endpoint(app_reference, hf_token) {
140
208
  ...determine_protocol(_app_reference)
141
209
  };
142
210
  }
211
+ const join_urls = (...urls) => {
212
+ try {
213
+ return urls.reduce((base_url, part) => {
214
+ base_url = base_url.replace(/\/+$/, "");
215
+ part = part.replace(/^\/+/, "");
216
+ return new URL(part, base_url + "/").toString();
217
+ });
218
+ } catch (e) {
219
+ throw new Error(INVALID_URL_MSG);
220
+ }
221
+ };
143
222
  function transform_api_info(api_info, config, api_map) {
144
223
  const transformed_info = {
145
224
  named_endpoints: {},
@@ -150,13 +229,13 @@ function transform_api_info(api_info, config, api_map) {
150
229
  transformed_info[category] = {};
151
230
  Object.entries(api_info[category]).forEach(
152
231
  ([endpoint, { parameters, returns }]) => {
153
- var _a, _b;
154
- const dependencyIndex = config.dependencies.findIndex(
232
+ var _a, _b, _c, _d;
233
+ const dependencyIndex = ((_a = config.dependencies.find(
155
234
  (dep) => dep.api_name === endpoint || dep.api_name === endpoint.replace("/", "")
156
- ) || api_map[endpoint.replace("/", "")] || -1;
157
- const dependencyTypes = dependencyIndex !== -1 ? config.dependencies[dependencyIndex].types : { continuous: false, generator: false };
158
- if (dependencyIndex !== -1 && ((_b = (_a = config.dependencies[dependencyIndex]) == null ? void 0 : _a.inputs) == null ? void 0 : _b.length) !== parameters.length) {
159
- const components = config.dependencies[dependencyIndex].inputs.map(
235
+ )) == null ? void 0 : _a.id) || api_map[endpoint.replace("/", "")] || -1;
236
+ const dependencyTypes = dependencyIndex !== -1 ? (_b = config.dependencies.find((dep) => dep.id == dependencyIndex)) == null ? void 0 : _b.types : { continuous: false, generator: false };
237
+ if (dependencyIndex !== -1 && ((_d = (_c = config.dependencies.find((dep) => dep.id == dependencyIndex)) == null ? void 0 : _c.inputs) == null ? void 0 : _d.length) !== parameters.length) {
238
+ const components = config.dependencies.find((dep) => dep.id == dependencyIndex).inputs.map(
160
239
  (input) => {
161
240
  var _a2;
162
241
  return (_a2 = config.components.find((c) => c.id === input)) == null ? void 0 : _a2.type;
@@ -177,6 +256,7 @@ function transform_api_info(api_info, config, api_map) {
177
256
  }
178
257
  });
179
258
  } catch (e) {
259
+ console.error(e);
180
260
  }
181
261
  }
182
262
  const transform_type = (data, component, serializer, signature_type) => ({
@@ -324,7 +404,8 @@ function handle_message(data, last_status) {
324
404
  message: !data.success ? data.output.error : void 0,
325
405
  stage: data.success ? "complete" : "error",
326
406
  code: data.code,
327
- progress_data: data.progress_data
407
+ progress_data: data.progress_data,
408
+ changed_state_ids: data.success ? data.output.changed_state_ids : void 0
328
409
  },
329
410
  data: data.success ? data.output : null
330
411
  };
@@ -404,11 +485,14 @@ async function view_api() {
404
485
  serialize: false,
405
486
  config: JSON.stringify(config)
406
487
  }),
407
- headers
488
+ headers,
489
+ credentials: "include"
408
490
  });
409
491
  } else {
410
- response = await this.fetch(`${config == null ? void 0 : config.root}/${API_INFO_URL}`, {
411
- headers
492
+ const url = join_urls(config.root, API_INFO_URL);
493
+ response = await this.fetch(url, {
494
+ headers,
495
+ credentials: "include"
412
496
  });
413
497
  }
414
498
  if (!response.ok) {
@@ -446,7 +530,8 @@ async function upload_files(root_url, files, upload_id) {
446
530
  response = await this.fetch(upload_url, {
447
531
  method: "POST",
448
532
  body: formData,
449
- headers
533
+ headers,
534
+ credentials: "include"
450
535
  });
451
536
  } catch (e) {
452
537
  throw new Error(BROKEN_CONNECTION_MSG + e.message);
@@ -558,13 +643,13 @@ async function walk_and_store_blobs(data, type = void 0, path = [], root = false
558
643
  if (Array.isArray(data)) {
559
644
  let blob_refs = [];
560
645
  await Promise.all(
561
- data.map(async (item) => {
646
+ data.map(async (_, index) => {
562
647
  var _a;
563
648
  let new_path = path.slice();
564
- new_path.push(item);
649
+ new_path.push(String(index));
565
650
  const array_refs = await walk_and_store_blobs(
566
- data[item],
567
- root ? ((_a = endpoint_info == null ? void 0 : endpoint_info.parameters[item]) == null ? void 0 : _a.component) || void 0 : type,
651
+ data[index],
652
+ root ? ((_a = endpoint_info == null ? void 0 : endpoint_info.parameters[index]) == null ? void 0 : _a.component) || void 0 : type,
568
653
  new_path,
569
654
  false,
570
655
  endpoint_info
@@ -597,23 +682,15 @@ async function walk_and_store_blobs(data, type = void 0, path = [], root = false
597
682
  )
598
683
  );
599
684
  }
600
- if (!blob_refs.length && !(data instanceof Blob || data instanceof ArrayBuffer || data instanceof Uint8Array)) {
601
- return [
602
- {
603
- path,
604
- blob: new NodeBlob([JSON.stringify(data)]),
605
- type: typeof data
606
- }
607
- ];
608
- }
609
685
  return blob_refs;
610
686
  }
611
687
  return [];
612
688
  }
613
689
  function skip_queue(id, config) {
614
690
  var _a, _b;
615
- if (((_b = (_a = config == null ? void 0 : config.dependencies) == null ? void 0 : _a[id]) == null ? void 0 : _b.queue) !== null) {
616
- return !config.dependencies[id].queue;
691
+ let fn_queue = (_b = (_a = config == null ? void 0 : config.dependencies) == null ? void 0 : _a.find((dep) => dep.id == id)) == null ? void 0 : _b.queue;
692
+ if (fn_queue != null) {
693
+ return !fn_queue;
617
694
  }
618
695
  return !config.enable_queue;
619
696
  }
@@ -669,7 +746,8 @@ async function post_data(url, body, additional_headers) {
669
746
  var response = await this.fetch(url, {
670
747
  method: "POST",
671
748
  body: JSON.stringify(body),
672
- headers: { ...headers, ...additional_headers }
749
+ headers: { ...headers, ...additional_headers },
750
+ credentials: "include"
673
751
  });
674
752
  } catch (e) {
675
753
  return [{ error: BROKEN_CONNECTION_MSG }, 500];
@@ -693,10 +771,12 @@ async function predict(endpoint, data) {
693
771
  throw new Error("Could not resolve app config");
694
772
  }
695
773
  if (typeof endpoint === "number") {
696
- dependency = this.config.dependencies[endpoint];
774
+ dependency = this.config.dependencies.find((dep) => dep.id == endpoint);
697
775
  } else {
698
776
  const trimmed_endpoint = endpoint.replace(/^\//, "");
699
- dependency = this.config.dependencies[this.api_map[trimmed_endpoint]];
777
+ dependency = this.config.dependencies.find(
778
+ (dep) => dep.id == this.api_map[trimmed_endpoint]
779
+ );
700
780
  }
701
781
  if (dependency == null ? void 0 : dependency.types.continuous) {
702
782
  throw new Error(
@@ -884,15 +964,31 @@ const hardware_types = [
884
964
  "h100x8"
885
965
  ];
886
966
  async function duplicate(app_reference, options) {
887
- const { hf_token, private: _private, hardware, timeout } = options;
967
+ const { hf_token, private: _private, hardware, timeout, auth } = options;
888
968
  if (hardware && !hardware_types.includes(hardware)) {
889
969
  throw new Error(
890
970
  `Invalid hardware type provided. Valid types are: ${hardware_types.map((v) => `"${v}"`).join(",")}.`
891
971
  );
892
972
  }
973
+ const { http_protocol, host } = await process_endpoint(
974
+ app_reference,
975
+ hf_token
976
+ );
977
+ let cookies = null;
978
+ if (auth) {
979
+ const cookie_header = await get_cookie_header(
980
+ http_protocol,
981
+ host,
982
+ auth,
983
+ fetch
984
+ );
985
+ if (cookie_header)
986
+ cookies = parse_and_set_cookies(cookie_header);
987
+ }
893
988
  const headers = {
894
989
  Authorization: `Bearer ${hf_token}`,
895
- "Content-Type": "application/json"
990
+ "Content-Type": "application/json",
991
+ ...cookies ? { Cookie: cookies.join("; ") } : {}
896
992
  };
897
993
  const user = (await (await fetch(`https://huggingface.co/api/whoami-v2`, {
898
994
  headers
@@ -1193,6 +1289,34 @@ function submit(endpoint, data, event_data, trigger_id) {
1193
1289
  );
1194
1290
  }
1195
1291
  }
1292
+ const resolve_heartbeat = async (config2) => {
1293
+ await this._resolve_hearbeat(config2);
1294
+ };
1295
+ async function handle_render_config(render_config) {
1296
+ if (!config)
1297
+ return;
1298
+ let render_id = render_config.render_id;
1299
+ config.components = [
1300
+ ...config.components.filter((c) => c.props.rendered_in !== render_id),
1301
+ ...render_config.components
1302
+ ];
1303
+ config.dependencies = [
1304
+ ...config.dependencies.filter((d) => d.rendered_in !== render_id),
1305
+ ...render_config.dependencies
1306
+ ];
1307
+ const any_state = config.components.some((c) => c.type === "state");
1308
+ const any_unload = config.dependencies.some(
1309
+ (d) => d.targets.some((t) => t[1] === "unload")
1310
+ );
1311
+ config.connect_heartbeat = any_state || any_unload;
1312
+ await resolve_heartbeat(config);
1313
+ fire_event({
1314
+ type: "render",
1315
+ data: render_config,
1316
+ endpoint: _endpoint,
1317
+ fn_index
1318
+ });
1319
+ }
1196
1320
  this.handle_blob(config.root, resolved_data, endpoint_info).then(
1197
1321
  async (_payload) => {
1198
1322
  var _a;
@@ -1229,6 +1353,9 @@ function submit(endpoint, data, event_data, trigger_id) {
1229
1353
  event_data,
1230
1354
  trigger_id
1231
1355
  });
1356
+ if (output.render_config) {
1357
+ handle_render_config(output.render_config);
1358
+ }
1232
1359
  fire_event({
1233
1360
  type: "status",
1234
1361
  endpoint: _endpoint,
@@ -1492,7 +1619,9 @@ function submit(endpoint, data, event_data, trigger_id) {
1492
1619
  }
1493
1620
  let hfhubdev = "dev.spaces.huggingface.tech";
1494
1621
  const origin = hostname.includes(".dev.") ? `https://moon-${hostname.split(".")[1]}.${hfhubdev}` : `https://huggingface.co`;
1495
- const zerogpu_auth_promise = dependency.zerogpu && window.parent != window && config.space_id ? post_message("zerogpu-headers", origin) : Promise.resolve(null);
1622
+ const is_iframe = typeof window !== "undefined" && window.parent != window;
1623
+ const is_zerogpu_space = dependency.zerogpu && config.space_id;
1624
+ const zerogpu_auth_promise = is_iframe && is_zerogpu_space ? post_message("zerogpu-headers", origin) : Promise.resolve(null);
1496
1625
  const post_data_promise = zerogpu_auth_promise.then((headers) => {
1497
1626
  return post_data2(
1498
1627
  `${config.root}/queue/join?${url_params}`,
@@ -1588,12 +1717,7 @@ function submit(endpoint, data, event_data, trigger_id) {
1588
1717
  fn_index
1589
1718
  });
1590
1719
  if (data2.render_config) {
1591
- fire_event({
1592
- type: "render",
1593
- data: data2.render_config,
1594
- endpoint: _endpoint,
1595
- fn_index
1596
- });
1720
+ await handle_render_config(data2.render_config);
1597
1721
  }
1598
1722
  if (complete) {
1599
1723
  fire_event({
@@ -1661,12 +1785,14 @@ function get_endpoint_info(api_info, endpoint, api_map, config) {
1661
1785
  if (typeof endpoint === "number") {
1662
1786
  fn_index = endpoint;
1663
1787
  endpoint_info = api_info.unnamed_endpoints[fn_index];
1664
- dependency = config.dependencies[endpoint];
1788
+ dependency = config.dependencies.find((dep) => dep.id == endpoint);
1665
1789
  } else {
1666
1790
  const trimmed_endpoint = endpoint.replace(/^\//, "");
1667
1791
  fn_index = api_map[trimmed_endpoint];
1668
1792
  endpoint_info = api_info.named_endpoints[endpoint.trim()];
1669
- dependency = config.dependencies[api_map[trimmed_endpoint]];
1793
+ dependency = config.dependencies.find(
1794
+ (dep) => dep.id == api_map[trimmed_endpoint]
1795
+ );
1670
1796
  }
1671
1797
  if (typeof fn_index !== "number") {
1672
1798
  throw new Error(
@@ -1690,6 +1816,7 @@ class Client {
1690
1816
  __publicField(this, "session_hash", Math.random().toString(36).substring(2));
1691
1817
  __publicField(this, "jwt", false);
1692
1818
  __publicField(this, "last_status", {});
1819
+ __publicField(this, "cookies", null);
1693
1820
  // streaming
1694
1821
  __publicField(this, "stream_status", { open: false });
1695
1822
  __publicField(this, "pending_stream_messages", {});
@@ -1706,6 +1833,7 @@ class Client {
1706
1833
  __publicField(this, "predict");
1707
1834
  __publicField(this, "open_stream");
1708
1835
  __publicField(this, "resolve_config");
1836
+ __publicField(this, "resolve_cookies");
1709
1837
  this.app_reference = app_reference;
1710
1838
  this.options = options;
1711
1839
  this.view_api = view_api.bind(this);
@@ -1716,10 +1844,15 @@ class Client {
1716
1844
  this.predict = predict.bind(this);
1717
1845
  this.open_stream = open_stream.bind(this);
1718
1846
  this.resolve_config = resolve_config.bind(this);
1847
+ this.resolve_cookies = resolve_cookies.bind(this);
1719
1848
  this.upload = upload.bind(this);
1720
1849
  }
1721
1850
  fetch(input, init) {
1722
- return fetch(input, init);
1851
+ const headers = new Headers((init == null ? void 0 : init.headers) || {});
1852
+ if (this && this.cookies) {
1853
+ headers.append("Cookie", this.cookies);
1854
+ }
1855
+ return fetch(input, { ...init, headers });
1723
1856
  }
1724
1857
  async stream(url) {
1725
1858
  if (typeof window === "undefined" || typeof EventSource === "undefined") {
@@ -1742,27 +1875,48 @@ class Client {
1742
1875
  global.WebSocket = ws.WebSocket;
1743
1876
  }
1744
1877
  try {
1745
- await this._resolve_config().then(async ({ config }) => {
1746
- this.config = config;
1747
- if (config.space_id && this.options.hf_token) {
1748
- this.jwt = await get_jwt(config.space_id, this.options.hf_token);
1749
- }
1750
- if (this.config && this.config.connect_heartbeat) {
1751
- const heartbeat_url = new URL(
1752
- `${this.config.root}/heartbeat/${this.session_hash}`
1753
- );
1754
- if (this.jwt) {
1755
- heartbeat_url.searchParams.set("__sign", this.jwt);
1756
- }
1757
- this.heartbeat_event = await this.stream(heartbeat_url);
1758
- }
1759
- });
1878
+ if (this.options.auth) {
1879
+ await this.resolve_cookies();
1880
+ }
1881
+ await this._resolve_config().then(
1882
+ ({ config }) => this._resolve_hearbeat(config)
1883
+ );
1760
1884
  } catch (e) {
1761
- throw Error(CONFIG_ERROR_MSG + e.message);
1885
+ throw Error(e);
1762
1886
  }
1763
1887
  this.api_info = await this.view_api();
1764
1888
  this.api_map = map_names_to_ids(((_a = this.config) == null ? void 0 : _a.dependencies) || []);
1765
1889
  }
1890
+ async _resolve_hearbeat(_config) {
1891
+ var _a;
1892
+ if (_config) {
1893
+ this.config = _config;
1894
+ if (this.config && this.config.connect_heartbeat) {
1895
+ if (this.config.space_id && this.options.hf_token) {
1896
+ this.jwt = await get_jwt(
1897
+ this.config.space_id,
1898
+ this.options.hf_token,
1899
+ this.cookies
1900
+ );
1901
+ }
1902
+ }
1903
+ }
1904
+ if (_config.space_id && this.options.hf_token) {
1905
+ this.jwt = await get_jwt(_config.space_id, this.options.hf_token);
1906
+ }
1907
+ if (this.config && this.config.connect_heartbeat) {
1908
+ const heartbeat_url = new URL(
1909
+ `${this.config.root}/heartbeat/${this.session_hash}`
1910
+ );
1911
+ if (this.jwt) {
1912
+ heartbeat_url.searchParams.set("__sign", this.jwt);
1913
+ }
1914
+ if (!this.heartbeat_event)
1915
+ this.heartbeat_event = await this.stream(heartbeat_url);
1916
+ } else {
1917
+ (_a = this.heartbeat_event) == null ? void 0 : _a.close();
1918
+ }
1919
+ }
1766
1920
  static async connect(app_reference, options = {}) {
1767
1921
  const client2 = new this(app_reference, options);
1768
1922
  await client2.init();
@@ -1789,8 +1943,7 @@ class Client {
1789
1943
  }
1790
1944
  return this.config_success(config);
1791
1945
  } catch (e) {
1792
- console.error(e);
1793
- if (space_id) {
1946
+ if (space_id && status_callback) {
1794
1947
  check_space_status(
1795
1948
  space_id,
1796
1949
  RE_SPACE_NAME.test(space_id) ? "space_name" : "subdomain",
@@ -1804,6 +1957,7 @@ class Client {
1804
1957
  load_status: "error",
1805
1958
  detail: "NOT_FOUND"
1806
1959
  });
1960
+ throw Error(e);
1807
1961
  }
1808
1962
  }
1809
1963
  }
@@ -1825,6 +1979,9 @@ class Client {
1825
1979
  return this.prepare_return_obj();
1826
1980
  }
1827
1981
  async handle_space_success(status) {
1982
+ if (!this) {
1983
+ throw new Error(CONFIG_ERROR_MSG);
1984
+ }
1828
1985
  const { status_callback } = this.options;
1829
1986
  if (status_callback)
1830
1987
  status_callback(status);
@@ -1837,7 +1994,6 @@ class Client {
1837
1994
  const _config = await this.config_success(this.config);
1838
1995
  return _config;
1839
1996
  } catch (e) {
1840
- console.error(e);
1841
1997
  if (status_callback) {
1842
1998
  status_callback({
1843
1999
  status: "error",
@@ -1846,6 +2002,7 @@ class Client {
1846
2002
  detail: "NOT_FOUND"
1847
2003
  });
1848
2004
  }
2005
+ throw e;
1849
2006
  }
1850
2007
  }
1851
2008
  }
@@ -1896,7 +2053,8 @@ class Client {
1896
2053
  const response = await this.fetch(`${root_url}/component_server/`, {
1897
2054
  method: "POST",
1898
2055
  body,
1899
- headers
2056
+ headers,
2057
+ credentials: "include"
1900
2058
  });
1901
2059
  if (!response.ok) {
1902
2060
  throw new Error(
@@ -1909,6 +2067,9 @@ class Client {
1909
2067
  console.warn(e);
1910
2068
  }
1911
2069
  }
2070
+ set_cookies(raw_cookies) {
2071
+ this.cookies = parse_and_set_cookies(raw_cookies).join("; ");
2072
+ }
1912
2073
  prepare_return_obj() {
1913
2074
  return {
1914
2075
  config: this.config,
@@ -1 +1 @@
1
- {"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../src/test/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,cAAc,EAAE,MAAM,KAAK,CAAC;AAmCzD,eAAO,MAAM,QAAQ,EAAE,cAAc,EAkZpC,CAAC"}
1
+ {"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../src/test/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,cAAc,EAAE,MAAM,KAAK,CAAC;AA4CzD,eAAO,MAAM,QAAQ,EAAE,cAAc,EAgoBpC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"test_data.d.ts","sourceRoot":"","sources":["../../src/test/test_data.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAElE,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;CAuB5B,CAAC;AAEF,eAAO,MAAM,oBAAoB,EAAE,OAAO,CAAC,OAAO,CA6CjD,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,OAAO,CAAC,OAAO,CAiC9C,CAAC;AAEF,eAAO,MAAM,eAAe,EAAE,MA4V7B,CAAC;AAEF,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;CAmB3B,CAAC;AAEF,eAAO,MAAM,kBAAkB;;CAE9B,CAAC;AAEF,eAAO,MAAM,2BAA2B;;;;;;;;;;;;;;;;;;CAoBvC,CAAC;AAEF,eAAO,MAAM,aAAa,EAAE,YAAY,CAAC,OAAO,CA0C/C,CAAC;AAEF,eAAO,MAAM,oBAAoB;;;;;CAKhC,CAAC"}
1
+ {"version":3,"file":"test_data.d.ts","sourceRoot":"","sources":["../../src/test/test_data.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAElE,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;CAuB5B,CAAC;AAEF,eAAO,MAAM,oBAAoB,EAAE,OAAO,CAAC,OAAO,CA6CjD,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,OAAO,CAAC,OAAO,CAiC9C,CAAC;AAEF,eAAO,MAAM,eAAe,EAAE,MA+V7B,CAAC;AAEF,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;CAmB3B,CAAC;AAEF,eAAO,MAAM,kBAAkB;;CAE9B,CAAC;AAEF,eAAO,MAAM,2BAA2B;;;;;;;;;;;;;;;;;;CAoBvC,CAAC;AAEF,eAAO,MAAM,aAAa,EAAE,YAAY,CAAC,OAAO,CA0C/C,CAAC;AAEF,eAAO,MAAM,oBAAoB;;;;;CAKhC,CAAC"}
package/dist/types.d.ts CHANGED
@@ -108,6 +108,7 @@ export interface Config {
108
108
  max_file_size?: number;
109
109
  }
110
110
  export interface Dependency {
111
+ id: number;
111
112
  targets: [number, string][];
112
113
  inputs: number[];
113
114
  outputs: number[];
@@ -133,6 +134,7 @@ export interface Dependency {
133
134
  final_event: Payload | null;
134
135
  show_api: boolean;
135
136
  zerogpu?: boolean;
137
+ rendered_in: number | null;
136
138
  }
137
139
  export interface DependencyTypes {
138
140
  continuous: boolean;
@@ -161,6 +163,7 @@ export interface DuplicateOptions extends ClientOptions {
161
163
  export interface ClientOptions {
162
164
  hf_token?: `hf_${string}`;
163
165
  status_callback?: SpaceStatusCallback | null;
166
+ auth?: [string, string] | null;
164
167
  }
165
168
  export interface FileData {
166
169
  name: string;
@@ -199,6 +202,8 @@ export interface RenderMessage {
199
202
  data: {
200
203
  components: any[];
201
204
  layout: any;
205
+ dependencies: Dependency[];
206
+ render_id: number;
202
207
  };
203
208
  }
204
209
  export interface Status {
@@ -219,6 +224,7 @@ export interface Status {
219
224
  desc: string | null;
220
225
  }[];
221
226
  time?: Date;
227
+ changed_state_ids?: number[];
222
228
  }
223
229
  export {};
224
230
  //# sourceMappingURL=types.d.ts.map