@olares/lares-mobile 0.15.2 → 0.15.5

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/README.md CHANGED
@@ -19,6 +19,24 @@ import { LaresApp, laresPortsFromAccount, findLaresEntrance } from "@olares/lare
19
19
 
20
20
  PC 预览:`IS_PC_TEST` 时走 `/laresHost` 代理,`laresPortsFromAccount` 会继续用 `PROTOCOL` / `LARES_SUB_DOMAIN` / `ACCOUNT_DOMAIN`。自定义鉴权请求再传 `request`。
21
21
 
22
+ 宿主页面与 Host 跨源时(打包桌面端是 `file://`)必须传 `request`,浏览器不会带上 Olares cookie,靠 cookie 的默认实现一律 401。`request` 会用在所有调用上,包括聊天附件上传和语音转写这两个二进制请求——它们的 `body` 是 `File` / `Blob`,`headers` 里自带 `content-type`,实现里不要再当成 JSON 序列化。
23
+
24
+ ```js
25
+ async function request(url, { method = "GET", body, headers, signal } = {}) {
26
+ // body 可能是 File / Blob,原样发出去
27
+ const res = await send(url, { method, body, headers, signal, token });
28
+ return { ok: res.ok, status: res.status, body: res.json }; // 4xx 也要返回,不要 throw
29
+ }
30
+ ```
31
+
32
+ 事件流 `/api/events.mux` 是唯一带不了 header 的调用——浏览器 `WebSocket` 构造器不收 header。Olares 会从 `Sec-WebSocket-Protocol` 取 token 提升成 `X-Authorization` 再走入口鉴权,所以另外传 `socketProtocol`:
33
+
34
+ ```js
35
+ ports.socketProtocol = () => currentUser.access_token; // 传函数则每次连接现取,换 token 无需重建 runtime
36
+ ```
37
+
38
+ 也接受直接给字符串或数组。**不传就不传**,不要传空串——服务端不回应的子协议会让握手直接失败,同源的 PC 端保持裸构造。
39
+
22
40
  切账号时更新 `baseUrl` / `env` 即可,客户端会丢掉上一台的 runtime 和设置缓存,改连新 Host。
23
41
 
24
42
  本地联调(包未上 registry 时):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olares/lares-mobile",
3
- "version": "0.15.2",
3
+ "version": "0.15.5",
4
4
  "description": "Lares chat component for LarePass",
5
5
  "type": "module",
6
6
  "repository": {
@@ -28,7 +28,7 @@
28
28
  "vue": "^3.2.0"
29
29
  },
30
30
  "dependencies": {
31
- "@olares/lares-core": "0.15.2",
31
+ "@olares/lares-core": "0.15.5",
32
32
  "markdown-it": "^14.3.0"
33
33
  }
34
34
  }
package/src/App.vue CHANGED
@@ -110,6 +110,7 @@
110
110
  </template>
111
111
 
112
112
  <script>
113
+ import { pickHostPorts } from "@olares/lares-core/larepass/host";
113
114
  import { connectChat } from "./runtime.js";
114
115
  import { createT } from "./i18n.js";
115
116
  import { failText, withPendingUser } from "./chat/format.js";
@@ -140,6 +141,9 @@ export default {
140
141
  proxyPrefix: { type: String, default: undefined },
141
142
  request: { type: Function, default: undefined },
142
143
  env: { type: Object, default: undefined },
144
+ // Vue drops an undeclared prop into attrs, so a port missing from this list
145
+ // never reaches `ports` below no matter what the host passes.
146
+ socketProtocol: { type: [Function, String, Array], default: undefined },
143
147
  },
144
148
  data() {
145
149
  return {
@@ -178,12 +182,9 @@ export default {
178
182
  },
179
183
  computed: {
180
184
  ports() {
181
- return {
182
- baseUrl: this.baseUrl,
183
- proxyPrefix: this.proxyPrefix,
184
- request: this.request,
185
- env: this.env,
186
- };
185
+ // Built from the canonical key list rather than spelled out, so adding a
186
+ // port does not need this object edited in lockstep.
187
+ return pickHostPorts(this);
187
188
  },
188
189
  runtime() {
189
190
  return connectChat(this.ports);
package/src/host.js CHANGED
@@ -15,7 +15,25 @@ function pageOrigin() {
15
15
  }
16
16
  }
17
17
 
18
- function openMuxSocket(httpUrl, signal) {
18
+ /**
19
+ * A browser `WebSocket` takes no headers, so the mux upgrade cannot carry the
20
+ * `X-Authorization` every other call uses and a cross-origin host page — a
21
+ * packaged LarePass build sends no Olares cookie — is answered 401. Olares
22
+ * reads the token off `Sec-WebSocket-Protocol` instead and promotes it to the
23
+ * header before the entrance auth runs, so the host hands one down here.
24
+ *
25
+ * Read at connect time rather than captured with the client: a refreshed token
26
+ * then lands on the next reconnect instead of needing a rebuilt runtime.
27
+ */
28
+ function socketProtocols(ports) {
29
+ const value = typeof ports.socketProtocol === "function"
30
+ ? ports.socketProtocol()
31
+ : ports.socketProtocol;
32
+ if (Array.isArray(value)) return value.length > 0 ? value : undefined;
33
+ return value ? String(value) : undefined;
34
+ }
35
+
36
+ function openMuxSocket(httpUrl, signal, protocols) {
19
37
  const Socket = globalThis.WebSocket;
20
38
  if (typeof Socket !== "function") {
21
39
  return Promise.resolve({
@@ -26,7 +44,10 @@ function openMuxSocket(httpUrl, signal) {
26
44
  }
27
45
  return new Promise((resolve) => {
28
46
  let settled = false;
29
- const socket = new Socket(muxWsUrl(httpUrl, pageOrigin()));
47
+ const url = muxWsUrl(httpUrl, pageOrigin());
48
+ // Offering a subprotocol the server does not echo back aborts the
49
+ // handshake, so a host that hands down none keeps the bare constructor.
50
+ const socket = protocols === undefined ? new Socket(url) : new Socket(url, protocols);
30
51
  const finish = (result) => {
31
52
  if (settled) return;
32
53
  settled = true;
@@ -137,15 +158,26 @@ export function createHostClient(ports = {}) {
137
158
  downloadUrl(sessionId, path) {
138
159
  return urlFor(downloadFileUrl(sessionId, path));
139
160
  },
161
+ // Both carry a binary body, so neither can go through `request` as an RPC —
162
+ // but both must still ride the host's auth. `ports.request` is passed
163
+ // through rather than called here so a host that supplies none keeps the
164
+ // cookie `fetch` these two used before.
140
165
  upload(sessionId, file, options) {
141
- return uploadFile(file, sessionId, { ...options, url: urlFor(FILES_UPLOAD_PATH) });
166
+ return uploadFile(file, sessionId, {
167
+ ...options,
168
+ url: urlFor(FILES_UPLOAD_PATH),
169
+ request: ports.request,
170
+ });
142
171
  },
143
172
  transcribe(blob, language, signal) {
144
- return postTranscribe(blob, language, signal, { baseUrl: urlFor(VOICE_API) });
173
+ return postTranscribe(blob, language, signal, {
174
+ baseUrl: urlFor(VOICE_API),
175
+ request: ports.request,
176
+ });
145
177
  },
146
178
  async openMux(signal) {
147
179
  try {
148
- return await openMuxSocket(urlFor(MUX_PATH), signal);
180
+ return await openMuxSocket(urlFor(MUX_PATH), signal, socketProtocols(ports));
149
181
  } catch (err) {
150
182
  return {
151
183
  ok: false,
package/src/index.d.ts CHANGED
@@ -25,6 +25,11 @@ export type HostPorts = {
25
25
  proxyPrefix?: string;
26
26
  env?: HostEnv;
27
27
  request?: (url: string, init?: HostRequestInit) => Promise<HostResponse>;
28
+ /**
29
+ * Token offered as `Sec-WebSocket-Protocol` on the mux upgrade, which cannot
30
+ * carry `X-Authorization`. Pass a function to have it read per connect.
31
+ */
32
+ socketProtocol?: string | string[] | (() => string | string[] | undefined);
28
33
  };
29
34
 
30
35
  export const PC_TEST_PROXY: string;