@laravel/stream-vue 0.3.1 → 0.3.3
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 +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.es.js +42 -41
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ 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;
|
|
@@ -50,7 +51,7 @@ export declare const useJsonStream: <TJsonData = null>(url: string, options?: Om
|
|
|
50
51
|
cancel: () => void;
|
|
51
52
|
clearData: () => void;
|
|
52
53
|
data: Readonly<TJsonData | null>;
|
|
53
|
-
|
|
54
|
+
strData: Readonly<Ref<string, string>>;
|
|
54
55
|
};
|
|
55
56
|
|
|
56
57
|
export declare const useStream: <TJsonData = null>(url: string, options?: StreamOptions) => {
|
package/dist/index.es.js
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import { ref as S, onMounted as x, onUnmounted as O, watch as q, readonly as
|
|
1
|
+
import { ref as S, onMounted as x, onUnmounted as O, watch as q, readonly as v } from "vue";
|
|
2
2
|
const M = "data: ", $ = (t, {
|
|
3
3
|
eventName: e = "update",
|
|
4
4
|
endSignal: r = "</stream>",
|
|
5
5
|
glue: l = " ",
|
|
6
|
-
replace:
|
|
6
|
+
replace: w = !1,
|
|
7
7
|
onMessage: b = () => null,
|
|
8
8
|
onComplete: D = () => null,
|
|
9
9
|
onError: E = () => null
|
|
10
10
|
} = {}) => {
|
|
11
11
|
const d = S(""), f = S([]), u = Array.isArray(e) ? e : [e];
|
|
12
12
|
let c = null;
|
|
13
|
-
const
|
|
13
|
+
const g = () => {
|
|
14
14
|
d.value = "", f.value = [];
|
|
15
|
-
},
|
|
15
|
+
}, h = (a = !1) => {
|
|
16
16
|
u.forEach((s) => {
|
|
17
17
|
c == null || c.removeEventListener(s, y);
|
|
18
|
-
}), c == null || c.close(), c = null, a &&
|
|
18
|
+
}), c == null || c.close(), c = null, a && g();
|
|
19
19
|
}, y = (a) => {
|
|
20
20
|
if ([r, `${M}${r}`].includes(a.data)) {
|
|
21
|
-
|
|
21
|
+
h(), D();
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
w && g(), f.value.push(
|
|
25
25
|
a.data.startsWith(M) ? a.data.substring(M.length) : a.data
|
|
26
26
|
), d.value = f.value.join(l), b(a);
|
|
27
27
|
}, j = (a) => {
|
|
28
|
-
E(a),
|
|
28
|
+
E(a), h();
|
|
29
29
|
}, n = () => {
|
|
30
|
-
|
|
30
|
+
g(), c = new EventSource(t), u.forEach((a) => {
|
|
31
31
|
c.addEventListener(a, y);
|
|
32
32
|
}), c.addEventListener("error", j);
|
|
33
33
|
};
|
|
34
34
|
return x(() => {
|
|
35
35
|
n();
|
|
36
36
|
}), O(() => {
|
|
37
|
-
|
|
37
|
+
h();
|
|
38
38
|
}), q(
|
|
39
39
|
() => t,
|
|
40
40
|
(a, s) => {
|
|
41
|
-
a !== s && (
|
|
41
|
+
a !== s && (h(), n());
|
|
42
42
|
}
|
|
43
43
|
), {
|
|
44
|
-
message:
|
|
45
|
-
messageParts:
|
|
46
|
-
close:
|
|
47
|
-
clearMessage:
|
|
44
|
+
message: v(d),
|
|
45
|
+
messageParts: v(f),
|
|
46
|
+
close: h,
|
|
47
|
+
clearMessage: g
|
|
48
48
|
};
|
|
49
49
|
}, N = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
50
50
|
let X = (t = 21) => {
|
|
@@ -53,7 +53,7 @@ let X = (t = 21) => {
|
|
|
53
53
|
e += N[r[t] & 63];
|
|
54
54
|
return e;
|
|
55
55
|
};
|
|
56
|
-
const A = /* @__PURE__ */ new Map(),
|
|
56
|
+
const A = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), L = (t) => {
|
|
57
57
|
const e = A.get(t);
|
|
58
58
|
if (e)
|
|
59
59
|
return e;
|
|
@@ -65,16 +65,16 @@ const A = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), L = (t) => {
|
|
|
65
65
|
jsonData: null
|
|
66
66
|
};
|
|
67
67
|
return A.set(t, r), r;
|
|
68
|
-
}, P = (t) => (
|
|
68
|
+
}, P = (t) => (o.has(t) || o.set(t, []), o.get(t)), I = (t) => {
|
|
69
69
|
var e;
|
|
70
|
-
return
|
|
70
|
+
return o.has(t) && ((e = o.get(t)) == null ? void 0 : e.length);
|
|
71
71
|
}, K = (t, e) => (P(t).push(e), () => {
|
|
72
|
-
|
|
72
|
+
o.set(
|
|
73
73
|
t,
|
|
74
74
|
P(t).filter((r) => r !== e)
|
|
75
|
-
), I(t) || (A.delete(t),
|
|
75
|
+
), I(t) || (A.delete(t), o.delete(t));
|
|
76
76
|
}), V = (t, e = {}) => {
|
|
77
|
-
const r = e.id ?? X(), l = S(L(r)),
|
|
77
|
+
const r = e.id ?? X(), l = S(L(r)), w = (() => {
|
|
78
78
|
var s;
|
|
79
79
|
const n = {
|
|
80
80
|
"Content-Type": "application/json",
|
|
@@ -90,14 +90,14 @@ const A = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), L = (t) => {
|
|
|
90
90
|
...n
|
|
91
91
|
});
|
|
92
92
|
const a = L(r);
|
|
93
|
-
(s =
|
|
93
|
+
(s = o.get(r)) == null || s.forEach((i) => i(a));
|
|
94
94
|
}, c = () => {
|
|
95
95
|
var n;
|
|
96
96
|
l.value.controller.abort(), (E || d) && ((n = e.onCancel) == null || n.call(e)), u({
|
|
97
97
|
isFetching: !1,
|
|
98
98
|
isStreaming: !1
|
|
99
99
|
});
|
|
100
|
-
},
|
|
100
|
+
}, g = (n = {}) => {
|
|
101
101
|
const a = new AbortController();
|
|
102
102
|
u({
|
|
103
103
|
isFetching: !0,
|
|
@@ -106,12 +106,13 @@ const A = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), L = (t) => {
|
|
|
106
106
|
method: "POST",
|
|
107
107
|
signal: a.signal,
|
|
108
108
|
headers: {
|
|
109
|
-
...
|
|
109
|
+
...w,
|
|
110
110
|
...e.headers ?? {}
|
|
111
111
|
},
|
|
112
|
-
body: JSON.stringify(n)
|
|
112
|
+
body: JSON.stringify(n),
|
|
113
|
+
credentials: e.credentials ?? "same-origin"
|
|
113
114
|
}).then(async (s) => {
|
|
114
|
-
var
|
|
115
|
+
var i;
|
|
115
116
|
if (!s.ok) {
|
|
116
117
|
const m = await s.text();
|
|
117
118
|
throw new Error(m);
|
|
@@ -120,27 +121,27 @@ const A = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), L = (t) => {
|
|
|
120
121
|
throw new Error(
|
|
121
122
|
"ReadableStream not yet supported in this browser."
|
|
122
123
|
);
|
|
123
|
-
return (
|
|
124
|
+
return (i = e.onResponse) == null || i.call(e, s), u({
|
|
124
125
|
isFetching: !1,
|
|
125
126
|
isStreaming: !0
|
|
126
127
|
}), j(s.body.getReader());
|
|
127
128
|
}).catch((s) => {
|
|
128
|
-
var
|
|
129
|
+
var i, m;
|
|
129
130
|
u({
|
|
130
131
|
isFetching: !1,
|
|
131
132
|
isStreaming: !1
|
|
132
|
-
}), (
|
|
133
|
+
}), (i = e.onError) == null || i.call(e, s), (m = e.onFinish) == null || m.call(e);
|
|
133
134
|
});
|
|
134
|
-
},
|
|
135
|
-
c(),
|
|
135
|
+
}, h = (n) => {
|
|
136
|
+
c(), g(n), y();
|
|
136
137
|
}, y = () => {
|
|
137
138
|
u({
|
|
138
139
|
data: "",
|
|
139
140
|
jsonData: null
|
|
140
141
|
});
|
|
141
|
-
}, j = (n, a = "") => n.read().then(({ done: s, value:
|
|
142
|
+
}, j = (n, a = "") => n.read().then(({ done: s, value: i }) => {
|
|
142
143
|
var R, T, k;
|
|
143
|
-
const m = new TextDecoder("utf-8").decode(
|
|
144
|
+
const m = new TextDecoder("utf-8").decode(i), C = a + m;
|
|
144
145
|
(R = e.onData) == null || R.call(e, m);
|
|
145
146
|
const F = {
|
|
146
147
|
data: C
|
|
@@ -158,25 +159,25 @@ const A = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), L = (t) => {
|
|
|
158
159
|
return x(() => {
|
|
159
160
|
f = K(r, (n) => {
|
|
160
161
|
l.value = L(r), E.value = n.isFetching, d.value = n.isStreaming, b.value = n.data, D.value = n.jsonData;
|
|
161
|
-
}), window.addEventListener("beforeunload", c), e.initialInput &&
|
|
162
|
+
}), window.addEventListener("beforeunload", c), e.initialInput && g(e.initialInput);
|
|
162
163
|
}), O(() => {
|
|
163
164
|
f(), window.removeEventListener("beforeunload", c), I(r) || c();
|
|
164
165
|
}), {
|
|
165
|
-
data:
|
|
166
|
-
jsonData:
|
|
167
|
-
isFetching:
|
|
168
|
-
isStreaming:
|
|
166
|
+
data: v(b),
|
|
167
|
+
jsonData: v(D),
|
|
168
|
+
isFetching: v(E),
|
|
169
|
+
isStreaming: v(d),
|
|
169
170
|
id: r,
|
|
170
|
-
send:
|
|
171
|
+
send: h,
|
|
171
172
|
cancel: c,
|
|
172
173
|
clearData: y
|
|
173
174
|
};
|
|
174
175
|
}, B = (t, e = {}) => {
|
|
175
|
-
const { jsonData: r, data: l, ...
|
|
176
|
+
const { jsonData: r, data: l, ...w } = V(t, {
|
|
176
177
|
...e,
|
|
177
178
|
json: !0
|
|
178
179
|
});
|
|
179
|
-
return { data: r,
|
|
180
|
+
return { data: r, strData: l, ...w };
|
|
180
181
|
};
|
|
181
182
|
export {
|
|
182
183
|
$ as useEventStream,
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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"})});
|