@laravel/stream-vue 0.3.8 → 0.3.10

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/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { MaybeRefOrGetter } from 'vue';
1
2
  import { Ref } from 'vue';
2
3
 
3
4
  declare type EventStreamOptions = {
@@ -42,26 +43,26 @@ declare type StreamOptions<TSendBody extends Record<string, any> = {}> = {
42
43
  *
43
44
  * @returns StreamResult object containing the accumulated response, close, and reset functions
44
45
  */
45
- export declare const useEventStream: (url: string, { eventName, endSignal, glue, replace, onMessage, onComplete, onError, }?: EventStreamOptions) => EventStreamResult;
46
+ export declare const useEventStream: (url: MaybeRefOrGetter<string>, { eventName, endSignal, glue, replace, onMessage, onComplete, onError, }?: EventStreamOptions) => EventStreamResult;
46
47
 
47
- export declare const useJsonStream: <TJsonData = null, TSendBody extends Record<string, any> = {}>(url: string, options?: Omit<StreamOptions<TSendBody>, "json">) => {
48
+ export declare const useJsonStream: <TJsonData = null, TSendBody extends Record<string, any> = {}>(url: MaybeRefOrGetter<string>, options?: Omit<StreamOptions<TSendBody>, "json">) => {
48
49
  isFetching: Readonly<Ref<boolean>>;
49
50
  isStreaming: Readonly<Ref<boolean>>;
50
51
  id: string;
51
- send: (body: TSendBody) => void;
52
+ send: (body?: TSendBody | undefined) => void;
52
53
  cancel: () => void;
53
54
  clearData: () => void;
54
55
  data: Readonly<TJsonData | null>;
55
56
  strData: Readonly<Ref<string, string>>;
56
57
  };
57
58
 
58
- export declare const useStream: <TSendBody extends Record<string, any> = {}, TJsonData = null>(url: string, options?: StreamOptions<TSendBody>) => {
59
+ export declare const useStream: <TSendBody extends Record<string, any> = {}, TJsonData = null>(url: MaybeRefOrGetter<string>, options?: StreamOptions<TSendBody>) => {
59
60
  data: Readonly<Ref<string>>;
60
61
  jsonData: Readonly<TJsonData | null>;
61
62
  isFetching: Readonly<Ref<boolean>>;
62
63
  isStreaming: Readonly<Ref<boolean>>;
63
64
  id: string;
64
- send: (body: TSendBody) => void;
65
+ send: (body?: TSendBody) => void;
65
66
  cancel: () => void;
66
67
  clearData: () => void;
67
68
  };
package/dist/index.es.js CHANGED
@@ -1,60 +1,57 @@
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, {
1
+ import { toRef as x, ref as m, onMounted as O, onUnmounted as I, watch as J, readonly as E } from "vue";
2
+ const T = "data: ", ee = (n, {
3
3
  eventName: e = "update",
4
- endSignal: n = "</stream>",
5
- glue: a = " ",
6
- replace: i = !1,
7
- onMessage: F = () => null,
4
+ endSignal: a = "</stream>",
5
+ glue: t = " ",
6
+ replace: o = !1,
7
+ onMessage: M = () => null,
8
8
  onComplete: R = () => null,
9
- onError: b = () => null
9
+ onError: y = () => null
10
10
  } = {}) => {
11
- const d = m(""), h = m([]), v = Array.isArray(e) ? e : [e];
11
+ const S = x(n), h = m(""), u = m([]), v = Array.isArray(e) ? e : [e];
12
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();
13
+ const c = () => {
14
+ h.value = "", u.value = [];
15
+ }, f = (r = !1) => {
16
+ v.forEach((l) => {
17
+ s == null || s.removeEventListener(l, j);
18
+ }), s == null || s.close(), s = null, r && c();
19
+ }, j = (r) => {
20
+ if ([a, `${T}${a}`].includes(r.data)) {
21
+ f(), R();
22
22
  return;
23
23
  }
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);
24
+ o && c(), u.value.push(
25
+ r.data.startsWith(T) ? r.data.substring(T.length) : r.data
26
+ ), h.value = u.value.join(t), M(r);
27
+ }, C = (r) => {
28
+ y(r), f();
29
+ }, D = () => {
30
+ c(), s = new EventSource(S.value), v.forEach((r) => {
31
+ s.addEventListener(r, j);
32
+ }), s.addEventListener("error", C);
33
33
  };
34
- return q(() => {
35
- C();
36
- }), x(() => {
37
- u();
38
- }), J(
39
- () => t,
40
- (r, o) => {
41
- r !== o && (u(), C());
42
- }
43
- ), {
44
- message: S(d),
45
- messageParts: S(h),
46
- close: u,
47
- clearMessage: l
34
+ return O(() => {
35
+ D();
36
+ }), I(() => {
37
+ f();
38
+ }), J(S, (r, l) => {
39
+ r !== l && (f(), D());
40
+ }), {
41
+ message: E(h),
42
+ messageParts: E(u),
43
+ close: f,
44
+ clearMessage: c
48
45
  };
49
- }, N = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
50
- let X = (t = 21) => {
51
- let e = "", n = crypto.getRandomValues(new Uint8Array(t |= 0));
52
- for (; t--; )
53
- e += N[n[t] & 63];
46
+ }, K = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
47
+ let V = (n = 21) => {
48
+ let e = "", a = crypto.getRandomValues(new Uint8Array(n |= 0));
49
+ for (; n--; )
50
+ e += K[a[n] & 63];
54
51
  return e;
55
52
  };
56
- const w = /* @__PURE__ */ new Map(), K = (t, e) => {
57
- w.has(t) || w.set(t, {
53
+ const F = /* @__PURE__ */ new Map(), W = (n, e) => {
54
+ F.has(n) || F.set(n, {
58
55
  onData: [],
59
56
  onError: [],
60
57
  onFinish: [],
@@ -62,173 +59,175 @@ const w = /* @__PURE__ */ new Map(), K = (t, e) => {
62
59
  onCancel: [],
63
60
  onBeforeSend: []
64
61
  });
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);
62
+ const a = F.get(n);
63
+ return e.onData && a.onData.push(e.onData), e.onError && a.onError.push(e.onError), e.onFinish && a.onFinish.push(e.onFinish), e.onResponse && a.onResponse.push(e.onResponse), e.onCancel && a.onCancel.push(e.onCancel), e.onBeforeSend && a.onBeforeSend.push(e.onBeforeSend), () => {
64
+ Y(n, e);
68
65
  };
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)
66
+ }, b = (n, e, ...a) => {
67
+ const t = F.get(n);
68
+ return t ? t[e].map((o) => o(...a)) : [];
69
+ }, p = (n) => {
70
+ b(n, "onFinish");
71
+ }, P = (n, e) => {
72
+ b(n, "onError", e);
73
+ }, $ = (n, e) => {
74
+ b(n, "onResponse", e);
75
+ }, G = (n) => {
76
+ b(n, "onCancel");
77
+ }, H = (n, e) => {
78
+ b(n, "onData", e);
79
+ }, Q = (n, e) => {
80
+ const a = b(n, "onBeforeSend", e);
81
+ for (const t of a) {
82
+ if (t === !1)
86
83
  return !1;
87
- if (a !== null && typeof a == "object")
88
- return a;
84
+ if (t !== null && typeof t == "object")
85
+ return t;
89
86
  }
90
87
  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
88
+ }, Y = (n, e) => {
89
+ const a = F.get(n);
90
+ a && (e.onData && (a.onData = a.onData.filter(
91
+ (t) => t !== e.onData
92
+ )), e.onError && (a.onError = a.onError.filter(
93
+ (t) => t !== e.onError
94
+ )), e.onFinish && (a.onFinish = a.onFinish.filter(
95
+ (t) => t !== e.onFinish
96
+ )), e.onResponse && (a.onResponse = a.onResponse.filter(
97
+ (t) => t !== e.onResponse
98
+ )), e.onCancel && (a.onCancel = a.onCancel.filter(
99
+ (t) => t !== e.onCancel
100
+ )), e.onBeforeSend && (a.onBeforeSend = a.onBeforeSend.filter(
101
+ (t) => t !== e.onBeforeSend
105
102
  )));
106
- }, L = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Map(), A = (t) => {
107
- const e = L.get(t);
103
+ }, L = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), A = (n) => {
104
+ const e = L.get(n);
108
105
  if (e)
109
106
  return e;
110
- const n = {
107
+ const a = {
111
108
  controller: new AbortController(),
112
109
  data: "",
113
110
  isFetching: !1,
114
111
  isStreaming: !1,
115
112
  jsonData: null
116
113
  };
117
- return L.set(t, n), n;
118
- }, P = (t) => (f.has(t) || f.set(t, []), f.get(t)), O = (t) => {
114
+ return L.set(n, a), a;
115
+ }, q = (n) => (d.has(n) || d.set(n, []), d.get(n)), N = (n) => {
119
116
  var e;
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(
123
- t,
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),
117
+ return d.has(n) && ((e = d.get(n)) == null ? void 0 : e.length);
118
+ }, Z = (n, e) => (q(n).push(e), () => {
119
+ d.set(
120
+ n,
121
+ q(n).filter((a) => a !== e)
122
+ ), N(n) || (L.delete(n), d.delete(n));
123
+ }), _ = (n, e) => {
124
+ var t;
125
+ L.set(n, {
126
+ ...A(n),
130
127
  ...e
131
128
  });
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 = (() => {
129
+ const a = A(n);
130
+ (t = d.get(n)) == null || t.forEach((o) => o(a));
131
+ }, U = (n, e = {}) => {
132
+ const a = x(n), t = e.id ?? V(), o = m(A(t)), M = (() => {
136
133
  var g;
137
134
  const r = {
138
135
  "Content-Type": "application/json",
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;
136
+ "X-STREAM-ID": t
137
+ }, l = e.csrfToken ?? ((g = document.querySelector('meta[name="csrf-token"]')) == null ? void 0 : g.getAttribute("content"));
138
+ return l && (r["X-CSRF-TOKEN"] = l), r;
139
+ })(), R = m(o.value.data), y = m(o.value.jsonData), S = m(o.value.isFetching), h = m(o.value.isStreaming);
140
+ let u, v;
144
141
  const s = (r) => {
145
- Y(n, r);
146
- }, l = () => {
147
- a.value.controller.abort(), (b || d) && W(n), s({
142
+ _(t, r);
143
+ }, c = () => {
144
+ o.value.controller.abort(), (S || h) && G(t), s({
148
145
  isFetching: !1,
149
146
  isStreaming: !1
150
147
  });
151
- }, u = (r) => {
152
- const o = new AbortController(), g = {
148
+ }, f = (r) => {
149
+ const l = new AbortController(), g = {
153
150
  method: "POST",
154
- signal: o.signal,
151
+ signal: l.signal,
155
152
  headers: {
156
- ...i,
153
+ ...M,
157
154
  ...e.headers ?? {}
158
155
  },
159
156
  body: JSON.stringify(r ?? {}),
160
157
  credentials: e.credentials ?? "same-origin"
161
- }, k = G(n, g);
158
+ }, k = Q(t, g);
162
159
  k !== !1 && (s({
163
160
  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);
161
+ controller: l
162
+ }), fetch(a.value, k ?? g).then(async (i) => {
163
+ if (!i.ok) {
164
+ const w = await i.text();
165
+ throw new Error(w);
169
166
  }
170
- if (!c.body)
167
+ if (!i.body)
171
168
  throw new Error(
172
169
  "ReadableStream not yet supported in this browser."
173
170
  );
174
- return V(n, c), s({
171
+ return $(t, i), s({
175
172
  isFetching: !1,
176
173
  isStreaming: !0
177
- }), C(c.body.getReader());
178
- }).catch((c) => {
174
+ }), D(i.body.getReader());
175
+ }).catch((i) => {
179
176
  s({
180
177
  isFetching: !1,
181
178
  isStreaming: !1
182
- }), p(n, c), T(n);
179
+ }), P(t, i), p(t);
183
180
  }));
184
- }, y = (r) => {
185
- l(), u(r), j();
186
- }, j = () => {
181
+ }, j = (r) => {
182
+ c(), f(r), C();
183
+ }, C = () => {
187
184
  s({
188
185
  data: "",
189
186
  jsonData: null
190
187
  });
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);
188
+ }, D = async (r, l = "") => r.read().then(({ done: g, value: k }) => {
189
+ const i = new TextDecoder("utf-8").decode(k), w = l + i;
190
+ H(t, i);
194
191
  const B = {
195
- data: D
192
+ data: w
196
193
  };
197
194
  if (!g)
198
- return s(B), C(r, D);
195
+ return s(B), D(r, w);
199
196
  if (B.isStreaming = !1, e.json)
200
197
  try {
201
- B.jsonData = JSON.parse(D);
202
- } catch (I) {
203
- p(n, I);
198
+ B.jsonData = JSON.parse(w);
199
+ } catch (X) {
200
+ P(t, X);
204
201
  }
205
- return s(B), T(n), "";
202
+ return s(B), p(t), "";
206
203
  });
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();
204
+ return O(() => {
205
+ u = Z(t, (r) => {
206
+ o.value = A(t), S.value = r.isFetching, h.value = r.isStreaming, R.value = r.data, y.value = r.jsonData;
207
+ }), v = W(t, e), window.addEventListener("beforeunload", c), e.initialInput && f(e.initialInput);
208
+ }), I(() => {
209
+ u && u(), v && v(), window.removeEventListener("beforeunload", c), N(t) || c();
210
+ }), J(a, (r, l) => {
211
+ r !== l && (c(), C());
213
212
  }), {
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
213
+ data: E(R),
214
+ jsonData: E(y),
215
+ isFetching: E(S),
216
+ isStreaming: E(h),
217
+ id: t,
218
+ send: j,
219
+ cancel: c,
220
+ clearData: C
222
221
  };
223
- }, z = (t, e = {}) => {
224
- const { jsonData: n, data: a, ...i } = Z(t, {
222
+ }, ne = (n, e = {}) => {
223
+ const { jsonData: a, data: t, ...o } = U(n, {
225
224
  ...e,
226
225
  json: !0
227
226
  });
228
- return { data: n, strData: a, ...i };
227
+ return { data: a, strData: t, ...o };
229
228
  };
230
229
  export {
231
- U as useEventStream,
232
- z as useJsonStream,
233
- Z as useStream
230
+ ee as useEventStream,
231
+ ne as useJsonStream,
232
+ U as useStream
234
233
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
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"})});
1
+ (function(u,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],s):(u=typeof globalThis<"u"?globalThis:u||self,s(u.LaravelStreamVue={},u.Vue))})(this,function(u,s){"use strict";const T="data: ",x=(n,{eventName:e="update",endSignal:a="</stream>",glue:t=" ",replace:l=!1,onMessage:p=()=>null,onComplete:k=()=>null,onError:v=()=>null}={})=>{const E=s.toRef(n),g=s.ref(""),h=s.ref([]),b=Array.isArray(e)?e:[e];let o=null;const i=()=>{g.value="",h.value=[]},m=(r=!1)=>{b.forEach(c=>{o==null||o.removeEventListener(c,B)}),o==null||o.close(),o=null,r&&i()},B=r=>{if([a,`${T}${a}`].includes(r.data)){m(),k();return}l&&i(),h.value.push(r.data.startsWith(T)?r.data.substring(T.length):r.data),g.value=h.value.join(t),p(r)},C=r=>{v(r),m()},D=()=>{i(),o=new EventSource(E.value),b.forEach(r=>{o.addEventListener(r,B)}),o.addEventListener("error",C)};return s.onMounted(()=>{D()}),s.onUnmounted(()=>{m()}),s.watch(E,(r,c)=>{r!==c&&(m(),D())}),{message:s.readonly(g),messageParts:s.readonly(h),close:m,clearMessage:i}},I="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let V=(n=21)=>{let e="",a=crypto.getRandomValues(new Uint8Array(n|=0));for(;n--;)e+=I[a[n]&63];return e};const w=new Map,N=(n,e)=>{w.has(n)||w.set(n,{onData:[],onError:[],onFinish:[],onResponse:[],onCancel:[],onBeforeSend:[]});const a=w.get(n);return e.onData&&a.onData.push(e.onData),e.onError&&a.onError.push(e.onError),e.onFinish&&a.onFinish.push(e.onFinish),e.onResponse&&a.onResponse.push(e.onResponse),e.onCancel&&a.onCancel.push(e.onCancel),e.onBeforeSend&&a.onBeforeSend.push(e.onBeforeSend),()=>{G(n,e)}},y=(n,e,...a)=>{const t=w.get(n);return t?t[e].map(l=>l(...a)):[]},A=n=>{y(n,"onFinish")},P=(n,e)=>{y(n,"onError",e)},X=(n,e)=>{y(n,"onResponse",e)},K=n=>{y(n,"onCancel")},W=(n,e)=>{y(n,"onData",e)},$=(n,e)=>{const a=y(n,"onBeforeSend",e);for(const t of a){if(t===!1)return!1;if(t!==null&&typeof t=="object")return t}return null},G=(n,e)=>{const a=w.get(n);a&&(e.onData&&(a.onData=a.onData.filter(t=>t!==e.onData)),e.onError&&(a.onError=a.onError.filter(t=>t!==e.onError)),e.onFinish&&(a.onFinish=a.onFinish.filter(t=>t!==e.onFinish)),e.onResponse&&(a.onResponse=a.onResponse.filter(t=>t!==e.onResponse)),e.onCancel&&(a.onCancel=a.onCancel.filter(t=>t!==e.onCancel)),e.onBeforeSend&&(a.onBeforeSend=a.onBeforeSend.filter(t=>t!==e.onBeforeSend)))},F=new Map,d=new Map,j=n=>{const e=F.get(n);if(e)return e;const a={controller:new AbortController,data:"",isFetching:!1,isStreaming:!1,jsonData:null};return F.set(n,a),a},q=n=>(d.has(n)||d.set(n,[]),d.get(n)),O=n=>{var e;return d.has(n)&&((e=d.get(n))==null?void 0:e.length)},H=(n,e)=>(q(n).push(e),()=>{d.set(n,q(n).filter(a=>a!==e)),O(n)||(F.delete(n),d.delete(n))}),Q=(n,e)=>{var t;F.set(n,{...j(n),...e});const a=j(n);(t=d.get(n))==null||t.forEach(l=>l(a))},J=(n,e={})=>{const a=s.toRef(n),t=e.id??V(),l=s.ref(j(t)),p=(()=>{var S;const r={"Content-Type":"application/json","X-STREAM-ID":t},c=e.csrfToken??((S=document.querySelector('meta[name="csrf-token"]'))==null?void 0:S.getAttribute("content"));return c&&(r["X-CSRF-TOKEN"]=c),r})(),k=s.ref(l.value.data),v=s.ref(l.value.jsonData),E=s.ref(l.value.isFetching),g=s.ref(l.value.isStreaming);let h,b;const o=r=>{Q(t,r)},i=()=>{l.value.controller.abort(),(E||g)&&K(t),o({isFetching:!1,isStreaming:!1})},m=r=>{const c=new AbortController,S={method:"POST",signal:c.signal,headers:{...p,...e.headers??{}},body:JSON.stringify(r??{}),credentials:e.credentials??"same-origin"},L=$(t,S);L!==!1&&(o({isFetching:!0,controller:c}),fetch(a.value,L??S).then(async f=>{if(!f.ok){const R=await f.text();throw new Error(R)}if(!f.body)throw new Error("ReadableStream not yet supported in this browser.");return X(t,f),o({isFetching:!1,isStreaming:!0}),D(f.body.getReader())}).catch(f=>{o({isFetching:!1,isStreaming:!1}),P(t,f),A(t)}))},B=r=>{i(),m(r),C()},C=()=>{o({data:"",jsonData:null})},D=async(r,c="")=>r.read().then(({done:S,value:L})=>{const f=new TextDecoder("utf-8").decode(L),R=c+f;W(t,f);const M={data:R};if(!S)return o(M),D(r,R);if(M.isStreaming=!1,e.json)try{M.jsonData=JSON.parse(R)}catch(Y){P(t,Y)}return o(M),A(t),""});return s.onMounted(()=>{h=H(t,r=>{l.value=j(t),E.value=r.isFetching,g.value=r.isStreaming,k.value=r.data,v.value=r.jsonData}),b=N(t,e),window.addEventListener("beforeunload",i),e.initialInput&&m(e.initialInput)}),s.onUnmounted(()=>{h&&h(),b&&b(),window.removeEventListener("beforeunload",i),O(t)||i()}),s.watch(a,(r,c)=>{r!==c&&(i(),C())}),{data:s.readonly(k),jsonData:s.readonly(v),isFetching:s.readonly(E),isStreaming:s.readonly(g),id:t,send:B,cancel:i,clearData:C}},U=(n,e={})=>{const{jsonData:a,data:t,...l}=J(n,{...e,json:!0});return{data:a,strData:t,...l}};u.useEventStream=x,u.useJsonStream=U,u.useStream=J,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,68 +1,68 @@
1
1
  {
2
- "name": "@laravel/stream-vue",
3
- "version": "0.3.8",
4
- "description": "Laravel streaming hooks for Vue",
5
- "keywords": [
6
- "laravel",
7
- "stream",
8
- "use-stream",
9
- "server-sent-events",
10
- "sse",
11
- "vue",
12
- "composables"
13
- ],
14
- "homepage": "https://github.com/laravel/stream/tree/main/packages/vue#readme",
15
- "repository": {
16
- "type": "git",
17
- "url": "git+https://github.com/laravel/stream.git"
18
- },
19
- "bugs": {
20
- "url": "https://github.com/laravel/stream/issues"
21
- },
22
- "license": "MIT",
23
- "author": {
24
- "name": "Taylor Otwell"
25
- },
26
- "type": "module",
27
- "main": "dist/index.umd.js",
28
- "module": "dist/index.es.js",
29
- "types": "dist/index.d.ts",
30
- "exports": {
31
- ".": {
32
- "types": "./dist/index.d.ts",
33
- "import": "./dist/index.es.js",
34
- "require": "./dist/index.umd.js"
35
- }
36
- },
37
- "files": [
38
- "dist"
39
- ],
40
- "scripts": {
41
- "build": "vite build",
42
- "lint": "eslint --config eslint.config.mjs \"src/**/*.ts\"",
43
- "prepublish": "pnpm run build",
44
- "release": "vitest --run && npm publish",
45
- "test": "vitest",
46
- "format": "prettier --write ."
47
- },
48
- "devDependencies": {
49
- "@types/node": "^22.14.0",
50
- "@typescript-eslint/eslint-plugin": "^8.21.0",
51
- "@typescript-eslint/parser": "^8.21.0",
52
- "@vitejs/plugin-vue": "^5.0.0",
53
- "eslint": "^9.0.0",
54
- "jsdom": "^26.0.0",
55
- "msw": "^2.8.2",
56
- "prettier": "^3.5.3",
57
- "typescript": "^5.3.0",
58
- "vite": "^5.4.19",
59
- "vite-plugin-dts": "^4.5.3",
60
- "vitest": "^3.1.1"
61
- },
62
- "peerDependencies": {
63
- "vue": "^3.0.0"
64
- },
65
- "dependencies": {
66
- "nanoid": "^5.1.5"
2
+ "name": "@laravel/stream-vue",
3
+ "version": "0.3.10",
4
+ "description": "Laravel streaming hooks for Vue",
5
+ "keywords": [
6
+ "laravel",
7
+ "stream",
8
+ "use-stream",
9
+ "server-sent-events",
10
+ "sse",
11
+ "vue",
12
+ "composables"
13
+ ],
14
+ "homepage": "https://github.com/laravel/stream/tree/main/packages/vue#readme",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/laravel/stream.git"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/laravel/stream/issues"
21
+ },
22
+ "license": "MIT",
23
+ "author": {
24
+ "name": "Taylor Otwell"
25
+ },
26
+ "type": "module",
27
+ "main": "dist/index.umd.js",
28
+ "module": "dist/index.es.js",
29
+ "types": "dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.es.js",
34
+ "require": "./dist/index.umd.js"
67
35
  }
68
- }
36
+ },
37
+ "files": [
38
+ "dist"
39
+ ],
40
+ "devDependencies": {
41
+ "@types/node": "^22.14.0",
42
+ "@typescript-eslint/eslint-plugin": "^8.21.0",
43
+ "@typescript-eslint/parser": "^8.21.0",
44
+ "@vitejs/plugin-vue": "^5.0.0",
45
+ "eslint": "^9.0.0",
46
+ "jsdom": "^26.0.0",
47
+ "msw": "^2.8.2",
48
+ "prettier": "^3.5.3",
49
+ "typescript": "^5.3.0",
50
+ "vite": "^5.4.19",
51
+ "vite-plugin-dts": "^4.5.3",
52
+ "vitest": "^3.1.1"
53
+ },
54
+ "peerDependencies": {
55
+ "vue": "^3.3.0"
56
+ },
57
+ "dependencies": {
58
+ "nanoid": "^5.1.5"
59
+ },
60
+ "scripts": {
61
+ "build": "vite build",
62
+ "lint": "eslint --config eslint.config.mjs \"src/**/*.ts\"",
63
+ "prepublish": "pnpm run build",
64
+ "release": "vitest --run && pnpm publish --no-git-checks",
65
+ "test": "vitest",
66
+ "format": "prettier --write ."
67
+ }
68
+ }