-
Notifications
You must be signed in to change notification settings - Fork 1
/
chunkRes.ts
173 lines (166 loc) · 4 KB
/
chunkRes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* 微信http流式响应处理
* ```
* // Example:
* const chunkRes = ChunkRes()
* // can`t use ref() to save task; it will lost task info
* const task = wx.request({
* //...other params
* enableChunked: true,
* success: (res) => {
* const lastResTexts:string[] | undefined = chunkRes.onComplateReturn()
* // dosomething
* }
* })
* task.onChunkReceived(res => {
* const resTexts:string[] | undefined = chunkRes.onChunkReceivedReturn(res)
* // dosomething
* })
* ```
* @returns
*/
export const ChunkRes = () => {
/**
* 分段返回开始
*/
const CHUNK_START = 'data:'
/**
* 分段返回中断
*/
const SPLIT_WORD = '\ndata:'
/**
* 保存返回文本
*/
let lastText = ''
/**
* 保存解码异常的数据
*/
let lastData = new Uint8Array()
/**
* 返回数据转文本
* @param res
* @returns
*/
const getChunkText = (data: any) => {
// let data = res.data;
// console.log('getSeeResData:', data)
// 兼容处理,真机返回的的是 ArrayBuffer
if (data instanceof ArrayBuffer) {
data = new Uint8Array(data)
}
let text = data
// Uint8Array转码
if (typeof data != 'string') {
// 兼容处理 微信小程序不支持TextEncoder/TextDecoder
try {
console.log('lastData', lastData)
text = decodeURIComponent(escape(String.fromCharCode(...lastData, ...data)))
lastData = new Uint8Array()
} catch (error) {
text = ''
console.log('解码异常', data)
// Uint8Array 拼接
let swap = new Uint8Array(lastData.length + data.length)
swap.set(lastData, 0)
swap.set(data, lastData.length)
// lastData = lastData.concat(data)
lastData = swap
}
}
return text
}
/**
* 判断是否被拆分
* @param text
* @returns
*/
const isStartString = (text: string) => {
return text.substring(0, 5) == CHUNK_START
}
/**
* 对被合并的多段请求拆分
* @param text
*/
const splitText = (text: string) => {
return text
.replaceAll(`\n\n${SPLIT_WORD}`, `\n${SPLIT_WORD}`)
.replaceAll(`\n${SPLIT_WORD}`, `${SPLIT_WORD}`)
.split(SPLIT_WORD)
.filter((str) => !!str)
}
/**
* 返回数据集
* @param res
* @param onSuccess
*/
const onChunkReceived = (res: any, onSuccess: (value: string[]) => void) => {
let text = getChunkText(res)
console.log('onChunkReceived', text)
if (isStartString(text) && lastText) {
console.log('onSuccess', lastText)
onSuccess(splitText(removeStartText(lastText)))
// 存储本次的数据
lastText = text
} else {
lastText = lastText + text
}
}
/**
* 返回数据集(返回数据)
* @param res
* @param onSuccess
*/
const onChunkReceivedReturn = function (res: any) {
let text = getChunkText(res)
console.log('onChunkReceived', text)
if (isStartString(text) && lastText) {
// console.log("onSuccess", lastText);
// onSuccess();
let swap = lastText
// 存储本次的数据
lastText = text
return splitText(removeStartText(swap))
} else {
lastText = lastText + text
}
}
/**
* 删除文本的开始的 data:
* @param text
* @returns
*/
const removeStartText = (text: string) => {
if (text.substring(0, CHUNK_START.length) == CHUNK_START) {
return text.substring(CHUNK_START.length)
}
return text
}
/**
* 请求完成调用一下
* @param onSuccess
*/
const onComplate = (onSuccess: (value: string[]) => void) => {
if (lastText) {
onSuccess(splitText(removeStartText(lastText)))
lastText = ''
}
}
/**
* 请求完成调用一下(返回数据)
* @param onSuccess
*/
const onComplateReturn = () => {
if (lastText) {
let swap = lastText
lastText = ''
return splitText(removeStartText(swap))
}
}
return {
getChunkText,
onChunkReceived,
onChunkReceivedReturn,
onComplateReturn,
onComplate
}
}