werift 0.20.0 → 0.20.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.
Files changed (139) hide show
  1. package/lib/common/src/event.d.ts +45 -0
  2. package/lib/common/src/event.js +232 -0
  3. package/lib/common/src/event.js.map +1 -0
  4. package/lib/common/src/index.d.ts +1 -0
  5. package/lib/common/src/index.js +1 -0
  6. package/lib/common/src/index.js.map +1 -1
  7. package/lib/dtls/src/flight/client/flight5.js +6 -1
  8. package/lib/dtls/src/flight/client/flight5.js.map +1 -1
  9. package/lib/dtls/src/imports/common.d.ts +1 -0
  10. package/lib/dtls/src/imports/common.js +18 -0
  11. package/lib/dtls/src/imports/common.js.map +1 -0
  12. package/lib/dtls/src/socket.d.ts +1 -1
  13. package/lib/dtls/src/socket.js +5 -5
  14. package/lib/dtls/src/socket.js.map +1 -1
  15. package/lib/ice/src/candidate.d.ts +1 -1
  16. package/lib/ice/src/candidate.js +2 -1
  17. package/lib/ice/src/candidate.js.map +1 -1
  18. package/lib/ice/src/helper.js +2 -2
  19. package/lib/ice/src/helper.js.map +1 -1
  20. package/lib/ice/src/ice.d.ts +10 -5
  21. package/lib/ice/src/ice.js +46 -16
  22. package/lib/ice/src/ice.js.map +1 -1
  23. package/lib/ice/src/imports/common.d.ts +1 -0
  24. package/lib/ice/src/imports/common.js +18 -0
  25. package/lib/ice/src/imports/common.js.map +1 -0
  26. package/lib/ice/src/index.d.ts +4 -0
  27. package/lib/ice/src/index.js +4 -0
  28. package/lib/ice/src/index.js.map +1 -1
  29. package/lib/ice/src/stun/message.d.ts +1 -0
  30. package/lib/ice/src/stun/message.js +16 -6
  31. package/lib/ice/src/stun/message.js.map +1 -1
  32. package/lib/ice/src/stun/protocol.d.ts +3 -2
  33. package/lib/ice/src/stun/protocol.js +13 -7
  34. package/lib/ice/src/stun/protocol.js.map +1 -1
  35. package/lib/ice/src/stun/transaction.js +4 -3
  36. package/lib/ice/src/stun/transaction.js.map +1 -1
  37. package/lib/ice/src/transport.d.ts +21 -4
  38. package/lib/ice/src/transport.js +115 -7
  39. package/lib/ice/src/transport.js.map +1 -1
  40. package/lib/ice/src/turn/protocol.d.ts +52 -36
  41. package/lib/ice/src/turn/protocol.js +183 -76
  42. package/lib/ice/src/turn/protocol.js.map +1 -1
  43. package/lib/ice/src/types/model.d.ts +5 -5
  44. package/lib/ice/src/types/model.js.map +1 -1
  45. package/lib/ice/src/utils.d.ts +1 -0
  46. package/lib/ice/src/utils.js +15 -16
  47. package/lib/ice/src/utils.js.map +1 -1
  48. package/lib/rtp/src/extra/container/mp4/container.d.ts +1 -1
  49. package/lib/rtp/src/extra/container/mp4/container.js +2 -5
  50. package/lib/rtp/src/extra/container/mp4/container.js.map +1 -1
  51. package/lib/rtp/src/extra/processor/depacketizer.d.ts +1 -1
  52. package/lib/rtp/src/extra/processor/depacketizer.js +2 -2
  53. package/lib/rtp/src/extra/processor/depacketizer.js.map +1 -1
  54. package/lib/rtp/src/extra/processor/mp4.d.ts +1 -1
  55. package/lib/rtp/src/extra/processor/mp4.js +2 -5
  56. package/lib/rtp/src/extra/processor/mp4.js.map +1 -1
  57. package/lib/rtp/src/extra/processor/nack.d.ts +1 -1
  58. package/lib/rtp/src/extra/processor/nack.js +3 -3
  59. package/lib/rtp/src/extra/processor/nack.js.map +1 -1
  60. package/lib/rtp/src/extra/processor/rtcpCallback.d.ts +1 -1
  61. package/lib/rtp/src/extra/processor/rtcpCallback.js +2 -5
  62. package/lib/rtp/src/extra/processor/rtcpCallback.js.map +1 -1
  63. package/lib/rtp/src/extra/processor/rtpCallback.d.ts +1 -1
  64. package/lib/rtp/src/extra/processor/rtpCallback.js +2 -5
  65. package/lib/rtp/src/extra/processor/rtpCallback.js.map +1 -1
  66. package/lib/rtp/src/extra/processor/webm.d.ts +1 -1
  67. package/lib/rtp/src/extra/processor/webm.js +2 -5
  68. package/lib/rtp/src/extra/processor/webm.js.map +1 -1
  69. package/lib/rtp/src/imports/common.d.ts +1 -0
  70. package/lib/rtp/src/imports/common.js +18 -0
  71. package/lib/rtp/src/imports/common.js.map +1 -0
  72. package/lib/sctp/src/helper.js +2 -5
  73. package/lib/sctp/src/helper.js.map +1 -1
  74. package/lib/sctp/src/imports/common.d.ts +1 -0
  75. package/lib/sctp/src/imports/common.js +18 -0
  76. package/lib/sctp/src/imports/common.js.map +1 -0
  77. package/lib/sctp/src/sctp.d.ts +1 -1
  78. package/lib/sctp/src/sctp.js +3 -3
  79. package/lib/sctp/src/sctp.js.map +1 -1
  80. package/lib/webrtc/src/dataChannel.d.ts +1 -1
  81. package/lib/webrtc/src/dataChannel.js +5 -5
  82. package/lib/webrtc/src/dataChannel.js.map +1 -1
  83. package/lib/webrtc/src/imports/common.d.ts +1 -0
  84. package/lib/webrtc/src/imports/common.js +18 -0
  85. package/lib/webrtc/src/imports/common.js.map +1 -0
  86. package/lib/webrtc/src/media/receiver/nack.d.ts +1 -1
  87. package/lib/webrtc/src/media/receiver/nack.js +2 -2
  88. package/lib/webrtc/src/media/receiver/nack.js.map +1 -1
  89. package/lib/webrtc/src/media/rtpReceiver.d.ts +2 -2
  90. package/lib/webrtc/src/media/rtpReceiver.js +2 -5
  91. package/lib/webrtc/src/media/rtpReceiver.js.map +1 -1
  92. package/lib/webrtc/src/media/rtpSender.d.ts +1 -1
  93. package/lib/webrtc/src/media/rtpSender.js +5 -5
  94. package/lib/webrtc/src/media/rtpSender.js.map +1 -1
  95. package/lib/webrtc/src/media/rtpTransceiver.d.ts +1 -1
  96. package/lib/webrtc/src/media/rtpTransceiver.js +2 -5
  97. package/lib/webrtc/src/media/rtpTransceiver.js.map +1 -1
  98. package/lib/webrtc/src/media/sender/senderBWE.d.ts +1 -1
  99. package/lib/webrtc/src/media/sender/senderBWE.js +4 -7
  100. package/lib/webrtc/src/media/sender/senderBWE.js.map +1 -1
  101. package/lib/webrtc/src/media/track.d.ts +1 -1
  102. package/lib/webrtc/src/media/track.js +4 -7
  103. package/lib/webrtc/src/media/track.js.map +1 -1
  104. package/lib/webrtc/src/nonstandard/recorder/index.d.ts +2 -1
  105. package/lib/webrtc/src/nonstandard/recorder/index.js +2 -5
  106. package/lib/webrtc/src/nonstandard/recorder/index.js.map +1 -1
  107. package/lib/webrtc/src/nonstandard/recorder/writer/index.d.ts +1 -1
  108. package/lib/webrtc/src/nonstandard/recorder/writer/index.js.map +1 -1
  109. package/lib/webrtc/src/nonstandard/recorder/writer/webm.d.ts +1 -1
  110. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js +2 -2
  111. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js.map +1 -1
  112. package/lib/webrtc/src/peerConnection.d.ts +3 -2
  113. package/lib/webrtc/src/peerConnection.js +13 -11
  114. package/lib/webrtc/src/peerConnection.js.map +1 -1
  115. package/lib/webrtc/src/transport/dtls.d.ts +1 -1
  116. package/lib/webrtc/src/transport/dtls.js +12 -8
  117. package/lib/webrtc/src/transport/dtls.js.map +1 -1
  118. package/lib/webrtc/src/transport/ice.d.ts +2 -2
  119. package/lib/webrtc/src/transport/ice.js +10 -7
  120. package/lib/webrtc/src/transport/ice.js.map +1 -1
  121. package/lib/webrtc/src/transport/sctp.d.ts +1 -1
  122. package/lib/webrtc/src/transport/sctp.js +2 -2
  123. package/lib/webrtc/src/transport/sctp.js.map +1 -1
  124. package/package.json +1 -1
  125. package/src/dataChannel.ts +1 -1
  126. package/src/imports/common.ts +1 -0
  127. package/src/media/receiver/nack.ts +1 -1
  128. package/src/media/rtpReceiver.ts +1 -1
  129. package/src/media/rtpSender.ts +1 -1
  130. package/src/media/rtpTransceiver.ts +1 -1
  131. package/src/media/sender/senderBWE.ts +1 -1
  132. package/src/media/track.ts +1 -1
  133. package/src/nonstandard/recorder/index.ts +3 -1
  134. package/src/nonstandard/recorder/writer/index.ts +1 -1
  135. package/src/nonstandard/recorder/writer/webm.ts +1 -1
  136. package/src/peerConnection.ts +4 -1
  137. package/src/transport/dtls.ts +11 -5
  138. package/src/transport/ice.ts +9 -4
  139. package/src/transport/sctp.ts +1 -1
@@ -1,26 +1,27 @@
1
- import PCancelable from "p-cancelable";
2
- import Event from "rx.mini";
1
+ import { Event } from "../imports/common";
3
2
  import type { InterfaceAddresses } from "../../../common/src/network";
4
3
  import type { Candidate } from "../candidate";
5
- import { type Future } from "../helper";
6
4
  import type { Connection } from "../ice";
7
5
  import { Message } from "../stun/message";
8
6
  import { Transaction } from "../stun/transaction";
9
7
  import { type Transport } from "../transport";
10
8
  import type { Address, Protocol } from "../types/model";
11
- declare class TurnTransport implements Protocol {
12
- turn: TurnClient;
13
- readonly type = "turn";
9
+ export declare class StunOverTurnProtocol implements Protocol {
10
+ turn: TurnProtocol;
11
+ private ice;
12
+ static type: string;
13
+ readonly type: string;
14
14
  localCandidate: Candidate;
15
- receiver?: Connection;
16
- constructor(turn: TurnClient);
17
- private datagramReceived;
15
+ private disposer;
16
+ constructor(turn: TurnProtocol, ice: Connection);
17
+ private handleStunMessage;
18
18
  request(request: Message, addr: Address, integrityKey?: Buffer): Promise<[Message, readonly [string, number]]>;
19
19
  connectionMade(): Promise<void>;
20
20
  sendData(data: Buffer, addr: Address): Promise<void>;
21
21
  sendStun(message: Message, addr: Address): Promise<void>;
22
+ close(): Promise<void>;
22
23
  }
23
- declare class TurnClient implements Protocol {
24
+ export declare class TurnProtocol implements Protocol {
24
25
  server: Address;
25
26
  username: string;
26
27
  password: string;
@@ -30,33 +31,29 @@ declare class TurnClient implements Protocol {
30
31
  /**sec */
31
32
  channelRefreshTime?: number;
32
33
  };
33
- type: string;
34
+ static type: string;
35
+ readonly type: string;
34
36
  readonly onData: Event<[Buffer, readonly [string, number]]>;
35
- transactions: {
36
- [hexId: string]: Transaction;
37
- };
38
37
  integrityKey?: Buffer;
39
38
  nonce?: Buffer;
40
39
  realm?: string;
41
40
  relayedAddress: Address;
42
41
  mappedAddress: Address;
43
- refreshHandle?: Future;
44
- channelNumber: number;
45
- channelByAddr: {
46
- [addr: string]: {
47
- number: number;
48
- address: Address;
49
- };
50
- };
51
- addrByChannel: {
52
- [channel: number]: Address;
53
- };
54
42
  localCandidate: Candidate;
43
+ transactions: {
44
+ [hexId: string]: Transaction;
45
+ };
46
+ private refreshHandle?;
47
+ private channelNumber;
48
+ private channelByAddr;
49
+ private addrByChannel;
55
50
  /**sec */
56
- channelRefreshTime: number;
57
- onDatagramReceived: (data: Buffer, addr: Address) => void;
51
+ private channelRefreshTime;
58
52
  private channelBinding?;
59
- channelRefreshAt: number;
53
+ private channelRefreshAt;
54
+ private tcpBuffer;
55
+ private permissionByAddr;
56
+ private creatingPermission;
60
57
  constructor(server: Address, username: string, password: string, lifetime: number, transport: Transport, options?: {
61
58
  /**sec */
62
59
  channelRefreshTime?: number;
@@ -64,26 +61,45 @@ declare class TurnClient implements Protocol {
64
61
  connectionMade(): Promise<void>;
65
62
  private handleChannelData;
66
63
  private handleSTUNMessage;
67
- private datagramReceived;
68
- connect(): Promise<void>;
69
- createPermission(peerAddress: Address): Promise<Message>;
70
- refresh: (exp: number) => PCancelable<unknown>;
64
+ private dataReceived;
65
+ private send;
66
+ private createPermission;
67
+ private refresh;
71
68
  request(request: Message, addr: Address): Promise<[Message, Address]>;
72
69
  requestWithRetry(request: Message, addr: Address): Promise<[Message, Address]>;
73
70
  sendData(data: Buffer, addr: Address): Promise<void>;
71
+ getPermission(addr: Address): Promise<void>;
74
72
  getChannel(addr: Address): Promise<{
75
73
  number: number;
76
74
  address: Address;
77
75
  }>;
78
76
  private channelBind;
79
77
  sendStun(message: Message, addr: Address): Promise<void>;
78
+ close(): Promise<void>;
79
+ }
80
+ export interface TurnClientConfig {
81
+ address: Address;
82
+ username: string;
83
+ password: string;
80
84
  }
81
- export declare function createTurnEndpoint(serverAddr: Address, username: string, password: string, { lifetime, portRange, interfaceAddresses, }: {
85
+ export interface TurnClientOptions {
86
+ lifetime?: number;
87
+ ssl?: boolean;
88
+ transport?: "udp" | "tcp";
89
+ portRange?: [number, number];
90
+ interfaceAddresses?: InterfaceAddresses;
91
+ }
92
+ export declare function createTurnClient({ address, username, password }: TurnClientConfig, { lifetime, portRange, interfaceAddresses, transport: transportType, }?: TurnClientOptions): Promise<TurnProtocol>;
93
+ export declare function createStunOverTurnClient({ address, username, password, ice, }: {
94
+ address: Address;
95
+ username: string;
96
+ password: string;
97
+ ice: Connection;
98
+ }, { lifetime, portRange, interfaceAddresses, transport: transportType, }?: {
82
99
  lifetime?: number;
83
100
  ssl?: boolean;
84
- transport?: "udp";
101
+ transport?: "udp" | "tcp";
85
102
  portRange?: [number, number];
86
103
  interfaceAddresses?: InterfaceAddresses;
87
- }): Promise<TurnTransport>;
104
+ }): Promise<StunOverTurnProtocol>;
88
105
  export declare function makeIntegrityKey(username: string, realm: string, password: string): Buffer;
89
- export {};
@@ -3,14 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createTurnEndpoint = createTurnEndpoint;
6
+ exports.TurnProtocol = exports.StunOverTurnProtocol = void 0;
7
+ exports.createTurnClient = createTurnClient;
8
+ exports.createStunOverTurnClient = createStunOverTurnClient;
7
9
  exports.makeIntegrityKey = makeIntegrityKey;
8
10
  const crypto_1 = require("crypto");
9
11
  const jspack_1 = require("@shinyoshiaki/jspack");
10
12
  const debug_1 = __importDefault(require("debug"));
11
13
  const p_cancelable_1 = __importDefault(require("p-cancelable"));
12
- const rx_mini_1 = __importDefault(require("rx.mini"));
13
14
  const promises_1 = require("timers/promises");
15
+ const common_1 = require("../imports/common");
14
16
  const src_1 = require("../../../common/src");
15
17
  const exceptions_1 = require("../exceptions");
16
18
  const helper_1 = require("../helper");
@@ -21,21 +23,26 @@ const transport_1 = require("../transport");
21
23
  const log = (0, debug_1.default)("werift-ice:packages/ice/src/turn/protocol.ts");
22
24
  const DEFAULT_CHANNEL_REFRESH_TIME = 500;
23
25
  const DEFAULT_ALLOCATION_LIFETIME = 600;
24
- const TCP_TRANSPORT = 0x06000000;
25
26
  const UDP_TRANSPORT = 0x11000000;
26
- class TurnTransport {
27
- constructor(turn) {
27
+ class StunOverTurnProtocol {
28
+ constructor(turn, ice) {
28
29
  Object.defineProperty(this, "turn", {
29
30
  enumerable: true,
30
31
  configurable: true,
31
32
  writable: true,
32
33
  value: turn
33
34
  });
35
+ Object.defineProperty(this, "ice", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: ice
40
+ });
34
41
  Object.defineProperty(this, "type", {
35
42
  enumerable: true,
36
43
  configurable: true,
37
44
  writable: true,
38
- value: "turn"
45
+ value: StunOverTurnProtocol.type
39
46
  });
40
47
  Object.defineProperty(this, "localCandidate", {
41
48
  enumerable: true,
@@ -43,13 +50,13 @@ class TurnTransport {
43
50
  writable: true,
44
51
  value: void 0
45
52
  });
46
- Object.defineProperty(this, "receiver", {
53
+ Object.defineProperty(this, "disposer", {
47
54
  enumerable: true,
48
55
  configurable: true,
49
56
  writable: true,
50
- value: void 0
57
+ value: new common_1.EventDisposer()
51
58
  });
52
- Object.defineProperty(this, "datagramReceived", {
59
+ Object.defineProperty(this, "handleStunMessage", {
53
60
  enumerable: true,
54
61
  configurable: true,
55
62
  writable: true,
@@ -57,17 +64,18 @@ class TurnTransport {
57
64
  try {
58
65
  const message = (0, message_1.parseMessage)(data);
59
66
  if (!message) {
60
- this.receiver?.dataReceived(data, this.localCandidate.component);
67
+ this.ice.dataReceived(data, this.localCandidate.component);
61
68
  return;
62
69
  }
63
- if ((message?.messageClass === const_1.classes.RESPONSE ||
64
- message?.messageClass === const_1.classes.ERROR) &&
65
- this.turn.transactions[message.transactionIdHex]) {
70
+ if (message.messageClass === const_1.classes.RESPONSE ||
71
+ message.messageClass === const_1.classes.ERROR) {
66
72
  const transaction = this.turn.transactions[message.transactionIdHex];
67
- transaction.responseReceived(message, addr);
73
+ if (transaction) {
74
+ transaction.responseReceived(message, addr);
75
+ }
68
76
  }
69
- else if (message?.messageClass === const_1.classes.REQUEST) {
70
- this.receiver?.requestReceived(message, addr, this, data);
77
+ else if (message.messageClass === const_1.classes.REQUEST) {
78
+ this.ice.requestReceived(message, addr, this, data);
71
79
  }
72
80
  }
73
81
  catch (error) {
@@ -75,11 +83,16 @@ class TurnTransport {
75
83
  }
76
84
  }
77
85
  });
78
- turn.onDatagramReceived = this.datagramReceived;
86
+ turn.onData
87
+ .subscribe((data, addr) => {
88
+ this.handleStunMessage(data, addr);
89
+ })
90
+ .disposer(this.disposer);
79
91
  }
80
92
  async request(request, addr, integrityKey) {
81
- if (this.turn.transactions[request.transactionIdHex])
93
+ if (this.turn.transactions[request.transactionIdHex]) {
82
94
  throw new Error("exist");
95
+ }
83
96
  if (integrityKey) {
84
97
  request.addMessageIntegrity(integrityKey);
85
98
  request.addFingerprint();
@@ -103,8 +116,19 @@ class TurnTransport {
103
116
  async sendStun(message, addr) {
104
117
  await this.turn.sendData(message.bytes, addr);
105
118
  }
119
+ async close() {
120
+ this.disposer.dispose();
121
+ return this.turn.close();
122
+ }
106
123
  }
107
- class TurnClient {
124
+ exports.StunOverTurnProtocol = StunOverTurnProtocol;
125
+ Object.defineProperty(StunOverTurnProtocol, "type", {
126
+ enumerable: true,
127
+ configurable: true,
128
+ writable: true,
129
+ value: "turn"
130
+ });
131
+ class TurnProtocol {
108
132
  constructor(server, username, password, lifetime, transport, options = {}) {
109
133
  Object.defineProperty(this, "server", {
110
134
  enumerable: true,
@@ -146,19 +170,13 @@ class TurnClient {
146
170
  enumerable: true,
147
171
  configurable: true,
148
172
  writable: true,
149
- value: "inner_turn"
173
+ value: TurnProtocol.type
150
174
  });
151
175
  Object.defineProperty(this, "onData", {
152
176
  enumerable: true,
153
177
  configurable: true,
154
178
  writable: true,
155
- value: new rx_mini_1.default()
156
- });
157
- Object.defineProperty(this, "transactions", {
158
- enumerable: true,
159
- configurable: true,
160
- writable: true,
161
- value: {}
179
+ value: new common_1.Event()
162
180
  });
163
181
  Object.defineProperty(this, "integrityKey", {
164
182
  enumerable: true,
@@ -190,6 +208,18 @@ class TurnClient {
190
208
  writable: true,
191
209
  value: void 0
192
210
  });
211
+ Object.defineProperty(this, "localCandidate", {
212
+ enumerable: true,
213
+ configurable: true,
214
+ writable: true,
215
+ value: void 0
216
+ });
217
+ Object.defineProperty(this, "transactions", {
218
+ enumerable: true,
219
+ configurable: true,
220
+ writable: true,
221
+ value: {}
222
+ });
193
223
  Object.defineProperty(this, "refreshHandle", {
194
224
  enumerable: true,
195
225
  configurable: true,
@@ -214,36 +244,42 @@ class TurnClient {
214
244
  writable: true,
215
245
  value: {}
216
246
  });
217
- Object.defineProperty(this, "localCandidate", {
247
+ /**sec */
248
+ Object.defineProperty(this, "channelRefreshTime", {
218
249
  enumerable: true,
219
250
  configurable: true,
220
251
  writable: true,
221
252
  value: void 0
222
253
  });
223
- /**sec */
224
- Object.defineProperty(this, "channelRefreshTime", {
254
+ Object.defineProperty(this, "channelBinding", {
225
255
  enumerable: true,
226
256
  configurable: true,
227
257
  writable: true,
228
258
  value: void 0
229
259
  });
230
- Object.defineProperty(this, "onDatagramReceived", {
260
+ Object.defineProperty(this, "channelRefreshAt", {
261
+ enumerable: true,
262
+ configurable: true,
263
+ writable: true,
264
+ value: 0
265
+ });
266
+ Object.defineProperty(this, "tcpBuffer", {
231
267
  enumerable: true,
232
268
  configurable: true,
233
269
  writable: true,
234
- value: () => { }
270
+ value: Buffer.alloc(0)
235
271
  });
236
- Object.defineProperty(this, "channelBinding", {
272
+ Object.defineProperty(this, "permissionByAddr", {
237
273
  enumerable: true,
238
274
  configurable: true,
239
275
  writable: true,
240
- value: void 0
276
+ value: {}
241
277
  });
242
- Object.defineProperty(this, "channelRefreshAt", {
278
+ Object.defineProperty(this, "creatingPermission", {
243
279
  enumerable: true,
244
280
  configurable: true,
245
281
  writable: true,
246
- value: 0
282
+ value: Promise.resolve()
247
283
  });
248
284
  Object.defineProperty(this, "refresh", {
249
285
  enumerable: true,
@@ -278,23 +314,36 @@ class TurnClient {
278
314
  }
279
315
  async connectionMade() {
280
316
  this.transport.onData = (data, addr) => {
281
- this.datagramReceived(data, addr);
317
+ this.dataReceived(data, addr);
282
318
  };
319
+ const request = new message_1.Message(const_1.methods.ALLOCATE, const_1.classes.REQUEST);
320
+ request
321
+ .setAttribute("LIFETIME", this.lifetime)
322
+ .setAttribute("REQUESTED-TRANSPORT", UDP_TRANSPORT);
323
+ const [response] = await this.requestWithRetry(request, this.server).catch((e) => {
324
+ log("connect error", e);
325
+ throw e;
326
+ });
327
+ this.relayedAddress = response.getAttributeValue("XOR-RELAYED-ADDRESS");
328
+ this.mappedAddress = response.getAttributeValue("XOR-MAPPED-ADDRESS");
329
+ const exp = response.getAttributeValue("LIFETIME");
330
+ log("connect", this.relayedAddress, this.mappedAddress, { exp });
331
+ this.refreshHandle = (0, helper_1.future)(this.refresh(exp));
283
332
  }
284
333
  handleChannelData(data) {
285
334
  const [channel, length] = jspack_1.jspack.Unpack("!HH", data.slice(0, 4));
286
335
  const addr = this.addrByChannel[channel];
287
336
  if (addr) {
288
337
  const payload = data.subarray(4, 4 + length);
289
- this.onDatagramReceived(payload, addr);
290
338
  this.onData.execute(payload, addr);
291
339
  }
292
340
  }
293
341
  handleSTUNMessage(data, addr) {
294
342
  try {
295
343
  const message = (0, message_1.parseMessage)(data);
296
- if (!message)
344
+ if (!message) {
297
345
  throw new Error("not stun message");
346
+ }
298
347
  if (message.messageClass === const_1.classes.RESPONSE ||
299
348
  message.messageClass === const_1.classes.ERROR) {
300
349
  const transaction = this.transactions[message.transactionIdHex];
@@ -303,11 +352,10 @@ class TurnClient {
303
352
  }
304
353
  }
305
354
  else if (message.messageClass === const_1.classes.REQUEST) {
306
- this.onDatagramReceived(data, addr);
355
+ this.onData.execute(data, addr);
307
356
  }
308
357
  if (message.getAttributeValue("DATA")) {
309
358
  const buf = message.getAttributeValue("DATA");
310
- this.onDatagramReceived(buf, addr);
311
359
  this.onData.execute(buf, addr);
312
360
  }
313
361
  }
@@ -315,28 +363,42 @@ class TurnClient {
315
363
  log("parse error", data.toString());
316
364
  }
317
365
  }
318
- datagramReceived(data, addr) {
319
- if (data.length >= 4 && isChannelData(data)) {
320
- this.handleChannelData(data);
366
+ dataReceived(data, addr) {
367
+ const datagramReceived = (data, addr) => {
368
+ if (data.length >= 4 && isChannelData(data)) {
369
+ this.handleChannelData(data);
370
+ }
371
+ else {
372
+ this.handleSTUNMessage(data, addr);
373
+ }
374
+ };
375
+ if (this.transport.type === "tcp") {
376
+ this.tcpBuffer = Buffer.concat([this.tcpBuffer, data]);
377
+ while (this.tcpBuffer.length >= 4) {
378
+ let [, length] = (0, src_1.bufferReader)(this.tcpBuffer.subarray(0, 4), [2, 2]);
379
+ length += (0, message_1.paddingLength)(length);
380
+ const fullLength = isChannelData(this.tcpBuffer)
381
+ ? 4 + length
382
+ : 20 + length;
383
+ if (this.tcpBuffer.length < fullLength) {
384
+ break;
385
+ }
386
+ datagramReceived(this.tcpBuffer.subarray(0, fullLength), addr);
387
+ this.tcpBuffer = this.tcpBuffer.subarray(fullLength);
388
+ }
321
389
  }
322
390
  else {
323
- this.handleSTUNMessage(data, addr);
391
+ datagramReceived(data, addr);
324
392
  }
325
393
  }
326
- async connect() {
327
- const request = new message_1.Message(const_1.methods.ALLOCATE, const_1.classes.REQUEST);
328
- request
329
- .setAttribute("LIFETIME", this.lifetime)
330
- .setAttribute("REQUESTED-TRANSPORT", UDP_TRANSPORT);
331
- const [response] = await this.requestWithRetry(request, this.server).catch((e) => {
332
- log("connect error", e);
333
- throw e;
334
- });
335
- this.relayedAddress = response.getAttributeValue("XOR-RELAYED-ADDRESS");
336
- this.mappedAddress = response.getAttributeValue("XOR-MAPPED-ADDRESS");
337
- const exp = response.getAttributeValue("LIFETIME");
338
- log("connect", this.relayedAddress, this.mappedAddress, { exp });
339
- this.refreshHandle = (0, helper_1.future)(this.refresh(exp));
394
+ async send(data, addr) {
395
+ if (this.transport.type === "tcp") {
396
+ const padding = (0, message_1.paddingLength)(data.length);
397
+ await this.transport.send(padding > 0 ? Buffer.concat([data, Buffer.alloc(padding)]) : data, addr);
398
+ }
399
+ else {
400
+ await this.transport.send(data, addr);
401
+ }
340
402
  }
341
403
  async createPermission(peerAddress) {
342
404
  const request = new message_1.Message(const_1.methods.CREATE_PERMISSION, const_1.classes.REQUEST);
@@ -345,11 +407,10 @@ class TurnClient {
345
407
  .setAttribute("USERNAME", this.username)
346
408
  .setAttribute("REALM", this.realm)
347
409
  .setAttribute("NONCE", this.nonce);
348
- const [response] = await this.request(request, this.server).catch((e) => {
410
+ await this.request(request, this.server).catch((e) => {
349
411
  request;
350
412
  throw e;
351
413
  });
352
- return response;
353
414
  }
354
415
  async request(request, addr) {
355
416
  if (this.transactions[request.transactionIdHex]) {
@@ -408,21 +469,43 @@ class TurnClient {
408
469
  return [message, address];
409
470
  }
410
471
  async sendData(data, addr) {
411
- const channel = await this.getChannel(addr);
472
+ const channel = await this.getChannel(addr).catch((e) => {
473
+ return new Error("channelBind error");
474
+ });
475
+ if (channel instanceof Error) {
476
+ await this.getPermission(addr);
477
+ const indicate = new message_1.Message(const_1.methods.SEND, const_1.classes.INDICATION)
478
+ .setAttribute("DATA", data)
479
+ .setAttribute("XOR-PEER-ADDRESS", addr);
480
+ await this.sendStun(indicate, this.server);
481
+ return;
482
+ }
412
483
  const header = jspack_1.jspack.Pack("!HH", [channel.number, data.length]);
413
- this.transport.send(Buffer.concat([Buffer.from(header), data]), this.server);
484
+ await this.send(Buffer.concat([Buffer.from(header), data]), this.server);
485
+ }
486
+ async getPermission(addr) {
487
+ await this.creatingPermission;
488
+ const permitted = this.permissionByAddr[addr.join(":")];
489
+ if (!permitted) {
490
+ this.creatingPermission = this.createPermission(addr);
491
+ this.permissionByAddr[addr.join(":")] = true;
492
+ await this.creatingPermission.catch((e) => {
493
+ log("createPermission error", e);
494
+ throw e;
495
+ });
496
+ }
414
497
  }
415
498
  async getChannel(addr) {
416
499
  if (this.channelBinding) {
417
500
  await this.channelBinding;
418
501
  }
419
- let channel = this.channelByAddr[addr.join("")];
502
+ let channel = this.channelByAddr[addr.join(":")];
420
503
  if (!channel) {
421
- this.channelByAddr[addr.join("")] = {
504
+ this.channelByAddr[addr.join(":")] = {
422
505
  number: this.channelNumber++,
423
506
  address: addr,
424
507
  };
425
- channel = this.channelByAddr[addr.join("")];
508
+ channel = this.channelByAddr[addr.join(":")];
426
509
  this.addrByChannel[channel.number] = addr;
427
510
  this.channelBinding = this.channelBind(channel.number, addr);
428
511
  await this.channelBinding.catch((e) => {
@@ -456,18 +539,42 @@ class TurnClient {
456
539
  }
457
540
  }
458
541
  async sendStun(message, addr) {
459
- await this.transport.send(message.bytes, addr);
542
+ await this.send(message.bytes, addr);
460
543
  }
461
- }
462
- async function createTurnEndpoint(serverAddr, username, password, { lifetime, portRange, interfaceAddresses, }) {
463
- if (lifetime == undefined) {
464
- lifetime = DEFAULT_ALLOCATION_LIFETIME;
544
+ async close() {
545
+ this.refreshHandle?.cancel();
546
+ await this.transport.close();
465
547
  }
466
- const transport = await transport_1.UdpTransport.init("udp4", portRange, interfaceAddresses);
467
- const turnClient = new TurnClient(serverAddr, username, password, lifetime, transport);
468
- await turnClient.connectionMade();
469
- await turnClient.connect();
470
- const turnTransport = new TurnTransport(turnClient);
548
+ }
549
+ exports.TurnProtocol = TurnProtocol;
550
+ Object.defineProperty(TurnProtocol, "type", {
551
+ enumerable: true,
552
+ configurable: true,
553
+ writable: true,
554
+ value: "turn"
555
+ });
556
+ async function createTurnClient({ address, username, password }, { lifetime, portRange, interfaceAddresses, transport: transportType, } = {}) {
557
+ lifetime ?? (lifetime = DEFAULT_ALLOCATION_LIFETIME);
558
+ transportType ?? (transportType = "udp");
559
+ const transport = transportType === "udp"
560
+ ? await transport_1.UdpTransport.init("udp4", portRange, interfaceAddresses)
561
+ : await transport_1.TcpTransport.init(address);
562
+ const turn = new TurnProtocol(address, username, password, lifetime, transport);
563
+ await turn.connectionMade();
564
+ return turn;
565
+ }
566
+ async function createStunOverTurnClient({ address, username, password, ice, }, { lifetime, portRange, interfaceAddresses, transport: transportType, } = {}) {
567
+ const turn = await createTurnClient({
568
+ address,
569
+ username,
570
+ password,
571
+ }, {
572
+ lifetime,
573
+ portRange,
574
+ interfaceAddresses,
575
+ transport: transportType,
576
+ });
577
+ const turnTransport = new StunOverTurnProtocol(turn, ice);
471
578
  return turnTransport;
472
579
  }
473
580
  function makeIntegrityKey(username, realm, password) {