@gradio/client 0.15.0 → 0.16.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 +16 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/index.js +77 -35
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +81 -39
- package/src/utils.ts +14 -0
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# @gradio/client
|
2
2
|
|
3
|
+
## 0.16.0
|
4
|
+
|
5
|
+
### Features
|
6
|
+
|
7
|
+
- [#7845](https://github.com/gradio-app/gradio/pull/7845) [`dbb7373`](https://github.com/gradio-app/gradio/commit/dbb7373dde69d4ed2741942b5a1898f8620cec24) - ensure `ImageEditor` events work as expected. Thanks @pngwn!
|
8
|
+
|
9
|
+
### Fixes
|
10
|
+
|
11
|
+
- [#7974](https://github.com/gradio-app/gradio/pull/7974) [`79e0aa8`](https://github.com/gradio-app/gradio/commit/79e0aa81c94e755faa6e85d76ac5d5a666313e6a) - Fix heartbeat in the js client to be Lite compatible. Thanks @whitphx!
|
12
|
+
|
13
|
+
## 0.15.1
|
14
|
+
|
15
|
+
### Fixes
|
16
|
+
|
17
|
+
- [#7926](https://github.com/gradio-app/gradio/pull/7926) [`9666854`](https://github.com/gradio-app/gradio/commit/966685479078f59430b3bced7e6068eb8157c003) - Fixes streaming event race condition. Thanks @aliabid94!
|
18
|
+
|
3
19
|
## 0.15.0
|
4
20
|
|
5
21
|
### Highlights
|
package/dist/client.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAIA,OAAO,EAQN,cAAc,
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAIA,OAAO,EAQN,cAAc,EAId,MAAM,YAAY,C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AI,MAAY,EAChB,IAAI,QAAK,EACT,IAAI,UAAQ,EACZ,QAAQ,MAAY,GAClB,OAAO,CACT;IACC,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,IAAI,GAAG,KAAK,CAAC;CACnB,EAAE,CACH,CAmDA"}
|
package/dist/index.js
CHANGED
@@ -216,6 +216,16 @@ function apply_diff(obj, diff) {
|
|
216
216
|
});
|
217
217
|
return obj;
|
218
218
|
}
|
219
|
+
function post_message(message, origin) {
|
220
|
+
return new Promise((res, _rej) => {
|
221
|
+
const channel = new MessageChannel();
|
222
|
+
channel.port1.onmessage = ({ data }) => {
|
223
|
+
channel.port1.close();
|
224
|
+
res(data);
|
225
|
+
};
|
226
|
+
window.parent.postMessage(message, origin, [channel.port2]);
|
227
|
+
});
|
228
|
+
}
|
219
229
|
async function upload(file_data, root, upload_id, upload_fn = upload_files) {
|
220
230
|
let files = (Array.isArray(file_data) ? file_data : [file_data]).map(
|
221
231
|
(file_data2) => file_data2.blob
|
@@ -330,7 +340,7 @@ async function duplicate(app_reference, options) {
|
|
330
340
|
}
|
331
341
|
function api_factory(fetch_implementation, EventSource_factory) {
|
332
342
|
return { post_data: post_data2, upload_files: upload_files2, client: client2, handle_blob: handle_blob2 };
|
333
|
-
async function post_data2(url, body, token) {
|
343
|
+
async function post_data2(url, body, token, additional_headers) {
|
334
344
|
const headers = { "Content-Type": "application/json" };
|
335
345
|
if (token) {
|
336
346
|
headers.Authorization = `Bearer ${token}`;
|
@@ -339,7 +349,7 @@ function api_factory(fetch_implementation, EventSource_factory) {
|
|
339
349
|
var response = await fetch_implementation(url, {
|
340
350
|
method: "POST",
|
341
351
|
body: JSON.stringify(body),
|
342
|
-
headers
|
352
|
+
headers: { ...headers, ...additional_headers }
|
343
353
|
});
|
344
354
|
} catch (e) {
|
345
355
|
return [{ error: BROKEN_CONNECTION_MSG }, 500];
|
@@ -466,9 +476,10 @@ function api_factory(fetch_implementation, EventSource_factory) {
|
|
466
476
|
hf_token
|
467
477
|
);
|
468
478
|
const _config = await config_success(config);
|
469
|
-
const
|
479
|
+
const heartbeat_url = new URL(
|
470
480
|
`${config.root}/heartbeat/${session_hash}`
|
471
481
|
);
|
482
|
+
EventSource_factory(heartbeat_url);
|
472
483
|
res(_config);
|
473
484
|
} catch (e) {
|
474
485
|
console.error(e);
|
@@ -529,13 +540,16 @@ function api_factory(fetch_implementation, EventSource_factory) {
|
|
529
540
|
function submit(endpoint, data, event_data, trigger_id = null) {
|
530
541
|
let fn_index;
|
531
542
|
let api_info;
|
543
|
+
let dependency;
|
532
544
|
if (typeof endpoint === "number") {
|
533
545
|
fn_index = endpoint;
|
534
546
|
api_info = api.unnamed_endpoints[fn_index];
|
547
|
+
dependency = config.dependencies[endpoint];
|
535
548
|
} else {
|
536
549
|
const trimmed_endpoint = endpoint.replace(/^\//, "");
|
537
550
|
fn_index = api_map[trimmed_endpoint];
|
538
551
|
api_info = api.named_endpoints[endpoint.trim()];
|
552
|
+
dependency = config.dependencies[api_map[trimmed_endpoint]];
|
539
553
|
}
|
540
554
|
if (typeof fn_index !== "number") {
|
541
555
|
throw new Error(
|
@@ -832,14 +846,22 @@ function api_factory(fetch_implementation, EventSource_factory) {
|
|
832
846
|
fn_index,
|
833
847
|
time: /* @__PURE__ */ new Date()
|
834
848
|
});
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
849
|
+
let hostname = window.location.hostname;
|
850
|
+
let hfhubdev = "dev.spaces.huggingface.tech";
|
851
|
+
const origin = hostname.includes(".dev.") ? `https://moon-${hostname.split(".")[1]}.${hfhubdev}` : `https://huggingface.co`;
|
852
|
+
const zerogpu_auth_promise = dependency.zerogpu && window.parent != window && config.space_id ? post_message("zerogpu-headers", origin) : Promise.resolve(null);
|
853
|
+
const post_data_promise = zerogpu_auth_promise.then((headers) => {
|
854
|
+
return post_data2(
|
855
|
+
`${config.root}/queue/join?${url_params}`,
|
856
|
+
{
|
857
|
+
...payload,
|
858
|
+
session_hash
|
859
|
+
},
|
860
|
+
hf_token,
|
861
|
+
headers
|
862
|
+
);
|
863
|
+
});
|
864
|
+
post_data_promise.then(([response, status]) => {
|
843
865
|
if (status === 503) {
|
844
866
|
fire_event({
|
845
867
|
type: "status",
|
@@ -1074,6 +1096,10 @@ function api_factory(fetch_implementation, EventSource_factory) {
|
|
1074
1096
|
event_stream = EventSource_factory(url);
|
1075
1097
|
event_stream.onmessage = async function(event) {
|
1076
1098
|
let _data = JSON.parse(event.data);
|
1099
|
+
if (_data.msg === "close_stream") {
|
1100
|
+
close_stream();
|
1101
|
+
return;
|
1102
|
+
}
|
1077
1103
|
const event_id = _data.event_id;
|
1078
1104
|
if (!event_id) {
|
1079
1105
|
await Promise.all(
|
@@ -1096,9 +1122,6 @@ function api_factory(fetch_implementation, EventSource_factory) {
|
|
1096
1122
|
}
|
1097
1123
|
pending_stream_messages[event_id].push(_data);
|
1098
1124
|
}
|
1099
|
-
if (_data.msg === "close_stream") {
|
1100
|
-
close_stream();
|
1101
|
-
}
|
1102
1125
|
};
|
1103
1126
|
event_stream.onerror = async function(event) {
|
1104
1127
|
await Promise.all(
|
@@ -1118,10 +1141,7 @@ function api_factory(fetch_implementation, EventSource_factory) {
|
|
1118
1141
|
}
|
1119
1142
|
async function component_server(component_id, fn_name, data) {
|
1120
1143
|
var _a;
|
1121
|
-
const headers = {
|
1122
|
-
if (hf_token) {
|
1123
|
-
headers.Authorization = `Bearer ${hf_token}`;
|
1124
|
-
}
|
1144
|
+
const headers = {};
|
1125
1145
|
let root_url;
|
1126
1146
|
let component = config.components.find(
|
1127
1147
|
(comp) => comp.id === component_id
|
@@ -1131,26 +1151,48 @@ function api_factory(fetch_implementation, EventSource_factory) {
|
|
1131
1151
|
} else {
|
1132
1152
|
root_url = config.root;
|
1133
1153
|
}
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
fn_name,
|
1142
|
-
session_hash
|
1143
|
-
}),
|
1144
|
-
headers
|
1154
|
+
let body;
|
1155
|
+
if (data.binary) {
|
1156
|
+
body = new FormData();
|
1157
|
+
for (const key in data.data) {
|
1158
|
+
if (key === "binary")
|
1159
|
+
continue;
|
1160
|
+
body.append(key, data.data[key]);
|
1145
1161
|
}
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1162
|
+
body.set("component_id", component_id);
|
1163
|
+
body.set("fn_name", fn_name);
|
1164
|
+
body.set("session_hash", session_hash);
|
1165
|
+
} else {
|
1166
|
+
body = JSON.stringify({
|
1167
|
+
data,
|
1168
|
+
component_id,
|
1169
|
+
fn_name,
|
1170
|
+
session_hash
|
1171
|
+
});
|
1172
|
+
headers["Content-Type"] = "application/json";
|
1173
|
+
}
|
1174
|
+
if (hf_token) {
|
1175
|
+
headers.Authorization = `Bearer ${hf_token}`;
|
1176
|
+
}
|
1177
|
+
try {
|
1178
|
+
const response = await fetch_implementation(
|
1179
|
+
`${root_url}/component_server/`,
|
1180
|
+
{
|
1181
|
+
method: "POST",
|
1182
|
+
body,
|
1183
|
+
headers
|
1184
|
+
}
|
1150
1185
|
);
|
1186
|
+
if (!response.ok) {
|
1187
|
+
throw new Error(
|
1188
|
+
"Could not connect to component server: " + response.statusText
|
1189
|
+
);
|
1190
|
+
}
|
1191
|
+
const output = await response.json();
|
1192
|
+
return output;
|
1193
|
+
} catch (e) {
|
1194
|
+
console.warn(e);
|
1151
1195
|
}
|
1152
|
-
const output = await response.json();
|
1153
|
-
return output;
|
1154
1196
|
}
|
1155
1197
|
async function view_api(config2) {
|
1156
1198
|
if (api)
|
package/dist/utils.d.ts
CHANGED
@@ -30,4 +30,5 @@ export declare function set_space_hardware(space_id: string, new_hardware: (type
|
|
30
30
|
export declare function set_space_timeout(space_id: string, timeout: number, token: `hf_${string}`): Promise<number>;
|
31
31
|
export declare const hardware_types: readonly ["cpu-basic", "cpu-upgrade", "t4-small", "t4-medium", "a10g-small", "a10g-large", "a100-large"];
|
32
32
|
export declare function apply_diff(obj: any, diff: [string, (number | string)[], any][]): any;
|
33
|
+
export declare function post_message<Res = any>(message: any, origin: string): Promise<Res>;
|
33
34
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAC3B,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,eAAe,EAAE,OAAO,GACtB,MAAM,CAKR;AAED,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG;IACrD,WAAW,EAAE,IAAI,GAAG,KAAK,CAAC;IAC1B,aAAa,EAAE,OAAO,GAAG,QAAQ,CAAC;IAClC,IAAI,EAAE,MAAM,CAAC;CACb,CAgCA;AAED,eAAO,MAAM,aAAa,QAAqB,CAAC;AAChD,eAAO,MAAM,eAAe,QAAwB,CAAC;AACrD,wBAAsB,gBAAgB,CACrC,aAAa,EAAE,MAAM,EACrB,KAAK,CAAC,EAAE,MAAM,MAAM,EAAE,GACpB,OAAO,CAAC;IACV,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,IAAI,GAAG,KAAK,CAAC;IAC1B,aAAa,EAAE,OAAO,GAAG,QAAQ,CAAC;CAClC,CAAC,CA4CD;AAED,wBAAgB,gBAAgB,CAC/B,GAAG,EAAE,MAAM,CAAC,cAAc,CAAC,GACzB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQxB;AAID,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAe5E;AAED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,GAAG,EACrC,OAAO,EAAE,GAAG,EACZ,MAAM,EAAE,MAAM,GACZ,OAAO,CAAC,GAAG,CAAC,CASd"}
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
@@ -12,7 +12,8 @@ import {
|
|
12
12
|
set_space_timeout,
|
13
13
|
hardware_types,
|
14
14
|
resolve_root,
|
15
|
-
apply_diff
|
15
|
+
apply_diff,
|
16
|
+
post_message
|
16
17
|
} from "./utils.js";
|
17
18
|
|
18
19
|
import type {
|
@@ -185,7 +186,8 @@ export function api_factory(
|
|
185
186
|
async function post_data(
|
186
187
|
url: string,
|
187
188
|
body: unknown,
|
188
|
-
token?: `hf_${string}
|
189
|
+
token?: `hf_${string}`,
|
190
|
+
additional_headers?: Record<string, string>
|
189
191
|
): Promise<[PostResponse, number]> {
|
190
192
|
const headers: {
|
191
193
|
Authorization?: string;
|
@@ -199,7 +201,7 @@ export function api_factory(
|
|
199
201
|
var response = await fetch_implementation(url, {
|
200
202
|
method: "POST",
|
201
203
|
body: JSON.stringify(body),
|
202
|
-
headers
|
204
|
+
headers: { ...headers, ...additional_headers }
|
203
205
|
});
|
204
206
|
} catch (e) {
|
205
207
|
return [{ error: BROKEN_CONNECTION_MSG }, 500];
|
@@ -358,9 +360,10 @@ export function api_factory(
|
|
358
360
|
|
359
361
|
const _config = await config_success(config);
|
360
362
|
// connect to the heartbeat endpoint via GET request
|
361
|
-
const
|
363
|
+
const heartbeat_url = new URL(
|
362
364
|
`${config.root}/heartbeat/${session_hash}`
|
363
365
|
);
|
366
|
+
EventSource_factory(heartbeat_url); // Just connect to the endpoint without parsing the response. Ref: https://github.com/gradio-app/gradio/pull/7974#discussion_r1557717540
|
364
367
|
res(_config);
|
365
368
|
} catch (e) {
|
366
369
|
console.error(e);
|
@@ -438,15 +441,18 @@ export function api_factory(
|
|
438
441
|
): SubmitReturn {
|
439
442
|
let fn_index: number;
|
440
443
|
let api_info;
|
444
|
+
let dependency;
|
441
445
|
|
442
446
|
if (typeof endpoint === "number") {
|
443
447
|
fn_index = endpoint;
|
444
448
|
api_info = api.unnamed_endpoints[fn_index];
|
449
|
+
dependency = config.dependencies[endpoint];
|
445
450
|
} else {
|
446
451
|
const trimmed_endpoint = endpoint.replace(/^\//, "");
|
447
452
|
|
448
453
|
fn_index = api_map[trimmed_endpoint];
|
449
454
|
api_info = api.named_endpoints[endpoint.trim()];
|
455
|
+
dependency = config.dependencies[api_map[trimmed_endpoint]];
|
450
456
|
}
|
451
457
|
|
452
458
|
if (typeof fn_index !== "number") {
|
@@ -776,15 +782,27 @@ export function api_factory(
|
|
776
782
|
fn_index,
|
777
783
|
time: new Date()
|
778
784
|
});
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
{
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
785
|
+
let hostname = window.location.hostname;
|
786
|
+
let hfhubdev = "dev.spaces.huggingface.tech";
|
787
|
+
const origin = hostname.includes(".dev.")
|
788
|
+
? `https://moon-${hostname.split(".")[1]}.${hfhubdev}`
|
789
|
+
: `https://huggingface.co`;
|
790
|
+
const zerogpu_auth_promise =
|
791
|
+
dependency.zerogpu && window.parent != window && config.space_id
|
792
|
+
? post_message<Headers>("zerogpu-headers", origin)
|
793
|
+
: Promise.resolve(null);
|
794
|
+
const post_data_promise = zerogpu_auth_promise.then((headers) => {
|
795
|
+
return post_data(
|
796
|
+
`${config.root}/queue/join?${url_params}`,
|
797
|
+
{
|
798
|
+
...payload,
|
799
|
+
session_hash
|
800
|
+
},
|
801
|
+
hf_token,
|
802
|
+
headers
|
803
|
+
);
|
804
|
+
});
|
805
|
+
post_data_promise.then(([response, status]) => {
|
788
806
|
if (status === 503) {
|
789
807
|
fire_event({
|
790
808
|
type: "status",
|
@@ -1049,6 +1067,10 @@ export function api_factory(
|
|
1049
1067
|
event_stream = EventSource_factory(url);
|
1050
1068
|
event_stream.onmessage = async function (event) {
|
1051
1069
|
let _data = JSON.parse(event.data);
|
1070
|
+
if (_data.msg === "close_stream") {
|
1071
|
+
close_stream();
|
1072
|
+
return;
|
1073
|
+
}
|
1052
1074
|
const event_id = _data.event_id;
|
1053
1075
|
if (!event_id) {
|
1054
1076
|
await Promise.all(
|
@@ -1074,9 +1096,6 @@ export function api_factory(
|
|
1074
1096
|
}
|
1075
1097
|
pending_stream_messages[event_id].push(_data);
|
1076
1098
|
}
|
1077
|
-
if (_data.msg === "close_stream") {
|
1078
|
-
close_stream();
|
1079
|
-
}
|
1080
1099
|
};
|
1081
1100
|
event_stream.onerror = async function (event) {
|
1082
1101
|
await Promise.all(
|
@@ -1099,15 +1118,13 @@ export function api_factory(
|
|
1099
1118
|
async function component_server(
|
1100
1119
|
component_id: number,
|
1101
1120
|
fn_name: string,
|
1102
|
-
data: unknown[]
|
1121
|
+
data: unknown[] | { binary: boolean; data: Record<string, any> }
|
1103
1122
|
): Promise<any> {
|
1104
1123
|
const headers: {
|
1105
1124
|
Authorization?: string;
|
1106
1125
|
"Content-Type": "application/json";
|
1107
|
-
} = {
|
1108
|
-
|
1109
|
-
headers.Authorization = `Bearer ${hf_token}`;
|
1110
|
-
}
|
1126
|
+
} = {};
|
1127
|
+
|
1111
1128
|
let root_url: string;
|
1112
1129
|
let component = config.components.find(
|
1113
1130
|
(comp) => comp.id === component_id
|
@@ -1117,28 +1134,53 @@ export function api_factory(
|
|
1117
1134
|
} else {
|
1118
1135
|
root_url = config.root;
|
1119
1136
|
}
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
fn_name: fn_name,
|
1128
|
-
session_hash: session_hash
|
1129
|
-
}),
|
1130
|
-
headers
|
1137
|
+
|
1138
|
+
let body: FormData | string;
|
1139
|
+
if (data.binary) {
|
1140
|
+
body = new FormData();
|
1141
|
+
for (const key in data.data) {
|
1142
|
+
if (key === "binary") continue;
|
1143
|
+
body.append(key, data.data[key]);
|
1131
1144
|
}
|
1132
|
-
|
1145
|
+
body.set("component_id", component_id);
|
1146
|
+
body.set("fn_name", fn_name);
|
1147
|
+
body.set("session_hash", session_hash);
|
1148
|
+
} else {
|
1149
|
+
body = JSON.stringify({
|
1150
|
+
data: data,
|
1151
|
+
component_id,
|
1152
|
+
fn_name,
|
1153
|
+
session_hash
|
1154
|
+
});
|
1133
1155
|
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1156
|
+
headers["Content-Type"] = "application/json";
|
1157
|
+
}
|
1158
|
+
if (hf_token) {
|
1159
|
+
headers.Authorization = `Bearer ${hf_token}`;
|
1138
1160
|
}
|
1139
1161
|
|
1140
|
-
|
1141
|
-
|
1162
|
+
try {
|
1163
|
+
const response = await fetch_implementation(
|
1164
|
+
`${root_url}/component_server/`,
|
1165
|
+
{
|
1166
|
+
method: "POST",
|
1167
|
+
body: body,
|
1168
|
+
headers
|
1169
|
+
}
|
1170
|
+
);
|
1171
|
+
|
1172
|
+
if (!response.ok) {
|
1173
|
+
// console.warn(await response.text());
|
1174
|
+
throw new Error(
|
1175
|
+
"Could not connect to component server: " + response.statusText
|
1176
|
+
);
|
1177
|
+
}
|
1178
|
+
|
1179
|
+
const output = await response.json();
|
1180
|
+
return output;
|
1181
|
+
} catch (e) {
|
1182
|
+
console.warn(e);
|
1183
|
+
}
|
1142
1184
|
}
|
1143
1185
|
|
1144
1186
|
async function view_api(config?: Config): Promise<ApiInfo<JsApiData>> {
|
package/src/utils.ts
CHANGED
@@ -298,3 +298,17 @@ export function apply_diff(
|
|
298
298
|
|
299
299
|
return obj;
|
300
300
|
}
|
301
|
+
|
302
|
+
export function post_message<Res = any>(
|
303
|
+
message: any,
|
304
|
+
origin: string
|
305
|
+
): Promise<Res> {
|
306
|
+
return new Promise((res, _rej) => {
|
307
|
+
const channel = new MessageChannel();
|
308
|
+
channel.port1.onmessage = (({ data }) => {
|
309
|
+
channel.port1.close();
|
310
|
+
res(data);
|
311
|
+
}) as (ev: MessageEvent<Res>) => void;
|
312
|
+
window.parent.postMessage(message, origin, [channel.port2]);
|
313
|
+
});
|
314
|
+
}
|