@larose-ui/network 0.1.0
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/LICENSE +21 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +144 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 laRose contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { NetworkCondition } from '@larose-ui/core';
|
|
2
|
+
|
|
3
|
+
interface NetworkState {
|
|
4
|
+
condition: NetworkCondition;
|
|
5
|
+
online: boolean;
|
|
6
|
+
effectiveType?: string;
|
|
7
|
+
downlink?: number;
|
|
8
|
+
rtt?: number;
|
|
9
|
+
/** Consecutive request failures in the current window */
|
|
10
|
+
failureCount?: number;
|
|
11
|
+
}
|
|
12
|
+
interface NetworkMonitorOptions {
|
|
13
|
+
slowThresholdMs?: number;
|
|
14
|
+
pollIntervalMs?: number;
|
|
15
|
+
/** Failures within window before condition becomes `failed` */
|
|
16
|
+
failureThreshold?: number;
|
|
17
|
+
/** How long `recovering` lasts after reconnect (ms) */
|
|
18
|
+
recoveringDurationMs?: number;
|
|
19
|
+
}
|
|
20
|
+
type NetworkListener = (state: NetworkState) => void;
|
|
21
|
+
declare class NetworkMonitor {
|
|
22
|
+
private options;
|
|
23
|
+
private listeners;
|
|
24
|
+
private state;
|
|
25
|
+
private pollTimer?;
|
|
26
|
+
private recoveringTimer?;
|
|
27
|
+
private wasOffline;
|
|
28
|
+
private recoveringUntil;
|
|
29
|
+
private failureCount;
|
|
30
|
+
private onOnline;
|
|
31
|
+
private onOffline;
|
|
32
|
+
private onConnectionChange;
|
|
33
|
+
constructor(options?: NetworkMonitorOptions);
|
|
34
|
+
get current(): NetworkState;
|
|
35
|
+
subscribe(listener: NetworkListener): () => void;
|
|
36
|
+
/** Report a failed network/API request */
|
|
37
|
+
reportFailure(): void;
|
|
38
|
+
/** Report a successful request — clears failure streak */
|
|
39
|
+
reportSuccess(): void;
|
|
40
|
+
destroy(): void;
|
|
41
|
+
private handleOnline;
|
|
42
|
+
private readState;
|
|
43
|
+
private setCondition;
|
|
44
|
+
private update;
|
|
45
|
+
private attach;
|
|
46
|
+
}
|
|
47
|
+
declare function createNetworkMonitor(options?: NetworkMonitorOptions): NetworkMonitor;
|
|
48
|
+
declare function isOnlineNetwork(condition: NetworkCondition): boolean;
|
|
49
|
+
declare function isSlowNetwork(condition: NetworkCondition): boolean;
|
|
50
|
+
declare function isDegradedNetwork(condition: NetworkCondition): boolean;
|
|
51
|
+
declare function shouldUseSkeleton(condition: NetworkCondition): boolean;
|
|
52
|
+
/** @deprecated Use isOnlineNetwork — `online` condition alias for `fast` */
|
|
53
|
+
declare function normalizeNetworkCondition(condition: NetworkCondition): NetworkCondition;
|
|
54
|
+
|
|
55
|
+
export { NetworkMonitor, type NetworkMonitorOptions, type NetworkState, createNetworkMonitor, isDegradedNetwork, isOnlineNetwork, isSlowNetwork, normalizeNetworkCondition, shouldUseSkeleton };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function getConnection() {
|
|
3
|
+
if (typeof navigator === "undefined") return void 0;
|
|
4
|
+
return navigator.connection;
|
|
5
|
+
}
|
|
6
|
+
function resolveBaseCondition(online, connection, slowThresholdMs = 2e3) {
|
|
7
|
+
if (!online) return "offline";
|
|
8
|
+
const rtt = connection?.rtt;
|
|
9
|
+
const effectiveType = connection?.effectiveType;
|
|
10
|
+
if (rtt !== void 0 && rtt >= slowThresholdMs) return "high-latency";
|
|
11
|
+
if (effectiveType === "slow-2g" || effectiveType === "2g") return "slow";
|
|
12
|
+
if (effectiveType === "3g") return "intermittent";
|
|
13
|
+
return "fast";
|
|
14
|
+
}
|
|
15
|
+
var NetworkMonitor = class {
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
this.options = options;
|
|
18
|
+
this.state = this.readState();
|
|
19
|
+
this.attach();
|
|
20
|
+
}
|
|
21
|
+
options;
|
|
22
|
+
listeners = /* @__PURE__ */ new Set();
|
|
23
|
+
state;
|
|
24
|
+
pollTimer;
|
|
25
|
+
recoveringTimer;
|
|
26
|
+
wasOffline = false;
|
|
27
|
+
recoveringUntil = 0;
|
|
28
|
+
failureCount = 0;
|
|
29
|
+
onOnline = () => this.handleOnline();
|
|
30
|
+
onOffline = () => this.update();
|
|
31
|
+
onConnectionChange = () => this.update();
|
|
32
|
+
get current() {
|
|
33
|
+
return this.state;
|
|
34
|
+
}
|
|
35
|
+
subscribe(listener) {
|
|
36
|
+
this.listeners.add(listener);
|
|
37
|
+
listener(this.state);
|
|
38
|
+
return () => this.listeners.delete(listener);
|
|
39
|
+
}
|
|
40
|
+
/** Report a failed network/API request */
|
|
41
|
+
reportFailure() {
|
|
42
|
+
const threshold = this.options.failureThreshold ?? 3;
|
|
43
|
+
this.failureCount += 1;
|
|
44
|
+
if (this.failureCount >= threshold && this.state.online) {
|
|
45
|
+
this.setCondition("failed");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Report a successful request — clears failure streak */
|
|
49
|
+
reportSuccess() {
|
|
50
|
+
this.failureCount = 0;
|
|
51
|
+
if (this.state.condition === "failed") {
|
|
52
|
+
this.update();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
destroy() {
|
|
56
|
+
if (typeof window === "undefined") return;
|
|
57
|
+
window.removeEventListener("online", this.onOnline);
|
|
58
|
+
window.removeEventListener("offline", this.onOffline);
|
|
59
|
+
const connection = getConnection();
|
|
60
|
+
connection?.removeEventListener?.("change", this.onConnectionChange);
|
|
61
|
+
if (this.pollTimer) clearInterval(this.pollTimer);
|
|
62
|
+
if (this.recoveringTimer) clearTimeout(this.recoveringTimer);
|
|
63
|
+
this.listeners.clear();
|
|
64
|
+
}
|
|
65
|
+
handleOnline() {
|
|
66
|
+
if (this.wasOffline) {
|
|
67
|
+
this.recoveringUntil = Date.now() + (this.options.recoveringDurationMs ?? 5e3);
|
|
68
|
+
if (this.recoveringTimer) clearTimeout(this.recoveringTimer);
|
|
69
|
+
this.recoveringTimer = setTimeout(() => this.update(), this.options.recoveringDurationMs ?? 5e3);
|
|
70
|
+
}
|
|
71
|
+
this.wasOffline = false;
|
|
72
|
+
this.update();
|
|
73
|
+
}
|
|
74
|
+
readState() {
|
|
75
|
+
const online = typeof navigator === "undefined" ? true : navigator.onLine;
|
|
76
|
+
const connection = getConnection();
|
|
77
|
+
const base = resolveBaseCondition(
|
|
78
|
+
online,
|
|
79
|
+
connection,
|
|
80
|
+
this.options.slowThresholdMs
|
|
81
|
+
);
|
|
82
|
+
let condition = base;
|
|
83
|
+
if (!online) {
|
|
84
|
+
this.wasOffline = true;
|
|
85
|
+
condition = "offline";
|
|
86
|
+
} else if (Date.now() < this.recoveringUntil) {
|
|
87
|
+
condition = "recovering";
|
|
88
|
+
} else if (this.failureCount >= (this.options.failureThreshold ?? 3)) {
|
|
89
|
+
condition = "failed";
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
online,
|
|
93
|
+
effectiveType: connection?.effectiveType,
|
|
94
|
+
downlink: connection?.downlink,
|
|
95
|
+
rtt: connection?.rtt,
|
|
96
|
+
failureCount: this.failureCount,
|
|
97
|
+
condition
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
setCondition(condition) {
|
|
101
|
+
this.state = { ...this.state, condition };
|
|
102
|
+
this.listeners.forEach((l) => l(this.state));
|
|
103
|
+
}
|
|
104
|
+
update() {
|
|
105
|
+
this.state = this.readState();
|
|
106
|
+
this.listeners.forEach((l) => l(this.state));
|
|
107
|
+
}
|
|
108
|
+
attach() {
|
|
109
|
+
if (typeof window === "undefined") return;
|
|
110
|
+
window.addEventListener("online", this.onOnline);
|
|
111
|
+
window.addEventListener("offline", this.onOffline);
|
|
112
|
+
const connection = getConnection();
|
|
113
|
+
connection?.addEventListener?.("change", this.onConnectionChange);
|
|
114
|
+
const interval = this.options.pollIntervalMs ?? 3e4;
|
|
115
|
+
this.pollTimer = setInterval(() => this.update(), interval);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
function createNetworkMonitor(options) {
|
|
119
|
+
return new NetworkMonitor(options);
|
|
120
|
+
}
|
|
121
|
+
function isOnlineNetwork(condition) {
|
|
122
|
+
return condition === "online" || condition === "fast" || condition === "recovering";
|
|
123
|
+
}
|
|
124
|
+
function isSlowNetwork(condition) {
|
|
125
|
+
return condition === "slow" || condition === "intermittent" || condition === "high-latency";
|
|
126
|
+
}
|
|
127
|
+
function isDegradedNetwork(condition) {
|
|
128
|
+
return isSlowNetwork(condition) || condition === "failed" || condition === "offline";
|
|
129
|
+
}
|
|
130
|
+
function shouldUseSkeleton(condition) {
|
|
131
|
+
return isSlowNetwork(condition) || condition === "recovering";
|
|
132
|
+
}
|
|
133
|
+
function normalizeNetworkCondition(condition) {
|
|
134
|
+
return condition === "online" ? "fast" : condition;
|
|
135
|
+
}
|
|
136
|
+
export {
|
|
137
|
+
NetworkMonitor,
|
|
138
|
+
createNetworkMonitor,
|
|
139
|
+
isDegradedNetwork,
|
|
140
|
+
isOnlineNetwork,
|
|
141
|
+
isSlowNetwork,
|
|
142
|
+
normalizeNetworkCondition,
|
|
143
|
+
shouldUseSkeleton
|
|
144
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@larose-ui/network",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Network condition detection for laRose UI platform",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@larose-ui/core": "0.1.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"tsup": "^8.3.5",
|
|
23
|
+
"typescript": "^5.7.2",
|
|
24
|
+
"vitest": "^2.1.8"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/larose-ui/larose.git",
|
|
33
|
+
"directory": "packages/network"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"larose",
|
|
37
|
+
"react",
|
|
38
|
+
"ui-platform",
|
|
39
|
+
"design-system",
|
|
40
|
+
"saas"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
44
|
+
"dev": "tsup src/index.ts --format esm --dts --watch",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"clean": "rm -rf dist"
|
|
48
|
+
}
|
|
49
|
+
}
|