network-speed-js 1.0.0 → 1.0.1
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 +9 -2
- package/dist/core/performance-utils.d.ts +21 -0
- package/dist/core/performance-utils.d.ts.map +1 -0
- package/dist/core/sdk.d.ts +34 -0
- package/dist/core/sdk.d.ts.map +1 -0
- package/dist/core/speed-tester.d.ts +34 -0
- package/dist/core/speed-tester.d.ts.map +1 -0
- package/dist/core/types.d.ts +59 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/network-speed-js.js +53 -45
- package/dist/network-speed-js.umd.js +1 -1
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<div align="center">
|
|
4
4
|
|
|
5
|
-
一个基于 **Performance API** 的现代化网速测试 SDK,支持内外网自动检测、资源监听和完整的
|
|
5
|
+
一个基于 **Performance API** 的现代化网速测试 SDK,支持内外网自动检测、资源监听和完整的 TS 类型支持。
|
|
6
6
|
|
|
7
7
|
**框架无关 · 开箱即用 · 准确可靠**
|
|
8
8
|
|
|
@@ -471,7 +471,7 @@ location /speed-test.bin {
|
|
|
471
471
|
add_header Pragma "no-cache";
|
|
472
472
|
add_header Expires "0";
|
|
473
473
|
|
|
474
|
-
#
|
|
474
|
+
# 设置内容类型(根据实际文件类型调整)
|
|
475
475
|
add_header Content-Type "application/octet-stream";
|
|
476
476
|
|
|
477
477
|
# 启用 CORS
|
|
@@ -489,10 +489,17 @@ dd if=/dev/urandom of=speed-test.bin bs=1024 count=500
|
|
|
489
489
|
|
|
490
490
|
**测速文件建议:**
|
|
491
491
|
- 文件大小:200KB ~ 1MB
|
|
492
|
+
- 文件类型:任意(.bin、.jpg、.png、.json、.txt 等)
|
|
492
493
|
- 禁用缓存
|
|
493
494
|
- 启用 CORS
|
|
494
495
|
- 使用 CDN 分发
|
|
495
496
|
|
|
497
|
+
**支持的资源类型:**
|
|
498
|
+
- ✅ 二进制文件(.bin)
|
|
499
|
+
- ✅ 图片文件(.jpg、.png、.webp)
|
|
500
|
+
- ✅ 文本文件(.txt、.json)
|
|
501
|
+
- ✅ 任何可通过 HTTP 访问的资源
|
|
502
|
+
|
|
496
503
|
### 配置项详解
|
|
497
504
|
|
|
498
505
|
#### intranetUrl
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ResourceSpeedInfo } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 计算单个资源的下载速度
|
|
4
|
+
*/
|
|
5
|
+
export declare function calcSpeedByResource(entry: PerformanceResourceTiming): ResourceSpeedInfo | null;
|
|
6
|
+
/**
|
|
7
|
+
* 获取所有资源的测速信息
|
|
8
|
+
*/
|
|
9
|
+
export declare function getAllResourcesSpeeds(): ResourceSpeedInfo[];
|
|
10
|
+
/**
|
|
11
|
+
* 清除指定URL的性能记录
|
|
12
|
+
*/
|
|
13
|
+
export declare function clearPerformanceEntry(url: string): void;
|
|
14
|
+
/**
|
|
15
|
+
* 评估网络类型
|
|
16
|
+
*/
|
|
17
|
+
export declare function evaluateNetworkType(speedMbps: number, thresholds?: {
|
|
18
|
+
fast: number;
|
|
19
|
+
medium: number;
|
|
20
|
+
}): 'fast' | 'medium' | 'slow' | 'unknown';
|
|
21
|
+
//# sourceMappingURL=performance-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"performance-utils.d.ts","sourceRoot":"","sources":["../../src/core/performance-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,yBAAyB,GAC/B,iBAAiB,GAAG,IAAI,CAoB1B;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,iBAAiB,EAAE,CAU3D;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAKvD;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,MAAM,EACjB,UAAU;;;CAA0B,GACnC,MAAM,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CASxC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { SpeedTestOptions, SpeedTestResult, ResourceSpeedInfo } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 网速测试 SDK
|
|
4
|
+
*/
|
|
5
|
+
export declare class NetworkSpeedSDK {
|
|
6
|
+
private tester;
|
|
7
|
+
constructor(options?: SpeedTestOptions);
|
|
8
|
+
/**
|
|
9
|
+
* 执行网速测试
|
|
10
|
+
*/
|
|
11
|
+
test(): Promise<SpeedTestResult>;
|
|
12
|
+
/**
|
|
13
|
+
* 获取所有已加载资源的速度信息
|
|
14
|
+
*/
|
|
15
|
+
getAllResourcesSpeeds(): ResourceSpeedInfo[];
|
|
16
|
+
/**
|
|
17
|
+
* 监听特定资源的性能数据
|
|
18
|
+
*/
|
|
19
|
+
observeResource(urlPattern: string, callback: (entry: PerformanceResourceTiming) => void): () => void;
|
|
20
|
+
/**
|
|
21
|
+
* 更新配置
|
|
22
|
+
*/
|
|
23
|
+
updateOptions(options: Partial<SpeedTestOptions>): void;
|
|
24
|
+
/**
|
|
25
|
+
* 销毁SDK实例
|
|
26
|
+
*/
|
|
27
|
+
destroy(): void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 创建SDK实例的工厂函数
|
|
31
|
+
*/
|
|
32
|
+
export declare function createNetworkSpeedSDK(options?: SpeedTestOptions): NetworkSpeedSDK;
|
|
33
|
+
export default NetworkSpeedSDK;
|
|
34
|
+
//# sourceMappingURL=sdk.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sdk.d.ts","sourceRoot":"","sources":["../../src/core/sdk.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEpF;;GAEG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,MAAM,CAAc;gBAEhB,OAAO,GAAE,gBAAqB;IAI1C;;OAEG;IACG,IAAI,IAAI,OAAO,CAAC,eAAe,CAAC;IAItC;;OAEG;IACH,qBAAqB,IAAI,iBAAiB,EAAE;IAI5C;;OAEG;IACH,eAAe,CACb,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,CAAC,KAAK,EAAE,yBAAyB,KAAK,IAAI,GACnD,MAAM,IAAI;IAIb;;OAEG;IACH,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,IAAI;IAIvD;;OAEG;IACH,OAAO,IAAI,IAAI;CAGhB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,CAAC,EAAE,gBAAgB,GAAG,eAAe,CAEjF;AAGD,eAAe,eAAe,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { SpeedTestResult, SpeedTestOptions, PerformanceEntryCallback } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 网速测试核心类
|
|
4
|
+
*/
|
|
5
|
+
export declare class SpeedTester {
|
|
6
|
+
private options;
|
|
7
|
+
private observer;
|
|
8
|
+
constructor(options?: SpeedTestOptions);
|
|
9
|
+
/**
|
|
10
|
+
* 执行测速
|
|
11
|
+
*/
|
|
12
|
+
test(): Promise<SpeedTestResult>;
|
|
13
|
+
/**
|
|
14
|
+
* 自动检测内外网并测速
|
|
15
|
+
*/
|
|
16
|
+
private testWithAutoDetect;
|
|
17
|
+
/**
|
|
18
|
+
* 测试单个URL
|
|
19
|
+
*/
|
|
20
|
+
private testSingleUrl;
|
|
21
|
+
/**
|
|
22
|
+
* 监听特定资源的性能数据
|
|
23
|
+
*/
|
|
24
|
+
observeResource(urlPattern: string, callback: PerformanceEntryCallback): () => void;
|
|
25
|
+
/**
|
|
26
|
+
* 更新配置
|
|
27
|
+
*/
|
|
28
|
+
updateOptions(options: Partial<SpeedTestOptions>): void;
|
|
29
|
+
/**
|
|
30
|
+
* 销毁实例
|
|
31
|
+
*/
|
|
32
|
+
destroy(): void;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=speed-tester.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"speed-tester.d.ts","sourceRoot":"","sources":["../../src/core/speed-tester.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,gBAAgB,EAChB,wBAAwB,EACzB,MAAM,SAAS,CAAC;AAGjB;;GAEG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,OAAO,CAA6B;IAC5C,OAAO,CAAC,QAAQ,CAAoC;gBAExC,OAAO,GAAE,gBAAqB;IAU1C;;OAEG;IACG,IAAI,IAAI,OAAO,CAAC,eAAe,CAAC;IAQtC;;OAEG;YACW,kBAAkB;IAehC;;OAEG;IACH,OAAO,CAAC,aAAa;IAuErB;;OAEG;IACH,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,wBAAwB,GAAG,MAAM,IAAI;IAkBnF;;OAEG;IACH,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,IAAI;IAIvD;;OAEG;IACH,OAAO,IAAI,IAAI;CAMhB"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 网速测试结果
|
|
3
|
+
*/
|
|
4
|
+
export interface SpeedTestResult {
|
|
5
|
+
/** 下载速度 (Mbps) */
|
|
6
|
+
speedMbps: number;
|
|
7
|
+
/** 下载速度 (KB/s) */
|
|
8
|
+
speedKBps: number;
|
|
9
|
+
/** 网络类型评估 */
|
|
10
|
+
networkType: 'fast' | 'medium' | 'slow' | 'unknown';
|
|
11
|
+
/** 是否为内网 */
|
|
12
|
+
isIntranet: boolean;
|
|
13
|
+
/** 测试耗时 (ms) */
|
|
14
|
+
duration: number;
|
|
15
|
+
/** 传输大小 (bytes) */
|
|
16
|
+
transferSize: number;
|
|
17
|
+
/** 测试资源URL */
|
|
18
|
+
resourceUrl: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 资源测速信息
|
|
22
|
+
*/
|
|
23
|
+
export interface ResourceSpeedInfo {
|
|
24
|
+
/** 资源名称 */
|
|
25
|
+
name: string;
|
|
26
|
+
/** 下载速度 (Mbps) */
|
|
27
|
+
speedMbps: number;
|
|
28
|
+
/** 下载速度 (KB/s) */
|
|
29
|
+
speedKBps: number;
|
|
30
|
+
/** 下载时间 (ms) */
|
|
31
|
+
downloadTime: number;
|
|
32
|
+
/** 传输大小 (bytes) */
|
|
33
|
+
transferSize: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* SDK配置选项
|
|
37
|
+
*/
|
|
38
|
+
export interface SpeedTestOptions {
|
|
39
|
+
/** 内网测速资源URL */
|
|
40
|
+
intranetUrl?: string;
|
|
41
|
+
/** 外网测速资源URL */
|
|
42
|
+
internetUrl?: string;
|
|
43
|
+
/** 超时时间 (ms) */
|
|
44
|
+
timeout?: number;
|
|
45
|
+
/** 是否自动检测内外网 */
|
|
46
|
+
autoDetect?: boolean;
|
|
47
|
+
/** 网速评估阈值 (Mbps) */
|
|
48
|
+
thresholds?: {
|
|
49
|
+
fast: number;
|
|
50
|
+
medium: number;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Performance Observer 回调参数
|
|
55
|
+
*/
|
|
56
|
+
export interface PerformanceEntryCallback {
|
|
57
|
+
(entry: PerformanceResourceTiming): void;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,kBAAkB;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,kBAAkB;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa;IACb,WAAW,EAAE,MAAM,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAC;IACpD,YAAY;IACZ,UAAU,EAAE,OAAO,CAAC;IACpB,gBAAgB;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,mBAAmB;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc;IACd,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,WAAW;IACX,IAAI,EAAE,MAAM,CAAC;IACb,kBAAkB;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,kBAAkB;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,mBAAmB;IACnB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,gBAAgB;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gBAAgB;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gBAAgB;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,gBAAgB;IAChB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,oBAAoB;IACpB,UAAU,CAAC,EAAE;QACX,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,CAAC,KAAK,EAAE,yBAAyB,GAAG,IAAI,CAAC;CAC1C"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export * from './core/sdk';
|
|
2
|
+
export type { SpeedTestResult, SpeedTestOptions, ResourceSpeedInfo, PerformanceEntryCallback, } from './core/types';
|
|
3
|
+
export { calcSpeedByResource, getAllResourcesSpeeds, evaluateNetworkType, } from './core/performance-utils';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAC;AAG3B,YAAY,EACV,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,wBAAwB,GACzB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,0BAA0B,CAAC"}
|
package/dist/network-speed-js.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
1
|
+
var m = Object.defineProperty;
|
|
2
|
+
var h = (t, e, r) => e in t ? m(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var a = (t, e, r) => h(t, typeof e != "symbol" ? e + "" : e, r);
|
|
4
4
|
function d(t) {
|
|
5
5
|
const e = t.responseEnd - t.responseStart;
|
|
6
6
|
if (e <= 0 || t.transferSize === 0)
|
|
7
7
|
return null;
|
|
8
|
-
const r = t.transferSize * 8 / e / 1e3,
|
|
8
|
+
const r = t.transferSize * 8 / e / 1e3, n = t.transferSize / e;
|
|
9
9
|
return {
|
|
10
10
|
name: t.name,
|
|
11
11
|
speedMbps: Number(r.toFixed(2)),
|
|
12
|
-
speedKBps: Number(
|
|
12
|
+
speedKBps: Number(n.toFixed(2)),
|
|
13
13
|
downloadTime: Number(e.toFixed(2)),
|
|
14
14
|
transferSize: t.transferSize
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function b() {
|
|
18
18
|
return performance.getEntriesByType("resource").filter(
|
|
19
19
|
(e) => e instanceof PerformanceResourceTiming && e.transferSize > 0
|
|
20
20
|
).map(d).filter((e) => e !== null);
|
|
21
21
|
}
|
|
22
|
-
function
|
|
22
|
+
function S(t) {
|
|
23
23
|
performance.getEntriesByName(t).forEach(() => {
|
|
24
24
|
performance.clearResourceTimings();
|
|
25
25
|
});
|
|
@@ -27,10 +27,10 @@ function b(t) {
|
|
|
27
27
|
function w(t, e = { fast: 10, medium: 2 }) {
|
|
28
28
|
return t >= e.fast ? "fast" : t >= e.medium ? "medium" : t > 0 ? "slow" : "unknown";
|
|
29
29
|
}
|
|
30
|
-
class
|
|
30
|
+
class y {
|
|
31
31
|
constructor(e = {}) {
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
a(this, "options");
|
|
33
|
+
a(this, "observer", null);
|
|
34
34
|
this.options = {
|
|
35
35
|
intranetUrl: e.intranetUrl || "",
|
|
36
36
|
internetUrl: e.internetUrl || "",
|
|
@@ -61,50 +61,58 @@ class T {
|
|
|
61
61
|
* 测试单个URL
|
|
62
62
|
*/
|
|
63
63
|
testSingleUrl(e, r) {
|
|
64
|
-
return new Promise((
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
for (const l of
|
|
64
|
+
return new Promise((n, c) => {
|
|
65
|
+
const o = `${e}?t=${Date.now()}`;
|
|
66
|
+
S(o);
|
|
67
|
+
const u = new PerformanceObserver((s) => {
|
|
68
|
+
for (const l of s.getEntries())
|
|
69
69
|
if (l.entryType === "resource" && l.name.includes(e)) {
|
|
70
|
-
const
|
|
71
|
-
if (
|
|
72
|
-
const
|
|
73
|
-
speedMbps:
|
|
74
|
-
speedKBps:
|
|
70
|
+
const i = d(l);
|
|
71
|
+
if (i) {
|
|
72
|
+
const p = {
|
|
73
|
+
speedMbps: i.speedMbps,
|
|
74
|
+
speedKBps: i.speedKBps,
|
|
75
75
|
networkType: w(
|
|
76
|
-
|
|
76
|
+
i.speedMbps,
|
|
77
77
|
this.options.thresholds
|
|
78
78
|
),
|
|
79
79
|
isIntranet: r,
|
|
80
|
-
duration:
|
|
81
|
-
transferSize:
|
|
80
|
+
duration: i.downloadTime,
|
|
81
|
+
transferSize: i.transferSize,
|
|
82
82
|
resourceUrl: e
|
|
83
83
|
};
|
|
84
|
-
|
|
84
|
+
u.disconnect(), n(p);
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
});
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
|
|
88
|
+
u.observe({ entryTypes: ["resource"] });
|
|
89
|
+
const f = setTimeout(() => {
|
|
90
|
+
u.disconnect(), c(new Error(`测速超时: ${e}`));
|
|
91
91
|
}, this.options.timeout);
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
92
|
+
fetch(o, {
|
|
93
|
+
method: "GET",
|
|
94
|
+
cache: "no-store"
|
|
95
|
+
// 禁用缓存
|
|
96
|
+
}).then((s) => {
|
|
97
|
+
if (!s.ok)
|
|
98
|
+
throw new Error(`HTTP error! status: ${s.status}`);
|
|
99
|
+
return s.blob();
|
|
100
|
+
}).then(() => {
|
|
101
|
+
clearTimeout(f);
|
|
102
|
+
}).catch((s) => {
|
|
103
|
+
clearTimeout(f), u.disconnect(), c(new Error(`资源加载失败: ${s.message}`));
|
|
104
|
+
});
|
|
97
105
|
});
|
|
98
106
|
}
|
|
99
107
|
/**
|
|
100
108
|
* 监听特定资源的性能数据
|
|
101
109
|
*/
|
|
102
110
|
observeResource(e, r) {
|
|
103
|
-
const
|
|
104
|
-
for (const
|
|
105
|
-
|
|
111
|
+
const n = new PerformanceObserver((c) => {
|
|
112
|
+
for (const o of c.getEntries())
|
|
113
|
+
o.entryType === "resource" && o.name.includes(e) && r(o);
|
|
106
114
|
});
|
|
107
|
-
return
|
|
115
|
+
return n.observe({ entryTypes: ["resource"] }), () => n.disconnect();
|
|
108
116
|
}
|
|
109
117
|
/**
|
|
110
118
|
* 更新配置
|
|
@@ -119,10 +127,10 @@ class T {
|
|
|
119
127
|
this.observer && (this.observer.disconnect(), this.observer = null);
|
|
120
128
|
}
|
|
121
129
|
}
|
|
122
|
-
class
|
|
130
|
+
class T {
|
|
123
131
|
constructor(e = {}) {
|
|
124
|
-
|
|
125
|
-
this.tester = new
|
|
132
|
+
a(this, "tester");
|
|
133
|
+
this.tester = new y(e);
|
|
126
134
|
}
|
|
127
135
|
/**
|
|
128
136
|
* 执行网速测试
|
|
@@ -134,7 +142,7 @@ class g {
|
|
|
134
142
|
* 获取所有已加载资源的速度信息
|
|
135
143
|
*/
|
|
136
144
|
getAllResourcesSpeeds() {
|
|
137
|
-
return
|
|
145
|
+
return b();
|
|
138
146
|
}
|
|
139
147
|
/**
|
|
140
148
|
* 监听特定资源的性能数据
|
|
@@ -155,13 +163,13 @@ class g {
|
|
|
155
163
|
this.tester.destroy();
|
|
156
164
|
}
|
|
157
165
|
}
|
|
158
|
-
function
|
|
159
|
-
return new
|
|
166
|
+
function U(t) {
|
|
167
|
+
return new T(t);
|
|
160
168
|
}
|
|
161
169
|
export {
|
|
162
|
-
|
|
170
|
+
T as NetworkSpeedSDK,
|
|
163
171
|
d as calcSpeedByResource,
|
|
164
|
-
|
|
172
|
+
U as createNetworkSpeedSDK,
|
|
165
173
|
w as evaluateNetworkType,
|
|
166
|
-
|
|
174
|
+
b as getAllResourcesSpeeds
|
|
167
175
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(r,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(r=typeof globalThis<"u"?globalThis:r||self,s(r.NetworkSpeedJS={}))})(this,function(r){"use strict";var
|
|
1
|
+
(function(r,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(r=typeof globalThis<"u"?globalThis:r||self,s(r.NetworkSpeedJS={}))})(this,function(r){"use strict";var g=Object.defineProperty;var v=(r,s,n)=>s in r?g(r,s,{enumerable:!0,configurable:!0,writable:!0,value:n}):r[s]=n;var f=(r,s,n)=>v(r,typeof s!="symbol"?s+"":s,n);function s(t){const e=t.responseEnd-t.responseStart;if(e<=0||t.transferSize===0)return null;const o=t.transferSize*8/e/1e3,c=t.transferSize/e;return{name:t.name,speedMbps:Number(o.toFixed(2)),speedKBps:Number(c.toFixed(2)),downloadTime:Number(e.toFixed(2)),transferSize:t.transferSize}}function n(){return performance.getEntriesByType("resource").filter(e=>e instanceof PerformanceResourceTiming&&e.transferSize>0).map(s).filter(e=>e!==null)}function y(t){performance.getEntriesByName(t).forEach(()=>{performance.clearResourceTimings()})}function m(t,e={fast:10,medium:2}){return t>=e.fast?"fast":t>=e.medium?"medium":t>0?"slow":"unknown"}class b{constructor(e={}){f(this,"options");f(this,"observer",null);this.options={intranetUrl:e.intranetUrl||"",internetUrl:e.internetUrl||"",timeout:e.timeout||1e4,autoDetect:e.autoDetect??!0,thresholds:e.thresholds||{fast:10,medium:2}}}async test(){return this.options.autoDetect?this.testWithAutoDetect():this.testSingleUrl(this.options.internetUrl,!1)}async testWithAutoDetect(){if(this.options.intranetUrl)try{return await this.testSingleUrl(this.options.intranetUrl,!0)}catch{console.log("内网测速失败,切换到外网测速")}return this.testSingleUrl(this.options.internetUrl,!1)}testSingleUrl(e,o){return new Promise((c,l)=>{const u=`${e}?t=${Date.now()}`;y(u);const d=new PerformanceObserver(i=>{for(const p of i.getEntries())if(p.entryType==="resource"&&p.name.includes(e)){const a=s(p);if(a){const T={speedMbps:a.speedMbps,speedKBps:a.speedKBps,networkType:m(a.speedMbps,this.options.thresholds),isIntranet:o,duration:a.downloadTime,transferSize:a.transferSize,resourceUrl:e};d.disconnect(),c(T)}}});d.observe({entryTypes:["resource"]});const S=setTimeout(()=>{d.disconnect(),l(new Error(`测速超时: ${e}`))},this.options.timeout);fetch(u,{method:"GET",cache:"no-store"}).then(i=>{if(!i.ok)throw new Error(`HTTP error! status: ${i.status}`);return i.blob()}).then(()=>{clearTimeout(S)}).catch(i=>{clearTimeout(S),d.disconnect(),l(new Error(`资源加载失败: ${i.message}`))})})}observeResource(e,o){const c=new PerformanceObserver(l=>{for(const u of l.getEntries())u.entryType==="resource"&&u.name.includes(e)&&o(u)});return c.observe({entryTypes:["resource"]}),()=>c.disconnect()}updateOptions(e){this.options={...this.options,...e}}destroy(){this.observer&&(this.observer.disconnect(),this.observer=null)}}class h{constructor(e={}){f(this,"tester");this.tester=new b(e)}async test(){return this.tester.test()}getAllResourcesSpeeds(){return n()}observeResource(e,o){return this.tester.observeResource(e,o)}updateOptions(e){this.tester.updateOptions(e)}destroy(){this.tester.destroy()}}function w(t){return new h(t)}r.NetworkSpeedSDK=h,r.calcSpeedByResource=s,r.createNetworkSpeedSDK=w,r.evaluateNetworkType=m,r.getAllResourcesSpeeds=n,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "network-speed-js",
|
|
3
3
|
"private": false,
|
|
4
4
|
"description": "A framework-agnostic network speed testing SDK based on Performance API with intranet/internet auto-detection support",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.1",
|
|
6
6
|
"author": "Sunny-117",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"keywords": [
|
|
@@ -35,8 +35,12 @@
|
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"dev": "vite",
|
|
38
|
-
"build": "
|
|
38
|
+
"build": "npm run build:lib && npm run build:types",
|
|
39
|
+
"build:lib": "vite build --mode lib",
|
|
40
|
+
"build:types": "tsc --project tsconfig.build.json",
|
|
41
|
+
"build:demo": "vite build",
|
|
39
42
|
"preview": "vite preview",
|
|
43
|
+
"deploy": "sh deploy.sh",
|
|
40
44
|
"prepublishOnly": "npm run build"
|
|
41
45
|
},
|
|
42
46
|
"peerDependencies": {
|