@laravel/stream-vue 0.3.3 → 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 CHANGED
@@ -49,29 +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. The default values for this object are shown below:
53
-
54
- ```vue
55
- <script setup lang="ts">
56
- import { useStream } from "@laravel/stream-vue";
57
-
58
- const { data } = useStream("chat", {
59
- id: undefined,
60
- initialInput: undefined,
61
- headers: undefined,
62
- csrfToken: undefined,
63
- credentials: undefined,
64
- onResponse: (response: Response) => void,
65
- onData: (data: string) => void,
66
- onCancel: () => void,
67
- onFinish: () => void,
68
- onError: (error: Error) => void,
69
- });
70
- </script>
71
-
72
- <template>
73
- <div>{{ data }}</div>
74
- </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
+ };
75
69
  ```
76
70
 
77
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.
@@ -80,6 +74,8 @@ const { data } = useStream("chat", {
80
74
 
81
75
  `onFinish` is called when a stream has finished and when an error is thrown during the fetch/read cycle.
82
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
+
83
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:
84
80
 
85
81
  ```vue
package/dist/index.d.ts CHANGED
@@ -29,6 +29,7 @@ declare type StreamOptions = {
29
29
  onCancel?: () => void;
30
30
  onFinish?: () => void;
31
31
  onError?: (error: Error) => void;
32
+ onBeforeSend?: (request: RequestInit) => boolean | RequestInit | void;
32
33
  };
33
34
 
34
35
  /**
package/dist/index.es.js CHANGED
@@ -1,186 +1,234 @@
1
- import { ref as S, onMounted as x, onUnmounted as O, watch as q, readonly as v } from "vue";
2
- const M = "data: ", $ = (t, {
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: r = "</stream>",
5
- glue: l = " ",
6
- replace: w = !1,
7
- onMessage: b = () => null,
8
- onComplete: D = () => null,
9
- onError: E = () => null
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 = S(""), f = S([]), u = Array.isArray(e) ? e : [e];
12
- let c = null;
13
- const g = () => {
14
- d.value = "", f.value = [];
15
- }, h = (a = !1) => {
16
- u.forEach((s) => {
17
- c == null || c.removeEventListener(s, y);
18
- }), c == null || c.close(), c = null, a && g();
19
- }, y = (a) => {
20
- if ([r, `${M}${r}`].includes(a.data)) {
21
- h(), D();
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
- w && g(), f.value.push(
25
- a.data.startsWith(M) ? a.data.substring(M.length) : a.data
26
- ), d.value = f.value.join(l), b(a);
27
- }, j = (a) => {
28
- E(a), h();
29
- }, n = () => {
30
- g(), c = new EventSource(t), u.forEach((a) => {
31
- c.addEventListener(a, y);
32
- }), c.addEventListener("error", j);
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 x(() => {
35
- n();
36
- }), O(() => {
37
- h();
38
- }), q(
34
+ return q(() => {
35
+ C();
36
+ }), x(() => {
37
+ u();
38
+ }), J(
39
39
  () => t,
40
- (a, s) => {
41
- a !== s && (h(), n());
40
+ (r, o) => {
41
+ r !== o && (u(), C());
42
42
  }
43
43
  ), {
44
- message: v(d),
45
- messageParts: v(f),
46
- close: h,
47
- clearMessage: g
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 = "", r = crypto.getRandomValues(new Uint8Array(t |= 0));
51
+ let e = "", n = crypto.getRandomValues(new Uint8Array(t |= 0));
52
52
  for (; t--; )
53
- e += N[r[t] & 63];
53
+ e += N[n[t] & 63];
54
54
  return e;
55
55
  };
56
- const A = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), L = (t) => {
57
- const e = A.get(t);
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 r = {
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 A.set(t, r), r;
68
- }, P = (t) => (o.has(t) || o.set(t, []), o.get(t)), I = (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 o.has(t) && ((e = o.get(t)) == null ? void 0 : e.length);
71
- }, K = (t, e) => (P(t).push(e), () => {
72
- o.set(
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((r) => r !== e)
75
- ), I(t) || (A.delete(t), o.delete(t));
76
- }), V = (t, e = {}) => {
77
- const r = e.id ?? X(), l = S(L(r)), w = (() => {
78
- var s;
79
- const n = {
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": r
82
- }, a = e.csrfToken ?? ((s = document.querySelector('meta[name="csrf-token"]')) == null ? void 0 : s.getAttribute("content"));
83
- return a && (n["X-CSRF-TOKEN"] = a), n;
84
- })(), b = S(l.value.data), D = S(l.value.jsonData), E = S(l.value.isFetching), d = S(l.value.isStreaming);
85
- let f;
86
- const u = (n) => {
87
- var s;
88
- A.set(r, {
89
- ...L(r),
90
- ...n
91
- });
92
- const a = L(r);
93
- (s = o.get(r)) == null || s.forEach((i) => i(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
- }, g = (n = {}) => {
101
- const a = new AbortController();
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: a.signal,
154
+ signal: o.signal,
108
155
  headers: {
109
- ...w,
156
+ ...i,
110
157
  ...e.headers ?? {}
111
158
  },
112
- body: JSON.stringify(n),
159
+ body: JSON.stringify(r),
113
160
  credentials: e.credentials ?? "same-origin"
114
- }).then(async (s) => {
115
- var i;
116
- if (!s.ok) {
117
- const m = await s.text();
118
- throw new Error(m);
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);
119
169
  }
120
- if (!s.body)
170
+ if (!c.body)
121
171
  throw new Error(
122
172
  "ReadableStream not yet supported in this browser."
123
173
  );
124
- return (i = e.onResponse) == null || i.call(e, s), u({
174
+ return V(n, c), s({
125
175
  isFetching: !1,
126
176
  isStreaming: !0
127
- }), j(s.body.getReader());
128
- }).catch((s) => {
129
- var i, m;
130
- u({
177
+ }), C(c.body.getReader());
178
+ }).catch((c) => {
179
+ s({
131
180
  isFetching: !1,
132
181
  isStreaming: !1
133
- }), (i = e.onError) == null || i.call(e, s), (m = e.onFinish) == null || m.call(e);
134
- });
135
- }, h = (n) => {
136
- c(), g(n), y();
137
- }, y = () => {
138
- u({
182
+ }), p(n, c), T(n);
183
+ }));
184
+ }, y = (r) => {
185
+ l(), u(r), j();
186
+ }, j = () => {
187
+ s({
139
188
  data: "",
140
189
  jsonData: null
141
190
  });
142
- }, j = (n, a = "") => n.read().then(({ done: s, value: i }) => {
143
- var R, T, k;
144
- const m = new TextDecoder("utf-8").decode(i), C = a + m;
145
- (R = e.onData) == null || R.call(e, m);
146
- const F = {
147
- 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
148
196
  };
149
- if (!s)
150
- return u(F), j(n, C);
151
- if (F.isStreaming = !1, e.json)
197
+ if (!g)
198
+ return s(B), C(r, D);
199
+ if (B.isStreaming = !1, e.json)
152
200
  try {
153
- F.jsonData = JSON.parse(C);
154
- } catch (J) {
155
- (T = e.onError) == null || T.call(e, J);
201
+ B.jsonData = JSON.parse(D);
202
+ } catch (I) {
203
+ p(n, I);
156
204
  }
157
- return u(F), (k = e.onFinish) == null || k.call(e), "";
205
+ return s(B), T(n), "";
158
206
  });
159
- return x(() => {
160
- f = K(r, (n) => {
161
- l.value = L(r), E.value = n.isFetching, d.value = n.isStreaming, b.value = n.data, D.value = n.jsonData;
162
- }), window.addEventListener("beforeunload", c), e.initialInput && g(e.initialInput);
163
- }), O(() => {
164
- f(), window.removeEventListener("beforeunload", c), I(r) || c();
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();
165
213
  }), {
166
- data: v(b),
167
- jsonData: v(D),
168
- isFetching: v(E),
169
- isStreaming: v(d),
170
- id: r,
171
- send: h,
172
- cancel: c,
173
- clearData: y
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
174
222
  };
175
- }, B = (t, e = {}) => {
176
- const { jsonData: r, data: l, ...w } = V(t, {
223
+ }, z = (t, e = {}) => {
224
+ const { jsonData: n, data: a, ...i } = Z(t, {
177
225
  ...e,
178
226
  json: !0
179
227
  });
180
- return { data: r, strData: l, ...w };
228
+ return { data: n, strData: a, ...i };
181
229
  };
182
230
  export {
183
- $ as useEventStream,
184
- B as useJsonStream,
185
- V as useStream
231
+ U as useEventStream,
232
+ z as useJsonStream,
233
+ Z as useStream
186
234
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(i,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],n):(i=typeof globalThis<"u"?globalThis:i||self,n(i.LaravelStreamVue={},i.Vue))})(this,function(i,n){"use strict";const M="data: ",q=(t,{eventName:e="update",endSignal:r="</stream>",glue:d=" ",replace:w=!1,onMessage:F=()=>null,onComplete:L=()=>null,onError:E=()=>null}={})=>{const h=n.ref(""),m=n.ref([]),o=Array.isArray(e)?e:[e];let c=null;const g=()=>{h.value="",m.value=[]},S=(a=!1)=>{o.forEach(l=>{c==null||c.removeEventListener(l,b)}),c==null||c.close(),c=null,a&&g()},b=a=>{if([r,`${M}${r}`].includes(a.data)){S(),L();return}w&&g(),m.value.push(a.data.startsWith(M)?a.data.substring(M.length):a.data),h.value=m.value.join(d),F(a)},T=a=>{E(a),S()},s=()=>{g(),c=new EventSource(t),o.forEach(a=>{c.addEventListener(a,b)}),c.addEventListener("error",T)};return n.onMounted(()=>{s()}),n.onUnmounted(()=>{S()}),n.watch(()=>t,(a,l)=>{a!==l&&(S(),s())}),{message:n.readonly(h),messageParts:n.readonly(m),close:S,clearMessage:g}},x="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let I=(t=21)=>{let e="",r=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=x[r[t]&63];return e};const j=new Map,f=new Map,D=t=>{const e=j.get(t);if(e)return e;const r={controller:new AbortController,data:"",isFetching:!1,isStreaming:!1,jsonData:null};return j.set(t,r),r},C=t=>(f.has(t)||f.set(t,[]),f.get(t)),R=t=>{var e;return f.has(t)&&((e=f.get(t))==null?void 0:e.length)},V=(t,e)=>(C(t).push(e),()=>{f.set(t,C(t).filter(r=>r!==e)),R(t)||(j.delete(t),f.delete(t))}),P=(t,e={})=>{const r=e.id??I(),d=n.ref(D(r)),w=(()=>{var l;const s={"Content-Type":"application/json","X-STREAM-ID":r},a=e.csrfToken??((l=document.querySelector('meta[name="csrf-token"]'))==null?void 0:l.getAttribute("content"));return a&&(s["X-CSRF-TOKEN"]=a),s})(),F=n.ref(d.value.data),L=n.ref(d.value.jsonData),E=n.ref(d.value.isFetching),h=n.ref(d.value.isStreaming);let m;const o=s=>{var l;j.set(r,{...D(r),...s});const a=D(r);(l=f.get(r))==null||l.forEach(u=>u(a))},c=()=>{var s;d.value.controller.abort(),(E||h)&&((s=e.onCancel)==null||s.call(e)),o({isFetching:!1,isStreaming:!1})},g=(s={})=>{const a=new AbortController;o({isFetching:!0,controller:a}),fetch(t,{method:"POST",signal:a.signal,headers:{...w,...e.headers??{}},body:JSON.stringify(s),credentials:e.credentials??"same-origin"}).then(async l=>{var u;if(!l.ok){const y=await l.text();throw new Error(y)}if(!l.body)throw new Error("ReadableStream not yet supported in this browser.");return(u=e.onResponse)==null||u.call(e,l),o({isFetching:!1,isStreaming:!0}),T(l.body.getReader())}).catch(l=>{var u,y;o({isFetching:!1,isStreaming:!1}),(u=e.onError)==null||u.call(e,l),(y=e.onFinish)==null||y.call(e)})},S=s=>{c(),g(s),b()},b=()=>{o({data:"",jsonData:null})},T=(s,a="")=>s.read().then(({done:l,value:u})=>{var k,O,J;const y=new TextDecoder("utf-8").decode(u),A=a+y;(k=e.onData)==null||k.call(e,y);const v={data:A};if(!l)return o(v),T(s,A);if(v.isStreaming=!1,e.json)try{v.jsonData=JSON.parse(A)}catch(X){(O=e.onError)==null||O.call(e,X)}return o(v),(J=e.onFinish)==null||J.call(e),""});return n.onMounted(()=>{m=V(r,s=>{d.value=D(r),E.value=s.isFetching,h.value=s.isStreaming,F.value=s.data,L.value=s.jsonData}),window.addEventListener("beforeunload",c),e.initialInput&&g(e.initialInput)}),n.onUnmounted(()=>{m(),window.removeEventListener("beforeunload",c),R(r)||c()}),{data:n.readonly(F),jsonData:n.readonly(L),isFetching:n.readonly(E),isStreaming:n.readonly(h),id:r,send:S,cancel:c,clearData:b}},N=(t,e={})=>{const{jsonData:r,data:d,...w}=P(t,{...e,json:!0});return{data:r,strData:d,...w}};i.useEventStream=q,i.useJsonStream=N,i.useStream=P,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
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"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@laravel/stream-vue",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Laravel streaming hooks for Vue",
5
5
  "keywords": [
6
6
  "laravel",