@laravel/stream-vue 0.3.2 → 0.3.4
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 +19 -22
- package/dist/index.d.ts +2 -0
- package/dist/index.es.js +179 -130
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,28 +49,23 @@ const sendMessage = () => {
|
|
|
49
49
|
|
|
50
50
|
When sending data back to the stream, the active connection to the stream is canceled before sending the new data. All requests are sent as JSON `POST` requests.
|
|
51
51
|
|
|
52
|
-
The second argument given to `useStream` is an options object that you may use to customize the stream consumption behavior
|
|
53
|
-
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
</script>
|
|
70
|
-
|
|
71
|
-
<template>
|
|
72
|
-
<div>{{ data }}</div>
|
|
73
|
-
</template>
|
|
52
|
+
The second argument given to `useStream` is an options object that you may use to customize the stream consumption behavior:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
type StreamOptions = {
|
|
56
|
+
id?: string;
|
|
57
|
+
initialInput?: Record<string, any>;
|
|
58
|
+
headers?: Record<string, string>;
|
|
59
|
+
csrfToken?: string;
|
|
60
|
+
json?: boolean;
|
|
61
|
+
credentials?: RequestCredentials;
|
|
62
|
+
onResponse?: (response: Response) => void;
|
|
63
|
+
onData?: (data: string) => void;
|
|
64
|
+
onCancel?: () => void;
|
|
65
|
+
onFinish?: () => void;
|
|
66
|
+
onError?: (error: Error) => void;
|
|
67
|
+
onBeforeSend?: (request: RequestInit) => boolean | RequestInit | void;
|
|
68
|
+
};
|
|
74
69
|
```
|
|
75
70
|
|
|
76
71
|
`onResponse` is triggered after a successful initial response from the stream and the raw [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) is passed to the callback.
|
|
@@ -79,6 +74,8 @@ const { data } = useStream("chat", {
|
|
|
79
74
|
|
|
80
75
|
`onFinish` is called when a stream has finished and when an error is thrown during the fetch/read cycle.
|
|
81
76
|
|
|
77
|
+
`onBeforeSend` is called right before sending the request to the server and receives the `RequestInit` object as an argument. Returning `false` from this callback cancels the request, returning a [`RequestInit`](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit) object will override the existing `RequestInit` object.
|
|
78
|
+
|
|
82
79
|
By default, a request is not made the to stream on initialization. You may pass an initial payload to the stream by using the `initialInput` option:
|
|
83
80
|
|
|
84
81
|
```vue
|
package/dist/index.d.ts
CHANGED
|
@@ -23,11 +23,13 @@ declare type StreamOptions = {
|
|
|
23
23
|
headers?: Record<string, string>;
|
|
24
24
|
csrfToken?: string;
|
|
25
25
|
json?: boolean;
|
|
26
|
+
credentials?: RequestCredentials;
|
|
26
27
|
onResponse?: (response: Response) => void;
|
|
27
28
|
onData?: (data: string) => void;
|
|
28
29
|
onCancel?: () => void;
|
|
29
30
|
onFinish?: () => void;
|
|
30
31
|
onError?: (error: Error) => void;
|
|
32
|
+
onBeforeSend?: (request: RequestInit) => boolean | RequestInit | void;
|
|
31
33
|
};
|
|
32
34
|
|
|
33
35
|
/**
|
package/dist/index.es.js
CHANGED
|
@@ -1,185 +1,234 @@
|
|
|
1
|
-
import { ref as
|
|
2
|
-
const M = "data: ",
|
|
1
|
+
import { ref as m, onMounted as q, onUnmounted as x, watch as J, readonly as S } from "vue";
|
|
2
|
+
const M = "data: ", U = (t, {
|
|
3
3
|
eventName: e = "update",
|
|
4
|
-
endSignal:
|
|
5
|
-
glue:
|
|
6
|
-
replace:
|
|
7
|
-
onMessage:
|
|
8
|
-
onComplete:
|
|
9
|
-
onError:
|
|
4
|
+
endSignal: n = "</stream>",
|
|
5
|
+
glue: a = " ",
|
|
6
|
+
replace: i = !1,
|
|
7
|
+
onMessage: F = () => null,
|
|
8
|
+
onComplete: R = () => null,
|
|
9
|
+
onError: b = () => null
|
|
10
10
|
} = {}) => {
|
|
11
|
-
const d =
|
|
12
|
-
let
|
|
13
|
-
const
|
|
14
|
-
d.value = "",
|
|
15
|
-
},
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}),
|
|
19
|
-
}, y = (
|
|
20
|
-
if ([
|
|
21
|
-
|
|
11
|
+
const d = m(""), h = m([]), v = Array.isArray(e) ? e : [e];
|
|
12
|
+
let s = null;
|
|
13
|
+
const l = () => {
|
|
14
|
+
d.value = "", h.value = [];
|
|
15
|
+
}, u = (r = !1) => {
|
|
16
|
+
v.forEach((o) => {
|
|
17
|
+
s == null || s.removeEventListener(o, y);
|
|
18
|
+
}), s == null || s.close(), s = null, r && l();
|
|
19
|
+
}, y = (r) => {
|
|
20
|
+
if ([n, `${M}${n}`].includes(r.data)) {
|
|
21
|
+
u(), R();
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
), d.value =
|
|
27
|
-
}, j = (
|
|
28
|
-
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}),
|
|
24
|
+
i && l(), h.value.push(
|
|
25
|
+
r.data.startsWith(M) ? r.data.substring(M.length) : r.data
|
|
26
|
+
), d.value = h.value.join(a), F(r);
|
|
27
|
+
}, j = (r) => {
|
|
28
|
+
b(r), u();
|
|
29
|
+
}, C = () => {
|
|
30
|
+
l(), s = new EventSource(t), v.forEach((r) => {
|
|
31
|
+
s.addEventListener(r, y);
|
|
32
|
+
}), s.addEventListener("error", j);
|
|
33
33
|
};
|
|
34
|
-
return
|
|
35
|
-
|
|
36
|
-
}),
|
|
37
|
-
|
|
38
|
-
}),
|
|
34
|
+
return q(() => {
|
|
35
|
+
C();
|
|
36
|
+
}), x(() => {
|
|
37
|
+
u();
|
|
38
|
+
}), J(
|
|
39
39
|
() => t,
|
|
40
|
-
(
|
|
41
|
-
|
|
40
|
+
(r, o) => {
|
|
41
|
+
r !== o && (u(), C());
|
|
42
42
|
}
|
|
43
43
|
), {
|
|
44
|
-
message:
|
|
45
|
-
messageParts:
|
|
46
|
-
close:
|
|
47
|
-
clearMessage:
|
|
44
|
+
message: S(d),
|
|
45
|
+
messageParts: S(h),
|
|
46
|
+
close: u,
|
|
47
|
+
clearMessage: l
|
|
48
48
|
};
|
|
49
49
|
}, N = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
50
50
|
let X = (t = 21) => {
|
|
51
|
-
let e = "",
|
|
51
|
+
let e = "", n = crypto.getRandomValues(new Uint8Array(t |= 0));
|
|
52
52
|
for (; t--; )
|
|
53
|
-
e += N[
|
|
53
|
+
e += N[n[t] & 63];
|
|
54
54
|
return e;
|
|
55
55
|
};
|
|
56
|
-
const
|
|
57
|
-
|
|
56
|
+
const w = /* @__PURE__ */ new Map(), K = (t, e) => {
|
|
57
|
+
w.has(t) || w.set(t, {
|
|
58
|
+
onData: [],
|
|
59
|
+
onError: [],
|
|
60
|
+
onFinish: [],
|
|
61
|
+
onResponse: [],
|
|
62
|
+
onCancel: [],
|
|
63
|
+
onBeforeSend: []
|
|
64
|
+
});
|
|
65
|
+
const n = w.get(t);
|
|
66
|
+
return e.onData && n.onData.push(e.onData), e.onError && n.onError.push(e.onError), e.onFinish && n.onFinish.push(e.onFinish), e.onResponse && n.onResponse.push(e.onResponse), e.onCancel && n.onCancel.push(e.onCancel), e.onBeforeSend && n.onBeforeSend.push(e.onBeforeSend), () => {
|
|
67
|
+
H(t, e);
|
|
68
|
+
};
|
|
69
|
+
}, E = (t, e, ...n) => {
|
|
70
|
+
const a = w.get(t);
|
|
71
|
+
return a ? a[e].map((i) => i(...n)) : [];
|
|
72
|
+
}, T = (t) => {
|
|
73
|
+
E(t, "onFinish");
|
|
74
|
+
}, p = (t, e) => {
|
|
75
|
+
E(t, "onError", e);
|
|
76
|
+
}, V = (t, e) => {
|
|
77
|
+
E(t, "onResponse", e);
|
|
78
|
+
}, W = (t) => {
|
|
79
|
+
E(t, "onCancel");
|
|
80
|
+
}, $ = (t, e) => {
|
|
81
|
+
E(t, "onData", e);
|
|
82
|
+
}, G = (t, e) => {
|
|
83
|
+
const n = E(t, "onBeforeSend", e);
|
|
84
|
+
for (const a of n) {
|
|
85
|
+
if (a === !1)
|
|
86
|
+
return !1;
|
|
87
|
+
if (a !== null && typeof a == "object")
|
|
88
|
+
return a;
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
}, H = (t, e) => {
|
|
92
|
+
const n = w.get(t);
|
|
93
|
+
n && (e.onData && (n.onData = n.onData.filter(
|
|
94
|
+
(a) => a !== e.onData
|
|
95
|
+
)), e.onError && (n.onError = n.onError.filter(
|
|
96
|
+
(a) => a !== e.onError
|
|
97
|
+
)), e.onFinish && (n.onFinish = n.onFinish.filter(
|
|
98
|
+
(a) => a !== e.onFinish
|
|
99
|
+
)), e.onResponse && (n.onResponse = n.onResponse.filter(
|
|
100
|
+
(a) => a !== e.onResponse
|
|
101
|
+
)), e.onCancel && (n.onCancel = n.onCancel.filter(
|
|
102
|
+
(a) => a !== e.onCancel
|
|
103
|
+
)), e.onBeforeSend && (n.onBeforeSend = n.onBeforeSend.filter(
|
|
104
|
+
(a) => a !== e.onBeforeSend
|
|
105
|
+
)));
|
|
106
|
+
}, L = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Map(), A = (t) => {
|
|
107
|
+
const e = L.get(t);
|
|
58
108
|
if (e)
|
|
59
109
|
return e;
|
|
60
|
-
const
|
|
110
|
+
const n = {
|
|
61
111
|
controller: new AbortController(),
|
|
62
112
|
data: "",
|
|
63
113
|
isFetching: !1,
|
|
64
114
|
isStreaming: !1,
|
|
65
115
|
jsonData: null
|
|
66
116
|
};
|
|
67
|
-
return
|
|
68
|
-
}, P = (t) => (
|
|
117
|
+
return L.set(t, n), n;
|
|
118
|
+
}, P = (t) => (f.has(t) || f.set(t, []), f.get(t)), O = (t) => {
|
|
69
119
|
var e;
|
|
70
|
-
return
|
|
71
|
-
},
|
|
72
|
-
|
|
120
|
+
return f.has(t) && ((e = f.get(t)) == null ? void 0 : e.length);
|
|
121
|
+
}, Q = (t, e) => (P(t).push(e), () => {
|
|
122
|
+
f.set(
|
|
73
123
|
t,
|
|
74
|
-
P(t).filter((
|
|
75
|
-
),
|
|
76
|
-
}),
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
124
|
+
P(t).filter((n) => n !== e)
|
|
125
|
+
), O(t) || (L.delete(t), f.delete(t));
|
|
126
|
+
}), Y = (t, e) => {
|
|
127
|
+
var a;
|
|
128
|
+
L.set(t, {
|
|
129
|
+
...A(t),
|
|
130
|
+
...e
|
|
131
|
+
});
|
|
132
|
+
const n = A(t);
|
|
133
|
+
(a = f.get(t)) == null || a.forEach((i) => i(n));
|
|
134
|
+
}, Z = (t, e = {}) => {
|
|
135
|
+
const n = e.id ?? X(), a = m(A(n)), i = (() => {
|
|
136
|
+
var g;
|
|
137
|
+
const r = {
|
|
80
138
|
"Content-Type": "application/json",
|
|
81
|
-
"X-STREAM-ID":
|
|
82
|
-
},
|
|
83
|
-
return
|
|
84
|
-
})(),
|
|
85
|
-
let
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
...n
|
|
91
|
-
});
|
|
92
|
-
const a = L(r);
|
|
93
|
-
(s = i.get(r)) == null || s.forEach((o) => o(a));
|
|
94
|
-
}, c = () => {
|
|
95
|
-
var n;
|
|
96
|
-
l.value.controller.abort(), (E || d) && ((n = e.onCancel) == null || n.call(e)), u({
|
|
139
|
+
"X-STREAM-ID": n
|
|
140
|
+
}, o = e.csrfToken ?? ((g = document.querySelector('meta[name="csrf-token"]')) == null ? void 0 : g.getAttribute("content"));
|
|
141
|
+
return o && (r["X-CSRF-TOKEN"] = o), r;
|
|
142
|
+
})(), F = m(a.value.data), R = m(a.value.jsonData), b = m(a.value.isFetching), d = m(a.value.isStreaming);
|
|
143
|
+
let h, v;
|
|
144
|
+
const s = (r) => {
|
|
145
|
+
Y(n, r);
|
|
146
|
+
}, l = () => {
|
|
147
|
+
a.value.controller.abort(), (b || d) && W(n), s({
|
|
97
148
|
isFetching: !1,
|
|
98
149
|
isStreaming: !1
|
|
99
150
|
});
|
|
100
|
-
},
|
|
101
|
-
const
|
|
102
|
-
u({
|
|
103
|
-
isFetching: !0,
|
|
104
|
-
controller: a
|
|
105
|
-
}), fetch(t, {
|
|
151
|
+
}, u = (r = {}) => {
|
|
152
|
+
const o = new AbortController(), g = {
|
|
106
153
|
method: "POST",
|
|
107
|
-
signal:
|
|
154
|
+
signal: o.signal,
|
|
108
155
|
headers: {
|
|
109
|
-
...
|
|
156
|
+
...i,
|
|
110
157
|
...e.headers ?? {}
|
|
111
158
|
},
|
|
112
|
-
body: JSON.stringify(
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
159
|
+
body: JSON.stringify(r),
|
|
160
|
+
credentials: e.credentials ?? "same-origin"
|
|
161
|
+
}, k = G(n, g);
|
|
162
|
+
k !== !1 && (s({
|
|
163
|
+
isFetching: !0,
|
|
164
|
+
controller: o
|
|
165
|
+
}), fetch(t, k ?? g).then(async (c) => {
|
|
166
|
+
if (!c.ok) {
|
|
167
|
+
const D = await c.text();
|
|
168
|
+
throw new Error(D);
|
|
118
169
|
}
|
|
119
|
-
if (!
|
|
170
|
+
if (!c.body)
|
|
120
171
|
throw new Error(
|
|
121
172
|
"ReadableStream not yet supported in this browser."
|
|
122
173
|
);
|
|
123
|
-
return (
|
|
174
|
+
return V(n, c), s({
|
|
124
175
|
isFetching: !1,
|
|
125
176
|
isStreaming: !0
|
|
126
|
-
}),
|
|
127
|
-
}).catch((
|
|
128
|
-
|
|
129
|
-
u({
|
|
177
|
+
}), C(c.body.getReader());
|
|
178
|
+
}).catch((c) => {
|
|
179
|
+
s({
|
|
130
180
|
isFetching: !1,
|
|
131
181
|
isStreaming: !1
|
|
132
|
-
}), (
|
|
133
|
-
});
|
|
134
|
-
},
|
|
135
|
-
|
|
136
|
-
},
|
|
137
|
-
|
|
182
|
+
}), p(n, c), T(n);
|
|
183
|
+
}));
|
|
184
|
+
}, y = (r) => {
|
|
185
|
+
l(), u(r), j();
|
|
186
|
+
}, j = () => {
|
|
187
|
+
s({
|
|
138
188
|
data: "",
|
|
139
189
|
jsonData: null
|
|
140
190
|
});
|
|
141
|
-
},
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
data: C
|
|
191
|
+
}, C = (r, o = "") => r.read().then(({ done: g, value: k }) => {
|
|
192
|
+
const c = new TextDecoder("utf-8").decode(k), D = o + c;
|
|
193
|
+
$(n, c);
|
|
194
|
+
const B = {
|
|
195
|
+
data: D
|
|
147
196
|
};
|
|
148
|
-
if (!
|
|
149
|
-
return
|
|
150
|
-
if (
|
|
197
|
+
if (!g)
|
|
198
|
+
return s(B), C(r, D);
|
|
199
|
+
if (B.isStreaming = !1, e.json)
|
|
151
200
|
try {
|
|
152
|
-
|
|
153
|
-
} catch (
|
|
154
|
-
(
|
|
201
|
+
B.jsonData = JSON.parse(D);
|
|
202
|
+
} catch (I) {
|
|
203
|
+
p(n, I);
|
|
155
204
|
}
|
|
156
|
-
return
|
|
205
|
+
return s(B), T(n), "";
|
|
157
206
|
});
|
|
158
|
-
return
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}), window.addEventListener("beforeunload",
|
|
162
|
-
}),
|
|
163
|
-
|
|
207
|
+
return q(() => {
|
|
208
|
+
h = Q(n, (r) => {
|
|
209
|
+
a.value = A(n), b.value = r.isFetching, d.value = r.isStreaming, F.value = r.data, R.value = r.jsonData;
|
|
210
|
+
}), v = K(n, e), window.addEventListener("beforeunload", l), e.initialInput && u(e.initialInput);
|
|
211
|
+
}), x(() => {
|
|
212
|
+
h(), v(), window.removeEventListener("beforeunload", l), O(n) || l();
|
|
164
213
|
}), {
|
|
165
|
-
data:
|
|
166
|
-
jsonData:
|
|
167
|
-
isFetching:
|
|
168
|
-
isStreaming:
|
|
169
|
-
id:
|
|
170
|
-
send:
|
|
171
|
-
cancel:
|
|
172
|
-
clearData:
|
|
214
|
+
data: S(F),
|
|
215
|
+
jsonData: S(R),
|
|
216
|
+
isFetching: S(b),
|
|
217
|
+
isStreaming: S(d),
|
|
218
|
+
id: n,
|
|
219
|
+
send: y,
|
|
220
|
+
cancel: l,
|
|
221
|
+
clearData: j
|
|
173
222
|
};
|
|
174
|
-
},
|
|
175
|
-
const { jsonData:
|
|
223
|
+
}, z = (t, e = {}) => {
|
|
224
|
+
const { jsonData: n, data: a, ...i } = Z(t, {
|
|
176
225
|
...e,
|
|
177
226
|
json: !0
|
|
178
227
|
});
|
|
179
|
-
return { data:
|
|
228
|
+
return { data: n, strData: a, ...i };
|
|
180
229
|
};
|
|
181
230
|
export {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
231
|
+
U as useEventStream,
|
|
232
|
+
z as useJsonStream,
|
|
233
|
+
Z as useStream
|
|
185
234
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(f,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],s):(f=typeof globalThis<"u"?globalThis:f||self,s(f.LaravelStreamVue={},f.Vue))})(this,function(f,s){"use strict";const T="data: ",J=(t,{eventName:e="update",endSignal:n="</stream>",glue:a=" ",replace:u=!1,onMessage:j=()=>null,onComplete:k=()=>null,onError:y=()=>null}={})=>{const m=s.ref(""),g=s.ref([]),C=Array.isArray(e)?e:[e];let o=null;const c=()=>{m.value="",g.value=[]},h=(r=!1)=>{C.forEach(l=>{o==null||o.removeEventListener(l,v)}),o==null||o.close(),o=null,r&&c()},v=r=>{if([n,`${T}${n}`].includes(r.data)){h(),k();return}u&&c(),g.value.push(r.data.startsWith(T)?r.data.substring(T.length):r.data),m.value=g.value.join(a),j(r)},B=r=>{y(r),h()},D=()=>{c(),o=new EventSource(t),C.forEach(r=>{o.addEventListener(r,v)}),o.addEventListener("error",B)};return s.onMounted(()=>{D()}),s.onUnmounted(()=>{h()}),s.watch(()=>t,(r,l)=>{r!==l&&(h(),D())}),{message:s.readonly(m),messageParts:s.readonly(g),close:h,clearMessage:c}},x="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let I=(t=21)=>{let e="",n=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=x[n[t]&63];return e};const b=new Map,V=(t,e)=>{b.has(t)||b.set(t,{onData:[],onError:[],onFinish:[],onResponse:[],onCancel:[],onBeforeSend:[]});const n=b.get(t);return e.onData&&n.onData.push(e.onData),e.onError&&n.onError.push(e.onError),e.onFinish&&n.onFinish.push(e.onFinish),e.onResponse&&n.onResponse.push(e.onResponse),e.onCancel&&n.onCancel.push(e.onCancel),e.onBeforeSend&&n.onBeforeSend.push(e.onBeforeSend),()=>{$(t,e)}},E=(t,e,...n)=>{const a=b.get(t);return a?a[e].map(u=>u(...n)):[]},p=t=>{E(t,"onFinish")},A=(t,e)=>{E(t,"onError",e)},N=(t,e)=>{E(t,"onResponse",e)},X=t=>{E(t,"onCancel")},K=(t,e)=>{E(t,"onData",e)},W=(t,e)=>{const n=E(t,"onBeforeSend",e);for(const a of n){if(a===!1)return!1;if(a!==null&&typeof a=="object")return a}return null},$=(t,e)=>{const n=b.get(t);n&&(e.onData&&(n.onData=n.onData.filter(a=>a!==e.onData)),e.onError&&(n.onError=n.onError.filter(a=>a!==e.onError)),e.onFinish&&(n.onFinish=n.onFinish.filter(a=>a!==e.onFinish)),e.onResponse&&(n.onResponse=n.onResponse.filter(a=>a!==e.onResponse)),e.onCancel&&(n.onCancel=n.onCancel.filter(a=>a!==e.onCancel)),e.onBeforeSend&&(n.onBeforeSend=n.onBeforeSend.filter(a=>a!==e.onBeforeSend)))},F=new Map,d=new Map,R=t=>{const e=F.get(t);if(e)return e;const n={controller:new AbortController,data:"",isFetching:!1,isStreaming:!1,jsonData:null};return F.set(t,n),n},P=t=>(d.has(t)||d.set(t,[]),d.get(t)),q=t=>{var e;return d.has(t)&&((e=d.get(t))==null?void 0:e.length)},G=(t,e)=>(P(t).push(e),()=>{d.set(t,P(t).filter(n=>n!==e)),q(t)||(F.delete(t),d.delete(t))}),H=(t,e)=>{var a;F.set(t,{...R(t),...e});const n=R(t);(a=d.get(t))==null||a.forEach(u=>u(n))},O=(t,e={})=>{const n=e.id??I(),a=s.ref(R(n)),u=(()=>{var S;const r={"Content-Type":"application/json","X-STREAM-ID":n},l=e.csrfToken??((S=document.querySelector('meta[name="csrf-token"]'))==null?void 0:S.getAttribute("content"));return l&&(r["X-CSRF-TOKEN"]=l),r})(),j=s.ref(a.value.data),k=s.ref(a.value.jsonData),y=s.ref(a.value.isFetching),m=s.ref(a.value.isStreaming);let g,C;const o=r=>{H(n,r)},c=()=>{a.value.controller.abort(),(y||m)&&X(n),o({isFetching:!1,isStreaming:!1})},h=(r={})=>{const l=new AbortController,S={method:"POST",signal:l.signal,headers:{...u,...e.headers??{}},body:JSON.stringify(r),credentials:e.credentials??"same-origin"},L=W(n,S);L!==!1&&(o({isFetching:!0,controller:l}),fetch(t,L??S).then(async i=>{if(!i.ok){const w=await i.text();throw new Error(w)}if(!i.body)throw new Error("ReadableStream not yet supported in this browser.");return N(n,i),o({isFetching:!1,isStreaming:!0}),D(i.body.getReader())}).catch(i=>{o({isFetching:!1,isStreaming:!1}),A(n,i),p(n)}))},v=r=>{c(),h(r),B()},B=()=>{o({data:"",jsonData:null})},D=(r,l="")=>r.read().then(({done:S,value:L})=>{const i=new TextDecoder("utf-8").decode(L),w=l+i;K(n,i);const M={data:w};if(!S)return o(M),D(r,w);if(M.isStreaming=!1,e.json)try{M.jsonData=JSON.parse(w)}catch(U){A(n,U)}return o(M),p(n),""});return s.onMounted(()=>{g=G(n,r=>{a.value=R(n),y.value=r.isFetching,m.value=r.isStreaming,j.value=r.data,k.value=r.jsonData}),C=V(n,e),window.addEventListener("beforeunload",c),e.initialInput&&h(e.initialInput)}),s.onUnmounted(()=>{g(),C(),window.removeEventListener("beforeunload",c),q(n)||c()}),{data:s.readonly(j),jsonData:s.readonly(k),isFetching:s.readonly(y),isStreaming:s.readonly(m),id:n,send:v,cancel:c,clearData:B}},Q=(t,e={})=>{const{jsonData:n,data:a,...u}=O(t,{...e,json:!0});return{data:n,strData:a,...u}};f.useEventStream=J,f.useJsonStream=Q,f.useStream=O,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
|