@open-wxa/wxa-runner-core 0.12.1 → 0.12.2

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.
@@ -348,6 +348,7 @@ ${frames}` : "");
348
348
  "rmdir",
349
349
  "getFileInfo",
350
350
  "copyFile",
351
+ "rename",
351
352
  "readFile"
352
353
  ]) {
353
354
  const handler = (args, { api }) => {
@@ -752,6 +753,7 @@ ${frames}` : "");
752
753
  function createNetworkApis(options) {
753
754
  const controllers = /* @__PURE__ */ new Map();
754
755
  const sockets = /* @__PURE__ */ new Map();
756
+ const pendingSockets = /* @__PURE__ */ new Set();
755
757
  const { emit } = options;
756
758
  const startTask = (args, run) => {
757
759
  const id = String(++taskSeq);
@@ -889,17 +891,42 @@ ${frames}` : "");
889
891
  const socketTaskId = String(++taskSeq);
890
892
  let socket;
891
893
  try {
892
- socket = new WebSocket(args.url, args.protocols && args.protocols.length ? args.protocols : void 0);
894
+ if (options.socketProxy) {
895
+ const url = new URL("/__wxa__/socket", location.href);
896
+ url.protocol = location.protocol === "https:" ? "wss:" : "ws:";
897
+ socket = new WebSocket(url);
898
+ pendingSockets.add(socketTaskId);
899
+ } else socket = new WebSocket(args.url, args.protocols && args.protocols.length ? args.protocols : void 0);
893
900
  } catch (error) {
894
901
  return fail(api, reason(error));
895
902
  }
896
903
  socket.binaryType = "arraybuffer";
897
904
  sockets.set(socketTaskId, socket);
898
- socket.onopen = () => emit("onSocketTaskStateChange", { socketTaskId, state: "open", header: {}, profile: {} });
899
- socket.onmessage = (event) => emit("onSocketTaskStateChange", { socketTaskId, state: "message", data: event.data });
900
- socket.onerror = () => emit("onSocketTaskStateChange", { socketTaskId, state: "error", errMsg: "connectSocket:fail" });
905
+ let failed = false;
906
+ const onError = () => {
907
+ failed = true;
908
+ emit("onSocketTaskStateChange", { socketTaskId, state: "error", errMsg: "connectSocket:fail" });
909
+ };
910
+ socket.onopen = () => {
911
+ if (options.socketProxy) socket.send(JSON.stringify({
912
+ url: args.url,
913
+ header: args.header,
914
+ protocols: args.protocols,
915
+ timeout: args.timeout || options.defaultSocketTimeout?.() || 6e4
916
+ }));
917
+ else emit("onSocketTaskStateChange", { socketTaskId, state: "open", header: {}, profile: {} });
918
+ };
919
+ socket.onmessage = (event) => {
920
+ if (pendingSockets.has(socketTaskId)) {
921
+ const result = JSON.parse(event.data);
922
+ pendingSockets.delete(socketTaskId);
923
+ emit("onSocketTaskStateChange", { socketTaskId, state: "open", header: result.header, profile: {} });
924
+ } else emit("onSocketTaskStateChange", { socketTaskId, state: "message", data: event.data });
925
+ };
926
+ socket.onerror = onError;
901
927
  socket.onclose = (event) => {
902
928
  sockets.delete(socketTaskId);
929
+ if (pendingSockets.delete(socketTaskId) && !failed) onError();
903
930
  emit("onSocketTaskStateChange", { socketTaskId, state: "close", code: event.code, reason: event.reason });
904
931
  };
905
932
  return ok(api, { socketTaskId });
@@ -908,7 +935,7 @@ ${frames}` : "");
908
935
  const socket = sockets.get(String(args.socketTaskId));
909
936
  if (!socket) return fail(api, "socket task not found");
910
937
  if (args.operationType === "send") {
911
- if (socket.readyState !== WebSocket.OPEN) return fail(api, "socket is not open");
938
+ if (pendingSockets.has(String(args.socketTaskId)) || socket.readyState !== WebSocket.OPEN) return fail(api, "socket is not open");
912
939
  socket.send(args.data);
913
940
  } else if (args.operationType === "close") {
914
941
  socket.close(args.code || 1e3, args.reason);
@@ -3168,6 +3195,8 @@ html, body { margin: 0; height: 100%; background: radial-gradient(120% 80% at 50
3168
3195
  screenTop: () => this.shell.screenTop()
3169
3196
  }),
3170
3197
  ...createNetworkApis({
3198
+ socketProxy: this.info.socketProxy,
3199
+ defaultSocketTimeout: () => this.info.wxConfig.networkTimeout?.connectSocket || 6e4,
3171
3200
  emit: (event, data) => this.emitToService(event, data),
3172
3201
  defaultTimeout: () => this.info.wxConfig.networkTimeout?.request || 6e4,
3173
3202
  routes: this.routes,