@libp2p/webrtc 4.0.10 → 4.0.11

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.
Files changed (32) hide show
  1. package/dist/index.min.js +12 -12
  2. package/dist/src/webrtc/index.d.ts +1 -6
  3. package/dist/src/webrtc/index.d.ts.map +1 -1
  4. package/dist/src/webrtc/index.js +1 -6
  5. package/dist/src/webrtc/index.js.map +1 -1
  6. package/package.json +7 -7
  7. package/src/webrtc/index.ts +1 -6
  8. package/dist/src/webrtc/rtc-data-channel.d.ts +0 -29
  9. package/dist/src/webrtc/rtc-data-channel.d.ts.map +0 -1
  10. package/dist/src/webrtc/rtc-data-channel.js +0 -115
  11. package/dist/src/webrtc/rtc-data-channel.js.map +0 -1
  12. package/dist/src/webrtc/rtc-events.d.ts +0 -9
  13. package/dist/src/webrtc/rtc-events.d.ts.map +0 -1
  14. package/dist/src/webrtc/rtc-events.js +0 -15
  15. package/dist/src/webrtc/rtc-events.js.map +0 -1
  16. package/dist/src/webrtc/rtc-ice-candidate.d.ts +0 -22
  17. package/dist/src/webrtc/rtc-ice-candidate.d.ts.map +0 -1
  18. package/dist/src/webrtc/rtc-ice-candidate.js +0 -47
  19. package/dist/src/webrtc/rtc-ice-candidate.js.map +0 -1
  20. package/dist/src/webrtc/rtc-peer-connection.d.ts +0 -47
  21. package/dist/src/webrtc/rtc-peer-connection.d.ts.map +0 -1
  22. package/dist/src/webrtc/rtc-peer-connection.js +0 -245
  23. package/dist/src/webrtc/rtc-peer-connection.js.map +0 -1
  24. package/dist/src/webrtc/rtc-session-description.d.ts +0 -10
  25. package/dist/src/webrtc/rtc-session-description.d.ts.map +0 -1
  26. package/dist/src/webrtc/rtc-session-description.js +0 -18
  27. package/dist/src/webrtc/rtc-session-description.js.map +0 -1
  28. package/src/webrtc/rtc-data-channel.ts +0 -140
  29. package/src/webrtc/rtc-events.ts +0 -19
  30. package/src/webrtc/rtc-ice-candidate.ts +0 -50
  31. package/src/webrtc/rtc-peer-connection.ts +0 -306
  32. package/src/webrtc/rtc-session-description.ts +0 -19
@@ -1,306 +0,0 @@
1
- import node from 'node-datachannel'
2
- import defer, { type DeferredPromise } from 'p-defer'
3
- import { DataChannel } from './rtc-data-channel.js'
4
- import { DataChannelEvent, PeerConnectionIceEvent } from './rtc-events.js'
5
- import { IceCandidate } from './rtc-ice-candidate.js'
6
- import { SessionDescription } from './rtc-session-description.js'
7
-
8
- export class PeerConnection extends EventTarget implements RTCPeerConnection {
9
- static async generateCertificate (keygenAlgorithm: AlgorithmIdentifier): Promise<RTCCertificate> {
10
- throw new Error('Not implemented')
11
- }
12
-
13
- canTrickleIceCandidates: boolean | null
14
- sctp: RTCSctpTransport | null
15
-
16
- onconnectionstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null
17
- ondatachannel: ((this: RTCPeerConnection, ev: RTCDataChannelEvent) => any) | null
18
- onicecandidate: ((this: RTCPeerConnection, ev: RTCPeerConnectionIceEvent) => any) | null
19
- onicecandidateerror: ((this: RTCPeerConnection, ev: Event) => any) | null
20
- oniceconnectionstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null
21
- onicegatheringstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null
22
- onnegotiationneeded: ((this: RTCPeerConnection, ev: Event) => any) | null
23
- onsignalingstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null
24
- ontrack: ((this: RTCPeerConnection, ev: RTCTrackEvent) => any) | null
25
-
26
- #peerConnection: node.PeerConnection
27
- #config: RTCConfiguration
28
- #localOffer: DeferredPromise<RTCSessionDescriptionInit>
29
- #localAnswer: DeferredPromise<RTCSessionDescriptionInit>
30
- #dataChannels: Set<DataChannel>
31
-
32
- constructor (init: RTCConfiguration = {}) {
33
- super()
34
-
35
- this.#config = init
36
- this.#localOffer = defer()
37
- this.#localAnswer = defer()
38
- this.#dataChannels = new Set()
39
-
40
- const iceServers = init.iceServers ?? []
41
-
42
- this.#peerConnection = new node.PeerConnection(`peer-${Math.random()}`, {
43
- iceServers: iceServers.map(server => {
44
- const urls = (Array.isArray(server.urls) ? server.urls : [server.urls]).map(str => new URL(str))
45
-
46
- return urls.map(url => {
47
- /** @type {import('../lib/index.js').IceServer} */
48
- const iceServer = {
49
- hostname: url.hostname,
50
- port: parseInt(url.port, 10),
51
- username: server.username,
52
- password: server.credential
53
- // relayType - how to specify?
54
- }
55
-
56
- return iceServer
57
- })
58
- })
59
- .flat(),
60
- iceTransportPolicy: init?.iceTransportPolicy
61
- })
62
-
63
- this.#peerConnection.onStateChange(() => {
64
- this.dispatchEvent(new Event('connectionstatechange'))
65
- })
66
- // https://github.com/murat-dogan/node-datachannel/pull/171
67
- // this.#peerConnection.onSignalingStateChange(() => {
68
- // this.dispatchEvent(new Event('signalingstatechange'))
69
- // })
70
- this.#peerConnection.onGatheringStateChange(() => {
71
- this.dispatchEvent(new Event('icegatheringstatechange'))
72
- })
73
- this.#peerConnection.onDataChannel(channel => {
74
- this.dispatchEvent(new DataChannelEvent(new DataChannel(channel)))
75
- })
76
-
77
- // forward events to properties
78
- this.addEventListener('connectionstatechange', event => {
79
- this.onconnectionstatechange?.(event)
80
- })
81
- this.addEventListener('signalingstatechange', event => {
82
- this.onsignalingstatechange?.(event)
83
- })
84
- this.addEventListener('icegatheringstatechange', event => {
85
- this.onicegatheringstatechange?.(event)
86
- })
87
- this.addEventListener('datachannel', event => {
88
- this.ondatachannel?.(event as RTCDataChannelEvent)
89
- })
90
-
91
- this.#peerConnection.onLocalDescription((sdp, type) => {
92
- if (type === 'offer') {
93
- this.#localOffer.resolve({
94
- sdp,
95
- type
96
- })
97
- }
98
-
99
- if (type === 'answer') {
100
- this.#localAnswer.resolve({
101
- sdp,
102
- type
103
- })
104
- }
105
- })
106
-
107
- this.#peerConnection.onLocalCandidate((candidate, mid) => {
108
- if (mid === 'unspec') {
109
- this.#localAnswer.reject(new Error(`Invalid description type ${mid}`))
110
- return
111
- }
112
-
113
- const event = new PeerConnectionIceEvent(new IceCandidate({ candidate }))
114
-
115
- this.onicecandidate?.(event)
116
- })
117
-
118
- this.canTrickleIceCandidates = null
119
- this.sctp = null
120
- this.onconnectionstatechange = null
121
- this.ondatachannel = null
122
- this.onicecandidate = null
123
- this.onicecandidateerror = null
124
- this.oniceconnectionstatechange = null
125
- this.onicegatheringstatechange = null
126
- this.onnegotiationneeded = null
127
- this.onsignalingstatechange = null
128
- this.ontrack = null
129
- }
130
-
131
- get connectionState (): RTCPeerConnectionState {
132
- return assertState(this.#peerConnection.state(), RTCPeerConnectionStates)
133
- }
134
-
135
- get iceConnectionState (): RTCIceConnectionState {
136
- return assertState(this.#peerConnection.state(), RTCIceConnectionStates)
137
- }
138
-
139
- get iceGatheringState (): RTCIceGatheringState {
140
- return assertState(this.#peerConnection.gatheringState(), RTCIceGatheringStates)
141
- }
142
-
143
- get signalingState (): RTCSignalingState {
144
- return assertState(this.#peerConnection.signalingState(), RTCSignalingStates)
145
- }
146
-
147
- get currentLocalDescription (): RTCSessionDescription | null {
148
- return toSessionDescription(this.#peerConnection.localDescription())
149
- }
150
-
151
- get localDescription (): RTCSessionDescription | null {
152
- return toSessionDescription(this.#peerConnection.localDescription())
153
- }
154
-
155
- get pendingLocalDescription (): RTCSessionDescription | null {
156
- return toSessionDescription(this.#peerConnection.localDescription())
157
- }
158
-
159
- get currentRemoteDescription (): RTCSessionDescription | null {
160
- // not exposed by node-datachannel
161
- return toSessionDescription(null)
162
- }
163
-
164
- get pendingRemoteDescription (): RTCSessionDescription | null {
165
- // not exposed by node-datachannel
166
- return toSessionDescription(null)
167
- }
168
-
169
- get remoteDescription (): RTCSessionDescription | null {
170
- // not exposed by node-datachannel
171
- return toSessionDescription(null)
172
- }
173
-
174
- async addIceCandidate (candidate?: RTCIceCandidateInit): Promise<void> {
175
- if (candidate == null || candidate.candidate == null) {
176
- throw new Error('Candidate invalid')
177
- }
178
-
179
- this.#peerConnection.addRemoteCandidate(candidate.candidate, candidate.sdpMid ?? '0')
180
- }
181
-
182
- addTrack (track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender {
183
- throw new Error('Not implemented')
184
- }
185
-
186
- addTransceiver (trackOrKind: MediaStreamTrack | string, init?: RTCRtpTransceiverInit): RTCRtpTransceiver {
187
- throw new Error('Not implemented')
188
- }
189
-
190
- close (): void {
191
- // close all channels before shutting down
192
- this.#dataChannels.forEach(channel => {
193
- channel.close()
194
- })
195
-
196
- this.#peerConnection.close()
197
- this.#peerConnection.destroy()
198
- }
199
-
200
- createDataChannel (label: string, dataChannelDict: RTCDataChannelInit = {}): RTCDataChannel {
201
- const channel = this.#peerConnection.createDataChannel(label, dataChannelDict)
202
- const dataChannel = new DataChannel(channel, dataChannelDict)
203
-
204
- // ensure we can close all channels when shutting down
205
- this.#dataChannels.add(dataChannel)
206
- dataChannel.addEventListener('close', () => {
207
- this.#dataChannels.delete(dataChannel)
208
- })
209
-
210
- return dataChannel
211
- }
212
-
213
- async createOffer (options?: RTCOfferOptions): Promise<RTCSessionDescriptionInit>
214
- async createOffer (successCallback: RTCSessionDescriptionCallback, failureCallback: RTCPeerConnectionErrorCallback, options?: RTCOfferOptions): Promise<void>
215
- async createOffer (...args: any[]): Promise<any> {
216
- return this.#localOffer.promise
217
- }
218
-
219
- async createAnswer (options?: RTCAnswerOptions): Promise<RTCSessionDescriptionInit>
220
- async createAnswer (successCallback: RTCSessionDescriptionCallback, failureCallback: RTCPeerConnectionErrorCallback): Promise<void>
221
- async createAnswer (...args: any[]): Promise<any> {
222
- return this.#localAnswer.promise
223
- }
224
-
225
- getConfiguration (): RTCConfiguration {
226
- return this.#config
227
- }
228
-
229
- getReceivers (): RTCRtpReceiver[] {
230
- throw new Error('Not implemented')
231
- }
232
-
233
- getSenders (): RTCRtpSender[] {
234
- throw new Error('Not implemented')
235
- }
236
-
237
- async getStats (selector?: MediaStreamTrack | null): Promise<RTCStatsReport> {
238
- throw new Error('Not implemented')
239
- }
240
-
241
- getTransceivers (): RTCRtpTransceiver[] {
242
- throw new Error('Not implemented')
243
- }
244
-
245
- removeTrack (sender: RTCRtpSender): void {
246
- throw new Error('Not implemented')
247
- }
248
-
249
- restartIce (): void {
250
- throw new Error('Not implemented')
251
- }
252
-
253
- setConfiguration (configuration: RTCConfiguration = {}): void {
254
- this.#config = configuration
255
- }
256
-
257
- async setLocalDescription (description?: RTCLocalSessionDescriptionInit): Promise<void> {
258
- if (description == null || description.type == null) {
259
- throw new Error('Local description type must be set')
260
- }
261
-
262
- if (description.type !== 'offer') {
263
- // any other type causes libdatachannel to throw
264
- return
265
- }
266
-
267
- // @ts-expect-error types are wrong
268
- this.#peerConnection.setLocalDescription(description.type)
269
- }
270
-
271
- async setRemoteDescription (description: RTCSessionDescriptionInit): Promise<void> {
272
- if (description.sdp == null) {
273
- throw new Error('Remote SDP must be set')
274
- }
275
-
276
- // @ts-expect-error types are wrong
277
- this.#peerConnection.setRemoteDescription(description.sdp, description.type)
278
- }
279
- }
280
-
281
- export { PeerConnection as RTCPeerConnection }
282
-
283
- function assertState <T> (state: any, states: T[]): T {
284
- if (state != null && !states.includes(state)) {
285
- throw new Error(`Invalid value encountered - "${state}" must be one of ${states}`)
286
- }
287
-
288
- return state as T
289
- }
290
-
291
- function toSessionDescription (description: { sdp?: string, type: string } | null): RTCSessionDescription | null {
292
- if (description == null) {
293
- return null
294
- }
295
-
296
- return new SessionDescription({
297
- sdp: description.sdp,
298
- type: assertState(description.type, RTCSdpTypes)
299
- })
300
- }
301
-
302
- const RTCPeerConnectionStates: RTCPeerConnectionState[] = ['closed', 'connected', 'connecting', 'disconnected', 'failed', 'new']
303
- const RTCSdpTypes: RTCSdpType[] = ['answer', 'offer', 'pranswer', 'rollback']
304
- const RTCIceConnectionStates: RTCIceConnectionState[] = ['checking', 'closed', 'completed', 'connected', 'disconnected', 'failed', 'new']
305
- const RTCIceGatheringStates: RTCIceGatheringState[] = ['complete', 'gathering', 'new']
306
- const RTCSignalingStates: RTCSignalingState[] = ['closed', 'have-local-offer', 'have-local-pranswer', 'have-remote-offer', 'have-remote-pranswer', 'stable']
@@ -1,19 +0,0 @@
1
- /**
2
- * @see https://developer.mozilla.org/docs/Web/API/RTCSessionDescription
3
- */
4
- export class SessionDescription implements RTCSessionDescription {
5
- readonly sdp: string
6
- readonly type: RTCSdpType
7
-
8
- constructor (init: RTCSessionDescriptionInit) {
9
- this.sdp = init.sdp ?? ''
10
- this.type = init.type
11
- }
12
-
13
- toJSON (): RTCSessionDescriptionInit {
14
- return {
15
- sdp: this.sdp,
16
- type: this.type
17
- }
18
- }
19
- }