@tencent-adp/portal-cli 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/acp/agent.js CHANGED
@@ -10,7 +10,7 @@ import { runAcpTurn } from './turn.js';
10
10
  export function portalInitialize(needsLogin = false) {
11
11
  return {
12
12
  protocolVersion: PROTOCOL_VERSION,
13
- agentInfo: { name: 'portal', title: 'Portal', version: '0.0.1' },
13
+ agentInfo: { name: 'portal', title: 'Portal', version: '0.0.3' },
14
14
  agentCapabilities: {
15
15
  loadSession: true,
16
16
  sessionCapabilities: { list: {} },
@@ -79,18 +79,23 @@ async function createAgent(interactive, request, canShowLoginUrl = false) {
79
79
  });
80
80
  if (!auth)
81
81
  throw acp.RequestError.authRequired(undefined, '请先登录 Portal');
82
+ await finishLoginUrl();
82
83
  const client = new PortalClient(auth);
83
84
  const workbench = await loadWorkbench(client, auth.spaceId);
84
85
  return new PortalAcpAgent(client, auth.spaceId, workbench, auth.baseUrl);
85
86
  }
86
87
  finally {
87
- if (showedLoginUrl && request) {
88
- await request.notify(acp.methods.client.elicitation.complete, {
89
- elicitationId: LOGIN_URL_ID,
90
- }).catch((error) => {
91
- console.error(error instanceof Error ? error.message : String(error));
92
- });
93
- }
88
+ await finishLoginUrl();
89
+ }
90
+ async function finishLoginUrl() {
91
+ if (!showedLoginUrl || !request)
92
+ return;
93
+ showedLoginUrl = false;
94
+ await request.notify(acp.methods.client.elicitation.complete, {
95
+ elicitationId: LOGIN_URL_ID,
96
+ }).catch((error) => {
97
+ console.error(error instanceof Error ? error.message : String(error));
98
+ });
94
99
  }
95
100
  }
96
101
  //# sourceMappingURL=server.js.map
@@ -1,6 +1,7 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { createHash, randomBytes } from 'node:crypto';
3
3
  import { createServer } from 'node:http';
4
+ import { portalHeaders } from '../portal/headers.js';
4
5
  import { readAuthFile, writeAuthFile } from './loadAuth.js';
5
6
  const CALLBACK_PORT = 0;
6
7
  const CALLBACK_PATH = '/callback';
@@ -257,12 +258,20 @@ export function startLoginCallback(state, port = CALLBACK_PORT) {
257
258
  });
258
259
  }
259
260
  function answer(response, status, text) {
260
- response.writeHead(status, { 'content-type': 'text/html; charset=utf-8' });
261
- response.end(`<!doctype html><meta charset="utf-8"><title>Portal</title><p>${text}</p>`);
261
+ response.writeHead(status, {
262
+ 'content-type': 'text/html; charset=utf-8',
263
+ connection: 'close',
264
+ });
265
+ response.end(`<!doctype html><meta charset="utf-8"><title>Portal</title><p>${text}</p>`, () => {
266
+ response.socket?.destroy();
267
+ });
262
268
  }
263
269
  function closeServer(server) {
264
270
  return new Promise((resolve) => {
265
271
  server.close(() => resolve());
272
+ // A browser that keeps the callback socket open would otherwise hold
273
+ // server.close() until the tab is closed, so Zed never leaves the login page.
274
+ server.closeAllConnections();
266
275
  });
267
276
  }
268
277
  function openBrowser(url) {
@@ -273,7 +282,7 @@ function openBrowser(url) {
273
282
  child.unref();
274
283
  }
275
284
  async function getJson(url) {
276
- const response = await fetch(url, { headers: { Accept: 'application/json' } });
285
+ const response = await fetch(url, { headers: portalHeaders(url, { Accept: 'application/json' }) });
277
286
  const body = await readResponse(response);
278
287
  if (!response.ok)
279
288
  throw new Error(stringField(body, 'error_description') || `GET ${url.pathname} failed with HTTP ${response.status}`);
@@ -282,7 +291,7 @@ async function getJson(url) {
282
291
  async function postJson(url, payload) {
283
292
  const response = await fetch(url, {
284
293
  method: 'POST',
285
- headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
294
+ headers: portalHeaders(url, { Accept: 'application/json', 'Content-Type': 'application/json' }),
286
295
  body: JSON.stringify(payload),
287
296
  });
288
297
  const body = await readResponse(response);
@@ -293,7 +302,7 @@ async function postJson(url, payload) {
293
302
  async function postForm(url, fields) {
294
303
  const response = await fetch(url, {
295
304
  method: 'POST',
296
- headers: { Accept: 'application/json', 'Content-Type': 'application/x-www-form-urlencoded' },
305
+ headers: portalHeaders(url, { Accept: 'application/json', 'Content-Type': 'application/x-www-form-urlencoded' }),
297
306
  body: new URLSearchParams(fields),
298
307
  });
299
308
  const body = await readResponse(response);
@@ -1,4 +1,5 @@
1
1
  import { randomBytes } from 'node:crypto';
2
+ import { portalHeaders } from './headers.js';
2
3
  import { loggedSseBody, logSse } from './sseLog.js';
3
4
  export class PortalClient {
4
5
  auth;
@@ -72,10 +73,10 @@ export class PortalClient {
72
73
  async call(url, init) {
73
74
  const send = () => fetch(url, {
74
75
  ...init,
75
- headers: {
76
+ headers: portalHeaders(url, {
76
77
  ...init.headers,
77
78
  Authorization: `Bearer ${this.auth.token}`,
78
- },
79
+ }),
79
80
  });
80
81
  const response = await send();
81
82
  if (response.status !== 401 || !this.auth.renew)
@@ -0,0 +1,3 @@
1
+ /** AIO-Forward rejects a POST that has no Origin. Browsers send it; Node does not. */
2
+ export declare function portalHeaders(url: string | URL, headers?: Record<string, string>): Record<string, string>;
3
+ //# sourceMappingURL=headers.d.ts.map
@@ -0,0 +1,5 @@
1
+ /** AIO-Forward rejects a POST that has no Origin. Browsers send it; Node does not. */
2
+ export function portalHeaders(url, headers = {}) {
3
+ return { ...headers, Origin: new URL(url).origin };
4
+ }
5
+ //# sourceMappingURL=headers.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tencent-adp/portal-cli",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Talk to a Portal agent and let it use this machine",
5
5
  "type": "module",
6
6
  "bin": {