@lambo-design/shared 1.0.0-beta.269 → 1.0.0-beta.270

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lambo-design/shared",
3
- "version": "1.0.0-beta.269",
3
+ "version": "1.0.0-beta.270",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "lambo",
@@ -14,26 +14,40 @@ class CustomEventSource {
14
14
 
15
15
  async readStream(reader) {
16
16
  let done, value, chunk = '';
17
+ const decoder = new TextDecoder('utf-8');
17
18
  do {
18
19
  ({done, value} = await reader.read());
19
20
  if (done) {
20
21
  this.dispatchEvent('message_finished');
21
22
  } else {
22
- const decoder = new TextDecoder('utf-8');
23
- chunk +=decoder.decode(value, { stream: true })
23
+ chunk += decoder.decode(value, { stream: true })
24
24
  if (chunk && chunk.trim().endsWith("}")) {
25
25
  const lines = chunk.split('\n');
26
- chunk = '';
26
+ // chunk = '';
27
27
  for (let line of lines) {
28
28
  if (line.startsWith('data:')) {
29
29
  const data = line.substring(5).trim(); // 去掉 'data:' 前缀
30
30
  if (data) {
31
+ try {
32
+ const jsonData = JSON.parse(data);
33
+ }catch (e) {
34
+ // 解析失败,说明数据可能不完整,等待更多数据
35
+ console.warn("JSON 解析失败,等待更多数据:", e);
36
+ break;
37
+ }
31
38
  this.dispatchEvent('message', { data: data });
32
39
  }
33
40
  } else if (line.startsWith("{") && line.endsWith("}")) {
41
+ try {
42
+ const jsonData = JSON.parse(line);
43
+ }catch (e) {
44
+ console.warn("JSON 解析失败,等待更多数据:", e);
45
+ break;
46
+ }
34
47
  this.dispatchEvent('message', { data: line });
35
48
  }
36
49
  }
50
+ chunk = '';
37
51
  }
38
52
  }
39
53
  } while (!done);