Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70ca41b

Browse files
author
msojocs
committed
msojocs#9 fix: 云开发控制台
1 parent 158bc10 commit 70ca41b

File tree

8 files changed

+167
-19
lines changed

8 files changed

+167
-19
lines changed
File renamed without changes.
File renamed without changes.

docs/Console.MD

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

test/cloudconsole

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
# node test/pack
3+
tools/fix-cloudconsole
4+
rm -rf /home/msojocs/.config/wechat_devtools/WeappCache
5+
bin/wechat-devtools

tools/fix-cloudconsole

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
echo "Fix Cloud Console"
3+
root_dir=$(cd `dirname $0`/.. && pwd -P)
4+
5+
package_dir="$root_dir/package.nw"
6+
tmp_dir="$root_dir/tmp"
7+
8+
# unpack 文件 到 路径
9+
node "$root_dir/tools/wxvpkg/unpack" "$package_dir/core.wxvpkg" "$tmp_dir/core.wxvpkg"
10+
11+
12+
# find
13+
open_find_result=$( grep -lr "this.props.onWindowOpenFail());" "$root_dir/tmp/core.wxvpkg" )
14+
token_find_result=$( grep -lr "constructor(){this._sessionToken=\"\",this._tokenMap={}}" "$root_dir/tmp/core.wxvpkg" )
15+
echo "云开发控制台启动点: $open_find_result"
16+
echo "WebSocket token存储对象位置: $token_find_result"
17+
18+
19+
# replace
20+
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);}}});"
21+
sed -i "s/this.props.onWindowOpenFail());/$new_cb_handle/g" $open_find_result
22+
23+
new_constructor="constructor(){if(window.tokenData){/*有就直接用*/this._sessionToken=window.tokenData._sessionToken;this._tokenMap=window.tokenData._tokenMap;}else{/*没有就新建*/this._sessionToken=\"\",this._tokenMap={};window.tokenData=this;/*新建完要给中间人*/}}"
24+
sed -i "s#constructor(){this._sessionToken=\"\",this._tokenMap={}}#$new_constructor#g" "$token_find_result"
25+
26+
27+
# pack 路径 到 文件
28+
node "$root_dir/tools/wxvpkg/pack" "$root_dir/tmp/core.wxvpkg" "$package_dir/core.wxvpkg"
29+
rm -rf "$tmp_dir/core.wxvpkg"

tools/update-wechat-devtools-node

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ const patch_wechat_devtools_CLI = function () {
231231
});
232232
});
233233
};
234+
const patch_wechat_devtools_core = function () {
235+
info("Patching wechat-devtools CLI supports");
236+
237+
return new Promise((resolve, reject) => {
238+
spawn(path.resolve(__dirname, "fix-cloudconsole"), [], {
239+
stdio: "inherit",
240+
}).on("close", (code) => {
241+
resolve();
242+
});
243+
});
244+
};
234245
const rebuild_wechat_devtools_node_modules = function () {
235246
info("Rebuilding wechat-devtools node modules");
236247

@@ -268,22 +279,22 @@ const patch_wechat_devtools = function () {
268279
});
269280
});
270281
};
271-
// const patch_wcc_wcsc = function () {
272-
// info("Patching wcc and wcsc");
273-
274-
// return new Promise((resolve, reject) => {
275-
// fs.copyFileSync(
276-
// path.resolve(__dirname, "../compiler/wcc"),
277-
// path.resolve(__dirname, "../package.nw/js/vendor/wcc.exe")
278-
// );
279-
// fs.copyFileSync(
280-
// path.resolve(__dirname, "../compiler/wcsc"),
281-
// path.resolve(__dirname, "../package.nw/js/vendor/wcsc.exe")
282-
// );
283-
284-
// resolve();
285-
// });
286-
// };
282+
const patch_wcc_wcsc = function () {
283+
info("Patching wcc and wcsc");
284+
285+
return new Promise((resolve, reject) => {
286+
fs.copyFileSync(
287+
path.resolve(__dirname, "../compiler/wine/wcc"),
288+
path.resolve(__dirname, "../package.nw/js/vendor/wcc")
289+
);
290+
fs.copyFileSync(
291+
path.resolve(__dirname, "../compiler/wine/wcsc"),
292+
path.resolve(__dirname, "../package.nw/js/vendor/wcsc")
293+
);
294+
295+
resolve();
296+
});
297+
};
287298

288299
const start = async () => {
289300
try {
@@ -294,9 +305,10 @@ const start = async () => {
294305
await patch_wechat_devtools_package_name();
295306
await patch_wechat_devtools_editor_selection_autocopy();
296307
await patch_wechat_devtools_CLI();
308+
await patch_wechat_devtools_core();
297309
await rebuild_wechat_devtools_node_modules();
298310
await patch_wechat_devtools();
299-
// await patch_wcc_wcsc();
311+
await patch_wcc_wcsc();
300312
console.log(
301313
`Succeeded upgrading wechat-devtools to version ${version}`
302314
);

test/pack renamed to tools/wxvpkg/pack

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
#!/usr/bin/env node
2+
13
// https://gist.github.com/chemzqm/9f2334ca201dc2fbc363fdd757aa2ed4
24
const path = require('path')
35
const fs = require('fs')
46
const { execSync } = require('child_process')
57

6-
let file = path.resolve(__dirname, '../package.nw/core.wxvpkg')
8+
const args = process.argv.slice(2);
9+
const from = args[0]
10+
const to = args[1]
11+
12+
let file = to
713
console.log(file)
814
if (fs.existsSync(file)) {
915
execSync(`rm -rf ${file}`)
1016
}
1117

1218
let fd = fs.openSync(file, 'w')
13-
let dest = path.join(__dirname, '../package.nw/core.wxvpkg.ext')
19+
let dest = from
1420

1521
function writeSync(buf, start) {
1622
fs.writeSync(fd, buf, 0, buf.length, start)

tools/wxvpkg/unpack

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Extract core.wxvpkg of current folder to dest folder
2+
const path = require('path')
3+
const fs = require('fs')
4+
5+
const args = process.argv.slice(2);
6+
const from = args[0]
7+
const to = args[1]
8+
9+
let dest = to
10+
fs.mkdirSync(dest, {recursive: true})
11+
let file = from
12+
let fd = fs.openSync(file, 'r')
13+
14+
// read buffer
15+
function readSync(start, length) {
16+
const n = Buffer.alloc(length);
17+
fs.readSync(fd, n, 0, length, start)
18+
return n
19+
}
20+
21+
const totalCount = readSync(14, 4).readInt32BE(0)
22+
const map = {};
23+
let n = 18;
24+
for (let i = 0; i < totalCount; i++) {
25+
const e = {};
26+
// byte length of filename
27+
const i = readSync(n, 4).readInt32BE(0);
28+
n += 4;
29+
e.name = readSync(n, i).toString();
30+
n += i;
31+
e.offset = readSync(n, 4).readInt32BE(0);
32+
n += 4;
33+
e.length = readSync(n, 4).readInt32BE(0);
34+
n += 4;
35+
map[e.name] = e
36+
}
37+
38+
let created = []
39+
for (let item of Object.values(map)) {
40+
let dir = path.join(dest, path.dirname(item.name))
41+
if (created.indexOf(dir) == -1) {
42+
fs.mkdirSync(dir, {recursive: true})
43+
created.push(dir)
44+
}
45+
let buf = readSync(item.offset, item.length)
46+
let filepath = path.join(dest, item.name)
47+
fs.writeFileSync(filepath, buf.toString('utf8'), 'utf8')
48+
}
49+
50+
fs.closeSync(fd)

0 commit comments

Comments
 (0)