Skip to content

Commit

Permalink
fix: 拆分hack函数,回退SDK版本
Browse files Browse the repository at this point in the history
  • Loading branch information
duan602728596 committed May 16, 2024
1 parent c4d5d60 commit 9623a37
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/NIMNodePolyfill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"localStorage": "^1.0.4",
"nim-web-sdk-ng": "^10.2.6",
"nim-web-sdk-ng": "^0.17.0",
"sessionstorage": "^0.1.0",
"ws": "^8.16.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/qqtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dayjs": "^1.11.10",
"icqq": "^0.6.6",
"monaco-editor": "0.45.0",
"nim-web-sdk-ng": "^10.2.6",
"nim-web-sdk-ng": "^0.17.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-monaco-editor": "^0.55.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ script.

/**
* Rewrite the WebSocket's send method to fix the verification problem.
* @class HACK_INTERCEPTS_SEND_Websocket
* @class HACK_INTERCEPTS_SEND_NIM_Websocket
*/
class HACK_INTERCEPTS_SEND_Websocket extends WebSocket {}
class HACK_INTERCEPTS_SEND_NIM_Websocket extends WebSocket {}

HACK_INTERCEPTS_SEND_Websocket.prototype.ORIGINAL_send = HACK_INTERCEPTS_SEND_Websocket.prototype.send;
HACK_INTERCEPTS_SEND_NIM_Websocket.prototype.ORIGINAL_send = HACK_INTERCEPTS_SEND_NIM_Websocket.prototype.send;

HACK_INTERCEPTS_SEND_Websocket.prototype.send = function() {
HACK_INTERCEPTS_SEND_NIM_Websocket.prototype.send = function () {
if (/3:::/.test(arguments[0])) {
const message = arguments[0].replace(/3:::/, '');
let data = null;

try {
data = JSON.parse(message);
} catch {}
} catch { /* noop */ }

if (data && data?.SER === 1 && data?.SID === 2 && data?.Q?.length) {
for (const Q of data.Q) {
Expand All @@ -46,6 +46,29 @@ script.
}
}
}
}

return this.ORIGINAL_send.apply(this, arguments);
};

globalThis.HACK_INTERCEPTS_SEND_NIM_Websocket = HACK_INTERCEPTS_SEND_NIM_Websocket;

/**
* Rewrite the WebSocket's send method to fix the verification problem.
* @class HACK_INTERCEPTS_SEND_QCHAT_Websocket
*/
class HACK_INTERCEPTS_SEND_QCHAT_Websocket extends WebSocket {}

HACK_INTERCEPTS_SEND_QCHAT_Websocket.prototype.ORIGINAL_send = HACK_INTERCEPTS_SEND_QCHAT_Websocket.prototype.send;

HACK_INTERCEPTS_SEND_QCHAT_Websocket.prototype.send = function() {
if (/3:::/.test(arguments[0])) {
const message = arguments[0].replace(/3:::/, '');
let data = null;

try {
data = JSON.parse(message);
} catch { /* noop */ }

if (data && data?.SER === 1 && data?.SID === 24 && data?.Q?.length) {
for (const Q of data.Q) {
Expand All @@ -61,5 +84,5 @@ script.
return this.ORIGINAL_send.apply(this, arguments);
};

globalThis.HACK_INTERCEPTS_SEND_Websocket = HACK_INTERCEPTS_SEND_Websocket;
globalThis.HACK_INTERCEPTS_SEND_QCHAT_Websocket = HACK_INTERCEPTS_SEND_QCHAT_Websocket;
})();
8 changes: 4 additions & 4 deletions scripts/fixTypesError.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { cwd } from './utils.mjs';

const nodeModules = path.join(cwd, 'node_modules');

async function replaceWebsocket(fp) {
async function replaceWebsocket(fp, ws) {
const filePath = path.join(nodeModules, fp);
const file = await fsP.readFile(filePath, { encoding: 'utf8' });
const newFile = file.replace(
/window\.WebSocket/g, 'window.HACK_INTERCEPTS_SEND_Websocket||window.WebSocket');
/window\.WebSocket/g, `window.${ ws }||window.WebSocket`);

await fsP.writeFile(filePath, newFile, { encoding: 'utf8' });
}
Expand All @@ -21,8 +21,8 @@ async function fixTypesError() {

// 替换window.WebSocket
await Promise.all([
replaceWebsocket('nim-web-sdk-ng/dist/NIM_BROWSER_SDK.js'),
replaceWebsocket('nim-web-sdk-ng/dist/QCHAT_BROWSER_SDK.js')
replaceWebsocket('nim-web-sdk-ng/dist/NIM_BROWSER_SDK.js', 'HACK_INTERCEPTS_SEND_NIM_Websocket'),
replaceWebsocket('nim-web-sdk-ng/dist/QCHAT_BROWSER_SDK.js', 'HACK_INTERCEPTS_SEND_QCHAT_Websocket')
]);
}

Expand Down

0 comments on commit 9623a37

Please sign in to comment.