@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.
package/dist/index.mjs CHANGED
@@ -98,6 +98,26 @@ var FileStore = class {
98
98
  this.write(destPath, this.read(srcPath));
99
99
  return {};
100
100
  },
101
+ rename: ({ oldPath, newPath }) => {
102
+ const source = this.normalize(oldPath);
103
+ const target = this.normalize(newPath);
104
+ const node = this.existing(oldPath);
105
+ this.assertNotRoot(source);
106
+ this.assertNotRoot(target);
107
+ if (source === target) return {};
108
+ const parent = target.slice(0, target.lastIndexOf("/")) || "/";
109
+ if (!this.nodes.get(parent)?.dir) throw new Error(`no such file or directory ${newPath}`);
110
+ if (node.dir && target.startsWith(source + "/")) throw new Error("invalid argument: directory moved into itself");
111
+ const destination = this.nodes.get(target);
112
+ if (destination) {
113
+ if (destination.dir !== node.dir) throw new Error(`incompatible file type ${newPath}`);
114
+ if (destination.dir && this.readdir(target).length) throw new Error(`directory not empty ${newPath}`);
115
+ }
116
+ const entries = [...this.nodes].filter(([key]) => key === source || node.dir && key.startsWith(source + "/"));
117
+ for (const [key] of entries) this.nodes.delete(key);
118
+ for (const [key, value] of entries) this.nodes.set(target + key.slice(source.length), value);
119
+ return {};
120
+ },
101
121
  access: ({ path: path7 }) => {
102
122
  this.existing(path7);
103
123
  return {};
@@ -225,7 +245,7 @@ import net from "net";
225
245
  import os3 from "os";
226
246
  import path6 from "path";
227
247
  import express2 from "express";
228
- import { WebSocket as WebSocket2, WebSocketServer } from "ws";
248
+ import { WebSocket as WebSocket3, WebSocketServer as WebSocketServer2 } from "ws";
229
249
  import { readRegionData, resolveBaseLib, usesIsolatedContext } from "@open-wxa/wxa-runner-extractor";
230
250
 
231
251
  // src/project/AppBuilder.ts
@@ -1411,6 +1431,71 @@ async function handleProxy(req, res) {
1411
1431
  }
1412
1432
  }
1413
1433
 
1434
+ // src/server/socketProxy.ts
1435
+ import { WebSocket, WebSocketServer } from "ws";
1436
+ function mirrorClose(to, code, reason) {
1437
+ if (code === 1005) to.close();
1438
+ else if (code === 1006 || code === 1015) to.terminate();
1439
+ else to.close(code, reason);
1440
+ }
1441
+ function createSocketProxy() {
1442
+ const server = new WebSocketServer({
1443
+ noServer: true,
1444
+ // 不能让其他网站借本地开发服务器建立任意带凭据的连接。
1445
+ verifyClient: ({ origin, req }) => origin === `http://${req.headers.host}`
1446
+ });
1447
+ server.on("connection", (client) => {
1448
+ let upstream;
1449
+ const timer = setTimeout(() => client.close(1008, "Missing socket request"), 1e4);
1450
+ timer.unref();
1451
+ client.on("error", () => void 0);
1452
+ client.once("message", (data, binary) => {
1453
+ clearTimeout(timer);
1454
+ try {
1455
+ if (binary || data.toString().length > 64 * 1024) throw new Error("Invalid socket request");
1456
+ const { url, header: header2, protocols, timeout } = JSON.parse(data.toString());
1457
+ const target = new URL(url);
1458
+ if (!["ws:", "wss:"].includes(target.protocol) || target.username || target.password) throw new Error("Invalid socket URL");
1459
+ upstream = new WebSocket(target, protocols || [], {
1460
+ headers: header2 || {},
1461
+ handshakeTimeout: typeof timeout === "number" && timeout > 0 ? timeout : 6e4
1462
+ });
1463
+ } catch {
1464
+ client.close(1008, "Invalid socket request");
1465
+ return;
1466
+ }
1467
+ const socket = upstream;
1468
+ let header = {};
1469
+ socket.on("upgrade", (response) => {
1470
+ header = response.headers;
1471
+ });
1472
+ socket.on("open", () => {
1473
+ if (client.readyState !== WebSocket.OPEN) {
1474
+ socket.close();
1475
+ return;
1476
+ }
1477
+ client.send(JSON.stringify({ ready: true, header }));
1478
+ });
1479
+ socket.on("message", (message, isBinary) => {
1480
+ if (client.readyState === WebSocket.OPEN) client.send(message, { binary: isBinary });
1481
+ });
1482
+ client.on("message", (message, isBinary) => {
1483
+ if (socket.readyState === WebSocket.OPEN) socket.send(message, { binary: isBinary });
1484
+ });
1485
+ socket.on("error", () => client.terminate());
1486
+ socket.on("close", (code, reason) => {
1487
+ if (client.readyState === WebSocket.OPEN) mirrorClose(client, code, reason);
1488
+ });
1489
+ });
1490
+ client.on("close", (code, reason) => {
1491
+ clearTimeout(timer);
1492
+ if (upstream?.readyState === WebSocket.OPEN) mirrorClose(upstream, code, reason);
1493
+ else if (upstream) upstream.terminate();
1494
+ });
1495
+ });
1496
+ return server;
1497
+ }
1498
+
1414
1499
  // src/server/hmr.ts
1415
1500
  var STYLE_FILE = /\.(wxss|less|scss|sass|css)$/;
1416
1501
  function planHotUpdate(files, units) {
@@ -1429,7 +1514,7 @@ function planHotUpdate(files, units) {
1429
1514
  import { execFile } from "child_process";
1430
1515
  import { randomUUID } from "crypto";
1431
1516
  import { setTimeout as sleep } from "timers/promises";
1432
- import { WebSocket } from "ws";
1517
+ import { WebSocket as WebSocket2 } from "ws";
1433
1518
  import { findDeveloperTicket, findElectronDevtoolsCli, TICKET_ENV } from "@open-wxa/wxa-runner-extractor";
1434
1519
  var AUTO_PORT = 9420;
1435
1520
  var CLI_TIMEOUT = 6e4;
@@ -1520,7 +1605,7 @@ function cliErrorMessage(stdout) {
1520
1605
  function requestTicket(port) {
1521
1606
  return new Promise((resolve, reject) => {
1522
1607
  const id = `wxa-runner-${randomUUID()}`;
1523
- const socket = new WebSocket(`ws://127.0.0.1:${port}`);
1608
+ const socket = new WebSocket2(`ws://127.0.0.1:${port}`);
1524
1609
  const settle = (finish) => {
1525
1610
  clearTimeout(timer);
1526
1611
  socket.removeAllListeners().on("error", () => void 0).terminate();
@@ -1906,6 +1991,7 @@ var DevServer = class {
1906
1991
  constructor(options) {
1907
1992
  this.options = options;
1908
1993
  this.server = null;
1994
+ this.socketProxy = null;
1909
1995
  this.sockets = null;
1910
1996
  this.watcher = null;
1911
1997
  this.address = "";
@@ -1959,12 +2045,17 @@ var DevServer = class {
1959
2045
  ${error.message}`);
1960
2046
  }
1961
2047
  const server = http.createServer(this.app);
1962
- this.sockets = new WebSocketServer({ noServer: true, maxPayload: 64 * 1024 });
2048
+ this.sockets = new WebSocketServer2({ noServer: true, maxPayload: 64 * 1024 });
1963
2049
  this.sockets.on("connection", (socket) => {
1964
2050
  socket.on("error", () => void 0);
1965
2051
  if (this.options.forwardConsole) printBrowserReports(socket, this.logger);
1966
2052
  });
2053
+ this.socketProxy = createSocketProxy();
1967
2054
  server.on("upgrade", (req, socket, head) => {
2055
+ if (req.url === "/__wxa__/socket") {
2056
+ this.socketProxy.handleUpgrade(req, socket, head, (ws) => this.socketProxy.emit("connection", ws, req));
2057
+ return;
2058
+ }
1968
2059
  if (req.url?.startsWith("/__wxa__/ws")) {
1969
2060
  this.sockets.handleUpgrade(req, socket, head, (ws) => this.sockets.emit("connection", ws, req));
1970
2061
  } else {
@@ -1984,6 +2075,8 @@ ${error.message}`);
1984
2075
  await this.watcher?.close();
1985
2076
  this.sockets?.clients.forEach((client) => client.terminate());
1986
2077
  this.sockets?.close();
2078
+ this.socketProxy?.clients.forEach((client) => client.terminate());
2079
+ this.socketProxy?.close();
1987
2080
  const server = this.server;
1988
2081
  this.server = null;
1989
2082
  if (server) {
@@ -2014,6 +2107,7 @@ ${error.message}`);
2014
2107
  baseLibVersion: this.baseLib.version,
2015
2108
  device: this.device,
2016
2109
  devtoolsLogin: this.devtoolsLogin,
2110
+ socketProxy: true,
2017
2111
  forwardConsole: this.options.forwardConsole ?? false,
2018
2112
  webViewWindow: this.options.webViewWindow ?? false
2019
2113
  };
@@ -2239,7 +2333,7 @@ ${message}`);
2239
2333
  broadcast(message) {
2240
2334
  const data = JSON.stringify(message);
2241
2335
  this.sockets?.clients.forEach((client) => {
2242
- if (client.readyState === WebSocket2.OPEN) client.send(data);
2336
+ if (client.readyState === WebSocket3.OPEN) client.send(data);
2243
2337
  });
2244
2338
  }
2245
2339
  };