MCPcopy Create free account
hub / github.com/dashscope/dashscope-sdk-java / start

Function start

src/main/resources/sandbox/web-preview/preview.mjs:254–306  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

252}
253
254async function start() {
255 const input = createInterface({ input: process.stdin });
256 const lines = input[Symbol.asyncIterator]();
257 const { upstream: address, loginToken, sandboxHeaders, openClaw, autoPairOpenClaw } =
258 JSON.parse((await lines.next()).value);
259 const approvePairing = openClaw && autoPairOpenClaw ? requestId => {
260 console.log(requestId);
261 const deadline = setTimeout(() => input.close(), 60_000);
262 return lines.next().then(result => result.value === 'approved', () => false).finally(() => {
263 clearTimeout(deadline);
264 input.close();
265 });
266 } : undefined;
267 if (!approvePairing) input.close();
268 const upstream = new URL(address);
269 const loginUrl = new URL('/', upstream);
270 if (!openClaw) loginUrl.searchParams.set('token', loginToken);
271 const login = await fetch(loginUrl, {
272 headers: sandboxHeaders,
273 redirect: 'manual',
274 signal: AbortSignal.timeout(15_000),
275 });
276 const upstreamCookie = openClaw ? ''
277 : login.headers.getSetCookie().map(cookie => cookie.split(';', 1)[0]).join('; ');
278 await login.body?.cancel();
279 const ready = openClaw
280 ? login.ok && login.headers.get('content-type')?.startsWith('text/html')
281 : login.status === 303 && upstreamCookie;
282 if (!ready) {
283 input.close();
284 console.error('原生 Web UI 启动检查失败。HTTP ' + login.status);
285 process.exitCode = 1;
286 return;
287 }
288
289 const entryPath = '/__preview/' + randomBytes(24).toString('base64url');
290 const sessionCookie = 'sandbox_preview_' + randomBytes(8).toString('hex') + '=' + randomBytes(24).toString('base64url');
291 // OpenClaw 在浏览器读取 fragment,并在原生 WebSocket 握手中提交 Gateway Token。
292 const entryLocation = openClaw ? '/#token=' + encodeURIComponent(loginToken) : '/';
293 const gatewayAuthorization = openClaw ? 'Bearer ' + loginToken : undefined;
294 const server = createPreviewServer({
295 upstream, upstreamCookie, sandboxHeaders, gatewayAuthorization, entryPath, sessionCookie, entryLocation,
296 approvePairing,
297 });
298 server.on('error', () => {
299 input.close();
300 console.error('无法启动本地 Web UI 代理。');
301 process.exitCode = 1;
302 });
303 server.listen(0, '127.0.0.1', () => {
304 console.log('http://127.0.0.1:' + server.address().port + entryPath);
305 });
306}
307
308if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
309 start().catch(() => {

Callers 1

preview.mjsFile · 0.85

Calls 9

createPreviewServerFunction · 0.85
closeMethod · 0.65
cancelMethod · 0.65
parseMethod · 0.45
nextMethod · 0.45
timeoutMethod · 0.45
getMethod · 0.45
errorMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected