@steve02081504/fount-p2p 0.0.20 → 0.0.21

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.
@@ -1,118 +0,0 @@
1
- /**
2
- * 服务端 WebRTC polyfill 在 Windows 等环境常产出 `.local` host candidate,
3
- * 远端无法解析。按 iceLocalHostnamePolicy 改写为 loopback 或丢弃。
4
- */
5
-
6
- /** @typedef {'none' | 'rewrite-loopback' | 'drop'} IceLocalHostnamePolicy */
7
-
8
- /**
9
- * @param {string | null | undefined} candidateSdp ICE candidate SDP 行
10
- * @param {IceLocalHostnamePolicy} policy 处理策略
11
- * @returns {string | null} 处理后的 SDP;drop 策略下不可用时返回 null
12
- */
13
- export function applyIceLocalHostnamePolicy(candidateSdp, policy) {
14
- const sdp = String(candidateSdp || '').trim()
15
- if (!sdp || !/\.local/i.test(sdp)) return sdp || null
16
- if (!/\btyp host\b/i.test(sdp)) return sdp
17
- if (policy === 'drop') return null
18
- if (policy === 'rewrite-loopback') {
19
- const rewritten = sdp.replace(/(\s)[\w-]+\.local(\s|$)/gi, '$1127.0.0.1$2')
20
- return rewritten === sdp ? null : rewritten
21
- }
22
- return sdp
23
- }
24
-
25
- /**
26
- * @param {RTCIceCandidate | { candidate?: string } | null | undefined} candidate ICE candidate
27
- * @param {typeof RTCIceCandidate} RTCIceCandidateCtor 构造函数
28
- * @param {IceLocalHostnamePolicy} policy 处理策略
29
- * @returns {RTCIceCandidate | { candidate?: string } | null | undefined} 过滤/改写后的 candidate
30
- */
31
- export function filterIceLocalHostnameCandidate(candidate, RTCIceCandidateCtor, policy) {
32
- if (!candidate || policy === 'none') return candidate
33
- const raw = typeof candidate === 'string'
34
- ? candidate
35
- : candidate.candidate ?? candidate.toJSON?.()?.candidate ?? ''
36
- const rewritten = applyIceLocalHostnamePolicy(raw, policy)
37
- if (!rewritten) return null
38
- if (rewritten === raw) return candidate
39
- try {
40
- const init = typeof candidate.toJSON === 'function'
41
- ? { ...candidate.toJSON(), candidate: rewritten }
42
- : { candidate: rewritten, sdpMid: candidate.sdpMid, sdpMLineIndex: candidate.sdpMLineIndex }
43
- return new RTCIceCandidateCtor(init)
44
- }
45
- catch {
46
- return { ...candidate, candidate: rewritten }
47
- }
48
- }
49
-
50
- /**
51
- * @param {typeof RTCPeerConnection} BaseRTC 原始 polyfill 类
52
- * @param {typeof RTCIceCandidate} [RTCIceCandidate] ICE candidate 构造函数
53
- * @param {IceLocalHostnamePolicy} [policy='drop'] 策略
54
- * @returns {typeof RTCPeerConnection} 包装后的 RTCPeerConnection 类(none 时原样返回)
55
- */
56
- export function wrapRtcPeerConnectionForIceLocalHostname(BaseRTC, RTCIceCandidate = globalThis.RTCIceCandidate, policy = 'drop') {
57
- if (policy === 'none') return BaseRTC
58
-
59
- /**
60
- * @param {RTCPeerConnectionIceEvent} event ICE 候选事件
61
- * @param {(event: RTCPeerConnectionIceEvent) => void} handler 用户 handler
62
- * @returns {void}
63
- */
64
- function invokeFilteredIceHandler(event, handler) {
65
- if (!event?.candidate) {
66
- handler(event)
67
- return
68
- }
69
- const filtered = filterIceLocalHostnameCandidate(event.candidate, RTCIceCandidate, policy)
70
- if (!filtered) return
71
- if (filtered === event.candidate) {
72
- handler(event)
73
- return
74
- }
75
- handler({ ...event, candidate: filtered })
76
- }
77
-
78
- return class IceLocalHostnameFilteredRTCPeerConnection extends BaseRTC {
79
- /** @type {((event: RTCPeerConnectionIceEvent) => void) | null} */
80
- #userIceHandler = null
81
-
82
- /**
83
- * @param {RTCConfiguration} [config] RTC 配置
84
- */
85
- constructor(config) {
86
- super(config)
87
- /** @param {RTCPeerConnectionIceEvent} event ICE 候选事件 */
88
- const relayIce = event => {
89
- if (this.#userIceHandler)
90
- invokeFilteredIceHandler(event, this.#userIceHandler)
91
- }
92
- super.onicecandidate = relayIce
93
- const iceObs = this.onIceCandidate
94
- if (iceObs && typeof iceObs.subscribe === 'function')
95
- iceObs.subscribe(candidate => {
96
- if (!this.#userIceHandler) return
97
- if (!candidate) {
98
- this.#userIceHandler({ candidate: null })
99
- return
100
- }
101
- const filtered = filterIceLocalHostnameCandidate(candidate, RTCIceCandidate, policy)
102
- if (filtered)
103
- this.#userIceHandler({ candidate: filtered })
104
- })
105
-
106
- }
107
-
108
- /** @returns {((event: RTCPeerConnectionIceEvent) => void) | null} 用户 ICE 处理器 */
109
- get onicecandidate() {
110
- return this.#userIceHandler
111
- }
112
-
113
- /** @param {((event: RTCPeerConnectionIceEvent) => void) | null} handler ICE 处理器 */
114
- set onicecandidate(handler) {
115
- this.#userIceHandler = handler
116
- }
117
- }
118
- }