@floegence/floe-webapp-protocol 0.15.0 → 0.15.2
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/client.d.ts +2 -25
- package/dist/index2.js +61 -160
- package/package.json +2 -2
package/dist/client.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { type JSX } from 'solid-js';
|
|
2
2
|
import type { ChannelInitGrant, Client, ClientObserverLike, DirectConnectInfo } from '@floegence/flowersec-core';
|
|
3
|
+
import { type AutoReconnectConfig, type ConnectionStatus } from '@floegence/flowersec-core/reconnect';
|
|
3
4
|
import { type ControlplaneConfig } from './controlplane';
|
|
4
5
|
import type { ProtocolContract } from './contract';
|
|
5
|
-
/**
|
|
6
|
-
* Connection status
|
|
7
|
-
*/
|
|
8
|
-
export type ConnectionStatus = 'disconnected' | 'connecting' | 'connected' | 'error';
|
|
9
6
|
interface ProtocolContextValue {
|
|
10
7
|
status: () => ConnectionStatus;
|
|
11
8
|
error: () => Error | null;
|
|
@@ -14,26 +11,7 @@ interface ProtocolContextValue {
|
|
|
14
11
|
connect: (config: ConnectConfig) => Promise<void>;
|
|
15
12
|
disconnect: () => void;
|
|
16
13
|
}
|
|
17
|
-
|
|
18
|
-
* Configuration for connecting to a flowersec server
|
|
19
|
-
*/
|
|
20
|
-
export interface AutoReconnectConfig {
|
|
21
|
-
/**
|
|
22
|
-
* Enable auto reconnect on failure / unexpected disconnect.
|
|
23
|
-
* Default: false.
|
|
24
|
-
*/
|
|
25
|
-
enabled?: boolean;
|
|
26
|
-
/** Maximum total attempts (including the first). Default: 5. */
|
|
27
|
-
maxAttempts?: number;
|
|
28
|
-
/** Base delay for the first retry. Default: 500ms. */
|
|
29
|
-
initialDelayMs?: number;
|
|
30
|
-
/** Max delay cap. Default: 10s. */
|
|
31
|
-
maxDelayMs?: number;
|
|
32
|
-
/** Exponential backoff factor. Default: 1.8. */
|
|
33
|
-
factor?: number;
|
|
34
|
-
/** Random jitter ratio in [-ratio, +ratio]. Default: 0.2. */
|
|
35
|
-
jitterRatio?: number;
|
|
36
|
-
}
|
|
14
|
+
export type { AutoReconnectConfig, ConnectionStatus };
|
|
37
15
|
export interface ConnectConfig {
|
|
38
16
|
mode?: 'tunnel' | 'direct';
|
|
39
17
|
observer?: ClientObserverLike;
|
|
@@ -57,4 +35,3 @@ export declare function ProtocolProvider(props: {
|
|
|
57
35
|
contract?: ProtocolContract;
|
|
58
36
|
}): JSX.Element;
|
|
59
37
|
export declare function useProtocol(): ProtocolContextValue;
|
|
60
|
-
export {};
|
package/dist/index2.js
CHANGED
|
@@ -1,174 +1,75 @@
|
|
|
1
|
-
import { createContext as
|
|
2
|
-
import { createStore as
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
initialDelayMs: 500,
|
|
18
|
-
maxDelayMs: 1e4,
|
|
19
|
-
factor: 1.8,
|
|
20
|
-
jitterRatio: 0.2
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
function S(a, c) {
|
|
24
|
-
const l = Math.min(c.maxDelayMs, c.initialDelayMs * Math.pow(c.factor, a)), h = c.jitterRatio <= 0 ? 0 : l * c.jitterRatio * (Math.random() * 2 - 1);
|
|
25
|
-
return Math.max(0, Math.round(l + h));
|
|
26
|
-
}
|
|
27
|
-
function _(a) {
|
|
28
|
-
const [c, l] = W({
|
|
29
|
-
status: "disconnected",
|
|
30
|
-
error: null,
|
|
31
|
-
client: null
|
|
32
|
-
}), h = a.contract ?? I;
|
|
33
|
-
let s = 0, i = null, u = null, m = null;
|
|
34
|
-
const p = () => {
|
|
35
|
-
u && (clearTimeout(u), u = null), m?.(), m = null;
|
|
36
|
-
}, v = (n) => new Promise((t) => {
|
|
37
|
-
m = t, u = setTimeout(() => {
|
|
38
|
-
u = null, m = null, t();
|
|
39
|
-
}, n);
|
|
40
|
-
}), w = () => {
|
|
41
|
-
p(), i = null, s += 1, c.client && c.client.close(), l({
|
|
42
|
-
status: "disconnected",
|
|
43
|
-
error: null,
|
|
44
|
-
client: null
|
|
45
|
-
});
|
|
46
|
-
}, y = (n, t, r) => {
|
|
47
|
-
if (n !== s || i !== t || c.status !== "connected") return;
|
|
48
|
-
if (!R(t.autoReconnect).enabled) {
|
|
49
|
-
l({
|
|
50
|
-
status: "error",
|
|
51
|
-
error: r,
|
|
52
|
-
client: null
|
|
53
|
-
});
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
p(), s += 1;
|
|
57
|
-
const o = s;
|
|
58
|
-
l({
|
|
59
|
-
status: "connecting",
|
|
60
|
-
error: r,
|
|
61
|
-
client: null
|
|
62
|
-
}), x(o, t).catch(() => {
|
|
1
|
+
import { createContext as h, onCleanup as v, createComponent as P, useContext as b } from "solid-js";
|
|
2
|
+
import { createStore as C } from "solid-js/store";
|
|
3
|
+
import { createReconnectManager as M } from "@floegence/flowersec-core/reconnect";
|
|
4
|
+
import { requestChannelGrant as T } from "./index4.js";
|
|
5
|
+
import { redevenV1Contract as x } from "./index6.js";
|
|
6
|
+
const s = h();
|
|
7
|
+
function B(r) {
|
|
8
|
+
const e = M(), [n, a] = C({
|
|
9
|
+
status: e.state().status,
|
|
10
|
+
error: e.state().error,
|
|
11
|
+
client: e.state().client
|
|
12
|
+
}), i = r.contract ?? x, u = e.subscribe((t) => {
|
|
13
|
+
a({
|
|
14
|
+
status: t.status,
|
|
15
|
+
error: t.error,
|
|
16
|
+
client: t.client
|
|
63
17
|
});
|
|
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
|
-
throw new Error("Direct mode requires `directInfo`");
|
|
97
|
-
return e(t.directInfo, o);
|
|
98
|
-
}, x = async (n, t) => {
|
|
99
|
-
const r = R(t.autoReconnect);
|
|
100
|
-
let e = 0;
|
|
101
|
-
for (; ; ) {
|
|
102
|
-
if (n !== s || i !== t) return;
|
|
103
|
-
e += 1;
|
|
104
|
-
try {
|
|
105
|
-
const o = await b(n, t);
|
|
106
|
-
if (n !== s) {
|
|
107
|
-
o.close();
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
if (i !== t) {
|
|
111
|
-
o.close();
|
|
112
|
-
return;
|
|
18
|
+
}), l = {
|
|
19
|
+
status: () => n.status,
|
|
20
|
+
error: () => n.error,
|
|
21
|
+
client: () => n.client,
|
|
22
|
+
contract: () => i,
|
|
23
|
+
connect: async (t) => {
|
|
24
|
+
await e.connect({
|
|
25
|
+
autoReconnect: t.autoReconnect,
|
|
26
|
+
observer: t.observer,
|
|
27
|
+
connectOnce: async ({
|
|
28
|
+
signal: m,
|
|
29
|
+
observer: d
|
|
30
|
+
}) => {
|
|
31
|
+
const {
|
|
32
|
+
connectTunnelBrowser: p,
|
|
33
|
+
connectDirectBrowser: w
|
|
34
|
+
} = await import("@floegence/flowersec-core/browser"), o = {
|
|
35
|
+
observer: d,
|
|
36
|
+
signal: m,
|
|
37
|
+
keepaliveIntervalMs: t.keepaliveIntervalMs ?? 15e3,
|
|
38
|
+
connectTimeoutMs: t.connectTimeoutMs ?? 1e4,
|
|
39
|
+
handshakeTimeoutMs: t.handshakeTimeoutMs ?? 1e4
|
|
40
|
+
};
|
|
41
|
+
if ((t.mode ?? "tunnel") === "tunnel") {
|
|
42
|
+
const c = (t.getGrant ? await t.getGrant() : null) ?? t.grant ?? (t.controlplane ? await T(t.controlplane) : null);
|
|
43
|
+
if (!c)
|
|
44
|
+
throw new Error("Tunnel mode requires `getGrant`, `grant`, or `controlplane` config");
|
|
45
|
+
return p(c, o);
|
|
46
|
+
}
|
|
47
|
+
if (!t.directInfo)
|
|
48
|
+
throw new Error("Direct mode requires `directInfo`");
|
|
49
|
+
return w(t.directInfo, o);
|
|
113
50
|
}
|
|
114
|
-
|
|
115
|
-
status: "connected",
|
|
116
|
-
client: o,
|
|
117
|
-
error: null
|
|
118
|
-
});
|
|
119
|
-
return;
|
|
120
|
-
} catch (o) {
|
|
121
|
-
const d = o instanceof Error ? o : new Error(String(o));
|
|
122
|
-
if (n !== s || i !== t) return;
|
|
123
|
-
if (!(r.enabled && e < r.maxAttempts))
|
|
124
|
-
throw l({
|
|
125
|
-
status: "error",
|
|
126
|
-
error: d,
|
|
127
|
-
client: null
|
|
128
|
-
}), d;
|
|
129
|
-
l({
|
|
130
|
-
status: "connecting",
|
|
131
|
-
error: d,
|
|
132
|
-
client: null
|
|
133
|
-
});
|
|
134
|
-
const T = S(e - 1, r);
|
|
135
|
-
await v(T);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}, D = {
|
|
139
|
-
status: () => c.status,
|
|
140
|
-
error: () => c.error,
|
|
141
|
-
client: () => c.client,
|
|
142
|
-
contract: () => h,
|
|
143
|
-
connect: async (n) => {
|
|
144
|
-
p(), s += 1;
|
|
145
|
-
const t = s;
|
|
146
|
-
i = n, c.client && c.client.close(), l({
|
|
147
|
-
status: "connecting",
|
|
148
|
-
error: null,
|
|
149
|
-
client: null
|
|
150
|
-
}), await x(t, n);
|
|
51
|
+
});
|
|
151
52
|
},
|
|
152
53
|
disconnect: () => {
|
|
153
|
-
|
|
54
|
+
e.disconnect();
|
|
154
55
|
}
|
|
155
56
|
};
|
|
156
|
-
return
|
|
157
|
-
|
|
158
|
-
}),
|
|
159
|
-
value:
|
|
57
|
+
return v(() => {
|
|
58
|
+
u(), e.disconnect();
|
|
59
|
+
}), P(s.Provider, {
|
|
60
|
+
value: l,
|
|
160
61
|
get children() {
|
|
161
|
-
return
|
|
62
|
+
return r.children;
|
|
162
63
|
}
|
|
163
64
|
});
|
|
164
65
|
}
|
|
165
|
-
function
|
|
166
|
-
const
|
|
167
|
-
if (!
|
|
66
|
+
function D() {
|
|
67
|
+
const r = b(s);
|
|
68
|
+
if (!r)
|
|
168
69
|
throw new Error("useProtocol must be used within a ProtocolProvider");
|
|
169
|
-
return
|
|
70
|
+
return r;
|
|
170
71
|
}
|
|
171
72
|
export {
|
|
172
|
-
|
|
173
|
-
|
|
73
|
+
B as ProtocolProvider,
|
|
74
|
+
D as useProtocol
|
|
174
75
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@floegence/floe-webapp-protocol",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"solid-js": "^1.8.0"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@floegence/flowersec-core": "^0.
|
|
27
|
+
"@floegence/flowersec-core": "^0.9.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"solid-js": "^1.9.11",
|