@livekit/rtc-node 0.10.4 → 0.11.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.
Files changed (136) hide show
  1. package/README.md +54 -2
  2. package/build.rs +1 -1
  3. package/dist/audio_frame.d.ts +1 -1
  4. package/dist/audio_frame.d.ts.map +1 -1
  5. package/dist/audio_frame.js +4 -4
  6. package/dist/audio_frame.js.map +1 -1
  7. package/dist/audio_resampler.d.ts.map +1 -1
  8. package/dist/audio_resampler.js +8 -7
  9. package/dist/audio_resampler.js.map +1 -1
  10. package/dist/audio_source.d.ts.map +1 -1
  11. package/dist/audio_source.js +8 -4
  12. package/dist/audio_source.js.map +1 -1
  13. package/dist/audio_stream.d.ts +1 -1
  14. package/dist/audio_stream.d.ts.map +1 -1
  15. package/dist/audio_stream.js +4 -3
  16. package/dist/audio_stream.js.map +1 -1
  17. package/dist/e2ee.d.ts.map +1 -1
  18. package/dist/e2ee.js +25 -25
  19. package/dist/e2ee.js.map +1 -1
  20. package/dist/ffi_client.d.ts +3 -3
  21. package/dist/ffi_client.d.ts.map +1 -1
  22. package/dist/ffi_client.js +9 -7
  23. package/dist/ffi_client.js.map +1 -1
  24. package/dist/index.d.ts +29 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +34 -4
  27. package/dist/index.js.map +1 -1
  28. package/dist/napi/native.d.ts +1 -0
  29. package/dist/participant.d.ts +48 -2
  30. package/dist/participant.d.ts.map +1 -1
  31. package/dist/participant.js +143 -24
  32. package/dist/participant.js.map +1 -1
  33. package/dist/proto/audio_frame_pb.d.ts +451 -526
  34. package/dist/proto/audio_frame_pb.d.ts.map +1 -1
  35. package/dist/proto/audio_frame_pb.js +200 -1179
  36. package/dist/proto/audio_frame_pb.js.map +1 -1
  37. package/dist/proto/e2ee_pb.d.ts +264 -333
  38. package/dist/proto/e2ee_pb.d.ts.map +1 -1
  39. package/dist/proto/e2ee_pb.js +136 -810
  40. package/dist/proto/e2ee_pb.js.map +1 -1
  41. package/dist/proto/ffi_pb.d.ts +175 -129
  42. package/dist/proto/ffi_pb.d.ts.map +1 -1
  43. package/dist/proto/ffi_pb.js +58 -395
  44. package/dist/proto/ffi_pb.js.map +1 -1
  45. package/dist/proto/handle_pb.d.ts +14 -13
  46. package/dist/proto/handle_pb.d.ts.map +1 -1
  47. package/dist/proto/handle_pb.js +8 -39
  48. package/dist/proto/handle_pb.js.map +1 -1
  49. package/dist/proto/participant_pb.d.ts +54 -52
  50. package/dist/proto/participant_pb.d.ts.map +1 -1
  51. package/dist/proto/participant_pb.js +18 -90
  52. package/dist/proto/participant_pb.js.map +1 -1
  53. package/dist/proto/room_pb.d.ts +1042 -1187
  54. package/dist/proto/room_pb.d.ts.map +1 -1
  55. package/dist/proto/room_pb.js +470 -2938
  56. package/dist/proto/room_pb.js.map +1 -1
  57. package/dist/proto/rpc_pb.d.ts +235 -0
  58. package/dist/proto/rpc_pb.d.ts.map +1 -0
  59. package/dist/proto/rpc_pb.js +74 -0
  60. package/dist/proto/rpc_pb.js.map +1 -0
  61. package/dist/proto/stats_pb.d.ts +738 -800
  62. package/dist/proto/stats_pb.d.ts.map +1 -1
  63. package/dist/proto/stats_pb.js +218 -1960
  64. package/dist/proto/stats_pb.js.map +1 -1
  65. package/dist/proto/track_pb.d.ts +225 -258
  66. package/dist/proto/track_pb.d.ts.map +1 -1
  67. package/dist/proto/track_pb.js +98 -591
  68. package/dist/proto/track_pb.js.map +1 -1
  69. package/dist/proto/video_frame_pb.d.ts +357 -390
  70. package/dist/proto/video_frame_pb.d.ts.map +1 -1
  71. package/dist/proto/video_frame_pb.js +137 -790
  72. package/dist/proto/video_frame_pb.js.map +1 -1
  73. package/dist/room.d.ts +21 -7
  74. package/dist/room.d.ts.map +1 -1
  75. package/dist/room.js +134 -54
  76. package/dist/room.js.map +1 -1
  77. package/dist/rpc.d.ts +78 -0
  78. package/dist/rpc.d.ts.map +1 -0
  79. package/dist/rpc.js +69 -0
  80. package/dist/rpc.js.map +1 -0
  81. package/dist/track.d.ts.map +1 -1
  82. package/dist/track.js +7 -3
  83. package/dist/track.js.map +1 -1
  84. package/dist/track_publication.d.ts.map +1 -1
  85. package/dist/track_publication.js +3 -2
  86. package/dist/track_publication.js.map +1 -1
  87. package/dist/version.d.ts +2 -0
  88. package/dist/version.d.ts.map +1 -0
  89. package/dist/version.js +2 -0
  90. package/dist/version.js.map +1 -0
  91. package/dist/video_frame.d.ts +2 -2
  92. package/dist/video_frame.d.ts.map +1 -1
  93. package/dist/video_frame.js +47 -25
  94. package/dist/video_frame.js.map +1 -1
  95. package/dist/video_source.d.ts.map +1 -1
  96. package/dist/video_source.js +4 -3
  97. package/dist/video_source.js.map +1 -1
  98. package/dist/video_stream.d.ts +1 -1
  99. package/dist/video_stream.d.ts.map +1 -1
  100. package/dist/video_stream.js +7 -3
  101. package/dist/video_stream.js.map +1 -1
  102. package/package.json +13 -10
  103. package/src/audio_frame.ts +6 -6
  104. package/src/audio_resampler.ts +10 -9
  105. package/src/audio_source.ts +9 -8
  106. package/src/audio_stream.ts +7 -6
  107. package/src/e2ee.ts +34 -34
  108. package/src/ffi_client.ts +23 -10
  109. package/src/index.ts +61 -2
  110. package/src/napi/native.d.ts +1 -0
  111. package/src/nodejs.rs +6 -3
  112. package/src/participant.ts +207 -47
  113. package/src/proto/audio_frame_pb.ts +562 -1279
  114. package/src/proto/e2ee_pb.ts +365 -886
  115. package/src/proto/ffi_pb.ts +206 -405
  116. package/src/proto/handle_pb.ts +21 -35
  117. package/src/proto/participant_pb.ts +71 -112
  118. package/src/proto/room_pb.ts +1284 -2995
  119. package/src/proto/rpc_pb.ts +310 -0
  120. package/src/proto/stats_pb.ts +1028 -1947
  121. package/src/proto/track_pb.ts +287 -621
  122. package/src/proto/video_frame_pb.ts +445 -905
  123. package/src/room.ts +181 -75
  124. package/src/rpc.ts +121 -0
  125. package/src/track.ts +8 -7
  126. package/src/track_publication.ts +5 -4
  127. package/src/version.ts +1 -0
  128. package/src/video_frame.ts +54 -27
  129. package/src/video_source.ts +7 -6
  130. package/src/video_stream.ts +7 -6
  131. package/dist/utils.d.ts +0 -7
  132. package/dist/utils.d.ts.map +0 -1
  133. package/dist/utils.js +0 -48
  134. package/dist/utils.js.map +0 -1
  135. package/src/utils.test.ts +0 -47
  136. package/src/utils.ts +0 -44
@@ -1,11 +1,18 @@
1
1
  // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
- import { FfiClient, FfiRequest } from './ffi_client.js';
5
- import type { OwnedVideoBuffer, VideoConvertResponse } from './proto/video_frame_pb.js';
6
- import {
4
+ import { create } from '@bufbuild/protobuf';
5
+ import { FfiClient } from './ffi_client.js';
6
+ import { FfiRequestSchema } from './proto/ffi_pb.js';
7
+ import type {
8
+ OwnedVideoBuffer,
7
9
  VideoBufferInfo,
8
10
  VideoBufferInfo_ComponentInfo,
11
+ VideoConvertResponse,
12
+ } from './proto/video_frame_pb.js';
13
+ import {
14
+ VideoBufferInfoSchema,
15
+ VideoBufferInfo_ComponentInfoSchema,
9
16
  VideoBufferType,
10
17
  } from './proto/video_frame_pb.js';
11
18
 
@@ -29,7 +36,7 @@ export class VideoFrame {
29
36
 
30
37
  /** @internal */
31
38
  protoInfo(): VideoBufferInfo {
32
- const info = new VideoBufferInfo({
39
+ const info = create(VideoBufferInfoSchema, {
33
40
  width: this.width,
34
41
  height: this.height,
35
42
  type: this.type,
@@ -55,7 +62,7 @@ export class VideoFrame {
55
62
 
56
63
  /** @internal */
57
64
  static fromOwnedInfo(owned: OwnedVideoBuffer): VideoFrame {
58
- const info = owned.info;
65
+ const info = owned.info!;
59
66
  return new VideoFrame(
60
67
  FfiClient.instance.copyBuffer(
61
68
  info.dataPtr,
@@ -76,7 +83,7 @@ export class VideoFrame {
76
83
  }
77
84
 
78
85
  convert(dstType: VideoBufferType, flipY = false): VideoFrame {
79
- const req = new FfiRequest({
86
+ const req = create(FfiRequestSchema, {
80
87
  message: {
81
88
  case: 'videoConvert',
82
89
  value: {
@@ -87,11 +94,11 @@ export class VideoFrame {
87
94
  },
88
95
  });
89
96
  const resp = FfiClient.instance.request<VideoConvertResponse>(req);
90
- if (resp.error) {
91
- throw resp.error;
97
+ if (resp.message.case !== 'buffer') {
98
+ throw new Error(resp.message.value ?? 'Unknown Error');
92
99
  }
93
100
 
94
- return VideoFrame.fromOwnedInfo(resp.buffer);
101
+ return VideoFrame.fromOwnedInfo(resp.message.value);
95
102
  }
96
103
  }
97
104
 
@@ -130,13 +137,17 @@ const getPlaneInfos = (
130
137
  const chromaHeight = Math.trunc((height + 1) / 2);
131
138
  switch (type) {
132
139
  case VideoBufferType.I420: {
133
- const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
134
- const u = new VideoBufferInfo_ComponentInfo({
140
+ const y = create(VideoBufferInfo_ComponentInfoSchema, {
141
+ dataPtr,
142
+ stride: width,
143
+ size: width * height,
144
+ });
145
+ const u = create(VideoBufferInfo_ComponentInfoSchema, {
135
146
  dataPtr: y.dataPtr + BigInt(y.size),
136
147
  stride: chromaWidth,
137
148
  size: chromaWidth * chromaHeight,
138
149
  });
139
- const v = new VideoBufferInfo_ComponentInfo({
150
+ const v = create(VideoBufferInfo_ComponentInfoSchema, {
140
151
  dataPtr: u.dataPtr + BigInt(u.size),
141
152
  stride: chromaWidth,
142
153
  size: chromaWidth * chromaHeight,
@@ -144,18 +155,22 @@ const getPlaneInfos = (
144
155
  return [y, u, v];
145
156
  }
146
157
  case VideoBufferType.I420A: {
147
- const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
148
- const u = new VideoBufferInfo_ComponentInfo({
158
+ const y = create(VideoBufferInfo_ComponentInfoSchema, {
159
+ dataPtr,
160
+ stride: width,
161
+ size: width * height,
162
+ });
163
+ const u = create(VideoBufferInfo_ComponentInfoSchema, {
149
164
  dataPtr: y.dataPtr + BigInt(y.size),
150
165
  stride: chromaWidth,
151
166
  size: chromaWidth * chromaHeight,
152
167
  });
153
- const v = new VideoBufferInfo_ComponentInfo({
168
+ const v = create(VideoBufferInfo_ComponentInfoSchema, {
154
169
  dataPtr: u.dataPtr + BigInt(u.size),
155
170
  stride: chromaWidth,
156
171
  size: chromaWidth * chromaHeight,
157
172
  });
158
- const a = new VideoBufferInfo_ComponentInfo({
173
+ const a = create(VideoBufferInfo_ComponentInfoSchema, {
159
174
  dataPtr: v.dataPtr + BigInt(v.size),
160
175
  stride: width,
161
176
  size: width * height,
@@ -163,13 +178,17 @@ const getPlaneInfos = (
163
178
  return [y, u, v, a];
164
179
  }
165
180
  case VideoBufferType.I422: {
166
- const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
167
- const u = new VideoBufferInfo_ComponentInfo({
181
+ const y = create(VideoBufferInfo_ComponentInfoSchema, {
182
+ dataPtr,
183
+ stride: width,
184
+ size: width * height,
185
+ });
186
+ const u = create(VideoBufferInfo_ComponentInfoSchema, {
168
187
  dataPtr: y.dataPtr + BigInt(y.size),
169
188
  stride: chromaWidth,
170
189
  size: chromaWidth * height,
171
190
  });
172
- const v = new VideoBufferInfo_ComponentInfo({
191
+ const v = create(VideoBufferInfo_ComponentInfoSchema, {
173
192
  dataPtr: u.dataPtr + BigInt(u.size),
174
193
  stride: chromaWidth,
175
194
  size: chromaWidth * height,
@@ -177,13 +196,17 @@ const getPlaneInfos = (
177
196
  return [y, u, v];
178
197
  }
179
198
  case VideoBufferType.I444: {
180
- const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
181
- const u = new VideoBufferInfo_ComponentInfo({
199
+ const y = create(VideoBufferInfo_ComponentInfoSchema, {
200
+ dataPtr,
201
+ stride: width,
202
+ size: width * height,
203
+ });
204
+ const u = create(VideoBufferInfo_ComponentInfoSchema, {
182
205
  dataPtr: y.dataPtr + BigInt(y.size),
183
206
  stride: width,
184
207
  size: width * height,
185
208
  });
186
- const v = new VideoBufferInfo_ComponentInfo({
209
+ const v = create(VideoBufferInfo_ComponentInfoSchema, {
187
210
  dataPtr: u.dataPtr + BigInt(u.size),
188
211
  stride: width,
189
212
  size: width * height,
@@ -191,17 +214,17 @@ const getPlaneInfos = (
191
214
  return [y, u, v];
192
215
  }
193
216
  case VideoBufferType.I010: {
194
- const y = new VideoBufferInfo_ComponentInfo({
217
+ const y = create(VideoBufferInfo_ComponentInfoSchema, {
195
218
  dataPtr,
196
219
  stride: width * 2,
197
220
  size: width * height * 2,
198
221
  });
199
- const u = new VideoBufferInfo_ComponentInfo({
222
+ const u = create(VideoBufferInfo_ComponentInfoSchema, {
200
223
  dataPtr: y.dataPtr + BigInt(y.size),
201
224
  stride: chromaWidth * 2,
202
225
  size: chromaWidth * chromaHeight * 2,
203
226
  });
204
- const v = new VideoBufferInfo_ComponentInfo({
227
+ const v = create(VideoBufferInfo_ComponentInfoSchema, {
205
228
  dataPtr: u.dataPtr + BigInt(u.size),
206
229
  stride: chromaWidth * 2,
207
230
  size: chromaWidth * chromaHeight * 2,
@@ -209,8 +232,12 @@ const getPlaneInfos = (
209
232
  return [y, u, v];
210
233
  }
211
234
  case VideoBufferType.NV12: {
212
- const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
213
- const uv = new VideoBufferInfo_ComponentInfo({
235
+ const y = create(VideoBufferInfo_ComponentInfoSchema, {
236
+ dataPtr,
237
+ stride: width,
238
+ size: width * height,
239
+ });
240
+ const uv = create(VideoBufferInfo_ComponentInfoSchema, {
214
241
  dataPtr: y.dataPtr + BigInt(y.size),
215
242
  stride: chromaWidth * 2,
216
243
  size: chromaWidth * chromaHeight * 2,
@@ -1,6 +1,7 @@
1
1
  // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
+ import { create } from '@bufbuild/protobuf';
4
5
  import { FfiClient, FfiHandle } from './ffi_client.js';
5
6
  import type {
6
7
  CaptureVideoFrameResponse,
@@ -8,8 +9,8 @@ import type {
8
9
  VideoSourceInfo,
9
10
  } from './proto/video_frame_pb.js';
10
11
  import {
11
- CaptureVideoFrameRequest,
12
- NewVideoSourceRequest,
12
+ CaptureVideoFrameRequestSchema,
13
+ NewVideoSourceRequestSchema,
13
14
  VideoRotation,
14
15
  VideoSourceType,
15
16
  } from './proto/video_frame_pb.js';
@@ -28,7 +29,7 @@ export class VideoSource {
28
29
  this.width = width;
29
30
  this.height = height;
30
31
 
31
- const req = new NewVideoSourceRequest({
32
+ const req = create(NewVideoSourceRequestSchema, {
32
33
  type: VideoSourceType.VIDEO_SOURCE_NATIVE,
33
34
  resolution: {
34
35
  width: width,
@@ -43,12 +44,12 @@ export class VideoSource {
43
44
  },
44
45
  });
45
46
 
46
- this.info = res.source.info;
47
- this.ffiHandle = new FfiHandle(res.source.handle.id);
47
+ this.info = res.source!.info!;
48
+ this.ffiHandle = new FfiHandle(res.source!.handle!.id);
48
49
  }
49
50
 
50
51
  captureFrame(frame: VideoFrame, timestampUs = 0n, rotation = VideoRotation.VIDEO_ROTATION_0) {
51
- const req = new CaptureVideoFrameRequest({
52
+ const req = create(CaptureVideoFrameRequestSchema, {
52
53
  sourceHandle: this.ffiHandle.handle,
53
54
  buffer: frame.protoInfo(),
54
55
  rotation,
@@ -1,6 +1,8 @@
1
1
  // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
+ import { create } from '@bufbuild/protobuf';
5
+ import { Mutex } from '@livekit/mutex';
4
6
  import type { FfiEvent } from './ffi_client.js';
5
7
  import { FfiClient, FfiClientEvent, FfiHandle } from './ffi_client.js';
6
8
  import type {
@@ -8,9 +10,8 @@ import type {
8
10
  VideoRotation,
9
11
  VideoStreamInfo,
10
12
  } from './proto/video_frame_pb.js';
11
- import { NewVideoStreamRequest, VideoStreamType } from './proto/video_frame_pb.js';
13
+ import { NewVideoStreamRequestSchema, VideoStreamType } from './proto/video_frame_pb.js';
12
14
  import type { Track } from './track.js';
13
- import { Mutex } from './utils.js';
14
15
  import { VideoFrame } from './video_frame.js';
15
16
 
16
17
  export type VideoFrameEvent = {
@@ -36,7 +37,7 @@ export class VideoStream implements AsyncIterableIterator<VideoFrameEvent> {
36
37
  constructor(track: Track) {
37
38
  this.track = track;
38
39
 
39
- const req = new NewVideoStreamRequest({
40
+ const req = create(NewVideoStreamRequestSchema, {
40
41
  type: VideoStreamType.VIDEO_STREAM_NATIVE,
41
42
  trackHandle: track.ffi_handle.handle,
42
43
  });
@@ -48,8 +49,8 @@ export class VideoStream implements AsyncIterableIterator<VideoFrameEvent> {
48
49
  },
49
50
  });
50
51
 
51
- this.info = res.stream.info;
52
- this.ffiHandle = new FfiHandle(res.stream.handle.id);
52
+ this.info = res.stream!.info!;
53
+ this.ffiHandle = new FfiHandle(res.stream!.handle!.id);
53
54
 
54
55
  FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onEvent);
55
56
  }
@@ -67,7 +68,7 @@ export class VideoStream implements AsyncIterableIterator<VideoFrameEvent> {
67
68
  case 'frameReceived':
68
69
  const rotation = streamEvent.value.rotation;
69
70
  const timestampUs = streamEvent.value.timestampUs;
70
- const frame = VideoFrame.fromOwnedInfo(streamEvent.value.buffer);
71
+ const frame = VideoFrame.fromOwnedInfo(streamEvent.value.buffer!);
71
72
  const value = { rotation, timestampUs, frame };
72
73
  if (this.queueResolve) {
73
74
  this.queueResolve({ done: false, value });
package/dist/utils.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export declare class Mutex {
2
- #private;
3
- constructor(limit?: number);
4
- isLocked(): boolean;
5
- lock(): Promise<() => void>;
6
- }
7
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,qBAAa,KAAK;;gBAKJ,KAAK,SAAI;IAMrB,QAAQ,IAAI,OAAO;IAIb,IAAI,IAAI,OAAO,CAAC,MAAM,IAAI,CAAC;CAyBlC"}
package/dist/utils.js DELETED
@@ -1,48 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _Mutex_locking, _Mutex_locks, _Mutex_limit;
13
- // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
14
- //
15
- // SPDX-License-Identifier: Apache-2.0
16
- export class Mutex {
17
- constructor(limit = 1) {
18
- _Mutex_locking.set(this, void 0);
19
- _Mutex_locks.set(this, void 0);
20
- _Mutex_limit.set(this, void 0);
21
- __classPrivateFieldSet(this, _Mutex_locking, Promise.resolve(), "f");
22
- __classPrivateFieldSet(this, _Mutex_locks, 0, "f");
23
- __classPrivateFieldSet(this, _Mutex_limit, limit, "f");
24
- }
25
- isLocked() {
26
- return __classPrivateFieldGet(this, _Mutex_locks, "f") >= __classPrivateFieldGet(this, _Mutex_limit, "f");
27
- }
28
- async lock() {
29
- if (__classPrivateFieldGet(this, _Mutex_locks, "f") >= __classPrivateFieldGet(this, _Mutex_limit, "f")) {
30
- await __classPrivateFieldGet(this, _Mutex_locking, "f");
31
- }
32
- __classPrivateFieldSet(this, _Mutex_locks, __classPrivateFieldGet(this, _Mutex_locks, "f") + 1, "f");
33
- let unlock;
34
- let unlocked = false;
35
- const willLock = new Promise((resolve) => (unlock = () => {
36
- if (unlocked) {
37
- throw new Error('This unlock method has already been called');
38
- }
39
- unlocked = true;
40
- __classPrivateFieldSet(this, _Mutex_locks, __classPrivateFieldGet(this, _Mutex_locks, "f") - 1, "f");
41
- resolve();
42
- }));
43
- __classPrivateFieldSet(this, _Mutex_locking, __classPrivateFieldGet(this, _Mutex_locking, "f").then(() => willLock), "f");
44
- return unlock;
45
- }
46
- }
47
- _Mutex_locking = new WeakMap(), _Mutex_locks = new WeakMap(), _Mutex_limit = new WeakMap();
48
- //# sourceMappingURL=utils.js.map
package/dist/utils.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAA6C;AAC7C,EAAE;AACF,sCAAsC;AACtC,MAAM,OAAO,KAAK;IAKhB,YAAY,KAAK,GAAG,CAAC;QAJrB,iCAAwB;QACxB,+BAAe;QACf,+BAAe;QAGb,uBAAA,IAAI,kBAAY,OAAO,CAAC,OAAO,EAAE,MAAA,CAAC;QAClC,uBAAA,IAAI,gBAAU,CAAC,MAAA,CAAC;QAChB,uBAAA,IAAI,gBAAU,KAAK,MAAA,CAAC;IACtB,CAAC;IAED,QAAQ;QACN,OAAO,uBAAA,IAAI,oBAAO,IAAI,uBAAA,IAAI,oBAAO,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,uBAAA,IAAI,oBAAO,IAAI,uBAAA,IAAI,oBAAO,EAAE,CAAC;YAC/B,MAAM,uBAAA,IAAI,sBAAS,CAAC;QACtB,CAAC;QAED,6FAAe,CAAC,MAAA,CAAC;QAEjB,IAAI,MAAkB,CAAC;QACvB,IAAI,QAAQ,GAAG,KAAK,CAAC;QAErB,MAAM,QAAQ,GAAG,IAAI,OAAO,CAC1B,CAAC,OAAO,EAAE,EAAE,CACV,CAAC,MAAM,GAAG,GAAG,EAAE;YACb,IAAI,QAAQ,EAAE,CAAC;gBACb,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;YAChE,CAAC;YACD,QAAQ,GAAG,IAAI,CAAC;YAChB,6FAAe,CAAC,MAAA,CAAC;YACjB,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CACL,CAAC;QAEF,uBAAA,IAAI,kBAAY,uBAAA,IAAI,sBAAS,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,MAAA,CAAC;QACnD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
package/src/utils.test.ts DELETED
@@ -1,47 +0,0 @@
1
- // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
- //
3
- // SPDX-License-Identifier: Apache-2.0
4
- import { describe, expect, it } from 'vitest';
5
- import { Mutex } from './utils';
6
-
7
- describe('Mutex', () => {
8
- it('should not be locked initially', () => {
9
- const mutex = new Mutex();
10
- expect(mutex.isLocked()).toBe(false);
11
- });
12
-
13
- it('should lock and unlock correctly', async () => {
14
- const mutex = new Mutex();
15
- const unlock = await mutex.lock();
16
- expect(mutex.isLocked()).toBe(true);
17
- unlock();
18
- expect(mutex.isLocked()).toBe(false);
19
- });
20
-
21
- it('should handle multiple locks', async () => {
22
- const mutex = new Mutex(2);
23
- const unlock1 = await mutex.lock();
24
- const unlock2 = await mutex.lock();
25
- expect(mutex.isLocked()).toBe(true);
26
- unlock1();
27
- expect(mutex.isLocked()).toBe(false);
28
- const unlock3 = await mutex.lock();
29
- expect(mutex.isLocked()).toBe(true);
30
- unlock2();
31
- expect(mutex.isLocked()).toBe(false);
32
- unlock3();
33
- expect(mutex.isLocked()).toBe(false);
34
- });
35
-
36
- it('should throw an error when unlocking the same lock twice', async () => {
37
- const mutex = new Mutex(2);
38
- const unlock1 = await mutex.lock();
39
- const unlock2 = await mutex.lock();
40
- expect(mutex.isLocked()).toBe(true);
41
- unlock1();
42
- expect(mutex.isLocked()).toBe(false);
43
- expect(() => unlock1()).toThrow('This unlock method has already been called');
44
- unlock2();
45
- expect(mutex.isLocked()).toBe(false);
46
- });
47
- });
package/src/utils.ts DELETED
@@ -1,44 +0,0 @@
1
- // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
- //
3
- // SPDX-License-Identifier: Apache-2.0
4
- export class Mutex {
5
- #locking: Promise<void>;
6
- #locks: number;
7
- #limit: number;
8
-
9
- constructor(limit = 1) {
10
- this.#locking = Promise.resolve();
11
- this.#locks = 0;
12
- this.#limit = limit;
13
- }
14
-
15
- isLocked(): boolean {
16
- return this.#locks >= this.#limit;
17
- }
18
-
19
- async lock(): Promise<() => void> {
20
- if (this.#locks >= this.#limit) {
21
- await this.#locking;
22
- }
23
-
24
- this.#locks += 1;
25
-
26
- let unlock: () => void;
27
- let unlocked = false;
28
-
29
- const willLock = new Promise<void>(
30
- (resolve) =>
31
- (unlock = () => {
32
- if (unlocked) {
33
- throw new Error('This unlock method has already been called');
34
- }
35
- unlocked = true;
36
- this.#locks -= 1;
37
- resolve();
38
- }),
39
- );
40
-
41
- this.#locking = this.#locking.then(() => willLock);
42
- return unlock;
43
- }
44
- }