#9 fix: 云开发控制台

This commit is contained in:
msojocs 2022-02-04 19:34:10 +08:00
parent 158bc1054a
commit 70ca41b66e
8 changed files with 165 additions and 17 deletions

46
docs/Console.MD Normal file
View File

@ -0,0 +1,46 @@
# 修复创建云开发控制台窗口
# 分析
1. 打开云开发控制台,提示`Uncaught TypeError:Cannot read property 'isDev' of undefined`
2. 定位错误出发位置`global.appConfig.isDev`
3. 打开云开发控制台与主界面的调试器,对比`global`对象,发现不一致,云开发控制台缺失大量属性;
4. 结合NW.js新特性[文档链接](https://nwjs.readthedocs.io/en/latest/References/Window/#windowopenurl-options-callback)
5. 云开发控制台应与主界面共享变量,但在新特性后,二者隔离了;因此修复方法就是让它们共享关键变量可通过open方法的回调实现经尝试设定`new_instance`与`mixed_context`无效)
# 修复
## 定位Window.open位置
可通过断点调试实现
文件路径: `package.nw/core.wxvpkg.ext/284af385b4ef6206861fea66a2452277.js`
定位字符串:`nw.Window.open`
在回调函数中添加:
```
Object.keys(window).forEach(key=>{
if(!e.window[key]){
/*没有就添加*/
try{
e.window[key] = window[key];
}catch(e){
/*部分方法不可修改,会抛异常*/
console.error(e);
}
}
})
```
界面可显示,但一直停留在"等待开发者工具初始化"界面,主界面控制台显示`MESSAGE_CENTER connection: invalid token PLUGIN_cloudconsolev1#{token}# , closing`
经检查是token存储器被隔离了于是可借助`window`对象作为中间人传递此数据对象
## 处理TOKEN数据
修改token存储对象构造方法
```
constructor() {
if(window.tokenData){
/*有就直接用*/
this._sessionToken = window.tokenData._sessionToken
this._tokenMap = window.tokenData._tokenMap
}else{
/*没有就新建*/
(this._sessionToken = ""), (this._tokenMap = {});
window.tokenData=this;/*新建完要给中间人*/
}
}
```

5
test/cloudconsole Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
# node test/pack
tools/fix-cloudconsole
rm -rf /home/msojocs/.config/wechat_devtools/WeappCache
bin/wechat-devtools

29
tools/fix-cloudconsole Normal file
View File

@ -0,0 +1,29 @@
#!/bin/bash
echo "Fix Cloud Console"
root_dir=$(cd `dirname $0`/.. && pwd -P)
package_dir="$root_dir/package.nw"
tmp_dir="$root_dir/tmp"
# unpack 文件 到 路径
node "$root_dir/tools/wxvpkg/unpack" "$package_dir/core.wxvpkg" "$tmp_dir/core.wxvpkg"
# find
open_find_result=$( grep -lr "this.props.onWindowOpenFail());" "$root_dir/tmp/core.wxvpkg" )
token_find_result=$( grep -lr "constructor(){this._sessionToken=\"\",this._tokenMap={}}" "$root_dir/tmp/core.wxvpkg" )
echo "云开发控制台启动点: $open_find_result"
echo "WebSocket token存储对象位置: $token_find_result"
# replace
new_cb_handle="this.props.onWindowOpenFail());Object.keys(window).forEach(key=>{if(!e.window[key]){try{e.window[key]=window[key];}catch(e){console.error(e);}}});"
sed -i "s/this.props.onWindowOpenFail());/$new_cb_handle/g" $open_find_result
new_constructor="constructor(){if(window.tokenData){/*有就直接用*/this._sessionToken=window.tokenData._sessionToken;this._tokenMap=window.tokenData._tokenMap;}else{/*没有就新建*/this._sessionToken=\"\",this._tokenMap={};window.tokenData=this;/*新建完要给中间人*/}}"
sed -i "s#constructor(){this._sessionToken=\"\",this._tokenMap={}}#$new_constructor#g" "$token_find_result"
# pack 路径 到 文件
node "$root_dir/tools/wxvpkg/pack" "$root_dir/tmp/core.wxvpkg" "$package_dir/core.wxvpkg"
rm -rf "$tmp_dir/core.wxvpkg"

View File

@ -231,6 +231,17 @@ const patch_wechat_devtools_CLI = function () {
}); });
}); });
}; };
const patch_wechat_devtools_core = function () {
info("Patching wechat-devtools CLI supports");
return new Promise((resolve, reject) => {
spawn(path.resolve(__dirname, "fix-cloudconsole"), [], {
stdio: "inherit",
}).on("close", (code) => {
resolve();
});
});
};
const rebuild_wechat_devtools_node_modules = function () { const rebuild_wechat_devtools_node_modules = function () {
info("Rebuilding wechat-devtools node modules"); info("Rebuilding wechat-devtools node modules");
@ -268,22 +279,22 @@ const patch_wechat_devtools = function () {
}); });
}); });
}; };
// const patch_wcc_wcsc = function () { const patch_wcc_wcsc = function () {
// info("Patching wcc and wcsc"); info("Patching wcc and wcsc");
// return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// fs.copyFileSync( fs.copyFileSync(
// path.resolve(__dirname, "../compiler/wcc"), path.resolve(__dirname, "../compiler/wine/wcc"),
// path.resolve(__dirname, "../package.nw/js/vendor/wcc.exe") path.resolve(__dirname, "../package.nw/js/vendor/wcc")
// ); );
// fs.copyFileSync( fs.copyFileSync(
// path.resolve(__dirname, "../compiler/wcsc"), path.resolve(__dirname, "../compiler/wine/wcsc"),
// path.resolve(__dirname, "../package.nw/js/vendor/wcsc.exe") path.resolve(__dirname, "../package.nw/js/vendor/wcsc")
// ); );
// resolve(); resolve();
// }); });
// }; };
const start = async () => { const start = async () => {
try { try {
@ -294,9 +305,10 @@ const start = async () => {
await patch_wechat_devtools_package_name(); await patch_wechat_devtools_package_name();
await patch_wechat_devtools_editor_selection_autocopy(); await patch_wechat_devtools_editor_selection_autocopy();
await patch_wechat_devtools_CLI(); await patch_wechat_devtools_CLI();
await patch_wechat_devtools_core();
await rebuild_wechat_devtools_node_modules(); await rebuild_wechat_devtools_node_modules();
await patch_wechat_devtools(); await patch_wechat_devtools();
// await patch_wcc_wcsc(); await patch_wcc_wcsc();
console.log( console.log(
`Succeeded upgrading wechat-devtools to version ${version}` `Succeeded upgrading wechat-devtools to version ${version}`
); );

View File

@ -1,16 +1,22 @@
#!/usr/bin/env node
// https://gist.github.com/chemzqm/9f2334ca201dc2fbc363fdd757aa2ed4 // https://gist.github.com/chemzqm/9f2334ca201dc2fbc363fdd757aa2ed4
const path = require('path') const path = require('path')
const fs = require('fs') const fs = require('fs')
const { execSync } = require('child_process') const { execSync } = require('child_process')
let file = path.resolve(__dirname, '../package.nw/core.wxvpkg') const args = process.argv.slice(2);
const from = args[0]
const to = args[1]
let file = to
console.log(file) console.log(file)
if (fs.existsSync(file)) { if (fs.existsSync(file)) {
execSync(`rm -rf ${file}`) execSync(`rm -rf ${file}`)
} }
let fd = fs.openSync(file, 'w') let fd = fs.openSync(file, 'w')
let dest = path.join(__dirname, '../package.nw/core.wxvpkg.ext') let dest = from
function writeSync(buf, start) { function writeSync(buf, start) {
fs.writeSync(fd, buf, 0, buf.length, start) fs.writeSync(fd, buf, 0, buf.length, start)

50
tools/wxvpkg/unpack Normal file
View File

@ -0,0 +1,50 @@
// Extract core.wxvpkg of current folder to dest folder
const path = require('path')
const fs = require('fs')
const args = process.argv.slice(2);
const from = args[0]
const to = args[1]
let dest = to
fs.mkdirSync(dest, {recursive: true})
let file = from
let fd = fs.openSync(file, 'r')
// read buffer
function readSync(start, length) {
const n = Buffer.alloc(length);
fs.readSync(fd, n, 0, length, start)
return n
}
const totalCount = readSync(14, 4).readInt32BE(0)
const map = {};
let n = 18;
for (let i = 0; i < totalCount; i++) {
const e = {};
// byte length of filename
const i = readSync(n, 4).readInt32BE(0);
n += 4;
e.name = readSync(n, i).toString();
n += i;
e.offset = readSync(n, 4).readInt32BE(0);
n += 4;
e.length = readSync(n, 4).readInt32BE(0);
n += 4;
map[e.name] = e
}
let created = []
for (let item of Object.values(map)) {
let dir = path.join(dest, path.dirname(item.name))
if (created.indexOf(dir) == -1) {
fs.mkdirSync(dir, {recursive: true})
created.push(dir)
}
let buf = readSync(item.offset, item.length)
let filepath = path.join(dest, item.name)
fs.writeFileSync(filepath, buf.toString('utf8'), 'utf8')
}
fs.closeSync(fd)