Skip to content

Commit 1afe42b

Browse files
committed
feat: replace node:vm with vm2
1 parent 3fbf551 commit 1afe42b

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

lib/routes/zhihu/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Script } from 'node:vm';
2-
31
import { load } from 'cheerio';
42
import { JSDOM, VirtualConsole } from 'jsdom';
3+
import { VM } from 'vm2';
54

65
import { config } from '@/config';
76
import cache from '@/utils/cache';
@@ -106,7 +105,9 @@ const generateZseCk = async (url: string, apiPath: string, configuredDc0: string
106105
(seed.headers.getSetCookie?.() ?? [])
107106
.find((line) => line.startsWith('d_c0='))
108107
?.split(';', 1)[0]
109-
.slice('d_c0='.length) || '';
108+
.slice('d_c0='.length)
109+
.split('|', 1)[0]
110+
.replace(/=+$/, '') || '';
110111
}
111112
if (!dc0) {
112113
throw new Error('zhihu: failed to obtain a guest d_c0 cookie');
@@ -173,7 +174,7 @@ const generateZseCk = async (url: string, apiPath: string, configuredDc0: string
173174
let zseCk: string | undefined;
174175
try {
175176
// Zhihu's challenge is intentionally delivered as executable JavaScript.
176-
new Script(vmScript).runInContext(dom.getInternalVMContext());
177+
new VM({ timeout: 3000, sandbox: window }).run(vmScript);
177178
const timeout = new Promise<undefined>((resolve) => {
178179
setTimeout(resolve, 3000);
179180
});

0 commit comments

Comments
 (0)